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

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

Issue 2943463002: Implement dumb URLLoader{Factory} for ServiceWorker script loading (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"
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"
22 #include "content/common/resource_request_body_impl.h" 23 #include "content/common/resource_request_body_impl.h"
23 #include "content/common/service_worker/service_worker_messages.h" 24 #include "content/common/service_worker/service_worker_messages.h"
24 #include "content/common/service_worker/service_worker_types.h" 25 #include "content/common/service_worker/service_worker_types.h"
25 #include "content/common/service_worker/service_worker_utils.h" 26 #include "content/common/service_worker/service_worker_utils.h"
26 #include "content/public/browser/content_browser_client.h" 27 #include "content/public/browser/content_browser_client.h"
27 #include "content/public/common/browser_side_navigation_policy.h" 28 #include "content/public/common/browser_side_navigation_policy.h"
28 #include "content/public/common/child_process_host.h" 29 #include "content/public/common/child_process_host.h"
29 #include "content/public/common/content_client.h" 30 #include "content/public/common/content_client.h"
30 #include "content/public/common/content_features.h" 31 #include "content/public/common/content_features.h"
31 #include "content/public/common/origin_util.h" 32 #include "content/public/common/origin_util.h"
32 #include "mojo/public/cpp/bindings/strong_associated_binding.h" 33 #include "mojo/public/cpp/bindings/strong_associated_binding.h"
33 #include "net/base/url_util.h" 34 #include "net/base/url_util.h"
35 #include "storage/browser/blob/blob_storage_context.h"
34 36
35 namespace content { 37 namespace content {
36 38
37 namespace { 39 namespace {
38 40
39 // Provider host for navigation with PlzNavigate or when service worker's 41 // Provider host for navigation with PlzNavigate or when service worker's
40 // context is created on the browser side. This function provides the next 42 // context is created on the browser side. This function provides the next
41 // ServiceWorkerProviderHost ID for them, starts at -2 and keeps going down. 43 // ServiceWorkerProviderHost ID for them, starts at -2 and keeps going down.
42 int NextBrowserProvidedProviderId() { 44 int NextBrowserProvidedProviderId() {
43 static int g_next_browser_provided_provider_id = -2; 45 static int g_next_browser_provided_provider_id = -2;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 // process, to have been destroyed before being claimed by the renderer. The 93 // process, to have been destroyed before being claimed by the renderer. The
92 // provider is then destroyed in the renderer, and no matching host will be 94 // provider is then destroyed in the renderer, and no matching host will be
93 // found. 95 // found.
94 DCHECK(IsBrowserSideNavigationEnabled() && 96 DCHECK(IsBrowserSideNavigationEnabled() &&
95 ServiceWorkerUtils::IsBrowserAssignedProviderId(provider_id)); 97 ServiceWorkerUtils::IsBrowserAssignedProviderId(provider_id));
96 return; 98 return;
97 } 99 }
98 context->RemoveProviderHost(process_id, provider_id); 100 context->RemoveProviderHost(process_id, provider_id);
99 } 101 }
100 102
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
101 } // anonymous namespace 252 } // anonymous namespace
102 253
103 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback( 254 ServiceWorkerProviderHost::OneShotGetReadyCallback::OneShotGetReadyCallback(
104 const GetRegistrationForReadyCallback& callback) 255 const GetRegistrationForReadyCallback& callback)
105 : callback(callback), 256 : callback(callback),
106 called(false) { 257 called(false) {
107 } 258 }
108 259
109 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() { 260 ServiceWorkerProviderHost::OneShotGetReadyCallback::~OneShotGetReadyCallback() {
110 } 261 }
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 if (get_ready_callback_) 723 if (get_ready_callback_)
573 return false; 724 return false;
574 get_ready_callback_.reset(new OneShotGetReadyCallback(callback)); 725 get_ready_callback_.reset(new OneShotGetReadyCallback(callback));
575 ReturnRegistrationForReadyIfNeeded(); 726 ReturnRegistrationForReadyIfNeeded();
576 return true; 727 return true;
577 } 728 }
578 729
579 std::unique_ptr<ServiceWorkerProviderHost> 730 std::unique_ptr<ServiceWorkerProviderHost>
580 ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() { 731 ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() {
581 DCHECK(!IsBrowserSideNavigationEnabled()); 732 DCHECK(!IsBrowserSideNavigationEnabled());
733 DCHECK(!ServiceWorkerUtils::IsServicificationEnabled());
582 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); 734 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_);
583 DCHECK_NE(MSG_ROUTING_NONE, info_.route_id); 735 DCHECK_NE(MSG_ROUTING_NONE, info_.route_id);
584 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_); 736 DCHECK_EQ(kDocumentMainThreadId, render_thread_id_);
585 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type); 737 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type);
586 738
587 std::unique_ptr<ServiceWorkerProviderHost> provisional_host = 739 std::unique_ptr<ServiceWorkerProviderHost> provisional_host =
588 base::WrapUnique(new ServiceWorkerProviderHost( 740 base::WrapUnique(new ServiceWorkerProviderHost(
589 process_id(), 741 process_id(),
590 ServiceWorkerProviderHostInfo(std::move(info_), binding_.Unbind(), 742 ServiceWorkerProviderHostInfo(std::move(info_), binding_.Unbind(),
591 provider_.PassInterface()), 743 provider_.PassInterface()),
(...skipping 13 matching lines...) Expand all
605 757
606 render_process_id_ = ChildProcessHost::kInvalidUniqueID; 758 render_process_id_ = ChildProcessHost::kInvalidUniqueID;
607 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; 759 render_thread_id_ = kInvalidEmbeddedWorkerThreadId;
608 dispatcher_host_ = nullptr; 760 dispatcher_host_ = nullptr;
609 return provisional_host; 761 return provisional_host;
610 } 762 }
611 763
612 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( 764 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer(
613 ServiceWorkerProviderHost* provisional_host) { 765 ServiceWorkerProviderHost* provisional_host) {
614 DCHECK(!IsBrowserSideNavigationEnabled()); 766 DCHECK(!IsBrowserSideNavigationEnabled());
767 DCHECK(!ServiceWorkerUtils::IsServicificationEnabled());
615 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); 768 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_);
616 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, provisional_host->process_id()); 769 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, provisional_host->process_id());
617 DCHECK_NE(MSG_ROUTING_NONE, provisional_host->frame_id()); 770 DCHECK_NE(MSG_ROUTING_NONE, provisional_host->frame_id());
618 771
619 render_process_id_ = provisional_host->process_id(); 772 render_process_id_ = provisional_host->process_id();
620 render_thread_id_ = kDocumentMainThreadId; 773 render_thread_id_ = kDocumentMainThreadId;
621 dispatcher_host_ = provisional_host->dispatcher_host(); 774 dispatcher_host_ = provisional_host->dispatcher_host();
622 info_ = std::move(provisional_host->info_); 775 info_ = std::move(provisional_host->info_);
623 776
624 // Take the connection over from the provisional host. 777 // Take the connection over from the provisional host.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 dispatcher_host->GetRegistrationObjectInfoAndVersionAttributes( 855 dispatcher_host->GetRegistrationObjectInfoAndVersionAttributes(
703 AsWeakPtr(), registration, &info, &attrs); 856 AsWeakPtr(), registration, &info, &attrs);
704 857
705 // Initialize provider_info. 858 // Initialize provider_info.
706 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info = 859 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info =
707 mojom::ServiceWorkerProviderInfoForStartWorker::New(); 860 mojom::ServiceWorkerProviderInfoForStartWorker::New();
708 provider_info->provider_id = provider_id(); 861 provider_info->provider_id = provider_id();
709 provider_info->attributes = std::move(attrs); 862 provider_info->attributes = std::move(attrs);
710 provider_info->registration = std::move(info); 863 provider_info->registration = std::move(info);
711 provider_info->client_request = mojo::MakeRequest(&provider_); 864 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
712 binding_.Bind(mojo::MakeRequest(&provider_info->host_ptr_info)); 877 binding_.Bind(mojo::MakeRequest(&provider_info->host_ptr_info));
713 binding_.set_connection_error_handler( 878 binding_.set_connection_error_handler(
714 base::Bind(&RemoveProviderHost, context_, process_id, provider_id())); 879 base::Bind(&RemoveProviderHost, context_, process_id, provider_id()));
715 880
716 // Set the document URL to the script url in order to allow 881 // Set the document URL to the script url in order to allow
717 // register/unregister/getRegistration on ServiceWorkerGlobalScope. 882 // register/unregister/getRegistration on ServiceWorkerGlobalScope.
718 SetDocumentUrl(running_hosted_version()->script_url()); 883 SetDocumentUrl(running_hosted_version()->script_url());
719 884
720 return provider_info; 885 return provider_info;
721 } 886 }
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 render_thread_id_, provider_id(), 1057 render_thread_id_, provider_id(),
893 GetOrCreateServiceWorkerHandle( 1058 GetOrCreateServiceWorkerHandle(
894 associated_registration_->active_version()), 1059 associated_registration_->active_version()),
895 false /* shouldNotifyControllerChange */, 1060 false /* shouldNotifyControllerChange */,
896 associated_registration_->active_version()->used_features())); 1061 associated_registration_->active_version()->used_features()));
897 } 1062 }
898 } 1063 }
899 } 1064 }
900 1065
901 } // namespace content 1066 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698