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
{
38 typedef base::Callback
<void(ServiceWorkerStatusCode status
,
39 const std::string
& status_message
,
40 ServiceWorkerRegistration
* registration
)>
43 // For registration jobs.
44 CONTENT_EXPORT
ServiceWorkerRegisterJob(
45 base::WeakPtr
<ServiceWorkerContextCore
> context
,
47 const GURL
& script_url
);
50 CONTENT_EXPORT
ServiceWorkerRegisterJob(
51 base::WeakPtr
<ServiceWorkerContextCore
> context
,
52 ServiceWorkerRegistration
* registration
,
53 bool force_bypass_cache
);
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 void AddRegistrationToMatchingProviderHosts(
138 ServiceWorkerRegistration
* registration
);
140 // The ServiceWorkerContextCore object should always outlive this.
141 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
143 RegistrationJobType job_type_
;
145 const GURL script_url_
;
146 std::vector
<RegistrationCallback
> callbacks_
;
149 bool doom_installing_worker_
;
150 bool is_promise_resolved_
;
151 bool should_uninstall_on_failure_
;
152 bool force_bypass_cache_
;
153 ServiceWorkerStatusCode promise_resolved_status_
;
154 std::string promise_resolved_status_message_
;
155 scoped_refptr
<ServiceWorkerRegistration
> promise_resolved_registration_
;
156 base::WeakPtrFactory
<ServiceWorkerRegisterJob
> weak_factory_
;
158 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob
);
161 } // namespace content
163 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_