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

Side by Side Diff: content/renderer/service_worker/service_worker_context_client.cc

Issue 2787883003: [ServiceWorker] Add EmbeddedWorkerInstanceHost Interface. (Closed)
Patch Set: Address comments from shimazu@ Created 3 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 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 19 matching lines...) Expand all
30 #include "content/child/service_worker/web_service_worker_registration_impl.h" 30 #include "content/child/service_worker/web_service_worker_registration_impl.h"
31 #include "content/child/thread_safe_sender.h" 31 #include "content/child/thread_safe_sender.h"
32 #include "content/child/web_data_consumer_handle_impl.h" 32 #include "content/child/web_data_consumer_handle_impl.h"
33 #include "content/child/web_url_loader_impl.h" 33 #include "content/child/web_url_loader_impl.h"
34 #include "content/child/webmessageportchannel_impl.h" 34 #include "content/child/webmessageportchannel_impl.h"
35 #include "content/common/devtools_messages.h" 35 #include "content/common/devtools_messages.h"
36 #include "content/common/service_worker/embedded_worker_messages.h" 36 #include "content/common/service_worker/embedded_worker_messages.h"
37 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h" 37 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h"
38 #include "content/common/service_worker/service_worker_messages.h" 38 #include "content/common/service_worker/service_worker_messages.h"
39 #include "content/common/service_worker/service_worker_status_code.h" 39 #include "content/common/service_worker/service_worker_status_code.h"
40 #include "content/common/service_worker/service_worker_utils.h"
41 #include "content/public/common/push_event_payload.h" 40 #include "content/public/common/push_event_payload.h"
42 #include "content/public/common/referrer.h" 41 #include "content/public/common/referrer.h"
43 #include "content/public/renderer/content_renderer_client.h" 42 #include "content/public/renderer/content_renderer_client.h"
44 #include "content/public/renderer/document_state.h" 43 #include "content/public/renderer/document_state.h"
45 #include "content/renderer/devtools/devtools_agent.h" 44 #include "content/renderer/devtools/devtools_agent.h"
46 #include "content/renderer/render_thread_impl.h" 45 #include "content/renderer/render_thread_impl.h"
47 #include "content/renderer/service_worker/embedded_worker_devtools_agent.h" 46 #include "content/renderer/service_worker/embedded_worker_devtools_agent.h"
48 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" 47 #include "content/renderer/service_worker/embedded_worker_dispatcher.h"
49 #include "content/renderer/service_worker/embedded_worker_instance_client_impl.h " 48 #include "content/renderer/service_worker/embedded_worker_instance_client_impl.h "
50 #include "content/renderer/service_worker/service_worker_type_converters.h" 49 #include "content/renderer/service_worker/service_worker_type_converters.h"
(...skipping 19 matching lines...) Expand all
70 #include "third_party/WebKit/public/web/modules/serviceworker/WebServiceWorkerCo ntextProxy.h" 69 #include "third_party/WebKit/public/web/modules/serviceworker/WebServiceWorkerCo ntextProxy.h"
71 70
72 namespace content { 71 namespace content {
73 72
74 namespace { 73 namespace {
75 74
76 // For now client must be a per-thread instance. 75 // For now client must be a per-thread instance.
77 base::LazyInstance<base::ThreadLocalPointer<ServiceWorkerContextClient>>:: 76 base::LazyInstance<base::ThreadLocalPointer<ServiceWorkerContextClient>>::
78 Leaky g_worker_client_tls = LAZY_INSTANCE_INITIALIZER; 77 Leaky g_worker_client_tls = LAZY_INSTANCE_INITIALIZER;
79 78
80 void CallWorkerContextDestroyedOnMainThread(int embedded_worker_id) {
81 if (!RenderThreadImpl::current() ||
82 !RenderThreadImpl::current()->embedded_worker_dispatcher())
83 return;
84 RenderThreadImpl::current()->embedded_worker_dispatcher()->
85 WorkerContextDestroyed(embedded_worker_id);
86 }
87
88 // Called on the main thread only and blink owns it. 79 // Called on the main thread only and blink owns it.
89 class WebServiceWorkerNetworkProviderImpl 80 class WebServiceWorkerNetworkProviderImpl
90 : public blink::WebServiceWorkerNetworkProvider { 81 : public blink::WebServiceWorkerNetworkProvider {
91 public: 82 public:
92 explicit WebServiceWorkerNetworkProviderImpl( 83 explicit WebServiceWorkerNetworkProviderImpl(
93 std::unique_ptr<ServiceWorkerNetworkProvider> provider) 84 std::unique_ptr<ServiceWorkerNetworkProvider> provider)
94 : provider_(std::move(provider)) {} 85 : provider_(std::move(provider)) {}
95 86
96 // Blink calls this method for each request starting with the main script, 87 // Blink calls this method for each request starting with the main script,
97 // we tag them with the provider id. 88 // we tag them with the provider id.
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 ServiceWorkerContextClient::ThreadSpecificInstance() { 455 ServiceWorkerContextClient::ThreadSpecificInstance() {
465 return g_worker_client_tls.Pointer()->Get(); 456 return g_worker_client_tls.Pointer()->Get();
466 } 457 }
467 458
468 ServiceWorkerContextClient::ServiceWorkerContextClient( 459 ServiceWorkerContextClient::ServiceWorkerContextClient(
469 int embedded_worker_id, 460 int embedded_worker_id,
470 int64_t service_worker_version_id, 461 int64_t service_worker_version_id,
471 const GURL& service_worker_scope, 462 const GURL& service_worker_scope,
472 const GURL& script_url, 463 const GURL& script_url,
473 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, 464 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request,
465 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host,
474 std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client) 466 std::unique_ptr<EmbeddedWorkerInstanceClientImpl> embedded_worker_client)
475 : embedded_worker_id_(embedded_worker_id), 467 : embedded_worker_id_(embedded_worker_id),
476 service_worker_version_id_(service_worker_version_id), 468 service_worker_version_id_(service_worker_version_id),
477 service_worker_scope_(service_worker_scope), 469 service_worker_scope_(service_worker_scope),
478 script_url_(script_url), 470 script_url_(script_url),
479 sender_(ChildThreadImpl::current()->thread_safe_sender()), 471 sender_(ChildThreadImpl::current()->thread_safe_sender()),
480 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), 472 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
481 proxy_(nullptr), 473 proxy_(nullptr),
482 pending_dispatcher_request_(std::move(dispatcher_request)), 474 pending_dispatcher_request_(std::move(dispatcher_request)),
483 embedded_worker_client_(std::move(embedded_worker_client)) { 475 embedded_worker_client_(std::move(embedded_worker_client)) {
476 instance_host_ =
477 mojom::ThreadSafeEmbeddedWorkerInstanceHostAssociatedPtr::Create(
478 std::move(instance_host), main_thread_task_runner_);
484 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", 479 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker",
485 "ServiceWorkerContextClient::StartingWorkerContext", 480 "ServiceWorkerContextClient::StartingWorkerContext",
486 this); 481 this);
487 TRACE_EVENT_ASYNC_STEP_INTO0( 482 TRACE_EVENT_ASYNC_STEP_INTO0(
488 "ServiceWorker", 483 "ServiceWorker",
489 "ServiceWorkerContextClient::StartingWorkerContext", 484 "ServiceWorkerContextClient::StartingWorkerContext",
490 this, 485 this,
491 "PrepareWorker"); 486 "PrepareWorker");
492 } 487 }
493 488
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 std::vector<char> copy(data, data + size); 556 std::vector<char> copy(data, data + size);
562 Send(new ServiceWorkerHostMsg_SetCachedMetadata(GetRoutingID(), url, copy)); 557 Send(new ServiceWorkerHostMsg_SetCachedMetadata(GetRoutingID(), url, copy));
563 } 558 }
564 559
565 void ServiceWorkerContextClient::clearCachedMetadata( 560 void ServiceWorkerContextClient::clearCachedMetadata(
566 const blink::WebURL& url) { 561 const blink::WebURL& url) {
567 Send(new ServiceWorkerHostMsg_ClearCachedMetadata(GetRoutingID(), url)); 562 Send(new ServiceWorkerHostMsg_ClearCachedMetadata(GetRoutingID(), url));
568 } 563 }
569 564
570 void ServiceWorkerContextClient::workerReadyForInspection() { 565 void ServiceWorkerContextClient::workerReadyForInspection() {
571 Send(new EmbeddedWorkerHostMsg_WorkerReadyForInspection(embedded_worker_id_)); 566 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread());
567 (*instance_host_)->OnReadyForInspection();
572 } 568 }
573 569
574 void ServiceWorkerContextClient::workerContextFailedToStart() { 570 void ServiceWorkerContextClient::workerContextFailedToStart() {
575 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); 571 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread());
576 DCHECK(!proxy_); 572 DCHECK(!proxy_);
577 573
578 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoadFailed(embedded_worker_id_)); 574 (*instance_host_)->OnScriptLoadFailed();
575 (*instance_host_)->OnStopped();
579 576
580 RenderThreadImpl::current()->embedded_worker_dispatcher()-> 577 RenderThreadImpl::current()->embedded_worker_dispatcher()->
581 WorkerContextDestroyed(embedded_worker_id_); 578 WorkerContextDestroyed(embedded_worker_id_);
582 } 579 }
583 580
584 void ServiceWorkerContextClient::workerScriptLoaded() { 581 void ServiceWorkerContextClient::workerScriptLoaded() {
585 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); 582 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread());
586 DCHECK(!proxy_); 583 DCHECK(!proxy_);
587 584
588 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded(embedded_worker_id_)); 585 (*instance_host_)->OnScriptLoaded();
589 } 586 }
590 587
591 bool ServiceWorkerContextClient::hasAssociatedRegistration() { 588 bool ServiceWorkerContextClient::hasAssociatedRegistration() {
592 return provider_context_ && provider_context_->HasAssociatedRegistration(); 589 return provider_context_ && provider_context_->HasAssociatedRegistration();
593 } 590 }
594 591
595 void ServiceWorkerContextClient::workerContextStarted( 592 void ServiceWorkerContextClient::workerContextStarted(
596 blink::WebServiceWorkerContextProxy* proxy) { 593 blink::WebServiceWorkerContextProxy* proxy) {
597 DCHECK(!worker_task_runner_.get()); 594 DCHECK(!worker_task_runner_.get());
598 DCHECK_NE(0, WorkerThread::GetCurrentId()); 595 DCHECK_NE(0, WorkerThread::GetCurrentId());
(...skipping 17 matching lines...) Expand all
616 DCHECK_NE(registration_info.registration_id, 613 DCHECK_NE(registration_info.registration_id,
617 kInvalidServiceWorkerRegistrationId); 614 kInvalidServiceWorkerRegistrationId);
618 615
619 DCHECK(pending_dispatcher_request_.is_pending()); 616 DCHECK(pending_dispatcher_request_.is_pending());
620 DCHECK(!context_->event_dispatcher_binding.is_bound()); 617 DCHECK(!context_->event_dispatcher_binding.is_bound());
621 context_->event_dispatcher_binding.Bind( 618 context_->event_dispatcher_binding.Bind(
622 std::move(pending_dispatcher_request_)); 619 std::move(pending_dispatcher_request_));
623 620
624 SetRegistrationInServiceWorkerGlobalScope(registration_info, version_attrs); 621 SetRegistrationInServiceWorkerGlobalScope(registration_info, version_attrs);
625 622
626 Send(new EmbeddedWorkerHostMsg_WorkerThreadStarted( 623 (*instance_host_)
627 embedded_worker_id_, WorkerThread::GetCurrentId(), 624 ->OnThreadStarted(WorkerThread::GetCurrentId(),
628 provider_context_->provider_id())); 625 provider_context_->provider_id());
629 626
630 TRACE_EVENT_ASYNC_STEP_INTO0( 627 TRACE_EVENT_ASYNC_STEP_INTO0(
631 "ServiceWorker", 628 "ServiceWorker",
632 "ServiceWorkerContextClient::StartingWorkerContext", 629 "ServiceWorkerContextClient::StartingWorkerContext",
633 this, 630 this,
634 "ExecuteScript"); 631 "ExecuteScript");
635 } 632 }
636 633
637 void ServiceWorkerContextClient::didEvaluateWorkerScript(bool success) { 634 void ServiceWorkerContextClient::didEvaluateWorkerScript(bool success) {
638 Send(new EmbeddedWorkerHostMsg_WorkerScriptEvaluated( 635 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
639 embedded_worker_id_, success)); 636 (*instance_host_)->OnScriptEvaluated(success);
640 637
641 // Schedule a task to send back WorkerStarted asynchronously, 638 // Schedule a task to send back WorkerStarted asynchronously,
642 // so that at the time we send it we can be sure that the 639 // so that at the time we send it we can be sure that the
643 // worker run loop has been started. 640 // worker run loop has been started.
644 worker_task_runner_->PostTask( 641 worker_task_runner_->PostTask(
645 FROM_HERE, base::Bind(&ServiceWorkerContextClient::SendWorkerStarted, 642 FROM_HERE, base::Bind(&ServiceWorkerContextClient::SendWorkerStarted,
646 GetWeakPtr())); 643 GetWeakPtr()));
647 } 644 }
648 645
649 void ServiceWorkerContextClient::didInitializeWorkerContext( 646 void ServiceWorkerContextClient::didInitializeWorkerContext(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 // this client. 679 // this client.
683 g_worker_client_tls.Pointer()->Set(NULL); 680 g_worker_client_tls.Pointer()->Set(NULL);
684 681
685 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( 682 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread(
686 context, service_worker_version_id_, script_url_); 683 context, service_worker_version_id_, script_url_);
687 } 684 }
688 685
689 void ServiceWorkerContextClient::workerContextDestroyed() { 686 void ServiceWorkerContextClient::workerContextDestroyed() {
690 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); 687 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL);
691 688
692 // Check if mojo is enabled 689 // TODO(falken): The signals to the browser should be in the order:
693 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { 690 // (1) WorkerStopped (via OnStopped())
694 DCHECK(embedded_worker_client_); 691 // (2) ProviderDestroyed (via following
695 main_thread_task_runner_->PostTask( 692 // EmbeddedWorkerInstanceClientImpl::StopWorkerCompleted) But this ordering is
696 FROM_HERE, 693 // currently not guaranteed since the Mojo pipes are different.
697 base::Bind(&EmbeddedWorkerInstanceClientImpl::StopWorkerCompleted, 694 // https://crbug.com/676526
698 base::Passed(&embedded_worker_client_))); 695 (*instance_host_)->OnStopped();
699 return;
700 }
701 696
702 // Now we should be able to free the WebEmbeddedWorker container on the 697 DCHECK(embedded_worker_client_);
703 // main thread.
704 main_thread_task_runner_->PostTask( 698 main_thread_task_runner_->PostTask(
705 FROM_HERE, 699 FROM_HERE,
706 base::Bind(&CallWorkerContextDestroyedOnMainThread, 700 base::Bind(&EmbeddedWorkerInstanceClientImpl::StopWorkerCompleted,
707 embedded_worker_id_)); 701 base::Passed(&embedded_worker_client_)));
702 return;
708 } 703 }
709 704
710 void ServiceWorkerContextClient::countFeature(uint32_t feature) { 705 void ServiceWorkerContextClient::countFeature(uint32_t feature) {
711 Send(new EmbeddedWorkerHostMsg_CountFeature(service_worker_version_id_, 706 Send(new EmbeddedWorkerHostMsg_CountFeature(service_worker_version_id_,
712 feature)); 707 feature));
713 } 708 }
714 709
715 void ServiceWorkerContextClient::reportException( 710 void ServiceWorkerContextClient::reportException(
716 const blink::WebString& error_message, 711 const blink::WebString& error_message,
717 int line_number, 712 int line_number,
718 int column_number, 713 int column_number,
719 const blink::WebString& source_url) { 714 const blink::WebString& source_url) {
720 Send(new EmbeddedWorkerHostMsg_ReportException( 715 (*instance_host_)
721 embedded_worker_id_, error_message.utf16(), line_number, column_number, 716 ->OnReportException(error_message.utf16(), line_number, column_number,
722 blink::WebStringToGURL(source_url))); 717 blink::WebStringToGURL(source_url));
723 } 718 }
724 719
725 void ServiceWorkerContextClient::reportConsoleMessage( 720 void ServiceWorkerContextClient::reportConsoleMessage(
726 int source, 721 int source,
727 int level, 722 int level,
728 const blink::WebString& message, 723 const blink::WebString& message,
729 int line_number, 724 int line_number,
730 const blink::WebString& source_url) { 725 const blink::WebString& source_url) {
731 EmbeddedWorkerHostMsg_ReportConsoleMessage_Params params; 726 (*instance_host_)
732 params.source_identifier = source; 727 ->OnReportConsoleMessage(source, level, message.utf16(), line_number,
733 params.message_level = level; 728 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 } 729 }
741 730
742 void ServiceWorkerContextClient::sendDevToolsMessage( 731 void ServiceWorkerContextClient::sendDevToolsMessage(
743 int session_id, 732 int session_id,
744 int call_id, 733 int call_id,
745 const blink::WebString& message, 734 const blink::WebString& message,
746 const blink::WebString& state_cookie) { 735 const blink::WebString& state_cookie) {
747 // Return if this context has been stopped. 736 // Return if this context has been stopped.
748 if (!embedded_worker_client_) 737 if (!embedded_worker_client_)
749 return; 738 return;
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 1054
1066 void ServiceWorkerContextClient::Send(IPC::Message* message) { 1055 void ServiceWorkerContextClient::Send(IPC::Message* message) {
1067 sender_->Send(message); 1056 sender_->Send(message);
1068 } 1057 }
1069 1058
1070 void ServiceWorkerContextClient::SendWorkerStarted() { 1059 void ServiceWorkerContextClient::SendWorkerStarted() {
1071 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); 1060 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
1072 TRACE_EVENT_ASYNC_END0("ServiceWorker", 1061 TRACE_EVENT_ASYNC_END0("ServiceWorker",
1073 "ServiceWorkerContextClient::StartingWorkerContext", 1062 "ServiceWorkerContextClient::StartingWorkerContext",
1074 this); 1063 this);
1075 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); 1064 (*instance_host_)->OnStarted();
1076 } 1065 }
1077 1066
1078 void ServiceWorkerContextClient::SetRegistrationInServiceWorkerGlobalScope( 1067 void ServiceWorkerContextClient::SetRegistrationInServiceWorkerGlobalScope(
1079 const ServiceWorkerRegistrationObjectInfo& info, 1068 const ServiceWorkerRegistrationObjectInfo& info,
1080 const ServiceWorkerVersionAttributes& attrs) { 1069 const ServiceWorkerVersionAttributes& attrs) {
1081 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); 1070 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
1082 ServiceWorkerDispatcher* dispatcher = 1071 ServiceWorkerDispatcher* dispatcher =
1083 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( 1072 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
1084 sender_.get(), main_thread_task_runner_.get()); 1073 sender_.get(), main_thread_task_runner_.get());
1085 1074
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
1503 } 1492 }
1504 1493
1505 base::WeakPtr<ServiceWorkerContextClient> 1494 base::WeakPtr<ServiceWorkerContextClient>
1506 ServiceWorkerContextClient::GetWeakPtr() { 1495 ServiceWorkerContextClient::GetWeakPtr() {
1507 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); 1496 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
1508 DCHECK(context_); 1497 DCHECK(context_);
1509 return context_->weak_factory.GetWeakPtr(); 1498 return context_->weak_factory.GetWeakPtr();
1510 } 1499 }
1511 1500
1512 } // namespace content 1501 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698