OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/active_script_controller.h" | 5 #include "chrome/browser/extensions/active_script_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "extensions/common/extension_messages.h" | 30 #include "extensions/common/extension_messages.h" |
31 #include "extensions/common/extension_set.h" | 31 #include "extensions/common/extension_set.h" |
32 #include "extensions/common/feature_switch.h" | 32 #include "extensions/common/feature_switch.h" |
33 #include "extensions/common/manifest.h" | 33 #include "extensions/common/manifest.h" |
34 #include "extensions/common/permissions/permission_set.h" | 34 #include "extensions/common/permissions/permission_set.h" |
35 #include "extensions/common/permissions/permissions_data.h" | 35 #include "extensions/common/permissions/permissions_data.h" |
36 #include "ipc/ipc_message_macros.h" | 36 #include "ipc/ipc_message_macros.h" |
37 | 37 |
38 namespace extensions { | 38 namespace extensions { |
39 | 39 |
40 namespace { | |
41 | |
42 // Returns true if the extension should be regarded as a "permitted" extension | |
43 // for the case of metrics. We need this because we only actually withhold | |
44 // permissions if the switch is enabled, but want to record metrics in all | |
45 // cases. | |
46 // "ExtensionWouldHaveHadHostPermissionsWithheldIfSwitchWasOn()" would be | |
47 // more accurate, but too long. | |
48 bool ShouldRecordExtension(const Extension* extension) { | |
49 return extension->ShouldDisplayInExtensionSettings() && | |
50 !Manifest::IsPolicyLocation(extension->location()) && | |
51 !Manifest::IsComponentLocation(extension->location()) && | |
52 !PermissionsData::CanExecuteScriptEverywhere(extension) && | |
53 extension->permissions_data() | |
54 ->active_permissions() | |
55 ->ShouldWarnAllHosts(); | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 ActiveScriptController::ActiveScriptController( | 40 ActiveScriptController::ActiveScriptController( |
61 content::WebContents* web_contents) | 41 content::WebContents* web_contents) |
62 : content::WebContentsObserver(web_contents), | 42 : content::WebContentsObserver(web_contents), |
63 browser_context_(web_contents->GetBrowserContext()), | 43 browser_context_(web_contents->GetBrowserContext()), |
64 enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()), | 44 was_used_on_page_(false), |
65 extension_registry_observer_(this) { | 45 extension_registry_observer_(this) { |
66 CHECK(web_contents); | 46 CHECK(web_contents); |
67 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 47 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
68 } | 48 } |
69 | 49 |
70 ActiveScriptController::~ActiveScriptController() { | 50 ActiveScriptController::~ActiveScriptController() { |
71 LogUMA(); | 51 LogUMA(); |
72 } | 52 } |
73 | 53 |
74 // static | 54 // static |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // Allow current tab to run injection. | 116 // Allow current tab to run injection. |
137 OnClicked(extension); | 117 OnClicked(extension); |
138 } | 118 } |
139 | 119 |
140 void ActiveScriptController::OnClicked(const Extension* extension) { | 120 void ActiveScriptController::OnClicked(const Extension* extension) { |
141 DCHECK(ContainsKey(pending_requests_, extension->id())); | 121 DCHECK(ContainsKey(pending_requests_, extension->id())); |
142 RunPendingForExtension(extension); | 122 RunPendingForExtension(extension); |
143 } | 123 } |
144 | 124 |
145 bool ActiveScriptController::WantsToRun(const Extension* extension) { | 125 bool ActiveScriptController::WantsToRun(const Extension* extension) { |
146 return enabled_ && pending_requests_.count(extension->id()) > 0; | 126 return pending_requests_.count(extension->id()) > 0; |
147 } | 127 } |
148 | 128 |
149 PermissionsData::AccessType | 129 PermissionsData::AccessType |
150 ActiveScriptController::RequiresUserConsentForScriptInjection( | 130 ActiveScriptController::RequiresUserConsentForScriptInjection( |
151 const Extension* extension, | 131 const Extension* extension, |
152 UserScript::InjectionType type) { | 132 UserScript::InjectionType type) { |
153 CHECK(extension); | 133 CHECK(extension); |
154 | 134 |
155 // If the feature is not enabled, we automatically allow all extensions to | |
156 // run scripts. | |
157 if (!enabled_) | |
158 permitted_extensions_.insert(extension->id()); | |
159 | |
160 // Allow the extension if it's been explicitly granted permission. | 135 // Allow the extension if it's been explicitly granted permission. |
161 if (permitted_extensions_.count(extension->id()) > 0) | 136 if (permitted_extensions_.count(extension->id()) > 0) |
162 return PermissionsData::ACCESS_ALLOWED; | 137 return PermissionsData::ACCESS_ALLOWED; |
163 | 138 |
164 GURL url = web_contents()->GetVisibleURL(); | 139 GURL url = web_contents()->GetVisibleURL(); |
165 int tab_id = SessionTabHelper::IdForTab(web_contents()); | 140 int tab_id = SessionTabHelper::IdForTab(web_contents()); |
166 switch (type) { | 141 switch (type) { |
167 case UserScript::CONTENT_SCRIPT: | 142 case UserScript::CONTENT_SCRIPT: |
168 return extension->permissions_data()->GetContentScriptAccess( | 143 return extension->permissions_data()->GetContentScriptAccess( |
169 extension, url, url, tab_id, -1, NULL); | 144 extension, url, url, tab_id, -1, NULL); |
(...skipping 10 matching lines...) Expand all Loading... |
180 const Extension* extension, | 155 const Extension* extension, |
181 const base::Closure& callback) { | 156 const base::Closure& callback) { |
182 CHECK(extension); | 157 CHECK(extension); |
183 PendingRequestList& list = pending_requests_[extension->id()]; | 158 PendingRequestList& list = pending_requests_[extension->id()]; |
184 list.push_back(callback); | 159 list.push_back(callback); |
185 | 160 |
186 // If this was the first entry, we need to notify that a new extension wants | 161 // If this was the first entry, we need to notify that a new extension wants |
187 // to run. | 162 // to run. |
188 if (list.size() == 1u) | 163 if (list.size() == 1u) |
189 NotifyChange(extension); | 164 NotifyChange(extension); |
| 165 |
| 166 was_used_on_page_ = true; |
190 } | 167 } |
191 | 168 |
192 void ActiveScriptController::RunPendingForExtension( | 169 void ActiveScriptController::RunPendingForExtension( |
193 const Extension* extension) { | 170 const Extension* extension) { |
194 DCHECK(extension); | 171 DCHECK(extension); |
195 | 172 |
196 content::NavigationEntry* visible_entry = | 173 content::NavigationEntry* visible_entry = |
197 web_contents()->GetController().GetVisibleEntry(); | 174 web_contents()->GetController().GetVisibleEntry(); |
198 // Refuse to run if there's no visible entry, because we have no idea of | 175 // Refuse to run if there's no visible entry, because we have no idea of |
199 // determining if it's the proper page. This should rarely, if ever, happen. | 176 // determining if it's the proper page. This should rarely, if ever, happen. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 ->enabled_extensions().GetByID(extension_id); | 223 ->enabled_extensions().GetByID(extension_id); |
247 // We shouldn't allow extensions which are no longer enabled to run any | 224 // We shouldn't allow extensions which are no longer enabled to run any |
248 // scripts. Ignore the request. | 225 // scripts. Ignore the request. |
249 if (!extension) | 226 if (!extension) |
250 return; | 227 return; |
251 | 228 |
252 // If the request id is -1, that signals that the content script has already | 229 // If the request id is -1, that signals that the content script has already |
253 // ran (because this feature is not enabled). Add the extension to the list of | 230 // ran (because this feature is not enabled). Add the extension to the list of |
254 // permitted extensions (for metrics), and return immediately. | 231 // permitted extensions (for metrics), and return immediately. |
255 if (request_id == -1) { | 232 if (request_id == -1) { |
256 if (ShouldRecordExtension(extension)) { | 233 if (util::ScriptsMayRequireActionForExtension( |
257 DCHECK(!enabled_); | 234 extension, |
| 235 extension->permissions_data()->active_permissions().get())) { |
258 permitted_extensions_.insert(extension->id()); | 236 permitted_extensions_.insert(extension->id()); |
259 } | 237 } |
260 return; | 238 return; |
261 } | 239 } |
262 | 240 |
263 switch (RequiresUserConsentForScriptInjection(extension, script_type)) { | 241 switch (RequiresUserConsentForScriptInjection(extension, script_type)) { |
264 case PermissionsData::ACCESS_ALLOWED: | 242 case PermissionsData::ACCESS_ALLOWED: |
265 PermitScriptInjection(request_id); | 243 PermitScriptInjection(request_id); |
266 break; | 244 break; |
267 case PermissionsData::ACCESS_WITHHELD: | 245 case PermissionsData::ACCESS_WITHHELD: |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 | 284 |
307 // We also notify that page actions may have changed. | 285 // We also notify that page actions may have changed. |
308 extension_action_api->NotifyPageActionsChanged(web_contents()); | 286 extension_action_api->NotifyPageActionsChanged(web_contents()); |
309 } | 287 } |
310 | 288 |
311 void ActiveScriptController::LogUMA() const { | 289 void ActiveScriptController::LogUMA() const { |
312 UMA_HISTOGRAM_COUNTS_100( | 290 UMA_HISTOGRAM_COUNTS_100( |
313 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", | 291 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", |
314 pending_requests_.size()); | 292 pending_requests_.size()); |
315 | 293 |
316 // We only log the permitted extensions metric if the feature is enabled, | 294 // We only log the permitted extensions metric if the feature was used at all |
317 // because otherwise the data will be boring (100% allowed). | 295 // on the page, because otherwise the data will be boring. |
318 if (enabled_) { | 296 if (was_used_on_page_) { |
319 UMA_HISTOGRAM_COUNTS_100( | 297 UMA_HISTOGRAM_COUNTS_100( |
320 "Extensions.ActiveScriptController.PermittedExtensions", | 298 "Extensions.ActiveScriptController.PermittedExtensions", |
321 permitted_extensions_.size()); | 299 permitted_extensions_.size()); |
322 UMA_HISTOGRAM_COUNTS_100( | 300 UMA_HISTOGRAM_COUNTS_100( |
323 "Extensions.ActiveScriptController.DeniedExtensions", | 301 "Extensions.ActiveScriptController.DeniedExtensions", |
324 pending_requests_.size()); | 302 pending_requests_.size()); |
325 } | 303 } |
326 } | 304 } |
327 | 305 |
328 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { | 306 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { |
329 bool handled = true; | 307 bool handled = true; |
330 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) | 308 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) |
331 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RequestScriptInjectionPermission, | 309 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RequestScriptInjectionPermission, |
332 OnRequestScriptInjectionPermission) | 310 OnRequestScriptInjectionPermission) |
333 IPC_MESSAGE_UNHANDLED(handled = false) | 311 IPC_MESSAGE_UNHANDLED(handled = false) |
334 IPC_END_MESSAGE_MAP() | 312 IPC_END_MESSAGE_MAP() |
335 return handled; | 313 return handled; |
336 } | 314 } |
337 | 315 |
338 void ActiveScriptController::DidNavigateMainFrame( | 316 void ActiveScriptController::DidNavigateMainFrame( |
339 const content::LoadCommittedDetails& details, | 317 const content::LoadCommittedDetails& details, |
340 const content::FrameNavigateParams& params) { | 318 const content::FrameNavigateParams& params) { |
341 if (details.is_in_page) | 319 if (details.is_in_page) |
342 return; | 320 return; |
343 | 321 |
344 LogUMA(); | 322 LogUMA(); |
345 permitted_extensions_.clear(); | 323 permitted_extensions_.clear(); |
346 pending_requests_.clear(); | 324 pending_requests_.clear(); |
| 325 was_used_on_page_ = false; |
347 } | 326 } |
348 | 327 |
349 void ActiveScriptController::OnExtensionUnloaded( | 328 void ActiveScriptController::OnExtensionUnloaded( |
350 content::BrowserContext* browser_context, | 329 content::BrowserContext* browser_context, |
351 const Extension* extension, | 330 const Extension* extension, |
352 UnloadedExtensionInfo::Reason reason) { | 331 UnloadedExtensionInfo::Reason reason) { |
353 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); | 332 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); |
354 if (iter != pending_requests_.end()) { | 333 if (iter != pending_requests_.end()) { |
355 pending_requests_.erase(iter); | 334 pending_requests_.erase(iter); |
356 ExtensionActionAPI::Get(browser_context_)-> | 335 ExtensionActionAPI::Get(browser_context_)-> |
357 NotifyPageActionsChanged(web_contents()); | 336 NotifyPageActionsChanged(web_contents()); |
358 } | 337 } |
359 } | 338 } |
360 | 339 |
361 } // namespace extensions | 340 } // namespace extensions |
OLD | NEW |