Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / uma_policy.cc
index e94f781..f94ecc0 100644 (file)
@@ -7,16 +7,20 @@
 #include "base/metrics/histogram.h"
 #include "base/strings/stringprintf.h"
 #include "chrome/browser/browser_process.h"
+#include "chrome/browser/extensions/active_script_controller.h"
 #include "chrome/browser/extensions/activity_log/activity_action_constants.h"
 #include "chrome/browser/extensions/activity_log/ad_network_database.h"
-#include "chrome/browser/sessions/session_id.h"
+#include "chrome/browser/sessions/session_tab_helper.h"
 #include "chrome/browser/ui/browser.h"
 #include "chrome/browser/ui/browser_list.h"
 #include "chrome/browser/ui/tabs/tab_strip_model.h"
 #include "chrome/common/url_constants.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/common/url_constants.h"
+#include "extensions/browser/extension_registry.h"
 #include "extensions/common/dom_action_types.h"
+#include "extensions/common/extension.h"
+#include "extensions/common/manifest.h"
 
 namespace extensions {
 
@@ -40,6 +44,15 @@ const int kCreatedObject      = 1 << UmaPolicy::CREATED_OBJECT;
 const int kAdInjected         = 1 << UmaPolicy::AD_INJECTED;
 const int kAdRemoved          = 1 << UmaPolicy::AD_REMOVED;
 const int kAdReplaced         = 1 << UmaPolicy::AD_REPLACED;
+const int kAdLikelyInjected   = 1 << UmaPolicy::AD_LIKELY_INJECTED;
+const int kAdLikelyReplaced   = 1 << UmaPolicy::AD_LIKELY_REPLACED;
+
+// A mask of all the ad injection flags.
+const int kAnyAdActivity = kAdInjected |
+                           kAdRemoved |
+                           kAdReplaced |
+                           kAdLikelyInjected |
+                           kAdLikelyReplaced;
 
 }  // namespace
 
@@ -90,33 +103,30 @@ void UmaPolicy::ProcessAction(scoped_refptr<Action> action) {
 }
 
 int UmaPolicy::MatchActionToStatus(scoped_refptr<Action> action) {
-  if (action->action_type() == Action::ACTION_CONTENT_SCRIPT) {
+  if (action->action_type() == Action::ACTION_CONTENT_SCRIPT)
     return kContentScript;
-  } else if (action->action_type() == Action::ACTION_API_CALL &&
-             action->api_name() == "tabs.executeScript") {
+  if (action->action_type() == Action::ACTION_API_CALL &&
+      action->api_name() == "tabs.executeScript")
     return kContentScript;
-  } else if (action->action_type() != Action::ACTION_DOM_ACCESS) {
+  if (action->action_type() != Action::ACTION_DOM_ACCESS)
     return kNoStatus;
-  }
 
-  int dom_verb;
+  int dom_verb = DomActionType::MODIFIED;
   if (!action->other() ||
       !action->other()->GetIntegerWithoutPathExpansion(
-          activity_log_constants::kActionDomVerb, &dom_verb)) {
+          activity_log_constants::kActionDomVerb, &dom_verb))
     return kNoStatus;
-  }
 
   int ret_bit = kNoStatus;
   DomActionType::Type dom_type = static_cast<DomActionType::Type>(dom_verb);
   if (dom_type == DomActionType::GETTER)
     return kReadDom;
-  if (dom_type == DomActionType::SETTER) {
+  if (dom_type == DomActionType::SETTER)
     ret_bit |= kModifiedDom;
-  } else if (dom_type == DomActionType::METHOD) {
+  else if (dom_type == DomActionType::METHOD)
     ret_bit |= kDomMethod;
-  } else {
+  else
     return kNoStatus;
-  }
 
   if (action->api_name() == "HTMLDocument.write" ||
       action->api_name() == "HTMLDocument.writeln") {
@@ -126,21 +136,20 @@ int UmaPolicy::MatchActionToStatus(scoped_refptr<Action> action) {
   } else if (action->api_name() == "Document.createElement") {
     std::string arg;
     action->args()->GetString(0, &arg);
-    if (arg == "script") {
+    if (arg == "script")
       ret_bit |= kCreatedScript;
-    } else if (arg == "iframe") {
+    else if (arg == "iframe")
       ret_bit |= kCreatedIframe;
-    } else if (arg == "div") {
+    else if (arg == "div")
       ret_bit |= kCreatedDiv;
-    } else if (arg == "a") {
+    else if (arg == "a")
       ret_bit |= kCreatedLink;
-    } else if (arg == "input") {
+    else if (arg == "input")
       ret_bit |= kCreatedInput;
-    } else if (arg == "embed") {
+    else if (arg == "embed")
       ret_bit |= kCreatedEmbed;
-    } else if (arg == "object") {
+    else if (arg == "object")
       ret_bit |= kCreatedObject;
-    }
   }
 
   const Action::InjectionType ad_injection =
@@ -155,38 +164,67 @@ int UmaPolicy::MatchActionToStatus(scoped_refptr<Action> action) {
     case Action::INJECTION_REPLACED_AD:
       ret_bit |= kAdReplaced;
       break;
+    case Action::INJECTION_LIKELY_NEW_AD:
+      ret_bit |= kAdLikelyInjected;
+      break;
+    case Action::INJECTION_LIKELY_REPLACED_AD:
+      ret_bit |= kAdLikelyReplaced;
+      break;
     case Action::NO_AD_INJECTION:
       break;
     case Action::NUM_INJECTION_TYPES:
       NOTREACHED();
-  };
+  }
 
   return ret_bit;
 }
 
