Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(398)

Side by Side Diff: extensions/renderer/dispatcher.cc

Issue 1880933002: Begin to enable extension APIs in Extension Service Worker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments - 3 Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/dispatcher.h" 5 #include "extensions/renderer/dispatcher.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 23 matching lines...) Expand all
34 #include "content/public/renderer/render_thread.h" 34 #include "content/public/renderer/render_thread.h"
35 #include "extensions/common/api/messaging/message.h" 35 #include "extensions/common/api/messaging/message.h"
36 #include "extensions/common/constants.h" 36 #include "extensions/common/constants.h"
37 #include "extensions/common/extension_api.h" 37 #include "extensions/common/extension_api.h"
38 #include "extensions/common/extension_messages.h" 38 #include "extensions/common/extension_messages.h"
39 #include "extensions/common/extension_urls.h" 39 #include "extensions/common/extension_urls.h"
40 #include "extensions/common/feature_switch.h" 40 #include "extensions/common/feature_switch.h"
41 #include "extensions/common/features/behavior_feature.h" 41 #include "extensions/common/features/behavior_feature.h"
42 #include "extensions/common/features/feature.h" 42 #include "extensions/common/features/feature.h"
43 #include "extensions/common/features/feature_provider.h" 43 #include "extensions/common/features/feature_provider.h"
44 #include "extensions/common/features/feature_util.h"
44 #include "extensions/common/manifest.h" 45 #include "extensions/common/manifest.h"
45 #include "extensions/common/manifest_constants.h" 46 #include "extensions/common/manifest_constants.h"
46 #include "extensions/common/manifest_handlers/background_info.h" 47 #include "extensions/common/manifest_handlers/background_info.h"
47 #include "extensions/common/manifest_handlers/content_capabilities_handler.h" 48 #include "extensions/common/manifest_handlers/content_capabilities_handler.h"
48 #include "extensions/common/manifest_handlers/externally_connectable.h" 49 #include "extensions/common/manifest_handlers/externally_connectable.h"
49 #include "extensions/common/manifest_handlers/options_page_info.h" 50 #include "extensions/common/manifest_handlers/options_page_info.h"
50 #include "extensions/common/message_bundle.h" 51 #include "extensions/common/message_bundle.h"
51 #include "extensions/common/permissions/permission_set.h" 52 #include "extensions/common/permissions/permission_set.h"
52 #include "extensions/common/permissions/permissions_data.h" 53 #include "extensions/common/permissions/permissions_data.h"
53 #include "extensions/common/switches.h" 54 #include "extensions/common/switches.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 #include "extensions/renderer/set_icon_natives.h" 89 #include "extensions/renderer/set_icon_natives.h"
89 #include "extensions/renderer/static_v8_external_one_byte_string_resource.h" 90 #include "extensions/renderer/static_v8_external_one_byte_string_resource.h"
90 #include "extensions/renderer/test_features_native_handler.h" 91 #include "extensions/renderer/test_features_native_handler.h"
91 #include "extensions/renderer/test_native_handler.h" 92 #include "extensions/renderer/test_native_handler.h"
92 #include "extensions/renderer/user_gestures_native_handler.h" 93 #include "extensions/renderer/user_gestures_native_handler.h"
93 #include "extensions/renderer/utils_native_handler.h" 94 #include "extensions/renderer/utils_native_handler.h"
94 #include "extensions/renderer/v8_context_native_handler.h" 95 #include "extensions/renderer/v8_context_native_handler.h"
95 #include "extensions/renderer/v8_helpers.h" 96 #include "extensions/renderer/v8_helpers.h"
96 #include "extensions/renderer/wake_event_page.h" 97 #include "extensions/renderer/wake_event_page.h"
97 #include "extensions/renderer/worker_script_context_set.h" 98 #include "extensions/renderer/worker_script_context_set.h"
99 #include "extensions/renderer/worker_thread_dispatcher.h"
98 #include "grit/extensions_renderer_resources.h" 100 #include "grit/extensions_renderer_resources.h"
99 #include "mojo/public/js/constants.h" 101 #include "mojo/public/js/constants.h"
100 #include "third_party/WebKit/public/platform/WebString.h" 102 #include "third_party/WebKit/public/platform/WebString.h"
101 #include "third_party/WebKit/public/platform/WebURLRequest.h" 103 #include "third_party/WebKit/public/platform/WebURLRequest.h"
102 #include "third_party/WebKit/public/web/WebCustomElement.h" 104 #include "third_party/WebKit/public/web/WebCustomElement.h"
103 #include "third_party/WebKit/public/web/WebDataSource.h" 105 #include "third_party/WebKit/public/web/WebDataSource.h"
104 #include "third_party/WebKit/public/web/WebDocument.h" 106 #include "third_party/WebKit/public/web/WebDocument.h"
105 #include "third_party/WebKit/public/web/WebFrame.h" 107 #include "third_party/WebKit/public/web/WebFrame.h"
106 #include "third_party/WebKit/public/web/WebLocalFrame.h" 108 #include "third_party/WebKit/public/web/WebLocalFrame.h"
107 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 109 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
108 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" 110 #include "third_party/WebKit/public/web/WebScopedUserGesture.h"
111 #include "third_party/WebKit/public/web/WebScriptController.h"
109 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" 112 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
110 #include "third_party/WebKit/public/web/WebView.h" 113 #include "third_party/WebKit/public/web/WebView.h"
111 #include "ui/base/layout.h" 114 #include "ui/base/layout.h"
112 #include "ui/base/resource/resource_bundle.h" 115 #include "ui/base/resource/resource_bundle.h"
113 #include "v8/include/v8.h" 116 #include "v8/include/v8.h"
114 117
115 using base::UserMetricsAction; 118 using base::UserMetricsAction;
116 using blink::WebDataSource; 119 using blink::WebDataSource;
117 using blink::WebDocument; 120 using blink::WebDocument;
118 using blink::WebScopedUserGesture; 121 using blink::WebScopedUserGesture;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 } 225 }
223 226
224 script_context_set_.reset(new ScriptContextSet(&active_extension_ids_)); 227 script_context_set_.reset(new ScriptContextSet(&active_extension_ids_));
225 user_script_set_manager_.reset(new UserScriptSetManager()); 228 user_script_set_manager_.reset(new UserScriptSetManager());
226 script_injection_manager_.reset( 229 script_injection_manager_.reset(
227 new ScriptInjectionManager(user_script_set_manager_.get())); 230 new ScriptInjectionManager(user_script_set_manager_.get()));
228 user_script_set_manager_observer_.Add(user_script_set_manager_.get()); 231 user_script_set_manager_observer_.Add(user_script_set_manager_.get());
229 request_sender_.reset(new RequestSender(this)); 232 request_sender_.reset(new RequestSender(this));
230 PopulateSourceMap(); 233 PopulateSourceMap();
231 WakeEventPage::Get()->Init(RenderThread::Get()); 234 WakeEventPage::Get()->Init(RenderThread::Get());
235 // Ideally this should be done after checking
236 // ExtensionAPIEnabledInExtensionServiceWorkers(), but the Dispatcher is
237 // created so early that sending an IPC from browser/ process to synchronize
238 // this enabled-ness is too late.
239 WorkerThreadDispatcher::Get()->Init(RenderThread::Get());
232 240
233 RenderThread::Get()->RegisterExtension(SafeBuiltins::CreateV8Extension()); 241 RenderThread::Get()->RegisterExtension(SafeBuiltins::CreateV8Extension());
234 242
235 // WebSecurityPolicy whitelists. They should be registered for both 243 // WebSecurityPolicy whitelists. They should be registered for both
236 // chrome-extension: and chrome-extension-resource. 244 // chrome-extension: and chrome-extension-resource.
237 using RegisterFunction = void (*)(const WebString&); 245 using RegisterFunction = void (*)(const WebString&);
238 RegisterFunction register_functions[] = { 246 RegisterFunction register_functions[] = {
239 // Treat as secure because communication with them is entirely in the 247 // Treat as secure because communication with them is entirely in the
240 // browser, so there is no danger of manipulation or eavesdropping on 248 // browser, so there is no danger of manipulation or eavesdropping on
241 // communication with them by third parties. 249 // communication with them by third parties.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 { 328 {
321 scoped_ptr<ModuleSystem> module_system( 329 scoped_ptr<ModuleSystem> module_system(
322 new ModuleSystem(context, &source_map_)); 330 new ModuleSystem(context, &source_map_));
323 context->set_module_system(std::move(module_system)); 331 context->set_module_system(std::move(module_system));
324 } 332 }
325 ModuleSystem* module_system = context->module_system(); 333 ModuleSystem* module_system = context->module_system();
326 334
327 // Enable natives in startup. 335 // Enable natives in startup.
328 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system); 336 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system);
329 337
330 RegisterNativeHandlers(module_system, context); 338 RegisterNativeHandlers(module_system, context, request_sender_.get(),
339 v8_schema_registry_.get());
331 340
332 // chrome.Event is part of the public API (although undocumented). Make it 341 // chrome.Event is part of the public API (although undocumented). Make it
333 // lazily evalulate to Event from event_bindings.js. For extensions only 342 // lazily evalulate to Event from event_bindings.js. For extensions only
334 // though, not all webpages! 343 // though, not all webpages!
335 if (context->extension()) { 344 if (context->extension()) {
336 v8::Local<v8::Object> chrome = AsObjectOrEmpty(GetOrCreateChrome(context)); 345 v8::Local<v8::Object> chrome = AsObjectOrEmpty(GetOrCreateChrome(context));
337 if (!chrome.IsEmpty()) 346 if (!chrome.IsEmpty())
338 module_system->SetLazyField(chrome, "Event", kEventBindings, "Event"); 347 module_system->SetLazyField(chrome, "Event", kEventBindings, "Event");
339 } 348 }
340 349
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 case Feature::SERVICE_WORKER_CONTEXT: 388 case Feature::SERVICE_WORKER_CONTEXT:
380 // Handled in DidInitializeServiceWorkerContextOnWorkerThread(). 389 // Handled in DidInitializeServiceWorkerContextOnWorkerThread().
381 NOTREACHED(); 390 NOTREACHED();
382 break; 391 break;
383 } 392 }
384 393
385 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); 394 VLOG(1) << "Num tracked contexts: " << script_context_set_->size();
386 } 395 }
387 396
388 // static 397 // static
398 void Dispatcher::InstallV8ExtensionForServiceWorkers() {
399 if (ExtensionsClient::Get()->ExtensionAPIEnabledInExtensionServiceWorkers()) {
400 blink::WebScriptController::registerExtensionForServiceWorkerScript(
401 SafeBuiltins::CreateV8Extension());
402 }
403 }
404
389 void Dispatcher::DidInitializeServiceWorkerContextOnWorkerThread( 405 void Dispatcher::DidInitializeServiceWorkerContextOnWorkerThread(
390 v8::Local<v8::Context> v8_context, 406 v8::Local<v8::Context> v8_context,
407 int embedded_worker_id,
391 const GURL& url) { 408 const GURL& url) {
392 const base::TimeTicks start_time = base::TimeTicks::Now(); 409 const base::TimeTicks start_time = base::TimeTicks::Now();
393 410
394 if (!url.SchemeIs(kExtensionScheme) && 411 if (!url.SchemeIs(kExtensionScheme) &&
395 !url.SchemeIs(kExtensionResourceScheme)) { 412 !url.SchemeIs(kExtensionResourceScheme)) {
396 // Early-out if this isn't a chrome-extension:// or resource scheme, 413 // Early-out if this isn't a chrome-extension:// or resource scheme,
397 // because looking up the extension registry is unnecessary if it's not. 414 // because looking up the extension registry is unnecessary if it's not.
398 // Checking this will also skip over hosted apps, which is the desired 415 // Checking this will also skip over hosted apps, which is the desired
399 // behavior - hosted app service workers are not our concern. 416 // behavior - hosted app service workers are not our concern.
400 return; 417 return;
(...skipping 24 matching lines...) Expand all
425 // saying that an extension is ready, and documenting that extension APIs 442 // saying that an extension is ready, and documenting that extension APIs
426 // won't work before that event has fired? 443 // won't work before that event has fired?
427 return; 444 return;
428 } 445 }
429 446
430 ScriptContext* context = new ScriptContext( 447 ScriptContext* context = new ScriptContext(
431 v8_context, nullptr, extension, Feature::SERVICE_WORKER_CONTEXT, 448 v8_context, nullptr, extension, Feature::SERVICE_WORKER_CONTEXT,
432 extension, Feature::SERVICE_WORKER_CONTEXT); 449 extension, Feature::SERVICE_WORKER_CONTEXT);
433 context->set_url(url); 450 context->set_url(url);
434 451
452 if (ExtensionsClient::Get()->ExtensionAPIEnabledInExtensionServiceWorkers()) {
453 WorkerThreadDispatcher::Get()->AddWorkerData(embedded_worker_id);
454 {
455 // TODO(lazyboy): Make sure accessing |source_map_| in worker thread is
456 // safe.
457 std::unique_ptr<ModuleSystem> module_system(
458 new ModuleSystem(context, &source_map_));
459 context->set_module_system(std::move(module_system));
460 }
461
462 ModuleSystem* module_system = context->module_system();
463 // Enable natives in startup.
464 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system);
465 RegisterNativeHandlers(
466 module_system, context,
467 WorkerThreadDispatcher::Get()->GetRequestSender(),
468 WorkerThreadDispatcher::Get()->GetV8SchemaRegistry());
469 // chrome.Event is part of the public API (although undocumented). Make it
470 // lazily evalulate to Event from event_bindings.js.
471 v8::Local<v8::Object> chrome = AsObjectOrEmpty(GetOrCreateChrome(context));
472 if (!chrome.IsEmpty())
473 module_system->SetLazyField(chrome, "Event", kEventBindings, "Event");
474
475 UpdateBindingsForContext(context);
476 // TODO(lazyboy): Get rid of RequireGuestViewModules() as this doesn't seem
477 // necessary for Extension SW.
478 RequireGuestViewModules(context);
479 delegate_->RequireAdditionalModules(context,
480 false /* is_within_platform_app */);
481 }
482
435 g_worker_script_context_set.Get().Insert(make_scoped_ptr(context)); 483 g_worker_script_context_set.Get().Insert(make_scoped_ptr(context));
436 484
437 v8::Isolate* isolate = context->isolate(); 485 v8::Isolate* isolate = context->isolate();
438 486
439 // Fetch the source code for service_worker_bindings.js. 487 // Fetch the source code for service_worker_bindings.js.
440 base::StringPiece script_resource = 488 base::StringPiece script_resource =
441 ResourceBundle::GetSharedInstance().GetRawDataResource( 489 ResourceBundle::GetSharedInstance().GetRawDataResource(
442 IDR_SERVICE_WORKER_BINDINGS_JS); 490 IDR_SERVICE_WORKER_BINDINGS_JS);
443 v8::Local<v8::String> script = v8::String::NewExternal( 491 v8::Local<v8::String> script = v8::String::NewExternal(
444 isolate, new StaticV8ExternalOneByteStringResource(script_resource)); 492 isolate, new StaticV8ExternalOneByteStringResource(script_resource));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 // In fact |request_sender_| should really be owned by ScriptContext. 537 // In fact |request_sender_| should really be owned by ScriptContext.
490 request_sender_->InvalidateSource(context); 538 request_sender_->InvalidateSource(context);
491 539
492 script_context_set_->Remove(context); 540 script_context_set_->Remove(context);
493 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); 541 VLOG(1) << "Num tracked contexts: " << script_context_set_->size();
494 } 542 }
495 543
496 // static 544 // static
497 void Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread( 545 void Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread(
498 v8::Local<v8::Context> v8_context, 546 v8::Local<v8::Context> v8_context,
547 int embedded_worker_id,
499 const GURL& url) { 548 const GURL& url) {
500 if (url.SchemeIs(kExtensionScheme) || 549 if (url.SchemeIs(kExtensionScheme) ||
501 url.SchemeIs(kExtensionResourceScheme)) { 550 url.SchemeIs(kExtensionResourceScheme)) {
502 // See comment in DidInitializeServiceWorkerContextOnWorkerThread. 551 // See comment in DidInitializeServiceWorkerContextOnWorkerThread.
503 g_worker_script_context_set.Get().Remove(v8_context, url); 552 g_worker_script_context_set.Get().Remove(v8_context, url);
504 } 553 }
554 if (ExtensionsClient::Get()->ExtensionAPIEnabledInExtensionServiceWorkers())
555 WorkerThreadDispatcher::Get()->RemoveWorkerData(embedded_worker_id);
505 } 556 }
506 557
507 void Dispatcher::DidCreateDocumentElement(blink::WebLocalFrame* frame) { 558 void Dispatcher::DidCreateDocumentElement(blink::WebLocalFrame* frame) {
508 // Note: use GetEffectiveDocumentURL not just frame->document()->url() 559 // Note: use GetEffectiveDocumentURL not just frame->document()->url()
509 // so that this also injects the stylesheet on about:blank frames that 560 // so that this also injects the stylesheet on about:blank frames that
510 // are hosted in the extension process. 561 // are hosted in the extension process.
511 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( 562 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL(
512 frame, frame->document().url(), true /* match_about_blank */); 563 frame, frame->document().url(), true /* match_about_blank */);
513 564
514 const Extension* extension = 565 const Extension* extension =
(...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after
1299 blink::WebCustomElement::addEmbedderCustomElementName("webview"); 1350 blink::WebCustomElement::addEmbedderCustomElementName("webview");
1300 blink::WebCustomElement::addEmbedderCustomElementName("webviewbrowserplugin"); 1351 blink::WebCustomElement::addEmbedderCustomElementName("webviewbrowserplugin");
1301 } 1352 }
1302 1353
1303 void Dispatcher::UpdateBindings(const std::string& extension_id) { 1354 void Dispatcher::UpdateBindings(const std::string& extension_id) {
1304 script_context_set().ForEach(extension_id, 1355 script_context_set().ForEach(extension_id,
1305 base::Bind(&Dispatcher::UpdateBindingsForContext, 1356 base::Bind(&Dispatcher::UpdateBindingsForContext,
1306 base::Unretained(this))); 1357 base::Unretained(this)));
1307 } 1358 }
1308 1359
1360 // Note: this function runs on multiple threads: main renderer thread and
1361 // service worker threads.
1309 void Dispatcher::UpdateBindingsForContext(ScriptContext* context) { 1362 void Dispatcher::UpdateBindingsForContext(ScriptContext* context) {
1310 v8::HandleScope handle_scope(context->isolate()); 1363 v8::HandleScope handle_scope(context->isolate());
1311 v8::Context::Scope context_scope(context->v8_context()); 1364 v8::Context::Scope context_scope(context->v8_context());
1312 1365
1313 // TODO(kalman): Make the bindings registration have zero overhead then run 1366 // TODO(kalman): Make the bindings registration have zero overhead then run
1314 // the same code regardless of context type. 1367 // the same code regardless of context type.
1315 switch (context->context_type()) { 1368 switch (context->context_type()) {
1316 case Feature::UNSPECIFIED_CONTEXT: 1369 case Feature::UNSPECIFIED_CONTEXT:
1317 case Feature::WEB_PAGE_CONTEXT: 1370 case Feature::WEB_PAGE_CONTEXT:
1318 case Feature::BLESSED_WEB_PAGE_CONTEXT: 1371 case Feature::BLESSED_WEB_PAGE_CONTEXT:
1319 // Hard-code registration of any APIs that are exposed to webpage-like 1372 // Hard-code registration of any APIs that are exposed to webpage-like
1320 // contexts, because it's too expensive to run the full bindings code. 1373 // contexts, because it's too expensive to run the full bindings code.
1321 // All of the same permission checks will still apply. 1374 // All of the same permission checks will still apply.
1322 if (context->GetAvailability("app").is_available()) 1375 if (context->GetAvailability("app").is_available())
1323 RegisterBinding("app", context); 1376 RegisterBinding("app", context);
1324 if (context->GetAvailability("webstore").is_available()) 1377 if (context->GetAvailability("webstore").is_available())
1325 RegisterBinding("webstore", context); 1378 RegisterBinding("webstore", context);
1326 if (context->GetAvailability("dashboardPrivate").is_available()) 1379 if (context->GetAvailability("dashboardPrivate").is_available())
1327 RegisterBinding("dashboardPrivate", context); 1380 RegisterBinding("dashboardPrivate", context);
1328 if (IsRuntimeAvailableToContext(context)) 1381 if (IsRuntimeAvailableToContext(context))
1329 RegisterBinding("runtime", context); 1382 RegisterBinding("runtime", context);
1330 UpdateContentCapabilities(context); 1383 UpdateContentCapabilities(context);
1331 break; 1384 break;
1332 1385
1386 case Feature::SERVICE_WORKER_CONTEXT:
1387 DCHECK(ExtensionsClient::Get()
1388 ->ExtensionAPIEnabledInExtensionServiceWorkers());
1389 // Intentional fallthrough.
1333 case Feature::BLESSED_EXTENSION_CONTEXT: 1390 case Feature::BLESSED_EXTENSION_CONTEXT:
1334 case Feature::UNBLESSED_EXTENSION_CONTEXT: 1391 case Feature::UNBLESSED_EXTENSION_CONTEXT:
1335 case Feature::CONTENT_SCRIPT_CONTEXT: 1392 case Feature::CONTENT_SCRIPT_CONTEXT:
1336 case Feature::WEBUI_CONTEXT: { 1393 case Feature::WEBUI_CONTEXT: {
1337 // Extension context; iterate through all the APIs and bind the available 1394 // Extension context; iterate through all the APIs and bind the available
1338 // ones. 1395 // ones.
1339 const FeatureProvider* api_feature_provider = 1396 const FeatureProvider* api_feature_provider =
1340 FeatureProvider::GetAPIFeatures(); 1397 FeatureProvider::GetAPIFeatures();
1341 for (const auto& map_entry : api_feature_provider->GetAllFeatures()) { 1398 for (const auto& map_entry : api_feature_provider->GetAllFeatures()) {
1342 // Internal APIs are included via require(api_name) from internal code 1399 // Internal APIs are included via require(api_name) from internal code
1343 // rather than chrome[api_name]. 1400 // rather than chrome[api_name].
1344 if (map_entry.second->IsInternal()) 1401 if (map_entry.second->IsInternal())
1345 continue; 1402 continue;
1346 1403
1347 // If this API has a parent feature (and isn't marked 'noparent'), 1404 // If this API has a parent feature (and isn't marked 'noparent'),
1348 // then this must be a function or event, so we should not register. 1405 // then this must be a function or event, so we should not register.
1349 if (api_feature_provider->GetParent(map_entry.second.get()) != nullptr) 1406 if (api_feature_provider->GetParent(map_entry.second.get()) != nullptr)
1350 continue; 1407 continue;
1351 1408
1352 // Skip chrome.test if this isn't a test. 1409 // Skip chrome.test if this isn't a test.
1353 if (map_entry.first == "test" && 1410 if (map_entry.first == "test" &&
1354 !base::CommandLine::ForCurrentProcess()->HasSwitch( 1411 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1355 ::switches::kTestType)) { 1412 ::switches::kTestType)) {
1356 continue; 1413 continue;
1357 } 1414 }
1358 1415
1359 if (context->IsAnyFeatureAvailableToContext(*map_entry.second.get())) 1416 if (context->IsAnyFeatureAvailableToContext(*map_entry.second.get())) {
1417 // TODO(lazyboy): RegisterBinding() uses |source_map_|, any thread
1418 // safety issue?
1360 RegisterBinding(map_entry.first, context); 1419 RegisterBinding(map_entry.first, context);
1420 }
1361 } 1421 }
1362 break; 1422 break;
1363 } 1423 }
1364 case Feature::SERVICE_WORKER_CONTEXT:
1365 // Handled in DidInitializeServiceWorkerContextOnWorkerThread().
1366 NOTREACHED();
1367 break;
1368 } 1424 }
1369 } 1425 }
1370 1426
1371 void Dispatcher::RegisterBinding(const std::string& api_name, 1427 void Dispatcher::RegisterBinding(const std::string& api_name,
1372 ScriptContext* context) { 1428 ScriptContext* context) {
1373 std::string bind_name; 1429 std::string bind_name;
1374 v8::Local<v8::Object> bind_object = 1430 v8::Local<v8::Object> bind_object =
1375 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); 1431 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context);
1376 1432
1377 // Empty if the bind object failed to be created, probably because the 1433 // Empty if the bind object failed to be created, probably because the
(...skipping 27 matching lines...) Expand all
1405 context, api_name, "binding"))); 1461 context, api_name, "binding")));
1406 module_system->SetNativeLazyField( 1462 module_system->SetNativeLazyField(
1407 bind_object, bind_name, api_name, "binding"); 1463 bind_object, bind_name, api_name, "binding");
1408 } else { 1464 } else {
1409 module_system->SetLazyField(bind_object, bind_name, api_name, "binding"); 1465 module_system->SetLazyField(bind_object, bind_name, api_name, "binding");
1410 } 1466 }
1411 } 1467 }
1412 1468
1413 // NOTE: please use the naming convention "foo_natives" for these. 1469 // NOTE: please use the naming convention "foo_natives" for these.
1414 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, 1470 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system,
1415 ScriptContext* context) { 1471 ScriptContext* context,
1416 RegisterNativeHandlers(module_system, 1472 RequestSender* request_sender,
1417 context, 1473 V8SchemaRegistry* v8_schema_registry) {
1418 this, 1474 RegisterNativeHandlers(module_system, context, this, request_sender,
1419 request_sender_.get(), 1475 v8_schema_registry);
1420 v8_schema_registry_.get());
1421 const Extension* extension = context->extension(); 1476 const Extension* extension = context->extension();
1422 int manifest_version = extension ? extension->manifest_version() : 1; 1477 int manifest_version = extension ? extension->manifest_version() : 1;
1423 bool is_component_extension = 1478 bool is_component_extension =
1424 extension && Manifest::IsComponentLocation(extension->location()); 1479 extension && Manifest::IsComponentLocation(extension->location());
1425 bool send_request_disabled = 1480 bool send_request_disabled =
1426 (extension && Manifest::IsUnpackedLocation(extension->location()) && 1481 (extension && Manifest::IsUnpackedLocation(extension->location()) &&
1427 BackgroundInfo::HasLazyBackgroundPage(extension)); 1482 BackgroundInfo::HasLazyBackgroundPage(extension));
1428 module_system->RegisterNativeHandler( 1483 module_system->RegisterNativeHandler(
1429 "process", 1484 "process",
1430 scoped_ptr<NativeHandler>(new ProcessInfoNativeHandler( 1485 scoped_ptr<NativeHandler>(new ProcessInfoNativeHandler(
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1481 iter != active_extension_ids_.end(); 1536 iter != active_extension_ids_.end();
1482 ++iter) { 1537 ++iter) {
1483 const Extension* extension = 1538 const Extension* extension =
1484 RendererExtensionRegistry::Get()->GetByID(*iter); 1539 RendererExtensionRegistry::Get()->GetByID(*iter);
1485 if (extension && extension->is_platform_app()) 1540 if (extension && extension->is_platform_app())
1486 return true; 1541 return true;
1487 } 1542 }
1488 return false; 1543 return false;
1489 } 1544 }
1490 1545
1546 // static.
1491 v8::Local<v8::Object> Dispatcher::GetOrCreateObject( 1547 v8::Local<v8::Object> Dispatcher::GetOrCreateObject(
1492 const v8::Local<v8::Object>& object, 1548 const v8::Local<v8::Object>& object,
1493 const std::string& field, 1549 const std::string& field,
1494 v8::Isolate* isolate) { 1550 v8::Isolate* isolate) {
1495 v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str()); 1551 v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str());
1496 // If the object has a callback property, it is assumed it is an unavailable 1552 // If the object has a callback property, it is assumed it is an unavailable
1497 // API, so it is safe to delete. This is checked before GetOrCreateObject is 1553 // API, so it is safe to delete. This is checked before GetOrCreateObject is
1498 // called. 1554 // called.
1499 if (object->HasRealNamedCallbackProperty(key)) { 1555 if (object->HasRealNamedCallbackProperty(key)) {
1500 object->Delete(key); 1556 object->Delete(key);
1501 } else if (object->HasRealNamedProperty(key)) { 1557 } else if (object->HasRealNamedProperty(key)) {
1502 v8::Local<v8::Value> value = object->Get(key); 1558 v8::Local<v8::Value> value = object->Get(key);
1503 CHECK(value->IsObject()); 1559 CHECK(value->IsObject());
1504 return v8::Local<v8::Object>::Cast(value); 1560 return v8::Local<v8::Object>::Cast(value);
1505 } 1561 }
1506 1562
1507 v8::Local<v8::Object> new_object = v8::Object::New(isolate); 1563 v8::Local<v8::Object> new_object = v8::Object::New(isolate);
1508 object->Set(key, new_object); 1564 object->Set(key, new_object);
1509 return new_object; 1565 return new_object;
1510 } 1566 }
1511 1567
1568 // static.
1512 v8::Local<v8::Object> Dispatcher::GetOrCreateBindObjectIfAvailable( 1569 v8::Local<v8::Object> Dispatcher::GetOrCreateBindObjectIfAvailable(
1513 const std::string& api_name, 1570 const std::string& api_name,
1514 std::string* bind_name, 1571 std::string* bind_name,
1515 ScriptContext* context) { 1572 ScriptContext* context) {
1516 std::vector<std::string> split = base::SplitString( 1573 std::vector<std::string> split = base::SplitString(
1517 api_name, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 1574 api_name, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
1518 1575
1519 v8::Local<v8::Object> bind_object; 1576 v8::Local<v8::Object> bind_object;
1520 1577
1521 // Check if this API has an ancestor. If the API's ancestor is available and 1578 // Check if this API has an ancestor. If the API's ancestor is available and
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1601 // The "guestViewDeny" module must always be loaded last. It registers 1658 // The "guestViewDeny" module must always be loaded last. It registers
1602 // error-providing custom elements for the GuestView types that are not 1659 // error-providing custom elements for the GuestView types that are not
1603 // available, and thus all of those types must have been checked and loaded 1660 // available, and thus all of those types must have been checked and loaded
1604 // (or not loaded) beforehand. 1661 // (or not loaded) beforehand.
1605 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { 1662 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) {
1606 module_system->Require("guestViewDeny"); 1663 module_system->Require("guestViewDeny");
1607 } 1664 }
1608 } 1665 }
1609 1666
1610 } // namespace extensions 1667 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698