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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 is_extension_process_ = | 147 is_extension_process_ = |
147 command_line.HasSwitch(switches::kExtensionProcess) || | 148 command_line.HasSwitch(switches::kExtensionProcess) || |
148 command_line.HasSwitch(switches::kSingleProcess); | 149 command_line.HasSwitch(switches::kSingleProcess); |
149 | 150 |
150 if (is_extension_process_) { | 151 if (is_extension_process_) { |
151 RenderThread::Get()->SetIdleNotificationDelayInMs( | 152 RenderThread::Get()->SetIdleNotificationDelayInMs( |
152 kInitialExtensionIdleHandlerDelayMs); | 153 kInitialExtensionIdleHandlerDelayMs); |
153 } | 154 } |
154 | 155 |
155 user_script_slave_.reset(new UserScriptSlave(&extensions_)); | 156 user_script_slave_.reset(new UserScriptSlave(&extensions_)); |
| 157 request_sender_.reset(new ExtensionRequestSender(this, &v8_context_set_)); |
156 PopulateSourceMap(); | 158 PopulateSourceMap(); |
157 PopulateLazyBindingsMap(); | 159 PopulateLazyBindingsMap(); |
158 } | 160 } |
159 | 161 |
160 ExtensionDispatcher::~ExtensionDispatcher() { | 162 ExtensionDispatcher::~ExtensionDispatcher() { |
161 } | 163 } |
162 | 164 |
163 bool ExtensionDispatcher::OnControlMessageReceived( | 165 bool ExtensionDispatcher::OnControlMessageReceived( |
164 const IPC::Message& message) { | 166 const IPC::Message& message) { |
165 bool handled = true; | 167 bool handled = true; |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 return true; | 356 return true; |
355 } | 357 } |
356 | 358 |
357 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system, | 359 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system, |
358 ChromeV8Context* context) { | 360 ChromeV8Context* context) { |
359 module_system->RegisterNativeHandler("event_bindings", | 361 module_system->RegisterNativeHandler("event_bindings", |
360 scoped_ptr<NativeHandler>(EventBindings::Get(this))); | 362 scoped_ptr<NativeHandler>(EventBindings::Get(this))); |
361 module_system->RegisterNativeHandler("miscellaneous_bindings", | 363 module_system->RegisterNativeHandler("miscellaneous_bindings", |
362 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this))); | 364 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this))); |
363 module_system->RegisterNativeHandler("schema_generated_bindings", | 365 module_system->RegisterNativeHandler("schema_generated_bindings", |
364 scoped_ptr<NativeHandler>(new SchemaGeneratedBindings(this))); | 366 scoped_ptr<NativeHandler>( |
| 367 new SchemaGeneratedBindings(this, request_sender_.get()))); |
365 | 368 |
366 // Custom bindings. | 369 // Custom bindings. |
367 module_system->RegisterNativeHandler("app", | 370 module_system->RegisterNativeHandler("app", |
368 scoped_ptr<NativeHandler>(new AppBindings(this, context))); | 371 scoped_ptr<NativeHandler>(new AppBindings(this, context))); |
369 module_system->RegisterNativeHandler("context_menus", | 372 module_system->RegisterNativeHandler("context_menus", |
370 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings())); | 373 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings())); |
371 module_system->RegisterNativeHandler("extension", | 374 module_system->RegisterNativeHandler("extension", |
372 scoped_ptr<NativeHandler>( | 375 scoped_ptr<NativeHandler>( |
373 new ExtensionCustomBindings(this))); | 376 new ExtensionCustomBindings(this))); |
374 module_system->RegisterNativeHandler("experimental_socket", | 377 module_system->RegisterNativeHandler("experimental_socket", |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 return Feature::BLESSED_EXTENSION_CONTEXT; | 752 return Feature::BLESSED_EXTENSION_CONTEXT; |
750 | 753 |
751 if (extensions_.ExtensionBindingsAllowed(url_info)) | 754 if (extensions_.ExtensionBindingsAllowed(url_info)) |
752 return Feature::UNBLESSED_EXTENSION_CONTEXT; | 755 return Feature::UNBLESSED_EXTENSION_CONTEXT; |
753 | 756 |
754 if (url_info.url().is_valid()) | 757 if (url_info.url().is_valid()) |
755 return Feature::WEB_PAGE_CONTEXT; | 758 return Feature::WEB_PAGE_CONTEXT; |
756 | 759 |
757 return Feature::UNSPECIFIED_CONTEXT; | 760 return Feature::UNSPECIFIED_CONTEXT; |
758 } | 761 } |
| 762 |
| 763 void ExtensionDispatcher::OnExtensionResponse(int request_id, |
| 764 bool success, |
| 765 const std::string& response, |
| 766 const std::string& error) { |
| 767 request_sender_->HandleResponse(request_id, success, response, error); |
| 768 } |
| 769 |
| 770 bool ExtensionDispatcher::CheckCurrentContextAccessToExtensionAPI( |
| 771 const std::string& function_name) const { |
| 772 ChromeV8Context* context = v8_context_set().GetCurrent(); |
| 773 if (!context) { |
| 774 DLOG(ERROR) << "Not in a v8::Context"; |
| 775 return false; |
| 776 } |
| 777 |
| 778 const ::Extension* extension = NULL; |
| 779 if (!context->extension_id().empty()) { |
| 780 extension = extensions()->GetByID(context->extension_id()); |
| 781 } |
| 782 |
| 783 if (!extension || !extension->HasAPIPermission(function_name)) { |
| 784 static const char kMessage[] = |
| 785 "You do not have permission to use '%s'. Be sure to declare" |
| 786 " in your manifest what permissions you need."; |
| 787 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 788 v8::ThrowException( |
| 789 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 790 return false; |
| 791 } |
| 792 |
| 793 if (!IsExtensionActive(extension->id()) && |
| 794 ExtensionAPI::GetInstance()->IsPrivileged(function_name)) { |
| 795 static const char kMessage[] = |
| 796 "%s can only be used in an extension process."; |
| 797 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 798 v8::ThrowException( |
| 799 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 800 return false; |
| 801 } |
| 802 |
| 803 return true; |
| 804 } |
OLD | NEW |