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)>
44 // For registration jobs.
45 CONTENT_EXPORT ServiceWorkerRegisterJob(
46 base::WeakPtr<ServiceWorkerContextCore> context,
48 const GURL& script_url);
51 CONTENT_EXPORT ServiceWorkerRegisterJob(
52 base::WeakPtr<ServiceWorkerContextCore> context,
53 ServiceWorkerRegistration* registration);
54 ~ServiceWorkerRegisterJob() override;
56 // Registers a callback to be called when the promise would resolve (whether
57 // successfully or not). Multiple callbacks may be registered.
58 // If |provider_host| is not NULL, its process will be regarded as a candidate
59 // process to run the worker.
60 void AddCallback(const RegistrationCallback& callback,
61 ServiceWorkerProviderHost* provider_host);
63 // ServiceWorkerRegisterJobBase implementation:
64 void Start() override;
65 void Abort() override;
66 bool Equals(ServiceWorkerRegisterJobBase* job) override;
67 RegistrationJobType GetType() override;
70 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
71 AssociateInstallingVersionToDocuments);
72 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
73 DisassociateVersionFromDocuments);
87 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the
88 // getter/setter functions.
92 scoped_refptr<ServiceWorkerRegistration> registration;
94 // Holds the version created by this job. It can be the 'installing',
95 // 'waiting', or 'active' version depending on the phase.
96 scoped_refptr<ServiceWorkerVersion> new_version;
98 scoped_refptr<ServiceWorkerRegistration> uninstalling_registration;
101 void set_registration(
102 const scoped_refptr<ServiceWorkerRegistration>& registration);
103 ServiceWorkerRegistration* registration();
104 void set_new_version(ServiceWorkerVersion* version);
105 ServiceWorkerVersion* new_version();
106 void set_uninstalling_registration(
107 const scoped_refptr<ServiceWorkerRegistration>& registration);
108 ServiceWorkerRegistration* uninstalling_registration();
110 void SetPhase(Phase phase);
112 void ContinueWithRegistration(
113 ServiceWorkerStatusCode status,
114 const scoped_refptr<ServiceWorkerRegistration>& registration);
115 void ContinueWithUpdate(
116 ServiceWorkerStatusCode status,
117 const scoped_refptr<ServiceWorkerRegistration>& registration);
118 void RegisterAndContinue(ServiceWorkerStatusCode status);
119 void WaitForUninstall(
120 const scoped_refptr<ServiceWorkerRegistration>& registration);
121 void ContinueWithRegistrationForSameScriptUrl(
122 const scoped_refptr<ServiceWorkerRegistration>& existing_registration,
123 ServiceWorkerStatusCode status);
124 void UpdateAndContinue();
125 void OnStartWorkerFinished(ServiceWorkerStatusCode status);
126 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status);
127 void InstallAndContinue();
128 void OnInstallFinished(ServiceWorkerStatusCode status);
129 void ActivateAndContinue();
130 void OnActivateFinished(ServiceWorkerStatusCode status);
131 void Complete(ServiceWorkerStatusCode status);
132 void CompleteInternal(ServiceWorkerStatusCode status);
133 void ResolvePromise(ServiceWorkerStatusCode status,
134 ServiceWorkerRegistration* registration);
136 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload.
137 void OnPausedAfterDownload() override;
138 bool OnMessageReceived(const IPC::Message& message) override;
140 // ServiceWorkerRegistration::Listener overrides
141 void OnRegistrationFinishedUninstalling(
142 ServiceWorkerRegistration* registration) override;
144 void OnCompareScriptResourcesComplete(
145 ServiceWorkerStatusCode status,
148 void AssociateProviderHostsToRegistration(
149 ServiceWorkerRegistration* registration);
151 // The ServiceWorkerContextCore object should always outlive this.
152 base::WeakPtr<ServiceWorkerContextCore> context_;
154 RegistrationJobType job_type_;
156 const GURL script_url_;
157 std::vector<RegistrationCallback> callbacks_;
160 bool is_promise_resolved_;
161 ServiceWorkerStatusCode promise_resolved_status_;
162 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_;
163 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_;
165 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob);
168 } // namespace content
170 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_