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

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 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& 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 for (ExtensionMap::const_iterator ext_iter = exts.begin();
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) && active_script_controller)
203 active_script_controller->OnAdInjectionDetected(ext_iter->first);
186 } 204 }
187 205
188 std::string prefix = "ExtensionActivity."; 206 std::string prefix = "ExtensionActivity.";
189 if (GURL(url).host() != "www.google.com") { 207 if (GURL(cleaned_url).host() != "www.google.com") {
190 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT), 208 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT),
191 statuses[CONTENT_SCRIPT - 1]); 209 statuses[CONTENT_SCRIPT - 1]);
192 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(READ_DOM), 210 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(READ_DOM),
193 statuses[READ_DOM - 1]); 211 statuses[READ_DOM - 1]);
194 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(MODIFIED_DOM), 212 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(MODIFIED_DOM),
195 statuses[MODIFIED_DOM - 1]); 213 statuses[MODIFIED_DOM - 1]);
196 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOM_METHOD), 214 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOM_METHOD),
197 statuses[DOM_METHOD - 1]); 215 statuses[DOM_METHOD - 1]);
198 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOCUMENT_WRITE), 216 UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(DOCUMENT_WRITE),
199 statuses[DOCUMENT_WRITE - 1]); 217 statuses[DOCUMENT_WRITE - 1]);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 int32 tab_id = SessionID::IdForTab(contents); 303 int32 tab_id = SessionID::IdForTab(contents);
286 304
287 std::map<int32, std::string>::iterator tab_it = tab_list_.find(tab_id); 305 std::map<int32, std::string>::iterator tab_it = tab_list_.find(tab_id);
288 306
289 // Ignore tabs that haven't changed status. 307 // Ignore tabs that haven't changed status.
290 if (tab_it != tab_list_.end() && tab_it->second == url) 308 if (tab_it != tab_list_.end() && tab_it->second == url)
291 return; 309 return;
292 310
293 // Is this an existing tab whose URL has changed. 311 // Is this an existing tab whose URL has changed.
294 if (tab_it != tab_list_.end()) { 312 if (tab_it != tab_list_.end()) {
295 CleanupClosedPage(tab_it->second); 313 CleanupClosedPage(tab_it->second, contents);
296 tab_list_.erase(tab_id); 314 tab_list_.erase(tab_id);
297 } 315 }
298 316
299 // Check that tab_list_ isn't over the kMaxTabsTracked budget. 317 // Check that tab_list_ isn't over the kMaxTabsTracked budget.
300 if (tab_list_.size() >= kMaxTabsTracked) 318 if (tab_list_.size() >= kMaxTabsTracked)
301 return; 319 return;
302 320
303 // Set up the new entries. 321 // Set up the new entries.
304 tab_list_[tab_id] = url; 322 tab_list_[tab_id] = url;
305 SetupOpenedPage(url); 323 SetupOpenedPage(url);
306 } 324 }
307 325
308 // Use the value from SessionID::IdForTab, *not* |index|. |index| will be 326 // Use the value from SessionID::IdForTab, *not* |index|. |index| will be
309 // duplicated across tabs in a session, whereas IdForTab uniquely identifies 327 // duplicated across tabs in a session, whereas IdForTab uniquely identifies
310 // each tab. 328 // each tab.
311 void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model, 329 void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model,
312 content::WebContents* contents, 330 content::WebContents* contents,
313 int index) { 331 int index) {
314 if (!contents) 332 if (!contents)
315 return; 333 return;
316 std::string url = CleanURL(contents->GetLastCommittedURL()); 334 std::string url = CleanURL(contents->GetLastCommittedURL());
317 int32 tab_id = SessionID::IdForTab(contents); 335 int32 tab_id = SessionID::IdForTab(contents);
318 std::map<int, std::string>::iterator tab_it = tab_list_.find(tab_id); 336 std::map<int, std::string>::iterator tab_it = tab_list_.find(tab_id);
319 if (tab_it != tab_list_.end()) 337 if (tab_it != tab_list_.end())
320 tab_list_.erase(tab_id); 338 tab_list_.erase(tab_id);
321 339
322 CleanupClosedPage(url); 340 CleanupClosedPage(url, contents);
323 } 341 }
324 342
325 void UmaPolicy::SetupOpenedPage(const std::string& url) { 343 void UmaPolicy::SetupOpenedPage(const std::string& url) {
326 url_status_[url][kNumberOfTabs]++; 344 url_status_[url][kNumberOfTabs]++;
327 } 345 }
328 346
329 void UmaPolicy::CleanupClosedPage(const std::string& url) { 347 void UmaPolicy::CleanupClosedPage(const std::string& cleaned_url,
330 SiteMap::iterator old_site_lookup = url_status_.find(url); 348 content::WebContents* web_contents) {
349 DCHECK_EQ(cleaned_url, CleanURL(GURL(cleaned_url)));
350 SiteMap::iterator old_site_lookup = url_status_.find(cleaned_url);
331 if (old_site_lookup == url_status_.end()) 351 if (old_site_lookup == url_status_.end())
332 return; 352 return;
333 old_site_lookup->second[kNumberOfTabs]--; 353 old_site_lookup->second[kNumberOfTabs]--;
334 if (old_site_lookup->second[kNumberOfTabs] == 0) { 354 if (old_site_lookup->second[kNumberOfTabs] == 0) {
335 HistogramOnClose(url); 355 HistogramOnClose(cleaned_url, web_contents);
336 url_status_.erase(url); 356 url_status_.erase(cleaned_url);
337 } 357 }
338 } 358 }
339 359
340 // Helpers. -------------------------------------------------------------------- 360 // Helpers. --------------------------------------------------------------------
341 361
342 // We don't want to treat # ref navigations as if they were new pageloads. 362 // 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. 363 // 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. 364 // We convert to a string in the hopes that this is faster than Replacements.
345 std::string UmaPolicy::CleanURL(const GURL& gurl) { 365 std::string UmaPolicy::CleanURL(const GURL& gurl) {
346 if (gurl.spec().empty()) 366 if (gurl.spec().empty())
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 return "AdReplaced"; 419 return "AdReplaced";
400 case NONE: 420 case NONE:
401 case MAX_STATUS: 421 case MAX_STATUS:
402 default: 422 default:
403 NOTREACHED(); 423 NOTREACHED();
404 return ""; 424 return "";
405 } 425 }
406 } 426 }
407 427
408 } // namespace extensions 428 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698