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/chrome_version_info.h" |
13 #include "chrome/common/chrome_view_type.h" | 14 #include "chrome/common/chrome_view_type.h" |
14 #include "chrome/common/extensions/api/extension_api.h" | 15 #include "chrome/common/extensions/api/extension_api.h" |
15 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
16 #include "chrome/common/extensions/extension_messages.h" | 17 #include "chrome/common/extensions/extension_messages.h" |
17 #include "chrome/common/extensions/extension_permission_set.h" | 18 #include "chrome/common/extensions/extension_permission_set.h" |
18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
19 #include "chrome/renderer/chrome_render_process_observer.h" | 20 #include "chrome/renderer/chrome_render_process_observer.h" |
20 #include "chrome/renderer/extensions/api_definitions_natives.h" | 21 #include "chrome/renderer/extensions/api_definitions_natives.h" |
21 #include "chrome/renderer/extensions/app_bindings.h" | 22 #include "chrome/renderer/extensions/app_bindings.h" |
22 #include "chrome/renderer/extensions/chrome_v8_context.h" | 23 #include "chrome/renderer/extensions/chrome_v8_context.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 using extensions::SetIconNatives; | 86 using extensions::SetIconNatives; |
86 using extensions::TTSCustomBindings; | 87 using extensions::TTSCustomBindings; |
87 using extensions::TabsCustomBindings; | 88 using extensions::TabsCustomBindings; |
88 using extensions::WebRequestCustomBindings; | 89 using extensions::WebRequestCustomBindings; |
89 | 90 |
90 namespace { | 91 namespace { |
91 | 92 |
92 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; | 93 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; |
93 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; | 94 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; |
94 static const char kEventDispatchFunction[] = "Event.dispatchJSON"; | 95 static const char kEventDispatchFunction[] = "Event.dispatchJSON"; |
95 static const char kOnUnloadEvent[] = | 96 static const char kOnUnloadEvent[] = "runtime.onBackgroundPageUnloadingSoon"; |
96 "experimental.runtime.onBackgroundPageUnloadingSoon"; | |
97 | 97 |
98 class ChromeHiddenNativeHandler : public NativeHandler { | 98 class ChromeHiddenNativeHandler : public NativeHandler { |
99 public: | 99 public: |
100 ChromeHiddenNativeHandler() { | 100 ChromeHiddenNativeHandler() { |
101 RouteFunction("GetChromeHidden", | 101 RouteFunction("GetChromeHidden", |
102 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden, | 102 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden, |
103 base::Unretained(this))); | 103 base::Unretained(this))); |
104 } | 104 } |
105 | 105 |
106 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) { | 106 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 const Extension* extension) { | 172 const Extension* extension) { |
173 if (!render_view) | 173 if (!render_view) |
174 return false; | 174 return false; |
175 | 175 |
176 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 176 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
177 return (extension && extension->has_lazy_background_page() && | 177 return (extension && extension->has_lazy_background_page() && |
178 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 178 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
179 } | 179 } |
180 }; | 180 }; |
181 | 181 |
| 182 class ChannelNativeHandler : public NativeHandler { |
| 183 public: |
| 184 explicit ChannelNativeHandler(chrome::VersionInfo::Channel channel) |
| 185 : channel_(channel) { |
| 186 RouteFunction("IsDevChannel", |
| 187 base::Bind(&ChannelNativeHandler::IsDevChannel, |
| 188 base::Unretained(this))); |
| 189 } |
| 190 |
| 191 v8::Handle<v8::Value> IsDevChannel(const v8::Arguments& args) { |
| 192 return v8::Boolean::New(channel_ <= chrome::VersionInfo::CHANNEL_DEV); |
| 193 } |
| 194 |
| 195 chrome::VersionInfo::Channel channel_; |
| 196 }; |
| 197 |
182 void InstallAppBindings(ModuleSystem* module_system, | 198 void InstallAppBindings(ModuleSystem* module_system, |
183 v8::Handle<v8::Object> chrome, | 199 v8::Handle<v8::Object> chrome, |
184 v8::Handle<v8::Object> chrome_hidden) { | 200 v8::Handle<v8::Object> chrome_hidden) { |
185 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); | 201 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); |
186 module_system->SetLazyField(chrome, "appNotifications", "app", | 202 module_system->SetLazyField(chrome, "appNotifications", "app", |
187 "chromeAppNotifications"); | 203 "chromeAppNotifications"); |
188 module_system->SetLazyField(chrome_hidden, "app", "app", | 204 module_system->SetLazyField(chrome_hidden, "app", "app", |
189 "chromeHiddenApp"); | 205 "chromeHiddenApp"); |
190 } | 206 } |
191 | 207 |
192 void InstallWebstoreBindings(ModuleSystem* module_system, | 208 void InstallWebstoreBindings(ModuleSystem* module_system, |
193 v8::Handle<v8::Object> chrome, | 209 v8::Handle<v8::Object> chrome, |
194 v8::Handle<v8::Object> chrome_hidden) { | 210 v8::Handle<v8::Object> chrome_hidden) { |
195 module_system->SetLazyField(chrome, "webstore", "webstore", "chromeWebstore"); | 211 module_system->SetLazyField(chrome, "webstore", "webstore", "chromeWebstore"); |
196 module_system->SetLazyField(chrome_hidden, "webstore", "webstore", | 212 module_system->SetLazyField(chrome_hidden, "webstore", "webstore", |
197 "chromeHiddenWebstore"); | 213 "chromeHiddenWebstore"); |
198 } | 214 } |
199 | 215 |
200 } | 216 } |
201 | 217 |
202 ExtensionDispatcher::ExtensionDispatcher() | 218 ExtensionDispatcher::ExtensionDispatcher() |
203 : is_webkit_initialized_(false), | 219 : is_webkit_initialized_(false), |
204 webrequest_adblock_(false), | 220 webrequest_adblock_(false), |
205 webrequest_adblock_plus_(false), | 221 webrequest_adblock_plus_(false), |
206 webrequest_other_(false), | 222 webrequest_other_(false), |
207 source_map_(&ResourceBundle::GetSharedInstance()) { | 223 source_map_(&ResourceBundle::GetSharedInstance()), |
| 224 chrome_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) { |
208 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); | 225 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); |
209 is_extension_process_ = | 226 is_extension_process_ = |
210 command_line.HasSwitch(switches::kExtensionProcess) || | 227 command_line.HasSwitch(switches::kExtensionProcess) || |
211 command_line.HasSwitch(switches::kSingleProcess); | 228 command_line.HasSwitch(switches::kSingleProcess); |
212 | 229 |
213 if (is_extension_process_) { | 230 if (is_extension_process_) { |
214 RenderThread::Get()->SetIdleNotificationDelayInMs( | 231 RenderThread::Get()->SetIdleNotificationDelayInMs( |
215 kInitialExtensionIdleHandlerDelayMs); | 232 kInitialExtensionIdleHandlerDelayMs); |
216 } | 233 } |
217 | 234 |
218 user_script_slave_.reset(new UserScriptSlave(&extensions_)); | 235 user_script_slave_.reset(new UserScriptSlave(&extensions_)); |
219 request_sender_.reset(new ExtensionRequestSender(this, &v8_context_set_)); | 236 request_sender_.reset(new ExtensionRequestSender(this, &v8_context_set_)); |
220 PopulateSourceMap(); | 237 PopulateSourceMap(); |
221 PopulateLazyBindingsMap(); | 238 PopulateLazyBindingsMap(); |
222 } | 239 } |
223 | 240 |
224 ExtensionDispatcher::~ExtensionDispatcher() { | 241 ExtensionDispatcher::~ExtensionDispatcher() { |
225 } | 242 } |
226 | 243 |
227 bool ExtensionDispatcher::OnControlMessageReceived( | 244 bool ExtensionDispatcher::OnControlMessageReceived( |
228 const IPC::Message& message) { | 245 const IPC::Message& message) { |
229 bool handled = true; | 246 bool handled = true; |
230 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) | 247 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) |
| 248 IPC_MESSAGE_HANDLER(ExtensionMsg_SetChannel, OnSetChannel) |
231 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) | 249 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) |
232 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect, OnDispatchOnConnect) | 250 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect, OnDispatchOnConnect) |
233 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage) | 251 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage) |
234 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect, | 252 IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect, |
235 OnDispatchOnDisconnect) | 253 OnDispatchOnDisconnect) |
236 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames) | 254 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames) |
237 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded) | 255 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded) |
238 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) | 256 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) |
239 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, | 257 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, |
240 OnSetScriptingWhitelist) | 258 OnSetScriptingWhitelist) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 } | 303 } |
286 } | 304 } |
287 | 305 |
288 void ExtensionDispatcher::OnSetFunctionNames( | 306 void ExtensionDispatcher::OnSetFunctionNames( |
289 const std::vector<std::string>& names) { | 307 const std::vector<std::string>& names) { |
290 function_names_.clear(); | 308 function_names_.clear(); |
291 for (size_t i = 0; i < names.size(); ++i) | 309 for (size_t i = 0; i < names.size(); ++i) |
292 function_names_.insert(names[i]); | 310 function_names_.insert(names[i]); |
293 } | 311 } |
294 | 312 |
| 313 void ExtensionDispatcher::OnSetChannel(int channel) { |
| 314 chrome_channel_ = channel; |
| 315 } |
| 316 |
295 void ExtensionDispatcher::OnMessageInvoke(const std::string& extension_id, | 317 void ExtensionDispatcher::OnMessageInvoke(const std::string& extension_id, |
296 const std::string& function_name, | 318 const std::string& function_name, |
297 const ListValue& args, | 319 const ListValue& args, |
298 const GURL& event_url, | 320 const GURL& event_url, |
299 bool user_gesture) { | 321 bool user_gesture) { |
300 scoped_ptr<WebScopedUserGesture> web_user_gesture; | 322 scoped_ptr<WebScopedUserGesture> web_user_gesture; |
301 if (user_gesture) { | 323 if (user_gesture) { |
302 web_user_gesture.reset(new WebScopedUserGesture); | 324 web_user_gesture.reset(new WebScopedUserGesture); |
303 } | 325 } |
304 | 326 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS); | 541 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS); |
520 source_map_.RegisterSource("declarative", | 542 source_map_.RegisterSource("declarative", |
521 IDR_DECLARATIVE_CUSTOM_BINDINGS_JS); | 543 IDR_DECLARATIVE_CUSTOM_BINDINGS_JS); |
522 source_map_.RegisterSource("declarativeWebRequest", | 544 source_map_.RegisterSource("declarativeWebRequest", |
523 IDR_DECLARATIVE_WEBREQUEST_CUSTOM_BINDINGS_JS); | 545 IDR_DECLARATIVE_WEBREQUEST_CUSTOM_BINDINGS_JS); |
524 source_map_.RegisterSource("devtools", IDR_DEVTOOLS_CUSTOM_BINDINGS_JS); | 546 source_map_.RegisterSource("devtools", IDR_DEVTOOLS_CUSTOM_BINDINGS_JS); |
525 source_map_.RegisterSource("experimental.mediaGalleries", | 547 source_map_.RegisterSource("experimental.mediaGalleries", |
526 IDR_MEDIA_GALLERY_CUSTOM_BINDINGS_JS); | 548 IDR_MEDIA_GALLERY_CUSTOM_BINDINGS_JS); |
527 source_map_.RegisterSource("experimental.offscreen", | 549 source_map_.RegisterSource("experimental.offscreen", |
528 IDR_EXPERIMENTAL_OFFSCREENTABS_CUSTOM_BINDINGS_JS); | 550 IDR_EXPERIMENTAL_OFFSCREENTABS_CUSTOM_BINDINGS_JS); |
529 source_map_.RegisterSource("experimental.runtime", | |
530 IDR_EXPERIMENTAL_RUNTIME_CUSTOM_BINDINGS_JS); | |
531 source_map_.RegisterSource("experimental.usb", | 551 source_map_.RegisterSource("experimental.usb", |
532 IDR_EXPERIMENTAL_USB_CUSTOM_BINDINGS_JS); | 552 IDR_EXPERIMENTAL_USB_CUSTOM_BINDINGS_JS); |
533 source_map_.RegisterSource("extension", IDR_EXTENSION_CUSTOM_BINDINGS_JS); | 553 source_map_.RegisterSource("extension", IDR_EXTENSION_CUSTOM_BINDINGS_JS); |
534 source_map_.RegisterSource("fileBrowserHandler", | 554 source_map_.RegisterSource("fileBrowserHandler", |
535 IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS); | 555 IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS); |
536 source_map_.RegisterSource("fileBrowserPrivate", | 556 source_map_.RegisterSource("fileBrowserPrivate", |
537 IDR_FILE_BROWSER_PRIVATE_CUSTOM_BINDINGS_JS); | 557 IDR_FILE_BROWSER_PRIVATE_CUSTOM_BINDINGS_JS); |
538 source_map_.RegisterSource("i18n", IDR_I18N_CUSTOM_BINDINGS_JS); | 558 source_map_.RegisterSource("i18n", IDR_I18N_CUSTOM_BINDINGS_JS); |
539 source_map_.RegisterSource("experimental.input.ime", | 559 source_map_.RegisterSource("experimental.input.ime", |
540 IDR_INPUT_IME_CUSTOM_BINDINGS_JS); | 560 IDR_INPUT_IME_CUSTOM_BINDINGS_JS); |
541 source_map_.RegisterSource("omnibox", IDR_OMNIBOX_CUSTOM_BINDINGS_JS); | 561 source_map_.RegisterSource("omnibox", IDR_OMNIBOX_CUSTOM_BINDINGS_JS); |
542 source_map_.RegisterSource("pageActions", | 562 source_map_.RegisterSource("pageActions", |
543 IDR_PAGE_ACTIONS_CUSTOM_BINDINGS_JS); | 563 IDR_PAGE_ACTIONS_CUSTOM_BINDINGS_JS); |
544 source_map_.RegisterSource("pageAction", IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS); | 564 source_map_.RegisterSource("pageAction", IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS); |
545 source_map_.RegisterSource("pageCapture", | 565 source_map_.RegisterSource("pageCapture", |
546 IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS); | 566 IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS); |
547 source_map_.RegisterSource("platformApp", IDR_PLATFORM_APP_JS); | 567 source_map_.RegisterSource("platformApp", IDR_PLATFORM_APP_JS); |
| 568 source_map_.RegisterSource("runtime", IDR_RUNTIME_CUSTOM_BINDINGS_JS); |
548 source_map_.RegisterSource("storage", IDR_STORAGE_CUSTOM_BINDINGS_JS); | 569 source_map_.RegisterSource("storage", IDR_STORAGE_CUSTOM_BINDINGS_JS); |
549 source_map_.RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS); | 570 source_map_.RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS); |
550 source_map_.RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS); | 571 source_map_.RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS); |
551 source_map_.RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS); | 572 source_map_.RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS); |
552 source_map_.RegisterSource("types", IDR_TYPES_CUSTOM_BINDINGS_JS); | 573 source_map_.RegisterSource("types", IDR_TYPES_CUSTOM_BINDINGS_JS); |
553 source_map_.RegisterSource("webRequest", IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS); | 574 source_map_.RegisterSource("webRequest", IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS); |
554 source_map_.RegisterSource("webstore", IDR_WEBSTORE_CUSTOM_BINDINGS_JS); | 575 source_map_.RegisterSource("webstore", IDR_WEBSTORE_CUSTOM_BINDINGS_JS); |
555 } | 576 } |
556 | 577 |
557 void ExtensionDispatcher::PopulateLazyBindingsMap() { | 578 void ExtensionDispatcher::PopulateLazyBindingsMap() { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); | 636 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); |
616 | 637 |
617 RegisterNativeHandlers(module_system.get(), context); | 638 RegisterNativeHandlers(module_system.get(), context); |
618 | 639 |
619 module_system->RegisterNativeHandler("chrome_hidden", | 640 module_system->RegisterNativeHandler("chrome_hidden", |
620 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler())); | 641 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler())); |
621 module_system->RegisterNativeHandler("print", | 642 module_system->RegisterNativeHandler("print", |
622 scoped_ptr<NativeHandler>(new PrintNativeHandler())); | 643 scoped_ptr<NativeHandler>(new PrintNativeHandler())); |
623 module_system->RegisterNativeHandler("lazy_background_page", | 644 module_system->RegisterNativeHandler("lazy_background_page", |
624 scoped_ptr<NativeHandler>(new LazyBackgroundPageNativeHandler(this))); | 645 scoped_ptr<NativeHandler>(new LazyBackgroundPageNativeHandler(this))); |
| 646 module_system->RegisterNativeHandler("channel", |
| 647 scoped_ptr<NativeHandler>(new ChannelNativeHandler( |
| 648 static_cast<chrome::VersionInfo::Channel>(chrome_channel_)))); |
625 | 649 |
626 int manifest_version = 1; | 650 int manifest_version = 1; |
627 if (extension) | 651 if (extension) |
628 manifest_version = extension->manifest_version(); | 652 manifest_version = extension->manifest_version(); |
629 | 653 |
630 // Create the 'chrome' variable if it doesn't already exist. | 654 // Create the 'chrome' variable if it doesn't already exist. |
631 { | 655 { |
632 v8::HandleScope handle_scope; | 656 v8::HandleScope handle_scope; |
633 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); | 657 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); |
634 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 658 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 static const char kMessage[] = | 923 static const char kMessage[] = |
900 "%s can only be used in an extension process."; | 924 "%s can only be used in an extension process."; |
901 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 925 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
902 v8::ThrowException( | 926 v8::ThrowException( |
903 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 927 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
904 return false; | 928 return false; |
905 } | 929 } |
906 | 930 |
907 return true; | 931 return true; |
908 } | 932 } |
OLD | NEW |