Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_register_job.h
blobfcfa47ea1f339a5ec54adfb420b7b77d7120c644
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_
8 #include <vector>
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"
15 #include "url/gurl.h"
17 namespace content {
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
35 // - updating storage
36 class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase,
37 public EmbeddedWorkerInstance::Listener,
38 public ServiceWorkerRegistration::Listener {
39 public:
40 typedef base::Callback<void(ServiceWorkerStatusCode status,
41 ServiceWorkerRegistration* registration,
42 ServiceWorkerVersion* version)>
43 RegistrationCallback;
45 // For registration jobs.
46 CONTENT_EXPORT ServiceWorkerRegisterJob(
47 base::WeakPtr<ServiceWorkerContextCore> context,
48 const GURL& pattern,
49 const GURL& script_url);
51 // For update jobs.
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. If |process_id|
59 // is not -1, it's added to the existing clients when deciding in which
60 // process to create the Service Worker instance. If there are no existing
61 // clients, a new RenderProcessHost will be created.
62 void AddCallback(const RegistrationCallback& callback, int process_id);
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;
70 private:
71 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
72 AssociateInstallingVersionToDocuments);
73 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
74 DisassociateVersionFromDocuments);
76 enum Phase {
77 INITIAL,
78 START,
79 WAIT_FOR_UNINSTALL,
80 REGISTER,
81 UPDATE,
82 INSTALL,
83 STORE,
84 COMPLETE,
85 ABORT,
88 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the
89 // getter/setter functions.
90 struct Internal {
91 Internal();
92 ~Internal();
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(ServiceWorkerRegistration* registration);
103 ServiceWorkerRegistration* registration();
104 void set_new_version(ServiceWorkerVersion* version);
105 ServiceWorkerVersion* new_version();
106 void set_uninstalling_registration(ServiceWorkerRegistration* registration);
107 ServiceWorkerRegistration* uninstalling_registration();
109 void SetPhase(Phase phase);
111 void ContinueWithRegistration(
112 ServiceWorkerStatusCode status,
113 const scoped_refptr<ServiceWorkerRegistration>& registration);
114 void ContinueWithUpdate(
115 ServiceWorkerStatusCode status,
116 const scoped_refptr<ServiceWorkerRegistration>& registration);
117 void RegisterAndContinue(ServiceWorkerStatusCode status);
118 void WaitForUninstall(
119 const scoped_refptr<ServiceWorkerRegistration>& registration);
120 void ContinueWithRegistrationForSameScriptUrl(
121 const scoped_refptr<ServiceWorkerRegistration>& existing_registration,
122 ServiceWorkerStatusCode status);
123 void UpdateAndContinue();
124 void OnStartWorkerFinished(ServiceWorkerStatusCode status);
125 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status);
126 void InstallAndContinue();
127 void OnInstallFinished(ServiceWorkerStatusCode status);
128 void ActivateAndContinue();
129 void OnActivateFinished(ServiceWorkerStatusCode status);
130 void Complete(ServiceWorkerStatusCode status);
131 void CompleteInternal(ServiceWorkerStatusCode status);
132 void ResolvePromise(ServiceWorkerStatusCode status,
133 ServiceWorkerRegistration* registration,
134 ServiceWorkerVersion* version);
136 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload.
137 virtual void OnPausedAfterDownload() OVERRIDE;
138 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
140 // ServiceWorkerRegistration::Listener overrides
141 virtual void OnRegistrationFinishedUninstalling(
142 ServiceWorkerRegistration* registration) OVERRIDE;
144 void OnCompareScriptResourcesComplete(
145 ServiceWorkerVersion* most_recent_version,
146 ServiceWorkerStatusCode status,
147 bool are_equal);
149 void AssociateProviderHostsToRegistration(
150 ServiceWorkerRegistration* registration);
152 // The ServiceWorkerContextCore object should always outlive this.
153 base::WeakPtr<ServiceWorkerContextCore> context_;
155 RegistrationJobType job_type_;
156 const GURL pattern_;
157 const GURL script_url_;
158 std::vector<RegistrationCallback> callbacks_;
159 std::vector<int> pending_process_ids_;
160 Phase phase_;
161 Internal internal_;
162 bool is_promise_resolved_;
163 ServiceWorkerStatusCode promise_resolved_status_;
164 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_;
165 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_;
166 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_;
168 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob);
171 } // namespace content
173 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_