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
{
39 typedef base::Callback
<void(ServiceWorkerStatusCode status
,
40 const std::string
& status_message
,
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
) const override
;
67 RegistrationJobType
GetType() const override
;
69 void DoomInstallingWorker();
72 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest
,
73 AssociateInstallingVersionToDocuments
);
74 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest
,
75 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
;
100 void set_registration(
101 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
102 ServiceWorkerRegistration
* registration();
103 void set_new_version(ServiceWorkerVersion
* version
);
104 ServiceWorkerVersion
* new_version();
106 void SetPhase(Phase phase
);
108 void ContinueWithRegistration(
109 ServiceWorkerStatusCode status
,
110 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
111 void ContinueWithUpdate(
112 ServiceWorkerStatusCode status
,
113 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
114 void RegisterAndContinue();
115 void ContinueWithUninstallingRegistration(
116 const scoped_refptr
<ServiceWorkerRegistration
>& existing_registration
,
117 ServiceWorkerStatusCode status
);
118 void ContinueWithRegistrationForSameScriptUrl(
119 const scoped_refptr
<ServiceWorkerRegistration
>& existing_registration
,
120 ServiceWorkerStatusCode status
);
121 void UpdateAndContinue();
122 void OnStartWorkerFinished(ServiceWorkerStatusCode status
);
123 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status
);
124 void InstallAndContinue();
125 void OnInstallFinished(ServiceWorkerStatusCode status
);
126 void ActivateAndContinue();
127 void OnActivateFinished(ServiceWorkerStatusCode status
);
128 void Complete(ServiceWorkerStatusCode status
);
129 void Complete(ServiceWorkerStatusCode status
,
130 const std::string
& status_message
);
131 void CompleteInternal(ServiceWorkerStatusCode status
,
132 const std::string
& status_message
);
133 void ResolvePromise(ServiceWorkerStatusCode status
,
134 const std::string
& status_message
,
135 ServiceWorkerRegistration
* registration
);
137 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload.
138 void OnPausedAfterDownload() override
;
139 bool OnMessageReceived(const IPC::Message
& message
) override
;
141 void OnCompareScriptResourcesComplete(
142 ServiceWorkerStatusCode status
,
145 void AddRegistrationToMatchingProviderHosts(
146 ServiceWorkerRegistration
* registration
);
148 // The ServiceWorkerContextCore object should always outlive this.
149 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
151 RegistrationJobType job_type_
;
153 const GURL script_url_
;
154 std::vector
<RegistrationCallback
> callbacks_
;
157 bool doom_installing_worker_
;
158 bool is_promise_resolved_
;
159 bool should_uninstall_on_failure_
;
160 ServiceWorkerStatusCode promise_resolved_status_
;
161 std::string promise_resolved_status_message_
;
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_