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

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

Issue 2653493009: Add two interfaces for ServiceWorkerProviderContext/ProviderHost (Closed)
Patch Set: Rebased/Fixed unittests when BrowserSideNavigation is enabled Created 3 years, 7 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 #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
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <set> 13 #include <set>
14 #include <string> 14 #include <string>
15 #include <unordered_map> 15 #include <unordered_map>
16 #include <vector> 16 #include <vector>
17 17
18 #include "base/gtest_prod_util.h" 18 #include "base/gtest_prod_util.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/memory/ref_counted.h" 20 #include "base/memory/ref_counted.h"
21 #include "base/memory/weak_ptr.h" 21 #include "base/memory/weak_ptr.h"
22 #include "content/browser/service_worker/service_worker_registration.h" 22 #include "content/browser/service_worker/service_worker_registration.h"
23 #include "content/common/content_export.h" 23 #include "content/common/content_export.h"
24 #include "content/common/service_worker/service_worker_provider_host_info.h" 24 #include "content/common/service_worker/service_worker_provider_host_info.h"
25 #include "content/common/service_worker/service_worker_provider_interfaces.mojom .h"
25 #include "content/common/service_worker/service_worker_types.h" 26 #include "content/common/service_worker/service_worker_types.h"
26 #include "content/common/worker_url_loader_factory_provider.mojom.h" 27 #include "content/common/worker_url_loader_factory_provider.mojom.h"
27 #include "content/public/common/request_context_frame_type.h" 28 #include "content/public/common/request_context_frame_type.h"
28 #include "content/public/common/request_context_type.h" 29 #include "content/public/common/request_context_type.h"
29 #include "content/public/common/resource_type.h" 30 #include "content/public/common/resource_type.h"
31 #include "mojo/public/cpp/bindings/associated_binding.h"
30 32
31 namespace storage { 33 namespace storage {
32 class BlobStorageContext; 34 class BlobStorageContext;
33 } 35 }
34 36
35 namespace content { 37 namespace content {
36 38
37 class MessagePort; 39 class MessagePort;
38 class ResourceRequestBodyImpl; 40 class ResourceRequestBodyImpl;
39 class ServiceWorkerContextCore; 41 class ServiceWorkerContextCore;
40 class ServiceWorkerDispatcherHost; 42 class ServiceWorkerDispatcherHost;
41 class ServiceWorkerRequestHandler; 43 class ServiceWorkerRequestHandler;
42 class ServiceWorkerVersion; 44 class ServiceWorkerVersion;
43 class WebContents; 45 class WebContents;
44 46
45 // This class is the browser-process representation of a service worker 47 // This class is the browser-process representation of a service worker
46 // provider. There are two general types of providers: 1) those for a client 48 // provider. There are two general types of providers: 1) those for a client
47 // (windows, dedicated workers, or shared workers), and 2) those for hosting a 49 // (windows, dedicated workers, or shared workers), and 2) those for hosting a
48 // running service worker. 50 // running service worker.
49 // 51 //
50 // For client providers, there is a provider per document or a worker and the 52 // For client providers, there is a provider per document or a worker and the
51 // lifetime of this object is tied to the lifetime of its document or the worker 53 // lifetime of this object is tied to the lifetime of its document or the worker
52 // in the renderer process. This class holds service worker state that is scoped 54 // in the renderer process. This class holds service worker state that is scoped
53 // to an individual document or a worker. 55 // to an individual document or a worker.
54 // 56 //
55 // For providers hosting a running service worker, this class will observe 57 // For providers hosting a running service worker, this class will observe
56 // resource loads made directly by the service worker. 58 // resource loads made directly by the service worker.
57 class CONTENT_EXPORT ServiceWorkerProviderHost 59 class CONTENT_EXPORT ServiceWorkerProviderHost
58 : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener), 60 : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener),
59 public base::SupportsWeakPtr<ServiceWorkerProviderHost> { 61 public base::SupportsWeakPtr<ServiceWorkerProviderHost>,
62 public NON_EXPORTED_BASE(mojom::ServiceWorkerProviderHost) {
60 public: 63 public:
61 using GetRegistrationForReadyCallback = 64 using GetRegistrationForReadyCallback =
62 base::Callback<void(ServiceWorkerRegistration* reigstration)>; 65 base::Callback<void(ServiceWorkerRegistration* reigstration)>;
63 66
64 using WebContentsGetter = base::Callback<WebContents*(void)>; 67 using WebContentsGetter = base::Callback<WebContents*(void)>;
65 68
66 // PlzNavigate 69 // PlzNavigate
67 // Used to pre-create a ServiceWorkerProviderHost for a navigation. The 70 // Used to pre-create a ServiceWorkerProviderHost for a navigation. The
68 // ServiceWorkerNetworkProvider will later be created in the renderer, should 71 // ServiceWorkerNetworkProvider will later be created in the renderer, should
69 // the navigation succeed. |is_parent_frame_is_secure| should be true for main 72 // the navigation succeed. |is_parent_frame_is_secure| should be true for main
70 // frames. Otherwise it is true iff all ancestor frames of this frame have a 73 // frames. Otherwise it is true iff all ancestor frames of this frame have a
71 // secure origin. |web_contents_getter| indicates the tab where the navigation 74 // secure origin. |web_contents_getter| indicates the tab where the navigation
72 // is occurring. 75 // is occurring.
73 static std::unique_ptr<ServiceWorkerProviderHost> PreCreateNavigationHost( 76 static std::unique_ptr<ServiceWorkerProviderHost> PreCreateNavigationHost(
74 base::WeakPtr<ServiceWorkerContextCore> context, 77 base::WeakPtr<ServiceWorkerContextCore> context,
75 bool are_ancestors_secure, 78 bool are_ancestors_secure,
76 const WebContentsGetter& web_contents_getter); 79 const WebContentsGetter& web_contents_getter);
77 80
78 // Used to create a ServiceWorkerProviderHost when the renderer-side provider 81 // Used to create a ServiceWorkerProviderHost when the renderer-side provider
79 // is created. This ProviderHost will be created for the process specified by 82 // is created. This ProviderHost will be created for the process specified by
80 // |process_id|. 83 // |process_id|.
81 static std::unique_ptr<ServiceWorkerProviderHost> Create( 84 static std::unique_ptr<ServiceWorkerProviderHost> Create(
82 int process_id, 85 int process_id,
83 ServiceWorkerProviderHostInfo info, 86 ServiceWorkerProviderHostInfo info,
84 base::WeakPtr<ServiceWorkerContextCore> context, 87 base::WeakPtr<ServiceWorkerContextCore> context,
85 ServiceWorkerDispatcherHost* dispatcher_host); 88 ServiceWorkerDispatcherHost* dispatcher_host);
86 89
87 virtual ~ServiceWorkerProviderHost(); 90 ~ServiceWorkerProviderHost() override;
88 91
89 const std::string& client_uuid() const { return client_uuid_; } 92 const std::string& client_uuid() const { return client_uuid_; }
90 int process_id() const { return render_process_id_; } 93 int process_id() const { return render_process_id_; }
91 int provider_id() const { return provider_id_; } 94 int provider_id() const { return info_.provider_id; }
92 int frame_id() const; 95 int frame_id() const;
93 int route_id() const { return route_id_; } 96 int route_id() const { return info_.route_id; }
94 const WebContentsGetter& web_contents_getter() const { 97 const WebContentsGetter& web_contents_getter() const {
95 return web_contents_getter_; 98 return web_contents_getter_;
96 } 99 }
97 100
98 bool is_parent_frame_secure() const { return is_parent_frame_secure_; } 101 bool is_parent_frame_secure() const { return info_.is_parent_frame_secure; }
99 102
100 // Returns whether this provider host is secure enough to have a service 103 // Returns whether this provider host is secure enough to have a service
101 // worker controller. 104 // worker controller.
102 // Analogous to Blink's Document::isSecureContext. Because of how service 105 // Analogous to Blink's Document::isSecureContext. Because of how service
103 // worker intercepts main resource requests, this check must be done 106 // worker intercepts main resource requests, this check must be done
104 // browser-side once the URL is known (see comments in 107 // browser-side once the URL is known (see comments in
105 // ServiceWorkerNetworkProvider::CreateForNavigation). This function uses 108 // ServiceWorkerNetworkProvider::CreateForNavigation). This function uses
106 // |document_url_| and |is_parent_frame_secure_| to determine context 109 // |document_url_| and |is_parent_frame_secure_| to determine context
107 // security, so they must be set properly before calling this function. 110 // security, so they must be set properly before calling this function.
108 bool IsContextSecureForServiceWorker() const; 111 bool IsContextSecureForServiceWorker() const;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 // Only clients can have an associated registration. 153 // Only clients can have an associated registration.
151 DCHECK(!associated_registration_ || IsProviderForClient()); 154 DCHECK(!associated_registration_ || IsProviderForClient());
152 return associated_registration_.get(); 155 return associated_registration_.get();
153 } 156 }
154 157
155 // The running version, if any, that this provider is providing resource 158 // The running version, if any, that this provider is providing resource
156 // loads for. 159 // loads for.
157 ServiceWorkerVersion* running_hosted_version() const { 160 ServiceWorkerVersion* running_hosted_version() const {
158 // Only providers for controllers can host a running version. 161 // Only providers for controllers can host a running version.
159 DCHECK(!running_hosted_version_ || 162 DCHECK(!running_hosted_version_ ||
160 provider_type_ == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER); 163 info_.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER);
161 return running_hosted_version_.get(); 164 return running_hosted_version_.get();
162 } 165 }
163 166
164 // Sets the |document_url_|. When this object is for a client, 167 // Sets the |document_url_|. When this object is for a client,
165 // |matching_registrations_| gets also updated to ensure that |document_url_| 168 // |matching_registrations_| gets also updated to ensure that |document_url_|
166 // is in scope of all |matching_registrations_|. 169 // is in scope of all |matching_registrations_|.
167 void SetDocumentUrl(const GURL& url); 170 void SetDocumentUrl(const GURL& url);
168 const GURL& document_url() const { return document_url_; } 171 const GURL& document_url() const { return document_url_; }
169 172
170 void SetTopmostFrameUrl(const GURL& url); 173 void SetTopmostFrameUrl(const GURL& url);
171 const GURL& topmost_frame_url() const { return topmost_frame_url_; } 174 const GURL& topmost_frame_url() const { return topmost_frame_url_; }
172 175
173 ServiceWorkerProviderType provider_type() const { return provider_type_; } 176 ServiceWorkerProviderType provider_type() const { return info_.type; }
174 bool IsProviderForClient() const; 177 bool IsProviderForClient() const;
175 blink::WebServiceWorkerClientType client_type() const; 178 blink::WebServiceWorkerClientType client_type() const;
176 179
177 // Associates to |registration| to listen for its version change events and 180 // Associates to |registration| to listen for its version change events and
178 // sets the controller. If |notify_controllerchange| is true, instructs the 181 // sets the controller. If |notify_controllerchange| is true, instructs the
179 // renderer to dispatch a 'controllerchange' event. 182 // renderer to dispatch a 'controllerchange' event.
180 void AssociateRegistration(ServiceWorkerRegistration* registration, 183 void AssociateRegistration(ServiceWorkerRegistration* registration,
181 bool notify_controllerchange); 184 bool notify_controllerchange);
182 185
183 // Clears the associated registration and stop listening to it. 186 // Clears the associated registration and stop listening to it.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 // |registration| claims the document to be controlled. 237 // |registration| claims the document to be controlled.
235 void ClaimedByRegistration(ServiceWorkerRegistration* registration); 238 void ClaimedByRegistration(ServiceWorkerRegistration* registration);
236 239
237 // Called by dispatcher host to get the registration for the "ready" property. 240 // Called by dispatcher host to get the registration for the "ready" property.
238 // Returns false if there's a completed or ongoing request for the document. 241 // Returns false if there's a completed or ongoing request for the document.
239 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator- service-worker-ready 242 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator- service-worker-ready
240 bool GetRegistrationForReady(const GetRegistrationForReadyCallback& callback); 243 bool GetRegistrationForReady(const GetRegistrationForReadyCallback& callback);
241 244
242 // Methods to support cross site navigations. 245 // Methods to support cross site navigations.
243 std::unique_ptr<ServiceWorkerProviderHost> PrepareForCrossSiteTransfer(); 246 std::unique_ptr<ServiceWorkerProviderHost> PrepareForCrossSiteTransfer();
244 void CompleteCrossSiteTransfer( 247 void CompleteCrossSiteTransfer(ServiceWorkerProviderHost* provisional_host);
245 int new_process_id,
246 int new_frame_id,
247 int new_provider_id,
248 ServiceWorkerProviderType new_provider_type,
249 ServiceWorkerDispatcherHost* dispatcher_host);
250 ServiceWorkerDispatcherHost* dispatcher_host() const { 248 ServiceWorkerDispatcherHost* dispatcher_host() const {
251 return dispatcher_host_; 249 return dispatcher_host_;
252 } 250 }
253 251
254 // PlzNavigate 252 // PlzNavigate
255 // Completes initialization of provider hosts used for navigation requests. 253 // Completes initialization of provider hosts used for navigation requests.
256 void CompleteNavigationInitialized( 254 void CompleteNavigationInitialized(
257 int process_id, 255 int process_id,
258 int frame_routing_id, 256 ServiceWorkerProviderHostInfo info,
259 ServiceWorkerDispatcherHost* dispatcher_host); 257 ServiceWorkerDispatcherHost* dispatcher_host);
260 258
261 // Sends event messages to the renderer. Events for the worker are queued up 259 // Sends event messages to the renderer. Events for the worker are queued up
262 // until the worker thread id is known via SetReadyToSendMessagesToWorker(). 260 // until the worker thread id is known via SetReadyToSendMessagesToWorker().
263 void SendUpdateFoundMessage( 261 void SendUpdateFoundMessage(
264 int registration_handle_id); 262 int registration_handle_id);
265 void SendSetVersionAttributesMessage( 263 void SendSetVersionAttributesMessage(
266 int registration_handle_id, 264 int registration_handle_id,
267 ChangedVersionAttributesMask changed_mask, 265 ChangedVersionAttributesMask changed_mask,
268 ServiceWorkerVersion* installing_version, 266 ServiceWorkerVersion* installing_version,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 314
317 struct OneShotGetReadyCallback { 315 struct OneShotGetReadyCallback {
318 GetRegistrationForReadyCallback callback; 316 GetRegistrationForReadyCallback callback;
319 bool called; 317 bool called;
320 318
321 explicit OneShotGetReadyCallback( 319 explicit OneShotGetReadyCallback(
322 const GetRegistrationForReadyCallback& callback); 320 const GetRegistrationForReadyCallback& callback);
323 ~OneShotGetReadyCallback(); 321 ~OneShotGetReadyCallback();
324 }; 322 };
325 323
326 ServiceWorkerProviderHost(int render_process_id, 324 ServiceWorkerProviderHost(int process_id,
327 int route_id, 325 ServiceWorkerProviderHostInfo info,
328 int provider_id,
329 ServiceWorkerProviderType provider_type,
330 bool is_parent_frame_secure,
331 base::WeakPtr<ServiceWorkerContextCore> context, 326 base::WeakPtr<ServiceWorkerContextCore> context,
332 ServiceWorkerDispatcherHost* dispatcher_host); 327 ServiceWorkerDispatcherHost* dispatcher_host);
333 328
334 // ServiceWorkerRegistration::Listener overrides. 329 // ServiceWorkerRegistration::Listener overrides.
335 void OnVersionAttributesChanged( 330 void OnVersionAttributesChanged(
336 ServiceWorkerRegistration* registration, 331 ServiceWorkerRegistration* registration,
337 ChangedVersionAttributesMask changed_mask, 332 ChangedVersionAttributesMask changed_mask,
338 const ServiceWorkerRegistrationInfo& info) override; 333 const ServiceWorkerRegistrationInfo& info) override;
339 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override; 334 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override;
340 void OnRegistrationFinishedUninstalling( 335 void OnRegistrationFinishedUninstalling(
(...skipping 28 matching lines...) Expand all
369 int frame_routing_id, 364 int frame_routing_id,
370 ServiceWorkerDispatcherHost* dispatcher_host); 365 ServiceWorkerDispatcherHost* dispatcher_host);
371 366
372 // Clears the information of the ServiceWorkerWorkerClient of dedicated (or 367 // Clears the information of the ServiceWorkerWorkerClient of dedicated (or
373 // shared) worker, when the connection to the worker is disconnected. 368 // shared) worker, when the connection to the worker is disconnected.
374 void UnregisterWorkerFetchContext(mojom::ServiceWorkerWorkerClient*); 369 void UnregisterWorkerFetchContext(mojom::ServiceWorkerWorkerClient*);
375 370
376 std::string client_uuid_; 371 std::string client_uuid_;
377 int render_process_id_; 372 int render_process_id_;
378 373
379 // See the constructor's documentation.
380 int route_id_;
381
382 // For provider hosts that are hosting a running service worker, the id of the 374 // For provider hosts that are hosting a running service worker, the id of the
383 // service worker thread. Otherwise, |kDocumentMainThreadId|. May be 375 // service worker thread. Otherwise, |kDocumentMainThreadId|. May be
384 // |kInvalidEmbeddedWorkerThreadId| before the hosted service worker starts 376 // |kInvalidEmbeddedWorkerThreadId| before the hosted service worker starts
385 // up, or during cross-site transfers. 377 // up, or during cross-site transfers.
386 int render_thread_id_; 378 int render_thread_id_;
387 379
388 // Unique within the renderer process. 380 // Keeps the basic provider info provided from the renderer side.
389 int provider_id_; 381 ServiceWorkerProviderHostInfo info_;
390 382
391 // PlzNavigate 383 // PlzNavigate
392 // Only set when this object is pre-created for a navigation. It indicates the 384 // Only set when this object is pre-created for a navigation. It indicates the
393 // tab where the navigation occurs. 385 // tab where the navigation occurs.
394 WebContentsGetter web_contents_getter_; 386 WebContentsGetter web_contents_getter_;
395 387
396 ServiceWorkerProviderType provider_type_;
397 const bool is_parent_frame_secure_;
falken 2017/05/18 04:13:09 Can you put the refactoring to ServiceWorkerProvid
shimazu 2017/05/19 08:31:06 Done.
398 GURL document_url_; 388 GURL document_url_;
399 GURL topmost_frame_url_; 389 GURL topmost_frame_url_;
400 390
401 std::vector<GURL> associated_patterns_; 391 std::vector<GURL> associated_patterns_;
402 scoped_refptr<ServiceWorkerRegistration> associated_registration_; 392 scoped_refptr<ServiceWorkerRegistration> associated_registration_;
403 393
404 // Keyed by registration scope URL length. 394 // Keyed by registration scope URL length.
405 typedef std::map<size_t, scoped_refptr<ServiceWorkerRegistration>> 395 typedef std::map<size_t, scoped_refptr<ServiceWorkerRegistration>>
406 ServiceWorkerRegistrationMap; 396 ServiceWorkerRegistrationMap;
407 // Contains all living registrations whose pattern this document's URL 397 // Contains all living registrations whose pattern this document's URL
408 // starts with. It is empty if IsContextSecureForServiceWorker() is 398 // starts with. It is empty if IsContextSecureForServiceWorker() is
409 // false. 399 // false.
410 ServiceWorkerRegistrationMap matching_registrations_; 400 ServiceWorkerRegistrationMap matching_registrations_;
411 401
412 std::unique_ptr<OneShotGetReadyCallback> get_ready_callback_; 402 std::unique_ptr<OneShotGetReadyCallback> get_ready_callback_;
413 scoped_refptr<ServiceWorkerVersion> controlling_version_; 403 scoped_refptr<ServiceWorkerVersion> controlling_version_;
414 scoped_refptr<ServiceWorkerVersion> running_hosted_version_; 404 scoped_refptr<ServiceWorkerVersion> running_hosted_version_;
415 base::WeakPtr<ServiceWorkerContextCore> context_; 405 base::WeakPtr<ServiceWorkerContextCore> context_;
416 ServiceWorkerDispatcherHost* dispatcher_host_; 406 ServiceWorkerDispatcherHost* dispatcher_host_;
417 bool allow_association_; 407 bool allow_association_;
418 408
409 mojom::ServiceWorkerProviderAssociatedPtr client_;
falken 2017/05/18 04:13:09 "client" is a really overloaded word in service wo
shimazu 2017/05/19 08:31:06 Sounds good! Done.
410 mojo::AssociatedBinding<mojom::ServiceWorkerProviderHost> binding_;
411
419 std::vector<base::Closure> queued_events_; 412 std::vector<base::Closure> queued_events_;
420 413
421 // Keeps ServiceWorkerWorkerClient pointers of dedicated or shared workers 414 // Keeps ServiceWorkerWorkerClient pointers of dedicated or shared workers
422 // which are associated with the ServiceWorkerProviderHost. 415 // which are associated with the ServiceWorkerProviderHost.
423 std::unordered_map<mojom::ServiceWorkerWorkerClient*, 416 std::unordered_map<mojom::ServiceWorkerWorkerClient*,
424 mojom::ServiceWorkerWorkerClientAssociatedPtr> 417 mojom::ServiceWorkerWorkerClientAssociatedPtr>
425 worker_clients_; 418 worker_clients_;
426 419
427 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); 420 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost);
428 }; 421 };
429 422
430 } // namespace content 423 } // namespace content
431 424
432 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ 425 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698