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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_UMA_POLICY_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_UMA_POLICY_H_
11 #include "chrome/browser/extensions/activity_log/activity_log_policy.h"
12 #include "chrome/browser/ui/browser_list_observer.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
16 namespace extensions
{
18 // The UmaPolicy keeps track of how many extensions have read from or modified
19 // a given pageload. UmaPolicy records to a histogram when a given tab is
21 // * If multiple tabs are open for the same URL at the same time, UmaPolicy
22 // treats them as if they are the same.
23 // * UmaPolicy does not record statistics for incognito tabs. (For privacy.)
24 // * If the number of tabs open exceeds 50, UmaPolicy stops recording stats
25 // for tabs 51+. (For memory.)
26 // * UmaPolicy only handles top frames; stats are not recorded for iframes.
27 class UmaPolicy
: public ActivityLogPolicy
,
28 public TabStripModelObserver
,
29 public chrome::BrowserListObserver
{
31 // The possible status bits for a pageload. If you alter this, make sure to
32 // also update GetHistogramName.
51 MAX_STATUS
// Insert new page statuses right before this one.
54 explicit UmaPolicy(Profile
* profile
);
56 // ActivityLogPolicy implementation.
57 virtual void ProcessAction(scoped_refptr
<Action
> action
) OVERRIDE
;
58 virtual void Close() OVERRIDE
;
60 // Gets the histogram name associated with each PageStatus.
61 static const char* GetHistogramName(PageStatus status
);
64 // Run when Close() is called.
68 // Used as a special key in the ExtensionMap.
69 static const char kNumberOfTabs
[];
71 // The max number of tabs we track at a time.
72 static const size_t kMaxTabsTracked
;
74 typedef std::map
<std::string
, int> ExtensionMap
;
75 typedef std::map
<std::string
, ExtensionMap
> SiteMap
;
77 // BrowserListObserver
78 virtual void OnBrowserAdded(Browser
* browser
) OVERRIDE
;
79 virtual void OnBrowserRemoved(Browser
* browser
) OVERRIDE
;
81 // TabStripModelObserver
82 // Fired when a page loads, either as a new tab or replacing the contents of
84 virtual void TabChangedAt(content::WebContents
* contents
,
86 TabChangeType change_type
) OVERRIDE
;
87 // Fired when a tab closes.
88 virtual void TabClosingAt(TabStripModel
* tab_strip_model
,
89 content::WebContents
* contents
,
92 // Assign a status bitmask based on the action's properties.
93 int MatchActionToStatus(scoped_refptr
<Action
> action
);
95 // When a page is opened, add it to the SiteMap url_status_.
96 void SetupOpenedPage(const std::string
& url
);
98 // When a page is closing, remove it from the SiteMap url_status_.
99 void CleanupClosedPage(const std::string
& url
);
101 // When a page is closing, save statistics about the page to histograms.
102 void HistogramOnClose(const std::string
& url
);
104 // Standardizes the way URLs are treated.
105 static std::string
CleanURL(const GURL
& gurl
);
107 // Used by UmaPolicyTest.ProcessActionTest.
108 SiteMap
url_status() { return url_status_
; }
112 // URL -> extension id -> page status.
116 std::map
<int32
, std::string
> tab_list_
;
118 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest
, CleanURLTest
);
119 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest
, MatchActionToStatusTest
);
120 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest
, ProcessActionTest
);
121 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest
, SiteUrlTest
);
124 } // namespace extensions
126 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_UMA_POLICY_H_