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

Side by Side Diff: content/browser/service_worker/service_worker_provider_host.cc

Issue 2779763004: Create ServiceWorkerProviderHost before starting worker (Closed)
Patch Set: Fix ForeignFetchRequestHandlerTest.InitializeHandler_TimeoutBehaviorForServiceWorker Created 3 years, 6 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/browser/service_worker/service_worker_provider_host.h" 5 #include "content/browser/service_worker/service_worker_provider_host.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/feature_list.h" 9 #include "base/feature_list.h"
10 #include "base/guid.h" 10 #include "base/guid.h"
(...skipping 18 matching lines...) Expand all
29 #include "content/public/common/content_client.h" 29 #include "content/public/common/content_client.h"
30 #include "content/public/common/content_features.h" 30 #include "content/public/common/content_features.h"
31 #include "content/public/common/origin_util.h" 31 #include "content/public/common/origin_util.h"
32 #include "mojo/public/cpp/bindings/strong_associated_binding.h" 32 #include "mojo/public/cpp/bindings/strong_associated_binding.h"
33 #include "net/base/url_util.h" 33 #include "net/base/url_util.h"
34 34
35 namespace content { 35 namespace content {
36 36
37 namespace { 37 namespace {
38 38
39 // PlzNavigate 39 // Provider host for navigation with PlzNavigate or service worker's context is
40 // Next ServiceWorkerProviderHost ID for navigations, starts at -2 and keeps 40 // created on the browser side. This function provides the next
41 // going down. 41 // ServiceWorkerProviderHost ID for them, starts at -2 and keeps going down.
42 int g_next_navigation_provider_id = -2; 42 int NextBrowserProvidedProviderId() {
43 static int g_next_navigation_provider_id = -2;
44 return g_next_navigation_provider_id--;
45 }
43 46
44 // A request handler derivative used to handle navigation requests when 47 // A request handler derivative used to handle navigation requests when
45 // skip_service_worker flag is set. It tracks the document URL and sets the url 48 // skip_service_worker flag is set. It tracks the document URL and sets the url
46 // to the provider host. 49 // to the provider host.
47 class ServiceWorkerURLTrackingRequestHandler 50 class ServiceWorkerURLTrackingRequestHandler
48 : public ServiceWorkerRequestHandler { 51 : public ServiceWorkerRequestHandler {
49 public: 52 public:
50 ServiceWorkerURLTrackingRequestHandler( 53 ServiceWorkerURLTrackingRequestHandler(
51 base::WeakPtr<ServiceWorkerContextCore> context, 54 base::WeakPtr<ServiceWorkerContextCore> context,
52 base::WeakPtr<ServiceWorkerProviderHost> provider_host, 55 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
53 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, 56 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
54 ResourceType resource_type) 57 ResourceType resource_type)
55 : ServiceWorkerRequestHandler(context, 58 : ServiceWorkerRequestHandler(context,
56 provider_host, 59 provider_host,
57 blob_storage_context, 60 blob_storage_context,
58 resource_type) {} 61 resource_type) {}
59 ~ServiceWorkerURLTrackingRequestHandler() override {} 62 ~ServiceWorkerURLTrackingRequestHandler() override {}
60 63
61 // Called via custom URLRequestJobFactory. 64 // Called via custom URLRequestJobFactory.
62 net::URLRequestJob* MaybeCreateJob( 65 net::URLRequestJob* MaybeCreateJob(net::URLRequest* request,
63 net::URLRequest* request, 66 net::NetworkDelegate*,
64 net::NetworkDelegate* /* network_delegate */, 67 ResourceContext*) override {
65 ResourceContext* /* resource_context */) override {
66 // |provider_host_| may have been deleted when the request is resumed. 68 // |provider_host_| may have been deleted when the request is resumed.
67 if (!provider_host_) 69 if (!provider_host_)
68 return nullptr; 70 return nullptr;
69 const GURL stripped_url = net::SimplifyUrlForRequest(request->url()); 71 const GURL stripped_url = net::SimplifyUrlForRequest(request->url());
70 provider_host_->SetDocumentUrl(stripped_url); 72 provider_host_->SetDocumentUrl(stripped_url);
71 provider_host_->SetTopmostFrameUrl(request->first_party_for_cookies()); 73 provider_host_->SetTopmostFrameUrl(request->first_party_for_cookies());
72 return nullptr; 74 return nullptr;
73 } 75 }
74 76
75 private: 77 private:
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { 109 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() {
108 } 110 }
109 111
110 // static 112 // static
111 std::unique_ptr<ServiceWorkerProviderHost> 113 std::unique_ptr<ServiceWorkerProviderHost>
112 ServiceWorkerProviderHost::PreCreateNavigationHost( 114 ServiceWorkerProviderHost::PreCreateNavigationHost(
113 base::WeakPtr<ServiceWorkerContextCore> context, 115 base::WeakPtr<ServiceWorkerContextCore> context,
114 bool are_ancestors_secure, 116 bool are_ancestors_secure,
115 const WebContentsGetter& web_contents_getter) { 117 const WebContentsGetter& web_contents_getter) {
116 CHECK(IsBrowserSideNavigationEnabled()); 118 CHECK(IsBrowserSideNavigationEnabled());
117 // Generate a new browser-assigned id for the host.
118 int provider_id = g_next_navigation_provider_id--;
119 auto host = base::WrapUnique(new ServiceWorkerProviderHost( 119 auto host = base::WrapUnique(new ServiceWorkerProviderHost(
120 ChildProcessHost::kInvalidUniqueID, 120 ChildProcessHost::kInvalidUniqueID,
121 ServiceWorkerProviderHostInfo(provider_id, MSG_ROUTING_NONE, 121 ServiceWorkerProviderHostInfo(
122 SERVICE_WORKER_PROVIDER_FOR_WINDOW, 122 NextBrowserProvidedProviderId(), MSG_ROUTING_NONE,
123 are_ancestors_secure), 123 SERVICE_WORKER_PROVIDER_FOR_WINDOW, are_ancestors_secure),
124 context, nullptr)); 124 context, nullptr));
125 host->web_contents_getter_ = web_contents_getter; 125 host->web_contents_getter_ = web_contents_getter;
126 return host; 126 return host;
127 } 127 }
128 128
129 // static 129 // static
130 std::unique_ptr<ServiceWorkerProviderHost>
131 ServiceWorkerProviderHost::PreCreateForWorkerContext(
132 ServiceWorkerVersion* version,
133 base::WeakPtr<ServiceWorkerContextCore> context) {
134 std::unique_ptr<ServiceWorkerProviderHost> host =
135 base::WrapUnique(new ServiceWorkerProviderHost(
136 ChildProcessHost::kInvalidUniqueID,
137 ServiceWorkerProviderHostInfo(
138 NextBrowserProvidedProviderId(), MSG_ROUTING_NONE,
139 SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, true),
140 context, nullptr));
141 host->SetHostedVersion(version);
142 return host;
143 }
144
145 // static
130 std::unique_ptr<ServiceWorkerProviderHost> ServiceWorkerProviderHost::Create( 146 std::unique_ptr<ServiceWorkerProviderHost> ServiceWorkerProviderHost::Create(
131 int process_id, 147 int process_id,
132 ServiceWorkerProviderHostInfo info, 148 ServiceWorkerProviderHostInfo info,
133 base::WeakPtr<ServiceWorkerContextCore> context, 149 base::WeakPtr<ServiceWorkerContextCore> context,
134 ServiceWorkerDispatcherHost* dispatcher_host) { 150 ServiceWorkerDispatcherHost* dispatcher_host) {
135 return base::WrapUnique(new ServiceWorkerProviderHost( 151 return base::WrapUnique(new ServiceWorkerProviderHost(
136 process_id, std::move(info), context, dispatcher_host)); 152 process_id, std::move(info), context, dispatcher_host));
137 } 153 }
138 154
139 void ServiceWorkerProviderHost::BindWorkerFetchContext( 155 void ServiceWorkerProviderHost::BindWorkerFetchContext(
(...skipping 29 matching lines...) Expand all
169 : client_uuid_(base::GenerateGUID()), 185 : client_uuid_(base::GenerateGUID()),
170 render_process_id_(render_process_id), 186 render_process_id_(render_process_id),
171 render_thread_id_(kDocumentMainThreadId), 187 render_thread_id_(kDocumentMainThreadId),
172 info_(std::move(info)), 188 info_(std::move(info)),
173 context_(context), 189 context_(context),
174 dispatcher_host_(dispatcher_host), 190 dispatcher_host_(dispatcher_host),
175 allow_association_(true), 191 allow_association_(true),
176 binding_(this) { 192 binding_(this) {
177 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type); 193 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type);
178 194
179 // PlzNavigate
180 CHECK(render_process_id != ChildProcessHost::kInvalidUniqueID ||
181 IsBrowserSideNavigationEnabled());
182 195
183 if (info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { 196 if (info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) {
184 // Actual thread id is set when the service worker context gets started. 197 // Actual |render_process_id| will be set after choosing a process for the
198 // controller, and |render_thread id| will be set when the service worker
199 // context gets started.
200 CHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id);
185 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; 201 render_thread_id_ = kInvalidEmbeddedWorkerThreadId;
202 } else {
203 // PlzNavigate
204 CHECK(render_process_id != ChildProcessHost::kInvalidUniqueID ||
205 IsBrowserSideNavigationEnabled());
186 } 206 }
207
187 context_->RegisterProviderHostByClientID(client_uuid_, this); 208 context_->RegisterProviderHostByClientID(client_uuid_, this);
188 209
189 // PlzNavigate 210 // |client_| and |binding_| will be bound on CompleteNavigationInitialized
190 // |provider_| and |binding_| will be bound on CompleteNavigationInitialized. 211 // (PlzNavigate) or on CompleteStartWorkerPreparation (providers for
191 if (IsBrowserSideNavigationEnabled()) { 212 // controller).
192 DCHECK(!info.client_ptr_info.is_valid() && !info.host_request.is_pending()); 213 if (!info_.client_ptr_info.is_valid() && !info_.host_request.is_pending()) {
214 DCHECK(IsBrowserSideNavigationEnabled() ||
215 info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER);
193 return; 216 return;
194 } 217 }
195 218
196 provider_.Bind(std::move(info_.client_ptr_info)); 219 provider_.Bind(std::move(info_.client_ptr_info));
197 binding_.Bind(std::move(info_.host_request)); 220 binding_.Bind(std::move(info_.host_request));
198 binding_.set_connection_error_handler(base::Bind( 221 binding_.set_connection_error_handler(base::Bind(
199 &RemoveProviderHost, context_, render_process_id, info_.provider_id)); 222 &RemoveProviderHost, context_, render_process_id, info_.provider_id));
200 } 223 }
201 224
202 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { 225 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 DCHECK(IsProviderForClient()); 342 DCHECK(IsProviderForClient());
320 Send(new ServiceWorkerMsg_SetControllerServiceWorker( 343 Send(new ServiceWorkerMsg_SetControllerServiceWorker(
321 render_thread_id_, provider_id(), GetOrCreateServiceWorkerHandle(version), 344 render_thread_id_, provider_id(), GetOrCreateServiceWorkerHandle(version),
322 notify_controllerchange, 345 notify_controllerchange,
323 version ? version->used_features() : std::set<uint32_t>())); 346 version ? version->used_features() : std::set<uint32_t>()));
324 } 347 }
325 348
326 void ServiceWorkerProviderHost::SetHostedVersion( 349 void ServiceWorkerProviderHost::SetHostedVersion(
327 ServiceWorkerVersion* version) { 350 ServiceWorkerVersion* version) {
328 DCHECK(!IsProviderForClient()); 351 DCHECK(!IsProviderForClient());
329 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, version->running_status()); 352 DCHECK_EQ(EmbeddedWorkerStatus::STOPPED, version->running_status());
330 DCHECK_EQ(render_process_id_, version->embedded_worker()->process_id());
331 running_hosted_version_ = version; 353 running_hosted_version_ = version;
332 } 354 }
333 355
334 bool ServiceWorkerProviderHost::IsProviderForClient() const { 356 bool ServiceWorkerProviderHost::IsProviderForClient() const {
335 switch (info_.type) { 357 switch (info_.type) {
336 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: 358 case SERVICE_WORKER_PROVIDER_FOR_WINDOW:
337 case SERVICE_WORKER_PROVIDER_FOR_WORKER: 359 case SERVICE_WORKER_PROVIDER_FOR_WORKER:
338 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: 360 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER:
339 return true; 361 return true;
340 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: 362 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER:
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 DCHECK(!binding_.is_bound()); 658 DCHECK(!binding_.is_bound());
637 provider_.Bind(std::move(info.client_ptr_info)); 659 provider_.Bind(std::move(info.client_ptr_info));
638 binding_.Bind(std::move(info.host_request)); 660 binding_.Bind(std::move(info.host_request));
639 binding_.set_connection_error_handler( 661 binding_.set_connection_error_handler(
640 base::Bind(&RemoveProviderHost, context_, process_id, provider_id())); 662 base::Bind(&RemoveProviderHost, context_, process_id, provider_id()));
641 info_.route_id = info.route_id; 663 info_.route_id = info.route_id;
642 664
643 FinalizeInitialization(process_id, dispatcher_host); 665 FinalizeInitialization(process_id, dispatcher_host);
644 } 666 }
645 667
668 void ServiceWorkerProviderHost::CompleteStartWorkerPreparation(
669 int process_id,
670 mojom::ServiceWorkerProviderClientInfoPtr* provider_client_info) {
671 DCHECK(context_);
672
673 DCHECK_EQ(kInvalidEmbeddedWorkerThreadId, render_thread_id_);
674 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_);
675 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, provider_type());
676 DCHECK(running_hosted_version_);
677
678 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id);
679
680 ServiceWorkerDispatcherHost* dispatcher_host =
681 context_->GetDispatcherHost(process_id);
682 DCHECK(dispatcher_host);
683 render_process_id_ = process_id;
684 dispatcher_host_ = dispatcher_host;
685
686 // Retrieve the registration associated with |version|. The registration
687 // must be alive because the version keeps it during starting worker.
688 ServiceWorkerRegistration* registration = context_->GetLiveRegistration(
689 running_hosted_version()->registration_id());
690 DCHECK(registration);
691 ServiceWorkerRegistrationObjectInfo info;
692 ServiceWorkerVersionAttributes attrs;
693 dispatcher_host->GetRegistrationObjectInfoAndVersionAttributes(
694 AsWeakPtr(), registration, &info, &attrs);
695
696 // Initialize provider_client_info.
697 (*provider_client_info)->provider_id = provider_id();
698 (*provider_client_info)->type = provider_type();
699 (*provider_client_info)->attributes = std::move(attrs);
700 (*provider_client_info)->registration = std::move(info);
701 (*provider_client_info)->is_parent_frame_secure = is_parent_frame_secure();
702 (*provider_client_info)->client_request = mojo::MakeRequest(&provider_);
703 binding_.Bind(mojo::MakeRequest(&(*provider_client_info)->host_ptr_info));
704 binding_.set_connection_error_handler(
705 base::Bind(&RemoveProviderHost, context_, process_id, provider_id()));
706
707 // Set the document URL to the script url in order to allow
708 // register/unregister/getRegistration on ServiceWorkerGlobalScope.
709 SetDocumentUrl(running_hosted_version()->script_url());
710 }
711
646 void ServiceWorkerProviderHost::SendUpdateFoundMessage( 712 void ServiceWorkerProviderHost::SendUpdateFoundMessage(
647 int registration_handle_id) { 713 int registration_handle_id) {
648 if (!dispatcher_host_) 714 if (!dispatcher_host_)
649 return; // Could be nullptr in some tests. 715 return; // Could be nullptr in some tests.
650 716
651 if (!IsReadyToSendMessages()) { 717 if (!IsReadyToSendMessages()) {
652 queued_events_.push_back( 718 queued_events_.push_back(
653 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, 719 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage,
654 AsWeakPtr(), registration_handle_id)); 720 AsWeakPtr(), registration_handle_id));
655 return; 721 return;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 render_thread_id_, provider_id(), 892 render_thread_id_, provider_id(),
827 GetOrCreateServiceWorkerHandle( 893 GetOrCreateServiceWorkerHandle(
828 associated_registration_->active_version()), 894 associated_registration_->active_version()),
829 false /* shouldNotifyControllerChange */, 895 false /* shouldNotifyControllerChange */,
830 associated_registration_->active_version()->used_features())); 896 associated_registration_->active_version()->used_features()));
831 } 897 }
832 } 898 }
833 } 899 }
834 900
835 } // namespace content 901 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698