Chromium Code Reviews| 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..19b5ef25ec296cf71236217be27bbfc55a553a26 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/active_script_controller.cc |
| @@ -0,0 +1,131 @@ |
| +// 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_messages.h" |
| +#include "extensions/common/extension_set.h" |
| +#include "extensions/common/feature_switch.h" |
| +#include "extensions/common/permissions/permissions_data.h" |
| +#include "ipc/ipc_message_macros.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) |
| + : content::WebContentsObserver(web_contents), |
| + web_contents_(web_contents), |
|
not at google - send to devlin
2014/05/08 23:33:19
WebContentsObserver has a web_contents() method, u
Devlin
2014/05/09 00:15:32
Done.
|
| + enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()) { |
| + LOG(WARNING) << "Constructed"; |
|
not at google - send to devlin
2014/05/08 23:33:19
remove this
Devlin
2014/05/09 00:15:32
D'oh, missed that one. Thanks.
|
| +} |
| + |
| +ActiveScriptController::~ActiveScriptController() { |
| + LogActiveScriptUMA(extensions_executing_scripts_.size()); |
| +} |
| + |
| +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_); |
| + } |
| +} |
| + |
| +void ActiveScriptController::OnAdInjectionDetected( |
| + const std::string& extension_id) { |
| + UMA_HISTOGRAM_BOOLEAN( |
|
not at google - send to devlin
2014/05/08 23:33:19
a couple of problems here:
- it would be useful to
Devlin
2014/05/09 00:15:32
Can't stream because we can't get a web contents (
|
| + "ExtensionActivity.AdInjectionCouldHaveBeenStopped", |
| + extensions_executing_scripts_.count(extension_id) > 0); |
| +} |
| + |
| +ExtensionAction* ActiveScriptController::GetActionForExtension( |
| + const Extension* extension) { |
| + if (!enabled_ || 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()) { |
| + action->set_default_icon( |
| + make_scoped_ptr(new ExtensionIconSet(action_info->default_icon))); |
| + } |
| + |
| + active_script_actions_[extension->id()] = action; |
| + return action.get(); |
| +} |
| + |
| +LocationBarController::Action ActiveScriptController::OnClicked( |
| + const Extension* extension) { |
| + DCHECK(extensions_executing_scripts_.count(extension->id()) > 0); |
| + return LocationBarController::ACTION_NONE; |
| +} |
| + |
| +void ActiveScriptController::OnNavigated() { |
| + LogActiveScriptUMA(extensions_executing_scripts_.size()); |
| + extensions_executing_scripts_.clear(); |
| +} |
| + |
| +bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) |
| + IPC_MESSAGE_HANDLER(ExtensionHostMsg_NotifyExtensionScriptExecution, |
| + OnNotifyExtensionScriptExecution) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void ActiveScriptController::OnNotifyExtensionScriptExecution( |
| + const std::string& extension_id, |
| + int page_id) { |
| + if (!Extension::IdIsValid(extension_id)) { |
| + NOTREACHED(); |
|
not at google - send to devlin
2014/05/08 23:33:19
<< "\"" << extension_id << "\" is not a valid ID";
Devlin
2014/05/09 00:15:32
Done.
|
| + return; |
| + } |
| + NotifyScriptExecuting(extension_id, page_id); |
| +} |
| + |
| +} // namespace extensions |