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" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "content/browser/service_worker/embedded_worker_status.h" | 14 #include "content/browser/service_worker/embedded_worker_status.h" |
15 #include "content/browser/service_worker/service_worker_context_core.h" | 15 #include "content/browser/service_worker/service_worker_context_core.h" |
16 #include "content/browser/service_worker/service_worker_context_request_handler.
h" | 16 #include "content/browser/service_worker/service_worker_context_request_handler.
h" |
17 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" | 17 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" |
18 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 18 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
19 #include "content/browser/service_worker/service_worker_handle.h" | 19 #include "content/browser/service_worker/service_worker_handle.h" |
20 #include "content/browser/service_worker/service_worker_registration_handle.h" | 20 #include "content/browser/service_worker/service_worker_registration_handle.h" |
21 #include "content/browser/service_worker/service_worker_version.h" | 21 #include "content/browser/service_worker/service_worker_version.h" |
22 #include "content/browser/url_loader_factory_getter.h" | |
23 #include "content/common/resource_request_body_impl.h" | 22 #include "content/common/resource_request_body_impl.h" |
24 #include "content/common/service_worker/service_worker_messages.h" | 23 #include "content/common/service_worker/service_worker_messages.h" |
25 #include "content/common/service_worker/service_worker_types.h" | 24 #include "content/common/service_worker/service_worker_types.h" |
26 #include "content/common/service_worker/service_worker_utils.h" | 25 #include "content/common/service_worker/service_worker_utils.h" |
27 #include "content/public/browser/content_browser_client.h" | 26 #include "content/public/browser/content_browser_client.h" |
28 #include "content/public/common/browser_side_navigation_policy.h" | 27 #include "content/public/common/browser_side_navigation_policy.h" |
29 #include "content/public/common/child_process_host.h" | 28 #include "content/public/common/child_process_host.h" |
30 #include "content/public/common/content_client.h" | 29 #include "content/public/common/content_client.h" |
31 #include "content/public/common/content_features.h" | 30 #include "content/public/common/content_features.h" |
32 #include "content/public/common/origin_util.h" | 31 #include "content/public/common/origin_util.h" |
33 #include "mojo/public/cpp/bindings/strong_associated_binding.h" | 32 #include "mojo/public/cpp/bindings/strong_associated_binding.h" |
34 #include "net/base/url_util.h" | 33 #include "net/base/url_util.h" |
35 #include "storage/browser/blob/blob_storage_context.h" | |
36 | 34 |
37 namespace content { | 35 namespace content { |
38 | 36 |
39 namespace { | 37 namespace { |
40 | 38 |
41 // Provider host for navigation with PlzNavigate or when service worker's | 39 // Provider host for navigation with PlzNavigate or when service worker's |
42 // context is created on the browser side. This function provides the next | 40 // context is created on the browser side. This function provides the next |
43 // ServiceWorkerProviderHost ID for them, starts at -2 and keeps going down. | 41 // ServiceWorkerProviderHost ID for them, starts at -2 and keeps going down. |
44 int NextBrowserProvidedProviderId() { | 42 int NextBrowserProvidedProviderId() { |
45 static int g_next_browser_provided_provider_id = -2; | 43 static int g_next_browser_provided_provider_id = -2; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // process, to have been destroyed before being claimed by the renderer. The | 91 // process, to have been destroyed before being claimed by the renderer. The |
94 // provider is then destroyed in the renderer, and no matching host will be | 92 // provider is then destroyed in the renderer, and no matching host will be |
95 // found. | 93 // found. |
96 DCHECK(IsBrowserSideNavigationEnabled() && | 94 DCHECK(IsBrowserSideNavigationEnabled() && |
97 ServiceWorkerUtils::IsBrowserAssignedProviderId(provider_id)); | 95 ServiceWorkerUtils::IsBrowserAssignedProviderId(provider_id)); |
98 return; | 96 return; |
99 } | 97 } |
100 context->RemoveProviderHost(process_id, provider_id); | 98 context->RemoveProviderHost(process_id, provider_id); |
101 } | 99 } |
102 | 100 |
103 // Used by a Service Worker for script loading (for all script loading for now, | |
104 // but to be used only during installation once script streaming lands). | |
105 // For now this is just a proxy loader for the network loader. | |
106 // Eventually this should replace the existing URLRequestJob-based request | |
107 // interception for script loading, namely ServiceWorkerWriteToCacheJob. | |
108 // TODO(kinuko): Implement this. Hook up the existing code in | |
109 // ServiceWorkerContextRequestHandler. | |
110 class ScriptURLLoader : public mojom::URLLoader, public mojom::URLLoaderClient { | |
111 public: | |
112 ScriptURLLoader( | |
113 int32_t routing_id, | |
114 int32_t request_id, | |
115 uint32_t options, | |
116 const ResourceRequest& resource_request, | |
117 mojom::URLLoaderClientPtr client, | |
118 base::WeakPtr<ServiceWorkerContextCore> context, | |
119 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | |
120 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | |
121 scoped_refptr<URLLoaderFactoryGetter> loader_factory_getter, | |
122 const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) | |
123 : network_client_binding_(this), | |
124 forwarding_client_(std::move(client)), | |
125 provider_host_(provider_host) { | |
126 mojom::URLLoaderClientPtr network_client; | |
127 network_client_binding_.Bind(mojo::MakeRequest(&network_client)); | |
128 loader_factory_getter->GetNetworkFactory()->get()->CreateLoaderAndStart( | |
129 mojo::MakeRequest(&network_loader_), routing_id, request_id, options, | |
130 resource_request, std::move(network_client), traffic_annotation); | |
131 } | |
132 ~ScriptURLLoader() override {} | |
133 | |
134 // mojom::URLLoader: | |
135 void FollowRedirect() override { network_loader_->FollowRedirect(); } | |
136 void SetPriority(net::RequestPriority priority, | |
137 int32_t intra_priority_value) override { | |
138 network_loader_->SetPriority(priority, intra_priority_value); | |
139 } | |
140 | |
141 // mojom::URLLoaderClient for simply proxying network: | |
142 void OnReceiveResponse( | |
143 const ResourceResponseHead& response_head, | |
144 const base::Optional<net::SSLInfo>& ssl_info, | |
145 mojom::DownloadedTempFilePtr downloaded_file) override { | |
146 if (provider_host_) { | |
147 // We don't have complete info here, but fill in what we have now. | |
148 // At least we need headers and SSL info. | |
149 net::HttpResponseInfo response_info; | |
150 response_info.headers = response_head.headers; | |
151 if (ssl_info.has_value()) | |
152 response_info.ssl_info = *ssl_info; | |
153 response_info.was_fetched_via_spdy = response_head.was_fetched_via_spdy; | |
154 response_info.was_alpn_negotiated = response_head.was_alpn_negotiated; | |
155 response_info.alpn_negotiated_protocol = | |
156 response_head.alpn_negotiated_protocol; | |
157 response_info.connection_info = response_head.connection_info; | |
158 response_info.socket_address = response_head.socket_address; | |
159 | |
160 DCHECK(provider_host_->IsHostToRunningServiceWorker()); | |
161 provider_host_->running_hosted_version()->SetMainScriptHttpResponseInfo( | |
162 response_info); | |
163 } | |
164 forwarding_client_->OnReceiveResponse(response_head, ssl_info, | |
165 std::move(downloaded_file)); | |
166 } | |
167 void OnReceiveRedirect(const net::RedirectInfo& redirect_info, | |
168 const ResourceResponseHead& response_head) override { | |
169 forwarding_client_->OnReceiveRedirect(redirect_info, response_head); | |
170 } | |
171 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override { | |
172 forwarding_client_->OnDataDownloaded(data_len, encoded_data_len); | |
173 } | |
174 void OnUploadProgress(int64_t current_position, | |
175 int64_t total_size, | |
176 OnUploadProgressCallback ack_callback) override { | |
177 forwarding_client_->OnUploadProgress(current_position, total_size, | |
178 std::move(ack_callback)); | |
179 } | |
180 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override { | |
181 forwarding_client_->OnReceiveCachedMetadata(data); | |
182 } | |
183 void OnTransferSizeUpdated(int32_t transfer_size_diff) override { | |
184 forwarding_client_->OnTransferSizeUpdated(transfer_size_diff); | |
185 } | |
186 void OnStartLoadingResponseBody( | |
187 mojo::ScopedDataPipeConsumerHandle body) override { | |
188 forwarding_client_->OnStartLoadingResponseBody(std::move(body)); | |
189 } | |
190 void OnComplete(const ResourceRequestCompletionStatus& status) override { | |
191 forwarding_client_->OnComplete(status); | |
192 } | |
193 | |
194 private: | |
195 mojom::URLLoaderAssociatedPtr network_loader_; | |
196 mojo::Binding<mojom::URLLoaderClient> network_client_binding_; | |
197 mojom::URLLoaderClientPtr forwarding_client_; | |
198 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; | |
199 | |
200 DISALLOW_COPY_AND_ASSIGN(ScriptURLLoader); | |
201 }; | |
202 | |
203 // Created per one controller worker for script loading (only during | |
204 // installation, eventually). This is kept alive while | |
205 // ServiceWorkerNetworkProvider in the renderer process is alive. | |
206 // Used only when IsServicificationEnabled is true. | |
207 class ScriptURLLoaderFactory : public mojom::URLLoaderFactory { | |
208 public: | |
209 ScriptURLLoaderFactory( | |
210 base::WeakPtr<ServiceWorkerContextCore> context, | |
211 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | |
212 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | |
213 scoped_refptr<URLLoaderFactoryGetter> loader_factory_getter) | |
214 : context_(context), | |
215 provider_host_(provider_host), | |
216 blob_storage_context_(blob_storage_context), | |
217 loader_factory_getter_(loader_factory_getter) {} | |
218 ~ScriptURLLoaderFactory() override {} | |
219 | |
220 // mojom::URLLoaderFactory: | |
221 void CreateLoaderAndStart(mojom::URLLoaderAssociatedRequest request, | |
222 int32_t routing_id, | |
223 int32_t request_id, | |
224 uint32_t options, | |
225 const ResourceRequest& resource_request, | |
226 mojom::URLLoaderClientPtr client, | |
227 const net::MutableNetworkTrafficAnnotationTag& | |
228 traffic_annotation) override { | |
229 mojo::MakeStrongAssociatedBinding( | |
230 base::MakeUnique<ScriptURLLoader>( | |
231 routing_id, request_id, options, resource_request, | |
232 std::move(client), context_, provider_host_, blob_storage_context_, | |
233 loader_factory_getter_, traffic_annotation), | |
234 std::move(request)); | |
235 } | |
236 | |
237 void SyncLoad(int32_t routing_id, | |
238 int32_t request_id, | |
239 const ResourceRequest& request, | |
240 SyncLoadCallback callback) override { | |
241 NOTREACHED(); | |
242 } | |
243 | |
244 private: | |
245 base::WeakPtr<ServiceWorkerContextCore> context_; | |
246 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; | |
247 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | |
248 scoped_refptr<URLLoaderFactoryGetter> loader_factory_getter_; | |
249 DISALLOW_COPY_AND_ASSIGN(ScriptURLLoaderFactory); | |
250 }; | |
251 | |
252 } // anonymous namespace | 101 } // anonymous namespace |
253 | 102 |
254 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( | 103 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( |
255 const GetRegistrationForReadyCallback& callback) | 104 const GetRegistrationForReadyCallback& callback) |
256 : callback(callback), | 105 : callback(callback), |
257 called(false) { | 106 called(false) { |
258 } | 107 } |
259 | 108 |
260 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { | 109 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { |
261 } | 110 } |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 if (get_ready_callback_) | 572 if (get_ready_callback_) |
724 return false; | 573 return false; |
725 get_ready_callback_.reset(new OneShotGetReadyCallback(callback)); | 574 get_ready_callback_.reset(new OneShotGetReadyCallback(callback)); |
726 ReturnRegistrationForReadyIfNeeded(); | 575 ReturnRegistrationForReadyIfNeeded(); |
727 return true; | 576 return true; |
728 } | 577 } |
729 | 578 |
730 std::unique_ptr<ServiceWorkerProviderHost> | 579 std::unique_ptr<ServiceWorkerProviderHost> |
731 ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() { | 580 ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() { |
732 DCHECK(!IsBrowserSideNavigationEnabled()); | 581 DCHECK(!IsBrowserSideNavigationEnabled()); |
733 DCHECK(!ServiceWorkerUtils::IsServicificationEnabled()); | |
734 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); | 582 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
735 DCHECK_NE(MSG_ROUTING_NONE, info_.route_id); | 583 DCHECK_NE(MSG_ROUTING_NONE, info_.route_id); |
736 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_); | 584 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_); |
737 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type); | 585 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type); |
738 | 586 |
739 std::unique_ptr<ServiceWorkerProviderHost> provisional_host = | 587 std::unique_ptr<ServiceWorkerProviderHost> provisional_host = |
740 base::WrapUnique(new ServiceWorkerProviderHost( | 588 base::WrapUnique(new ServiceWorkerProviderHost( |
741 process_id(), | 589 process_id(), |
742 ServiceWorkerProviderHostInfo(std::move(info_), binding_.Unbind(), | 590 ServiceWorkerProviderHostInfo(std::move(info_), binding_.Unbind(), |
743 provider_.PassInterface()), | 591 provider_.PassInterface()), |
(...skipping 13 matching lines...) Expand all Loading... |
757 | 605 |
758 render_process_id_ = ChildProcessHost::kInvalidUniqueID; | 606 render_process_id_ = ChildProcessHost::kInvalidUniqueID; |
759 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; | 607 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; |
760 dispatcher_host_ = nullptr; | 608 dispatcher_host_ = nullptr; |
761 return provisional_host; | 609 return provisional_host; |
762 } | 610 } |
763 | 611 |
764 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( | 612 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( |
765 ServiceWorkerProviderHost* provisional_host) { | 613 ServiceWorkerProviderHost* provisional_host) { |
766 DCHECK(!IsBrowserSideNavigationEnabled()); | 614 DCHECK(!IsBrowserSideNavigationEnabled()); |
767 DCHECK(!ServiceWorkerUtils::IsServicificationEnabled()); | |
768 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); | 615 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
769 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, provisional_host->process_id()); | 616 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, provisional_host->process_id()); |
770 DCHECK_NE(MSG_ROUTING_NONE, provisional_host->frame_id()); | 617 DCHECK_NE(MSG_ROUTING_NONE, provisional_host->frame_id()); |
771 | 618 |
772 render_process_id_ = provisional_host->process_id(); | 619 render_process_id_ = provisional_host->process_id(); |
773 render_thread_id_ = kDocumentMainThreadId; | 620 render_thread_id_ = kDocumentMainThreadId; |
774 dispatcher_host_ = provisional_host->dispatcher_host(); | 621 dispatcher_host_ = provisional_host->dispatcher_host(); |
775 info_ = std::move(provisional_host->info_); | 622 info_ = std::move(provisional_host->info_); |
776 | 623 |
777 // Take the connection over from the provisional host. | 624 // Take the connection over from the provisional host. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 dispatcher_host->GetRegistrationObjectInfoAndVersionAttributes( | 702 dispatcher_host->GetRegistrationObjectInfoAndVersionAttributes( |
856 AsWeakPtr(), registration, &info, &attrs); | 703 AsWeakPtr(), registration, &info, &attrs); |
857 | 704 |
858 // Initialize provider_info. | 705 // Initialize provider_info. |
859 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info = | 706 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info = |
860 mojom::ServiceWorkerProviderInfoForStartWorker::New(); | 707 mojom::ServiceWorkerProviderInfoForStartWorker::New(); |
861 provider_info->provider_id = provider_id(); | 708 provider_info->provider_id = provider_id(); |
862 provider_info->attributes = std::move(attrs); | 709 provider_info->attributes = std::move(attrs); |
863 provider_info->registration = std::move(info); | 710 provider_info->registration = std::move(info); |
864 provider_info->client_request = mojo::MakeRequest(&provider_); | 711 provider_info->client_request = mojo::MakeRequest(&provider_); |
865 | |
866 mojom::URLLoaderFactoryAssociatedPtrInfo loader_factory_ptr_info; | |
867 if (ServiceWorkerUtils::IsServicificationEnabled()) { | |
868 mojo::MakeStrongAssociatedBinding( | |
869 base::MakeUnique<ScriptURLLoaderFactory>( | |
870 context_, AsWeakPtr(), context_->blob_storage_context(), | |
871 context_->loader_factory_getter()), | |
872 mojo::MakeRequest(&loader_factory_ptr_info)); | |
873 provider_info->script_loader_factory_ptr_info = | |
874 std::move(loader_factory_ptr_info); | |
875 } | |
876 | |
877 binding_.Bind(mojo::MakeRequest(&provider_info->host_ptr_info)); | 712 binding_.Bind(mojo::MakeRequest(&provider_info->host_ptr_info)); |
878 binding_.set_connection_error_handler( | 713 binding_.set_connection_error_handler( |
879 base::Bind(&RemoveProviderHost, context_, process_id, provider_id())); | 714 base::Bind(&RemoveProviderHost, context_, process_id, provider_id())); |
880 | 715 |
881 // Set the document URL to the script url in order to allow | 716 // Set the document URL to the script url in order to allow |
882 // register/unregister/getRegistration on ServiceWorkerGlobalScope. | 717 // register/unregister/getRegistration on ServiceWorkerGlobalScope. |
883 SetDocumentUrl(running_hosted_version()->script_url()); | 718 SetDocumentUrl(running_hosted_version()->script_url()); |
884 | 719 |
885 return provider_info; | 720 return provider_info; |
886 } | 721 } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 render_thread_id_, provider_id(), | 892 render_thread_id_, provider_id(), |
1058 GetOrCreateServiceWorkerHandle( | 893 GetOrCreateServiceWorkerHandle( |
1059 associated_registration_->active_version()), | 894 associated_registration_->active_version()), |
1060 false /* shouldNotifyControllerChange */, | 895 false /* shouldNotifyControllerChange */, |
1061 associated_registration_->active_version()->used_features())); | 896 associated_registration_->active_version()->used_features())); |
1062 } | 897 } |
1063 } | 898 } |
1064 } | 899 } |
1065 | 900 |
1066 } // namespace content | 901 } // namespace content |
OLD | NEW |