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

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

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

Powered by Google App Engine
This is Rietveld 408576698