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_REGISTER_JOB_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/service_worker/embedded_worker_instance.h"
12 #include "content/browser/service_worker/service_worker_register_job_base.h"
13 #include "content/browser/service_worker/service_worker_registration.h"
14 #include "content/common/service_worker/service_worker_status_code.h"
19 class ServiceWorkerJobCoordinator;
20 class ServiceWorkerStorage;
22 // Handles the initial registration of a Service Worker and the
23 // subsequent update of existing registrations.
25 // The control flow includes most or all of the following,
26 // depending on what is already registered:
27 // - creating a ServiceWorkerRegistration instance if there isn't
28 // already something registered
29 // - creating a ServiceWorkerVersion for the new version.
30 // - starting a worker for the ServiceWorkerVersion
31 // - firing the 'install' event at the ServiceWorkerVersion
32 // - firing the 'activate' event at the ServiceWorkerVersion
33 // - waiting for older ServiceWorkerVersions to deactivate
34 // - designating the new version to be the 'active' version
36 class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase,
37 public EmbeddedWorkerInstance::Listener,
38 public ServiceWorkerRegistration::Listener {
40 typedef base::Callback<void(ServiceWorkerStatusCode status,
41 ServiceWorkerRegistration* registration,
42 ServiceWorkerVersion* version)>
45 // For registration jobs.
46 CONTENT_EXPORT ServiceWorkerRegisterJob(
47 base::WeakPtr<ServiceWorkerContextCore> context,
49 const GURL& script_url);
52 CONTENT_EXPORT ServiceWorkerRegisterJob(
53 base::WeakPtr<ServiceWorkerContextCore> context,
54 ServiceWorkerRegistration* registration);
55 virtual ~ServiceWorkerRegisterJob();
57 // Registers a callback to be called when the promise would resolve (whether
58 // successfully or not). Multiple callbacks may be registered.
59 // If |provider_host| is not NULL, its process will be regarded as a candidate
60 // process to run the worker.
61 void AddCallback(const RegistrationCallback& callback,
62 ServiceWorkerProviderHost* provider_host);
64 // ServiceWorkerRegisterJobBase implementation:
65 virtual void Start() OVERRIDE;
66 virtual void Abort() OVERRIDE;
67 virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE;
68 virtual RegistrationJobType GetType() OVERRIDE;
71 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
72 AssociateInstallingVersionToDocuments);
73 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
74 DisassociateVersionFromDocuments);
88 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the
89 // getter/setter functions.
93 scoped_refptr<ServiceWorkerRegistration> registration;
95 // Holds the version created by this job. It can be the 'installing',
96 // 'waiting', or 'active' version depending on the phase.
97 scoped_refptr<ServiceWorkerVersion> new_version;
99 scoped_refptr<ServiceWorkerRegistration> uninstalling_registration;
102 void set_registration(
103 const scoped_refptr<ServiceWorkerRegistration>& registration);
104 ServiceWorkerRegistration* registration();
105 void set_new_version(ServiceWorkerVersion* version);
106 ServiceWorkerVersion* new_version();
107 void set_uninstalling_registration(
108 const scoped_refptr<ServiceWorkerRegistration>& registration);
109 ServiceWorkerRegistration* uninstalling_registration();
111 void SetPhase(Phase phase);
113 void ContinueWithRegistration(
114 ServiceWorkerStatusCode status,
115 const scoped_refptr<ServiceWorkerRegistration>& registration);
116 void ContinueWithUpdate(
117 ServiceWorkerStatusCode status,
118 const scoped_refptr<ServiceWorkerRegistration>& registration);
119 void RegisterAndContinue(ServiceWorkerStatusCode status);
120 void WaitForUninstall(
121 const scoped_refptr<ServiceWorkerRegistration>& registration);
122 void ContinueWithRegistrationForSameScriptUrl(
123 const scoped_refptr<ServiceWorkerRegistration>& existing_registration,
124 ServiceWorkerStatusCode status);
125 void UpdateAndContinue();
126 void OnStartWorkerFinished(ServiceWorkerStatusCode status);
127 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status);
128 void InstallAndContinue();
129 void OnInstallFinished(ServiceWorkerStatusCode status);
130 void ActivateAndContinue();
131 void OnActivateFinished(ServiceWorkerStatusCode status);
132 void Complete(ServiceWorkerStatusCode status);
133 void CompleteInternal(ServiceWorkerStatusCode status);
134 void ResolvePromise(ServiceWorkerStatusCode status,
135 ServiceWorkerRegistration* registration,
136 ServiceWorkerVersion* version);
138 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload.
139 virtual void OnPausedAfterDownload() OVERRIDE;
140 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
142 // ServiceWorkerRegistration::Listener overrides
143 virtual void OnRegistrationFinishedUninstalling(
144 ServiceWorkerRegistration* registration) OVERRIDE;
146 void OnCompareScriptResourcesComplete(
147 ServiceWorkerVersion* most_recent_version,
148 ServiceWorkerStatusCode status,
151 void AssociateProviderHostsToRegistration(
152 ServiceWorkerRegistration* registration);
154 // The ServiceWorkerContextCore object should always outlive this.
155 base::WeakPtr<ServiceWorkerContextCore> context_;
157 RegistrationJobType job_type_;
159 const GURL script_url_;
160 std::vector<RegistrationCallback> callbacks_;
163 bool is_promise_resolved_;
164 ServiceWorkerStatusCode promise_resolved_status_;
165 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_;
166 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_;
167 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_;
169 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob);
172 } // namespace content
174 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_