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

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

Issue 307933008: Revert 273866 "Block content scripts from executing until user g..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 if (list.size() == 1u) 103 if (list.size() == 1u)
104 LocationBarController::NotifyChange(web_contents()); 104 LocationBarController::NotifyChange(web_contents());
105 } 105 }
106 106
107 void ActiveScriptController::OnActiveTabPermissionGranted( 107 void ActiveScriptController::OnActiveTabPermissionGranted(
108 const Extension* extension) { 108 const Extension* extension) {
109 RunPendingForExtension(extension); 109 RunPendingForExtension(extension);
110 } 110 }
111 111
112 void ActiveScriptController::OnAdInjectionDetected( 112 void ActiveScriptController::OnAdInjectionDetected(
113 const std::set<std::string>& ad_injectors) { 113 const std::set<std::string> ad_injectors) {
114 // We're only interested in data if there are ad injectors detected. 114 // We're only interested in data if there are ad injectors detected.
115 if (ad_injectors.empty()) 115 if (ad_injectors.empty())
116 return; 116 return;
117 117
118 size_t num_preventable_ad_injectors = 118 size_t num_preventable_ad_injectors =
119 base::STLSetIntersection<std::set<std::string> >( 119 base::STLSetIntersection<std::set<std::string> >(
120 ad_injectors, permitted_extensions_).size(); 120 ad_injectors, permitted_extensions_).size();
121 121
122 UMA_HISTOGRAM_COUNTS_100( 122 UMA_HISTOGRAM_COUNTS_100(
123 "Extensions.ActiveScriptController.PreventableAdInjectors", 123 "Extensions.ActiveScriptController.PreventableAdInjectors",
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 ++request) { 206 ++request) {
207 // Only run if it's on the proper page. 207 // Only run if it's on the proper page.
208 if (request->page_id == page_id) 208 if (request->page_id == page_id)
209 request->closure.Run(); 209 request->closure.Run();
210 } 210 }
211 211
212 // Inform the location bar that the action is now gone. 212 // Inform the location bar that the action is now gone.
213 LocationBarController::NotifyChange(web_contents()); 213 LocationBarController::NotifyChange(web_contents());
214 } 214 }
215 215
216 void ActiveScriptController::OnRequestContentScriptPermission( 216 void ActiveScriptController::OnNotifyExtensionScriptExecution(
217 const std::string& extension_id, 217 const std::string& extension_id,
218 int page_id, 218 int page_id) {
219 int request_id) {
220 if (!Extension::IdIsValid(extension_id)) { 219 if (!Extension::IdIsValid(extension_id)) {
221 NOTREACHED() << "'" << extension_id << "' is not a valid id."; 220 NOTREACHED() << "'" << extension_id << "' is not a valid id.";
222 return; 221 return;
223 } 222 }
224 223
225 const Extension* extension = 224 const Extension* extension =
226 ExtensionRegistry::Get(web_contents()->GetBrowserContext()) 225 ExtensionRegistry::Get(web_contents()->GetBrowserContext())
227 ->enabled_extensions().GetByID(extension_id); 226 ->enabled_extensions().GetByID(extension_id);
228 // We shouldn't allow extensions which are no longer enabled to run any 227 // We shouldn't allow extensions which are no longer enabled to run any
229 // scripts. Ignore the request. 228 // scripts. Ignore the request.
230 if (!extension) 229 if (!extension)
231 return; 230 return;
232 231
233 // If the request id is -1, that signals that the content script has already 232 // Right now, we allow all content scripts to execute, but notify the
234 // ran (because this feature is not enabled). Add the extension to the list of 233 // controller of them.
235 // permitted extensions (for metrics), and return immediately. 234 // TODO(rdevlin.cronin): Fix this in a future CL.
236 if (request_id == -1) { 235 if (RequiresUserConsentForScriptInjection(extension))
237 DCHECK(!enabled_); 236 RequestScriptInjection(extension, page_id, base::Bind(&base::DoNothing));
238 permitted_extensions_.insert(extension->id());
239 return;
240 }
241
242 if (RequiresUserConsentForScriptInjection(extension)) {
243 // This base::Unretained() is safe, because the callback is only invoked by
244 // this object.
245 RequestScriptInjection(
246 extension,
247 page_id,
248 base::Bind(&ActiveScriptController::GrantContentScriptPermission,
249 base::Unretained(this),
250 request_id));
251 } else {
252 GrantContentScriptPermission(request_id);
253 }
254 }
255
256 void ActiveScriptController::GrantContentScriptPermission(int request_id) {
257 content::RenderViewHost* render_view_host =
258 web_contents()->GetRenderViewHost();
259 if (render_view_host) {
260 render_view_host->Send(new ExtensionMsg_GrantContentScriptPermission(
261 render_view_host->GetRoutingID(),
262 request_id));
263 }
264 } 237 }
265 238
266 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { 239 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) {
267 bool handled = true; 240 bool handled = true;
268 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) 241 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message)
269 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RequestContentScriptPermission, 242 IPC_MESSAGE_HANDLER(ExtensionHostMsg_NotifyExtensionScriptExecution,
270 OnRequestContentScriptPermission) 243 OnNotifyExtensionScriptExecution)
271 IPC_MESSAGE_UNHANDLED(handled = false) 244 IPC_MESSAGE_UNHANDLED(handled = false)
272 IPC_END_MESSAGE_MAP() 245 IPC_END_MESSAGE_MAP()
273 return handled; 246 return handled;
274 } 247 }
275 248
276 void ActiveScriptController::LogUMA() const { 249 void ActiveScriptController::LogUMA() const {
277 UMA_HISTOGRAM_COUNTS_100( 250 UMA_HISTOGRAM_COUNTS_100(
278 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", 251 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage",
279 pending_requests_.size()); 252 pending_requests_.size());
280 253
281 // We only log the permitted extensions metric if the feature is enabled, 254 // We only log the permitted extensions metric if the feature is enabled,
282 // because otherwise the data will be boring (100% allowed). 255 // because otherwise the data will be boring (100% allowed).
283 if (enabled_) { 256 if (enabled_) {
284 UMA_HISTOGRAM_COUNTS_100( 257 UMA_HISTOGRAM_COUNTS_100(
285 "Extensions.ActiveScriptController.PermittedExtensions", 258 "Extensions.ActiveScriptController.PermittedExtensions",
286 permitted_extensions_.size()); 259 permitted_extensions_.size());
287 UMA_HISTOGRAM_COUNTS_100( 260 UMA_HISTOGRAM_COUNTS_100(
288 "Extensions.ActiveScriptController.DeniedExtensions", 261 "Extensions.ActiveScriptController.DeniedExtensions",
289 pending_requests_.size()); 262 pending_requests_.size());
290 } 263 }
291 } 264 }
292 265
293 } // namespace extensions 266 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698