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

Unified Diff: chrome/browser/extensions/active_script_controller.cc

Issue 270153004: Introduce ActiveScriptController; track active extension scripts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: UMA 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 side-by-side diff with in-line comments
Download patch
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..14f325daa9c083b1d431bd3592e2e5a6dd136495
--- /dev/null
+++ b/chrome/browser/extensions/active_script_controller.cc
@@ -0,0 +1,118 @@
+// 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/chrome_notification_types.h"
+#include "chrome/browser/extensions/extension_action.h"
+#include "chrome/common/extensions/api/extension_action/action_info.h"
+#include "content/public/browser/notification_service.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"
+#include "grit/theme_resources.h"
+#include "ui/base/resource/resource_bundle.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)
+ : LocationBarControllerProvider(web_contents),
+ enabled_(FeatureSwitch::active_script_enforcement()->IsEnabled()) {
not at google - send to devlin 2014/05/07 22:49:02 if it's not enabled then just don't create it. thi
Devlin 2014/05/08 18:15:46 Can't just not create it, since we need it for the
+}
+
+ActiveScriptController::~ActiveScriptController() {
+ LogActiveScriptUMA(extensions_executing_scripts_.size());
+}
+
+void ActiveScriptController::NotifyScriptExecuting(
+ const std::string& extension_id) {
+ if (!extensions_executing_scripts_.count(extension_id)) {
not at google - send to devlin 2014/05/07 22:49:02 nit: early return.
Devlin 2014/05/08 18:15:46 Done.
+ const Extension* extension =
+ GetExtensionRegistry()->enabled_extensions().GetByID(extension_id);
+ if (!extension ||
+ !PermissionsData::ShouldNotifyForScriptExecution(extension)) {
+ return;
+ }
+ extensions_executing_scripts_.insert(extension_id);
+ NotifyChange();
+ }
+}
+
+bool ActiveScriptController::HasActionForExtensionId(
not at google - send to devlin 2014/05/07 22:49:02 eventually we'll want to separate the metrics for
Devlin 2014/05/08 18:15:46 Agreed, but not at that point yet (since right now
+ const std::string& extension_id) {
+ return extensions_executing_scripts_.count(extension_id) > 0;
+}
+
+std::vector<ExtensionAction*> ActiveScriptController::GetCurrentActions() {
+ std::vector<ExtensionAction*> current_actions;
+ if (!enabled_)
+ return current_actions;
+
+ const ExtensionSet& extensions = GetExtensionRegistry()->enabled_extensions();
+
+ for (ExtensionSet::const_iterator iter = extensions.begin();
+ iter != extensions.end();
+ ++iter) {
+ if (extensions_executing_scripts_.count((*iter)->id()))
+ current_actions.push_back(GetActionForExtension(*iter));
+ }
+ return current_actions;
+}
+
+LocationBarController::Action ActiveScriptController::OnClicked(
+ const Extension* extension,
+ LocationBarController::MouseButton button) {
+ return LocationBarController::ACTION_NONE;
+}
+
+void ActiveScriptController::NavigatedMainFrame(
+ const content::LoadCommittedDetails& details,
+ const content::FrameNavigateParams& params) {
+ LogActiveScriptUMA(extensions_executing_scripts_.size());
+ extensions_executing_scripts_.clear();
not at google - send to devlin 2014/05/07 23:27:51 also need to make sure not to clear these on a has
Devlin 2014/05/08 18:15:46 Done.
+}
+
+ExtensionAction* ActiveScriptController::GetActionForExtension(
not at google - send to devlin 2014/05/07 22:49:02 nit: GetOrCreateExtensionAction
Devlin 2014/05/08 18:15:46 Moot.
+ const Extension* extension) {
+ ActiveScriptMap::iterator existing = script_badges_.find(extension->id());
+ if (existing != script_badges_.end())
+ return existing->second.get();
+
+ linked_ptr<ExtensionAction> badge(
+ new ExtensionAction(extension->id(),
+ ActionInfo::TYPE_ACTIVE_SCRIPT,
+ ActionInfo()));
+ badge->SetTitle(ExtensionAction::kDefaultTabId, extension->name());
+ badge->SetIsVisible(ExtensionAction::kDefaultTabId, true);
+
+ const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension);
not at google - send to devlin 2014/05/07 22:49:02 see above
Devlin 2014/05/08 18:15:46 Assuming you're referencing the "GetOrCreate" comm
+ 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));
+ badge->set_default_icon(icon_set.Pass());
not at google - send to devlin 2014/05/07 22:49:02 I swear we already have code somewhere to do this
Devlin 2014/05/08 18:15:46 It's similar to the code in the ctor for Extension
+ }
+
+ script_badges_[extension->id()] = badge;
+ return badge.get();
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698