Chromium Code Reviews| 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 "chrome/browser/extensions/extension_action.h" | |
| 9 #include "chrome/common/extensions/api/extension_action/action_info.h" | |
| 10 #include "content/public/browser/navigation_controller.h" | |
| 11 #include "content/public/browser/navigation_entry.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 #include "extensions/browser/extension_registry.h" | |
| 14 #include "extensions/common/extension.h" | |
| 15 #include "extensions/common/extension_messages.h" | |
| 16 #include "extensions/common/extension_set.h" | |
| 17 #include "extensions/common/feature_switch.h" | |
| 18 #include "extensions/common/permissions/permissions_data.h" | |
| 19 #include "ipc/ipc_message_macros.h" | |
| 20 | |
| 21 namespace extensions { | |
| 22 | |
| 23 ActiveScriptController::ActiveScriptController( | |
| 24 content::WebContents* web_contents) | |
| 25 : content::WebContentsObserver(web_contents), | |
| 26 enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()) { | |
| 27 } | |
| 28 | |
| 29 ActiveScriptController::~ActiveScriptController() { | |
| 30 LogUMA(); | |
| 31 } | |
| 32 | |
| 33 void ActiveScriptController::NotifyScriptExecuting( | |
| 34 const std::string& extension_id, int page_id) { | |
| 35 if (extensions_executing_scripts_.count(extension_id) || | |
| 36 web_contents()->GetController().GetVisibleEntry()->GetPageID() | |
| 37 != page_id) { | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 const Extension* extension = | |
| 42 ExtensionRegistry::Get(web_contents()->GetBrowserContext()) | |
| 43 ->enabled_extensions().GetByID(extension_id); | |
| 44 if (extension && | |
| 45 PermissionsData::RequiresActionForScriptExecution(extension)) { | |
| 46 extensions_executing_scripts_.insert(extension_id); | |
| 47 LocationBarController::NotifyChange(web_contents()); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 void ActiveScriptController::OnAdInjectionDetected( | |
| 52 const std::string& extension_id) { | |
| 53 ad_injectors_.insert(extension_id); | |
| 54 } | |
| 55 | |
| 56 ExtensionAction* ActiveScriptController::GetActionForExtension( | |
| 57 const Extension* extension) { | |
| 58 if (!enabled_ || extensions_executing_scripts_.count(extension->id()) == 0) | |
| 59 return NULL; // No action for this extension. | |
| 60 | |
| 61 ActiveScriptMap::iterator existing = | |
| 62 active_script_actions_.find(extension->id()); | |
| 63 if (existing != active_script_actions_.end()) | |
| 64 return existing->second.get(); | |
| 65 | |
| 66 linked_ptr<ExtensionAction> action( | |
| 67 new ExtensionAction(extension->id(), | |
| 68 ActionInfo::TYPE_PAGE, | |
| 69 ActionInfo())); | |
| 70 action->SetTitle(ExtensionAction::kDefaultTabId, extension->name()); | |
| 71 action->SetIsVisible(ExtensionAction::kDefaultTabId, true); | |
| 72 | |
| 73 const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension); | |
| 74 if (!action_info) | |
| 75 action_info = ActionInfo::GetBrowserActionInfo(extension); | |
| 76 | |
| 77 if (action_info && !action_info->default_icon.empty()) { | |
| 78 action->set_default_icon( | |
| 79 make_scoped_ptr(new ExtensionIconSet(action_info->default_icon))); | |
| 80 } | |
| 81 | |
| 82 active_script_actions_[extension->id()] = action; | |
| 83 return action.get(); | |
| 84 } | |
| 85 | |
| 86 LocationBarController::Action ActiveScriptController::OnClicked( | |
| 87 const Extension* extension) { | |
| 88 DCHECK(extensions_executing_scripts_.count(extension->id()) > 0); | |
| 89 return LocationBarController::ACTION_NONE; | |
| 90 } | |
| 91 | |
| 92 void ActiveScriptController::OnNavigated() { | |
| 93 LogUMA(); | |
| 94 extensions_executing_scripts_.clear(); | |
| 95 } | |
| 96 | |
| 97 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { | |
| 98 bool handled = true; | |
| 99 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) | |
| 100 IPC_MESSAGE_HANDLER(ExtensionHostMsg_NotifyExtensionScriptExecution, | |
| 101 OnNotifyExtensionScriptExecution) | |
| 102 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 103 IPC_END_MESSAGE_MAP() | |
| 104 return handled; | |
| 105 } | |
| 106 | |
| 107 void ActiveScriptController::OnNotifyExtensionScriptExecution( | |
| 108 const std::string& extension_id, | |
| 109 int page_id) { | |
| 110 if (!Extension::IdIsValid(extension_id)) { | |
| 111 NOTREACHED() << "'" << extension_id << "' is not a valid id."; | |
| 112 return; | |
| 113 } | |
| 114 NotifyScriptExecuting(extension_id, page_id); | |
| 115 } | |
| 116 | |
| 117 void ActiveScriptController::LogUMA() const { | |
| 118 UMA_HISTOGRAM_COUNTS_100( | |
| 119 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", | |
| 120 extensions_executing_scripts_.size()); | |
| 121 | |
| 122 size_t num_preventable_ad_injectors = 0u; | |
| 123 for (std::set<std::string>::const_iterator iter = ad_injectors_.begin(); | |
| 124 iter != ad_injectors_.end(); | |
| 125 ++iter) { | |
| 126 num_preventable_ad_injectors += extensions_executing_scripts_.count(*iter); | |
| 127 } | |
|
not at google - send to devlin
2014/05/09 00:22:59
easier:
size_t num_preventable_ad_injectors = STL
Devlin
2014/05/09 22:23:29
Done.
| |
| 128 | |
| 129 UMA_HISTOGRAM_COUNTS_100( | |
| 130 "Extensions.ActiveScriptController.PreventableAdInjectors", | |
| 131 num_preventable_ad_injectors); | |
|
not at google - send to devlin
2014/05/09 00:22:59
maybe it WOULD be nice to also log how many wouldn
Ilya Sherman
2014/05/09 22:08:43
Sorry, missed this question before. Either way is
Devlin
2014/05/09 22:23:29
Added it, to save us from having to add up all dif
| |
| 132 } | |
| 133 | |
| 134 } // namespace extensions | |
| OLD | NEW |