Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_provider_host.h
blob3fcd709cd4d6700e6ea1657273573ad857cec33c
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 // 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
60 // SharedWorker).
61 ServiceWorkerProviderHost(int render_process_id,
62 int route_id,
63 int provider_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_; }
72 int frame_id() const;
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
100 // loads for.
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
156 // still alive.
157 bool IsContextAlive();
159 // Dispatches message event to the document.
160 void PostMessage(
161 ServiceWorkerVersion* version,
162 const base::string16& message,
163 const std::vector<TransferredMessagePort>& sent_message_ports);
165 // Activates the WebContents associated with
166 // { render_process_id_, route_id_ }.
167 // Runs the |callback| with the updated ServiceWorkerClientInfo in parameter.
168 void Focus(const GetClientInfoCallback& callback);
170 // Asks the renderer to send back the document information.
171 void GetWindowClientInfo(const GetClientInfoCallback& callback) const;
173 // Same as above but has to be called from the UI thread.
174 // It is taking the process and frame ids in parameter because |this| is meant
175 // to live on the IO thread.
176 static ServiceWorkerClientInfo GetWindowClientInfoOnUI(int render_process_id,
177 int render_frame_id);
179 // Adds reference of this host's process to the |pattern|, the reference will
180 // be removed in destructor.
181 void AddScopedProcessReferenceToPattern(const GURL& pattern);
183 // |registration| claims the document to be controlled.
184 void ClaimedByRegistration(ServiceWorkerRegistration* registration);
186 // Called by dispatcher host to get the registration for the "ready" property.
187 // Returns false if there's a completed or ongoing request for the document.
188 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-ready
189 bool GetRegistrationForReady(const GetRegistrationForReadyCallback& callback);
191 // Methods to support cross site navigations.
192 void PrepareForCrossSiteTransfer();
193 void CompleteCrossSiteTransfer(
194 int new_process_id,
195 int new_frame_id,
196 int new_provider_id,
197 ServiceWorkerProviderType new_provider_type,
198 ServiceWorkerDispatcherHost* dispatcher_host);
199 ServiceWorkerDispatcherHost* dispatcher_host() const {
200 return dispatcher_host_;
203 // Sends event messages to the renderer. Events for the worker are queued up
204 // until the worker thread id is known via SetReadyToSendMessagesToWorker().
205 void SendUpdateFoundMessage(
206 int registration_handle_id);
207 void SendSetVersionAttributesMessage(
208 int registration_handle_id,
209 ChangedVersionAttributesMask changed_mask,
210 ServiceWorkerVersion* installing_version,
211 ServiceWorkerVersion* waiting_version,
212 ServiceWorkerVersion* active_version);
213 void SendServiceWorkerStateChangedMessage(
214 int worker_handle_id,
215 blink::WebServiceWorkerState state);
217 // Sets the worker thread id and flushes queued events.
218 void SetReadyToSendMessagesToWorker(int render_thread_id);
220 void AddMatchingRegistration(ServiceWorkerRegistration* registration);
221 void RemoveMatchingRegistration(ServiceWorkerRegistration* registration);
223 // Add matched registrations for document generated by shift-reload.
224 void AddAllMatchingRegistrations();
226 // An optimized implementation of [[Match Service Worker Registration]]
227 // for current document.
228 ServiceWorkerRegistration* MatchRegistration() const;
230 // Called when our controller has been terminated and doomed due to an
231 // exceptional condition like it could no longer be read from the script
232 // cache.
233 void NotifyControllerLost();
235 private:
236 friend class ServiceWorkerProviderHostTest;
237 friend class ServiceWorkerWriteToCacheJobTest;
238 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest, Update_SameScript);
239 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest,
240 Update_SameSizeScript);
241 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest,
242 Update_TruncatedScript);
243 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest,
244 Update_ElongatedScript);
245 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWriteToCacheJobTest,
246 Update_EmptyScript);
247 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
248 UpdateBefore24Hours);
249 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
250 UpdateAfter24Hours);
251 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
252 UpdateForceBypassCache);
254 struct OneShotGetReadyCallback {
255 GetRegistrationForReadyCallback callback;
256 bool called;
258 explicit OneShotGetReadyCallback(
259 const GetRegistrationForReadyCallback& callback);
260 ~OneShotGetReadyCallback();
263 // ServiceWorkerRegistration::Listener overrides.
264 void OnVersionAttributesChanged(
265 ServiceWorkerRegistration* registration,
266 ChangedVersionAttributesMask changed_mask,
267 const ServiceWorkerRegistrationInfo& info) override;
268 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override;
269 void OnRegistrationFinishedUninstalling(
270 ServiceWorkerRegistration* registration) override;
271 void OnSkippedWaiting(ServiceWorkerRegistration* registration) override;
273 // Sets the controller version field to |version| or if |version| is NULL,
274 // clears the field. If |notify_controllerchange| is true, instructs the
275 // renderer to dispatch a 'controller' change event.
276 void SetControllerVersionAttribute(ServiceWorkerVersion* version,
277 bool notify_controllerchange);
279 void SendAssociateRegistrationMessage();
281 // Increase/decrease this host's process reference for |pattern|.
282 void IncreaseProcessReference(const GURL& pattern);
283 void DecreaseProcessReference(const GURL& pattern);
285 void ReturnRegistrationForReadyIfNeeded();
287 bool IsReadyToSendMessages() const;
288 void Send(IPC::Message* message) const;
290 std::string client_uuid_;
291 int render_process_id_;
292 int route_id_;
293 int render_thread_id_;
294 int provider_id_;
295 ServiceWorkerProviderType provider_type_;
296 GURL document_url_;
297 GURL topmost_frame_url_;
299 std::vector<GURL> associated_patterns_;
300 scoped_refptr<ServiceWorkerRegistration> associated_registration_;
302 // Keyed by registration scope URL length.
303 typedef std::map<size_t, scoped_refptr<ServiceWorkerRegistration>>
304 ServiceWorkerRegistrationMap;
305 // Contains all living registrations which has pattern this document's
306 // URL starts with.
307 ServiceWorkerRegistrationMap matching_registrations_;
309 scoped_ptr<OneShotGetReadyCallback> get_ready_callback_;
310 scoped_refptr<ServiceWorkerVersion> controlling_version_;
311 scoped_refptr<ServiceWorkerVersion> running_hosted_version_;
312 base::WeakPtr<ServiceWorkerContextCore> context_;
313 ServiceWorkerDispatcherHost* dispatcher_host_;
314 bool allow_association_;
316 std::vector<base::Closure> queued_events_;
318 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost);
321 } // namespace content
323 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_