Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_register_job.h
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.
4
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_
7
8 #include <vector>
9
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"
16
17 namespace content {
18
19 class ServiceWorkerJobCoordinator;
20 class ServiceWorkerStorage;
21
22 // Handles the initial registration of a Service Worker and the
23 // subsequent update of existing registrations.
24 //
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       RegistrationCallback;
43
44   // For registration jobs.
45   CONTENT_EXPORT ServiceWorkerRegisterJob(
46       base::WeakPtr<ServiceWorkerContextCore> context,
47       const GURL& pattern,
48       const GURL& script_url);
49
50   // For update jobs.
51   CONTENT_EXPORT ServiceWorkerRegisterJob(
52       base::WeakPtr<ServiceWorkerContextCore> context,
53       ServiceWorkerRegistration* registration);
54   ~ServiceWorkerRegisterJob() override;
55
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);
62
63   // ServiceWorkerRegisterJobBase implementation:
64   void Start() override;
65   void Abort() override;
66   bool Equals(ServiceWorkerRegisterJobBase* job) override;
67   RegistrationJobType GetType() override;
68
69  private:
70   FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
71                            AssociateInstallingVersionToDocuments);
72   FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest,
73                            DisassociateVersionFromDocuments);
74
75   enum Phase {
76     INITIAL,
77     START,
78     WAIT_FOR_UNINSTALL,
79     REGISTER,
80     UPDATE,
81     INSTALL,
82     STORE,
83     COMPLETE,
84     ABORT,
85   };
86
87   // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the
88   // getter/setter functions.
89   struct Internal {
90     Internal();
91     ~Internal();
92     scoped_refptr<ServiceWorkerRegistration> registration;
93
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;
97
98     scoped_refptr<ServiceWorkerRegistration> uninstalling_registration;
99   };
100
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();
109
110   void SetPhase(Phase phase);
111
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);
135
136   // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload.
137   void OnPausedAfterDownload() override;
138   bool OnMessageReceived(const IPC::Message& message) override;
139
140   // ServiceWorkerRegistration::Listener overrides
141   void OnRegistrationFinishedUninstalling(
142       ServiceWorkerRegistration* registration) override;
143
144   void OnCompareScriptResourcesComplete(
145       ServiceWorkerStatusCode status,
146       bool are_equal);
147
148   void AssociateProviderHostsToRegistration(
149       ServiceWorkerRegistration* registration);
150
151   // The ServiceWorkerContextCore object should always outlive this.
152   base::WeakPtr<ServiceWorkerContextCore> context_;
153
154   RegistrationJobType job_type_;
155   const GURL pattern_;
156   const GURL script_url_;
157   std::vector<RegistrationCallback> callbacks_;
158   Phase phase_;
159   Internal internal_;
160   bool is_promise_resolved_;
161   ServiceWorkerStatusCode promise_resolved_status_;
162   scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_;
163   base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_;
164
165   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob);
166 };
167
168 }  // namespace content
169
170 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_