- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / uma_policy.h
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_UMA_POLICY_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_UMA_POLICY_H_
7
8 #include <map>
9 #include <string>
10
11 #include "chrome/browser/extensions/activity_log/activity_log_policy.h"
12
13 #include "chrome/browser/ui/browser_list_observer.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
15 #include "url/gurl.h"
16
17 namespace extensions {
18
19 // The UmaPolicy keeps track of how many extensions have read from or modified
20 // a given pageload. UmaPolicy records to a histogram when a given tab is
21 // closed. Caveats:
22 //   * If multiple tabs are open for the same URL at the same time, UmaPolicy
23 //     treats them as if they are the same.
24 //   * UmaPolicy does not record statistics for incognito tabs. (For privacy.)
25 //   * If the number of tabs open exceeds 50, UmaPolicy stops recording stats
26 //     for tabs 51+. (For memory.)
27 //   * UmaPolicy only handles top frames; stats are not recorded for iframes.
28 class UmaPolicy : public ActivityLogPolicy,
29                   public TabStripModelObserver,
30                   public chrome::BrowserListObserver {
31  public:
32   // The possible status bits for a pageload. If you alter this, make sure to
33   // also update GetHistogramName.
34   enum PageStatus {
35     NONE = 0,
36     CONTENT_SCRIPT = 1,
37     READ_DOM,
38     MODIFIED_DOM,
39     DOM_METHOD,
40     DOCUMENT_WRITE,
41     INNER_HTML,
42     CREATED_SCRIPT,
43     CREATED_IFRAME,
44     CREATED_DIV,
45     CREATED_LINK,
46     CREATED_INPUT,
47     CREATED_EMBED,
48     CREATED_OBJECT,
49     MAX_STATUS  // Insert new page statuses right before this one.
50   };
51
52   explicit UmaPolicy(Profile* profile);
53
54   virtual void ProcessAction(scoped_refptr<Action> action) OVERRIDE;
55   virtual void Close() OVERRIDE;
56
57   // Gets the histogram name associated with each PageStatus.
58   static const char* GetHistogramName(PageStatus status);
59
60  protected:
61   // Run when Close() is called.
62   virtual ~UmaPolicy();
63
64  private:
65   // Used as a special key in the ExtensionMap.
66   static const char kNumberOfTabs[];
67
68   // The max number of tabs we track at a time.
69   static const size_t kMaxTabsTracked;
70
71   typedef std::map<std::string, int> ExtensionMap;
72   typedef std::map<std::string, ExtensionMap> SiteMap;
73
74   // BrowserListObserver
75   virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
76   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
77
78   // TabStripModelObserver
79   // Fired when a page loads, either as a new tab or replacing the contents of
80   // an older tab.
81   virtual void TabChangedAt(content::WebContents* contents,
82                             int index,
83                             TabChangeType change_type) OVERRIDE;
84   // Fired when a tab closes.
85   virtual void TabClosingAt(TabStripModel* tab_strip_model,
86                             content::WebContents* contents,
87                             int index) OVERRIDE;
88
89   // Assign a status bitmask based on the action's properties.
90   int MatchActionToStatus(scoped_refptr<Action> action);
91
92   // When a page is opened, add it to the SiteMap url_status_.
93   void SetupOpenedPage(const std::string& url);
94
95   // When a page is closing, remove it from the SiteMap url_status_.
96   void CleanupClosedPage(const std::string& url);
97
98   // When a page is closing, save statistics about the page to histograms.
99   void HistogramOnClose(const std::string& url);
100
101   // Standardizes the way URLs are treated.
102   static std::string CleanURL(const GURL& gurl);
103
104   // Used by UmaPolicyTest.ProcessActionTest.
105   SiteMap url_status() { return url_status_; }
106
107   Profile* profile_;
108
109   // URL -> extension id -> page status.
110   SiteMap url_status_;
111
112   // tab index -> URL.
113   std::map<int32, std::string> tab_list_;
114
115   FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, CleanURLTest);
116   FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, MatchActionToStatusTest);
117   FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, ProcessActionTest);
118   FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, SiteUrlTest);
119 };
120
121 }  // namespace extensions
122
123 #endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_UMA_POLICY_H_