Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_provider_host.h
blobd73612308971ab4b7e59db9a125b03b58986c298
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_
8 #include <set>
9 #include <vector>
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"
20 namespace IPC {
21 class Sender;
24 namespace storage {
25 class BlobStorageContext;
28 namespace content {
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> {
48 public:
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
58 // SharedWorker).
59 ServiceWorkerProviderHost(int render_process_id,
60 int render_frame_id,
61 int provider_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
97 // loads for.
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_; }
109 bool IsProviderForClient() const;
110 blink::WebServiceWorkerClientType client_type() const;
112 // Associates to |registration| to listen for its version change events and
113 // sets the controller. If |notify_controllerchange| is true, instructs the
114 // renderer to dispatch a 'controllerchange' event.
115 void AssociateRegistration(ServiceWorkerRegistration* registration,
116 bool notify_controllerchange);
118 // Clears the associated registration and stop listening to it.
119 void DisassociateRegistration();
121 // Returns false if the version is not in the expected STARTING in our
122 // process state. That would be indicative of a bad IPC message.
123 bool SetHostedVersionId(int64 versions_id);
125 // Returns a handler for a request, the handler may return NULL if
126 // the request doesn't require special handling.
127 scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler(
128 FetchRequestMode request_mode,
129 FetchCredentialsMode credentials_mode,
130 ResourceType resource_type,
131 RequestContextType request_context_type,
132 RequestContextFrameType frame_type,
133 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
134 scoped_refptr<ResourceRequestBody> body);
136 // Used to get a ServiceWorkerObjectInfo to send to the renderer. Finds an
137 // existing ServiceWorkerHandle, and increments its reference count, or else
138 // creates a new one (initialized to ref count 1). Returns the
139 // ServiceWorkerInfo from the handle. The renderer is expected to use
140 // ServiceWorkerHandleReference::Adopt to balance out the ref count.
141 ServiceWorkerObjectInfo GetOrCreateServiceWorkerHandle(
142 ServiceWorkerVersion* version);
144 // Returns true if |registration| can be associated with this provider.
145 bool CanAssociateRegistration(ServiceWorkerRegistration* registration);
147 // For use by the ServiceWorkerControlleeRequestHandler to disallow
148 // new registration association while a navigation is occurring and
149 // an existing registration is being looked for.
150 void SetAllowAssociation(bool allow) { allow_association_ = allow; }
152 // Returns true if the context referred to by this host (i.e. |context_|) is
153 // still alive.
154 bool IsContextAlive();
156 // Dispatches message event to the document.
157 void PostMessage(
158 const base::string16& message,
159 const std::vector<TransferredMessagePort>& sent_message_ports);
161 // Activates the WebContents associated with
162 // { render_process_id_, render_frame_id_ }.
163 // Runs the |callback| with the updated ServiceWorkerClientInfo in parameter.
164 void Focus(const GetClientInfoCallback& callback);
166 // Asks the renderer to send back the document information.
167 void GetWindowClientInfo(const GetClientInfoCallback& callback) const;
169 // Same as above but has to be called from the UI thread.
170 // It is taking the process and frame ids in parameter because |this| is meant
171 // to live on the IO thread.
172 static ServiceWorkerClientInfo GetWindowClientInfoOnUI(int render_process_id,
173 int render_frame_id);
175 // Adds reference of this host's process to the |pattern|, the reference will
176 // be removed in destructor.
177 void AddScopedProcessReferenceToPattern(const GURL& pattern);
179 // |registration| claims the document to be controlled.
180 void ClaimedByRegistration(ServiceWorkerRegistration* registration);
182 // Called by dispatcher host to get the registration for the "ready" property.
183 // Returns false if there's a completed or ongoing request for the document.
184 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-ready
185 bool GetRegistrationForReady(const GetRegistrationForReadyCallback& callback);
187 // Methods to support cross site navigations.
188 void PrepareForCrossSiteTransfer();
189 void CompleteCrossSiteTransfer(
190 int new_process_id,
191 int new_frame_id,
192 int new_provider_id,
193 ServiceWorkerProviderType new_provider_type,
194 ServiceWorkerDispatcherHost* dispatcher_host);
195 ServiceWorkerDispatcherHost* dispatcher_host() const {
196 return dispatcher_host_;
199 // Sends event messages to the renderer. Events for the worker are queued up
200 // until the worker thread id is known via SetReadyToSendMessagesToWorker().
201 void SendUpdateFoundMessage(
202 int registration_handle_id);
203 void SendSetVersionAttributesMessage(
204 int registration_handle_id,
205 ChangedVersionAttributesMask changed_mask,
206 ServiceWorkerVersion* installing_version,
207 ServiceWorkerVersion* waiting_version,
208 ServiceWorkerVersion* active_version);
209 void SendServiceWorkerStateChangedMessage(
210 int worker_handle_id,
211 blink::WebServiceWorkerState state);
213 // Sets the worker thread id and flushes queued events.
214 void SetReadyToSendMessagesToWorker(int render_thread_id);
216 void AddMatchingRegistration(ServiceWorkerRegistration* registration);
217 void RemoveMatchingRegistration(ServiceWorkerRegistration* registration);
219 // An optimized implementation of [[Match Service Worker Registration]]
220 // for current document.
221 ServiceWorkerRegistration* MatchRegistration() const;
223 void NotifyControllerActivationFailed();
225 private:
226 friend class ServiceWorkerProviderHostTest;
227 friend class ServiceWorkerWriteToCacheJobTest;
228 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
229 UpdateBefore24Hours);
230 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
231 UpdateAfter24Hours);
233 struct OneShotGetReadyCallback {
234 GetRegistrationForReadyCallback callback;
235 bool called;
237 explicit OneShotGetReadyCallback(
238 const GetRegistrationForReadyCallback& callback);
239 ~OneShotGetReadyCallback();
242 // ServiceWorkerRegistration::Listener overrides.
243 void OnVersionAttributesChanged(
244 ServiceWorkerRegistration* registration,
245 ChangedVersionAttributesMask changed_mask,
246 const ServiceWorkerRegistrationInfo& info) override;
247 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override;
248 void OnRegistrationFinishedUninstalling(
249 ServiceWorkerRegistration* registration) override;
250 void OnSkippedWaiting(ServiceWorkerRegistration* registration) override;
252 // Sets the controller version field to |version| or if |version| is NULL,
253 // clears the field. If |notify_controllerchange| is true, instructs the
254 // renderer to dispatch a 'controller' change event.
255 void SetControllerVersionAttribute(ServiceWorkerVersion* version,
256 bool notify_controllerchange);
258 void SendAssociateRegistrationMessage();
260 // Increase/decrease this host's process reference for |pattern|.
261 void IncreaseProcessReference(const GURL& pattern);
262 void DecreaseProcessReference(const GURL& pattern);
264 void ReturnRegistrationForReadyIfNeeded();
266 bool IsReadyToSendMessages() const;
267 void Send(IPC::Message* message) const;
269 std::string client_uuid_;
270 int render_process_id_;
271 int render_frame_id_;
272 int render_thread_id_;
273 int provider_id_;
274 ServiceWorkerProviderType provider_type_;
275 GURL document_url_;
276 GURL topmost_frame_url_;
278 std::vector<GURL> associated_patterns_;
279 scoped_refptr<ServiceWorkerRegistration> associated_registration_;
281 // Keyed by registration scope URL length.
282 typedef std::map<size_t, scoped_refptr<ServiceWorkerRegistration>>
283 ServiceWorkerRegistrationMap;
284 // Contains all living registrations which has pattern this document's
285 // URL starts with.
286 ServiceWorkerRegistrationMap matching_registrations_;
288 scoped_ptr<OneShotGetReadyCallback> get_ready_callback_;
289 scoped_refptr<ServiceWorkerVersion> controlling_version_;
290 scoped_refptr<ServiceWorkerVersion> running_hosted_version_;
291 base::WeakPtr<ServiceWorkerContextCore> context_;
292 ServiceWorkerDispatcherHost* dispatcher_host_;
293 bool allow_association_;
295 std::vector<base::Closure> queued_events_;
297 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost);
300 } // namespace content
302 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_