| OLD | NEW |
| 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 <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include "content/public/renderer/render_thread.h" | 35 #include "content/public/renderer/render_thread.h" |
| 36 #include "extensions/common/api/messaging/message.h" | 36 #include "extensions/common/api/messaging/message.h" |
| 37 #include "extensions/common/constants.h" | 37 #include "extensions/common/constants.h" |
| 38 #include "extensions/common/extension_api.h" | 38 #include "extensions/common/extension_api.h" |
| 39 #include "extensions/common/extension_messages.h" | 39 #include "extensions/common/extension_messages.h" |
| 40 #include "extensions/common/extension_urls.h" | 40 #include "extensions/common/extension_urls.h" |
| 41 #include "extensions/common/feature_switch.h" | 41 #include "extensions/common/feature_switch.h" |
| 42 #include "extensions/common/features/behavior_feature.h" | 42 #include "extensions/common/features/behavior_feature.h" |
| 43 #include "extensions/common/features/feature.h" | 43 #include "extensions/common/features/feature.h" |
| 44 #include "extensions/common/features/feature_provider.h" | 44 #include "extensions/common/features/feature_provider.h" |
| 45 #include "extensions/common/features/feature_util.h" |
| 45 #include "extensions/common/manifest.h" | 46 #include "extensions/common/manifest.h" |
| 46 #include "extensions/common/manifest_constants.h" | 47 #include "extensions/common/manifest_constants.h" |
| 47 #include "extensions/common/manifest_handlers/background_info.h" | 48 #include "extensions/common/manifest_handlers/background_info.h" |
| 48 #include "extensions/common/manifest_handlers/content_capabilities_handler.h" | 49 #include "extensions/common/manifest_handlers/content_capabilities_handler.h" |
| 49 #include "extensions/common/manifest_handlers/externally_connectable.h" | 50 #include "extensions/common/manifest_handlers/externally_connectable.h" |
| 50 #include "extensions/common/manifest_handlers/options_page_info.h" | 51 #include "extensions/common/manifest_handlers/options_page_info.h" |
| 51 #include "extensions/common/message_bundle.h" | 52 #include "extensions/common/message_bundle.h" |
| 52 #include "extensions/common/permissions/permission_set.h" | 53 #include "extensions/common/permissions/permission_set.h" |
| 53 #include "extensions/common/permissions/permissions_data.h" | 54 #include "extensions/common/permissions/permissions_data.h" |
| 54 #include "extensions/common/switches.h" | 55 #include "extensions/common/switches.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 Loading... |
| 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()); | 232 request_sender_.reset(new RequestSender()); |
| 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 Loading... |
| 320 { | 328 { |
| 321 std::unique_ptr<ModuleSystem> module_system( | 329 std::unique_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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 break; | 387 break; |
| 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 | |
| 389 void Dispatcher::DidInitializeServiceWorkerContextOnWorkerThread( | 397 void Dispatcher::DidInitializeServiceWorkerContextOnWorkerThread( |
| 390 v8::Local<v8::Context> v8_context, | 398 v8::Local<v8::Context> v8_context, |
| 399 int embedded_worker_id, |
| 391 const GURL& url) { | 400 const GURL& url) { |
| 392 const base::TimeTicks start_time = base::TimeTicks::Now(); | 401 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 393 | 402 |
| 394 if (!url.SchemeIs(kExtensionScheme) && | 403 if (!url.SchemeIs(kExtensionScheme) && |
| 395 !url.SchemeIs(kExtensionResourceScheme)) { | 404 !url.SchemeIs(kExtensionResourceScheme)) { |
| 396 // Early-out if this isn't a chrome-extension:// or resource scheme, | 405 // 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. | 406 // 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 | 407 // Checking this will also skip over hosted apps, which is the desired |
| 399 // behavior - hosted app service workers are not our concern. | 408 // behavior - hosted app service workers are not our concern. |
| 400 return; | 409 return; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 425 // saying that an extension is ready, and documenting that extension APIs | 434 // saying that an extension is ready, and documenting that extension APIs |
| 426 // won't work before that event has fired? | 435 // won't work before that event has fired? |
| 427 return; | 436 return; |
| 428 } | 437 } |
| 429 | 438 |
| 430 ScriptContext* context = new ScriptContext( | 439 ScriptContext* context = new ScriptContext( |
| 431 v8_context, nullptr, extension, Feature::SERVICE_WORKER_CONTEXT, | 440 v8_context, nullptr, extension, Feature::SERVICE_WORKER_CONTEXT, |
| 432 extension, Feature::SERVICE_WORKER_CONTEXT); | 441 extension, Feature::SERVICE_WORKER_CONTEXT); |
| 433 context->set_url(url); | 442 context->set_url(url); |
| 434 | 443 |
| 444 if (ExtensionsClient::Get()->ExtensionAPIEnabledInExtensionServiceWorkers()) { |
| 445 WorkerThreadDispatcher::Get()->AddWorkerData(embedded_worker_id); |
| 446 { |
| 447 // TODO(lazyboy): Make sure accessing |source_map_| in worker thread is |
| 448 // safe. |
| 449 std::unique_ptr<ModuleSystem> module_system( |
| 450 new ModuleSystem(context, &source_map_)); |
| 451 context->set_module_system(std::move(module_system)); |
| 452 } |
| 453 |
| 454 ModuleSystem* module_system = context->module_system(); |
| 455 // Enable natives in startup. |
| 456 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system); |
| 457 RegisterNativeHandlers( |
| 458 module_system, context, |
| 459 WorkerThreadDispatcher::Get()->GetRequestSender(), |
| 460 WorkerThreadDispatcher::Get()->GetV8SchemaRegistry()); |
| 461 // chrome.Event is part of the public API (although undocumented). Make it |
| 462 // lazily evalulate to Event from event_bindings.js. |
| 463 v8::Local<v8::Object> chrome = AsObjectOrEmpty(GetOrCreateChrome(context)); |
| 464 if (!chrome.IsEmpty()) |
| 465 module_system->SetLazyField(chrome, "Event", kEventBindings, "Event"); |
| 466 |
| 467 UpdateBindingsForContext(context); |
| 468 // TODO(lazyboy): Get rid of RequireGuestViewModules() as this doesn't seem |
| 469 // necessary for Extension SW. |
| 470 RequireGuestViewModules(context); |
| 471 delegate_->RequireAdditionalModules(context, |
| 472 false /* is_within_platform_app */); |
| 473 } |
| 474 |
| 435 g_worker_script_context_set.Get().Insert(base::WrapUnique(context)); | 475 g_worker_script_context_set.Get().Insert(base::WrapUnique(context)); |
| 436 | 476 |
| 437 v8::Isolate* isolate = context->isolate(); | 477 v8::Isolate* isolate = context->isolate(); |
| 438 | 478 |
| 439 // Fetch the source code for service_worker_bindings.js. | 479 // Fetch the source code for service_worker_bindings.js. |
| 440 base::StringPiece script_resource = | 480 base::StringPiece script_resource = |
| 441 ResourceBundle::GetSharedInstance().GetRawDataResource( | 481 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 442 IDR_SERVICE_WORKER_BINDINGS_JS); | 482 IDR_SERVICE_WORKER_BINDINGS_JS); |
| 443 v8::Local<v8::String> script = v8::String::NewExternal( | 483 v8::Local<v8::String> script = v8::String::NewExternal( |
| 444 isolate, new StaticV8ExternalOneByteStringResource(script_resource)); | 484 isolate, new StaticV8ExternalOneByteStringResource(script_resource)); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 // In fact |request_sender_| should really be owned by ScriptContext. | 529 // In fact |request_sender_| should really be owned by ScriptContext. |
| 490 request_sender_->InvalidateSource(context); | 530 request_sender_->InvalidateSource(context); |
| 491 | 531 |
| 492 script_context_set_->Remove(context); | 532 script_context_set_->Remove(context); |
| 493 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); | 533 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); |
| 494 } | 534 } |
| 495 | 535 |
| 496 // static | 536 // static |
| 497 void Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread( | 537 void Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread( |
| 498 v8::Local<v8::Context> v8_context, | 538 v8::Local<v8::Context> v8_context, |
| 539 int embedded_worker_id, |
| 499 const GURL& url) { | 540 const GURL& url) { |
| 500 if (url.SchemeIs(kExtensionScheme) || | 541 if (url.SchemeIs(kExtensionScheme) || |
| 501 url.SchemeIs(kExtensionResourceScheme)) { | 542 url.SchemeIs(kExtensionResourceScheme)) { |
| 502 // See comment in DidInitializeServiceWorkerContextOnWorkerThread. | 543 // See comment in DidInitializeServiceWorkerContextOnWorkerThread. |
| 503 g_worker_script_context_set.Get().Remove(v8_context, url); | 544 g_worker_script_context_set.Get().Remove(v8_context, url); |
| 504 } | 545 } |
| 546 if (ExtensionsClient::Get()->ExtensionAPIEnabledInExtensionServiceWorkers()) |
| 547 WorkerThreadDispatcher::Get()->RemoveWorkerData(embedded_worker_id); |
| 505 } | 548 } |
| 506 | 549 |
| 507 void Dispatcher::DidCreateDocumentElement(blink::WebLocalFrame* frame) { | 550 void Dispatcher::DidCreateDocumentElement(blink::WebLocalFrame* frame) { |
| 508 // Note: use GetEffectiveDocumentURL not just frame->document()->url() | 551 // Note: use GetEffectiveDocumentURL not just frame->document()->url() |
| 509 // so that this also injects the stylesheet on about:blank frames that | 552 // so that this also injects the stylesheet on about:blank frames that |
| 510 // are hosted in the extension process. | 553 // are hosted in the extension process. |
| 511 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 554 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
| 512 frame, frame->document().url(), true /* match_about_blank */); | 555 frame, frame->document().url(), true /* match_about_blank */); |
| 513 | 556 |
| 514 const Extension* extension = | 557 const Extension* extension = |
| (...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1297 blink::WebCustomElement::addEmbedderCustomElementName("webview"); | 1340 blink::WebCustomElement::addEmbedderCustomElementName("webview"); |
| 1298 blink::WebCustomElement::addEmbedderCustomElementName("webviewbrowserplugin"); | 1341 blink::WebCustomElement::addEmbedderCustomElementName("webviewbrowserplugin"); |
| 1299 } | 1342 } |
| 1300 | 1343 |
| 1301 void Dispatcher::UpdateBindings(const std::string& extension_id) { | 1344 void Dispatcher::UpdateBindings(const std::string& extension_id) { |
| 1302 script_context_set().ForEach(extension_id, | 1345 script_context_set().ForEach(extension_id, |
| 1303 base::Bind(&Dispatcher::UpdateBindingsForContext, | 1346 base::Bind(&Dispatcher::UpdateBindingsForContext, |
| 1304 base::Unretained(this))); | 1347 base::Unretained(this))); |
| 1305 } | 1348 } |
| 1306 | 1349 |
| 1350 // Note: this function runs on multiple threads: main renderer thread and |
| 1351 // service worker threads. |
| 1307 void Dispatcher::UpdateBindingsForContext(ScriptContext* context) { | 1352 void Dispatcher::UpdateBindingsForContext(ScriptContext* context) { |
| 1308 v8::HandleScope handle_scope(context->isolate()); | 1353 v8::HandleScope handle_scope(context->isolate()); |
| 1309 v8::Context::Scope context_scope(context->v8_context()); | 1354 v8::Context::Scope context_scope(context->v8_context()); |
| 1310 | 1355 |
| 1311 // TODO(kalman): Make the bindings registration have zero overhead then run | 1356 // TODO(kalman): Make the bindings registration have zero overhead then run |
| 1312 // the same code regardless of context type. | 1357 // the same code regardless of context type. |
| 1313 switch (context->context_type()) { | 1358 switch (context->context_type()) { |
| 1314 case Feature::UNSPECIFIED_CONTEXT: | 1359 case Feature::UNSPECIFIED_CONTEXT: |
| 1315 case Feature::WEB_PAGE_CONTEXT: | 1360 case Feature::WEB_PAGE_CONTEXT: |
| 1316 case Feature::BLESSED_WEB_PAGE_CONTEXT: | 1361 case Feature::BLESSED_WEB_PAGE_CONTEXT: |
| 1317 // Hard-code registration of any APIs that are exposed to webpage-like | 1362 // Hard-code registration of any APIs that are exposed to webpage-like |
| 1318 // contexts, because it's too expensive to run the full bindings code. | 1363 // contexts, because it's too expensive to run the full bindings code. |
| 1319 // All of the same permission checks will still apply. | 1364 // All of the same permission checks will still apply. |
| 1320 if (context->GetAvailability("app").is_available()) | 1365 if (context->GetAvailability("app").is_available()) |
| 1321 RegisterBinding("app", context); | 1366 RegisterBinding("app", context); |
| 1322 if (context->GetAvailability("webstore").is_available()) | 1367 if (context->GetAvailability("webstore").is_available()) |
| 1323 RegisterBinding("webstore", context); | 1368 RegisterBinding("webstore", context); |
| 1324 if (context->GetAvailability("dashboardPrivate").is_available()) | 1369 if (context->GetAvailability("dashboardPrivate").is_available()) |
| 1325 RegisterBinding("dashboardPrivate", context); | 1370 RegisterBinding("dashboardPrivate", context); |
| 1326 if (IsRuntimeAvailableToContext(context)) | 1371 if (IsRuntimeAvailableToContext(context)) |
| 1327 RegisterBinding("runtime", context); | 1372 RegisterBinding("runtime", context); |
| 1328 UpdateContentCapabilities(context); | 1373 UpdateContentCapabilities(context); |
| 1329 break; | 1374 break; |
| 1330 | 1375 |
| 1376 case Feature::SERVICE_WORKER_CONTEXT: |
| 1377 DCHECK(ExtensionsClient::Get() |
| 1378 ->ExtensionAPIEnabledInExtensionServiceWorkers()); |
| 1379 // Intentional fallthrough. |
| 1331 case Feature::BLESSED_EXTENSION_CONTEXT: | 1380 case Feature::BLESSED_EXTENSION_CONTEXT: |
| 1332 case Feature::UNBLESSED_EXTENSION_CONTEXT: | 1381 case Feature::UNBLESSED_EXTENSION_CONTEXT: |
| 1333 case Feature::CONTENT_SCRIPT_CONTEXT: | 1382 case Feature::CONTENT_SCRIPT_CONTEXT: |
| 1334 case Feature::WEBUI_CONTEXT: { | 1383 case Feature::WEBUI_CONTEXT: { |
| 1335 // Extension context; iterate through all the APIs and bind the available | 1384 // Extension context; iterate through all the APIs and bind the available |
| 1336 // ones. | 1385 // ones. |
| 1337 const FeatureProvider* api_feature_provider = | 1386 const FeatureProvider* api_feature_provider = |
| 1338 FeatureProvider::GetAPIFeatures(); | 1387 FeatureProvider::GetAPIFeatures(); |
| 1339 for (const auto& map_entry : api_feature_provider->GetAllFeatures()) { | 1388 for (const auto& map_entry : api_feature_provider->GetAllFeatures()) { |
| 1340 // Internal APIs are included via require(api_name) from internal code | 1389 // Internal APIs are included via require(api_name) from internal code |
| 1341 // rather than chrome[api_name]. | 1390 // rather than chrome[api_name]. |
| 1342 if (map_entry.second->IsInternal()) | 1391 if (map_entry.second->IsInternal()) |
| 1343 continue; | 1392 continue; |
| 1344 | 1393 |
| 1345 // If this API has a parent feature (and isn't marked 'noparent'), | 1394 // If this API has a parent feature (and isn't marked 'noparent'), |
| 1346 // then this must be a function or event, so we should not register. | 1395 // then this must be a function or event, so we should not register. |
| 1347 if (api_feature_provider->GetParent(map_entry.second.get()) != nullptr) | 1396 if (api_feature_provider->GetParent(map_entry.second.get()) != nullptr) |
| 1348 continue; | 1397 continue; |
| 1349 | 1398 |
| 1350 // Skip chrome.test if this isn't a test. | 1399 // Skip chrome.test if this isn't a test. |
| 1351 if (map_entry.first == "test" && | 1400 if (map_entry.first == "test" && |
| 1352 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 1401 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1353 ::switches::kTestType)) { | 1402 ::switches::kTestType)) { |
| 1354 continue; | 1403 continue; |
| 1355 } | 1404 } |
| 1356 | 1405 |
| 1357 if (context->IsAnyFeatureAvailableToContext(*map_entry.second.get())) | 1406 if (context->IsAnyFeatureAvailableToContext(*map_entry.second.get())) { |
| 1407 // TODO(lazyboy): RegisterBinding() uses |source_map_|, any thread |
| 1408 // safety issue? |
| 1358 RegisterBinding(map_entry.first, context); | 1409 RegisterBinding(map_entry.first, context); |
| 1410 } |
| 1359 } | 1411 } |
| 1360 break; | 1412 break; |
| 1361 } | 1413 } |
| 1362 case Feature::SERVICE_WORKER_CONTEXT: | |
| 1363 // Handled in DidInitializeServiceWorkerContextOnWorkerThread(). | |
| 1364 NOTREACHED(); | |
| 1365 break; | |
| 1366 } | 1414 } |
| 1367 } | 1415 } |
| 1368 | 1416 |
| 1369 void Dispatcher::RegisterBinding(const std::string& api_name, | 1417 void Dispatcher::RegisterBinding(const std::string& api_name, |
| 1370 ScriptContext* context) { | 1418 ScriptContext* context) { |
| 1371 std::string bind_name; | 1419 std::string bind_name; |
| 1372 v8::Local<v8::Object> bind_object = | 1420 v8::Local<v8::Object> bind_object = |
| 1373 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); | 1421 GetOrCreateBindObjectIfAvailable(api_name, &bind_name, context); |
| 1374 | 1422 |
| 1375 // Empty if the bind object failed to be created, probably because the | 1423 // Empty if the bind object failed to be created, probably because the |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1403 new BindingGeneratingNativeHandler(context, api_name, "binding"))); | 1451 new BindingGeneratingNativeHandler(context, api_name, "binding"))); |
| 1404 module_system->SetNativeLazyField( | 1452 module_system->SetNativeLazyField( |
| 1405 bind_object, bind_name, api_name, "binding"); | 1453 bind_object, bind_name, api_name, "binding"); |
| 1406 } else { | 1454 } else { |
| 1407 module_system->SetLazyField(bind_object, bind_name, api_name, "binding"); | 1455 module_system->SetLazyField(bind_object, bind_name, api_name, "binding"); |
| 1408 } | 1456 } |
| 1409 } | 1457 } |
| 1410 | 1458 |
| 1411 // NOTE: please use the naming convention "foo_natives" for these. | 1459 // NOTE: please use the naming convention "foo_natives" for these. |
| 1412 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, | 1460 void Dispatcher::RegisterNativeHandlers(ModuleSystem* module_system, |
| 1413 ScriptContext* context) { | 1461 ScriptContext* context, |
| 1414 RegisterNativeHandlers(module_system, | 1462 RequestSender* request_sender, |
| 1415 context, | 1463 V8SchemaRegistry* v8_schema_registry) { |
| 1416 this, | 1464 RegisterNativeHandlers(module_system, context, this, request_sender, |
| 1417 request_sender_.get(), | 1465 v8_schema_registry); |
| 1418 v8_schema_registry_.get()); | |
| 1419 const Extension* extension = context->extension(); | 1466 const Extension* extension = context->extension(); |
| 1420 int manifest_version = extension ? extension->manifest_version() : 1; | 1467 int manifest_version = extension ? extension->manifest_version() : 1; |
| 1421 bool is_component_extension = | 1468 bool is_component_extension = |
| 1422 extension && Manifest::IsComponentLocation(extension->location()); | 1469 extension && Manifest::IsComponentLocation(extension->location()); |
| 1423 bool send_request_disabled = | 1470 bool send_request_disabled = |
| 1424 (extension && Manifest::IsUnpackedLocation(extension->location()) && | 1471 (extension && Manifest::IsUnpackedLocation(extension->location()) && |
| 1425 BackgroundInfo::HasLazyBackgroundPage(extension)); | 1472 BackgroundInfo::HasLazyBackgroundPage(extension)); |
| 1426 module_system->RegisterNativeHandler( | 1473 module_system->RegisterNativeHandler( |
| 1427 "process", | 1474 "process", |
| 1428 std::unique_ptr<NativeHandler>(new ProcessInfoNativeHandler( | 1475 std::unique_ptr<NativeHandler>(new ProcessInfoNativeHandler( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1476 iter != active_extension_ids_.end(); | 1523 iter != active_extension_ids_.end(); |
| 1477 ++iter) { | 1524 ++iter) { |
| 1478 const Extension* extension = | 1525 const Extension* extension = |
| 1479 RendererExtensionRegistry::Get()->GetByID(*iter); | 1526 RendererExtensionRegistry::Get()->GetByID(*iter); |
| 1480 if (extension && extension->is_platform_app()) | 1527 if (extension && extension->is_platform_app()) |
| 1481 return true; | 1528 return true; |
| 1482 } | 1529 } |
| 1483 return false; | 1530 return false; |
| 1484 } | 1531 } |
| 1485 | 1532 |
| 1533 // static. |
| 1486 v8::Local<v8::Object> Dispatcher::GetOrCreateObject( | 1534 v8::Local<v8::Object> Dispatcher::GetOrCreateObject( |
| 1487 const v8::Local<v8::Object>& object, | 1535 const v8::Local<v8::Object>& object, |
| 1488 const std::string& field, | 1536 const std::string& field, |
| 1489 v8::Isolate* isolate) { | 1537 v8::Isolate* isolate) { |
| 1490 v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str()); | 1538 v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str()); |
| 1491 // If the object has a callback property, it is assumed it is an unavailable | 1539 // If the object has a callback property, it is assumed it is an unavailable |
| 1492 // API, so it is safe to delete. This is checked before GetOrCreateObject is | 1540 // API, so it is safe to delete. This is checked before GetOrCreateObject is |
| 1493 // called. | 1541 // called. |
| 1494 if (object->HasRealNamedCallbackProperty(key)) { | 1542 if (object->HasRealNamedCallbackProperty(key)) { |
| 1495 object->Delete(key); | 1543 object->Delete(key); |
| 1496 } else if (object->HasRealNamedProperty(key)) { | 1544 } else if (object->HasRealNamedProperty(key)) { |
| 1497 v8::Local<v8::Value> value = object->Get(key); | 1545 v8::Local<v8::Value> value = object->Get(key); |
| 1498 CHECK(value->IsObject()); | 1546 CHECK(value->IsObject()); |
| 1499 return v8::Local<v8::Object>::Cast(value); | 1547 return v8::Local<v8::Object>::Cast(value); |
| 1500 } | 1548 } |
| 1501 | 1549 |
| 1502 v8::Local<v8::Object> new_object = v8::Object::New(isolate); | 1550 v8::Local<v8::Object> new_object = v8::Object::New(isolate); |
| 1503 object->Set(key, new_object); | 1551 object->Set(key, new_object); |
| 1504 return new_object; | 1552 return new_object; |
| 1505 } | 1553 } |
| 1506 | 1554 |
| 1555 // static. |
| 1507 v8::Local<v8::Object> Dispatcher::GetOrCreateBindObjectIfAvailable( | 1556 v8::Local<v8::Object> Dispatcher::GetOrCreateBindObjectIfAvailable( |
| 1508 const std::string& api_name, | 1557 const std::string& api_name, |
| 1509 std::string* bind_name, | 1558 std::string* bind_name, |
| 1510 ScriptContext* context) { | 1559 ScriptContext* context) { |
| 1511 std::vector<std::string> split = base::SplitString( | 1560 std::vector<std::string> split = base::SplitString( |
| 1512 api_name, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 1561 api_name, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 1513 | 1562 |
| 1514 v8::Local<v8::Object> bind_object; | 1563 v8::Local<v8::Object> bind_object; |
| 1515 | 1564 |
| 1516 // Check if this API has an ancestor. If the API's ancestor is available and | 1565 // Check if this API has an ancestor. If the API's ancestor is available and |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1602 // The "guestViewDeny" module must always be loaded last. It registers | 1651 // The "guestViewDeny" module must always be loaded last. It registers |
| 1603 // error-providing custom elements for the GuestView types that are not | 1652 // error-providing custom elements for the GuestView types that are not |
| 1604 // available, and thus all of those types must have been checked and loaded | 1653 // available, and thus all of those types must have been checked and loaded |
| 1605 // (or not loaded) beforehand. | 1654 // (or not loaded) beforehand. |
| 1606 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { | 1655 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { |
| 1607 module_system->Require("guestViewDeny"); | 1656 module_system->Require("guestViewDeny"); |
| 1608 } | 1657 } |
| 1609 } | 1658 } |
| 1610 | 1659 |
| 1611 } // namespace extensions | 1660 } // namespace extensions |
| OLD | NEW |