-void UmaPolicy::HistogramOnClose(const std::string& url) {
+void UmaPolicy::HistogramOnClose(const std::string& cleaned_url,
+                                 content::WebContents* web_contents) {
   // Let's try to avoid histogramming useless URLs.
-  if (url.empty() || url == content::kAboutBlankURL ||
-      url == chrome::kChromeUINewTabURL)
+  if (cleaned_url.empty() || cleaned_url == url::kAboutBlankURL ||
+      cleaned_url == chrome::kChromeUINewTabURL)
     return;
 
   int statuses[MAX_STATUS - 1];
   std::memset(statuses, 0, sizeof(statuses));
 
-  SiteMap::iterator site_lookup = url_status_.find(url);
-  ExtensionMap exts = site_lookup->second;
-  ExtensionMap::iterator ext_iter;
-  for (ext_iter = exts.begin(); ext_iter != exts.end(); ++ext_iter) {
+  ActiveScriptController* active_script_controller =
+      ActiveScriptController::GetForWebContents(web_contents);
+  SiteMap::iterator site_lookup = url_status_.find(cleaned_url);
+  const ExtensionMap& exts = site_lookup->second;
+  std::set<std::string> ad_injectors;
+  for (ExtensionMap::const_iterator ext_iter = exts.begin();
+       ext_iter != exts.end();
+       ++ext_iter) {
     if (ext_iter->first == kNumberOfTabs)
       continue;
     for (int i = NONE + 1; i < MAX_STATUS; ++i) {
       if (ext_iter->second & (1 << i))
         statuses[i-1]++;
     }
+
+    if (ext_iter->second & kAnyAdActivity)
+      ad_injectors.insert(ext_iter->first);
+  }
+  if (active_script_controller)
+    active_script_controller->OnAdInjectionDetected(ad_injectors);
+
+  ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
+  for (std::set<std::string>::const_iterator iter = ad_injectors.begin();
+       iter != ad_injectors.end();
+       ++iter) {
+    const Extension* extension =
+        registry->GetExtensionById(*iter, ExtensionRegistry::EVERYTHING);
+    if (extension) {
+      UMA_HISTOGRAM_ENUMERATION("Extensions.AdInjection.InstallLocation",
+                                extension->location(),
+                                Manifest::NUM_LOCATIONS);
+    }
   }
 
   std::string prefix = "ExtensionActivity.";
-  if (GURL(url).host() != "www.google.com") {
+  if (GURL(cleaned_url).host() != "www.google.com") {
     UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT),
                              statuses[CONTENT_SCRIPT - 1]);
     UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(READ_DOM),
@@ -219,6 +257,10 @@ void UmaPolicy::HistogramOnClose(const std::string& url) {
                              statuses[AD_REMOVED - 1]);
     UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(AD_REPLACED),
                              statuses[AD_REPLACED - 1]);
+    UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(AD_LIKELY_INJECTED),
+                             statuses[AD_LIKELY_INJECTED - 1]);
+    UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(AD_LIKELY_REPLACED),
+                             statuses[AD_LIKELY_REPLACED - 1]);
   } else {
     prefix += "Google.";
     UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(CONTENT_SCRIPT),
@@ -253,6 +295,10 @@ void UmaPolicy::HistogramOnClose(const std::string& url) {
                              statuses[AD_REMOVED - 1]);
     UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(AD_REPLACED),
                              statuses[AD_REPLACED - 1]);
