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 #include "chrome/browser/ui/browser_list_observer.h" 13 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 14 #include "url/gurl.h" 15 16 namespace extensions { 17 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 20 // closed. Caveats: 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 { 30 public: 31 // The possible status bits for a pageload. If you alter this, make sure to 32 // also update GetHistogramName. 33 enum PageStatus { 34 NONE = 0, 35 CONTENT_SCRIPT = 1, 36 READ_DOM, 37 MODIFIED_DOM, 38 DOM_METHOD, 39 DOCUMENT_WRITE, 40 INNER_HTML, 41 CREATED_SCRIPT, 42 CREATED_IFRAME, 43 CREATED_DIV, 44 CREATED_LINK, 45 CREATED_INPUT, 46 CREATED_EMBED, 47 CREATED_OBJECT, 48 AD_INJECTED, 49 AD_REMOVED, 50 AD_REPLACED, 51 AD_LIKELY_INJECTED, 52 AD_LIKELY_REPLACED, 53 MAX_STATUS // Insert new page statuses right before this one. 54 }; 55 56 explicit UmaPolicy(Profile* profile); 57 58 // ActivityLogPolicy implementation. 59 virtual void ProcessAction(scoped_refptr<Action> action) OVERRIDE; 60 virtual void Close() OVERRIDE; 61 62 // Gets the histogram name associated with each PageStatus. 63 static const char* GetHistogramName(PageStatus status); 64 65 protected: 66 // Run when Close() is called. 67 virtual ~UmaPolicy(); 68 69 private: 70 // Used as a special key in the ExtensionMap. 71 static const char kNumberOfTabs[]; 72 73 // The max number of tabs we track at a time. 74 static const size_t kMaxTabsTracked; 75 76 typedef std::map<std::string, int> ExtensionMap; 77 typedef std::map<std::string, ExtensionMap> SiteMap; 78 79 // BrowserListObserver 80 virtual void OnBrowserAdded(Browser* browser) OVERRIDE; 81 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE; 82 83 // TabStripModelObserver 84 // Fired when a page loads, either as a new tab or replacing the contents of 85 // an older tab. 86 virtual void TabChangedAt(content::WebContents* contents, 87 int index, 88 TabChangeType change_type) OVERRIDE; 89 // Fired when a tab closes. 90 virtual void TabClosingAt(TabStripModel* tab_strip_model, 91 content::WebContents* contents, 92 int index) OVERRIDE; 93 94 // Assign a status bitmask based on the action's properties. 95 int MatchActionToStatus(scoped_refptr<Action> action); 96 97 // When a page is opened, add it to the SiteMap url_status_. 98 void SetupOpenedPage(const std::string& url); 99 100 // When a page is closing, remove it from the SiteMap url_status_. 101 void CleanupClosedPage(const std::string& cleaned_url, 102 content::WebContents* web_contents); 103 104 // When a page is closing, save statistics about the page to histograms. 105 void HistogramOnClose(const std::string& cleaned_url, 106 content::WebContents* web_contents); 107 108 // Standardizes the way URLs are treated. 109 static std::string CleanURL(const GURL& gurl); 110 111 // Used by UmaPolicyTest.ProcessActionTest. 112 SiteMap url_status() { return url_status_; } 113 114 Profile* profile_; 115 116 // URL -> extension id -> page status. 117 SiteMap url_status_; 118 119 // tab index -> URL. 120 std::map<int32, std::string> tab_list_; 121 122 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, CleanURLTest); 123 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, MatchActionToStatusTest); 124 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, ProcessActionTest); 125 FRIEND_TEST_ALL_PREFIXES(UmaPolicyTest, SiteUrlTest); 126 }; 127 128 } // namespace extensions 129 130 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_UMA_POLICY_H_ 131