| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/renderer/service_worker/service_worker_context_client.h" | 5 #include "content/renderer/service_worker/service_worker_context_client.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 ServiceWorkerContextClient::ThreadSpecificInstance() { | 464 ServiceWorkerContextClient::ThreadSpecificInstance() { |
| 465 return g_worker_client_tls.Pointer()->Get(); | 465 return g_worker_client_tls.Pointer()->Get(); |
| 466 } | 466 } |
| 467 | 467 |
| 468 ServiceWorkerContextClient::ServiceWorkerContextClient( | 468 ServiceWorkerContextClient::ServiceWorkerContextClient( |
| 469 int embedded_worker_id, | 469 int embedded_worker_id, |
| 470 int64_t service_worker_version_id, | 470 int64_t service_worker_version_id, |
| 471 const GURL& service_worker_scope, | 471 const GURL& service_worker_scope, |
| 472 const GURL& script_url, | 472 const GURL& script_url, |
| 473 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, | 473 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, |
| 474 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, |
| 474 std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client) | 475 std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client) |
| 475 : embedded_worker_id_(embedded_worker_id), | 476 : embedded_worker_id_(embedded_worker_id), |
| 476 service_worker_version_id_(service_worker_version_id), | 477 service_worker_version_id_(service_worker_version_id), |
| 477 service_worker_scope_(service_worker_scope), | 478 service_worker_scope_(service_worker_scope), |
| 478 script_url_(script_url), | 479 script_url_(script_url), |
| 479 sender_(ChildThreadImpl::current()->thread_safe_sender()), | 480 sender_(ChildThreadImpl::current()->thread_safe_sender()), |
| 480 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 481 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 481 proxy_(nullptr), | 482 proxy_(nullptr), |
| 482 pending_dispatcher_request_(std::move(dispatcher_request)), | 483 pending_dispatcher_request_(std::move(dispatcher_request)), |
| 483 embedded_worker_client_(std::move(embedded_worker_client)) { | 484 embedded_worker_client_(std::move(embedded_worker_client)) { |
| 485 instance_host_ = |
| 486 mojom::ThreadSafeEmbeddedWorkerInstanceHostAssociatedPtr::Create( |
| 487 std::move(instance_host), main_thread_task_runner_); |
| 484 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", | 488 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", |
| 485 "ServiceWorkerContextClient::StartingWorkerContext", | 489 "ServiceWorkerContextClient::StartingWorkerContext", |
| 486 this); | 490 this); |
| 487 TRACE_EVENT_ASYNC_STEP_INTO0( | 491 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 488 "ServiceWorker", | 492 "ServiceWorker", |
| 489 "ServiceWorkerContextClient::StartingWorkerContext", | 493 "ServiceWorkerContextClient::StartingWorkerContext", |
| 490 this, | 494 this, |
| 491 "PrepareWorker"); | 495 "PrepareWorker"); |
| 492 } | 496 } |
| 493 | 497 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 std::vector<char> copy(data, data + size); | 565 std::vector<char> copy(data, data + size); |
| 562 Send(new ServiceWorkerHostMsg_SetCachedMetadata(GetRoutingID(), url, copy)); | 566 Send(new ServiceWorkerHostMsg_SetCachedMetadata(GetRoutingID(), url, copy)); |
| 563 } | 567 } |
| 564 | 568 |
| 565 void ServiceWorkerContextClient::clearCachedMetadata( | 569 void ServiceWorkerContextClient::clearCachedMetadata( |
| 566 const blink::WebURL& url) { | 570 const blink::WebURL& url) { |
| 567 Send(new ServiceWorkerHostMsg_ClearCachedMetadata(GetRoutingID(), url)); | 571 Send(new ServiceWorkerHostMsg_ClearCachedMetadata(GetRoutingID(), url)); |
| 568 } | 572 } |
| 569 | 573 |
| 570 void ServiceWorkerContextClient::workerReadyForInspection() { | 574 void ServiceWorkerContextClient::workerReadyForInspection() { |
| 571 Send(new EmbeddedWorkerHostMsg_WorkerReadyForInspection(embedded_worker_id_)); | 575 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 576 (*instance_host_)->OnReadyForInspection(); |
| 572 } | 577 } |
| 573 | 578 |
| 574 void ServiceWorkerContextClient::workerContextFailedToStart() { | 579 void ServiceWorkerContextClient::workerContextFailedToStart() { |
| 575 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); | 580 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 576 DCHECK(!proxy_); | 581 DCHECK(!proxy_); |
| 577 | 582 |
| 578 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoadFailed(embedded_worker_id_)); | 583 (*instance_host_)->OnScriptLoadFailed(); |
| 584 (*instance_host_)->OnStopped(); |
| 579 | 585 |
| 580 RenderThreadImpl::current()->embedded_worker_dispatcher()-> | 586 RenderThreadImpl::current()->embedded_worker_dispatcher()-> |
| 581 WorkerContextDestroyed(embedded_worker_id_); | 587 WorkerContextDestroyed(embedded_worker_id_); |
| 582 } | 588 } |
| 583 | 589 |
| 584 void ServiceWorkerContextClient::workerScriptLoaded() { | 590 void ServiceWorkerContextClient::workerScriptLoaded() { |
| 585 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); | 591 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 586 DCHECK(!proxy_); | 592 DCHECK(!proxy_); |
| 587 | 593 |
| 588 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded(embedded_worker_id_)); | 594 (*instance_host_)->OnScriptLoaded(); |
| 589 } | 595 } |
| 590 | 596 |
| 591 bool ServiceWorkerContextClient::hasAssociatedRegistration() { | 597 bool ServiceWorkerContextClient::hasAssociatedRegistration() { |
| 592 return provider_context_ && provider_context_->HasAssociatedRegistration(); | 598 return provider_context_ && provider_context_->HasAssociatedRegistration(); |
| 593 } | 599 } |
| 594 | 600 |
| 595 void ServiceWorkerContextClient::workerContextStarted( | 601 void ServiceWorkerContextClient::workerContextStarted( |
| 596 blink::WebServiceWorkerContextProxy* proxy) { | 602 blink::WebServiceWorkerContextProxy* proxy) { |
| 597 DCHECK(!worker_task_runner_.get()); | 603 DCHECK(!worker_task_runner_.get()); |
| 598 DCHECK_NE(0, WorkerThread::GetCurrentId()); | 604 DCHECK_NE(0, WorkerThread::GetCurrentId()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 616 DCHECK_NE(registration_info.registration_id, | 622 DCHECK_NE(registration_info.registration_id, |
| 617 kInvalidServiceWorkerRegistrationId); | 623 kInvalidServiceWorkerRegistrationId); |
| 618 | 624 |
| 619 DCHECK(pending_dispatcher_request_.is_pending()); | 625 DCHECK(pending_dispatcher_request_.is_pending()); |
| 620 DCHECK(!context_->event_dispatcher_binding.is_bound()); | 626 DCHECK(!context_->event_dispatcher_binding.is_bound()); |
| 621 context_->event_dispatcher_binding.Bind( | 627 context_->event_dispatcher_binding.Bind( |
| 622 std::move(pending_dispatcher_request_)); | 628 std::move(pending_dispatcher_request_)); |
| 623 | 629 |
| 624 SetRegistrationInServiceWorkerGlobalScope(registration_info, version_attrs); | 630 SetRegistrationInServiceWorkerGlobalScope(registration_info, version_attrs); |
| 625 | 631 |
| 626 Send(new EmbeddedWorkerHostMsg_WorkerThreadStarted( | 632 (*instance_host_) |
| 627 embedded_worker_id_, WorkerThread::GetCurrentId(), | 633 ->OnThreadStarted(WorkerThread::GetCurrentId(), |
| 628 provider_context_->provider_id())); | 634 provider_context_->provider_id()); |
| 629 | 635 |
| 630 TRACE_EVENT_ASYNC_STEP_INTO0( | 636 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 631 "ServiceWorker", | 637 "ServiceWorker", |
| 632 "ServiceWorkerContextClient::StartingWorkerContext", | 638 "ServiceWorkerContextClient::StartingWorkerContext", |
| 633 this, | 639 this, |
| 634 "ExecuteScript"); | 640 "ExecuteScript"); |
| 635 } | 641 } |
| 636 | 642 |
| 637 void ServiceWorkerContextClient::didEvaluateWorkerScript(bool success) { | 643 void ServiceWorkerContextClient::didEvaluateWorkerScript(bool success) { |
| 638 Send(new EmbeddedWorkerHostMsg_WorkerScriptEvaluated( | 644 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 639 embedded_worker_id_, success)); | 645 (*instance_host_)->OnScriptEvaluated(success); |
| 640 | 646 |
| 641 // Schedule a task to send back WorkerStarted asynchronously, | 647 // Schedule a task to send back WorkerStarted asynchronously, |
| 642 // so that at the time we send it we can be sure that the | 648 // so that at the time we send it we can be sure that the |
| 643 // worker run loop has been started. | 649 // worker run loop has been started. |
| 644 worker_task_runner_->PostTask( | 650 worker_task_runner_->PostTask( |
| 645 FROM_HERE, base::Bind(&ServiceWorkerContextClient::SendWorkerStarted, | 651 FROM_HERE, base::Bind(&ServiceWorkerContextClient::SendWorkerStarted, |
| 646 GetWeakPtr())); | 652 GetWeakPtr())); |
| 647 } | 653 } |
| 648 | 654 |
| 649 void ServiceWorkerContextClient::didInitializeWorkerContext( | 655 void ServiceWorkerContextClient::didInitializeWorkerContext( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 // Check if mojo is enabled | 698 // Check if mojo is enabled |
| 693 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { | 699 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { |
| 694 DCHECK(embedded_worker_client_); | 700 DCHECK(embedded_worker_client_); |
| 695 main_thread_task_runner_->PostTask( | 701 main_thread_task_runner_->PostTask( |
| 696 FROM_HERE, | 702 FROM_HERE, |
| 697 base::Bind(&EmbeddedWorkerInstanceClientImpl::StopWorkerCompleted, | 703 base::Bind(&EmbeddedWorkerInstanceClientImpl::StopWorkerCompleted, |
| 698 base::Passed(&embedded_worker_client_))); | 704 base::Passed(&embedded_worker_client_))); |
| 699 return; | 705 return; |
| 700 } | 706 } |
| 701 | 707 |
| 708 (*instance_host_)->OnStopped(); |
| 709 |
| 702 // Now we should be able to free the WebEmbeddedWorker container on the | 710 // Now we should be able to free the WebEmbeddedWorker container on the |
| 703 // main thread. | 711 // main thread. |
| 704 main_thread_task_runner_->PostTask( | 712 main_thread_task_runner_->PostTask( |
| 705 FROM_HERE, | 713 FROM_HERE, |
| 706 base::Bind(&CallWorkerContextDestroyedOnMainThread, | 714 base::Bind(&CallWorkerContextDestroyedOnMainThread, |
| 707 embedded_worker_id_)); | 715 embedded_worker_id_)); |
| 708 } | 716 } |
| 709 | 717 |
| 710 void ServiceWorkerContextClient::countFeature(uint32_t feature) { | 718 void ServiceWorkerContextClient::countFeature(uint32_t feature) { |
| 711 Send(new EmbeddedWorkerHostMsg_CountFeature(service_worker_version_id_, | 719 Send(new EmbeddedWorkerHostMsg_CountFeature(service_worker_version_id_, |
| 712 feature)); | 720 feature)); |
| 713 } | 721 } |
| 714 | 722 |
| 715 void ServiceWorkerContextClient::reportException( | 723 void ServiceWorkerContextClient::reportException( |
| 716 const blink::WebString& error_message, | 724 const blink::WebString& error_message, |
| 717 int line_number, | 725 int line_number, |
| 718 int column_number, | 726 int column_number, |
| 719 const blink::WebString& source_url) { | 727 const blink::WebString& source_url) { |
| 720 Send(new EmbeddedWorkerHostMsg_ReportException( | 728 (*instance_host_) |
| 721 embedded_worker_id_, error_message.utf16(), line_number, column_number, | 729 ->OnReportException(error_message.utf16(), line_number, column_number, |
| 722 blink::WebStringToGURL(source_url))); | 730 blink::WebStringToGURL(source_url)); |
| 723 } | 731 } |
| 724 | 732 |
| 725 void ServiceWorkerContextClient::reportConsoleMessage( | 733 void ServiceWorkerContextClient::reportConsoleMessage( |
| 726 int source, | 734 int source, |
| 727 int level, | 735 int level, |
| 728 const blink::WebString& message, | 736 const blink::WebString& message, |
| 729 int line_number, | 737 int line_number, |
| 730 const blink::WebString& source_url) { | 738 const blink::WebString& source_url) { |
| 731 EmbeddedWorkerHostMsg_ReportConsoleMessage_Params params; | 739 (*instance_host_) |
| 732 params.source_identifier = source; | 740 ->OnReportConsoleMessage(source, level, message.utf16(), line_number, |
| 733 params.message_level = level; | 741 blink::WebStringToGURL(source_url)); |
| 734 params.message = message.utf16(); | |
| 735 params.line_number = line_number; | |
| 736 params.source_url = blink::WebStringToGURL(source_url); | |
| 737 | |
| 738 Send(new EmbeddedWorkerHostMsg_ReportConsoleMessage( | |
| 739 embedded_worker_id_, params)); | |
| 740 } | 742 } |
| 741 | 743 |
| 742 void ServiceWorkerContextClient::sendDevToolsMessage( | 744 void ServiceWorkerContextClient::sendDevToolsMessage( |
| 743 int session_id, | 745 int session_id, |
| 744 int call_id, | 746 int call_id, |
| 745 const blink::WebString& message, | 747 const blink::WebString& message, |
| 746 const blink::WebString& state_cookie) { | 748 const blink::WebString& state_cookie) { |
| 747 // Return if this context has been stopped. | 749 // Return if this context has been stopped. |
| 748 if (!embedded_worker_client_) | 750 if (!embedded_worker_client_) |
| 749 return; | 751 return; |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1065 | 1067 |
| 1066 void ServiceWorkerContextClient::Send(IPC::Message* message) { | 1068 void ServiceWorkerContextClient::Send(IPC::Message* message) { |
| 1067 sender_->Send(message); | 1069 sender_->Send(message); |
| 1068 } | 1070 } |
| 1069 | 1071 |
| 1070 void ServiceWorkerContextClient::SendWorkerStarted() { | 1072 void ServiceWorkerContextClient::SendWorkerStarted() { |
| 1071 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1073 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 1072 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 1074 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 1073 "ServiceWorkerContextClient::StartingWorkerContext", | 1075 "ServiceWorkerContextClient::StartingWorkerContext", |
| 1074 this); | 1076 this); |
| 1075 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); | 1077 (*instance_host_)->OnStarted(); |
| 1076 } | 1078 } |
| 1077 | 1079 |
| 1078 void ServiceWorkerContextClient::SetRegistrationInServiceWorkerGlobalScope( | 1080 void ServiceWorkerContextClient::SetRegistrationInServiceWorkerGlobalScope( |
| 1079 const ServiceWorkerRegistrationObjectInfo& info, | 1081 const ServiceWorkerRegistrationObjectInfo& info, |
| 1080 const ServiceWorkerVersionAttributes& attrs) { | 1082 const ServiceWorkerVersionAttributes& attrs) { |
| 1081 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1083 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 1082 ServiceWorkerDispatcher* dispatcher = | 1084 ServiceWorkerDispatcher* dispatcher = |
| 1083 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 1085 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 1084 sender_.get(), main_thread_task_runner_.get()); | 1086 sender_.get(), main_thread_task_runner_.get()); |
| 1085 | 1087 |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1503 } | 1505 } |
| 1504 | 1506 |
| 1505 base::WeakPtr<ServiceWorkerContextClient> | 1507 base::WeakPtr<ServiceWorkerContextClient> |
| 1506 ServiceWorkerContextClient::GetWeakPtr() { | 1508 ServiceWorkerContextClient::GetWeakPtr() { |
| 1507 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1509 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 1508 DCHECK(context_); | 1510 DCHECK(context_); |
| 1509 return context_->weak_factory.GetWeakPtr(); | 1511 return context_->weak_factory.GetWeakPtr(); |
| 1510 } | 1512 } |
| 1511 | 1513 |
| 1512 } // namespace content | 1514 } // namespace content |
| OLD | NEW |