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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "content/browser/service_worker/service_worker_registration.h" | 23 #include "content/browser/service_worker/service_worker_registration.h" |
24 #include "content/common/content_export.h" | 24 #include "content/common/content_export.h" |
25 #include "content/common/service_worker/service_worker_provider_host_info.h" | 25 #include "content/common/service_worker/service_worker_provider_host_info.h" |
26 #include "content/common/service_worker/service_worker_provider_interfaces.mojom
.h" | 26 #include "content/common/service_worker/service_worker_provider_interfaces.mojom
.h" |
27 #include "content/common/service_worker/service_worker_types.h" | 27 #include "content/common/service_worker/service_worker_types.h" |
28 #include "content/common/worker_url_loader_factory_provider.mojom.h" | 28 #include "content/common/worker_url_loader_factory_provider.mojom.h" |
29 #include "content/public/common/request_context_frame_type.h" | 29 #include "content/public/common/request_context_frame_type.h" |
30 #include "content/public/common/request_context_type.h" | 30 #include "content/public/common/request_context_type.h" |
31 #include "content/public/common/resource_type.h" | 31 #include "content/public/common/resource_type.h" |
32 #include "mojo/public/cpp/bindings/associated_binding.h" | 32 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 33 #include "mojo/public/cpp/bindings/strong_associated_binding_set.h" |
33 | 34 |
34 namespace storage { | 35 namespace storage { |
35 class BlobStorageContext; | 36 class BlobStorageContext; |
36 } | 37 } |
37 | 38 |
38 namespace content { | 39 namespace content { |
39 | 40 |
40 class MessagePort; | 41 class MessagePort; |
41 class ResourceRequestBodyImpl; | 42 class ResourceRequestBodyImpl; |
42 class ServiceWorkerContextCore; | 43 class ServiceWorkerContextCore; |
43 class ServiceWorkerDispatcherHost; | 44 class ServiceWorkerDispatcherHost; |
44 class ServiceWorkerRequestHandler; | 45 class ServiceWorkerRequestHandler; |
45 class ServiceWorkerVersion; | 46 class ServiceWorkerVersion; |
46 class WebContents; | 47 class WebContents; |
47 | 48 |
48 // This class is the browser-process representation of a service worker | 49 // This class is the browser-process representation of a service worker |
49 // provider. There are two general types of providers: 1) those for a client | 50 // provider. There are two general types of providers: 1) those for a client |
50 // (windows, dedicated workers, or shared workers), and 2) those for hosting a | 51 // (windows, dedicated workers, or shared workers), and 2) those for hosting a |
51 // running service worker. | 52 // running service worker. |
52 // | 53 // |
53 // For client providers, there is a provider per document or a worker and the | 54 // For client providers, there is a provider per document or a worker and the |
54 // lifetime of this object is tied to the lifetime of its document or the worker | 55 // lifetime of this object is tied to the lifetime of its document or the worker |
55 // in the renderer process. This class holds service worker state that is scoped | 56 // in the renderer process. This class holds service worker state that is scoped |
56 // to an individual document or a worker. | 57 // to an individual document or a worker. |
57 // | 58 // |
58 // For providers hosting a running service worker, this class will observe | 59 // For providers hosting a running service worker, this class will observe |
59 // resource loads made directly by the service worker. | 60 // resource loads made directly by the service worker. |
60 // | 61 // |
61 // A ServiceWorkerProviderHost instance is created when a | 62 // A ServiceWorkerProviderHost is created in the following situations: |
62 // ServiceWorkerNetworkProvider is created on the renderer process, which | 63 // 1) When it's for a document or worker (i.e., a service |
63 // happens 1) when a document or worker (i.e., a service worker client) is | 64 // worker client), the provider host is created when |
64 // created, or 2) during service worker startup. Mojo's connection from | 65 // ServiceWorkerNetworkProvider is created on the renderer process. Mojo's |
65 // ServiceWorkerNetworkProvider is established on the creation time, and the | 66 // connection from ServiceWorkerNetworkProvider is established on the creation |
66 // instance is destroyed on disconnection from the renderer side. | 67 // time. |
67 // If PlzNavigate is turned on, an instance is pre-created on the browser | 68 // 2) When it's for a service worker context, the provider host is created on |
| 69 // the browser process before launching the service worker's thread. Mojo's |
| 70 // connection to the renderer is established with the StartWorker message. |
| 71 // 3) When PlzNavigate is turned on, an instance is pre-created on the browser |
68 // before ServiceWorkerNetworkProvider is created on the renderer because | 72 // before ServiceWorkerNetworkProvider is created on the renderer because |
69 // navigation is initiated on the browser side. In that case, establishment of | 73 // navigation is initiated on the browser side. In that case, establishment of |
70 // Mojo's connection will be deferred until ServiceWorkerNetworkProvider is | 74 // Mojo's connection will be deferred until ServiceWorkerNetworkProvider is |
71 // created on the renderer. | 75 // created on the renderer. |
| 76 // Destruction of the ServiceWorkerProviderHost instance happens on |
| 77 // disconnection of the Mojo's pipe from the renderer side regardless of what |
| 78 // the provider is for. |
72 class CONTENT_EXPORT ServiceWorkerProviderHost | 79 class CONTENT_EXPORT ServiceWorkerProviderHost |
73 : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener), | 80 : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener), |
74 public base::SupportsWeakPtr<ServiceWorkerProviderHost>, | 81 public base::SupportsWeakPtr<ServiceWorkerProviderHost>, |
75 public NON_EXPORTED_BASE(mojom::ServiceWorkerProviderHost) { | 82 public NON_EXPORTED_BASE(mojom::ServiceWorkerProviderHost) { |
76 public: | 83 public: |
77 using GetRegistrationForReadyCallback = | 84 using GetRegistrationForReadyCallback = |
78 base::Callback<void(ServiceWorkerRegistration* reigstration)>; | 85 base::Callback<void(ServiceWorkerRegistration* reigstration)>; |
79 | 86 |
80 using WebContentsGetter = base::Callback<WebContents*(void)>; | 87 using WebContentsGetter = base::Callback<WebContents*(void)>; |
81 | 88 |
82 // PlzNavigate | 89 // PlzNavigate |
83 // Used to pre-create a ServiceWorkerProviderHost for a navigation. The | 90 // Used to pre-create a ServiceWorkerProviderHost for a navigation. The |
84 // ServiceWorkerNetworkProvider will later be created in the renderer, should | 91 // ServiceWorkerNetworkProvider will later be created in the renderer, should |
85 // the navigation succeed. |is_parent_frame_is_secure| should be true for main | 92 // the navigation succeed. |is_parent_frame_is_secure| should be true for main |
86 // frames. Otherwise it is true iff all ancestor frames of this frame have a | 93 // frames. Otherwise it is true iff all ancestor frames of this frame have a |
87 // secure origin. |web_contents_getter| indicates the tab where the navigation | 94 // secure origin. |web_contents_getter| indicates the tab where the navigation |
88 // is occurring. | 95 // is occurring. |
89 static std::unique_ptr<ServiceWorkerProviderHost> PreCreateNavigationHost( | 96 static std::unique_ptr<ServiceWorkerProviderHost> PreCreateNavigationHost( |
90 base::WeakPtr<ServiceWorkerContextCore> context, | 97 base::WeakPtr<ServiceWorkerContextCore> context, |
91 bool are_ancestors_secure, | 98 bool are_ancestors_secure, |
92 const WebContentsGetter& web_contents_getter); | 99 const WebContentsGetter& web_contents_getter); |
93 | 100 |
| 101 // Creates a ServiceWorkerProviderHost for hosting a running service worker. |
| 102 // Information about the ServiceWorkerProviderHost is passed with the |
| 103 // StartWorker message. |
| 104 static std::unique_ptr<ServiceWorkerProviderHost> PreCreateForController( |
| 105 ServiceWorkerVersion* hosted_version, |
| 106 base::WeakPtr<ServiceWorkerContextCore> context); |
| 107 |
94 // Used to create a ServiceWorkerProviderHost when the renderer-side provider | 108 // Used to create a ServiceWorkerProviderHost when the renderer-side provider |
95 // is created. This ProviderHost will be created for the process specified by | 109 // is created. This ProviderHost will be created for the process specified by |
96 // |process_id|. | 110 // |process_id|. |
97 static std::unique_ptr<ServiceWorkerProviderHost> Create( | 111 static std::unique_ptr<ServiceWorkerProviderHost> Create( |
98 int process_id, | 112 int process_id, |
99 ServiceWorkerProviderHostInfo info, | 113 ServiceWorkerProviderHostInfo info, |
100 base::WeakPtr<ServiceWorkerContextCore> context, | 114 base::WeakPtr<ServiceWorkerContextCore> context, |
101 ServiceWorkerDispatcherHost* dispatcher_host); | 115 ServiceWorkerDispatcherHost* dispatcher_host); |
102 | 116 |
103 ~ServiceWorkerProviderHost() override; | 117 ~ServiceWorkerProviderHost() override; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 207 |
194 // Associates to |registration| to listen for its version change events and | 208 // Associates to |registration| to listen for its version change events and |
195 // sets the controller. If |notify_controllerchange| is true, instructs the | 209 // sets the controller. If |notify_controllerchange| is true, instructs the |
196 // renderer to dispatch a 'controllerchange' event. | 210 // renderer to dispatch a 'controllerchange' event. |
197 void AssociateRegistration(ServiceWorkerRegistration* registration, | 211 void AssociateRegistration(ServiceWorkerRegistration* registration, |
198 bool notify_controllerchange); | 212 bool notify_controllerchange); |
199 | 213 |
200 // Clears the associated registration and stop listening to it. | 214 // Clears the associated registration and stop listening to it. |
201 void DisassociateRegistration(); | 215 void DisassociateRegistration(); |
202 | 216 |
203 void SetHostedVersion(ServiceWorkerVersion* version); | |
204 | |
205 // Returns a handler for a request, the handler may return NULL if | 217 // Returns a handler for a request, the handler may return NULL if |
206 // the request doesn't require special handling. | 218 // the request doesn't require special handling. |
207 std::unique_ptr<ServiceWorkerRequestHandler> CreateRequestHandler( | 219 std::unique_ptr<ServiceWorkerRequestHandler> CreateRequestHandler( |
208 FetchRequestMode request_mode, | 220 FetchRequestMode request_mode, |
209 FetchCredentialsMode credentials_mode, | 221 FetchCredentialsMode credentials_mode, |
210 FetchRedirectMode redirect_mode, | 222 FetchRedirectMode redirect_mode, |
211 ResourceType resource_type, | 223 ResourceType resource_type, |
212 RequestContextType request_context_type, | 224 RequestContextType request_context_type, |
213 RequestContextFrameType frame_type, | 225 RequestContextFrameType frame_type, |
214 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 226 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 return dispatcher_host_; | 275 return dispatcher_host_; |
264 } | 276 } |
265 | 277 |
266 // PlzNavigate | 278 // PlzNavigate |
267 // Completes initialization of provider hosts used for navigation requests. | 279 // Completes initialization of provider hosts used for navigation requests. |
268 void CompleteNavigationInitialized( | 280 void CompleteNavigationInitialized( |
269 int process_id, | 281 int process_id, |
270 ServiceWorkerProviderHostInfo info, | 282 ServiceWorkerProviderHostInfo info, |
271 ServiceWorkerDispatcherHost* dispatcher_host); | 283 ServiceWorkerDispatcherHost* dispatcher_host); |
272 | 284 |
| 285 // Completes initialization of provider hosts for controllers. |
| 286 // This is called when a process for the service worker has already been |
| 287 // allocated and EmbeddedWorkerInstance is about to send the StartWorker |
| 288 // message. |out_provider_client_info| will be initialized by the values to |
| 289 // create ServiceWorkerNetworkProvider on the renderer which will be connected |
| 290 // to this instance. |
| 291 virtual void CompleteStartWorkerPreparation( |
| 292 int process_id, |
| 293 mojom::ServiceWorkerProviderClientInfoPtr* out_provider_client_info); |
| 294 |
273 // Sends event messages to the renderer. Events for the worker are queued up | 295 // Sends event messages to the renderer. Events for the worker are queued up |
274 // until the worker thread id is known via SetReadyToSendMessagesToWorker(). | 296 // until the worker thread id is known via SetReadyToSendMessagesToWorker(). |
275 void SendUpdateFoundMessage( | 297 void SendUpdateFoundMessage( |
276 int registration_handle_id); | 298 int registration_handle_id); |
277 void SendSetVersionAttributesMessage( | 299 void SendSetVersionAttributesMessage( |
278 int registration_handle_id, | 300 int registration_handle_id, |
279 ChangedVersionAttributesMask changed_mask, | 301 ChangedVersionAttributesMask changed_mask, |
280 ServiceWorkerVersion* installing_version, | 302 ServiceWorkerVersion* installing_version, |
281 ServiceWorkerVersion* waiting_version, | 303 ServiceWorkerVersion* waiting_version, |
282 ServiceWorkerVersion* active_version); | 304 ServiceWorkerVersion* active_version); |
(...skipping 15 matching lines...) Expand all Loading... |
298 // exceptional condition like it could no longer be read from the script | 320 // exceptional condition like it could no longer be read from the script |
299 // cache. | 321 // cache. |
300 void NotifyControllerLost(); | 322 void NotifyControllerLost(); |
301 | 323 |
302 // Binds the ServiceWorkerWorkerClient of a dedicated (or shared) worker to | 324 // Binds the ServiceWorkerWorkerClient of a dedicated (or shared) worker to |
303 // the parent frame's ServiceWorkerProviderHost. (This is used only when | 325 // the parent frame's ServiceWorkerProviderHost. (This is used only when |
304 // off-main-thread-fetch is enabled.) | 326 // off-main-thread-fetch is enabled.) |
305 void BindWorkerFetchContext( | 327 void BindWorkerFetchContext( |
306 mojom::ServiceWorkerWorkerClientAssociatedPtrInfo client_ptr_info); | 328 mojom::ServiceWorkerWorkerClientAssociatedPtrInfo client_ptr_info); |
307 | 329 |
| 330 protected: |
| 331 ServiceWorkerProviderHost(int process_id, |
| 332 ServiceWorkerProviderHostInfo info, |
| 333 base::WeakPtr<ServiceWorkerContextCore> context, |
| 334 ServiceWorkerDispatcherHost* dispatcher_host); |
| 335 |
308 private: | 336 private: |
309 friend class ForeignFetchRequestHandlerTest; | 337 friend class ForeignFetchRequestHandlerTest; |
310 friend class LinkHeaderServiceWorkerTest; | 338 friend class LinkHeaderServiceWorkerTest; |
311 friend class ServiceWorkerProviderHostTest; | 339 friend class ServiceWorkerProviderHostTest; |
312 friend class ServiceWorkerWriteToCacheJobTest; | 340 friend class ServiceWorkerWriteToCacheJobTest; |
313 friend class ServiceWorkerContextRequestHandlerTest; | 341 friend class ServiceWorkerContextRequestHandlerTest; |
314 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest, Update_SameScript); | 342 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest, Update_SameScript); |
315 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest, | 343 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest, |
316 Update_SameSizeScript); | 344 Update_SameSizeScript); |
317 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest, | 345 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest, |
(...skipping 10 matching lines...) Expand all Loading... |
328 | 356 |
329 struct OneShotGetReadyCallback { | 357 struct OneShotGetReadyCallback { |
330 GetRegistrationForReadyCallback callback; | 358 GetRegistrationForReadyCallback callback; |
331 bool called; | 359 bool called; |
332 | 360 |
333 explicit OneShotGetReadyCallback( | 361 explicit OneShotGetReadyCallback( |
334 const GetRegistrationForReadyCallback& callback); | 362 const GetRegistrationForReadyCallback& callback); |
335 ~OneShotGetReadyCallback(); | 363 ~OneShotGetReadyCallback(); |
336 }; | 364 }; |
337 | 365 |
338 ServiceWorkerProviderHost(int process_id, | |
339 ServiceWorkerProviderHostInfo info, | |
340 base::WeakPtr<ServiceWorkerContextCore> context, | |
341 ServiceWorkerDispatcherHost* dispatcher_host); | |
342 | |
343 // ServiceWorkerRegistration::Listener overrides. | 366 // ServiceWorkerRegistration::Listener overrides. |
344 void OnVersionAttributesChanged( | 367 void OnVersionAttributesChanged( |
345 ServiceWorkerRegistration* registration, | 368 ServiceWorkerRegistration* registration, |
346 ChangedVersionAttributesMask changed_mask, | 369 ChangedVersionAttributesMask changed_mask, |
347 const ServiceWorkerRegistrationInfo& info) override; | 370 const ServiceWorkerRegistrationInfo& info) override; |
348 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override; | 371 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override; |
349 void OnRegistrationFinishedUninstalling( | 372 void OnRegistrationFinishedUninstalling( |
350 ServiceWorkerRegistration* registration) override; | 373 ServiceWorkerRegistration* registration) override; |
351 void OnSkippedWaiting(ServiceWorkerRegistration* registration) override; | 374 void OnSkippedWaiting(ServiceWorkerRegistration* registration) override; |
352 | 375 |
(...skipping 13 matching lines...) Expand all Loading... |
366 | 389 |
367 // Increase/decrease this host's process reference for |pattern|. | 390 // Increase/decrease this host's process reference for |pattern|. |
368 void IncreaseProcessReference(const GURL& pattern); | 391 void IncreaseProcessReference(const GURL& pattern); |
369 void DecreaseProcessReference(const GURL& pattern); | 392 void DecreaseProcessReference(const GURL& pattern); |
370 | 393 |
371 void ReturnRegistrationForReadyIfNeeded(); | 394 void ReturnRegistrationForReadyIfNeeded(); |
372 | 395 |
373 bool IsReadyToSendMessages() const; | 396 bool IsReadyToSendMessages() const; |
374 void Send(IPC::Message* message) const; | 397 void Send(IPC::Message* message) const; |
375 | 398 |
| 399 // Sets the version hosted by this provider. This method should be used only |
| 400 // for provider hosts for controllers. |
| 401 void SetHostedVersion(ServiceWorkerVersion* version); |
| 402 |
376 // Notifies the information about the controller and associated registration | 403 // Notifies the information about the controller and associated registration |
377 // to the provider on the renderer. This is for cross site transfer and | 404 // to the provider on the renderer. This is for cross site transfer and |
378 // browser side navigation which need to decide which process will handle the | 405 // browser side navigation which need to decide which process will handle the |
379 // request later. | 406 // request later. |
380 void NotifyControllerToAssociatedProvider(); | 407 void NotifyControllerToAssociatedProvider(); |
381 | 408 |
382 // Clears the information of the ServiceWorkerWorkerClient of dedicated (or | 409 // Clears the information of the ServiceWorkerWorkerClient of dedicated (or |
383 // shared) worker, when the connection to the worker is disconnected. | 410 // shared) worker, when the connection to the worker is disconnected. |
384 void UnregisterWorkerFetchContext(mojom::ServiceWorkerWorkerClient*); | 411 void UnregisterWorkerFetchContext(mojom::ServiceWorkerWorkerClient*); |
385 | 412 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 std::unordered_map<mojom::ServiceWorkerWorkerClient*, | 464 std::unordered_map<mojom::ServiceWorkerWorkerClient*, |
438 mojom::ServiceWorkerWorkerClientAssociatedPtr> | 465 mojom::ServiceWorkerWorkerClientAssociatedPtr> |
439 worker_clients_; | 466 worker_clients_; |
440 | 467 |
441 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); | 468 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); |
442 }; | 469 }; |
443 | 470 |
444 } // namespace content | 471 } // namespace content |
445 | 472 |
446 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 473 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
OLD | NEW |