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

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

Issue 396033002: Support "always allow" for runtime script execution (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactoring, minor changes Created 6 years, 4 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"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "chrome/browser/extensions/active_tab_permission_granter.h" 12 #include "chrome/browser/extensions/active_tab_permission_granter.h"
13 #include "chrome/browser/extensions/extension_action.h" 13 #include "chrome/browser/extensions/extension_action.h"
14 #include "chrome/browser/extensions/extension_util.h" 14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/extensions/location_bar_controller.h" 15 #include "chrome/browser/extensions/location_bar_controller.h"
16 #include "chrome/browser/extensions/permissions_updater.h"
16 #include "chrome/browser/extensions/tab_helper.h" 17 #include "chrome/browser/extensions/tab_helper.h"
17 #include "chrome/browser/sessions/session_id.h" 18 #include "chrome/browser/sessions/session_id.h"
18 #include "chrome/common/extensions/api/extension_action/action_info.h" 19 #include "chrome/common/extensions/api/extension_action/action_info.h"
19 #include "content/public/browser/navigation_controller.h" 20 #include "content/public/browser/navigation_controller.h"
20 #include "content/public/browser/navigation_entry.h" 21 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/render_view_host.h" 22 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
24 #include "extensions/browser/extension_prefs.h"
23 #include "extensions/browser/extension_registry.h" 25 #include "extensions/browser/extension_registry.h"
24 #include "extensions/common/extension.h" 26 #include "extensions/common/extension.h"
25 #include "extensions/common/extension_messages.h" 27 #include "extensions/common/extension_messages.h"
26 #include "extensions/common/extension_set.h" 28 #include "extensions/common/extension_set.h"
27 #include "extensions/common/feature_switch.h" 29 #include "extensions/common/feature_switch.h"
28 #include "extensions/common/manifest.h" 30 #include "extensions/common/manifest.h"
31 #include "extensions/common/manifest_handlers/permissions_parser.h"
32 #include "extensions/common/permissions/permission_set.h"
29 #include "extensions/common/permissions/permissions_data.h" 33 #include "extensions/common/permissions/permissions_data.h"
30 #include "ipc/ipc_message_macros.h" 34 #include "ipc/ipc_message_macros.h"
31 35
32 namespace extensions { 36 namespace extensions {
33 37
34 namespace { 38 namespace {
35 39
36 // Returns true if the extension should be regarded as a "permitted" extension 40 // Returns true if the extension should be regarded as a "permitted" extension
37 // for the case of metrics. We need this because we only actually withhold 41 // for the case of metrics. We need this because we only actually withhold
38 // permissions if the switch is enabled, but want to record metrics in all 42 // permissions if the switch is enabled, but want to record metrics in all
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 ad_injectors, permitted_extensions_).size(); 97 ad_injectors, permitted_extensions_).size();
94 98
95 UMA_HISTOGRAM_COUNTS_100( 99 UMA_HISTOGRAM_COUNTS_100(
96 "Extensions.ActiveScriptController.PreventableAdInjectors", 100 "Extensions.ActiveScriptController.PreventableAdInjectors",
97 num_preventable_ad_injectors); 101 num_preventable_ad_injectors);
98 UMA_HISTOGRAM_COUNTS_100( 102 UMA_HISTOGRAM_COUNTS_100(
99 "Extensions.ActiveScriptController.UnpreventableAdInjectors", 103 "Extensions.ActiveScriptController.UnpreventableAdInjectors",
100 ad_injectors.size() - num_preventable_ad_injectors); 104 ad_injectors.size() - num_preventable_ad_injectors);
101 } 105 }
102 106
107 void ActiveScriptController::AlwaysRunOnVisibleHost(
108 const Extension* extension) {
109 GURL url = web_contents()->GetVisibleURL();
110 extensions::URLPatternSet new_explicit_hosts;
111 extensions::URLPatternSet new_scriptable_hosts;
112
113 scoped_refptr<const PermissionSet> withheld_permissions =
114 extension->permissions_data()->withheld_permissions();
115 if (withheld_permissions->explicit_hosts().MatchesURL(url)) {
116 new_explicit_hosts.AddOrigin(
117 extensions::UserScript::ValidUserScriptSchemes(), url.GetOrigin());
not at google - send to devlin 2014/08/12 19:49:27 don't need extensions:: here or below.
gpdavis 2014/08/12 21:19:54 Done.
118 }
119 if (withheld_permissions->scriptable_hosts().MatchesURL(url)) {
120 new_scriptable_hosts.AddOrigin(
121 extensions::UserScript::ValidUserScriptSchemes(), url.GetOrigin());
122 }
123
124 scoped_refptr<extensions::PermissionSet> new_permissions =
125 new extensions::PermissionSet(extensions::APIPermissionSet(),
126 extensions::ManifestPermissionSet(),
127 new_explicit_hosts,
128 new_scriptable_hosts);
129
130 // Update permissions for the session. This adds |new_permissions| to active
131 // permissions and granted permissions.
132 extensions::PermissionsUpdater updater(web_contents()->GetBrowserContext());
133 updater.AddPermissions(extension, new_permissions.get());
134
135 URLPatternSet permissions_union;
136 URLPatternSet::CreateUnion(
137 withheld_permissions->scriptable_hosts(),
138 PermissionsParser::GetRequiredPermissions(extension)->scriptable_hosts(),
139 &permissions_union);
140 UMA_HISTOGRAM_COUNTS_100(
141 "Extensions.ActiveScriptController.AlwaysRunCount",
142 extension->permissions_data()->GetEffectiveHostPermissions().size() -
143 permissions_union.size());
144
145 // Allow current tab to run injection.
146 OnClicked(extension);
147 }
148
149 bool ActiveScriptController::HasActiveScriptAction(const Extension* extension) {
150 return enabled_ && active_script_actions_.count(extension->id()) > 0;
151 }
152
103 ExtensionAction* ActiveScriptController::GetActionForExtension( 153 ExtensionAction* ActiveScriptController::GetActionForExtension(
104 const Extension* extension) { 154 const Extension* extension) {
105 if (!enabled_ || pending_requests_.count(extension->id()) == 0) 155 if (!enabled_ || pending_requests_.count(extension->id()) == 0)
106 return NULL; // No action for this extension. 156 return NULL; // No action for this extension.
107 157
108 ActiveScriptMap::iterator existing = 158 ActiveScriptMap::iterator existing =
109 active_script_actions_.find(extension->id()); 159 active_script_actions_.find(extension->id());
110 if (existing != active_script_actions_.end()) 160 if (existing != active_script_actions_.end())
111 return existing->second.get(); 161 return existing->second.get();
112 162
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 content::NavigationEntry* visible_entry = 247 content::NavigationEntry* visible_entry =
198 web_contents()->GetController().GetVisibleEntry(); 248 web_contents()->GetController().GetVisibleEntry();
199 // Refuse to run if there's no visible entry, because we have no idea of 249 // Refuse to run if there's no visible entry, because we have no idea of
200 // determining if it's the proper page. This should rarely, if ever, happen. 250 // determining if it's the proper page. This should rarely, if ever, happen.
201 if (!visible_entry) 251 if (!visible_entry)
202 return; 252 return;
203 253
204 // We add this to the list of permitted extensions and erase pending entries 254 // We add this to the list of permitted extensions and erase pending entries
205 // *before* running them to guard against the crazy case where running the 255 // *before* running them to guard against the crazy case where running the
206 // callbacks adds more entries. 256 // callbacks adds more entries.
207 permitted_extensions_.insert(extension->id()); 257 permitted_extensions_.insert(extension->id());
not at google - send to devlin 2014/08/12 19:49:27 Ok - this has exposed a bug in our current UMA of
gpdavis 2014/08/12 21:19:54 Alright, let's do all of that in a follow-up CL th
not at google - send to devlin 2014/08/12 23:13:17 Looks like you haven't removed the UMA from this p
gpdavis 2014/08/13 00:08:24 Oops-- sorry about that. Had so many changes in t
208 258
209 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); 259 PendingRequestMap::iterator iter = pending_requests_.find(extension->id());
210 if (iter == pending_requests_.end()) 260 if (iter == pending_requests_.end())
211 return; 261 return;
212 262
213 PendingRequestList requests; 263 PendingRequestList requests;
214 iter->second.swap(requests); 264 iter->second.swap(requests);
215 pending_requests_.erase(extension->id()); 265 pending_requests_.erase(extension->id());
216 266
217 // Clicking to run the extension counts as granting it permission to run on 267 // Clicking to run the extension counts as granting it permission to run on
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 pending_requests_.size()); 358 pending_requests_.size());
309 359
310 // We only log the permitted extensions metric if the feature is enabled, 360 // We only log the permitted extensions metric if the feature is enabled,
311 // because otherwise the data will be boring (100% allowed). 361 // because otherwise the data will be boring (100% allowed).
312 if (enabled_) { 362 if (enabled_) {
313 UMA_HISTOGRAM_COUNTS_100( 363 UMA_HISTOGRAM_COUNTS_100(
314 "Extensions.ActiveScriptController.PermittedExtensions", 364 "Extensions.ActiveScriptController.PermittedExtensions",
315 permitted_extensions_.size()); 365 permitted_extensions_.size());
316 UMA_HISTOGRAM_COUNTS_100( 366 UMA_HISTOGRAM_COUNTS_100(
317 "Extensions.ActiveScriptController.DeniedExtensions", 367 "Extensions.ActiveScriptController.DeniedExtensions",
318 pending_requests_.size()); 368 pending_requests_.size());
not at google - send to devlin 2014/08/12 19:49:27 As Ilya suggests, let's also log the always-run ex
319 } 369 }
320 } 370 }
321 371
322 } // namespace extensions 372 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698