| 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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 int world_id) { | 647 int world_id) { |
| 648 g_hack_extension_group = extension_group; | 648 g_hack_extension_group = extension_group; |
| 649 return true; | 649 return true; |
| 650 } | 650 } |
| 651 | 651 |
| 652 v8::Handle<v8::Object> Dispatcher::GetOrCreateObject( | 652 v8::Handle<v8::Object> Dispatcher::GetOrCreateObject( |
| 653 v8::Handle<v8::Object> object, | 653 v8::Handle<v8::Object> object, |
| 654 const std::string& field) { | 654 const std::string& field) { |
| 655 v8::HandleScope handle_scope; | 655 v8::HandleScope handle_scope; |
| 656 v8::Handle<v8::String> key = v8::String::New(field.c_str()); | 656 v8::Handle<v8::String> key = v8::String::New(field.c_str()); |
| 657 // If the object has a callback property, it is assumed it is an unavailable | 657 // This little dance is for APIs that may be unavailable but have available |
| 658 // API, so it is safe to delete. This is checked before GetOrCreateObject is | 658 // children. For example, chrome.app can be unavailable, while |
| 659 // called. | 659 // chrome.app.runtime is available. The lazy getter for chrome.app must be |
| 660 if (object->HasRealNamedCallbackProperty(key)) { | 660 // deleted, so that there isn't an error when accessing chrome.app.runtime. |
| 661 object->Delete(key); | 661 if (object->Has(key)) { |
| 662 } else if (object->HasRealNamedProperty(key)) { | |
| 663 v8::Handle<v8::Value> value = object->Get(key); | 662 v8::Handle<v8::Value> value = object->Get(key); |
| 664 CHECK(value->IsObject()); | 663 if (value->IsObject()) |
| 665 return handle_scope.Close(v8::Handle<v8::Object>::Cast(value)); | 664 return handle_scope.Close(v8::Handle<v8::Object>::Cast(value)); |
| 665 else |
| 666 object->Delete(key); |
| 666 } | 667 } |
| 667 | 668 |
| 668 v8::Handle<v8::Object> new_object = v8::Object::New(); | 669 v8::Handle<v8::Object> new_object = v8::Object::New(); |
| 669 object->Set(key, new_object); | 670 object->Set(key, new_object); |
| 670 return handle_scope.Close(new_object); | 671 return handle_scope.Close(new_object); |
| 671 } | 672 } |
| 672 | 673 |
| 673 void Dispatcher::RegisterSchemaGeneratedBindings( | 674 void Dispatcher::RegisterSchemaGeneratedBindings( |
| 674 ModuleSystem* module_system, | 675 ModuleSystem* module_system, |
| 675 ChromeV8Context* context) { | 676 ChromeV8Context* context, |
| 677 v8::Handle<v8::Context> v8_context) { |
| 676 std::set<std::string> apis = | 678 std::set<std::string> apis = |
| 677 ExtensionAPI::GetSharedInstance()->GetAllAPINames(); | 679 ExtensionAPI::GetSharedInstance()->GetAllAPINames(); |
| 678 for (std::set<std::string>::iterator it = apis.begin(); | 680 for (std::set<std::string>::iterator it = apis.begin(); |
| 679 it != apis.end(); ++it) { | 681 it != apis.end(); ++it) { |
| 680 const std::string& api_name = *it; | 682 const std::string& api_name = *it; |
| 681 if (!context->GetAvailabilityForContext(api_name).is_available()) | |
| 682 continue; | |
| 683 | 683 |
| 684 Feature* feature = | 684 Feature* feature = |
| 685 BaseFeatureProvider::GetByName("api")->GetFeature(api_name); | 685 BaseFeatureProvider::GetByName("api")->GetFeature(api_name); |
| 686 if (feature && feature->IsInternal()) | 686 if (feature && feature->IsInternal()) |
| 687 continue; | 687 continue; |
| 688 | 688 |
| 689 std::vector<std::string> split; | 689 std::vector<std::string> split; |
| 690 base::SplitString(api_name, '.', &split); | 690 base::SplitString(api_name, '.', &split); |
| 691 | 691 |
| 692 v8::Handle<v8::Object> bind_object = | 692 v8::Handle<v8::Object> bind_object = GetOrCreateChrome(v8_context); |
| 693 GetOrCreateChrome(context->v8_context()); | 693 for (size_t i = 0; i < split.size() - 1; ++i) |
| 694 | |
| 695 // Check if this API has an ancestor. If the API's ancestor is available and | |
| 696 // the API is not available, don't install the bindings for this API. If | |
| 697 // the API is available and its ancestor is not, delete the ancestor and | |
| 698 // install the bindings for the API. This is to prevent loading the ancestor | |
| 699 // API schema if it will not be needed. | |
| 700 // | |
| 701 // For example: | |
| 702 // If app is available and app.window is not, just install app. | |
| 703 // If app.window is available and app is not, delete app and install | |
| 704 // app.window on a new object so app does not have to be loaded. | |
| 705 std::string ancestor_name; | |
| 706 bool only_ancestor_available = false; | |
| 707 for (size_t i = 0; i < split.size() - 1; ++i) { | |
| 708 ancestor_name += (i ? ".": "") + split[i]; | |
| 709 if (!ancestor_name.empty() && | |
| 710 context->GetAvailability(ancestor_name).is_available() && | |
| 711 !context->GetAvailability(api_name).is_available()) { | |
| 712 only_ancestor_available = true; | |
| 713 break; | |
| 714 } | |
| 715 bind_object = GetOrCreateObject(bind_object, split[i]); | 694 bind_object = GetOrCreateObject(bind_object, split[i]); |
| 716 } | |
| 717 if (only_ancestor_available) | |
| 718 continue; | |
| 719 | 695 |
| 720 if (lazy_bindings_map_.find(api_name) != lazy_bindings_map_.end()) { | 696 if (lazy_bindings_map_.find(api_name) != lazy_bindings_map_.end()) { |
| 721 InstallBindings(module_system, context->v8_context(), api_name); | 697 InstallBindings(module_system, v8_context, api_name); |
| 722 } else if (!source_map_.Contains(api_name)) { | 698 } else if (!source_map_.Contains(api_name)) { |
| 723 module_system->RegisterNativeHandler( | 699 module_system->RegisterNativeHandler( |
| 724 api_name, | 700 api_name, |
| 725 scoped_ptr<NativeHandler>(new BindingGeneratingNativeHandler( | 701 scoped_ptr<NativeHandler>(new BindingGeneratingNativeHandler( |
| 726 module_system, | 702 module_system, |
| 727 api_name, | 703 api_name, |
| 728 "binding"))); | 704 "binding"))); |
| 729 module_system->SetNativeLazyField(bind_object, | 705 module_system->SetNativeLazyField(bind_object, |
| 730 split.back(), | 706 split.back(), |
| 731 api_name, | 707 api_name, |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 BackgroundInfo::HasLazyBackgroundPage(extension)); | 976 BackgroundInfo::HasLazyBackgroundPage(extension)); |
| 1001 module_system->RegisterNativeHandler("process", | 977 module_system->RegisterNativeHandler("process", |
| 1002 scoped_ptr<NativeHandler>(new ProcessInfoNativeHandler( | 978 scoped_ptr<NativeHandler>(new ProcessInfoNativeHandler( |
| 1003 this, v8_context, context->GetExtensionID(), | 979 this, v8_context, context->GetExtensionID(), |
| 1004 context->GetContextTypeDescription(), | 980 context->GetContextTypeDescription(), |
| 1005 ChromeRenderProcessObserver::is_incognito_process(), | 981 ChromeRenderProcessObserver::is_incognito_process(), |
| 1006 manifest_version, send_request_disabled))); | 982 manifest_version, send_request_disabled))); |
| 1007 | 983 |
| 1008 GetOrCreateChrome(v8_context); | 984 GetOrCreateChrome(v8_context); |
| 1009 | 985 |
| 1010 // TODO(kalman): see comment below about ExtensionAPI. | 986 // Loading JavaScript is expensive, so only run the full API bindings |
| 1011 InstallBindings(module_system.get(), v8_context, "app"); | 987 // generation mechanisms in extension pages (NOT all web pages). |
| 1012 InstallBindings(module_system.get(), v8_context, "webstore"); | 988 switch (context_type) { |
| 1013 if (extension && !extension->is_platform_app()) | 989 case Feature::UNSPECIFIED_CONTEXT: |
| 1014 module_system->Require("miscellaneous_bindings"); | 990 case Feature::WEB_PAGE_CONTEXT: |
| 1015 module_system->Require("json"); // see paranoid comment in json.js | 991 // TODO(kalman): see comment below about ExtensionAPI. |
| 992 InstallBindings(module_system.get(), v8_context, "app"); |
| 993 InstallBindings(module_system.get(), v8_context, "webstore"); |
| 994 break; |
| 995 case Feature::BLESSED_EXTENSION_CONTEXT: |
| 996 case Feature::UNBLESSED_EXTENSION_CONTEXT: |
| 997 case Feature::CONTENT_SCRIPT_CONTEXT: |
| 998 if (extension && !extension->is_platform_app()) |
| 999 module_system->Require("miscellaneous_bindings"); |
| 1000 module_system->Require("json"); // see paranoid comment in json.js |
| 1016 | 1001 |
| 1017 RegisterSchemaGeneratedBindings(module_system.get(), context); | 1002 // TODO(kalman): move this code back out of the switch and execute it |
| 1003 // regardless of |context_type|. ExtensionAPI knows how to return the |
| 1004 // correct APIs, however, until it doesn't have a 2MB overhead we can't |
| 1005 // load it in every process. |
| 1006 RegisterSchemaGeneratedBindings(module_system.get(), |
| 1007 context, |
| 1008 v8_context); |
| 1009 break; |
| 1010 } |
| 1018 | 1011 |
| 1019 bool is_within_platform_app = IsWithinPlatformApp(frame); | 1012 bool is_within_platform_app = IsWithinPlatformApp(frame); |
| 1020 // Inject custom JS into the platform app context. | 1013 // Inject custom JS into the platform app context. |
| 1021 if (is_within_platform_app) | 1014 if (is_within_platform_app) |
| 1022 module_system->Require("platformApp"); | 1015 module_system->Require("platformApp"); |
| 1023 | 1016 |
| 1024 // Only platform apps support the <webview> tag, because the "webView" and | 1017 // Only platform apps support the <webview> tag, because the "webView" and |
| 1025 // "denyWebView" modules will affect the performance of DOM modifications | 1018 // "denyWebView" modules will affect the performance of DOM modifications |
| 1026 // (http://crbug.com/196453). | 1019 // (http://crbug.com/196453). |
| 1027 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT && | 1020 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT && |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1381 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 1374 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 1382 v8::ThrowException( | 1375 v8::ThrowException( |
| 1383 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 1376 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 1384 return false; | 1377 return false; |
| 1385 } | 1378 } |
| 1386 | 1379 |
| 1387 return true; | 1380 return true; |
| 1388 } | 1381 } |
| 1389 | 1382 |
| 1390 } // namespace extensions | 1383 } // namespace extensions |
| OLD | NEW |