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 namespace { | |
24 | |
25 void LogActiveScriptUMA(size_t number_of_active_scripts) { | |
26 UMA_HISTOGRAM_COUNTS_100( | |
27 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", | |
28 number_of_active_scripts); | |
29 } | |
30 | |
31 } | |
32 | |
33 ActiveScriptController::ActiveScriptController( | |
34 content::WebContents* web_contents) | |
35 : content::WebContentsObserver(web_contents), | |
36 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.
| |
37 enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()) { | |
38 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.
| |
39 } | |
40 | |
41 ActiveScriptController::~ActiveScriptController() { | |
42 LogActiveScriptUMA(extensions_executing_scripts_.size()); | |
43 } | |
44 | |
45 void ActiveScriptController::NotifyScriptExecuting( | |
46 const std::string& extension_id, int page_id) { | |
47 if (extensions_executing_scripts_.count(extension_id) || | |
48 web_contents_->GetController().GetVisibleEntry()->GetPageID() | |
49 != page_id) { | |
50 return; | |
51 } | |
52 | |
53 const Extension* extension = | |
54 ExtensionRegistry::Get(web_contents_->GetBrowserContext()) | |
55 ->enabled_extensions().GetByID(extension_id); | |
56 if (extension && | |
57 PermissionsData::RequiresActionForScriptExecution(extension)) { | |
58 extensions_executing_scripts_.insert(extension_id); | |
59 LocationBarController::NotifyChange(web_contents_); | |
60 } | |
61 } | |
62 | |
63 void ActiveScriptController::OnAdInjectionDetected( | |
64 const std::string& extension_id) { | |
65 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 (
| |
66 "ExtensionActivity.AdInjectionCouldHaveBeenStopped", | |
67 extensions_executing_scripts_.count(extension_id) > 0); | |
68 } | |
69 | |
70 ExtensionAction* ActiveScriptController::GetActionForExtension( | |
71 const Extension* extension) { | |
72 if (!enabled_ || extensions_executing_scripts_.count(extension->id()) == 0) | |
73 return NULL; // No action for this extension. | |
74 | |
75 ActiveScriptMap::iterator existing = | |
76 active_script_actions_.find(extension->id()); | |
77 if (existing != active_script_actions_.end()) | |
78 return existing->second.get(); | |
79 | |
80 linked_ptr<ExtensionAction> action( | |
81 new ExtensionAction(extension->id(), | |
82 ActionInfo::TYPE_PAGE, | |
83 ActionInfo())); | |
84 action->SetTitle(ExtensionAction::kDefaultTabId, extension->name()); | |
85 action->SetIsVisible(ExtensionAction::kDefaultTabId, true); | |
86 | |
87 const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension); | |
88 if (!action_info) | |
89 action_info = ActionInfo::GetBrowserActionInfo(extension); | |
90 | |
91 if (action_info && !action_info->default_icon.empty()) { | |
92 action->set_default_icon( | |
93 make_scoped_ptr(new ExtensionIconSet(action_info->default_icon))); | |
94 } | |
95 | |
96 active_script_actions_[extension->id()] = action; | |
97 return action.get(); | |
98 } | |
99 | |
100 LocationBarController::Action ActiveScriptController::OnClicked( | |
101 const Extension* extension) { | |
102 DCHECK(extensions_executing_scripts_.count(extension->id()) > 0); | |
103 return LocationBarController::ACTION_NONE; | |
104 } | |
105 | |
106 void ActiveScriptController::OnNavigated() { | |
107 LogActiveScriptUMA(extensions_executing_scripts_.size()); | |
108 extensions_executing_scripts_.clear(); | |
109 } | |
110 | |
111 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { | |
112 bool handled = true; | |
113 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) | |
114 IPC_MESSAGE_HANDLER(ExtensionHostMsg_NotifyExtensionScriptExecution, | |
115 OnNotifyExtensionScriptExecution) | |
116 IPC_MESSAGE_UNHANDLED(handled = false) | |
117 IPC_END_MESSAGE_MAP() | |
118 return handled; | |
119 } | |
120 | |
121 void ActiveScriptController::OnNotifyExtensionScriptExecution( | |
122 const std::string& extension_id, | |
123 int page_id) { | |
124 if (!Extension::IdIsValid(extension_id)) { | |
125 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.
| |
126 return; | |
127 } | |
128 NotifyScriptExecuting(extension_id, page_id); | |
129 } | |
130 | |
131 } // namespace extensions | |
OLD | NEW |