Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: chrome/browser/extensions/active_script_controller.cc

Issue 874683005: [Extensions] Enable the scripts-require-action feature based on all-urls pref (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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()),
65 extension_registry_observer_(this) { 44 extension_registry_observer_(this) {
66 CHECK(web_contents); 45 CHECK(web_contents);
67 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 46 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
68 } 47 }
69 48
70 ActiveScriptController::~ActiveScriptController() { 49 ActiveScriptController::~ActiveScriptController() {
71 LogUMA(); 50 LogUMA();
72 } 51 }
73 52
74 // static 53 // static
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 // Allow current tab to run injection. 115 // Allow current tab to run injection.
137 OnClicked(extension); 116 OnClicked(extension);
138 } 117 }
139 118
140 void ActiveScriptController::OnClicked(const Extension* extension) { 119 void ActiveScriptController::OnClicked(const Extension* extension) {
141 DCHECK(ContainsKey(pending_requests_, extension->id())); 120 DCHECK(ContainsKey(pending_requests_, extension->id()));
142 RunPendingForExtension(extension); 121 RunPendingForExtension(extension);
143 } 122 }
144 123
145 bool ActiveScriptController::WantsToRun(const Extension* extension) { 124 bool ActiveScriptController::WantsToRun(const Extension* extension) {
146 return enabled_ && pending_requests_.count(extension->id()) > 0; 125 return pending_requests_.count(extension->id()) > 0;
147 } 126 }
148 127
149 PermissionsData::AccessType 128 PermissionsData::AccessType
150 ActiveScriptController::RequiresUserConsentForScriptInjection( 129 ActiveScriptController::RequiresUserConsentForScriptInjection(
151 const Extension* extension, 130 const Extension* extension,
152 UserScript::InjectionType type) { 131 UserScript::InjectionType type) {
153 CHECK(extension); 132 CHECK(extension);
154 133
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. 134 // Allow the extension if it's been explicitly granted permission.
161 if (permitted_extensions_.count(extension->id()) > 0) 135 if (permitted_extensions_.count(extension->id()) > 0)
162 return PermissionsData::ACCESS_ALLOWED; 136 return PermissionsData::ACCESS_ALLOWED;
163 137
164 GURL url = web_contents()->GetVisibleURL(); 138 GURL url = web_contents()->GetVisibleURL();
165 int tab_id = SessionTabHelper::IdForTab(web_contents()); 139 int tab_id = SessionTabHelper::IdForTab(web_contents());
166 switch (type) { 140 switch (type) {
167 case UserScript::CONTENT_SCRIPT: 141 case UserScript::CONTENT_SCRIPT:
168 return extension->permissions_data()->GetContentScriptAccess( 142 return extension->permissions_data()->GetContentScriptAccess(
169 extension, url, url, tab_id, -1, NULL); 143 extension, url, url, tab_id, -1, NULL);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 ->enabled_extensions().GetByID(extension_id); 220 ->enabled_extensions().GetByID(extension_id);
247 // We shouldn't allow extensions which are no longer enabled to run any 221 // We shouldn't allow extensions which are no longer enabled to run any
248 // scripts. Ignore the request. 222 // scripts. Ignore the request.
249 if (!extension) 223 if (!extension)
250 return; 224 return;
251 225
252 // If the request id is -1, that signals that the content script has already 226 // 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 227 // ran (because this feature is not enabled). Add the extension to the list of
254 // permitted extensions (for metrics), and return immediately. 228 // permitted extensions (for metrics), and return immediately.
255 if (request_id == -1) { 229 if (request_id == -1) {
256 if (ShouldRecordExtension(extension)) { 230 if (util::ScriptsMayRequireActionForExtension(
257 DCHECK(!enabled_); 231 extension,
232 extension->permissions_data()->active_permissions())) {
258 permitted_extensions_.insert(extension->id()); 233 permitted_extensions_.insert(extension->id());
259 } 234 }
260 return; 235 return;
261 } 236 }
262 237
263 switch (RequiresUserConsentForScriptInjection(extension, script_type)) { 238 switch (RequiresUserConsentForScriptInjection(extension, script_type)) {
264 case PermissionsData::ACCESS_ALLOWED: 239 case PermissionsData::ACCESS_ALLOWED:
265 PermitScriptInjection(request_id); 240 PermitScriptInjection(request_id);
266 break; 241 break;
267 case PermissionsData::ACCESS_WITHHELD: 242 case PermissionsData::ACCESS_WITHHELD:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 if (extension_action) { 277 if (extension_action) {
303 extension_action_api->NotifyChange( 278 extension_action_api->NotifyChange(
304 extension_action, web_contents(), browser_context_); 279 extension_action, web_contents(), browser_context_);
305 } 280 }
306 281
307 // We also notify that page actions may have changed. 282 // We also notify that page actions may have changed.
308 extension_action_api->NotifyPageActionsChanged(web_contents()); 283 extension_action_api->NotifyPageActionsChanged(web_contents());
309 } 284 }
310 285
311 void ActiveScriptController::LogUMA() const { 286 void ActiveScriptController::LogUMA() const {
312 UMA_HISTOGRAM_COUNTS_100( 287 UMA_HISTOGRAM_COUNTS_100(
not at google - send to devlin 2015/02/06 00:26:55 As discussed, this is rather pointless at this sta
Devlin 2015/02/06 18:58:43 Yeah. Part of me wonders if it's worth keeping in
not at google - send to devlin 2015/02/06 19:38:43 Mostly the usefulness is covered by the metric bel
Devlin 2015/02/06 23:18:49 Fair enough - we'd probably wanna tweak this. I'l
313 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", 288 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage",
314 pending_requests_.size()); 289 pending_requests_.size());
315 290
316 // We only log the permitted extensions metric if the feature is enabled, 291 // We only log the permitted extensions metric if the feature is enabled,
317 // because otherwise the data will be boring (100% allowed). 292 // because otherwise the data will likely be boring (100% allowed).
318 if (enabled_) { 293 if (extensions::FeatureSwitch::scripts_require_action()->IsEnabled()) {
not at google - send to devlin 2015/02/06 00:26:55 This isn't very useful with this change, since the
Devlin 2015/02/06 18:58:43 To say there's no longer a correlation between the
not at google - send to devlin 2015/02/06 19:38:43 I think that ultimately whether or not there's use
Devlin 2015/02/06 23:18:49 Added the "something_interesting_happened" ;)
319 UMA_HISTOGRAM_COUNTS_100( 294 UMA_HISTOGRAM_COUNTS_100(
320 "Extensions.ActiveScriptController.PermittedExtensions", 295 "Extensions.ActiveScriptController.PermittedExtensions",
321 permitted_extensions_.size()); 296 permitted_extensions_.size());
322 UMA_HISTOGRAM_COUNTS_100( 297 UMA_HISTOGRAM_COUNTS_100(
323 "Extensions.ActiveScriptController.DeniedExtensions", 298 "Extensions.ActiveScriptController.DeniedExtensions",
324 pending_requests_.size()); 299 pending_requests_.size());
325 } 300 }
326 } 301 }
327 302
328 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { 303 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) {
(...skipping 23 matching lines...) Expand all
352 UnloadedExtensionInfo::Reason reason) { 327 UnloadedExtensionInfo::Reason reason) {
353 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); 328 PendingRequestMap::iterator iter = pending_requests_.find(extension->id());
354 if (iter != pending_requests_.end()) { 329 if (iter != pending_requests_.end()) {
355 pending_requests_.erase(iter); 330 pending_requests_.erase(iter);
356 ExtensionActionAPI::Get(browser_context_)-> 331 ExtensionActionAPI::Get(browser_context_)->
357 NotifyPageActionsChanged(web_contents()); 332 NotifyPageActionsChanged(web_contents());
358 } 333 }
359 } 334 }
360 335
361 } // namespace extensions 336 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698