OLD | NEW |
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 Loading... |
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 when service worker's |
40 // Next ServiceWorkerProviderHost ID for navigations, starts at -2 and keeps | 40 // context is 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_browser_provided_provider_id = -2; |
| 44 return g_next_browser_provided_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 Loading... |
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::PreCreateForController( |
| 132 base::WeakPtr<ServiceWorkerContextCore> context) { |
| 133 auto host = base::WrapUnique(new ServiceWorkerProviderHost( |
| 134 ChildProcessHost::kInvalidUniqueID, |
| 135 ServiceWorkerProviderHostInfo(NextBrowserProvidedProviderId(), |
| 136 MSG_ROUTING_NONE, |
| 137 SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, |
| 138 true /* is_parent_frame_secure */), |
| 139 std::move(context), nullptr)); |
| 140 return host; |
| 141 } |
| 142 |
| 143 // static |
130 std::unique_ptr<ServiceWorkerProviderHost> ServiceWorkerProviderHost::Create( | 144 std::unique_ptr<ServiceWorkerProviderHost> ServiceWorkerProviderHost::Create( |
131 int process_id, | 145 int process_id, |
132 ServiceWorkerProviderHostInfo info, | 146 ServiceWorkerProviderHostInfo info, |
133 base::WeakPtr<ServiceWorkerContextCore> context, | 147 base::WeakPtr<ServiceWorkerContextCore> context, |
134 ServiceWorkerDispatcherHost* dispatcher_host) { | 148 ServiceWorkerDispatcherHost* dispatcher_host) { |
135 return base::WrapUnique(new ServiceWorkerProviderHost( | 149 return base::WrapUnique(new ServiceWorkerProviderHost( |
136 process_id, std::move(info), context, dispatcher_host)); | 150 process_id, std::move(info), context, dispatcher_host)); |
137 } | 151 } |
138 | 152 |
139 void ServiceWorkerProviderHost::BindWorkerFetchContext( | 153 void ServiceWorkerProviderHost::BindWorkerFetchContext( |
(...skipping 30 matching lines...) Expand all Loading... |
170 create_time_(base::TimeTicks::Now()), | 184 create_time_(base::TimeTicks::Now()), |
171 render_process_id_(render_process_id), | 185 render_process_id_(render_process_id), |
172 render_thread_id_(kDocumentMainThreadId), | 186 render_thread_id_(kDocumentMainThreadId), |
173 info_(std::move(info)), | 187 info_(std::move(info)), |
174 context_(context), | 188 context_(context), |
175 dispatcher_host_(dispatcher_host), | 189 dispatcher_host_(dispatcher_host), |
176 allow_association_(true), | 190 allow_association_(true), |
177 binding_(this) { | 191 binding_(this) { |
178 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type); | 192 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type); |
179 | 193 |
180 // PlzNavigate | |
181 CHECK(render_process_id != ChildProcessHost::kInvalidUniqueID || | |
182 IsBrowserSideNavigationEnabled()); | |
183 | 194 |
184 if (info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { | 195 if (info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { |
185 // Actual thread id is set when the service worker context gets started. | 196 // Actual |render_process_id| will be set after choosing a process for the |
| 197 // controller, and |render_thread id| will be set when the service worker |
| 198 // context gets started. |
| 199 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id); |
186 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; | 200 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; |
| 201 } else { |
| 202 // PlzNavigate |
| 203 DCHECK(render_process_id != ChildProcessHost::kInvalidUniqueID || |
| 204 IsBrowserSideNavigationEnabled()); |
187 } | 205 } |
| 206 |
188 context_->RegisterProviderHostByClientID(client_uuid_, this); | 207 context_->RegisterProviderHostByClientID(client_uuid_, this); |
189 | 208 |
190 // PlzNavigate | 209 // |client_| and |binding_| will be bound on CompleteNavigationInitialized |
191 // |provider_| and |binding_| will be bound on CompleteNavigationInitialized. | 210 // (PlzNavigate) or on CompleteStartWorkerPreparation (providers for |
192 if (IsBrowserSideNavigationEnabled()) { | 211 // controller). |
193 DCHECK(!info.client_ptr_info.is_valid() && !info.host_request.is_pending()); | 212 if (!info_.client_ptr_info.is_valid() && !info_.host_request.is_pending()) { |
| 213 DCHECK(IsBrowserSideNavigationEnabled() || |
| 214 info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER); |
194 return; | 215 return; |
195 } | 216 } |
196 | 217 |
197 provider_.Bind(std::move(info_.client_ptr_info)); | 218 provider_.Bind(std::move(info_.client_ptr_info)); |
198 binding_.Bind(std::move(info_.host_request)); | 219 binding_.Bind(std::move(info_.host_request)); |
199 binding_.set_connection_error_handler(base::Bind( | 220 binding_.set_connection_error_handler(base::Bind( |
200 &RemoveProviderHost, context_, render_process_id, info_.provider_id)); | 221 &RemoveProviderHost, context_, render_process_id, info_.provider_id)); |
201 } | 222 } |
202 | 223 |
203 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { | 224 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 return; // Could be NULL in some tests. | 338 return; // Could be NULL in some tests. |
318 | 339 |
319 // SetController message should be sent only for controllees. | 340 // SetController message should be sent only for controllees. |
320 DCHECK(IsProviderForClient()); | 341 DCHECK(IsProviderForClient()); |
321 Send(new ServiceWorkerMsg_SetControllerServiceWorker( | 342 Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
322 render_thread_id_, provider_id(), GetOrCreateServiceWorkerHandle(version), | 343 render_thread_id_, provider_id(), GetOrCreateServiceWorkerHandle(version), |
323 notify_controllerchange, | 344 notify_controllerchange, |
324 version ? version->used_features() : std::set<uint32_t>())); | 345 version ? version->used_features() : std::set<uint32_t>())); |
325 } | 346 } |
326 | 347 |
327 void ServiceWorkerProviderHost::SetHostedVersion( | |
328 ServiceWorkerVersion* version) { | |
329 DCHECK(!IsProviderForClient()); | |
330 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, version->running_status()); | |
331 DCHECK_EQ(render_process_id_, version->embedded_worker()->process_id()); | |
332 running_hosted_version_ = version; | |
333 } | |
334 | |
335 bool ServiceWorkerProviderHost::IsProviderForClient() const { | 348 bool ServiceWorkerProviderHost::IsProviderForClient() const { |
336 switch (info_.type) { | 349 switch (info_.type) { |
337 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | 350 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: |
338 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | 351 case SERVICE_WORKER_PROVIDER_FOR_WORKER: |
339 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | 352 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: |
340 return true; | 353 return true; |
341 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: | 354 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: |
342 return false; | 355 return false; |
343 case SERVICE_WORKER_PROVIDER_UNKNOWN: | 356 case SERVICE_WORKER_PROVIDER_UNKNOWN: |
344 NOTREACHED() << info_.type; | 357 NOTREACHED() << info_.type; |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 // Increase the references because the process which this provider host will | 665 // Increase the references because the process which this provider host will |
653 // host has been decided. | 666 // host has been decided. |
654 for (const GURL& pattern : associated_patterns_) | 667 for (const GURL& pattern : associated_patterns_) |
655 IncreaseProcessReference(pattern); | 668 IncreaseProcessReference(pattern); |
656 for (auto& key_registration : matching_registrations_) | 669 for (auto& key_registration : matching_registrations_) |
657 IncreaseProcessReference(key_registration.second->pattern()); | 670 IncreaseProcessReference(key_registration.second->pattern()); |
658 | 671 |
659 NotifyControllerToAssociatedProvider(); | 672 NotifyControllerToAssociatedProvider(); |
660 } | 673 } |
661 | 674 |
| 675 mojom::ServiceWorkerProviderInfoForStartWorkerPtr |
| 676 ServiceWorkerProviderHost::CompleteStartWorkerPreparation( |
| 677 int process_id, |
| 678 scoped_refptr<ServiceWorkerVersion> hosted_version) { |
| 679 DCHECK(context_); |
| 680 DCHECK_EQ(kInvalidEmbeddedWorkerThreadId, render_thread_id_); |
| 681 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
| 682 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, provider_type()); |
| 683 DCHECK(!running_hosted_version_); |
| 684 |
| 685 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id); |
| 686 |
| 687 running_hosted_version_ = std::move(hosted_version); |
| 688 |
| 689 ServiceWorkerDispatcherHost* dispatcher_host = |
| 690 context_->GetDispatcherHost(process_id); |
| 691 DCHECK(dispatcher_host); |
| 692 render_process_id_ = process_id; |
| 693 dispatcher_host_ = dispatcher_host; |
| 694 |
| 695 // Retrieve the registration associated with |version|. The registration |
| 696 // must be alive because the version keeps it during starting worker. |
| 697 ServiceWorkerRegistration* registration = context_->GetLiveRegistration( |
| 698 running_hosted_version()->registration_id()); |
| 699 DCHECK(registration); |
| 700 ServiceWorkerRegistrationObjectInfo info; |
| 701 ServiceWorkerVersionAttributes attrs; |
| 702 dispatcher_host->GetRegistrationObjectInfoAndVersionAttributes( |
| 703 AsWeakPtr(), registration, &info, &attrs); |
| 704 |
| 705 // Initialize provider_info. |
| 706 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info = |
| 707 mojom::ServiceWorkerProviderInfoForStartWorker::New(); |
| 708 provider_info->provider_id = provider_id(); |
| 709 provider_info->attributes = std::move(attrs); |
| 710 provider_info->registration = std::move(info); |
| 711 provider_info->client_request = mojo::MakeRequest(&provider_); |
| 712 binding_.Bind(mojo::MakeRequest(&provider_info->host_ptr_info)); |
| 713 binding_.set_connection_error_handler( |
| 714 base::Bind(&RemoveProviderHost, context_, process_id, provider_id())); |
| 715 |
| 716 // Set the document URL to the script url in order to allow |
| 717 // register/unregister/getRegistration on ServiceWorkerGlobalScope. |
| 718 SetDocumentUrl(running_hosted_version()->script_url()); |
| 719 |
| 720 return provider_info; |
| 721 } |
| 722 |
662 void ServiceWorkerProviderHost::SendUpdateFoundMessage( | 723 void ServiceWorkerProviderHost::SendUpdateFoundMessage( |
663 int registration_handle_id) { | 724 int registration_handle_id) { |
664 if (!dispatcher_host_) | 725 if (!dispatcher_host_) |
665 return; // Could be nullptr in some tests. | 726 return; // Could be nullptr in some tests. |
666 | 727 |
667 if (!IsReadyToSendMessages()) { | 728 if (!IsReadyToSendMessages()) { |
668 queued_events_.push_back( | 729 queued_events_.push_back( |
669 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, | 730 base::Bind(&ServiceWorkerProviderHost::SendUpdateFoundMessage, |
670 AsWeakPtr(), registration_handle_id)); | 731 AsWeakPtr(), registration_handle_id)); |
671 return; | 732 return; |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 render_thread_id_, provider_id(), | 892 render_thread_id_, provider_id(), |
832 GetOrCreateServiceWorkerHandle( | 893 GetOrCreateServiceWorkerHandle( |
833 associated_registration_->active_version()), | 894 associated_registration_->active_version()), |
834 false /* shouldNotifyControllerChange */, | 895 false /* shouldNotifyControllerChange */, |
835 associated_registration_->active_version()->used_features())); | 896 associated_registration_->active_version()->used_features())); |
836 } | 897 } |
837 } | 898 } |
838 } | 899 } |
839 | 900 |
840 } // namespace content | 901 } // namespace content |
OLD | NEW |