1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// found in the LICENSE file.
4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_
6a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_
7a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/memory/weak_ptr.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/service_worker/embedded_worker_instance.h"
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/browser/service_worker/service_worker_register_job_base.h"
13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/browser/service_worker/service_worker_registration.h"
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/common/service_worker/service_worker_status_code.h"
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "url/gurl.h"
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace content {
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ServiceWorkerJobCoordinator;
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass ServiceWorkerStorage;
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Handles the initial registration of a Service Worker and the
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// subsequent update of existing registrations.
24e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The control flow includes most or all of the following,
26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// depending on what is already registered:
27e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//  - creating a ServiceWorkerRegistration instance if there isn't
28e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//    already something registered
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//  - creating a ServiceWorkerVersion for the new version.
30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//  - starting a worker for the ServiceWorkerVersion
31e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//  - firing the 'install' event at the ServiceWorkerVersion
32e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//  - firing the 'activate' event at the ServiceWorkerVersion
33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//  - waiting for older ServiceWorkerVersions to deactivate
34e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//  - designating the new version to be the 'active' version
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//  - updating storage
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase,
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                 public EmbeddedWorkerInstance::Listener,
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                 public ServiceWorkerRegistration::Listener {
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) public:
40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  typedef base::Callback<void(ServiceWorkerStatusCode status,
41c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                              ServiceWorkerRegistration* registration,
42c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                              ServiceWorkerVersion* version)>
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      RegistrationCallback;
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // For registration jobs.
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  CONTENT_EXPORT ServiceWorkerRegisterJob(
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::WeakPtr<ServiceWorkerContextCore> context,
480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      const GURL& pattern,
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      const GURL& script_url);
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // For update jobs.
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  CONTENT_EXPORT ServiceWorkerRegisterJob(
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      base::WeakPtr<ServiceWorkerContextCore> context,
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ServiceWorkerRegistration* registration);
55e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual ~ServiceWorkerRegisterJob();
56a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Registers a callback to be called when the promise would resolve (whether
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // successfully or not). Multiple callbacks may be registered.
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // If |provider_host| is not NULL, its process will be regarded as a candidate
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // process to run the worker.
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void AddCallback(const RegistrationCallback& callback,
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                   ServiceWorkerProviderHost* provider_host);
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // ServiceWorkerRegisterJobBase implementation:
65e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual void Start() OVERRIDE;
666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void Abort() OVERRIDE;
67e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE;
68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual RegistrationJobType GetType() OVERRIDE;
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                           AssociateInstallingVersionToDocuments);
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                           DisassociateVersionFromDocuments);
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
76c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  enum Phase {
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    INITIAL,
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    START,
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    WAIT_FOR_UNINSTALL,
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    REGISTER,
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    UPDATE,
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    INSTALL,
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STORE,
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    COMPLETE,
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ABORT,
86c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  };
87c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
88c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the
89c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // getter/setter functions.
90c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  struct Internal {
91c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    Internal();
92c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    ~Internal();
93c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    scoped_refptr<ServiceWorkerRegistration> registration;
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Holds the version created by this job. It can be the 'installing',
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // 'waiting', or 'active' version depending on the phase.
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_refptr<ServiceWorkerVersion> new_version;
981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_refptr<ServiceWorkerRegistration> uninstalling_registration;
100c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  };
101c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void set_registration(
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const scoped_refptr<ServiceWorkerRegistration>& registration);
104c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ServiceWorkerRegistration* registration();
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void set_new_version(ServiceWorkerVersion* version);
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ServiceWorkerVersion* new_version();
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void set_uninstalling_registration(
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const scoped_refptr<ServiceWorkerRegistration>& registration);
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceWorkerRegistration* uninstalling_registration();
110c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
111c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void SetPhase(Phase phase);
112c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void ContinueWithRegistration(
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ServiceWorkerStatusCode status,
1155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const scoped_refptr<ServiceWorkerRegistration>& registration);
1165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void ContinueWithUpdate(
117e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      ServiceWorkerStatusCode status,
118e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      const scoped_refptr<ServiceWorkerRegistration>& registration);
119e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  void RegisterAndContinue(ServiceWorkerStatusCode status);
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void WaitForUninstall(
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const scoped_refptr<ServiceWorkerRegistration>& registration);
1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void ContinueWithRegistrationForSameScriptUrl(
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const scoped_refptr<ServiceWorkerRegistration>& existing_registration,
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ServiceWorkerStatusCode status);
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void UpdateAndContinue();
126c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void OnStartWorkerFinished(ServiceWorkerStatusCode status);
1270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void OnStoreRegistrationComplete(ServiceWorkerStatusCode status);
128c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void InstallAndContinue();
129c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void OnInstallFinished(ServiceWorkerStatusCode status);
130c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void ActivateAndContinue();
131a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  void OnActivateFinished(ServiceWorkerStatusCode status);
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Complete(ServiceWorkerStatusCode status);
1336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  void CompleteInternal(ServiceWorkerStatusCode status);
1340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void ResolvePromise(ServiceWorkerStatusCode status,
1350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                      ServiceWorkerRegistration* registration,
1360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                      ServiceWorkerVersion* version);
1370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload.
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void OnPausedAfterDownload() OVERRIDE;
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // ServiceWorkerRegistration::Listener overrides
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void OnRegistrationFinishedUninstalling(
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ServiceWorkerRegistration* registration) OVERRIDE;
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void OnCompareScriptResourcesComplete(
1476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ServiceWorkerVersion* most_recent_version,
1485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ServiceWorkerStatusCode status,
1495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      bool are_equal);
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void AssociateProviderHostsToRegistration(
1526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ServiceWorkerRegistration* registration);
1536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
154c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // The ServiceWorkerContextCore object should always outlive this.
155c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::WeakPtr<ServiceWorkerContextCore> context_;
156c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
1575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  RegistrationJobType job_type_;
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const GURL pattern_;
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const GURL script_url_;
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<RegistrationCallback> callbacks_;
161c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  Phase phase_;
162c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  Internal internal_;
1630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  bool is_promise_resolved_;
1640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ServiceWorkerStatusCode promise_resolved_status_;
1650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_;
1660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  scoped_refptr<ServiceWorkerVersion> promise_resolved_version_;
167a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_;
168a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
169a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob);
170a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)};
171e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
172a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace content
173a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
174a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_
175