+    UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(AD_LIKELY_INJECTED),
+                             statuses[AD_LIKELY_INJECTED - 1]);
+    UMA_HISTOGRAM_COUNTS_100(prefix + GetHistogramName(AD_LIKELY_REPLACED),
+                             statuses[AD_LIKELY_REPLACED - 1]);
   }
 }
 
@@ -270,7 +316,7 @@ void UmaPolicy::OnBrowserRemoved(Browser* browser) {
   browser->tab_strip_model()->RemoveObserver(this);
 }
 
-// Use the value from SessionID::IdForTab, *not* |index|. |index| will be
+// Use the value from SessionTabHelper::IdForTab, *not* |index|. |index| will be
 // duplicated across tabs in a session, whereas IdForTab uniquely identifies
 // each tab.
 void UmaPolicy::TabChangedAt(content::WebContents* contents,
@@ -282,7 +328,7 @@ void UmaPolicy::TabChangedAt(content::WebContents* contents,
     return;
 
   std::string url = CleanURL(contents->GetLastCommittedURL());
-  int32 tab_id = SessionID::IdForTab(contents);
+  int32 tab_id = SessionTabHelper::IdForTab(contents);
 
   std::map<int32, std::string>::iterator tab_it = tab_list_.find(tab_id);
 
@@ -292,7 +338,7 @@ void UmaPolicy::TabChangedAt(content::WebContents* contents,
 
   // Is this an existing tab whose URL has changed.
   if (tab_it != tab_list_.end()) {
-    CleanupClosedPage(tab_it->second);
+    CleanupClosedPage(tab_it->second, contents);
     tab_list_.erase(tab_id);
   }
 
@@ -305,7 +351,7 @@ void UmaPolicy::TabChangedAt(content::WebContents* contents,
   SetupOpenedPage(url);
 }
 
-// Use the value from SessionID::IdForTab, *not* |index|. |index| will be
+// Use the value from SessionTabHelper::IdForTab, *not* |index|. |index| will be
 // duplicated across tabs in a session, whereas IdForTab uniquely identifies
 // each tab.
 void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model,
@@ -314,26 +360,27 @@ void UmaPolicy::TabClosingAt(TabStripModel* tab_strip_model,
   if (!contents)
     return;
   std::string url = CleanURL(contents->GetLastCommittedURL());
-  int32 tab_id = SessionID::IdForTab(contents);
+  int32 tab_id = SessionTabHelper::IdForTab(contents);
   std::map<int, std::string>::iterator tab_it = tab_list_.find(tab_id);
   if (tab_it != tab_list_.end())
     tab_list_.erase(tab_id);
 
-  CleanupClosedPage(url);
+  CleanupClosedPage(url, contents);
 }
 
 void UmaPolicy::SetupOpenedPage(const std::string& url) {
   url_status_[url][kNumberOfTabs]++;
 }
 
-void UmaPolicy::CleanupClosedPage(const std::string& url) {
-  SiteMap::iterator old_site_lookup = url_status_.find(url);
+void UmaPolicy::CleanupClosedPage(const std::string& cleaned_url,
+                                  content::WebContents* web_contents) {
+  SiteMap::iterator old_site_lookup = url_status_.find(cleaned_url);
   if (old_site_lookup == url_status_.end())
     return;
   old_site_lookup->second[kNumberOfTabs]--;
   if (old_site_lookup->second[kNumberOfTabs] == 0) {
-    HistogramOnClose(url);
-    url_status_.erase(url);
+    HistogramOnClose(cleaned_url, web_contents);
+    url_status_.erase(cleaned_url);
   }
 }
 
@@ -344,7 +391,7 @@ void UmaPolicy::CleanupClosedPage(const std::string& url) {
 // We convert to a string in the hopes that this is faster than Replacements.
 std::string UmaPolicy::CleanURL(const GURL& gurl) {
   if (gurl.spec().empty())
-    return GURL(content::kAboutBlankURL).spec();
+    return GURL(url::kAboutBlankURL).spec();
   if (!gurl.is_valid())
     return gurl.spec();
   if (!gurl.has_ref())
@@ -397,6 +444,10 @@ const char* UmaPolicy::GetHistogramName(PageStatus status) {
       return "AdRemoved";
     case AD_REPLACED:
       return "AdReplaced";
+    case AD_LIKELY_INJECTED:
+      return "AdLikelyInjected";
+    case AD_LIKELY_REPLACED:
+      return "AdLikelyReplaced";
     case NONE:
     case MAX_STATUS:
     default: