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

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

Issue 2779763004: Create ServiceWorkerProviderHost before starting worker (Closed)
Patch Set: Rebased 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) {
197 CHECK(render_process_id == ChildProcessHost::kInvalidUniqueID);
falken 2017/06/01 08:12:34 CHECK_EQ
shimazu 2017/06/06 04:16:56 Done.
184 // Actual thread id is set when the service worker context gets started. 198 // Actual thread id is set when the service worker context gets started.
falken 2017/06/01 08:12:34 this comment be also mention render proc id and be
shimazu 2017/06/06 04:16:56 Done.
185 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; 199 render_thread_id_ = kInvalidEmbeddedWorkerThreadId;
200 } else {
201 // PlzNavigate
202 CHECK(render_process_id != ChildProcessHost::kInvalidUniqueID ||
203 IsBrowserSideNavigationEnabled());
186 } 204 }
205
187 context_->RegisterProviderHostByClientID(client_uuid_, this); 206 context_->RegisterProviderHostByClientID(client_uuid_, this);
188 207
189 // PlzNavigate 208 // |client_| and |binding_| will be bound on CompleteNavigationInitialized
190 // |provider_| and |binding_| will be bound on CompleteNavigationInitialized. 209 // (PlzNavigate) or on CompleteStartWorkerPreparation (providers for
191 if (IsBrowserSideNavigationEnabled()) { 210 // controller).
192 DCHECK(!info.client_ptr_info.is_valid() && !info.host_request.is_pending()); 211 if (!info_.client_ptr_info.is_valid() && !info_.host_request.is_pending()) {
212 DCHECK(IsBrowserSideNavigationEnabled() ||
213 info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER);
193 return; 214 return;
194 } 215 }
195 216
196 provider_.Bind(std::move(info_.client_ptr_info)); 217 provider_.Bind(std::move(info_.client_ptr_info));
197 binding_.Bind(std::move(info_.host_request)); 218 binding_.Bind(std::move(info_.host_request));
198 binding_.set_connection_error_handler(base::Bind( 219 binding_.set_connection_error_handler(base::Bind(
199 &RemoveProviderHost, context_, render_process_id, info_.provider_id)); 220 &RemoveProviderHost, context_, render_process_id, info_.provider_id));
200 } 221 }
201 222
202 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { 223 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 DCHECK(IsProviderForClient()); 340 DCHECK(IsProviderForClient());
320 Send(new ServiceWorkerMsg_SetControllerServiceWorker( 341 Send(new ServiceWorkerMsg_SetControllerServiceWorker(
321 render_thread_id_, provider_id(), GetOrCreateServiceWorkerHandle(version), 342 render_thread_id_, provider_id(), GetOrCreateServiceWorkerHandle(version),
322 notify_controllerchange, 343 notify_controllerchange,
323 version ? version->used_features() : std::set<uint32_t>())); 344 version ? version->used_features() : std::set<uint32_t>()));
324 } 345 }
325 346
326 void ServiceWorkerProviderHost::SetHostedVersion( 347 void ServiceWorkerProviderHost::SetHostedVersion(
327 ServiceWorkerVersion* version) { 348 ServiceWorkerVersion* version) {
328 DCHECK(!IsProviderForClient()); 349 DCHECK(!IsProviderForClient());
329 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, version->running_status()); 350 DCHECK_EQ(EmbeddedWorkerStatus::STOPPED, version->running_status());
330 DCHECK_EQ(render_process_id_, version->embedded_worker()->process_id());
331 running_hosted_version_ = version; 351 running_hosted_version_ = version;
332 } 352 }
333 353
334 bool ServiceWorkerProviderHost::IsProviderForClient() const { 354 bool ServiceWorkerProviderHost::IsProviderForClient() const {
335 switch (info_.type) { 355 switch (info_.type) {
336 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: 356 case SERVICE_WORKER_PROVIDER_FOR_WINDOW:
337 case SERVICE_WORKER_PROVIDER_FOR_WORKER: 357 case SERVICE_WORKER_PROVIDER_FOR_WORKER:
338 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: 358 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER:
339 return true; 359 return true;
340 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: 360 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER:
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 DCHECK(!binding_.is_bound()); 656 DCHECK(!binding_.is_bound());
637 provider_.Bind(std::move(info.client_ptr_info)); 657 provider_.Bind(std::move(info.client_ptr_info));
638 binding_.Bind(std::move(info.host_request)); 658 binding_.Bind(std::move(info.host_request));
639 binding_.set_connection_error_handler( 659 binding_.set_connection_error_handler(
640 base::Bind(&RemoveProviderHost, context_, process_id, provider_id())); 660 base::Bind(&RemoveProviderHost, context_, process_id, provider_id()));
641 info_.route_id = info.route_id; 661 info_.route_id = info.route_id;
642 662
643 FinalizeInitialization(process_id, dispatcher_host); 663 FinalizeInitialization(process_id, dispatcher_host);
644 } 664 }
645 665
666 void ServiceWorkerProviderHost::CompleteStartWorkerPreparation(
667 int process_id,
668 mojom::ServiceWorkerProviderClientInfoPtr* provider_client_info) {
669 DCHECK(context_);
670
671 DCHECK_EQ(kInvalidEmbeddedWorkerThreadId, render_thread_id_);
672 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_);
673 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, provider_type());
674 DCHECK(running_hosted_version_);
675
676 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id);
677
678 ServiceWorkerDispatcherHost* dispatcher_host =
679 context_->GetDispatcherHost(process_id);
680 DCHECK(dispatcher_host);
681 render_process_id_ = process_id;
682 dispatcher_host_ = dispatcher_host;
683
684 // Retrieve the registration associated with |version|. The registration
685 // must be alive because the version keeps it during starting worker.
686 ServiceWorkerRegistration* registration = context_->GetLiveRegistration(
687 running_hosted_version()->registration_id());
688 DCHECK(registration);
689 ServiceWorkerRegistrationObjectInfo info;
690 ServiceWorkerVersionAttributes attrs;
691 dispatcher_host->GetRegistrationObjectInfoAndVersionAttributes(
692 AsWeakPtr(), registration, &info, &attrs);
693
694 // Initialize provider_client_info.
695 (*provider_client_info)->provider_id = provider_id();
696 (*provider_client_info)->type = provider_type();
697 (*provider_client_info)->attributes = std::move(attrs);
698 (*provider_client_info)->registration = std::move(info);
699 (*provider_client_info)->is_parent_frame_secure = is_parent_frame_secure();
700 (*provider_client_info)->client_request = mojo::MakeRequest(&provider_);
701 binding_.Bind(mojo::MakeRequest(&(*provider_client_info)->host_ptr_info));
702 binding_.set_connection_error_handler(
703 base::Bind(&RemoveProviderHost, context_, process_id, provider_id()));
704
705 // Set the document URL to the script url in order to allow
706 // register/unregister/getRegistration on ServiceWorkerGlobalScope.
707 SetDocumentUrl(running_hosted_version()->script_url());
708 }
709
646 void ServiceWorkerProviderHost::SendUpdateFoundMessage( 710 void ServiceWorkerProviderHost::SendUpdateFoundMessage(
647 int registration_handle_id) { 711 int registration_handle_id) {
648 if (!dispatcher_host_) 712 if (!dispatcher_host_)
649 return; // Could be nullptr in some tests. 713 return; // Could be nullptr in some tests.
650 714
651 if (!IsReadyToSendMessages()) { 715 if (!IsReadyToSendMessages()) {
652 queued_events_.push_back( 716 queued_events_.push_back(
653 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, 717 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage,
654 AsWeakPtr(), registration_handle_id)); 718 AsWeakPtr(), registration_handle_id));
655 return; 719 return;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 render_thread_id_, provider_id(), 890 render_thread_id_, provider_id(),
827 GetOrCreateServiceWorkerHandle( 891 GetOrCreateServiceWorkerHandle(
828 associated_registration_->active_version()), 892 associated_registration_->active_version()),
829 false /* shouldNotifyControllerChange */, 893 false /* shouldNotifyControllerChange */,
830 associated_registration_->active_version()->used_features())); 894 associated_registration_->active_version()->used_features()));
831 } 895 }
832 } 896 }
833 } 897 }
834 898
835 } // namespace content 899 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698