OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/active_script_controller.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "chrome/browser/extensions/extension_action.h" |
| 10 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 11 #include "content/public/browser/navigation_controller.h" |
| 12 #include "content/public/browser/navigation_entry.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "extensions/browser/extension_registry.h" |
| 15 #include "extensions/common/extension.h" |
| 16 #include "extensions/common/extension_messages.h" |
| 17 #include "extensions/common/extension_set.h" |
| 18 #include "extensions/common/feature_switch.h" |
| 19 #include "extensions/common/permissions/permissions_data.h" |
| 20 #include "ipc/ipc_message_macros.h" |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 ActiveScriptController::ActiveScriptController( |
| 25 content::WebContents* web_contents) |
| 26 : content::WebContentsObserver(web_contents), |
| 27 enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()) { |
| 28 } |
| 29 |
| 30 ActiveScriptController::~ActiveScriptController() { |
| 31 LogUMA(); |
| 32 } |
| 33 |
| 34 void ActiveScriptController::NotifyScriptExecuting( |
| 35 const std::string& extension_id, int page_id) { |
| 36 if (extensions_executing_scripts_.count(extension_id) || |
| 37 web_contents()->GetController().GetVisibleEntry()->GetPageID() != |
| 38 page_id) { |
| 39 return; |
| 40 } |
| 41 |
| 42 const Extension* extension = |
| 43 ExtensionRegistry::Get(web_contents()->GetBrowserContext()) |
| 44 ->enabled_extensions().GetByID(extension_id); |
| 45 if (extension && |
| 46 PermissionsData::RequiresActionForScriptExecution(extension)) { |
| 47 extensions_executing_scripts_.insert(extension_id); |
| 48 LocationBarController::NotifyChange(web_contents()); |
| 49 } |
| 50 } |
| 51 |
| 52 void ActiveScriptController::OnAdInjectionDetected( |
| 53 const std::vector<std::string> ad_injectors) { |
| 54 size_t num_preventable_ad_injectors = |
| 55 base::STLSetIntersection<std::set<std::string> >( |
| 56 ad_injectors, extensions_executing_scripts_).size(); |
| 57 |
| 58 UMA_HISTOGRAM_COUNTS_100( |
| 59 "Extensions.ActiveScriptController.PreventableAdInjectors", |
| 60 num_preventable_ad_injectors); |
| 61 UMA_HISTOGRAM_COUNTS_100( |
| 62 "Extensions.ActiveScriptController.PreventableAdInjectors", |
| 63 ad_injectors.size() - num_preventable_ad_injectors); |
| 64 } |
| 65 |
| 66 ExtensionAction* ActiveScriptController::GetActionForExtension( |
| 67 const Extension* extension) { |
| 68 if (!enabled_ || extensions_executing_scripts_.count(extension->id()) == 0) |
| 69 return NULL; // No action for this extension. |
| 70 |
| 71 ActiveScriptMap::iterator existing = |
| 72 active_script_actions_.find(extension->id()); |
| 73 if (existing != active_script_actions_.end()) |
| 74 return existing->second.get(); |
| 75 |
| 76 linked_ptr<ExtensionAction> action(new ExtensionAction( |
| 77 extension->id(), ActionInfo::TYPE_PAGE, ActionInfo())); |
| 78 action->SetTitle(ExtensionAction::kDefaultTabId, extension->name()); |
| 79 action->SetIsVisible(ExtensionAction::kDefaultTabId, true); |
| 80 |
| 81 const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension); |
| 82 if (!action_info) |
| 83 action_info = ActionInfo::GetBrowserActionInfo(extension); |
| 84 |
| 85 if (action_info && !action_info->default_icon.empty()) { |
| 86 action->set_default_icon( |
| 87 make_scoped_ptr(new ExtensionIconSet(action_info->default_icon))); |
| 88 } |
| 89 |
| 90 active_script_actions_[extension->id()] = action; |
| 91 return action.get(); |
| 92 } |
| 93 |
| 94 LocationBarController::Action ActiveScriptController::OnClicked( |
| 95 const Extension* extension) { |
| 96 DCHECK(extensions_executing_scripts_.count(extension->id()) > 0); |
| 97 return LocationBarController::ACTION_NONE; |
| 98 } |
| 99 |
| 100 void ActiveScriptController::OnNavigated() { |
| 101 LogUMA(); |
| 102 extensions_executing_scripts_.clear(); |
| 103 } |
| 104 |
| 105 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { |
| 106 bool handled = true; |
| 107 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) |
| 108 IPC_MESSAGE_HANDLER(ExtensionHostMsg_NotifyExtensionScriptExecution, |
| 109 OnNotifyExtensionScriptExecution) |
| 110 IPC_MESSAGE_UNHANDLED(handled = false) |
| 111 IPC_END_MESSAGE_MAP() |
| 112 return handled; |
| 113 } |
| 114 |
| 115 void ActiveScriptController::OnNotifyExtensionScriptExecution( |
| 116 const std::string& extension_id, |
| 117 int page_id) { |
| 118 if (!Extension::IdIsValid(extension_id)) { |
| 119 NOTREACHED() << "'" << extension_id << "' is not a valid id."; |
| 120 return; |
| 121 } |
| 122 NotifyScriptExecuting(extension_id, page_id); |
| 123 } |
| 124 |
| 125 void ActiveScriptController::LogUMA() const { |
| 126 UMA_HISTOGRAM_COUNTS_100( |
| 127 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", |
| 128 extensions_executing_scripts_.size()); |
| 129 } |
| 130 |
| 131 } // namespace extensions |
OLD | NEW |