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

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

Issue 9386001: Implement a module system for the extension bindings JS. (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 16 matching lines...) Expand all
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
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 #include <iostream>
38 using namespace std;
39
37 namespace { 40 namespace {
41
38 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; 42 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000;
39 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; 43 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000;
44
45 v8::Local<v8::Value> Run(v8::Handle<v8::String> code) {
46 return v8::Script::New(code)->Run();
40 } 47 }
41 48
49 const char* GetResource(int resourceId) {
50 const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
51 return resource_bundle.GetRawDataResource(resourceId).as_string().c_str();
52 }
53
54 v8::Local<v8::Value> Run(int resourceId) {
55 return Run(v8::String::New(GetResource(resourceId)));
56 }
57
58 v8::Handle<v8::Value> Execute(const v8::Arguments& args) {
59 v8::Handle<v8::String> code = v8::Handle<v8::String>::Cast(args[0]);
60 return Run(code);
61 }
62
63 v8::Handle<v8::Value> GetSource(const v8::Arguments& args) {
64 static std::map<std::string, std::string>* js_source_map = NULL;
65 if (js_source_map == NULL) {
66 js_source_map = new std::map<std::string, std::string>();
67
68 (*js_source_map)["app"] = GetResource(IDR_APP_BINDINGS_JS);
69 (*js_source_map)["webstore"] = GetResource(IDR_WEBSTORE_BINDINGS_JS);
70
71 (*js_source_map)["json_schema"] = GetResource(IDR_JSON_SCHEMA_JS);
72 (*js_source_map)["event_bindings"] = GetResource(IDR_EVENT_BINDINGS_JS);
73 (*js_source_map)["miscellaneous_bindings"] =
74 GetResource(IDR_MISCELLANEOUS_BINDINGS_JS);
75 (*js_source_map)["schema_generated_bindings"] =
76 GetResource(IDR_SCHEMA_GENERATED_BINDINGS_JS);
77 (*js_source_map)["apitest"] =
78 GetResource(IDR_EXTENSION_APITEST_JS);
79
80 // Custom hooks.
81 (*js_source_map)["custom/browserAction"] =
82 GetResource(IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS);
83 (*js_source_map)["custom/chromePrivate"] =
84 GetResource(IDR_CHROME_PRIVATE_CUSTOM_BINDINGS_JS);
85 (*js_source_map)["custom/contentSettings"] =
86 GetResource(IDR_CONTENT_SETTINGS_CUSTOM_BINDINGS_JS);
87 (*js_source_map)["custom/contextMenus"] =
88 GetResource(IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS);
89 (*js_source_map)["custom/devtools"] =
90 GetResource(IDR_DEVTOOLS_CUSTOM_BINDINGS_JS);
91 (*js_source_map)["custom/extension"] =
92 GetResource(IDR_EXTENSION_CUSTOM_BINDINGS_JS);
93 (*js_source_map)["custom/fileBrowserHandler"] =
94 GetResource(IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS);
95 (*js_source_map)["custom/fileBrowserPrivateHandler"] =
96 GetResource(IDR_FILE_BROWSER_PRIVATE_CUSTOM_BINDINGS_JS);
97 (*js_source_map)["custom/input.ime"] =
98 GetResource(IDR_INPUT_IME_CUSTOM_BINDINGS_JS);
99 (*js_source_map)["custom/omnibox"] =
100 GetResource(IDR_OMNIBOX_CUSTOM_BINDINGS_JS);
101 (*js_source_map)["custom/pageAction"] =
102 GetResource(IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS);
103 (*js_source_map)["custom/pageActions"] =
104 GetResource(IDR_PAGE_ACTIONS_CUSTOM_BINDINGS_JS);
105 (*js_source_map)["custom/pageCapture"] =
106 GetResource(IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS);
107 (*js_source_map)["custom/socket"] =
108 GetResource(IDR_EXPERIMENTAL_SOCKET_CUSTOM_BINDINGS_JS);
109 (*js_source_map)["custom/storage"] =
110 GetResource(IDR_STORAGE_CUSTOM_BINDINGS_JS);
111 (*js_source_map)["custom/tabs"] =
112 GetResource(IDR_TABS_CUSTOM_BINDINGS_JS);
113 (*js_source_map)["custom/tts"] =
114 GetResource(IDR_TTS_CUSTOM_BINDINGS_JS);
115 (*js_source_map)["custom/ttsEngine"] =
116 GetResource(IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS);
117 (*js_source_map)["custom/types"] =
118 GetResource(IDR_TYPES_CUSTOM_BINDINGS_JS);
119 (*js_source_map)["custom/webRequest"] =
120 GetResource(IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS);
121 (*js_source_map)["custom/windows"] =
122 GetResource(IDR_WINDOWS_CUSTOM_BINDINGS_JS);
123 }
124 std::string module_name = *v8::String::Utf8Value(args[0]->ToString());
125 if (js_source_map->count(module_name) == 0) {
126 return v8::Undefined();
127 }
128 v8::HandleScope scope;
129 return scope.Close(v8::String::New((*js_source_map)[module_name].c_str()));
130 }
131
132 } // namespace
133
42 using namespace extensions; 134 using namespace extensions;
43 135
44 using WebKit::WebDataSource; 136 using WebKit::WebDataSource;
45 using WebKit::WebDocument; 137 using WebKit::WebDocument;
46 using WebKit::WebFrame; 138 using WebKit::WebFrame;
47 using WebKit::WebSecurityPolicy; 139 using WebKit::WebSecurityPolicy;
48 using WebKit::WebString; 140 using WebKit::WebString;
49 using WebKit::WebVector; 141 using WebKit::WebVector;
50 using WebKit::WebView; 142 using WebKit::WebView;
51 using content::RenderThread; 143 using content::RenderThread;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 187
96 void ExtensionDispatcher::WebKitInitialized() { 188 void ExtensionDispatcher::WebKitInitialized() {
97 // For extensions, we want to ensure we call the IdleHandler every so often, 189 // For extensions, we want to ensure we call the IdleHandler every so often,
98 // even if the extension keeps up activity. 190 // even if the extension keeps up activity.
99 if (is_extension_process_) { 191 if (is_extension_process_) {
100 forced_idle_timer_.Start(FROM_HERE, 192 forced_idle_timer_.Start(FROM_HERE,
101 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs), 193 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs),
102 RenderThread::Get(), &RenderThread::IdleHandler); 194 RenderThread::Get(), &RenderThread::IdleHandler);
103 } 195 }
104 196
105 RegisterExtension(new AppBindings(this), false);
106 RegisterExtension(ChromeWebstoreExtension::Get(), false);
107
108 // Add v8 extensions related to chrome extensions.
109 RegisterExtension(new ChromeV8Extension(
110 "extensions/json_schema.js", IDR_JSON_SCHEMA_JS, NULL), true);
111 RegisterExtension(EventBindings::Get(this), true);
112 RegisterExtension(MiscellaneousBindings::Get(this), true);
113 RegisterExtension(SchemaGeneratedBindings::Get(this), true);
114 RegisterExtension(new ChromeV8Extension(
115 "extensions/apitest.js", IDR_EXTENSION_APITEST_JS, NULL), true);
116
117 std::vector<v8::Extension*> custom_bindings =
118 custom_bindings_util::GetAll(this);
119 for (std::vector<v8::Extension*>::iterator it = custom_bindings.begin();
120 it != custom_bindings.end(); ++it) {
121 RegisterExtension(*it, true);
122 }
123
124 // Initialize host permissions for any extensions that were activated before 197 // Initialize host permissions for any extensions that were activated before
125 // WebKit was initialized. 198 // WebKit was initialized.
126 for (std::set<std::string>::iterator iter = active_extension_ids_.begin(); 199 for (std::set<std::string>::iterator iter = active_extension_ids_.begin();
127 iter != active_extension_ids_.end(); ++iter) { 200 iter != active_extension_ids_.end(); ++iter) {
128 const Extension* extension = extensions_.GetByID(*iter); 201 const Extension* extension = extensions_.GetByID(*iter);
129 if (extension) 202 if (extension)
130 InitOriginPermissions(extension); 203 InitOriginPermissions(extension);
131 } 204 }
132 205
133 is_webkit_initialized_ = true; 206 is_webkit_initialized_ = true;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 const std::string& v8_extension_name, 343 const std::string& v8_extension_name,
271 int extension_group) { 344 int extension_group) {
272 return AllowScriptExtension(frame, v8_extension_name, extension_group, 0); 345 return AllowScriptExtension(frame, v8_extension_name, extension_group, 0);
273 } 346 }
274 347
275 bool ExtensionDispatcher::AllowScriptExtension( 348 bool ExtensionDispatcher::AllowScriptExtension(
276 WebFrame* frame, 349 WebFrame* frame,
277 const std::string& v8_extension_name, 350 const std::string& v8_extension_name,
278 int extension_group, 351 int extension_group,
279 int world_id) { 352 int world_id) {
280 // NULL in unit tests. 353 // We haven't registered any extensions, so this must have been added by
281 if (!RenderThread::Get()) 354 // WebCore.
282 return true; 355 // TODO(koz): Have WebCore not ask us and delete this function.
356 return true;
357 }
283 358
284 // If we don't know about it, it was added by WebCore, so we should allow it. 359 bool ExtensionDispatcher::AllowAPI(
285 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name)) 360 WebFrame* frame,
286 return true; 361 const std::string& module_name,
287 362 int extension_group,
288 // If the V8 extension is not restricted, allow it to run anywhere. 363 int world_id) {
289 if (!restricted_v8_extensions_.count(v8_extension_name))
290 return true;
291
292 // Extension-only bindings should be restricted to content scripts and 364 // Extension-only bindings should be restricted to content scripts and
293 // extension-blessed URLs. 365 // extension-blessed URLs.
294 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS || 366 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS ||
295 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo( 367 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo(
296 frame->document().securityOrigin(), 368 frame->document().securityOrigin(),
297 UserScriptSlave::GetDataSourceURLForFrame(frame)))) { 369 UserScriptSlave::GetDataSourceURLForFrame(frame)))) {
298 // If the extension is a custom API binding, only allow if the extension 370 // If the extension is a custom API binding, only allow if the extension
299 // has permission to use the API. 371 // has permission to use the API.
300 std::string custom_binding_api_name = 372 std::string extension_id = GetExtensionID(frame, world_id);
301 custom_bindings_util::GetAPIName(v8_extension_name); 373 const Extension* extension = extensions_.GetByID(extension_id);
302 if (!custom_binding_api_name.empty()) { 374 if (!extension) {
303 std::string extension_id = GetExtensionID(frame, world_id); 375 // This can happen when a resource is blocked due to CSP; a valid
304 const Extension* extension = extensions_.GetByID(extension_id); 376 // chrome-extension:// URL is navigated to, so it passes the initial
305 if (!extension) { 377 // checks, but the URL gets changed to "chrome-extension://invalid"
306 // This can happen when a resource is blocked due to CSP; a valid 378 // afterwards (see chrome_content_renderer_client.cc). An extension
307 // chrome-extension:// URL is navigated to, so it passes the initial 379 // page still gets loaded, just for the extension with ID "invalid",
308 // checks, but the URL gets changed to "chrome-extension://invalid" 380 // which of course isn't found so GetById extension will be NULL.
309 // afterwards (see chrome_content_renderer_client.cc). An extension 381 //
310 // page still gets loaded, just for the extension with ID "invalid", 382 // Reference: http://crbug.com/111614.
311 // which of course isn't found so GetById extension will be NULL. 383 CHECK_EQ("invalid", extension_id);
312 // 384 return false;
313 // Reference: http://crbug.com/111614.
314 CHECK_EQ("invalid", extension_id);
315 return false;
316 }
317 return custom_bindings_util::AllowAPIInjection(
318 custom_binding_api_name, *extension);
319 } 385 }
320 386 return custom_bindings_util::AllowAPIInjection(
321 return true; 387 module_name, *extension);
322 } 388 }
323 389
324 return false; 390 return false;
325 } 391 }
326 392
393 class GlobalFunctions : public ChromeV8Extension {
394 public:
395 GlobalFunctions(ExtensionDispatcher* dispatcher,
396 WebFrame* frame,
397 int extension_group,
398 int world_id)
399 : ChromeV8Extension("blah", 0, dispatcher),
400 frame_(frame),
401 extension_group_(extension_group),
402 world_id_(world_id) {}
403
404 // ChromeV8Extension
405 virtual void SetNativeFunctions(v8::Handle<v8::Object> object) OVERRIDE {
406 RouteFunctionHere("AllowAPI", object);
407 RouteFunctionToStatic("GetChromeHidden", GetChromeHidden, object);
408 RouteFunctionToStatic("Print", Print, object);
409 }
410
411 v8::Handle<v8::Value> HandleNativeFunction(const std::string& name,
412 const v8::Arguments& args) OVERRIDE {
413 CHECK(!name.empty());
414 if (name == "AllowAPI") {
415 return AllowAPI(args);
416 } else {
417 CHECK(false) << "Unknown native function: '" << name << "'" << endl;
418 }
419 return v8::Undefined();
420 }
421
422 v8::Handle<v8::Value> AllowAPI(const v8::Arguments& args) {
423 std::string api_name = *v8::String::AsciiValue(args[0]);
424 return v8::Boolean::New(extension_dispatcher_->AllowAPI(frame_,
425 api_name, extension_group_, world_id_));
426 }
427
428 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) {
429 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent());
430 }
431
432 static v8::Handle<v8::Value> Print(const v8::Arguments& args) {
433 if (args.Length() < 1)
434 return v8::Undefined();
435
436 std::vector<std::string> components;
437 for (int i = 0; i < args.Length(); ++i)
438 components.push_back(*v8::String::Utf8Value(args[i]->ToString()));
439
440 LOG(ERROR) << JoinString(components, ',');
441 return v8::Undefined();
442 }
443
444 private:
445 WebFrame* frame_;
446 int extension_group_;
447 int world_id_;
448 };
449
450 v8::Handle<v8::Object> ExtensionDispatcher::BuildBrowserObject(
451 ChromeV8Context* context) {
452 v8::Persistent<v8::Object> browser =
453 v8::Persistent<v8::Object>::New(v8::Object::New());
454
455 browser->Set(v8::String::NewSymbol("Execute"),
456 v8::FunctionTemplate::New(Execute)->GetFunction());
457 browser->Set(v8::String::NewSymbol("GetSource"),
458 v8::FunctionTemplate::New(GetSource)->GetFunction());
459 browser->Set(v8::String::NewSymbol("natives"), context->natives());
460 RegisterExtensions(context);
461 return browser;
462 }
463
464 void ExtensionDispatcher::RegisterExtensions(ChromeV8Context* context) {
465 context->RegisterExtension(new AppBindings(this, context));
466 context->RegisterExtension(ChromeWebstoreExtension::Get(this));
467 context->RegisterExtension(EventBindings::Get(this));
468 context->RegisterExtension(MiscellaneousBindings::Get(this));
469 context->RegisterExtension(SchemaGeneratedBindings::Get(this));
470
471 std::vector<ChromeV8Extension*> extensions =
472 custom_bindings_util::GetAll(this);
473 for (std::vector<ChromeV8Extension*>::iterator i = extensions.begin();
474 i != extensions.end(); i++) {
475 context->RegisterExtension(*i);
476 }
477 }
478
327 void ExtensionDispatcher::DidCreateScriptContext( 479 void ExtensionDispatcher::DidCreateScriptContext(
328 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { 480 WebFrame* frame, v8::Handle<v8::Context> v8_context, int extension_group,
481 int world_id) {
482 ExtensionURLInfo thing(
483 frame->document().securityOrigin(),
484 UserScriptSlave::GetDataSourceURLForFrame(frame));
485 std::string extension_id = GetExtensionID(frame, world_id);
329 ChromeV8Context* context = 486 ChromeV8Context* context =
330 new ChromeV8Context(v8_context, frame, GetExtensionID(frame, world_id)); 487 new ChromeV8Context(v8_context, frame, extension_id);
331 v8_context_set_.Add(context); 488 v8_context_set_.Add(context);
489 SetupAPIBindings(context, frame, extension_group, world_id);
490
491 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size();
492 }
493
494 void ExtensionDispatcher::SetupAPIBindings(ChromeV8Context* context,
495 WebFrame* frame,
496 int extension_group,
497 int world_id) {
498 context->RegisterExtension(new GlobalFunctions(this, context->web_frame(),
499 extension_group, world_id));
332 500
333 const Extension* extension = extensions_.GetByID(context->extension_id()); 501 const Extension* extension = extensions_.GetByID(context->extension_id());
334 int manifest_version = 1; 502 int manifest_version = 1;
335 if (extension) 503 if (extension)
336 manifest_version = extension->manifest_version(); 504 manifest_version = extension->manifest_version();
337 505
338 context->DispatchOnLoadEvent( 506 Run(v8::String::New("var chrome = {};"));
339 is_extension_process_, 507 v8::Local<v8::Value> result = Run(IDR_BOOTSTRAP_JS);
340 ChromeRenderProcessObserver::is_incognito_process(), 508 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(result);
341 manifest_version);
342 509
343 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); 510 v8::Handle<v8::Object> browser = BuildBrowserObject(context);
511
512 bool allow_extension_apis =
513 extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS ||
514 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo(
515 frame->document().securityOrigin(),
516 UserScriptSlave::GetDataSourceURLForFrame(frame)));
517
518 v8::Handle<v8::Value> argv[] = {
519 v8::Local<v8::Value>::New(browser),
520 v8::String::New(context->extension_id().c_str()),
521 v8::Boolean::New(is_extension_process_),
522 v8::Boolean::New(ChromeRenderProcessObserver::is_incognito_process()),
523 v8::Integer::New(manifest_version),
524 v8::Boolean::New(allow_extension_apis)
525 };
526
527 f->Call(v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
344 } 528 }
345 529
346 std::string ExtensionDispatcher::GetExtensionID(WebFrame* frame, int world_id) { 530 std::string ExtensionDispatcher::GetExtensionID(WebFrame* frame, int world_id) {
347 if (!test_extension_id_.empty()) { 531 if (!test_extension_id_.empty()) {
348 return test_extension_id_; 532 return test_extension_id_;
349 } else if (world_id != 0) { 533 } else if (world_id != 0) {
350 // Isolated worlds (content script). 534 // Isolated worlds (content script).
351 return user_script_slave_->GetExtensionIdForIsolatedWorld(world_id); 535 return user_script_slave_->GetExtensionIdForIsolatedWorld(world_id);
352 } else { 536 } else {
353 // Extension pages (chrome-extension:// URLs). 537 // Extension pages (chrome-extension:// URLs).
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 682
499 RenderThread::Get()->RegisterExtension(extension); 683 RenderThread::Get()->RegisterExtension(extension);
500 } 684 }
501 685
502 void ExtensionDispatcher::OnUsingWebRequestAPI( 686 void ExtensionDispatcher::OnUsingWebRequestAPI(
503 bool adblock, bool adblock_plus, bool other) { 687 bool adblock, bool adblock_plus, bool other) {
504 webrequest_adblock_ = adblock; 688 webrequest_adblock_ = adblock;
505 webrequest_adblock_plus_ = adblock_plus; 689 webrequest_adblock_plus_ = adblock_plus;
506 webrequest_other_ = other; 690 webrequest_other_ = other;
507 } 691 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698