OLD | NEW |
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/dispatcher.h" | 5 #include "chrome/renderer/extensions/dispatcher.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/values.h" |
15 #include "chrome/common/child_process_logging.h" | 16 #include "chrome/common/child_process_logging.h" |
16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/chrome_version_info.h" | 18 #include "chrome/common/chrome_version_info.h" |
18 #include "chrome/common/extensions/api/extension_api.h" | 19 #include "chrome/common/extensions/api/extension_api.h" |
19 #include "chrome/common/extensions/background_info.h" | 20 #include "chrome/common/extensions/background_info.h" |
20 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
21 #include "chrome/common/extensions/extension_messages.h" | 22 #include "chrome/common/extensions/extension_messages.h" |
22 #include "chrome/common/extensions/features/base_feature_provider.h" | 23 #include "chrome/common/extensions/features/base_feature_provider.h" |
23 #include "chrome/common/extensions/features/feature.h" | 24 #include "chrome/common/extensions/features/feature.h" |
24 #include "chrome/common/extensions/manifest.h" | 25 #include "chrome/common/extensions/manifest.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 using WebKit::WebView; | 93 using WebKit::WebView; |
93 using content::RenderThread; | 94 using content::RenderThread; |
94 using content::RenderView; | 95 using content::RenderView; |
95 | 96 |
96 namespace extensions { | 97 namespace extensions { |
97 | 98 |
98 namespace { | 99 namespace { |
99 | 100 |
100 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; | 101 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; |
101 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; | 102 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; |
102 static const char kEventDispatchFunction[] = "Event.dispatchEvent"; | 103 static const char kEventModule[] = "event_bindings"; |
| 104 static const char kEventDispatchFunction[] = "dispatchEvent"; |
103 static const char kOnSuspendEvent[] = "runtime.onSuspend"; | 105 static const char kOnSuspendEvent[] = "runtime.onSuspend"; |
104 static const char kOnSuspendCanceledEvent[] = "runtime.onSuspendCanceled"; | 106 static const char kOnSuspendCanceledEvent[] = "runtime.onSuspendCanceled"; |
105 | 107 |
106 static v8::Handle<v8::Object> GetOrCreateChrome( | 108 static v8::Handle<v8::Object> GetOrCreateChrome( |
107 v8::Handle<v8::Context> context) { | 109 v8::Handle<v8::Context> context) { |
108 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); | 110 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); |
109 v8::Handle<v8::Object> global(context->Global()); | 111 v8::Handle<v8::Object> global(context->Global()); |
110 v8::Handle<v8::Value> chrome(global->Get(chrome_string)); | 112 v8::Handle<v8::Value> chrome(global->Get(chrome_string)); |
111 if (chrome.IsEmpty() || chrome->IsUndefined()) { | 113 if (chrome.IsEmpty() || chrome->IsUndefined()) { |
112 v8::Handle<v8::Object> chrome_object(v8::Object::New()); | 114 v8::Handle<v8::Object> chrome_object(v8::Object::New()); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 base::Unretained(this))); | 323 base::Unretained(this))); |
322 RouteFunction("InIncognitoContext", | 324 RouteFunction("InIncognitoContext", |
323 base::Bind(&ProcessInfoNativeHandler::InIncognitoContext, | 325 base::Bind(&ProcessInfoNativeHandler::InIncognitoContext, |
324 base::Unretained(this))); | 326 base::Unretained(this))); |
325 RouteFunction("GetManifestVersion", | 327 RouteFunction("GetManifestVersion", |
326 base::Bind(&ProcessInfoNativeHandler::GetManifestVersion, | 328 base::Bind(&ProcessInfoNativeHandler::GetManifestVersion, |
327 base::Unretained(this))); | 329 base::Unretained(this))); |
328 RouteFunction("IsSendRequestDisabled", | 330 RouteFunction("IsSendRequestDisabled", |
329 base::Bind(&ProcessInfoNativeHandler::IsSendRequestDisabled, | 331 base::Bind(&ProcessInfoNativeHandler::IsSendRequestDisabled, |
330 base::Unretained(this))); | 332 base::Unretained(this))); |
| 333 RouteFunction("Log", |
| 334 base::Bind(&ProcessInfoNativeHandler::Log, base::Unretained(this))); |
331 } | 335 } |
332 | 336 |
333 v8::Handle<v8::Value> GetExtensionId(const v8::Arguments& args) { | 337 v8::Handle<v8::Value> GetExtensionId(const v8::Arguments& args) { |
334 return v8::String::New(extension_id_.c_str()); | 338 return v8::String::New(extension_id_.c_str()); |
335 } | 339 } |
336 | 340 |
337 v8::Handle<v8::Value> GetContextType(const v8::Arguments& args) { | 341 v8::Handle<v8::Value> GetContextType(const v8::Arguments& args) { |
338 return v8::String::New(context_type_.c_str()); | 342 return v8::String::New(context_type_.c_str()); |
339 } | 343 } |
340 | 344 |
341 v8::Handle<v8::Value> InIncognitoContext(const v8::Arguments& args) { | 345 v8::Handle<v8::Value> InIncognitoContext(const v8::Arguments& args) { |
342 return v8::Boolean::New(is_incognito_context_); | 346 return v8::Boolean::New(is_incognito_context_); |
343 } | 347 } |
344 | 348 |
345 v8::Handle<v8::Value> GetManifestVersion(const v8::Arguments& args) { | 349 v8::Handle<v8::Value> GetManifestVersion(const v8::Arguments& args) { |
346 return v8::Integer::New(manifest_version_); | 350 return v8::Integer::New(manifest_version_); |
347 } | 351 } |
348 | 352 |
349 v8::Handle<v8::Value> IsSendRequestDisabled(const v8::Arguments& args) { | 353 v8::Handle<v8::Value> IsSendRequestDisabled(const v8::Arguments& args) { |
350 if (send_request_disabled_) { | 354 if (send_request_disabled_) { |
351 return v8::String::New( | 355 return v8::String::New( |
352 "sendRequest and onRequest are obsolete." | 356 "sendRequest and onRequest are obsolete." |
353 " Please use sendMessage and onMessage instead."); | 357 " Please use sendMessage and onMessage instead."); |
354 } | 358 } |
355 return v8::Undefined(); | 359 return v8::Undefined(); |
356 } | 360 } |
357 | 361 |
| 362 v8::Handle<v8::Value> Log(const v8::Arguments& args) { |
| 363 LOG(WARNING) << *v8::String::AsciiValue(args[0]); |
| 364 return v8::Undefined(); |
| 365 } |
| 366 |
358 private: | 367 private: |
359 std::string extension_id_; | 368 std::string extension_id_; |
360 std::string context_type_; | 369 std::string context_type_; |
361 bool is_incognito_context_; | 370 bool is_incognito_context_; |
362 int manifest_version_; | 371 int manifest_version_; |
363 bool send_request_disabled_; | 372 bool send_request_disabled_; |
364 }; | 373 }; |
365 | 374 |
366 class LoggingNativeHandler : public ObjectBackedNativeHandler { | 375 class LoggingNativeHandler : public ObjectBackedNativeHandler { |
367 public: | 376 public: |
368 explicit LoggingNativeHandler(v8::Handle<v8::Context> context) | 377 explicit LoggingNativeHandler(v8::Handle<v8::Context> context) |
369 : ObjectBackedNativeHandler(context) { | 378 : ObjectBackedNativeHandler(context) { |
370 RouteFunction("DCHECK", | 379 RouteFunction("DCHECK", |
371 base::Bind(&LoggingNativeHandler::Dcheck, base::Unretained(this))); | 380 base::Bind(&LoggingNativeHandler::Dcheck, base::Unretained(this))); |
372 RouteFunction("CHECK", | 381 RouteFunction("CHECK", |
373 base::Bind(&LoggingNativeHandler::Check, base::Unretained(this))); | 382 base::Bind(&LoggingNativeHandler::Check, base::Unretained(this))); |
| 383 RouteFunction("DCHECK_IS_ON", |
| 384 base::Bind(&LoggingNativeHandler::DcheckIsOn, base::Unretained(this))); |
374 } | 385 } |
375 | 386 |
376 v8::Handle<v8::Value> Check(const v8::Arguments& args) { | 387 v8::Handle<v8::Value> Check(const v8::Arguments& args) { |
377 bool check_value; | 388 bool check_value; |
378 std::string error_message; | 389 std::string error_message; |
379 ParseArgs(args, &check_value, &error_message); | 390 ParseArgs(args, &check_value, &error_message); |
380 CHECK(check_value) << error_message; | 391 CHECK(check_value) << error_message; |
381 return v8::Undefined(); | 392 return v8::Undefined(); |
382 } | 393 } |
383 | 394 |
384 v8::Handle<v8::Value> Dcheck(const v8::Arguments& args) { | 395 v8::Handle<v8::Value> Dcheck(const v8::Arguments& args) { |
385 bool check_value; | 396 bool check_value; |
386 std::string error_message; | 397 std::string error_message; |
387 ParseArgs(args, &check_value, &error_message); | 398 ParseArgs(args, &check_value, &error_message); |
388 DCHECK(check_value) << error_message; | 399 DCHECK(check_value) << error_message; |
389 return v8::Undefined(); | 400 return v8::Undefined(); |
390 } | 401 } |
391 | 402 |
| 403 v8::Handle<v8::Value> DcheckIsOn(const v8::Arguments& args) { |
| 404 return v8::Boolean::New(DCHECK_IS_ON()); |
| 405 } |
| 406 |
392 private: | 407 private: |
393 void ParseArgs(const v8::Arguments& args, | 408 void ParseArgs(const v8::Arguments& args, |
394 bool* check_value, | 409 bool* check_value, |
395 std::string* error_message) { | 410 std::string* error_message) { |
396 CHECK_LE(args.Length(), 2); | 411 CHECK_LE(args.Length(), 2); |
397 *check_value = args[0]->BooleanValue(); | 412 *check_value = args[0]->BooleanValue(); |
398 if (args.Length() == 2) | 413 if (args.Length() == 2) |
399 *error_message = "Error: " + std::string( | 414 *error_message = "Error: " + std::string( |
400 *v8::String::AsciiValue(args[1])); | 415 *v8::String::AsciiValue(args[1])); |
401 | 416 |
(...skipping 20 matching lines...) Expand all Loading... |
422 return dflt; | 437 return dflt; |
423 std::string ascii_value = *v8::String::AsciiValue(v8_string); | 438 std::string ascii_value = *v8::String::AsciiValue(v8_string); |
424 return ascii_value.empty() ? dflt : ascii_value; | 439 return ascii_value.empty() ? dflt : ascii_value; |
425 } | 440 } |
426 }; | 441 }; |
427 | 442 |
428 void InstallAppBindings(ModuleSystem* module_system, | 443 void InstallAppBindings(ModuleSystem* module_system, |
429 v8::Handle<v8::Object> chrome, | 444 v8::Handle<v8::Object> chrome, |
430 v8::Handle<v8::Object> chrome_hidden) { | 445 v8::Handle<v8::Object> chrome_hidden) { |
431 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); | 446 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); |
432 module_system->SetLazyField(chrome_hidden, "app", "app", | |
433 "chromeHiddenApp"); | |
434 } | 447 } |
435 | 448 |
436 void InstallWebstoreBindings(ModuleSystem* module_system, | 449 void InstallWebstoreBindings(ModuleSystem* module_system, |
437 v8::Handle<v8::Object> chrome, | 450 v8::Handle<v8::Object> chrome, |
438 v8::Handle<v8::Object> chrome_hidden) { | 451 v8::Handle<v8::Object> chrome_hidden) { |
439 module_system->SetLazyField(chrome, "webstore", "webstore", "chromeWebstore"); | 452 module_system->SetLazyField(chrome, "webstore", "webstore", "chromeWebstore"); |
440 module_system->SetLazyField(chrome_hidden, "webstore", "webstore", | 453 module_system->SetLazyField(chrome_hidden, "webstore", "webstore", |
441 "chromeHiddenWebstore"); | 454 "chromeHiddenWebstore"); |
442 } | 455 } |
443 | 456 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 for (size_t i = 0; i < names.size(); ++i) | 556 for (size_t i = 0; i < names.size(); ++i) |
544 function_names_.insert(names[i]); | 557 function_names_.insert(names[i]); |
545 } | 558 } |
546 | 559 |
547 void Dispatcher::OnSetChannel(int channel) { | 560 void Dispatcher::OnSetChannel(int channel) { |
548 Feature::SetCurrentChannel( | 561 Feature::SetCurrentChannel( |
549 static_cast<chrome::VersionInfo::Channel>(channel)); | 562 static_cast<chrome::VersionInfo::Channel>(channel)); |
550 } | 563 } |
551 | 564 |
552 void Dispatcher::OnMessageInvoke(const std::string& extension_id, | 565 void Dispatcher::OnMessageInvoke(const std::string& extension_id, |
| 566 const std::string& module_name, |
553 const std::string& function_name, | 567 const std::string& function_name, |
554 const base::ListValue& args, | 568 const base::ListValue& args, |
555 bool user_gesture) { | 569 bool user_gesture) { |
556 scoped_ptr<WebScopedUserGesture> web_user_gesture; | 570 scoped_ptr<WebScopedUserGesture> web_user_gesture; |
557 if (user_gesture) { | 571 if (user_gesture) { |
558 web_user_gesture.reset(new WebScopedUserGesture); | 572 web_user_gesture.reset(new WebScopedUserGesture); |
559 } | 573 } |
560 | 574 |
561 v8_context_set_.DispatchChromeHiddenMethod( | 575 v8_context_set_.ForEach( |
562 extension_id, function_name, args, NULL); | 576 extension_id, |
| 577 NULL, |
| 578 base::Bind(&CallModuleMethod, module_name, function_name, &args)); |
563 | 579 |
564 // Reset the idle handler each time there's any activity like event or message | 580 // Reset the idle handler each time there's any activity like event or message |
565 // dispatch, for which Invoke is the chokepoint. | 581 // dispatch, for which Invoke is the chokepoint. |
566 if (is_extension_process_) { | 582 if (is_extension_process_) { |
567 RenderThread::Get()->ScheduleIdleHandler( | 583 RenderThread::Get()->ScheduleIdleHandler( |
568 kInitialExtensionIdleHandlerDelayMs); | 584 kInitialExtensionIdleHandlerDelayMs); |
569 } | 585 } |
570 | 586 |
571 // Tell the browser process when an event has been dispatched with a lazy | 587 // Tell the browser process when an event has been dispatched with a lazy |
572 // background page active. | 588 // background page active. |
573 const Extension* extension = extensions_.GetByID(extension_id); | 589 const Extension* extension = extensions_.GetByID(extension_id); |
574 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension) && | 590 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension) && |
| 591 module_name == kEventModule && |
575 function_name == kEventDispatchFunction) { | 592 function_name == kEventDispatchFunction) { |
576 RenderView* background_view = | 593 RenderView* background_view = |
577 ExtensionHelper::GetBackgroundPage(extension_id); | 594 ExtensionHelper::GetBackgroundPage(extension_id); |
578 if (background_view) { | 595 if (background_view) { |
579 background_view->Send(new ExtensionHostMsg_EventAck( | 596 background_view->Send(new ExtensionHostMsg_EventAck( |
580 background_view->GetRoutingID())); | 597 background_view->GetRoutingID())); |
581 } | 598 } |
582 } | 599 } |
583 } | 600 } |
584 | 601 |
585 void Dispatcher::OnDispatchOnConnect( | 602 void Dispatcher::OnDispatchOnConnect( |
586 int target_port_id, | 603 int target_port_id, |
587 const std::string& channel_name, | 604 const std::string& channel_name, |
588 const base::DictionaryValue& source_tab, | 605 const base::DictionaryValue& source_tab, |
589 const ExtensionMsg_ExternalConnectionInfo& info) { | 606 const ExtensionMsg_ExternalConnectionInfo& info) { |
590 MiscellaneousBindings::DispatchOnConnect( | 607 MiscellaneousBindings::DispatchOnConnect( |
591 v8_context_set_.GetAll(), | 608 v8_context_set_.GetAll(), |
592 target_port_id, channel_name, source_tab, | 609 target_port_id, channel_name, source_tab, |
593 info.source_id, info.target_id, info.source_url, | 610 info.source_id, info.target_id, info.source_url, |
594 NULL); // All render views. | 611 NULL); // All render views. |
595 } | 612 } |
596 | 613 |
597 void Dispatcher::OnDeliverMessage(int target_port_id, | 614 void Dispatcher::OnDeliverMessage(int target_port_id, |
598 const std::string& message) { | 615 const base::ListValue& message) { |
599 MiscellaneousBindings::DeliverMessage( | 616 MiscellaneousBindings::DeliverMessage( |
600 v8_context_set_.GetAll(), | 617 v8_context_set_.GetAll(), |
601 target_port_id, | 618 target_port_id, |
602 message, | 619 message, |
603 NULL); // All render views. | 620 NULL); // All render views. |
604 } | 621 } |
605 | 622 |
606 void Dispatcher::OnDispatchOnDisconnect(int port_id, | 623 void Dispatcher::OnDispatchOnDisconnect(int port_id, |
607 const std::string& error_message) { | 624 const std::string& error_message) { |
608 MiscellaneousBindings::DispatchOnDisconnect( | 625 MiscellaneousBindings::DispatchOnDisconnect( |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 api_name, | 801 api_name, |
785 "binding"); | 802 "binding"); |
786 } | 803 } |
787 } | 804 } |
788 } | 805 } |
789 | 806 |
790 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, | 807 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, |
791 ChromeV8Context* context) { | 808 ChromeV8Context* context) { |
792 v8::Handle<v8::Context> v8_context = context->v8_context(); | 809 v8::Handle<v8::Context> v8_context = context->v8_context(); |
793 | 810 |
794 module_system->RegisterNativeHandler("event_bindings", | 811 module_system->RegisterNativeHandler("event_natives", |
795 scoped_ptr<NativeHandler>(EventBindings::Create(this, v8_context))); | 812 scoped_ptr<NativeHandler>(EventBindings::Create(this, v8_context))); |
796 module_system->RegisterNativeHandler("miscellaneous_bindings", | 813 module_system->RegisterNativeHandler("miscellaneous_bindings_natives", |
797 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this, v8_context))); | 814 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this, v8_context))); |
798 module_system->RegisterNativeHandler("apiDefinitions", | 815 module_system->RegisterNativeHandler("apiDefinitions", |
799 scoped_ptr<NativeHandler>(new ApiDefinitionsNatives(this, context))); | 816 scoped_ptr<NativeHandler>(new ApiDefinitionsNatives(this, context))); |
800 module_system->RegisterNativeHandler("sendRequest", | 817 module_system->RegisterNativeHandler("sendRequest", |
801 scoped_ptr<NativeHandler>( | 818 scoped_ptr<NativeHandler>( |
802 new SendRequestNatives(this, request_sender_.get(), context))); | 819 new SendRequestNatives(this, request_sender_.get(), context))); |
803 module_system->RegisterNativeHandler("setIcon", | 820 module_system->RegisterNativeHandler("setIcon", |
804 scoped_ptr<NativeHandler>( | 821 scoped_ptr<NativeHandler>( |
805 new SetIconNatives(this, request_sender_.get(), context))); | 822 new SetIconNatives(this, request_sender_.get(), context))); |
806 module_system->RegisterNativeHandler( | 823 module_system->RegisterNativeHandler( |
807 "contentWatcherNative", | 824 "contentWatcherNative", |
808 content_watcher_->MakeNatives(v8_context)); | 825 content_watcher_->MakeNatives(v8_context)); |
809 module_system->RegisterNativeHandler("activityLogger", | 826 module_system->RegisterNativeHandler("activityLogger", |
810 scoped_ptr<NativeHandler>(new APIActivityLogger(this, v8_context))); | 827 scoped_ptr<NativeHandler>(new APIActivityLogger(this, v8_context))); |
811 | 828 |
812 // Natives used by multiple APIs. | 829 // Natives used by multiple APIs. |
813 module_system->RegisterNativeHandler("file_system_natives", | 830 module_system->RegisterNativeHandler("file_system_natives", |
814 scoped_ptr<NativeHandler>(new FileSystemNatives(v8_context))); | 831 scoped_ptr<NativeHandler>(new FileSystemNatives(v8_context))); |
815 | 832 |
816 // Custom bindings. | 833 // Custom bindings. |
817 module_system->RegisterNativeHandler("app", | 834 module_system->RegisterNativeHandler("app", |
818 scoped_ptr<NativeHandler>(new AppBindings(this, context))); | 835 scoped_ptr<NativeHandler>(new AppBindings(this, context))); |
819 module_system->RegisterNativeHandler("app_runtime", | 836 module_system->RegisterNativeHandler("app_runtime", |
820 scoped_ptr<NativeHandler>( | 837 scoped_ptr<NativeHandler>( |
821 new AppRuntimeCustomBindings(this, v8_context))); | 838 new AppRuntimeCustomBindings(this, v8_context))); |
822 module_system->RegisterNativeHandler("app_window", | 839 module_system->RegisterNativeHandler("app_window_natives", |
823 scoped_ptr<NativeHandler>( | 840 scoped_ptr<NativeHandler>( |
824 new AppWindowCustomBindings(this, v8_context))); | 841 new AppWindowCustomBindings(this, v8_context))); |
825 module_system->RegisterNativeHandler("context_menus", | 842 module_system->RegisterNativeHandler("context_menus", |
826 scoped_ptr<NativeHandler>( | 843 scoped_ptr<NativeHandler>( |
827 new ContextMenusCustomBindings(this, v8_context))); | 844 new ContextMenusCustomBindings(this, v8_context))); |
828 module_system->RegisterNativeHandler("extension", | 845 module_system->RegisterNativeHandler("extension", |
829 scoped_ptr<NativeHandler>( | 846 scoped_ptr<NativeHandler>( |
830 new ExtensionCustomBindings(this, v8_context))); | 847 new ExtensionCustomBindings(this, v8_context))); |
831 module_system->RegisterNativeHandler("sync_file_system", | 848 module_system->RegisterNativeHandler("sync_file_system", |
832 scoped_ptr<NativeHandler>( | 849 scoped_ptr<NativeHandler>( |
(...skipping 23 matching lines...) Expand all Loading... |
856 module_system->RegisterNativeHandler("tts", | 873 module_system->RegisterNativeHandler("tts", |
857 scoped_ptr<NativeHandler>(new TTSCustomBindings(this, v8_context))); | 874 scoped_ptr<NativeHandler>(new TTSCustomBindings(this, v8_context))); |
858 module_system->RegisterNativeHandler("web_request", | 875 module_system->RegisterNativeHandler("web_request", |
859 scoped_ptr<NativeHandler>( | 876 scoped_ptr<NativeHandler>( |
860 new WebRequestCustomBindings(this, v8_context))); | 877 new WebRequestCustomBindings(this, v8_context))); |
861 module_system->RegisterNativeHandler("webstore", | 878 module_system->RegisterNativeHandler("webstore", |
862 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context))); | 879 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context))); |
863 } | 880 } |
864 | 881 |
865 void Dispatcher::PopulateSourceMap() { | 882 void Dispatcher::PopulateSourceMap() { |
866 source_map_.RegisterSource("event_bindings", IDR_EVENT_BINDINGS_JS); | 883 // Libraries. |
867 source_map_.RegisterSource("miscellaneous_bindings", | 884 source_map_.RegisterSource("contentWatcher", IDR_CONTENT_WATCHER_JS); |
868 IDR_MISCELLANEOUS_BINDINGS_JS); | 885 source_map_.RegisterSource("entryIdManager", IDR_ENTRY_ID_MANAGER); |
| 886 source_map_.RegisterSource(kEventModule, IDR_EVENT_BINDINGS_JS); |
| 887 source_map_.RegisterSource("imageUtil", IDR_IMAGE_UTIL_JS); |
869 source_map_.RegisterSource("json", IDR_JSON_JS); | 888 source_map_.RegisterSource("json", IDR_JSON_JS); |
870 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS); | 889 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS); |
871 source_map_.RegisterSource("test", IDR_TEST_CUSTOM_BINDINGS_JS); | |
872 | |
873 // Libraries. | |
874 source_map_.RegisterSource("contentWatcher", IDR_CONTENT_WATCHER_JS); | |
875 source_map_.RegisterSource("imageUtil", IDR_IMAGE_UTIL_JS); | |
876 source_map_.RegisterSource("lastError", IDR_LAST_ERROR_JS); | 890 source_map_.RegisterSource("lastError", IDR_LAST_ERROR_JS); |
| 891 source_map_.RegisterSource("miscellaneous_bindings", |
| 892 IDR_MISCELLANEOUS_BINDINGS_JS); |
| 893 source_map_.RegisterSource("on_unload", IDR_ON_UNLOAD_JS); |
877 source_map_.RegisterSource("schemaUtils", IDR_SCHEMA_UTILS_JS); | 894 source_map_.RegisterSource("schemaUtils", IDR_SCHEMA_UTILS_JS); |
878 source_map_.RegisterSource("sendRequest", IDR_SEND_REQUEST_JS); | 895 source_map_.RegisterSource("sendRequest", IDR_SEND_REQUEST_JS); |
879 source_map_.RegisterSource("setIcon", IDR_SET_ICON_JS); | 896 source_map_.RegisterSource("setIcon", IDR_SET_ICON_JS); |
| 897 source_map_.RegisterSource("test", IDR_TEST_CUSTOM_BINDINGS_JS); |
880 source_map_.RegisterSource("utils", IDR_UTILS_JS); | 898 source_map_.RegisterSource("utils", IDR_UTILS_JS); |
881 source_map_.RegisterSource("entryIdManager", IDR_ENTRY_ID_MANAGER); | |
882 | 899 |
883 // Custom bindings. | 900 // Custom bindings. |
884 source_map_.RegisterSource("app", IDR_APP_CUSTOM_BINDINGS_JS); | 901 source_map_.RegisterSource("app", IDR_APP_CUSTOM_BINDINGS_JS); |
885 source_map_.RegisterSource("app.runtime", IDR_APP_RUNTIME_CUSTOM_BINDINGS_JS); | 902 source_map_.RegisterSource("app.runtime", IDR_APP_RUNTIME_CUSTOM_BINDINGS_JS); |
886 source_map_.RegisterSource("app.window", IDR_APP_WINDOW_CUSTOM_BINDINGS_JS); | 903 source_map_.RegisterSource("app.window", IDR_APP_WINDOW_CUSTOM_BINDINGS_JS); |
887 source_map_.RegisterSource("bluetooth", IDR_BLUETOOTH_CUSTOM_BINDINGS_JS); | 904 source_map_.RegisterSource("bluetooth", IDR_BLUETOOTH_CUSTOM_BINDINGS_JS); |
888 source_map_.RegisterSource("browserAction", | 905 source_map_.RegisterSource("browserAction", |
889 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS); | 906 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS); |
890 source_map_.RegisterSource("contextMenus", | 907 source_map_.RegisterSource("contextMenus", |
891 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS); | 908 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 ExtensionURLInfo url_info(frame->document().securityOrigin(), | 1028 ExtensionURLInfo url_info(frame->document().securityOrigin(), |
1012 UserScriptSlave::GetDataSourceURLForFrame(frame)); | 1029 UserScriptSlave::GetDataSourceURLForFrame(frame)); |
1013 | 1030 |
1014 Feature::Context context_type = | 1031 Feature::Context context_type = |
1015 ClassifyJavaScriptContext(extension_id, extension_group, url_info); | 1032 ClassifyJavaScriptContext(extension_id, extension_group, url_info); |
1016 | 1033 |
1017 ChromeV8Context* context = | 1034 ChromeV8Context* context = |
1018 new ChromeV8Context(v8_context, frame, extension, context_type); | 1035 new ChromeV8Context(v8_context, frame, extension, context_type); |
1019 v8_context_set_.Add(context); | 1036 v8_context_set_.Add(context); |
1020 | 1037 |
1021 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(v8_context, | 1038 scoped_ptr<ModuleSystem> module_system( |
1022 &source_map_)); | 1039 new ModuleSystem(context, &source_map_)); |
1023 // Enable natives in startup. | 1040 // Enable natives in startup. |
1024 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); | 1041 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); |
1025 | 1042 |
1026 RegisterNativeHandlers(module_system.get(), context); | 1043 RegisterNativeHandlers(module_system.get(), context); |
1027 | 1044 |
1028 module_system->RegisterNativeHandler("chrome", | 1045 module_system->RegisterNativeHandler("chrome", |
1029 scoped_ptr<NativeHandler>(new ChromeNativeHandler(v8_context))); | 1046 scoped_ptr<NativeHandler>(new ChromeNativeHandler(v8_context))); |
1030 module_system->RegisterNativeHandler("chrome_hidden", | 1047 module_system->RegisterNativeHandler("chrome_hidden", |
1031 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler(v8_context))); | 1048 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler(v8_context))); |
1032 module_system->RegisterNativeHandler("print", | 1049 module_system->RegisterNativeHandler("print", |
(...skipping 29 matching lines...) Expand all Loading... |
1062 switch (context_type) { | 1079 switch (context_type) { |
1063 case Feature::UNSPECIFIED_CONTEXT: | 1080 case Feature::UNSPECIFIED_CONTEXT: |
1064 case Feature::WEB_PAGE_CONTEXT: | 1081 case Feature::WEB_PAGE_CONTEXT: |
1065 // TODO(kalman): see comment below about ExtensionAPI. | 1082 // TODO(kalman): see comment below about ExtensionAPI. |
1066 InstallBindings(module_system.get(), v8_context, "app"); | 1083 InstallBindings(module_system.get(), v8_context, "app"); |
1067 InstallBindings(module_system.get(), v8_context, "webstore"); | 1084 InstallBindings(module_system.get(), v8_context, "webstore"); |
1068 break; | 1085 break; |
1069 case Feature::BLESSED_EXTENSION_CONTEXT: | 1086 case Feature::BLESSED_EXTENSION_CONTEXT: |
1070 case Feature::UNBLESSED_EXTENSION_CONTEXT: | 1087 case Feature::UNBLESSED_EXTENSION_CONTEXT: |
1071 case Feature::CONTENT_SCRIPT_CONTEXT: | 1088 case Feature::CONTENT_SCRIPT_CONTEXT: |
1072 if (extension && !extension->is_platform_app()) | |
1073 module_system->Require("miscellaneous_bindings"); | |
1074 module_system->Require("json"); // see paranoid comment in json.js | 1089 module_system->Require("json"); // see paranoid comment in json.js |
1075 | 1090 |
1076 // TODO(kalman): move this code back out of the switch and execute it | 1091 // TODO(kalman): move this code back out of the switch and execute it |
1077 // regardless of |context_type|. ExtensionAPI knows how to return the | 1092 // regardless of |context_type|. ExtensionAPI knows how to return the |
1078 // correct APIs, however, until it doesn't have a 2MB overhead we can't | 1093 // correct APIs, however, until it doesn't have a 2MB overhead we can't |
1079 // load it in every process. | 1094 // load it in every process. |
1080 RegisterSchemaGeneratedBindings(module_system.get(), context); | 1095 RegisterSchemaGeneratedBindings(module_system.get(), context); |
1081 break; | 1096 break; |
1082 } | 1097 } |
1083 | 1098 |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1350 | 1365 |
1351 void Dispatcher::OnSuspend(const std::string& extension_id) { | 1366 void Dispatcher::OnSuspend(const std::string& extension_id) { |
1352 // Dispatch the suspend event. This doesn't go through the standard event | 1367 // Dispatch the suspend event. This doesn't go through the standard event |
1353 // dispatch machinery because it requires special handling. We need to let | 1368 // dispatch machinery because it requires special handling. We need to let |
1354 // the browser know when we are starting and stopping the event dispatch, so | 1369 // the browser know when we are starting and stopping the event dispatch, so |
1355 // that it still considers the extension idle despite any activity the suspend | 1370 // that it still considers the extension idle despite any activity the suspend |
1356 // event creates. | 1371 // event creates. |
1357 base::ListValue args; | 1372 base::ListValue args; |
1358 args.Set(0, new base::StringValue(kOnSuspendEvent)); | 1373 args.Set(0, new base::StringValue(kOnSuspendEvent)); |
1359 args.Set(1, new base::ListValue()); | 1374 args.Set(1, new base::ListValue()); |
1360 v8_context_set_.DispatchChromeHiddenMethod( | 1375 v8_context_set_.ForEach( |
1361 extension_id, kEventDispatchFunction, args, NULL); | 1376 extension_id, |
1362 | 1377 NULL, |
| 1378 base::Bind(&CallModuleMethod, |
| 1379 kEventModule, |
| 1380 kEventDispatchFunction, |
| 1381 &args)); |
1363 RenderThread::Get()->Send(new ExtensionHostMsg_SuspendAck(extension_id)); | 1382 RenderThread::Get()->Send(new ExtensionHostMsg_SuspendAck(extension_id)); |
1364 } | 1383 } |
1365 | 1384 |
1366 void Dispatcher::OnCancelSuspend(const std::string& extension_id) { | 1385 void Dispatcher::OnCancelSuspend(const std::string& extension_id) { |
1367 base::ListValue args; | 1386 base::ListValue args; |
1368 args.Set(0, new base::StringValue(kOnSuspendCanceledEvent)); | 1387 args.Set(0, new base::StringValue(kOnSuspendCanceledEvent)); |
1369 args.Set(1, new base::ListValue()); | 1388 args.Set(1, new base::ListValue()); |
1370 v8_context_set_.DispatchChromeHiddenMethod( | 1389 v8_context_set_.ForEach( |
1371 extension_id, kEventDispatchFunction, args, NULL); | 1390 extension_id, |
| 1391 NULL, |
| 1392 base::Bind(&CallModuleMethod, |
| 1393 kEventModule, |
| 1394 kEventDispatchFunction, |
| 1395 &args)); |
1372 } | 1396 } |
1373 | 1397 |
1374 Feature::Context Dispatcher::ClassifyJavaScriptContext( | 1398 Feature::Context Dispatcher::ClassifyJavaScriptContext( |
1375 const std::string& extension_id, | 1399 const std::string& extension_id, |
1376 int extension_group, | 1400 int extension_group, |
1377 const ExtensionURLInfo& url_info) { | 1401 const ExtensionURLInfo& url_info) { |
1378 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) { | 1402 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) { |
1379 return extensions_.Contains(extension_id) ? | 1403 return extensions_.Contains(extension_id) ? |
1380 Feature::CONTENT_SCRIPT_CONTEXT : Feature::UNSPECIFIED_CONTEXT; | 1404 Feature::CONTENT_SCRIPT_CONTEXT : Feature::UNSPECIFIED_CONTEXT; |
1381 } | 1405 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1457 "%s cannot be used within a sandboxed frame."; | 1481 "%s cannot be used within a sandboxed frame."; |
1458 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 1482 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
1459 v8::ThrowException( | 1483 v8::ThrowException( |
1460 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 1484 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
1461 return false; | 1485 return false; |
1462 } | 1486 } |
1463 | 1487 |
1464 return true; | 1488 return true; |
1465 } | 1489 } |
1466 | 1490 |
| 1491 // static |
| 1492 void Dispatcher::CallModuleMethod(const std::string& module_name, |
| 1493 const std::string& method_name, |
| 1494 const base::ListValue* args, |
| 1495 ChromeV8Context* context) { |
| 1496 v8::HandleScope handle_scope; |
| 1497 v8::Context::Scope context_scope(context->v8_context()); |
| 1498 |
| 1499 scoped_ptr<content::V8ValueConverter> converter( |
| 1500 content::V8ValueConverter::create()); |
| 1501 |
| 1502 std::vector<v8::Handle<v8::Value> > arguments; |
| 1503 for (base::ListValue::const_iterator it = args->begin(); it != args->end(); |
| 1504 ++it) { |
| 1505 arguments.push_back(converter->ToV8Value(*it, context->v8_context())); |
| 1506 } |
| 1507 |
| 1508 context->module_system()->CallModuleMethod( |
| 1509 module_name, method_name, &arguments); |
| 1510 } |
| 1511 |
1467 } // namespace extensions | 1512 } // namespace extensions |
OLD | NEW |