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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/browser/service_worker/service_worker_registration.h"
14 #include "content/common/content_export.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "content/public/common/request_context_frame_type.h"
17 #include "content/public/common/request_context_type.h"
18 #include "content/public/common/resource_type.h"
25 class BlobStorageContext
;
30 class ResourceRequestBody
;
31 class ServiceWorkerContextCore
;
32 class ServiceWorkerDispatcherHost
;
33 class ServiceWorkerRequestHandler
;
34 class ServiceWorkerVersion
;
36 // This class is the browser-process representation of a service worker
37 // provider. There is a provider per document or a worker and the lifetime
38 // of this object is tied to the lifetime of its document or the worker
39 // in the renderer process.
40 // This class holds service worker state that is scoped to an individual
41 // document or a worker.
43 // Note this class can also host a running service worker, in which
44 // case it will observe resource loads made directly by the service worker.
45 class CONTENT_EXPORT ServiceWorkerProviderHost
46 : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener
),
47 public base::SupportsWeakPtr
<ServiceWorkerProviderHost
> {
49 using GetClientInfoCallback
=
50 base::Callback
<void(const ServiceWorkerClientInfo
&)>;
51 using GetRegistrationForReadyCallback
=
52 base::Callback
<void(ServiceWorkerRegistration
* reigstration
)>;
54 // When this provider host is for a Service Worker context, |route_id| is
55 // MSG_ROUTING_NONE. When this provider host is for a Document,
56 // |route_id| is the frame ID of the Document. When this provider host is for
57 // a Shared Worker, |route_id| is the Shared Worker route ID.
58 // |provider_type| gives additional information whether the provider is
59 // created for controller (ServiceWorker) or controllee (Document or
61 ServiceWorkerProviderHost(int render_process_id
,
64 ServiceWorkerProviderType provider_type
,
65 base::WeakPtr
<ServiceWorkerContextCore
> context
,
66 ServiceWorkerDispatcherHost
* dispatcher_host
);
67 virtual ~ServiceWorkerProviderHost();
69 const std::string
& client_uuid() const { return client_uuid_
; }
70 int process_id() const { return render_process_id_
; }
71 int provider_id() const { return provider_id_
; }
73 int route_id() const { return route_id_
; }
75 bool IsHostToRunningServiceWorker() {
76 return running_hosted_version_
.get() != NULL
;
79 ServiceWorkerVersion
* controlling_version() const {
80 return controlling_version_
.get();
82 ServiceWorkerVersion
* active_version() const {
83 return associated_registration_
.get() ?
84 associated_registration_
->active_version() : NULL
;
86 ServiceWorkerVersion
* waiting_version() const {
87 return associated_registration_
.get() ?
88 associated_registration_
->waiting_version() : NULL
;
90 ServiceWorkerVersion
* installing_version() const {
91 return associated_registration_
.get() ?
92 associated_registration_
->installing_version() : NULL
;
95 ServiceWorkerRegistration
* associated_registration() const {
96 return associated_registration_
.get();
99 // The running version, if any, that this provider is providing resource
101 ServiceWorkerVersion
* running_hosted_version() const {
102 return running_hosted_version_
.get();
105 void SetDocumentUrl(const GURL
& url
);
106 const GURL
& document_url() const { return document_url_
; }
108 void SetTopmostFrameUrl(const GURL
& url
);
109 const GURL
& topmost_frame_url() const { return topmost_frame_url_
; }
111 ServiceWorkerProviderType
provider_type() const { return provider_type_
; }
112 bool IsProviderForClient() const;
113 blink::WebServiceWorkerClientType
client_type() const;
115 // Associates to |registration| to listen for its version change events and
116 // sets the controller. If |notify_controllerchange| is true, instructs the
117 // renderer to dispatch a 'controllerchange' event.
118 void AssociateRegistration(ServiceWorkerRegistration
* registration
,
119 bool notify_controllerchange
);
121 // Clears the associated registration and stop listening to it.
122 void DisassociateRegistration();
124 // Returns false if the version is not in the expected STARTING in our
125 // process state. That would be indicative of a bad IPC message.
126 bool SetHostedVersionId(int64 versions_id
);
128 // Returns a handler for a request, the handler may return NULL if
129 // the request doesn't require special handling.
130 scoped_ptr
<ServiceWorkerRequestHandler
> CreateRequestHandler(
131 FetchRequestMode request_mode
,
132 FetchCredentialsMode credentials_mode
,
133 ResourceType resource_type
,
134 RequestContextType request_context_type
,
135 RequestContextFrameType frame_type
,
136 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
137 scoped_refptr
<ResourceRequestBody
> body
);
139 // Used to get a ServiceWorkerObjectInfo to send to the renderer. Finds an
140 // existing ServiceWorkerHandle, and increments its reference count, or else
141 // creates a new one (initialized to ref count 1). Returns the
142 // ServiceWorkerInfo from the handle. The renderer is expected to use
143 // ServiceWorkerHandleReference::Adopt to balance out the ref count.
144 ServiceWorkerObjectInfo
GetOrCreateServiceWorkerHandle(
145 ServiceWorkerVersion
* version
);
147 // Returns true if |registration| can be associated with this provider.
148 bool CanAssociateRegistration(ServiceWorkerRegistration
* registration
);
150 // For use by the ServiceWorkerControlleeRequestHandler to disallow
151 // new registration association while a navigation is occurring and
152 // an existing registration is being looked for.
153 void SetAllowAssociation(bool allow
) { allow_association_
= allow
; }
155 // Returns true if the context referred to by this host (i.e. |context_|) is
157 bool IsContextAlive();
159 // Dispatches message event to the document.
161 const base::string16
& message
,
162 const std::vector
<TransferredMessagePort
>& sent_message_ports
);
164 // Activates the WebContents associated with
165 // { render_process_id_, route_id_ }.
166 // Runs the |callback| with the updated ServiceWorkerClientInfo in parameter.
167 void Focus(const GetClientInfoCallback
& callback
);
169 // Asks the renderer to send back the document information.
170 void GetWindowClientInfo(const GetClientInfoCallback
& callback
) const;
172 // Same as above but has to be called from the UI thread.
173 // It is taking the process and frame ids in parameter because |this| is meant
174 // to live on the IO thread.
175 static ServiceWorkerClientInfo
GetWindowClientInfoOnUI(int render_process_id
,
176 int render_frame_id
);
178 // Adds reference of this host's process to the |pattern|, the reference will
179 // be removed in destructor.
180 void AddScopedProcessReferenceToPattern(const GURL
& pattern
);
182 // |registration| claims the document to be controlled.
183 void ClaimedByRegistration(ServiceWorkerRegistration
* registration
);
185 // Called by dispatcher host to get the registration for the "ready" property.
186 // Returns false if there's a completed or ongoing request for the document.
187 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-ready
188 bool GetRegistrationForReady(const GetRegistrationForReadyCallback
& callback
);
190 // Methods to support cross site navigations.
191 void PrepareForCrossSiteTransfer();
192 void CompleteCrossSiteTransfer(
196 ServiceWorkerProviderType new_provider_type
,
197 ServiceWorkerDispatcherHost
* dispatcher_host
);
198 ServiceWorkerDispatcherHost
* dispatcher_host() const {
199 return dispatcher_host_
;
202 // Sends event messages to the renderer. Events for the worker are queued up
203 // until the worker thread id is known via SetReadyToSendMessagesToWorker().
204 void SendUpdateFoundMessage(
205 int registration_handle_id
);
206 void SendSetVersionAttributesMessage(
207 int registration_handle_id
,
208 ChangedVersionAttributesMask changed_mask
,
209 ServiceWorkerVersion
* installing_version
,
210 ServiceWorkerVersion
* waiting_version
,
211 ServiceWorkerVersion
* active_version
);
212 void SendServiceWorkerStateChangedMessage(
213 int worker_handle_id
,
214 blink::WebServiceWorkerState state
);
216 // Sets the worker thread id and flushes queued events.
217 void SetReadyToSendMessagesToWorker(int render_thread_id
);
219 void AddMatchingRegistration(ServiceWorkerRegistration
* registration
);
220 void RemoveMatchingRegistration(ServiceWorkerRegistration
* registration
);
222 // An optimized implementation of [[Match Service Worker Registration]]
223 // for current document.
224 ServiceWorkerRegistration
* MatchRegistration() const;
226 // Called when our controller has been terminated and doomed due to an
227 // exceptional condition like it could no longer be read from the script
229 void NotifyControllerLost();
232 friend class ServiceWorkerProviderHostTest
;
233 friend class ServiceWorkerWriteToCacheJobTest
;
234 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest
, Update_SameScript
);
235 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest
,
236 Update_SameSizeScript
);
237 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest
,
238 Update_TruncatedScript
);
239 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest
,
240 Update_ElongatedScript
);
241 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest
,
243 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
244 UpdateBefore24Hours
);
245 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
247 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
248 UpdateForceBypassCache
);
250 struct OneShotGetReadyCallback
{
251 GetRegistrationForReadyCallback callback
;
254 explicit OneShotGetReadyCallback(
255 const GetRegistrationForReadyCallback
& callback
);
256 ~OneShotGetReadyCallback();
259 // ServiceWorkerRegistration::Listener overrides.
260 void OnVersionAttributesChanged(
261 ServiceWorkerRegistration
* registration
,
262 ChangedVersionAttributesMask changed_mask
,
263 const ServiceWorkerRegistrationInfo
& info
) override
;
264 void OnRegistrationFailed(ServiceWorkerRegistration
* registration
) override
;
265 void OnRegistrationFinishedUninstalling(
266 ServiceWorkerRegistration
* registration
) override
;
267 void OnSkippedWaiting(ServiceWorkerRegistration
* registration
) override
;
269 // Sets the controller version field to |version| or if |version| is NULL,
270 // clears the field. If |notify_controllerchange| is true, instructs the
271 // renderer to dispatch a 'controller' change event.
272 void SetControllerVersionAttribute(ServiceWorkerVersion
* version
,
273 bool notify_controllerchange
);
275 void SendAssociateRegistrationMessage();
277 // Increase/decrease this host's process reference for |pattern|.
278 void IncreaseProcessReference(const GURL
& pattern
);
279 void DecreaseProcessReference(const GURL
& pattern
);
281 void ReturnRegistrationForReadyIfNeeded();
283 bool IsReadyToSendMessages() const;
284 void Send(IPC::Message
* message
) const;
286 std::string client_uuid_
;
287 int render_process_id_
;
289 int render_thread_id_
;
291 ServiceWorkerProviderType provider_type_
;
293 GURL topmost_frame_url_
;
295 std::vector
<GURL
> associated_patterns_
;
296 scoped_refptr
<ServiceWorkerRegistration
> associated_registration_
;
298 // Keyed by registration scope URL length.
299 typedef std::map
<size_t, scoped_refptr
<ServiceWorkerRegistration
>>
300 ServiceWorkerRegistrationMap
;
301 // Contains all living registrations which has pattern this document's
303 ServiceWorkerRegistrationMap matching_registrations_
;
305 scoped_ptr
<OneShotGetReadyCallback
> get_ready_callback_
;
306 scoped_refptr
<ServiceWorkerVersion
> controlling_version_
;
307 scoped_refptr
<ServiceWorkerVersion
> running_hosted_version_
;
308 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
309 ServiceWorkerDispatcherHost
* dispatcher_host_
;
310 bool allow_association_
;
312 std::vector
<base::Closure
> queued_events_
;
314 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost
);
317 } // namespace content
319 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_