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 // If |render_frame_id| is MSG_ROUTING_NONE, this provider host works for the
55 // worker context, i.e. ServiceWorker or SharedWorker.
56 // |provider_type| gives additional information whether the provider is
57 // created for controller (ServiceWorker) or controllee (Document or
59 ServiceWorkerProviderHost(int render_process_id
,
62 ServiceWorkerProviderType provider_type
,
63 base::WeakPtr
<ServiceWorkerContextCore
> context
,
64 ServiceWorkerDispatcherHost
* dispatcher_host
);
65 virtual ~ServiceWorkerProviderHost();
67 const std::string
& client_uuid() const { return client_uuid_
; }
68 int process_id() const { return render_process_id_
; }
69 int provider_id() const { return provider_id_
; }
70 int frame_id() const { return render_frame_id_
; }
72 bool IsHostToRunningServiceWorker() {
73 return running_hosted_version_
.get() != NULL
;
76 ServiceWorkerVersion
* controlling_version() const {
77 return controlling_version_
.get();
79 ServiceWorkerVersion
* active_version() const {
80 return associated_registration_
.get() ?
81 associated_registration_
->active_version() : NULL
;
83 ServiceWorkerVersion
* waiting_version() const {
84 return associated_registration_
.get() ?
85 associated_registration_
->waiting_version() : NULL
;
87 ServiceWorkerVersion
* installing_version() const {
88 return associated_registration_
.get() ?
89 associated_registration_
->installing_version() : NULL
;
92 ServiceWorkerRegistration
* associated_registration() const {
93 return associated_registration_
.get();
96 // The running version, if any, that this provider is providing resource
98 ServiceWorkerVersion
* running_hosted_version() const {
99 return running_hosted_version_
.get();
102 void SetDocumentUrl(const GURL
& url
);
103 const GURL
& document_url() const { return document_url_
; }
105 void SetTopmostFrameUrl(const GURL
& url
);
106 const GURL
& topmost_frame_url() const { return topmost_frame_url_
; }
108 ServiceWorkerProviderType
provider_type() const { return provider_type_
; }
110 // Associates to |registration| to listen for its version change events.
111 void AssociateRegistration(ServiceWorkerRegistration
* registration
);
113 // Clears the associated registration and stop listening to it.
114 void DisassociateRegistration();
116 // Returns false if the version is not in the expected STARTING in our
117 // process state. That would be indicative of a bad IPC message.
118 bool SetHostedVersionId(int64 versions_id
);
120 // Returns a handler for a request, the handler may return NULL if
121 // the request doesn't require special handling.
122 scoped_ptr
<ServiceWorkerRequestHandler
> CreateRequestHandler(
123 FetchRequestMode request_mode
,
124 FetchCredentialsMode credentials_mode
,
125 ResourceType resource_type
,
126 RequestContextType request_context_type
,
127 RequestContextFrameType frame_type
,
128 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
129 scoped_refptr
<ResourceRequestBody
> body
);
131 // Creates a ServiceWorkerHandle to retain |version| and returns a
132 // ServiceWorkerInfo with a newly created handle ID. The handle is held in
133 // the dispatcher host until its ref-count becomes zero.
134 ServiceWorkerObjectInfo
CreateAndRegisterServiceWorkerHandle(
135 ServiceWorkerVersion
* version
);
137 // Returns true if |registration| can be associated with this provider.
138 bool CanAssociateRegistration(ServiceWorkerRegistration
* registration
);
140 // For use by the ServiceWorkerControlleeRequestHandler to disallow
141 // new registration association while a navigation is occurring and
142 // an existing registration is being looked for.
143 void SetAllowAssociation(bool allow
) { allow_association_
= allow
; }
145 // Returns true if the context referred to by this host (i.e. |context_|) is
147 bool IsContextAlive();
149 // Dispatches message event to the document.
151 const base::string16
& message
,
152 const std::vector
<TransferredMessagePort
>& sent_message_ports
);
154 // Activates the WebContents associated with
155 // { render_process_id_, render_frame_id_ }.
156 // Runs the |callback| with the updated ServiceWorkerClientInfo in parameter.
157 void Focus(const GetClientInfoCallback
& callback
);
159 // Asks the renderer to send back the document information.
160 void GetClientInfo(const GetClientInfoCallback
& callback
) const;
162 // Same as above but has to be called from the UI thread.
163 // It is taking the process and frame ids in parameter because |this| is meant
164 // to live on the IO thread.
165 static ServiceWorkerClientInfo
GetClientInfoOnUI(int render_process_id
,
166 int render_frame_id
);
168 // Adds reference of this host's process to the |pattern|, the reference will
169 // be removed in destructor.
170 void AddScopedProcessReferenceToPattern(const GURL
& pattern
);
172 // |registration| claims the document to be controlled.
173 void ClaimedByRegistration(ServiceWorkerRegistration
* registration
);
175 // Called by dispatcher host to get the registration for the "ready" property.
176 // Returns false if there's a completed or ongoing request for the document.
177 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-ready
178 bool GetRegistrationForReady(const GetRegistrationForReadyCallback
& callback
);
180 // Methods to support cross site navigations.
181 void PrepareForCrossSiteTransfer();
182 void CompleteCrossSiteTransfer(
186 ServiceWorkerProviderType new_provider_type
,
187 ServiceWorkerDispatcherHost
* dispatcher_host
);
188 ServiceWorkerDispatcherHost
* dispatcher_host() const {
189 return dispatcher_host_
;
192 // Sends event messages to the renderer. Events for the worker are queued up
193 // until the worker thread id is known via SetReadyToSendMessagesToWorker().
194 void SendUpdateFoundMessage(
195 int registration_handle_id
);
196 void SendSetVersionAttributesMessage(
197 int registration_handle_id
,
198 ChangedVersionAttributesMask changed_mask
,
199 ServiceWorkerVersion
* installing_version
,
200 ServiceWorkerVersion
* waiting_version
,
201 ServiceWorkerVersion
* active_version
);
202 void SendServiceWorkerStateChangedMessage(
203 int worker_handle_id
,
204 blink::WebServiceWorkerState state
);
206 // Sets the worker thread id and flushes queued events.
207 void SetReadyToSendMessagesToWorker(int render_thread_id
);
209 void AddMatchingRegistration(ServiceWorkerRegistration
* registration
);
210 void RemoveMatchingRegistration(ServiceWorkerRegistration
* registration
);
212 // An optimized implementation of [[Match Service Worker Registration]]
213 // for current document.
214 ServiceWorkerRegistration
* MatchRegistration() const;
217 friend class ServiceWorkerProviderHostTest
;
218 friend class ServiceWorkerWriteToCacheJobTest
;
219 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
220 UpdateBefore24Hours
);
221 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
224 struct OneShotGetReadyCallback
{
225 GetRegistrationForReadyCallback callback
;
228 explicit OneShotGetReadyCallback(
229 const GetRegistrationForReadyCallback
& callback
);
230 ~OneShotGetReadyCallback();
233 // ServiceWorkerRegistration::Listener overrides.
234 void OnVersionAttributesChanged(
235 ServiceWorkerRegistration
* registration
,
236 ChangedVersionAttributesMask changed_mask
,
237 const ServiceWorkerRegistrationInfo
& info
) override
;
238 void OnRegistrationFailed(ServiceWorkerRegistration
* registration
) override
;
239 void OnRegistrationFinishedUninstalling(
240 ServiceWorkerRegistration
* registration
) override
;
241 void OnSkippedWaiting(ServiceWorkerRegistration
* registration
) override
;
243 // Sets the controller version field to |version| or if |version| is NULL,
245 void SetControllerVersionAttribute(ServiceWorkerVersion
* version
);
247 void SendAssociateRegistrationMessage();
249 // Increase/decrease this host's process reference for |pattern|.
250 void IncreaseProcessReference(const GURL
& pattern
);
251 void DecreaseProcessReference(const GURL
& pattern
);
253 void ReturnRegistrationForReadyIfNeeded();
255 bool IsReadyToSendMessages() const;
256 void Send(IPC::Message
* message
) const;
258 std::string client_uuid_
;
259 int render_process_id_
;
260 int render_frame_id_
;
261 int render_thread_id_
;
263 ServiceWorkerProviderType provider_type_
;
265 GURL topmost_frame_url_
;
267 std::vector
<GURL
> associated_patterns_
;
268 scoped_refptr
<ServiceWorkerRegistration
> associated_registration_
;
270 // Keyed by registration scope URL length.
271 typedef std::map
<size_t, scoped_refptr
<ServiceWorkerRegistration
>>
272 ServiceWorkerRegistrationMap
;
273 // Contains all living registrations which has pattern this document's
275 ServiceWorkerRegistrationMap matching_registrations_
;
277 scoped_ptr
<OneShotGetReadyCallback
> get_ready_callback_
;
278 scoped_refptr
<ServiceWorkerVersion
> controlling_version_
;
279 scoped_refptr
<ServiceWorkerVersion
> running_hosted_version_
;
280 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
281 ServiceWorkerDispatcherHost
* dispatcher_host_
;
282 bool allow_association_
;
285 std::vector
<base::Closure
> queued_events_
;
287 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost
);
290 } // namespace content
292 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_