Chromium Code Reviews| 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_view_type.h" | |
| 13 #include "chrome/common/extensions/api/extension_api.h" | 14 #include "chrome/common/extensions/api/extension_api.h" |
| 14 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_messages.h" | 16 #include "chrome/common/extensions/extension_messages.h" |
| 16 #include "chrome/common/extensions/extension_permission_set.h" | 17 #include "chrome/common/extensions/extension_permission_set.h" |
| 17 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/renderer/chrome_render_process_observer.h" | 19 #include "chrome/renderer/chrome_render_process_observer.h" |
| 19 #include "chrome/renderer/extensions/app_bindings.h" | 20 #include "chrome/renderer/extensions/app_bindings.h" |
| 20 #include "chrome/renderer/extensions/chrome_v8_context.h" | 21 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 21 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 22 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 22 #include "chrome/renderer/extensions/context_menus_custom_bindings.h" | 23 #include "chrome/renderer/extensions/context_menus_custom_bindings.h" |
| 23 #include "chrome/renderer/extensions/event_bindings.h" | 24 #include "chrome/renderer/extensions/event_bindings.h" |
| 24 #include "chrome/renderer/extensions/experimental.socket_custom_bindings.h" | 25 #include "chrome/renderer/extensions/experimental.socket_custom_bindings.h" |
| 25 #include "chrome/renderer/extensions/extension_custom_bindings.h" | 26 #include "chrome/renderer/extensions/extension_custom_bindings.h" |
| 26 #include "chrome/renderer/extensions/extension_groups.h" | 27 #include "chrome/renderer/extensions/extension_groups.h" |
| 28 #include "chrome/renderer/extensions/extension_helper.h" | |
| 27 #include "chrome/renderer/extensions/extension_request_sender.h" | 29 #include "chrome/renderer/extensions/extension_request_sender.h" |
| 28 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h" | 30 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h" |
| 29 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h" | 31 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h" |
| 30 #include "chrome/renderer/extensions/i18n_custom_bindings.h" | 32 #include "chrome/renderer/extensions/i18n_custom_bindings.h" |
| 31 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 33 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
| 32 #include "chrome/renderer/extensions/page_actions_custom_bindings.h" | 34 #include "chrome/renderer/extensions/page_actions_custom_bindings.h" |
| 33 #include "chrome/renderer/extensions/page_capture_custom_bindings.h" | 35 #include "chrome/renderer/extensions/page_capture_custom_bindings.h" |
| 34 #include "chrome/renderer/extensions/schema_generated_bindings.h" | 36 #include "chrome/renderer/extensions/schema_generated_bindings.h" |
| 35 #include "chrome/renderer/extensions/tabs_custom_bindings.h" | 37 #include "chrome/renderer/extensions/tabs_custom_bindings.h" |
| 36 #include "chrome/renderer/extensions/tts_custom_bindings.h" | 38 #include "chrome/renderer/extensions/tts_custom_bindings.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 | 115 |
| 114 std::vector<std::string> components; | 116 std::vector<std::string> components; |
| 115 for (int i = 0; i < args.Length(); ++i) | 117 for (int i = 0; i < args.Length(); ++i) |
| 116 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); | 118 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); |
| 117 | 119 |
| 118 LOG(ERROR) << JoinString(components, ','); | 120 LOG(ERROR) << JoinString(components, ','); |
| 119 return v8::Undefined(); | 121 return v8::Undefined(); |
| 120 } | 122 } |
| 121 }; | 123 }; |
| 122 | 124 |
| 125 class LazyBackgroundPageNativeHandler : public ChromeV8Extension { | |
| 126 public: | |
| 127 explicit LazyBackgroundPageNativeHandler(ExtensionDispatcher* dispatcher) | |
| 128 : ChromeV8Extension(dispatcher) { | |
| 129 RouteFunction("IncrementKeepaliveCount", | |
| 130 base::Bind(&LazyBackgroundPageNativeHandler::IncrementKeepaliveCount, | |
| 131 base::Unretained(this))); | |
| 132 RouteFunction("DecrementKeepaliveCount", | |
| 133 base::Bind(&LazyBackgroundPageNativeHandler::DecrementKeepaliveCount, | |
| 134 base::Unretained(this))); | |
| 135 } | |
| 136 | |
| 137 v8::Handle<v8::Value> IncrementKeepaliveCount(const v8::Arguments& args) { | |
| 138 ChromeV8Context* context = | |
| 139 extension_dispatcher()->v8_context_set().GetCurrent(); | |
| 140 if (IsLazyBackgroundPage(context->extension_id())) { | |
| 141 content::RenderThread::Get()->Send( | |
| 142 new ExtensionHostMsg_IncrementLazyKeepaliveCount( | |
| 143 context->extension_id())); | |
| 144 } | |
| 145 return v8::Undefined(); | |
| 146 } | |
| 147 | |
| 148 v8::Handle<v8::Value> DecrementKeepaliveCount(const v8::Arguments& args) { | |
| 149 ChromeV8Context* context = | |
| 150 extension_dispatcher()->v8_context_set().GetCurrent(); | |
| 151 if (IsLazyBackgroundPage(context->extension_id())) { | |
| 152 content::RenderThread::Get()->Send( | |
| 153 new ExtensionHostMsg_DecrementLazyKeepaliveCount( | |
| 154 context->extension_id())); | |
| 155 } | |
| 156 return v8::Undefined(); | |
| 157 } | |
| 158 | |
| 159 private: | |
| 160 bool IsLazyBackgroundPage(const std::string& extension_id) { | |
|
Yoyo Zhou
2012/04/04 18:52:24
This name seems to be... off.
Matt Perry
2012/04/04 19:21:44
Changed to IsCurrentContextLazyBackgroundPage
| |
| 161 content::RenderView* render_view = GetCurrentRenderView(); | |
| 162 if (!render_view) | |
| 163 return false; | |
| 164 | |
| 165 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | |
| 166 const ::Extension* extension = | |
| 167 extension_dispatcher()->extensions()->GetByID(extension_id); | |
| 168 return (extension && extension->has_lazy_background_page() && | |
| 169 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | |
| 170 } | |
| 171 }; | |
| 172 | |
| 123 void InstallAppBindings(ModuleSystem* module_system, | 173 void InstallAppBindings(ModuleSystem* module_system, |
| 124 v8::Handle<v8::Object> chrome, | 174 v8::Handle<v8::Object> chrome, |
| 125 v8::Handle<v8::Object> chrome_hidden) { | 175 v8::Handle<v8::Object> chrome_hidden) { |
| 126 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); | 176 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); |
| 127 module_system->SetLazyField(chrome, "appNotifications", "app", | 177 module_system->SetLazyField(chrome, "appNotifications", "app", |
| 128 "chromeAppNotifications"); | 178 "chromeAppNotifications"); |
| 129 module_system->SetLazyField(chrome_hidden, "app", "app", | 179 module_system->SetLazyField(chrome_hidden, "app", "app", |
| 130 "chromeHiddenApp"); | 180 "chromeHiddenApp"); |
| 131 } | 181 } |
| 132 | 182 |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 533 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(&source_map_)); | 583 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(&source_map_)); |
| 534 // Enable natives in startup. | 584 // Enable natives in startup. |
| 535 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); | 585 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); |
| 536 | 586 |
| 537 RegisterNativeHandlers(module_system.get(), context); | 587 RegisterNativeHandlers(module_system.get(), context); |
| 538 | 588 |
| 539 module_system->RegisterNativeHandler("chrome_hidden", | 589 module_system->RegisterNativeHandler("chrome_hidden", |
| 540 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler())); | 590 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler())); |
| 541 module_system->RegisterNativeHandler("print", | 591 module_system->RegisterNativeHandler("print", |
| 542 scoped_ptr<NativeHandler>(new PrintNativeHandler())); | 592 scoped_ptr<NativeHandler>(new PrintNativeHandler())); |
| 593 module_system->RegisterNativeHandler("lazy_background_page", | |
| 594 scoped_ptr<NativeHandler>(new LazyBackgroundPageNativeHandler(this))); | |
| 543 | 595 |
| 544 int manifest_version = 1; | 596 int manifest_version = 1; |
| 545 if (extension) | 597 if (extension) |
| 546 manifest_version = extension->manifest_version(); | 598 manifest_version = extension->manifest_version(); |
| 547 | 599 |
| 548 // Create the 'chrome' variable if it doesn't already exist. | 600 // Create the 'chrome' variable if it doesn't already exist. |
| 549 { | 601 { |
| 550 v8::HandleScope handle_scope; | 602 v8::HandleScope handle_scope; |
| 551 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); | 603 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); |
| 552 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 604 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 838 static const char kMessage[] = | 890 static const char kMessage[] = |
| 839 "%s can only be used in an extension process."; | 891 "%s can only be used in an extension process."; |
| 840 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 892 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 841 v8::ThrowException( | 893 v8::ThrowException( |
| 842 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 894 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 843 return false; | 895 return false; |
| 844 } | 896 } |
| 845 | 897 |
| 846 return true; | 898 return true; |
| 847 } | 899 } |
| OLD | NEW |