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/string_piece.h" | 10 #include "base/string_piece.h" |
11 #include "base/string_split.h" | |
11 #include "chrome/common/child_process_logging.h" | 12 #include "chrome/common/child_process_logging.h" |
12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/chrome_version_info.h" | 14 #include "chrome/common/chrome_version_info.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" |
18 #include "chrome/common/extensions/features/feature.h" | |
17 #include "chrome/common/extensions/manifest.h" | 19 #include "chrome/common/extensions/manifest.h" |
18 #include "chrome/common/extensions/permissions/permission_set.h" | 20 #include "chrome/common/extensions/permissions/permission_set.h" |
19 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
20 #include "chrome/common/view_type.h" | 22 #include "chrome/common/view_type.h" |
21 #include "chrome/renderer/chrome_render_process_observer.h" | 23 #include "chrome/renderer/chrome_render_process_observer.h" |
22 #include "chrome/renderer/extensions/api_definitions_natives.h" | 24 #include "chrome/renderer/extensions/api_definitions_natives.h" |
23 #include "chrome/renderer/extensions/app_bindings.h" | 25 #include "chrome/renderer/extensions/app_bindings.h" |
24 #include "chrome/renderer/extensions/app_runtime_custom_bindings.h" | 26 #include "chrome/renderer/extensions/app_runtime_custom_bindings.h" |
25 #include "chrome/renderer/extensions/app_window_custom_bindings.h" | 27 #include "chrome/renderer/extensions/app_window_custom_bindings.h" |
28 #include "chrome/renderer/extensions/binding_generating_native_handler.h" | |
26 #include "chrome/renderer/extensions/chrome_v8_context.h" | 29 #include "chrome/renderer/extensions/chrome_v8_context.h" |
27 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 30 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
28 #include "chrome/renderer/extensions/content_watcher.h" | 31 #include "chrome/renderer/extensions/content_watcher.h" |
29 #include "chrome/renderer/extensions/context_menus_custom_bindings.h" | 32 #include "chrome/renderer/extensions/context_menus_custom_bindings.h" |
30 #include "chrome/renderer/extensions/event_bindings.h" | 33 #include "chrome/renderer/extensions/event_bindings.h" |
31 #include "chrome/renderer/extensions/extension_custom_bindings.h" | 34 #include "chrome/renderer/extensions/extension_custom_bindings.h" |
32 #include "chrome/renderer/extensions/extension_groups.h" | 35 #include "chrome/renderer/extensions/extension_groups.h" |
33 #include "chrome/renderer/extensions/extension_helper.h" | 36 #include "chrome/renderer/extensions/extension_helper.h" |
34 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h" | 37 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h" |
35 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h" | 38 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h" |
36 #include "chrome/renderer/extensions/file_system_natives.h" | 39 #include "chrome/renderer/extensions/file_system_natives.h" |
37 #include "chrome/renderer/extensions/i18n_custom_bindings.h" | 40 #include "chrome/renderer/extensions/i18n_custom_bindings.h" |
38 #include "chrome/renderer/extensions/media_galleries_custom_bindings.h" | 41 #include "chrome/renderer/extensions/media_galleries_custom_bindings.h" |
39 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 42 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
40 #include "chrome/renderer/extensions/module_system.h" | 43 #include "chrome/renderer/extensions/module_system.h" |
41 #include "chrome/renderer/extensions/native_handler.h" | 44 #include "chrome/renderer/extensions/object_backed_native_handler.h" |
42 #include "chrome/renderer/extensions/page_actions_custom_bindings.h" | 45 #include "chrome/renderer/extensions/page_actions_custom_bindings.h" |
43 #include "chrome/renderer/extensions/page_capture_custom_bindings.h" | 46 #include "chrome/renderer/extensions/page_capture_custom_bindings.h" |
44 #include "chrome/renderer/extensions/request_sender.h" | 47 #include "chrome/renderer/extensions/request_sender.h" |
45 #include "chrome/renderer/extensions/runtime_custom_bindings.h" | 48 #include "chrome/renderer/extensions/runtime_custom_bindings.h" |
46 #include "chrome/renderer/extensions/send_request_natives.h" | 49 #include "chrome/renderer/extensions/send_request_natives.h" |
47 #include "chrome/renderer/extensions/set_icon_natives.h" | 50 #include "chrome/renderer/extensions/set_icon_natives.h" |
48 #include "chrome/renderer/extensions/sync_file_system_custom_bindings.h" | 51 #include "chrome/renderer/extensions/sync_file_system_custom_bindings.h" |
49 #include "chrome/renderer/extensions/tab_finder.h" | 52 #include "chrome/renderer/extensions/tab_finder.h" |
50 #include "chrome/renderer/extensions/tabs_custom_bindings.h" | 53 #include "chrome/renderer/extensions/tabs_custom_bindings.h" |
51 #include "chrome/renderer/extensions/tts_custom_bindings.h" | 54 #include "chrome/renderer/extensions/tts_custom_bindings.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
82 namespace extensions { | 85 namespace extensions { |
83 | 86 |
84 namespace { | 87 namespace { |
85 | 88 |
86 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; | 89 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; |
87 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; | 90 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; |
88 static const char kEventDispatchFunction[] = "Event.dispatchEvent"; | 91 static const char kEventDispatchFunction[] = "Event.dispatchEvent"; |
89 static const char kOnUnloadEvent[] = "runtime.onSuspend"; | 92 static const char kOnUnloadEvent[] = "runtime.onSuspend"; |
90 static const char kOnSuspendCanceledEvent[] = "runtime.onSuspendCanceled"; | 93 static const char kOnSuspendCanceledEvent[] = "runtime.onSuspendCanceled"; |
91 | 94 |
92 class ChromeHiddenNativeHandler : public NativeHandler { | 95 static v8::Handle<v8::Object> GetOrCreateChrome( |
96 v8::Handle<v8::Context> context) { | |
97 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); | |
98 v8::Handle<v8::Object> global(context->Global()); | |
99 v8::Handle<v8::Value> chrome(global->Get(chrome_string)); | |
100 if (chrome.IsEmpty() || chrome->IsUndefined()) { | |
101 v8::Handle<v8::Object> chrome_object(v8::Object::New()); | |
102 global->Set(chrome_string, chrome_object); | |
103 return chrome_object; | |
104 } | |
105 CHECK(chrome->IsObject()); | |
106 return chrome->ToObject(); | |
107 } | |
108 | |
109 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { | |
110 public: | |
111 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, v8::Isolate* isolate) | |
112 : ObjectBackedNativeHandler(isolate), | |
113 registry_(registry) { | |
114 RouteFunction("GetSchema", | |
115 base::Bind(&SchemaRegistryNativeHandler::GetSchema, | |
116 base::Unretained(this))); | |
117 } | |
118 | |
119 private: | |
120 v8::Handle<v8::Value> GetSchema(const v8::Arguments& args) { | |
121 return registry_->GetSchema(*v8::String::AsciiValue(args[0])); | |
122 } | |
123 | |
124 V8SchemaRegistry* registry_; | |
125 }; | |
126 | |
127 class V8ContextNativeHandler : public ObjectBackedNativeHandler { | |
128 public: | |
129 V8ContextNativeHandler(ChromeV8Context* context, v8::Isolate* isolate) | |
130 : ObjectBackedNativeHandler(isolate), | |
131 context_(context) { | |
132 RouteFunction("GetAvailability", | |
133 base::Bind(&V8ContextNativeHandler::GetAvailability, | |
134 base::Unretained(this))); | |
135 } | |
136 | |
137 private: | |
138 v8::Handle<v8::Value> GetAvailability(const v8::Arguments& args) { | |
139 CHECK_EQ(args.Length(), 1); | |
140 std::string api_name = *v8::String::AsciiValue(args[0]->ToString()); | |
141 Feature::Availability availability = context_->GetAvailability(api_name); | |
142 | |
143 v8::Handle<v8::Object> ret = v8::Object::New(); | |
144 ret->Set(v8::String::New("is_available"), | |
145 v8::Boolean::New(availability.is_available())); | |
146 ret->Set(v8::String::New("message"), | |
147 v8::String::New(availability.message().c_str())); | |
148 return ret; | |
149 } | |
150 | |
151 ChromeV8Context* context_; | |
152 }; | |
153 | |
154 class ChromeHiddenNativeHandler : public ObjectBackedNativeHandler { | |
93 public: | 155 public: |
94 explicit ChromeHiddenNativeHandler(v8::Isolate* isolate) | 156 explicit ChromeHiddenNativeHandler(v8::Isolate* isolate) |
95 : NativeHandler(isolate) { | 157 : ObjectBackedNativeHandler(isolate) { |
96 RouteFunction("GetChromeHidden", | 158 RouteFunction("GetChromeHidden", |
97 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden, | 159 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden, |
98 base::Unretained(this))); | 160 base::Unretained(this))); |
99 } | 161 } |
100 | 162 |
101 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) { | 163 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) { |
102 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent()); | 164 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent()); |
103 } | 165 } |
104 }; | 166 }; |
105 | 167 |
106 class PrintNativeHandler : public NativeHandler { | 168 class ChromeNativeHandler : public ObjectBackedNativeHandler { |
169 public: | |
170 explicit ChromeNativeHandler(v8::Isolate* isolate) | |
171 : ObjectBackedNativeHandler(isolate) { | |
172 RouteFunction("GetChrome", | |
173 base::Bind(&ChromeNativeHandler::GetChrome, base::Unretained(this))); | |
174 } | |
175 | |
176 v8::Handle<v8::Value> GetChrome(const v8::Arguments& args) { | |
177 return GetOrCreateChrome(v8::Context::GetCurrent()); | |
178 } | |
179 }; | |
180 | |
181 class PrintNativeHandler : public ObjectBackedNativeHandler { | |
107 public: | 182 public: |
108 explicit PrintNativeHandler(v8::Isolate* isolate) | 183 explicit PrintNativeHandler(v8::Isolate* isolate) |
109 : NativeHandler(isolate) { | 184 : ObjectBackedNativeHandler(isolate) { |
110 RouteFunction("Print", | 185 RouteFunction("Print", |
111 base::Bind(&PrintNativeHandler::Print, | 186 base::Bind(&PrintNativeHandler::Print, |
112 base::Unretained(this))); | 187 base::Unretained(this))); |
113 } | 188 } |
114 | 189 |
115 v8::Handle<v8::Value> Print(const v8::Arguments& args) { | 190 v8::Handle<v8::Value> Print(const v8::Arguments& args) { |
116 if (args.Length() < 1) | 191 if (args.Length() < 1) |
117 return v8::Undefined(); | 192 return v8::Undefined(); |
118 | 193 |
119 std::vector<std::string> components; | 194 std::vector<std::string> components; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 } | 306 } |
232 | 307 |
233 private: | 308 private: |
234 std::string extension_id_; | 309 std::string extension_id_; |
235 std::string context_type_; | 310 std::string context_type_; |
236 bool is_incognito_context_; | 311 bool is_incognito_context_; |
237 int manifest_version_; | 312 int manifest_version_; |
238 bool send_request_disabled_; | 313 bool send_request_disabled_; |
239 }; | 314 }; |
240 | 315 |
241 class LoggingNativeHandler : public NativeHandler { | 316 class LoggingNativeHandler : public ObjectBackedNativeHandler { |
242 public: | 317 public: |
243 explicit LoggingNativeHandler(v8::Isolate* isolate) | 318 explicit LoggingNativeHandler(v8::Isolate* isolate, ChromeV8Context* context) |
244 : NativeHandler(isolate) { | 319 : ObjectBackedNativeHandler(isolate), |
320 context_(context) { | |
245 RouteFunction("DCHECK", | 321 RouteFunction("DCHECK", |
246 base::Bind(&LoggingNativeHandler::Dcheck, | 322 base::Bind(&LoggingNativeHandler::Dcheck, base::Unretained(this))); |
247 base::Unretained(this))); | 323 RouteFunction("Error", |
324 base::Bind(&LoggingNativeHandler::Error, base::Unretained(this))); | |
248 } | 325 } |
249 | 326 |
250 v8::Handle<v8::Value> Dcheck(const v8::Arguments& args) { | 327 v8::Handle<v8::Value> Dcheck(const v8::Arguments& args) { |
251 CHECK_LE(args.Length(), 2); | 328 CHECK_LE(args.Length(), 2); |
252 bool check_value = args[0]->BooleanValue(); | 329 bool check_value = args[0]->BooleanValue(); |
253 std::string error_message; | 330 std::string error_message; |
254 if (args.Length() == 2) | 331 if (args.Length() == 2) |
255 error_message = "Error: " + std::string(*v8::String::AsciiValue(args[1])); | 332 error_message = "Error: " + std::string(*v8::String::AsciiValue(args[1])); |
256 | 333 |
257 v8::Handle<v8::StackTrace> stack_trace = | 334 v8::Handle<v8::StackTrace> stack_trace = |
258 v8::StackTrace::CurrentStackTrace(10); | 335 v8::StackTrace::CurrentStackTrace(10); |
259 if (stack_trace.IsEmpty() || stack_trace->GetFrameCount() <= 0) { | 336 if (stack_trace.IsEmpty() || stack_trace->GetFrameCount() <= 0) { |
260 error_message += "\n <no stack trace>"; | 337 error_message += "\n <no stack trace>"; |
261 } else { | 338 } else { |
262 for (size_t i = 0; i < (size_t) stack_trace->GetFrameCount(); ++i) { | 339 for (size_t i = 0; i < (size_t) stack_trace->GetFrameCount(); ++i) { |
263 v8::Handle<v8::StackFrame> frame = stack_trace->GetFrame(i); | 340 v8::Handle<v8::StackFrame> frame = stack_trace->GetFrame(i); |
264 CHECK(!frame.IsEmpty()); | 341 CHECK(!frame.IsEmpty()); |
265 error_message += base::StringPrintf("\n at %s (%s:%d:%d)", | 342 error_message += base::StringPrintf("\n at %s (%s:%d:%d)", |
266 ToStringOrDefault(frame->GetFunctionName(), "<anonymous>").c_str(), | 343 ToStringOrDefault(frame->GetFunctionName(), "<anonymous>").c_str(), |
267 ToStringOrDefault(frame->GetScriptName(), "<anonymous>").c_str(), | 344 ToStringOrDefault(frame->GetScriptName(), "<anonymous>").c_str(), |
268 frame->GetLineNumber(), | 345 frame->GetLineNumber(), |
269 frame->GetColumn()); | 346 frame->GetColumn()); |
270 } | 347 } |
271 } | 348 } |
272 DCHECK(check_value) << error_message; | 349 DCHECK(check_value) << error_message; |
273 LOG(WARNING) << error_message; | 350 LOG(WARNING) << error_message; |
274 return v8::Undefined(); | 351 return v8::Undefined(); |
275 } | 352 } |
276 | 353 |
354 v8::Handle<v8::Value> Error(const v8::Arguments& args) { | |
355 CHECK_EQ(args.Length(), 1); | |
356 std::string message = *v8::String::AsciiValue(args[0]->ToString()); | |
357 ExtensionHelper::Get(context_->GetRenderView())->AddMessageToRootConsole( | |
358 content::CONSOLE_MESSAGE_LEVEL_ERROR, | |
359 message); | |
360 return v8::Undefined(); | |
not at google - send to devlin
2013/02/15 22:26:17
I just tested, and console.error works from the on
cduvall
2013/02/19 23:58:49
Hmm I don't know why I thought it didn't, I though
| |
361 } | |
362 | |
277 private: | 363 private: |
278 std::string ToStringOrDefault(const v8::Handle<v8::String>& v8_string, | 364 std::string ToStringOrDefault(const v8::Handle<v8::String>& v8_string, |
279 const std::string& dflt) { | 365 const std::string& dflt) { |
280 if (v8_string.IsEmpty()) | 366 if (v8_string.IsEmpty()) |
281 return dflt; | 367 return dflt; |
282 std::string ascii_value = *v8::String::AsciiValue(v8_string); | 368 std::string ascii_value = *v8::String::AsciiValue(v8_string); |
283 return ascii_value.empty() ? dflt : ascii_value; | 369 return ascii_value.empty() ? dflt : ascii_value; |
284 } | 370 } |
371 | |
372 ChromeV8Context* context_; | |
285 }; | 373 }; |
286 | 374 |
287 void InstallAppBindings(ModuleSystem* module_system, | 375 void InstallAppBindings(ModuleSystem* module_system, |
288 v8::Handle<v8::Object> chrome, | 376 v8::Handle<v8::Object> chrome, |
289 v8::Handle<v8::Object> chrome_hidden) { | 377 v8::Handle<v8::Object> chrome_hidden) { |
290 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); | 378 module_system->SetLazyField(chrome, "app", "app", "chromeApp"); |
291 module_system->SetLazyField(chrome, "appNotifications", "app", | 379 module_system->SetLazyField(chrome, "appNotifications", "app", |
292 "chromeAppNotifications"); | 380 "chromeAppNotifications"); |
293 module_system->SetLazyField(chrome_hidden, "app", "app", | 381 module_system->SetLazyField(chrome_hidden, "app", "app", |
294 "chromeHiddenApp"); | 382 "chromeHiddenApp"); |
295 } | 383 } |
296 | 384 |
297 void InstallWebstoreBindings(ModuleSystem* module_system, | 385 void InstallWebstoreBindings(ModuleSystem* module_system, |
298 v8::Handle<v8::Object> chrome, | 386 v8::Handle<v8::Object> chrome, |
299 v8::Handle<v8::Object> chrome_hidden) { | 387 v8::Handle<v8::Object> chrome_hidden) { |
300 module_system->SetLazyField(chrome, "webstore", "webstore", "chromeWebstore"); | 388 module_system->SetLazyField(chrome, "webstore", "webstore", "chromeWebstore"); |
301 module_system->SetLazyField(chrome_hidden, "webstore", "webstore", | 389 module_system->SetLazyField(chrome_hidden, "webstore", "webstore", |
302 "chromeHiddenWebstore"); | 390 "chromeHiddenWebstore"); |
303 } | 391 } |
304 | 392 |
305 static v8::Handle<v8::Object> GetOrCreateChrome( | |
306 v8::Handle<v8::Context> context) { | |
307 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); | |
308 v8::Handle<v8::Object> global(context->Global()); | |
309 v8::Handle<v8::Value> chrome(global->Get(chrome_string)); | |
310 if (chrome.IsEmpty() || chrome->IsUndefined()) { | |
311 v8::Handle<v8::Object> chrome_object(v8::Object::New()); | |
312 global->Set(chrome_string, chrome_object); | |
313 return chrome_object; | |
314 } | |
315 CHECK(chrome->IsObject()); | |
316 return chrome->ToObject(); | |
317 } | |
318 | |
319 } // namespace | 393 } // namespace |
320 | 394 |
321 Dispatcher::Dispatcher() | 395 Dispatcher::Dispatcher() |
322 : content_watcher_(new ContentWatcher(this)), | 396 : content_watcher_(new ContentWatcher(this)), |
323 is_webkit_initialized_(false), | 397 is_webkit_initialized_(false), |
324 webrequest_adblock_(false), | 398 webrequest_adblock_(false), |
325 webrequest_adblock_plus_(false), | 399 webrequest_adblock_plus_(false), |
326 webrequest_other_(false), | 400 webrequest_other_(false), |
327 source_map_(&ResourceBundle::GetSharedInstance()) { | 401 source_map_(&ResourceBundle::GetSharedInstance()) { |
328 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); | 402 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
553 } // namespace | 627 } // namespace |
554 | 628 |
555 bool Dispatcher::AllowScriptExtension(WebFrame* frame, | 629 bool Dispatcher::AllowScriptExtension(WebFrame* frame, |
556 const std::string& v8_extension_name, | 630 const std::string& v8_extension_name, |
557 int extension_group, | 631 int extension_group, |
558 int world_id) { | 632 int world_id) { |
559 g_hack_extension_group = extension_group; | 633 g_hack_extension_group = extension_group; |
560 return true; | 634 return true; |
561 } | 635 } |
562 | 636 |
637 v8::Handle<v8::Object> Dispatcher::GetOrCreateObject( | |
638 v8::Handle<v8::Object> object, | |
639 const std::string& field) { | |
640 v8::Handle<v8::String> key = v8::String::New(field.c_str()); | |
641 // This little dance is for APIs that may be unavailable but have available | |
642 // children. For example, chrome.app can be unavailable, while | |
643 // chrome.app.runtime is available. The lazy getter for chrome.app must be | |
644 // deleted, so that there isn't an error when accessing chrome.app.runtime. | |
645 if (object->Has(key)) { | |
646 v8::Handle<v8::Object> existing = | |
647 v8::Handle<v8::Object>::Cast(object->Get(key)); | |
648 if (!existing.IsEmpty() && !existing->IsUndefined()) | |
649 return existing; | |
650 else | |
651 object->Delete(key); | |
652 } | |
653 | |
654 v8::Handle<v8::Object> new_object = v8::Object::New(); | |
655 object->Set(key, new_object); | |
656 return new_object; | |
657 } | |
658 | |
659 void Dispatcher::RegisterSchemaGeneratedBindings( | |
660 ModuleSystem* module_system, | |
661 ChromeV8Context* context, | |
662 v8::Handle<v8::Context> v8_context) { | |
663 std::set<std::string> apis = | |
664 ExtensionAPI::GetSharedInstance()->GetAllAPINames(); | |
665 for (std::set<std::string>::iterator it = apis.begin(); | |
666 it != apis.end(); ++it) { | |
667 const std::string& api_name = *it; | |
668 | |
669 std::vector<std::string> split; | |
670 base::SplitString(api_name, '.', &split); | |
671 | |
672 v8::Handle<v8::Object> bind_object = GetOrCreateChrome(v8_context); | |
673 for (size_t i = 0; i < split.size() - 1; ++i) | |
674 bind_object = GetOrCreateObject(bind_object, split[i]); | |
675 | |
676 if (lazy_bindings_map_.find(api_name) != lazy_bindings_map_.end()) { | |
677 InstallBindings(module_system, v8_context, api_name); | |
678 } else if (!source_map_.Contains(api_name)) { | |
679 module_system->RegisterNativeHandler( | |
680 api_name, | |
681 scoped_ptr<NativeHandler>(new BindingGeneratingNativeHandler( | |
682 module_system, | |
683 api_name, | |
684 "binding"))); | |
685 module_system->SetNativeLazyField(bind_object, | |
686 split.back(), | |
687 api_name, | |
688 "binding"); | |
689 } else { | |
690 module_system->SetLazyField(bind_object, | |
691 split.back(), | |
692 api_name, | |
693 "binding"); | |
694 } | |
695 } | |
696 } | |
697 | |
563 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, | 698 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, |
564 ChromeV8Context* context) { | 699 ChromeV8Context* context) { |
565 module_system->RegisterNativeHandler("event_bindings", | 700 module_system->RegisterNativeHandler("event_bindings", |
566 scoped_ptr<NativeHandler>(EventBindings::Get(this))); | 701 scoped_ptr<NativeHandler>(EventBindings::Get(this))); |
567 module_system->RegisterNativeHandler("miscellaneous_bindings", | 702 module_system->RegisterNativeHandler("miscellaneous_bindings", |
568 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this))); | 703 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this))); |
569 module_system->RegisterNativeHandler("apiDefinitions", | 704 module_system->RegisterNativeHandler("apiDefinitions", |
570 scoped_ptr<NativeHandler>(new ApiDefinitionsNatives(this))); | 705 scoped_ptr<NativeHandler>(new ApiDefinitionsNatives(this))); |
571 module_system->RegisterNativeHandler("sendRequest", | 706 module_system->RegisterNativeHandler("sendRequest", |
572 scoped_ptr<NativeHandler>( | 707 scoped_ptr<NativeHandler>( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
617 module_system->RegisterNativeHandler("web_request", | 752 module_system->RegisterNativeHandler("web_request", |
618 scoped_ptr<NativeHandler>(new WebRequestCustomBindings())); | 753 scoped_ptr<NativeHandler>(new WebRequestCustomBindings())); |
619 module_system->RegisterNativeHandler("webstore", | 754 module_system->RegisterNativeHandler("webstore", |
620 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context))); | 755 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context))); |
621 } | 756 } |
622 | 757 |
623 void Dispatcher::PopulateSourceMap() { | 758 void Dispatcher::PopulateSourceMap() { |
624 source_map_.RegisterSource("event_bindings", IDR_EVENT_BINDINGS_JS); | 759 source_map_.RegisterSource("event_bindings", IDR_EVENT_BINDINGS_JS); |
625 source_map_.RegisterSource("miscellaneous_bindings", | 760 source_map_.RegisterSource("miscellaneous_bindings", |
626 IDR_MISCELLANEOUS_BINDINGS_JS); | 761 IDR_MISCELLANEOUS_BINDINGS_JS); |
627 source_map_.RegisterSource("schema_generated_bindings", | |
628 IDR_SCHEMA_GENERATED_BINDINGS_JS); | |
629 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS); | 762 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS); |
630 source_map_.RegisterSource("apitest", IDR_EXTENSION_APITEST_JS); | 763 source_map_.RegisterSource("test", IDR_EXTENSION_APITEST_JS); |
631 | 764 |
632 // Libraries. | 765 // Libraries. |
633 source_map_.RegisterSource("contentWatcher", IDR_CONTENT_WATCHER_JS); | 766 source_map_.RegisterSource("contentWatcher", IDR_CONTENT_WATCHER_JS); |
634 source_map_.RegisterSource("lastError", IDR_LAST_ERROR_JS); | 767 source_map_.RegisterSource("lastError", IDR_LAST_ERROR_JS); |
635 source_map_.RegisterSource("schemaUtils", IDR_SCHEMA_UTILS_JS); | 768 source_map_.RegisterSource("schemaUtils", IDR_SCHEMA_UTILS_JS); |
636 source_map_.RegisterSource("sendRequest", IDR_SEND_REQUEST_JS); | 769 source_map_.RegisterSource("sendRequest", IDR_SEND_REQUEST_JS); |
637 source_map_.RegisterSource("setIcon", IDR_SET_ICON_JS); | 770 source_map_.RegisterSource("setIcon", IDR_SET_ICON_JS); |
638 source_map_.RegisterSource("utils", IDR_UTILS_JS); | 771 source_map_.RegisterSource("utils", IDR_UTILS_JS); |
639 source_map_.RegisterSource("entryIdManager", IDR_ENTRY_ID_MANAGER); | 772 source_map_.RegisterSource("entryIdManager", IDR_ENTRY_ID_MANAGER); |
640 | 773 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
684 IDR_SYSTEM_INDICATOR_CUSTOM_BINDINGS_JS); | 817 IDR_SYSTEM_INDICATOR_CUSTOM_BINDINGS_JS); |
685 source_map_.RegisterSource("tabCapture", IDR_TAB_CAPTURE_CUSTOM_BINDINGS_JS); | 818 source_map_.RegisterSource("tabCapture", IDR_TAB_CAPTURE_CUSTOM_BINDINGS_JS); |
686 source_map_.RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS); | 819 source_map_.RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS); |
687 source_map_.RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS); | 820 source_map_.RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS); |
688 source_map_.RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS); | 821 source_map_.RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS); |
689 source_map_.RegisterSource("types", IDR_TYPES_CUSTOM_BINDINGS_JS); | 822 source_map_.RegisterSource("types", IDR_TYPES_CUSTOM_BINDINGS_JS); |
690 source_map_.RegisterSource("webRequest", IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS); | 823 source_map_.RegisterSource("webRequest", IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS); |
691 source_map_.RegisterSource("webRequestInternal", | 824 source_map_.RegisterSource("webRequestInternal", |
692 IDR_WEB_REQUEST_INTERNAL_CUSTOM_BINDINGS_JS); | 825 IDR_WEB_REQUEST_INTERNAL_CUSTOM_BINDINGS_JS); |
693 source_map_.RegisterSource("webstore", IDR_WEBSTORE_CUSTOM_BINDINGS_JS); | 826 source_map_.RegisterSource("webstore", IDR_WEBSTORE_CUSTOM_BINDINGS_JS); |
827 source_map_.RegisterSource("binding", IDR_BINDING_JS); | |
694 | 828 |
695 // Platform app sources that are not API-specific.. | 829 // Platform app sources that are not API-specific.. |
696 source_map_.RegisterSource("tagWatcher", IDR_TAG_WATCHER_JS); | 830 source_map_.RegisterSource("tagWatcher", IDR_TAG_WATCHER_JS); |
697 source_map_.RegisterSource("webview", IDR_WEB_VIEW_JS); | 831 source_map_.RegisterSource("web_view", IDR_WEB_VIEW_JS); |
698 source_map_.RegisterSource("denyWebview", IDR_WEB_VIEW_DENY_JS); | 832 source_map_.RegisterSource("denyWebview", IDR_WEB_VIEW_DENY_JS); |
699 source_map_.RegisterSource("platformApp", IDR_PLATFORM_APP_JS); | 833 source_map_.RegisterSource("platformApp", IDR_PLATFORM_APP_JS); |
700 source_map_.RegisterSource("injectAppTitlebar", IDR_INJECT_APP_TITLEBAR_JS); | 834 source_map_.RegisterSource("injectAppTitlebar", IDR_INJECT_APP_TITLEBAR_JS); |
701 } | 835 } |
702 | 836 |
703 void Dispatcher::PopulateLazyBindingsMap() { | 837 void Dispatcher::PopulateLazyBindingsMap() { |
704 lazy_bindings_map_["app"] = InstallAppBindings; | 838 lazy_bindings_map_["app"] = InstallAppBindings; |
705 lazy_bindings_map_["webstore"] = InstallWebstoreBindings; | 839 lazy_bindings_map_["webstore"] = InstallWebstoreBindings; |
706 } | 840 } |
707 | 841 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
761 v8_context_set_.Add(context); | 895 v8_context_set_.Add(context); |
762 | 896 |
763 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(v8_context, | 897 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(v8_context, |
764 &source_map_)); | 898 &source_map_)); |
765 // Enable natives in startup. | 899 // Enable natives in startup. |
766 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); | 900 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system.get()); |
767 | 901 |
768 RegisterNativeHandlers(module_system.get(), context); | 902 RegisterNativeHandlers(module_system.get(), context); |
769 | 903 |
770 v8::Isolate* isolate = v8_context->GetIsolate(); | 904 v8::Isolate* isolate = v8_context->GetIsolate(); |
905 module_system->RegisterNativeHandler("chrome", | |
906 scoped_ptr<NativeHandler>(new ChromeNativeHandler(isolate))); | |
771 module_system->RegisterNativeHandler("chrome_hidden", | 907 module_system->RegisterNativeHandler("chrome_hidden", |
772 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler(isolate))); | 908 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler(isolate))); |
773 module_system->RegisterNativeHandler("print", | 909 module_system->RegisterNativeHandler("print", |
774 scoped_ptr<NativeHandler>(new PrintNativeHandler(isolate))); | 910 scoped_ptr<NativeHandler>(new PrintNativeHandler(isolate))); |
775 module_system->RegisterNativeHandler("lazy_background_page", | 911 module_system->RegisterNativeHandler("lazy_background_page", |
776 scoped_ptr<NativeHandler>(new LazyBackgroundPageNativeHandler(this))); | 912 scoped_ptr<NativeHandler>(new LazyBackgroundPageNativeHandler(this))); |
777 module_system->RegisterNativeHandler("logging", | 913 module_system->RegisterNativeHandler("logging", |
778 scoped_ptr<NativeHandler>(new LoggingNativeHandler(isolate))); | 914 scoped_ptr<NativeHandler>(new LoggingNativeHandler(isolate, context))); |
915 module_system->RegisterNativeHandler("schema_registry", | |
916 scoped_ptr<NativeHandler>( | |
917 new SchemaRegistryNativeHandler(v8_schema_registry(), isolate))); | |
918 module_system->RegisterNativeHandler("v8_context", | |
919 scoped_ptr<NativeHandler>(new V8ContextNativeHandler(context, isolate))); | |
779 | 920 |
780 int manifest_version = extension ? extension->manifest_version() : 1; | 921 int manifest_version = extension ? extension->manifest_version() : 1; |
781 bool send_request_disabled = | 922 bool send_request_disabled = |
782 (extension && extension->location() == Manifest::LOAD && | 923 (extension && extension->location() == Manifest::LOAD && |
783 extension->has_lazy_background_page()); | 924 extension->has_lazy_background_page()); |
784 module_system->RegisterNativeHandler("process", | 925 module_system->RegisterNativeHandler("process", |
785 scoped_ptr<NativeHandler>(new ProcessInfoNativeHandler( | 926 scoped_ptr<NativeHandler>(new ProcessInfoNativeHandler( |
786 this, context->GetExtensionID(), | 927 this, context->GetExtensionID(), |
787 context->GetContextTypeDescription(), | 928 context->GetContextTypeDescription(), |
788 ChromeRenderProcessObserver::is_incognito_process(), | 929 ChromeRenderProcessObserver::is_incognito_process(), |
789 manifest_version, send_request_disabled))); | 930 manifest_version, send_request_disabled))); |
790 | 931 |
791 GetOrCreateChrome(v8_context); | 932 GetOrCreateChrome(v8_context); |
792 | 933 |
793 // Loading JavaScript is expensive, so only run the full API bindings | 934 // Loading JavaScript is expensive, so only run the full API bindings |
794 // generation mechanisms in extension pages (NOT all web pages). | 935 // generation mechanisms in extension pages (NOT all web pages). |
795 switch (context_type) { | 936 switch (context_type) { |
796 case Feature::UNSPECIFIED_CONTEXT: | 937 case Feature::UNSPECIFIED_CONTEXT: |
797 case Feature::WEB_PAGE_CONTEXT: | 938 case Feature::WEB_PAGE_CONTEXT: |
798 // TODO(kalman): see comment below about ExtensionAPI. | 939 // TODO(kalman): see comment below about ExtensionAPI. |
799 InstallBindings(module_system.get(), v8_context, "app"); | 940 InstallBindings(module_system.get(), v8_context, "app"); |
800 InstallBindings(module_system.get(), v8_context, "webstore"); | 941 InstallBindings(module_system.get(), v8_context, "webstore"); |
801 break; | 942 break; |
802 | |
803 case Feature::BLESSED_EXTENSION_CONTEXT: | 943 case Feature::BLESSED_EXTENSION_CONTEXT: |
804 case Feature::UNBLESSED_EXTENSION_CONTEXT: | 944 case Feature::UNBLESSED_EXTENSION_CONTEXT: |
805 case Feature::CONTENT_SCRIPT_CONTEXT: { | 945 case Feature::CONTENT_SCRIPT_CONTEXT: { |
806 module_system->Require("miscellaneous_bindings"); | 946 if (extension && !extension->is_platform_app()) |
807 module_system->Require("schema_generated_bindings"); | 947 module_system->Require("miscellaneous_bindings"); |
808 module_system->Require("apitest"); | |
809 | 948 |
810 // TODO(kalman): move this code back out of the switch and execute it | 949 // TODO(kalman): move this code back out of the switch and execute it |
811 // regardless of |context_type|. ExtensionAPI knows how to return the | 950 // regardless of |context_type|. ExtensionAPI knows how to return the |
812 // correct APIs, however, until it doesn't have a 2MB overhead we can't | 951 // correct APIs, however, until it doesn't have a 2MB overhead we can't |
813 // load it in every process. | 952 // load it in every process. |
814 const std::set<std::string>& apis = context->GetAvailableExtensionAPIs(); | 953 RegisterSchemaGeneratedBindings(module_system.get(), |
815 for (std::set<std::string>::const_iterator i = apis.begin(); | 954 context, |
816 i != apis.end(); ++i) { | 955 v8_context); |
817 InstallBindings(module_system.get(), v8_context, *i); | |
818 } | |
819 | |
820 break; | 956 break; |
821 } | 957 } |
822 } | 958 } |
959 // TODO(cduvall): Hack for loading chromeHidden.JSON before an extension can | |
960 // trash the global JSON object (event.js). | |
961 // This is for ExtensionApiTest.Messaging. | |
962 module_system->Require("event_bindings"); | |
not at google - send to devlin
2013/02/15 22:26:17
ah, good point.
I just wrote https://codereview.c
cduvall
2013/02/19 23:58:49
Done. I'll change it to include json once your cha
| |
823 | 963 |
824 // Inject custom JS into the platform app context. | 964 // Inject custom JS into the platform app context. |
825 if (IsWithinPlatformApp(frame)) | 965 if (IsWithinPlatformApp(frame)) |
826 module_system->Require("platformApp"); | 966 module_system->Require("platformApp"); |
827 | 967 |
828 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { | 968 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { |
829 bool has_permission = extension->HasAPIPermission(APIPermission::kWebView); | 969 bool has_permission = extension->HasAPIPermission(APIPermission::kWebView); |
830 module_system->Require(has_permission ? "webview" : "denyWebview"); | 970 module_system->Require(has_permission ? "web_view" : "denyWebview"); |
831 } | 971 } |
832 | 972 |
833 context->set_module_system(module_system.Pass()); | 973 context->set_module_system(module_system.Pass()); |
834 | 974 |
835 context->DispatchOnLoadEvent( | 975 context->DispatchOnLoadEvent( |
836 ChromeRenderProcessObserver::is_incognito_process(), | 976 ChromeRenderProcessObserver::is_incognito_process(), |
837 manifest_version); | 977 manifest_version); |
838 | 978 |
839 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); | 979 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); |
840 } | 980 } |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1149 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 1289 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
1150 v8::ThrowException( | 1290 v8::ThrowException( |
1151 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 1291 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
1152 return false; | 1292 return false; |
1153 } | 1293 } |
1154 | 1294 |
1155 return true; | 1295 return true; |
1156 } | 1296 } |
1157 | 1297 |
1158 } // namespace extensions | 1298 } // namespace extensions |
OLD | NEW |