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 bool force_bypass_cache
);
55 ~ServiceWorkerRegisterJob() override
;
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 void Start() override
;
66 void Abort() override
;
67 bool Equals(ServiceWorkerRegisterJobBase
* job
) const override
;
68 RegistrationJobType
GetType() const override
;
70 void DoomInstallingWorker();
73 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest
,
74 AssociateInstallingVersionToDocuments
);
75 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest
,
76 DisassociateVersionFromDocuments
);
89 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the
90 // getter/setter functions.
94 scoped_refptr
<ServiceWorkerRegistration
> registration
;
96 // Holds the version created by this job. It can be the 'installing',
97 // 'waiting', or 'active' version depending on the phase.
98 scoped_refptr
<ServiceWorkerVersion
> new_version
;
101 void set_registration(
102 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
103 ServiceWorkerRegistration
* registration();
104 void set_new_version(ServiceWorkerVersion
* version
);
105 ServiceWorkerVersion
* new_version();
107 void SetPhase(Phase phase
);
109 void ContinueWithRegistration(
110 ServiceWorkerStatusCode status
,
111 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
112 void ContinueWithUpdate(
113 ServiceWorkerStatusCode status
,
114 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
115 void RegisterAndContinue();
116 void ContinueWithUninstallingRegistration(
117 const scoped_refptr
<ServiceWorkerRegistration
>& existing_registration
,
118 ServiceWorkerStatusCode status
);
119 void ContinueWithRegistrationForSameScriptUrl(
120 const scoped_refptr
<ServiceWorkerRegistration
>& existing_registration
,
121 ServiceWorkerStatusCode status
);
122 void UpdateAndContinue();
123 void OnStartWorkerFinished(ServiceWorkerStatusCode status
);
124 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status
);
125 void InstallAndContinue();
126 void OnInstallFinished(ServiceWorkerStatusCode status
);
127 void ActivateAndContinue();
128 void OnActivateFinished(ServiceWorkerStatusCode status
);
129 void Complete(ServiceWorkerStatusCode status
);
130 void Complete(ServiceWorkerStatusCode status
,
131 const std::string
& status_message
);
132 void CompleteInternal(ServiceWorkerStatusCode status
,
133 const std::string
& status_message
);
134 void ResolvePromise(ServiceWorkerStatusCode status
,
135 const std::string
& status_message
,
136 ServiceWorkerRegistration
* registration
);
138 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload.
139 void OnPausedAfterDownload() override
;
140 bool OnMessageReceived(const IPC::Message
& message
) override
;
142 void OnCompareScriptResourcesComplete(
143 ServiceWorkerStatusCode status
,
146 void AddRegistrationToMatchingProviderHosts(
147 ServiceWorkerRegistration
* registration
);
149 // The ServiceWorkerContextCore object should always outlive this.
150 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
152 RegistrationJobType job_type_
;
154 const GURL script_url_
;
155 std::vector
<RegistrationCallback
> callbacks_
;
158 bool doom_installing_worker_
;
159 bool is_promise_resolved_
;
160 bool should_uninstall_on_failure_
;
161 bool force_bypass_cache_
;
162 ServiceWorkerStatusCode promise_resolved_status_
;
163 std::string promise_resolved_status_message_
;
164 scoped_refptr
<ServiceWorkerRegistration
> promise_resolved_registration_
;
165 base::WeakPtrFactory
<ServiceWorkerRegisterJob
> weak_factory_
;
167 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob
);
170 } // namespace content
172 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_