| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include <memory> | 33 #include <memory> |
| 34 #include "core/dom/Document.h" | 34 #include "core/dom/Document.h" |
| 35 #include "core/dom/TaskRunnerHelper.h" | 35 #include "core/dom/TaskRunnerHelper.h" |
| 36 #include "core/events/MessageEvent.h" | 36 #include "core/events/MessageEvent.h" |
| 37 #include "core/exported/WebDataSourceImpl.h" | 37 #include "core/exported/WebDataSourceImpl.h" |
| 38 #include "core/inspector/ConsoleMessage.h" | 38 #include "core/inspector/ConsoleMessage.h" |
| 39 #include "core/loader/FrameLoadRequest.h" | 39 #include "core/loader/FrameLoadRequest.h" |
| 40 #include "core/loader/FrameLoader.h" | 40 #include "core/loader/FrameLoader.h" |
| 41 #include "core/loader/ThreadableLoadingContext.h" | 41 #include "core/loader/ThreadableLoadingContext.h" |
| 42 #include "core/loader/WorkerFetchContext.h" | 42 #include "core/loader/WorkerFetchContext.h" |
| 43 #include "core/loader/appcache/ApplicationCacheHost.h" |
| 43 #include "core/probe/CoreProbes.h" | 44 #include "core/probe/CoreProbes.h" |
| 44 #include "core/workers/ParentFrameTaskRunners.h" | 45 #include "core/workers/ParentFrameTaskRunners.h" |
| 45 #include "core/workers/SharedWorkerGlobalScope.h" | 46 #include "core/workers/SharedWorkerGlobalScope.h" |
| 46 #include "core/workers/SharedWorkerThread.h" | 47 #include "core/workers/SharedWorkerThread.h" |
| 47 #include "core/workers/WorkerClients.h" | 48 #include "core/workers/WorkerClients.h" |
| 48 #include "core/workers/WorkerContentSettingsClient.h" | 49 #include "core/workers/WorkerContentSettingsClient.h" |
| 49 #include "core/workers/WorkerGlobalScope.h" | 50 #include "core/workers/WorkerGlobalScope.h" |
| 50 #include "core/workers/WorkerInspectorProxy.h" | 51 #include "core/workers/WorkerInspectorProxy.h" |
| 51 #include "core/workers/WorkerLoaderProxy.h" | 52 #include "core/workers/WorkerLoaderProxy.h" |
| 52 #include "core/workers/WorkerScriptLoader.h" | 53 #include "core/workers/WorkerScriptLoader.h" |
| 54 #include "core/workers/WorkerShadowFetchContext.h" |
| 53 #include "core/workers/WorkerThreadStartupData.h" | 55 #include "core/workers/WorkerThreadStartupData.h" |
| 54 #include "platform/CrossThreadFunctional.h" | 56 #include "platform/CrossThreadFunctional.h" |
| 55 #include "platform/RuntimeEnabledFeatures.h" | 57 #include "platform/RuntimeEnabledFeatures.h" |
| 56 #include "platform/heap/Handle.h" | 58 #include "platform/heap/Handle.h" |
| 57 #include "platform/heap/Persistent.h" | 59 #include "platform/heap/Persistent.h" |
| 58 #include "platform/loader/fetch/ResourceResponse.h" | 60 #include "platform/loader/fetch/ResourceResponse.h" |
| 59 #include "platform/network/ContentSecurityPolicyParsers.h" | 61 #include "platform/network/ContentSecurityPolicyParsers.h" |
| 60 #include "platform/weborigin/KURL.h" | 62 #include "platform/weborigin/KURL.h" |
| 61 #include "platform/weborigin/SecurityOrigin.h" | 63 #include "platform/weborigin/SecurityOrigin.h" |
| 62 #include "platform/wtf/Functional.h" | 64 #include "platform/wtf/Functional.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 89 worker_inspector_proxy_(WorkerInspectorProxy::Create()), | 91 worker_inspector_proxy_(WorkerInspectorProxy::Create()), |
| 90 client_(client), | 92 client_(client), |
| 91 pause_worker_context_on_start_(false), | 93 pause_worker_context_on_start_(false), |
| 92 is_paused_on_start_(false), | 94 is_paused_on_start_(false), |
| 93 creation_address_space_(kWebAddressSpacePublic) { | 95 creation_address_space_(kWebAddressSpacePublic) { |
| 94 DCHECK(IsMainThread()); | 96 DCHECK(IsMainThread()); |
| 95 } | 97 } |
| 96 | 98 |
| 97 WebSharedWorkerImpl::~WebSharedWorkerImpl() { | 99 WebSharedWorkerImpl::~WebSharedWorkerImpl() { |
| 98 DCHECK(IsMainThread()); | 100 DCHECK(IsMainThread()); |
| 99 DCHECK(web_view_); | 101 // DCHECK(web_view_); |
| 100 // Detach the client before closing the view to avoid getting called back. | |
| 101 main_frame_->SetClient(0); | |
| 102 | 102 |
| 103 web_view_->Close(); | 103 if (shadow_fetch_context_) { |
| 104 main_frame_->Close(); | 104 shadow_fetch_context_->Detach(); |
| 105 } else { |
| 106 // Detach the client before closing the view to avoid getting called back. |
| 107 main_frame_->SetClient(0); |
| 108 |
| 109 web_view_->Close(); |
| 110 main_frame_->Close(); |
| 111 } |
| 112 |
| 105 if (loader_proxy_) | 113 if (loader_proxy_) |
| 106 loader_proxy_->DetachProvider(this); | 114 loader_proxy_->DetachProvider(this); |
| 107 } | 115 } |
| 108 | 116 |
| 109 void WebSharedWorkerImpl::TerminateWorkerThread() { | 117 void WebSharedWorkerImpl::TerminateWorkerThread() { |
| 110 DCHECK(IsMainThread()); | 118 DCHECK(IsMainThread()); |
| 111 if (asked_to_terminate_) | 119 if (asked_to_terminate_) |
| 112 return; | 120 return; |
| 113 asked_to_terminate_ = true; | 121 asked_to_terminate_ = true; |
| 114 if (main_script_loader_) { | 122 if (main_script_loader_) { |
| 115 main_script_loader_->Cancel(); | 123 main_script_loader_->Cancel(); |
| 116 main_script_loader_.Clear(); | 124 main_script_loader_.Clear(); |
| 117 client_->WorkerScriptLoadFailed(); | 125 client_->WorkerScriptLoadFailed(); |
| 118 delete this; | 126 delete this; |
| 119 return; | 127 return; |
| 120 } | 128 } |
| 121 if (worker_thread_) | 129 if (worker_thread_) |
| 122 worker_thread_->Terminate(); | 130 worker_thread_->Terminate(); |
| 123 worker_inspector_proxy_->WorkerThreadTerminated(); | 131 // worker_inspector_proxy_->WorkerThreadTerminated(); |
| 124 } | 132 } |
| 125 | 133 |
| 126 void WebSharedWorkerImpl::InitializeLoader() { | 134 void WebSharedWorkerImpl::InitializeLoader() { |
| 127 DCHECK(IsMainThread()); | 135 DCHECK(IsMainThread()); |
| 128 | 136 |
| 129 // Create 'shadow page'. This page is never displayed, it is used to proxy the | 137 // Create 'shadow page'. This page is never displayed, it is used to proxy the |
| 130 // loading requests from the worker context to the rest of WebKit and Chromium | 138 // loading requests from the worker context to the rest of WebKit and Chromium |
| 131 // infrastructure. | 139 // infrastructure. |
| 132 DCHECK(!web_view_); | 140 DCHECK(!web_view_); |
| 133 web_view_ = WebView::Create(nullptr, kWebPageVisibilityStateVisible); | 141 web_view_ = WebView::Create(nullptr, kWebPageVisibilityStateVisible); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 return client_->CreateDevToolsMessageLoop(); | 229 return client_->CreateDevToolsMessageLoop(); |
| 222 } | 230 } |
| 223 | 231 |
| 224 void WebSharedWorkerImpl::CountFeature(UseCounter::Feature feature) { | 232 void WebSharedWorkerImpl::CountFeature(UseCounter::Feature feature) { |
| 225 DCHECK(IsMainThread()); | 233 DCHECK(IsMainThread()); |
| 226 client_->CountFeature(feature); | 234 client_->CountFeature(feature); |
| 227 } | 235 } |
| 228 | 236 |
| 229 void WebSharedWorkerImpl::PostMessageToPageInspector(const String& message) { | 237 void WebSharedWorkerImpl::PostMessageToPageInspector(const String& message) { |
| 230 DCHECK(IsMainThread()); | 238 DCHECK(IsMainThread()); |
| 231 worker_inspector_proxy_->DispatchMessageFromWorker(message); | 239 // worker_inspector_proxy_->DispatchMessageFromWorker(message); |
| 232 } | 240 } |
| 233 | 241 |
| 234 void WebSharedWorkerImpl::DidCloseWorkerGlobalScope() { | 242 void WebSharedWorkerImpl::DidCloseWorkerGlobalScope() { |
| 235 DCHECK(IsMainThread()); | 243 DCHECK(IsMainThread()); |
| 236 client_->WorkerContextClosed(); | 244 client_->WorkerContextClosed(); |
| 237 TerminateWorkerThread(); | 245 TerminateWorkerThread(); |
| 238 } | 246 } |
| 239 | 247 |
| 240 void WebSharedWorkerImpl::DidTerminateWorkerThread() { | 248 void WebSharedWorkerImpl::DidTerminateWorkerThread() { |
| 241 DCHECK(IsMainThread()); | 249 DCHECK(IsMainThread()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 void WebSharedWorkerImpl::StartWorkerContext( | 290 void WebSharedWorkerImpl::StartWorkerContext( |
| 283 const WebURL& url, | 291 const WebURL& url, |
| 284 const WebString& name, | 292 const WebString& name, |
| 285 const WebString& content_security_policy, | 293 const WebString& content_security_policy, |
| 286 WebContentSecurityPolicyType policy_type, | 294 WebContentSecurityPolicyType policy_type, |
| 287 WebAddressSpace creation_address_space) { | 295 WebAddressSpace creation_address_space) { |
| 288 DCHECK(IsMainThread()); | 296 DCHECK(IsMainThread()); |
| 289 url_ = url; | 297 url_ = url; |
| 290 name_ = name; | 298 name_ = name; |
| 291 creation_address_space_ = creation_address_space; | 299 creation_address_space_ = creation_address_space; |
| 292 InitializeLoader(); | 300 |
| 301 InitializeShadowFetchContext(); |
| 302 |
| 303 // InitializeLoader(); |
| 304 } |
| 305 |
| 306 void WebSharedWorkerImpl::InitializeShadowFetchContext() { |
| 307 DCHECK(IsMainThread()); |
| 308 /* |
| 309 SetDevToolsAgentClient(this); |
| 310 client_->WorkerReadyForInspection(); |
| 311 if (pause_worker_context_on_start_) { |
| 312 is_paused_on_start_ = true; |
| 313 return; |
| 314 } |
| 315 */ |
| 316 ApplicationCacheHost* app_cache_host = ApplicationCacheHost::Create(nullptr); |
| 317 app_cache_host->SetWebApplicationCacheHost( |
| 318 client_->CreateApplicationCacheHost(app_cache_host)); |
| 319 shadow_fetch_context_ = WorkerShadowFetchContext::Create( |
| 320 url_, Platform::Current()->UserAgent(), creation_address_space_, |
| 321 app_cache_host, client_->CreateServiceWorkerNetworkProvider(), |
| 322 Settings::Create(), ParentFrameTaskRunners::Create(nullptr)); |
| 323 |
| 324 loading_document_ = &shadow_fetch_context_->GetExecutionContext(); |
| 325 |
| 326 main_script_loader_ = WorkerScriptLoader::Create(); |
| 327 main_script_loader_->SetRequestContext( |
| 328 WebURLRequest::kRequestContextSharedWorker); |
| 329 CrossOriginRequestPolicy cross_origin_request_policy = |
| 330 (static_cast<KURL>(url_)).ProtocolIsData() ? kAllowCrossOriginRequests |
| 331 : kDenyCrossOriginRequests; |
| 332 main_script_loader_->LoadAsynchronously( |
| 333 *loading_document_, url_, cross_origin_request_policy, |
| 334 creation_address_space_, |
| 335 Bind(&WebSharedWorkerImpl::DidReceiveScriptLoaderResponse, |
| 336 WTF::Unretained(this)), |
| 337 Bind(&WebSharedWorkerImpl::OnScriptLoaderFinished, |
| 338 WTF::Unretained(this))); |
| 293 } | 339 } |
| 294 | 340 |
| 295 void WebSharedWorkerImpl::DidReceiveScriptLoaderResponse() { | 341 void WebSharedWorkerImpl::DidReceiveScriptLoaderResponse() { |
| 296 DCHECK(IsMainThread()); | 342 DCHECK(IsMainThread()); |
| 297 probe::didReceiveScriptResponse(loading_document_, | 343 probe::didReceiveScriptResponse(loading_document_, |
| 298 main_script_loader_->Identifier()); | 344 main_script_loader_->Identifier()); |
| 299 client_->SelectAppCacheID(main_script_loader_->AppCacheID()); | 345 client_->SelectAppCacheID(main_script_loader_->AppCacheID()); |
| 300 } | 346 } |
| 301 | 347 |
| 302 void WebSharedWorkerImpl::OnScriptLoaderFinished() { | 348 void WebSharedWorkerImpl::OnScriptLoaderFinished() { |
| 303 DCHECK(IsMainThread()); | 349 DCHECK(IsMainThread()); |
| 304 DCHECK(loading_document_); | 350 DCHECK(loading_document_); |
| 305 DCHECK(main_script_loader_); | 351 DCHECK(main_script_loader_); |
| 306 if (asked_to_terminate_) | 352 if (asked_to_terminate_) |
| 307 return; | 353 return; |
| 308 if (main_script_loader_->Failed()) { | 354 if (main_script_loader_->Failed()) { |
| 309 main_script_loader_->Cancel(); | 355 main_script_loader_->Cancel(); |
| 310 client_->WorkerScriptLoadFailed(); | 356 client_->WorkerScriptLoadFailed(); |
| 311 | 357 |
| 312 // The SharedWorker was unable to load the initial script, so | 358 // The SharedWorker was unable to load the initial script, so |
| 313 // shut it down right here. | 359 // shut it down right here. |
| 314 delete this; | 360 delete this; |
| 315 return; | 361 return; |
| 316 } | 362 } |
| 317 | 363 |
| 364 /* |
| 318 Document* document = main_frame_->GetFrame()->GetDocument(); | 365 Document* document = main_frame_->GetFrame()->GetDocument(); |
| 319 // FIXME: this document's origin is pristine and without any extra privileges. | 366 // FIXME: this document's origin is pristine and without any extra privileges. |
| 320 // (crbug.com/254993) | 367 // (crbug.com/254993) |
| 321 SecurityOrigin* starter_origin = document->GetSecurityOrigin(); | 368 SecurityOrigin* starter_origin = document->GetSecurityOrigin(); |
| 369 */ |
| 370 SecurityOrigin* starter_origin = |
| 371 shadow_fetch_context_->GetSecurityContext().GetSecurityOrigin(); |
| 322 | 372 |
| 323 WorkerClients* worker_clients = WorkerClients::Create(); | 373 WorkerClients* worker_clients = WorkerClients::Create(); |
| 324 ProvideLocalFileSystemToWorker(worker_clients, | 374 ProvideLocalFileSystemToWorker(worker_clients, |
| 325 LocalFileSystemClient::Create()); | 375 LocalFileSystemClient::Create()); |
| 326 WebSecurityOrigin web_security_origin(loading_document_->GetSecurityOrigin()); | 376 WebSecurityOrigin web_security_origin(starter_origin); |
| 327 ProvideContentSettingsClientToWorker( | 377 ProvideContentSettingsClientToWorker( |
| 328 worker_clients, | 378 worker_clients, |
| 329 WTF::WrapUnique(client_->CreateWorkerContentSettingsClientProxy( | 379 WTF::WrapUnique(client_->CreateWorkerContentSettingsClientProxy( |
| 330 web_security_origin))); | 380 web_security_origin))); |
| 331 ProvideIndexedDBClientToWorker(worker_clients, | 381 ProvideIndexedDBClientToWorker(worker_clients, |
| 332 IndexedDBClientImpl::Create(*worker_clients)); | 382 IndexedDBClientImpl::Create(*worker_clients)); |
| 333 | 383 |
| 334 if (RuntimeEnabledFeatures::offMainThreadFetchEnabled()) { | 384 if (RuntimeEnabledFeatures::offMainThreadFetchEnabled()) { |
| 335 std::unique_ptr<WebWorkerFetchContext> web_worker_fetch_context = | 385 std::unique_ptr<WebWorkerFetchContext> web_worker_fetch_context = |
| 336 client_->CreateWorkerFetchContext( | 386 client_->CreateWorkerFetchContext( |
| 337 WebLocalFrameImpl::FromFrame(main_frame_->GetFrame()) | 387 WebLocalFrameImpl::FromFrame(main_frame_->GetFrame()) |
| 338 ->DataSource() | 388 ->DataSource() |
| 339 ->GetServiceWorkerNetworkProvider()); | 389 ->GetServiceWorkerNetworkProvider()); |
| 340 DCHECK(web_worker_fetch_context); | 390 DCHECK(web_worker_fetch_context); |
| 341 // TODO(horo): Set more information about the context (ex: DataSaverEnabled) | 391 // TODO(horo): Set more information about the context (ex: DataSaverEnabled) |
| 342 // to |web_worker_fetch_context|. | 392 // to |web_worker_fetch_context|. |
| 343 ProvideWorkerFetchContextToWorker(worker_clients, | 393 ProvideWorkerFetchContextToWorker(worker_clients, |
| 344 std::move(web_worker_fetch_context)); | 394 std::move(web_worker_fetch_context)); |
| 345 } | 395 } |
| 346 | 396 |
| 347 ContentSecurityPolicy* content_security_policy = | 397 ContentSecurityPolicy* content_security_policy = |
| 348 main_script_loader_->ReleaseContentSecurityPolicy(); | 398 main_script_loader_->ReleaseContentSecurityPolicy(); |
| 399 /* |
| 349 WorkerThreadStartMode start_mode = | 400 WorkerThreadStartMode start_mode = |
| 350 worker_inspector_proxy_->WorkerStartMode(document); | 401 worker_inspector_proxy_->WorkerStartMode(loading_document_); |
| 402 */ |
| 403 WorkerThreadStartMode start_mode = kDontPauseWorkerGlobalScopeOnStart; |
| 351 std::unique_ptr<WorkerSettings> worker_settings = | 404 std::unique_ptr<WorkerSettings> worker_settings = |
| 352 WTF::WrapUnique(new WorkerSettings(document->GetSettings())); | 405 WTF::WrapUnique(new WorkerSettings(shadow_fetch_context_->GetSettings())); |
| 353 WorkerV8Settings worker_v8_settings = WorkerV8Settings::Default(); | 406 WorkerV8Settings worker_v8_settings = WorkerV8Settings::Default(); |
| 354 worker_v8_settings.atomics_wait_mode_ = | 407 worker_v8_settings.atomics_wait_mode_ = |
| 355 WorkerV8Settings::AtomicsWaitMode::kAllow; | 408 WorkerV8Settings::AtomicsWaitMode::kAllow; |
| 356 std::unique_ptr<WorkerThreadStartupData> startup_data = | 409 std::unique_ptr<WorkerThreadStartupData> startup_data = |
| 357 WorkerThreadStartupData::Create( | 410 WorkerThreadStartupData::Create( |
| 358 url_, loading_document_->UserAgent(), | 411 url_, loading_document_->UserAgent(), |
| 359 main_script_loader_->SourceText(), nullptr, start_mode, | 412 main_script_loader_->SourceText(), nullptr, start_mode, |
| 360 content_security_policy ? content_security_policy->Headers().get() | 413 content_security_policy ? content_security_policy->Headers().get() |
| 361 : nullptr, | 414 : nullptr, |
| 362 main_script_loader_->GetReferrerPolicy(), starter_origin, | 415 main_script_loader_->GetReferrerPolicy(), starter_origin, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 375 | 428 |
| 376 loader_proxy_ = WorkerLoaderProxy::Create(this); | 429 loader_proxy_ = WorkerLoaderProxy::Create(this); |
| 377 reporting_proxy_ = new WebSharedWorkerReportingProxyImpl(this, task_runners); | 430 reporting_proxy_ = new WebSharedWorkerReportingProxyImpl(this, task_runners); |
| 378 worker_thread_ = | 431 worker_thread_ = |
| 379 SharedWorkerThread::Create(name_, loader_proxy_, *reporting_proxy_); | 432 SharedWorkerThread::Create(name_, loader_proxy_, *reporting_proxy_); |
| 380 probe::scriptImported(loading_document_, main_script_loader_->Identifier(), | 433 probe::scriptImported(loading_document_, main_script_loader_->Identifier(), |
| 381 main_script_loader_->SourceText()); | 434 main_script_loader_->SourceText()); |
| 382 main_script_loader_.Clear(); | 435 main_script_loader_.Clear(); |
| 383 | 436 |
| 384 GetWorkerThread()->Start(std::move(startup_data), task_runners); | 437 GetWorkerThread()->Start(std::move(startup_data), task_runners); |
| 385 worker_inspector_proxy_->WorkerThreadCreated(ToDocument(loading_document_), | 438 /* |
| 439 worker_inspector_proxy_->WorkerThreadCreated(loading_document_, |
| 386 GetWorkerThread(), url_); | 440 GetWorkerThread(), url_); |
| 441 */ |
| 387 client_->WorkerScriptLoaded(); | 442 client_->WorkerScriptLoaded(); |
| 388 } | 443 } |
| 389 | 444 |
| 390 void WebSharedWorkerImpl::TerminateWorkerContext() { | 445 void WebSharedWorkerImpl::TerminateWorkerContext() { |
| 391 DCHECK(IsMainThread()); | 446 DCHECK(IsMainThread()); |
| 392 TerminateWorkerThread(); | 447 TerminateWorkerThread(); |
| 393 } | 448 } |
| 394 | 449 |
| 395 void WebSharedWorkerImpl::PauseWorkerContextOnStart() { | 450 void WebSharedWorkerImpl::PauseWorkerContextOnStart() { |
| 396 pause_worker_context_on_start_ = true; | 451 pause_worker_context_on_start_ = true; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 if (devtools_agent) | 483 if (devtools_agent) |
| 429 devtools_agent->DispatchOnInspectorBackend(session_id, call_id, method, | 484 devtools_agent->DispatchOnInspectorBackend(session_id, call_id, method, |
| 430 message); | 485 message); |
| 431 } | 486 } |
| 432 | 487 |
| 433 WebSharedWorker* WebSharedWorker::Create(WebSharedWorkerClient* client) { | 488 WebSharedWorker* WebSharedWorker::Create(WebSharedWorkerClient* client) { |
| 434 return new WebSharedWorkerImpl(client); | 489 return new WebSharedWorkerImpl(client); |
| 435 } | 490 } |
| 436 | 491 |
| 437 } // namespace blink | 492 } // namespace blink |
| OLD | NEW |