Index: chrome/browser/extensions/active_script_controller.cc |
diff --git a/chrome/browser/extensions/active_script_controller.cc b/chrome/browser/extensions/active_script_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..53db5b39d674a2e26c9d6a0b6b0017712d7d4abe |
--- /dev/null |
+++ b/chrome/browser/extensions/active_script_controller.cc |
@@ -0,0 +1,105 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/active_script_controller.h" |
+ |
+#include "base/metrics/histogram.h" |
+#include "chrome/browser/extensions/extension_action.h" |
+#include "chrome/common/extensions/api/extension_action/action_info.h" |
+#include "content/public/browser/navigation_controller.h" |
+#include "content/public/browser/navigation_entry.h" |
+#include "content/public/browser/web_contents.h" |
+#include "extensions/browser/extension_registry.h" |
+#include "extensions/common/extension.h" |
+#include "extensions/common/extension_set.h" |
+#include "extensions/common/feature_switch.h" |
+#include "extensions/common/permissions/permissions_data.h" |
+ |
+namespace extensions { |
+ |
+namespace { |
+ |
+void LogActiveScriptUMA(size_t number_of_active_scripts) { |
+ UMA_HISTOGRAM_COUNTS_100( |
+ "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", |
+ number_of_active_scripts); |
+} |
+ |
+} |
+ |
+ActiveScriptController::ActiveScriptController( |
+ content::WebContents* web_contents) |
+ : web_contents_(web_contents), |
+ enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()) { |
+} |
+ |
+ActiveScriptController::~ActiveScriptController() { |
+ LogActiveScriptUMA(extensions_executing_scripts_.size()); |
not at google - send to devlin
2014/05/08 20:47:09
you should only log UMA if it's enabled
Devlin
2014/05/08 23:01:00
Free (still good data) even if not enabled.
|
+} |
+ |
+void ActiveScriptController::NotifyScriptExecuting( |
+ const std::string& extension_id, int page_id) { |
+ if (extensions_executing_scripts_.count(extension_id) || |
+ web_contents_->GetController().GetVisibleEntry()->GetPageID() |
+ != page_id) { |
+ return; |
+ } |
+ |
+ const Extension* extension = |
+ ExtensionRegistry::Get(web_contents_->GetBrowserContext()) |
+ ->enabled_extensions().GetByID(extension_id); |
+ if (extension && |
+ PermissionsData::RequiresActionForScriptExecution(extension)) { |
+ extensions_executing_scripts_.insert(extension_id); |
+ LocationBarController::NotifyChange(web_contents_); |
+ } |
+} |
+ |
+bool ActiveScriptController::HasActionForExtensionId( |
+ const std::string& extension_id) { |
+ return extensions_executing_scripts_.count(extension_id) > 0; |
not at google - send to devlin
2014/05/08 20:47:09
If this isn't |enabled_| then you'll be giving mis
|
+} |
+ |
+ExtensionAction* ActiveScriptController::GetActionForExtension( |
+ const Extension* extension) { |
not at google - send to devlin
2014/05/08 20:47:09
check |enabled_|
Devlin
2014/05/08 23:01:00
Done.
|
+ if (extensions_executing_scripts_.count(extension->id()) == 0) |
+ return NULL; // No action for this extension. |
+ |
+ ActiveScriptMap::iterator existing = |
+ active_script_actions_.find(extension->id()); |
+ if (existing != active_script_actions_.end()) |
+ return existing->second.get(); |
+ |
+ linked_ptr<ExtensionAction> action( |
+ new ExtensionAction(extension->id(), |
+ ActionInfo::TYPE_PAGE, |
+ ActionInfo())); |
+ action->SetTitle(ExtensionAction::kDefaultTabId, extension->name()); |
+ action->SetIsVisible(ExtensionAction::kDefaultTabId, true); |
+ |
+ const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension); |
+ if (!action_info) |
+ action_info = ActionInfo::GetBrowserActionInfo(extension); |
+ |
+ if (action_info && !action_info->default_icon.empty()) { |
+ scoped_ptr<ExtensionIconSet> icon_set( |
+ new ExtensionIconSet(action_info->default_icon)); |
+ action->set_default_icon(icon_set.Pass()); |
not at google - send to devlin
2014/05/08 20:47:09
easier on the eyes:
action->set_default_icon(
Devlin
2014/05/08 23:01:00
Done.
|
+ } |
+ |
+ active_script_actions_[extension->id()] = action; |
+ return action.get(); |
+} |
+ |
+LocationBarController::Action ActiveScriptController::OnClicked( |
+ const Extension* extension) { |
not at google - send to devlin
2014/05/08 20:47:09
DCHECK(enabled_)
Devlin
2014/05/08 23:01:00
We can get OnClicked even if not enabled, since th
|
+ return LocationBarController::ACTION_NONE; |
+} |
+ |
+void ActiveScriptController::OnNavigated() { |
+ LogActiveScriptUMA(extensions_executing_scripts_.size()); |
not at google - send to devlin
2014/05/08 20:47:09
only log UMA if enable
Generally there is so much
Devlin
2014/05/08 23:01:00
UMA if not enabled is fine. Only need to check en
|
+ extensions_executing_scripts_.clear(); |
+} |
+ |
+} // namespace extensions |