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

Side by Side Diff: chrome/renderer/extensions/extension_dispatcher.cc

Issue 9403006: Extensions: run "custom bindings" v8-extensions in content scripts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/renderer/extensions/extension_dispatcher.h" 5 #include "chrome/renderer/extensions/extension_dispatcher.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "chrome/common/child_process_logging.h" 8 #include "chrome/common/child_process_logging.h"
9 #include "chrome/common/chrome_switches.h" 9 #include "chrome/common/chrome_switches.h"
10 #include "chrome/common/extensions/extension.h" 10 #include "chrome/common/extensions/extension.h"
(...skipping 17 matching lines...) Expand all
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" 31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" 32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
34 #include "ui/base/resource/resource_bundle.h" 34 #include "ui/base/resource/resource_bundle.h"
35 #include "v8/include/v8.h" 35 #include "v8/include/v8.h"
36 36
37 namespace { 37 namespace {
38
38 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; 39 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000;
39 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; 40 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000;
41
42 ChromeV8Context::ContextType ExtensionGroupToContextType(int extension_group) {
43 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS)
44 return ChromeV8Context::CONTENT_SCRIPT;
45 else
46 return ChromeV8Context::FRAME;
47 }
48
40 } 49 }
41 50
42 using namespace extensions; 51 using namespace extensions;
43 52
44 using WebKit::WebDataSource; 53 using WebKit::WebDataSource;
45 using WebKit::WebDocument; 54 using WebKit::WebDocument;
46 using WebKit::WebFrame; 55 using WebKit::WebFrame;
47 using WebKit::WebSecurityPolicy; 56 using WebKit::WebSecurityPolicy;
48 using WebKit::WebString; 57 using WebKit::WebString;
49 using WebKit::WebVector; 58 using WebKit::WebVector;
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 active_extension_ids_.end(); 274 active_extension_ids_.end();
266 } 275 }
267 276
268 bool ExtensionDispatcher::AllowScriptExtension( 277 bool ExtensionDispatcher::AllowScriptExtension(
269 WebFrame* frame, 278 WebFrame* frame,
270 const std::string& v8_extension_name, 279 const std::string& v8_extension_name,
271 int extension_group) { 280 int extension_group) {
272 return AllowScriptExtension(frame, v8_extension_name, extension_group, 0); 281 return AllowScriptExtension(frame, v8_extension_name, extension_group, 0);
273 } 282 }
274 283
284 namespace {
285
286 // This is what the extension_group variable will be when DidCreateScriptContext
287 // is called. We know because it's the same as what AllowScriptExtension gets
288 // passed, and the two functions are called sequentially from WebKit.
289 //
290 // TODO(koz): Plumb extension_group through to AllowScriptExtension() from
291 // WebKit.
292 static int hack_DidCreateScriptContext_extension_group = 0;
293
294 }
295
275 bool ExtensionDispatcher::AllowScriptExtension( 296 bool ExtensionDispatcher::AllowScriptExtension(
276 WebFrame* frame, 297 WebFrame* frame,
277 const std::string& v8_extension_name, 298 const std::string& v8_extension_name,
278 int extension_group, 299 int extension_group,
279 int world_id) { 300 int world_id) {
301 hack_DidCreateScriptContext_extension_group = extension_group;
302
280 // NULL in unit tests. 303 // NULL in unit tests.
281 if (!RenderThread::Get()) 304 if (!RenderThread::Get())
282 return true; 305 return true;
283 306
284 // If we don't know about it, it was added by WebCore, so we should allow it. 307 // If we don't know about it, it was added by WebCore, so we should allow it.
285 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name)) 308 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name))
286 return true; 309 return true;
287 310
288 // If the V8 extension is not restricted, allow it to run anywhere. 311 // If the V8 extension is not restricted, allow it to run anywhere.
289 if (!restricted_v8_extensions_.count(v8_extension_name)) 312 if (!restricted_v8_extensions_.count(v8_extension_name))
290 return true; 313 return true;
291 314
292 // Extension-only bindings should be restricted to content scripts and 315 // Extension-only bindings should be restricted to content scripts and
293 // extension-blessed URLs. 316 // extension-blessed URLs.
294 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS || 317 ChromeV8Context::ContextType context_type =
318 ExtensionGroupToContextType(extension_group);
319
320 if (context_type == ChromeV8Context::CONTENT_SCRIPT ||
295 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo( 321 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo(
296 frame->document().securityOrigin(), 322 frame->document().securityOrigin(),
297 UserScriptSlave::GetDataSourceURLForFrame(frame)))) { 323 UserScriptSlave::GetDataSourceURLForFrame(frame)))) {
298 // If the extension is a custom API binding, only allow if the extension 324 // If the extension is a custom API binding, only allow if the extension
299 // has permission to use the API. 325 // has permission to use the API.
300 std::string custom_binding_api_name = 326 std::string custom_binding_api_name =
301 custom_bindings_util::GetAPIName(v8_extension_name); 327 custom_bindings_util::GetAPIName(v8_extension_name);
302 if (!custom_binding_api_name.empty()) { 328 if (!custom_binding_api_name.empty()) {
303 std::string extension_id = GetExtensionID(frame, world_id); 329 std::string extension_id = GetExtensionID(frame, world_id);
304 const Extension* extension = extensions_.GetByID(extension_id); 330 const Extension* extension = extensions_.GetByID(extension_id);
305 if (!extension) { 331 if (!extension) {
306 // This can happen when a resource is blocked due to CSP; a valid 332 // This can happen when a resource is blocked due to CSP; a valid
307 // chrome-extension:// URL is navigated to, so it passes the initial 333 // chrome-extension:// URL is navigated to, so it passes the initial
308 // checks, but the URL gets changed to "chrome-extension://invalid" 334 // checks, but the URL gets changed to "chrome-extension://invalid"
309 // afterwards (see chrome_content_renderer_client.cc). An extension 335 // afterwards (see chrome_content_renderer_client.cc). An extension
310 // page still gets loaded, just for the extension with ID "invalid", 336 // page still gets loaded, just for the extension with ID "invalid",
311 // which of course isn't found so GetById extension will be NULL. 337 // which of course isn't found so GetById extension will be NULL.
312 // 338 //
313 // Reference: http://crbug.com/111614. 339 // Reference: http://crbug.com/111614.
314 CHECK_EQ("invalid", extension_id); 340 CHECK_EQ("invalid", extension_id);
315 return false; 341 return false;
316 } 342 }
317 return custom_bindings_util::AllowAPIInjection( 343 return custom_bindings_util::AllowAPIInjection(
318 custom_binding_api_name, *extension); 344 custom_binding_api_name, *extension, context_type);
319 } 345 }
320 346
321 return true; 347 return true;
322 } 348 }
323 349
324 return false; 350 return false;
325 } 351 }
326 352
327 void ExtensionDispatcher::DidCreateScriptContext( 353 void ExtensionDispatcher::DidCreateScriptContext(
328 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { 354 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) {
329 ChromeV8Context* context = 355 ChromeV8Context* context =
330 new ChromeV8Context(v8_context, frame, GetExtensionID(frame, world_id)); 356 new ChromeV8Context(
357 v8_context,
358 frame,
359 GetExtensionID(frame, world_id),
360 ExtensionGroupToContextType(
361 hack_DidCreateScriptContext_extension_group));
331 v8_context_set_.Add(context); 362 v8_context_set_.Add(context);
332 363
333 const Extension* extension = extensions_.GetByID(context->extension_id()); 364 const Extension* extension = extensions_.GetByID(context->extension_id());
334 int manifest_version = 1; 365 int manifest_version = 1;
335 if (extension) 366 if (extension)
336 manifest_version = extension->manifest_version(); 367 manifest_version = extension->manifest_version();
337 368
338 context->DispatchOnLoadEvent( 369 context->DispatchOnLoadEvent(
339 is_extension_process_, 370 is_extension_process_,
340 ChromeRenderProcessObserver::is_incognito_process(), 371 ChromeRenderProcessObserver::is_incognito_process(),
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 529
499 RenderThread::Get()->RegisterExtension(extension); 530 RenderThread::Get()->RegisterExtension(extension);
500 } 531 }
501 532
502 void ExtensionDispatcher::OnUsingWebRequestAPI( 533 void ExtensionDispatcher::OnUsingWebRequestAPI(
503 bool adblock, bool adblock_plus, bool other) { 534 bool adblock, bool adblock_plus, bool other) {
504 webrequest_adblock_ = adblock; 535 webrequest_adblock_ = adblock;
505 webrequest_adblock_plus_ = adblock_plus; 536 webrequest_adblock_plus_ = adblock_plus;
506 webrequest_other_ = other; 537 webrequest_other_ = other;
507 } 538 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698