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

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: Kalman's 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
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& 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 (url.empty() || url == content::kAboutBlankURL ||
170 url == chrome::kChromeUINewTabURL) 177 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
183 // |web_contents| can be NULL in unit tests.
184 TabHelper* tab_helper =
185 web_contents ? TabHelper::FromWebContents(web_contents) : NULL;
186 ActiveScriptController* active_script_controller =
187 tab_helper && tab_helper->location_bar_controller() ?
188 tab_helper->location_bar_controller()->active_script_controller() :
189 NULL;
176 SiteMap::iterator site_lookup = url_status_.find(url); 190 SiteMap::iterator site_lookup = url_status_.find(url);
177 ExtensionMap exts = site_lookup->second; 191 const ExtensionMap& exts = site_lookup->second;
178 ExtensionMap::iterator ext_iter; 192 for (ExtensionMap::const_iterator ext_iter = exts.begin();
179 for (ext_iter = exts.begin(); ext_iter != exts.end(); ++ext_iter) { 193 ext_iter != exts.end();
194 ++ext_iter) {
180 if (ext_iter->first == kNumberOfTabs) 195 if (ext_iter->first == kNumberOfTabs)
181 continue; 196 continue;
182 for (int i = NONE + 1; i < MAX_STATUS; ++i) { 197 for (int i = NONE + 1; i < MAX_STATUS; ++i) {
183 if (ext_iter->second & (1 << i)) 198 if (ext_iter->second & (1 << i))
184 statuses[i-1]++; 199 statuses[i-1]++;
185 } 200 }
201
202 if ((ext_iter->second & kAnyAdActivity) &&
203 active_script_controller) {
204 bool injection_could_have_been_stopped =
205 active_script_controller->HasActionForExtensionId(ext_iter->first);
not at google - send to devlin 2014/05/08 20:47:09 for the enabled_ problem I mentioned in the contro
Devlin 2014/05/08 23:01:00 Done.
206 UMA_HISTOGRAM_BOOLEAN(
207 "ExtensionActivity.AdInjectionCouldHaveBeenStopped",
208 injection_could_have_been_stopped);
209 }
186 } 210 }
187 211
188 std::string prefix = "ExtensionActivity."; 212 std::string prefix = "ExtensionActivity.";
189 if (GURL(url).host() != "www.google.com") { 213 if (GURL(url).host() != "www.google.com") {
190 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT), 214 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT),
191 statuses[CONTENT_SCRIPT - 1]); 215 statuses[CONTENT_SCRIPT - 1]);
192 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(READ_DOM), 216 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(READ_DOM),
193 statuses[READ_DOM - 1]); 217 statuses[READ_DOM - 1]);
194 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(MODIFIED_DOM), 218 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(MODIFIED_DOM),
195 statuses[MODIFIED_DOM - 1]); 219 statuses[MODIFIED_DOM - 1]);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 int32 tab_id = SessionID::IdForTab(contents); 309 int32 tab_id = SessionID::IdForTab(contents);
286 310
287 std::map<int32, std::string>::iterator tab_it = tab_list_.find(tab_id); 311 std::map<int32, std::string>::iterator tab_it = tab_list_.find(tab_id);
288 312
289 // Ignore tabs that haven't changed status. 313 // Ignore tabs that haven't changed status.
290 if (tab_it != tab_list_.end() && tab_it->second == url) 314 if (tab_it != tab_list_.end() && tab_it->second == url)
291 return; 315 return;
292 316
293 // Is this an existing tab whose URL has changed. 317 // Is this an existing tab whose URL has changed.
294 if (tab_it != tab_list_.end()) { 318 if (tab_it != tab_list_.end()) {
295 CleanupClosedPage(tab_it->second); 319 CleanupClosedPage(tab_it->second, contents);
296 tab_list_.erase(tab_id); 320 tab_list_.erase(tab_id);
297 } 321 }
298 322
299 // Check that tab_list_ isn't over the kMaxTabsTracked budget. 323 // Check that tab_list_ isn't over the kMaxTabsTracked budget.
300 if (tab_list_.size() >= kMaxTabsTracked) 324 if (tab_list_.size() >= kMaxTabsTracked)
301 return; 325 return;
302 326
303 // Set up the new entries. 327 // Set up the new entries.
304 tab_list_[tab_id] = url; 328 tab_list_[tab_id] = url;
305 SetupOpenedPage(url); 329 SetupOpenedPage(url);
306 } 330 }
307 331
308 // Use the value from SessionID::IdForTab, *not* |index|. |index| will be 332 // Use the value from SessionID::IdForTab, *not* |index|. |index| will be
309 // duplicated across tabs in a session, whereas IdForTab uniquely identifies 333 // duplicated across tabs in a session, whereas IdForTab uniquely identifies
310 // each tab. 334 // each tab.
311 void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model, 335 void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model,
312 content::WebContents* contents, 336 content::WebContents* contents,
313 int index) { 337 int index) {
314 if (!contents) 338 if (!contents)
315 return; 339 return;
316 std::string url = CleanURL(contents->GetLastCommittedURL()); 340 std::string url = CleanURL(contents->GetLastCommittedURL());
317 int32 tab_id = SessionID::IdForTab(contents); 341 int32 tab_id = SessionID::IdForTab(contents);
318 std::map<int, std::string>::iterator tab_it = tab_list_.find(tab_id); 342 std::map<int, std::string>::iterator tab_it = tab_list_.find(tab_id);
319 if (tab_it != tab_list_.end()) 343 if (tab_it != tab_list_.end())
320 tab_list_.erase(tab_id); 344 tab_list_.erase(tab_id);
321 345
322 CleanupClosedPage(url); 346 CleanupClosedPage(url, contents);
323 } 347 }
324 348
325 void UmaPolicy::SetupOpenedPage(const std::string& url) { 349 void UmaPolicy::SetupOpenedPage(const std::string& url) {
326 url_status_[url][kNumberOfTabs]++; 350 url_status_[url][kNumberOfTabs]++;
327 } 351 }
328 352
329 void UmaPolicy::CleanupClosedPage(const std::string& url) { 353 void UmaPolicy::CleanupClosedPage(const std::string& url,
354 content::WebContents* web_contents) {
330 SiteMap::iterator old_site_lookup = url_status_.find(url); 355 SiteMap::iterator old_site_lookup = url_status_.find(url);
331 if (old_site_lookup == url_status_.end()) 356 if (old_site_lookup == url_status_.end())
332 return; 357 return;
333 old_site_lookup->second[kNumberOfTabs]--; 358 old_site_lookup->second[kNumberOfTabs]--;
334 if (old_site_lookup->second[kNumberOfTabs] == 0) { 359 if (old_site_lookup->second[kNumberOfTabs] == 0) {
335 HistogramOnClose(url); 360 HistogramOnClose(url, web_contents);
336 url_status_.erase(url); 361 url_status_.erase(url);
337 } 362 }
338 } 363 }
339 364
340 // Helpers. -------------------------------------------------------------------- 365 // Helpers. --------------------------------------------------------------------
341 366
342 // We don't want to treat # ref navigations as if they were new pageloads. 367 // 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. 368 // 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. 369 // We convert to a string in the hopes that this is faster than Replacements.
345 std::string UmaPolicy::CleanURL(const GURL& gurl) { 370 std::string UmaPolicy::CleanURL(const GURL& gurl) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 return "AdReplaced"; 424 return "AdReplaced";
400 case NONE: 425 case NONE:
401 case MAX_STATUS: 426 case MAX_STATUS:
402 default: 427 default:
403 NOTREACHED(); 428 NOTREACHED();
404 return ""; 429 return "";
405 } 430 }
406 } 431 }
407 432
408 } // namespace extensions 433 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698