Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: chrome/browser/extensions/activity_log/uma_policy.cc

Issue 270153004: Introduce ActiveScriptController; track active extension scripts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/activity_log/uma_policy.h" 5 #include "chrome/browser/extensions/activity_log/uma_policy.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/extensions/active_script_controller.h"
10 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" 11 #include "chrome/browser/extensions/activity_log/activity_action_constants.h"
11 #include "chrome/browser/extensions/activity_log/ad_network_database.h" 12 #include "chrome/browser/extensions/activity_log/ad_network_database.h"
13 #include "chrome/browser/extensions/location_bar_controller.h"
14 #include "chrome/browser/extensions/tab_helper.h"
12 #include "chrome/browser/sessions/session_id.h" 15 #include "chrome/browser/sessions/session_id.h"
13 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_list.h" 17 #include "chrome/browser/ui/browser_list.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/url_constants.h" 21 #include "content/public/common/url_constants.h"
19 #include "extensions/common/dom_action_types.h" 22 #include "extensions/common/dom_action_types.h"
20 23
21 namespace extensions { 24 namespace extensions {
(...skipping 12 matching lines...) Expand all
34 const int kCreatedIframe = 1 << UmaPolicy::CREATED_IFRAME; 37 const int kCreatedIframe = 1 << UmaPolicy::CREATED_IFRAME;
35 const int kCreatedDiv = 1 << UmaPolicy::CREATED_DIV; 38 const int kCreatedDiv = 1 << UmaPolicy::CREATED_DIV;
36 const int kCreatedLink = 1 << UmaPolicy::CREATED_LINK; 39 const int kCreatedLink = 1 << UmaPolicy::CREATED_LINK;
37 const int kCreatedInput = 1 << UmaPolicy::CREATED_INPUT; 40 const int kCreatedInput = 1 << UmaPolicy::CREATED_INPUT;
38 const int kCreatedEmbed = 1 << UmaPolicy::CREATED_EMBED; 41 const int kCreatedEmbed = 1 << UmaPolicy::CREATED_EMBED;
39 const int kCreatedObject = 1 << UmaPolicy::CREATED_OBJECT; 42 const int kCreatedObject = 1 << UmaPolicy::CREATED_OBJECT;
40 const int kAdInjected = 1 << UmaPolicy::AD_INJECTED; 43 const int kAdInjected = 1 << UmaPolicy::AD_INJECTED;
41 const int kAdRemoved = 1 << UmaPolicy::AD_REMOVED; 44 const int kAdRemoved = 1 << UmaPolicy::AD_REMOVED;
42 const int kAdReplaced = 1 << UmaPolicy::AD_REPLACED; 45 const int kAdReplaced = 1 << UmaPolicy::AD_REPLACED;
43 46
47 // A mask of all the ad injection flags.
48 const int kAnyAdActivity = kAdInjected | kAdRemoved | kAdReplaced;
49
44 } // namespace 50 } // namespace
45 51
46 // Class constants, also used in testing. -------------------------------------- 52 // Class constants, also used in testing. --------------------------------------
47 53
48 const char UmaPolicy::kNumberOfTabs[] = "num_tabs"; 54 const char UmaPolicy::kNumberOfTabs[] = "num_tabs";
49 const size_t UmaPolicy::kMaxTabsTracked = 50; 55 const size_t UmaPolicy::kMaxTabsTracked = 50;
50 56
51 // Setup and shutdown. --------------------------------------------------------- 57 // Setup and shutdown. ---------------------------------------------------------
52 58
53 UmaPolicy::UmaPolicy(Profile* profile) 59 UmaPolicy::UmaPolicy(Profile* profile)
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 case Action::INJECTION_REMOVED_AD: 158 case Action::INJECTION_REMOVED_AD:
153 ret_bit |= kAdRemoved; 159 ret_bit |= kAdRemoved;
154 break; 160 break;
155 case Action::INJECTION_REPLACED_AD: 161 case Action::INJECTION_REPLACED_AD:
156 ret_bit |= kAdReplaced; 162 ret_bit |= kAdReplaced;
157 break; 163 break;
158 case Action::NO_AD_INJECTION: 164 case Action::NO_AD_INJECTION:
159 break; 165 break;
160 case Action::NUM_INJECTION_TYPES: 166 case Action::NUM_INJECTION_TYPES:
161 NOTREACHED(); 167 NOTREACHED();
162 }; 168 }
163 169
164 return ret_bit; 170 return ret_bit;
165 } 171 }
166 172
167 void UmaPolicy::HistogramOnClose(const std::string& url) { 173 void UmaPolicy::HistogramOnClose(const std::string& cleaned_url,
174 content::WebContents* web_contents) {
168 // Let's try to avoid histogramming useless URLs. 175 // Let's try to avoid histogramming useless URLs.
169 if (url.empty() || url == content::kAboutBlankURL || 176 if (cleaned_url.empty() || cleaned_url == content::kAboutBlankURL ||
170 url == chrome::kChromeUINewTabURL) 177 cleaned_url == chrome::kChromeUINewTabURL)
171 return; 178 return;
172 179
173 int statuses[MAX_STATUS - 1]; 180 int statuses[MAX_STATUS - 1];
174 std::memset(statuses, 0, sizeof(statuses)); 181 std::memset(statuses, 0, sizeof(statuses));
175 182
176 SiteMap::iterator site_lookup = url_status_.find(url); 183 // |web_contents| can be NULL in unit tests.
177 ExtensionMap exts = site_lookup->second; 184 TabHelper* tab_helper =
178 ExtensionMap::iterator ext_iter; 185 web_contents ? TabHelper::FromWebContents(web_contents) : NULL;
179 for (ext_iter = exts.begin(); ext_iter != exts.end(); ++ext_iter) { 186 ActiveScriptController* active_script_controller =
187 tab_helper && tab_helper->location_bar_controller() ?
188 tab_helper->location_bar_controller()->active_script_controller() :
189 NULL;
190 SiteMap::iterator site_lookup = url_status_.find(cleaned_url);
191 const ExtensionMap& exts = site_lookup->second;
192 std::vector<std::string> ad_injectors;
193 for (ExtensionMap::const_iterator ext_iter = exts.begin();
194 ext_iter != exts.end();
195 ++ext_iter) {
180 if (ext_iter->first == kNumberOfTabs) 196 if (ext_iter->first == kNumberOfTabs)
181 continue; 197 continue;
182 for (int i = NONE + 1; i < MAX_STATUS; ++i) { 198 for (int i = NONE + 1; i < MAX_STATUS; ++i) {
183 if (ext_iter->second & (1 << i)) 199 if (ext_iter->second & (1 << i))
184 statuses[i-1]++; 200 statuses[i-1]++;
185 } 201 }
202
203 if ((ext_iter->second & kAnyAdActivity) && active_script_controller)
204 ad_injectors.push_back(ext_iter->first);
186 } 205 }
206 if (active_script_controller)
207 active_script_controller->OnAdInjectionDetected(ad_injectors);
187 208
188 std::string prefix = "ExtensionActivity."; 209 std::string prefix = "ExtensionActivity.";
189 if (GURL(url).host() != "www.google.com") { 210 if (GURL(cleaned_url).host() != "www.google.com") {
190 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT), 211 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT),
191 statuses[CONTENT_SCRIPT - 1]); 212 statuses[CONTENT_SCRIPT - 1]);
192 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(READ_DOM), 213 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(READ_DOM),
193 statuses[READ_DOM - 1]); 214 statuses[READ_DOM - 1]);
194 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(MODIFIED_DOM), 215 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(MODIFIED_DOM),
195 statuses[MODIFIED_DOM - 1]); 216 statuses[MODIFIED_DOM - 1]);
196 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOM_METHOD), 217 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOM_METHOD),
197 statuses[DOM_METHOD - 1]); 218 statuses[DOM_METHOD - 1]);
198 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOCUMENT_WRITE), 219 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOCUMENT_WRITE),
199 statuses[DOCUMENT_WRITE - 1]); 220 statuses[DOCUMENT_WRITE - 1]);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 int32 tab_id = SessionID::IdForTab(contents); 306 int32 tab_id = SessionID::IdForTab(contents);
286 307
287 std::map<int32, std::string>::iterator tab_it = tab_list_.find(tab_id); 308 std::map<int32, std::string>::iterator tab_it = tab_list_.find(tab_id);
288 309
289 // Ignore tabs that haven't changed status. 310 // Ignore tabs that haven't changed status.
290 if (tab_it != tab_list_.end() && tab_it->second == url) 311 if (tab_it != tab_list_.end() && tab_it->second == url)
291 return; 312 return;
292 313
293 // Is this an existing tab whose URL has changed. 314 // Is this an existing tab whose URL has changed.
294 if (tab_it != tab_list_.end()) { 315 if (tab_it != tab_list_.end()) {
295 CleanupClosedPage(tab_it->second); 316 CleanupClosedPage(tab_it->second, contents);
296 tab_list_.erase(tab_id); 317 tab_list_.erase(tab_id);
297 } 318 }
298 319
299 // Check that tab_list_ isn't over the kMaxTabsTracked budget. 320 // Check that tab_list_ isn't over the kMaxTabsTracked budget.
300 if (tab_list_.size() >= kMaxTabsTracked) 321 if (tab_list_.size() >= kMaxTabsTracked)
301 return; 322 return;
302 323
303 // Set up the new entries. 324 // Set up the new entries.
304 tab_list_[tab_id] = url; 325 tab_list_[tab_id] = url;
305 SetupOpenedPage(url); 326 SetupOpenedPage(url);
306 } 327 }
307 328
308 // Use the value from SessionID::IdForTab, *not* |index|. |index| will be 329 // Use the value from SessionID::IdForTab, *not* |index|. |index| will be
309 // duplicated across tabs in a session, whereas IdForTab uniquely identifies 330 // duplicated across tabs in a session, whereas IdForTab uniquely identifies
310 // each tab. 331 // each tab.
311 void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model, 332 void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model,
312 content::WebContents* contents, 333 content::WebContents* contents,
313 int index) { 334 int index) {
314 if (!contents) 335 if (!contents)
315 return; 336 return;
316 std::string url = CleanURL(contents->GetLastCommittedURL()); 337 std::string url = CleanURL(contents->GetLastCommittedURL());
317 int32 tab_id = SessionID::IdForTab(contents); 338 int32 tab_id = SessionID::IdForTab(contents);
318 std::map<int, std::string>::iterator tab_it = tab_list_.find(tab_id); 339 std::map<int, std::string>::iterator tab_it = tab_list_.find(tab_id);
319 if (tab_it != tab_list_.end()) 340 if (tab_it != tab_list_.end())
320 tab_list_.erase(tab_id); 341 tab_list_.erase(tab_id);
321 342
322 CleanupClosedPage(url); 343 CleanupClosedPage(url, contents);
323 } 344 }
324 345
325 void UmaPolicy::SetupOpenedPage(const std::string& url) { 346 void UmaPolicy::SetupOpenedPage(const std::string& url) {
326 url_status_[url][kNumberOfTabs]++; 347 url_status_[url][kNumberOfTabs]++;
327 } 348 }
328 349
329 void UmaPolicy::CleanupClosedPage(const std::string& url) { 350 void UmaPolicy::CleanupClosedPage(const std::string& cleaned_url,
330 SiteMap::iterator old_site_lookup = url_status_.find(url); 351 content::WebContents* web_contents) {
352 SiteMap::iterator old_site_lookup = url_status_.find(cleaned_url);
331 if (old_site_lookup == url_status_.end()) 353 if (old_site_lookup == url_status_.end())
332 return; 354 return;
333 old_site_lookup->second[kNumberOfTabs]--; 355 old_site_lookup->second[kNumberOfTabs]--;
334 if (old_site_lookup->second[kNumberOfTabs] == 0) { 356 if (old_site_lookup->second[kNumberOfTabs] == 0) {
335 HistogramOnClose(url); 357 HistogramOnClose(cleaned_url, web_contents);
336 url_status_.erase(url); 358 url_status_.erase(cleaned_url);
337 } 359 }
338 } 360 }
339 361
340 // Helpers. -------------------------------------------------------------------- 362 // Helpers. --------------------------------------------------------------------
341 363
342 // We don't want to treat # ref navigations as if they were new pageloads. 364 // We don't want to treat # ref navigations as if they were new pageloads.
343 // So we get rid of the ref if it has it. 365 // So we get rid of the ref if it has it.
344 // We convert to a string in the hopes that this is faster than Replacements. 366 // We convert to a string in the hopes that this is faster than Replacements.
345 std::string UmaPolicy::CleanURL(const GURL& gurl) { 367 std::string UmaPolicy::CleanURL(const GURL& gurl) {
346 if (gurl.spec().empty()) 368 if (gurl.spec().empty())
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 return "AdReplaced"; 421 return "AdReplaced";
400 case NONE: 422 case NONE:
401 case MAX_STATUS: 423 case MAX_STATUS:
402 default: 424 default:
403 NOTREACHED(); 425 NOTREACHED();
404 return ""; 426 return "";
405 } 427 }
406 } 428 }
407 429
408 } // namespace extensions 430 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/activity_log/uma_policy.h ('k') | chrome/browser/extensions/activity_log/uma_policy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698