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 and the lifetime of this
38 // object is tied to the lifetime of its document in the renderer process.
39 // This class holds service worker state that is scoped to an individual
42 // Note this class can also host a running service worker, in which
43 // case it will observe resource loads made directly by the service worker.
44 class CONTENT_EXPORT ServiceWorkerProviderHost
45 : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener
),
46 public base::SupportsWeakPtr
<ServiceWorkerProviderHost
> {
48 using GetClientInfoCallback
=
49 base::Callback
<void(const ServiceWorkerClientInfo
&)>;
51 // If |render_frame_id| is MSG_ROUTING_NONE, this provider host works for the
52 // worker context, i.e. ServiceWorker or SharedWorker.
53 // |provider_type| gives additional information whether the provider is
54 // created for controller (ServiceWorker) or controllee (Document or
56 ServiceWorkerProviderHost(int render_process_id
,
59 ServiceWorkerProviderType provider_type
,
60 base::WeakPtr
<ServiceWorkerContextCore
> context
,
61 ServiceWorkerDispatcherHost
* dispatcher_host
);
62 virtual ~ServiceWorkerProviderHost();
64 int process_id() const { return render_process_id_
; }
65 int provider_id() const { return provider_id_
; }
66 int frame_id() const { return render_frame_id_
; }
68 bool IsHostToRunningServiceWorker() {
69 return running_hosted_version_
.get() != NULL
;
72 ServiceWorkerVersion
* controlling_version() const {
73 return controlling_version_
.get();
75 ServiceWorkerVersion
* active_version() const {
76 return associated_registration_
.get() ?
77 associated_registration_
->active_version() : NULL
;
79 ServiceWorkerVersion
* waiting_version() const {
80 return associated_registration_
.get() ?
81 associated_registration_
->waiting_version() : NULL
;
83 ServiceWorkerVersion
* installing_version() const {
84 return associated_registration_
.get() ?
85 associated_registration_
->installing_version() : NULL
;
88 ServiceWorkerRegistration
* associated_registration() const {
89 return associated_registration_
.get();
92 // The running version, if any, that this provider is providing resource
94 ServiceWorkerVersion
* running_hosted_version() const {
95 return running_hosted_version_
.get();
98 void SetDocumentUrl(const GURL
& url
);
99 const GURL
& document_url() const { return document_url_
; }
101 void SetTopmostFrameUrl(const GURL
& url
);
102 const GURL
& topmost_frame_url() const { return topmost_frame_url_
; }
104 ServiceWorkerProviderType
provider_type() const { return provider_type_
; }
106 // Associates to |registration| to listen for its version change events.
107 void AssociateRegistration(ServiceWorkerRegistration
* registration
);
109 // Clears the associated registration and stop listening to it.
110 void DisassociateRegistration();
112 // Returns false if the version is not in the expected STARTING in our
113 // process state. That would be indicative of a bad IPC message.
114 bool SetHostedVersionId(int64 versions_id
);
116 // Returns a handler for a request, the handler may return NULL if
117 // the request doesn't require special handling.
118 scoped_ptr
<ServiceWorkerRequestHandler
> CreateRequestHandler(
119 FetchRequestMode request_mode
,
120 FetchCredentialsMode credentials_mode
,
121 ResourceType resource_type
,
122 RequestContextType request_context_type
,
123 RequestContextFrameType frame_type
,
124 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
125 scoped_refptr
<ResourceRequestBody
> body
);
127 // Creates a ServiceWorkerHandle to retain |version| and returns a
128 // ServiceWorkerInfo with a newly created handle ID. The handle is held in
129 // the dispatcher host until its ref-count becomes zero.
130 ServiceWorkerObjectInfo
CreateAndRegisterServiceWorkerHandle(
131 ServiceWorkerVersion
* version
);
133 // Returns true if |registration| can be associated with this provider.
134 bool CanAssociateRegistration(ServiceWorkerRegistration
* registration
);
136 // For use by the ServiceWorkerControlleeRequestHandler to disallow
137 // new registration association while a navigation is occurring and
138 // an existing registration is being looked for.
139 void SetAllowAssociation(bool allow
) { allow_association_
= allow
; }
141 // Returns true if the context referred to by this host (i.e. |context_|) is
143 bool IsContextAlive();
145 // Dispatches message event to the document.
147 const base::string16
& message
,
148 const std::vector
<TransferredMessagePort
>& sent_message_ports
);
150 // Activates the WebContents associated with
151 // { render_process_id_, render_frame_id_ }.
152 // Runs the |callback| with the updated ServiceWorkerClientInfo in parameter.
153 void Focus(const GetClientInfoCallback
& callback
);
155 // Asks the renderer to send back the document information.
156 void GetClientInfo(const GetClientInfoCallback
& callback
) const;
158 // Adds reference of this host's process to the |pattern|, the reference will
159 // be removed in destructor.
160 void AddScopedProcessReferenceToPattern(const GURL
& pattern
);
162 // |registration| claims the document to be controlled.
163 void ClaimedByRegistration(ServiceWorkerRegistration
* registration
);
165 // Methods to support cross site navigations.
166 void PrepareForCrossSiteTransfer();
167 void CompleteCrossSiteTransfer(
171 ServiceWorkerProviderType new_provider_type
,
172 ServiceWorkerDispatcherHost
* dispatcher_host
);
173 ServiceWorkerDispatcherHost
* dispatcher_host() const {
174 return dispatcher_host_
;
177 // Sends event messages to the renderer. Events for the worker are queued up
178 // until the worker thread id is known via SetReadyToSendMessagesToWorker().
179 void SendUpdateFoundMessage(
180 const ServiceWorkerRegistrationObjectInfo
& object_info
);
181 void SendSetVersionAttributesMessage(
182 int registration_handle_id
,
183 ChangedVersionAttributesMask changed_mask
,
184 ServiceWorkerVersion
* installing_version
,
185 ServiceWorkerVersion
* waiting_version
,
186 ServiceWorkerVersion
* active_version
);
187 void SendServiceWorkerStateChangedMessage(
188 int worker_handle_id
,
189 blink::WebServiceWorkerState state
);
191 // Sets the worker thread id and flushes queued events.
192 void SetReadyToSendMessagesToWorker(int render_thread_id
);
195 friend class ServiceWorkerProviderHostTest
;
196 friend class ServiceWorkerWriteToCacheJobTest
;
197 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
198 UpdateBefore24Hours
);
199 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
202 // ServiceWorkerRegistration::Listener overrides.
203 void OnRegistrationFailed(ServiceWorkerRegistration
* registration
) override
;
204 void OnSkippedWaiting(ServiceWorkerRegistration
* registration
) override
;
206 // Sets the controller version field to |version| or if |version| is NULL,
208 void SetControllerVersionAttribute(ServiceWorkerVersion
* version
);
210 void SendAssociateRegistrationMessage();
212 // Increase/decrease this host's process reference for |pattern|.
213 void IncreaseProcessReference(const GURL
& pattern
);
214 void DecreaseProcessReference(const GURL
& pattern
);
216 bool IsReadyToSendMessages() const;
217 void Send(IPC::Message
* message
) const;
219 int render_process_id_
;
220 int render_frame_id_
;
221 int render_thread_id_
;
223 ServiceWorkerProviderType provider_type_
;
225 GURL topmost_frame_url_
;
227 std::vector
<GURL
> associated_patterns_
;
228 scoped_refptr
<ServiceWorkerRegistration
> associated_registration_
;
230 scoped_refptr
<ServiceWorkerVersion
> controlling_version_
;
231 scoped_refptr
<ServiceWorkerVersion
> running_hosted_version_
;
232 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
233 ServiceWorkerDispatcherHost
* dispatcher_host_
;
234 bool allow_association_
;
237 std::vector
<base::Closure
> queued_events_
;
239 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost
);
242 } // namespace content
244 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_