| 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/extension_dispatcher.h" | 5 #include "chrome/renderer/extensions/extension_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/string_piece.h" | 10 #include "base/string_piece.h" |
| 11 #include "chrome/common/child_process_logging.h" | 11 #include "chrome/common/child_process_logging.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/extensions/api/extension_api.h" | 13 #include "chrome/common/extensions/api/extension_api.h" |
| 14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_messages.h" | 15 #include "chrome/common/extensions/extension_messages.h" |
| 16 #include "chrome/common/extensions/extension_permission_set.h" | 16 #include "chrome/common/extensions/extension_permission_set.h" |
| 17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/renderer/chrome_render_process_observer.h" | 18 #include "chrome/renderer/chrome_render_process_observer.h" |
| 19 #include "chrome/renderer/extensions/app_bindings.h" | 19 #include "chrome/renderer/extensions/app_bindings.h" |
| 20 #include "chrome/renderer/extensions/chrome_v8_context.h" | 20 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 21 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 21 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 22 #include "chrome/renderer/extensions/context_menus_custom_bindings.h" | 22 #include "chrome/renderer/extensions/context_menus_custom_bindings.h" |
| 23 #include "chrome/renderer/extensions/event_bindings.h" | 23 #include "chrome/renderer/extensions/event_bindings.h" |
| 24 #include "chrome/renderer/extensions/experimental.socket_custom_bindings.h" | 24 #include "chrome/renderer/extensions/experimental.socket_custom_bindings.h" |
| 25 #include "chrome/renderer/extensions/extension_custom_bindings.h" | 25 #include "chrome/renderer/extensions/extension_custom_bindings.h" |
| 26 #include "chrome/renderer/extensions/extension_groups.h" | 26 #include "chrome/renderer/extensions/extension_groups.h" |
| 27 #include "chrome/renderer/extensions/extension_request_sender.h" |
| 27 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h" | 28 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h" |
| 28 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h" | 29 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h" |
| 29 #include "chrome/renderer/extensions/i18n_custom_bindings.h" | 30 #include "chrome/renderer/extensions/i18n_custom_bindings.h" |
| 30 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 31 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
| 31 #include "chrome/renderer/extensions/page_actions_custom_bindings.h" | 32 #include "chrome/renderer/extensions/page_actions_custom_bindings.h" |
| 32 #include "chrome/renderer/extensions/page_capture_custom_bindings.h" | 33 #include "chrome/renderer/extensions/page_capture_custom_bindings.h" |
| 33 #include "chrome/renderer/extensions/schema_generated_bindings.h" | 34 #include "chrome/renderer/extensions/schema_generated_bindings.h" |
| 34 #include "chrome/renderer/extensions/tabs_custom_bindings.h" | 35 #include "chrome/renderer/extensions/tabs_custom_bindings.h" |
| 35 #include "chrome/renderer/extensions/tts_custom_bindings.h" | 36 #include "chrome/renderer/extensions/tts_custom_bindings.h" |
| 36 #include "chrome/renderer/extensions/user_script_slave.h" | 37 #include "chrome/renderer/extensions/user_script_slave.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 is_extension_process_ = | 150 is_extension_process_ = |
| 150 command_line.HasSwitch(switches::kExtensionProcess) || | 151 command_line.HasSwitch(switches::kExtensionProcess) || |
| 151 command_line.HasSwitch(switches::kSingleProcess); | 152 command_line.HasSwitch(switches::kSingleProcess); |
| 152 | 153 |
| 153 if (is_extension_process_) { | 154 if (is_extension_process_) { |
| 154 RenderThread::Get()->SetIdleNotificationDelayInMs( | 155 RenderThread::Get()->SetIdleNotificationDelayInMs( |
| 155 kInitialExtensionIdleHandlerDelayMs); | 156 kInitialExtensionIdleHandlerDelayMs); |
| 156 } | 157 } |
| 157 | 158 |
| 158 user_script_slave_.reset(new UserScriptSlave(&extensions_)); | 159 user_script_slave_.reset(new UserScriptSlave(&extensions_)); |
| 160 request_sender_.reset(new ExtensionRequestSender(this, &v8_context_set_)); |
| 159 PopulateSourceMap(); | 161 PopulateSourceMap(); |
| 160 PopulateLazyBindingsMap(); | 162 PopulateLazyBindingsMap(); |
| 161 } | 163 } |
| 162 | 164 |
| 163 ExtensionDispatcher::~ExtensionDispatcher() { | 165 ExtensionDispatcher::~ExtensionDispatcher() { |
| 164 } | 166 } |
| 165 | 167 |
| 166 bool ExtensionDispatcher::OnControlMessageReceived( | 168 bool ExtensionDispatcher::OnControlMessageReceived( |
| 167 const IPC::Message& message) { | 169 const IPC::Message& message) { |
| 168 bool handled = true; | 170 bool handled = true; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 return true; | 384 return true; |
| 383 } | 385 } |
| 384 | 386 |
| 385 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system, | 387 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system, |
| 386 ChromeV8Context* context) { | 388 ChromeV8Context* context) { |
| 387 module_system->RegisterNativeHandler("event_bindings", | 389 module_system->RegisterNativeHandler("event_bindings", |
| 388 scoped_ptr<NativeHandler>(EventBindings::Get(this))); | 390 scoped_ptr<NativeHandler>(EventBindings::Get(this))); |
| 389 module_system->RegisterNativeHandler("miscellaneous_bindings", | 391 module_system->RegisterNativeHandler("miscellaneous_bindings", |
| 390 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this))); | 392 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this))); |
| 391 module_system->RegisterNativeHandler("schema_generated_bindings", | 393 module_system->RegisterNativeHandler("schema_generated_bindings", |
| 392 scoped_ptr<NativeHandler>(new SchemaGeneratedBindings(this))); | 394 scoped_ptr<NativeHandler>( |
| 395 new SchemaGeneratedBindings(this, request_sender_.get()))); |
| 393 | 396 |
| 394 // Custom bindings. | 397 // Custom bindings. |
| 395 module_system->RegisterNativeHandler("app", | 398 module_system->RegisterNativeHandler("app", |
| 396 scoped_ptr<NativeHandler>(new AppBindings(this, context))); | 399 scoped_ptr<NativeHandler>(new AppBindings(this, context))); |
| 397 module_system->RegisterNativeHandler("context_menus", | 400 module_system->RegisterNativeHandler("context_menus", |
| 398 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings())); | 401 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings())); |
| 399 module_system->RegisterNativeHandler("extension", | 402 module_system->RegisterNativeHandler("extension", |
| 400 scoped_ptr<NativeHandler>( | 403 scoped_ptr<NativeHandler>( |
| 401 new ExtensionCustomBindings(this))); | 404 new ExtensionCustomBindings(this))); |
| 402 module_system->RegisterNativeHandler("experimental_socket", | 405 module_system->RegisterNativeHandler("experimental_socket", |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 return Feature::BLESSED_EXTENSION_CONTEXT; | 793 return Feature::BLESSED_EXTENSION_CONTEXT; |
| 791 | 794 |
| 792 if (extensions_.ExtensionBindingsAllowed(url_info)) | 795 if (extensions_.ExtensionBindingsAllowed(url_info)) |
| 793 return Feature::UNBLESSED_EXTENSION_CONTEXT; | 796 return Feature::UNBLESSED_EXTENSION_CONTEXT; |
| 794 | 797 |
| 795 if (url_info.url().is_valid()) | 798 if (url_info.url().is_valid()) |
| 796 return Feature::WEB_PAGE_CONTEXT; | 799 return Feature::WEB_PAGE_CONTEXT; |
| 797 | 800 |
| 798 return Feature::UNSPECIFIED_CONTEXT; | 801 return Feature::UNSPECIFIED_CONTEXT; |
| 799 } | 802 } |
| 803 |
| 804 void ExtensionDispatcher::OnExtensionResponse(int request_id, |
| 805 bool success, |
| 806 const std::string& response, |
| 807 const std::string& error) { |
| 808 request_sender_->HandleResponse(request_id, success, response, error); |
| 809 } |
| 810 |
| 811 bool ExtensionDispatcher::CheckCurrentContextAccessToExtensionAPI( |
| 812 const std::string& function_name) const { |
| 813 ChromeV8Context* context = v8_context_set().GetCurrent(); |
| 814 if (!context) { |
| 815 DLOG(ERROR) << "Not in a v8::Context"; |
| 816 return false; |
| 817 } |
| 818 |
| 819 const ::Extension* extension = NULL; |
| 820 if (!context->extension_id().empty()) { |
| 821 extension = extensions()->GetByID(context->extension_id()); |
| 822 } |
| 823 |
| 824 if (!extension || !extension->HasAPIPermission(function_name)) { |
| 825 static const char kMessage[] = |
| 826 "You do not have permission to use '%s'. Be sure to declare" |
| 827 " in your manifest what permissions you need."; |
| 828 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 829 v8::ThrowException( |
| 830 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 831 return false; |
| 832 } |
| 833 |
| 834 if (!IsExtensionActive(extension->id()) && |
| 835 ExtensionAPI::GetInstance()->IsPrivileged(function_name)) { |
| 836 static const char kMessage[] = |
| 837 "%s can only be used in an extension process."; |
| 838 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 839 v8::ThrowException( |
| 840 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 841 return false; |
| 842 } |
| 843 |
| 844 return true; |
| 845 } |
| OLD | NEW |