OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "content/browser/service_worker/embedded_worker_instance.h" | 11 #include "content/browser/service_worker/embedded_worker_instance.h" |
12 #include "content/browser/service_worker/service_worker_register_job_base.h" | 12 #include "content/browser/service_worker/service_worker_register_job_base.h" |
13 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
14 #include "content/common/service_worker/service_worker_status_code.h" | 14 #include "content/common/service_worker/service_worker_status_code.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 class ServiceWorkerJobCoordinator; | 19 class ServiceWorkerJobCoordinator; |
20 class ServiceWorkerStorage; | 20 class ServiceWorkerStorage; |
21 | 21 |
22 // Handles the registration of a Service Worker. | 22 // Handles the initial registration of a Service Worker and the |
| 23 // subsequent update of existing registrations. |
23 // | 24 // |
24 // The registration flow includes most or all of the following, | 25 // The control flow includes most or all of the following, |
25 // depending on what is already registered: | 26 // depending on what is already registered: |
26 // - creating a ServiceWorkerRegistration instance if there isn't | 27 // - creating a ServiceWorkerRegistration instance if there isn't |
27 // already something registered | 28 // already something registered |
28 // - creating a ServiceWorkerVersion for the new registration instance. | 29 // - creating a ServiceWorkerVersion for the new version. |
29 // - starting a worker for the ServiceWorkerVersion | 30 // - starting a worker for the ServiceWorkerVersion |
30 // - telling the Version to evaluate the script | |
31 // - firing the 'install' event at the ServiceWorkerVersion | 31 // - firing the 'install' event at the ServiceWorkerVersion |
32 // - firing the 'activate' event at the ServiceWorkerVersion | 32 // - firing the 'activate' event at the ServiceWorkerVersion |
33 // - waiting for older ServiceWorkerVersions to deactivate | 33 // - waiting for older ServiceWorkerVersions to deactivate |
34 // - designating the new version to be the 'active' version | 34 // - designating the new version to be the 'active' version |
| 35 // - updating storage |
35 class ServiceWorkerRegisterJob | 36 class ServiceWorkerRegisterJob |
36 : public ServiceWorkerRegisterJobBase, | 37 : public ServiceWorkerRegisterJobBase, |
37 public EmbeddedWorkerInstance::Listener { | 38 public EmbeddedWorkerInstance::Listener { |
38 public: | 39 public: |
39 typedef base::Callback<void(ServiceWorkerStatusCode status, | 40 typedef base::Callback<void(ServiceWorkerStatusCode status, |
40 ServiceWorkerRegistration* registration, | 41 ServiceWorkerRegistration* registration, |
41 ServiceWorkerVersion* version)> | 42 ServiceWorkerVersion* version)> |
42 RegistrationCallback; | 43 RegistrationCallback; |
43 | 44 |
| 45 // For registration jobs. |
44 CONTENT_EXPORT ServiceWorkerRegisterJob( | 46 CONTENT_EXPORT ServiceWorkerRegisterJob( |
45 base::WeakPtr<ServiceWorkerContextCore> context, | 47 base::WeakPtr<ServiceWorkerContextCore> context, |
46 const GURL& pattern, | 48 const GURL& pattern, |
47 const GURL& script_url); | 49 const GURL& script_url); |
| 50 |
| 51 // For update jobs. |
| 52 CONTENT_EXPORT ServiceWorkerRegisterJob( |
| 53 base::WeakPtr<ServiceWorkerContextCore> context, |
| 54 ServiceWorkerRegistration* registration); |
48 virtual ~ServiceWorkerRegisterJob(); | 55 virtual ~ServiceWorkerRegisterJob(); |
49 | 56 |
50 // Registers a callback to be called when the promise would resolve (whether | 57 // Registers a callback to be called when the promise would resolve (whether |
51 // successfully or not). Multiple callbacks may be registered. If |process_id| | 58 // successfully or not). Multiple callbacks may be registered. If |process_id| |
52 // is not -1, it's added to the existing clients when deciding in which | 59 // is not -1, it's added to the existing clients when deciding in which |
53 // process to create the Service Worker instance. If there are no existing | 60 // process to create the Service Worker instance. If there are no existing |
54 // clients, a new RenderProcessHost will be created. | 61 // clients, a new RenderProcessHost will be created. |
55 void AddCallback(const RegistrationCallback& callback, int process_id); | 62 void AddCallback(const RegistrationCallback& callback, int process_id); |
56 | 63 |
57 // ServiceWorkerRegisterJobBase implementation: | 64 // ServiceWorkerRegisterJobBase implementation: |
58 virtual void Start() OVERRIDE; | 65 virtual void Start() OVERRIDE; |
59 virtual void Abort() OVERRIDE; | 66 virtual void Abort() OVERRIDE; |
60 virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE; | 67 virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE; |
61 virtual RegistrationJobType GetType() OVERRIDE; | 68 virtual RegistrationJobType GetType() OVERRIDE; |
62 | 69 |
| 70 // TODO(michaeln): Use the registration listerer's OnVersionAttributesChanged |
| 71 // method to replace these methods, have the host listen for changes |
| 72 // to their registration. |
| 73 CONTENT_EXPORT static void AssociateInstallingVersionToDocuments( |
| 74 base::WeakPtr<ServiceWorkerContextCore> context, |
| 75 ServiceWorkerVersion* version); |
| 76 static void AssociateWaitingVersionToDocuments( |
| 77 base::WeakPtr<ServiceWorkerContextCore> context, |
| 78 ServiceWorkerVersion* version); |
| 79 static void AssociateActiveVersionToDocuments( |
| 80 base::WeakPtr<ServiceWorkerContextCore> context, |
| 81 ServiceWorkerVersion* version); |
| 82 CONTENT_EXPORT static void DisassociateVersionFromDocuments( |
| 83 base::WeakPtr<ServiceWorkerContextCore> context, |
| 84 ServiceWorkerVersion* version); |
| 85 |
63 private: | 86 private: |
64 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, | 87 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, |
65 AssociateInstallingVersionToDocuments); | 88 AssociateInstallingVersionToDocuments); |
66 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, | 89 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, |
67 DisassociateVersionFromDocuments); | 90 DisassociateVersionFromDocuments); |
68 | 91 |
69 enum Phase { | 92 enum Phase { |
70 INITIAL, | 93 INITIAL, |
71 START, | 94 START, |
72 REGISTER, | 95 REGISTER, |
(...skipping 17 matching lines...) Expand all Loading... |
90 scoped_refptr<ServiceWorkerVersion> new_version; | 113 scoped_refptr<ServiceWorkerVersion> new_version; |
91 }; | 114 }; |
92 | 115 |
93 void set_registration(ServiceWorkerRegistration* registration); | 116 void set_registration(ServiceWorkerRegistration* registration); |
94 ServiceWorkerRegistration* registration(); | 117 ServiceWorkerRegistration* registration(); |
95 void set_new_version(ServiceWorkerVersion* version); | 118 void set_new_version(ServiceWorkerVersion* version); |
96 ServiceWorkerVersion* new_version(); | 119 ServiceWorkerVersion* new_version(); |
97 | 120 |
98 void SetPhase(Phase phase); | 121 void SetPhase(Phase phase); |
99 | 122 |
100 void HandleExistingRegistrationAndContinue( | 123 void ContinueWithRegistration( |
| 124 ServiceWorkerStatusCode status, |
| 125 const scoped_refptr<ServiceWorkerRegistration>& registration); |
| 126 void ContinueWithUpdate( |
101 ServiceWorkerStatusCode status, | 127 ServiceWorkerStatusCode status, |
102 const scoped_refptr<ServiceWorkerRegistration>& registration); | 128 const scoped_refptr<ServiceWorkerRegistration>& registration); |
103 void RegisterAndContinue(ServiceWorkerStatusCode status); | 129 void RegisterAndContinue(ServiceWorkerStatusCode status); |
104 void UpdateAndContinue(ServiceWorkerStatusCode status); | 130 void UpdateAndContinue(); |
105 void OnStartWorkerFinished(ServiceWorkerStatusCode status); | 131 void OnStartWorkerFinished(ServiceWorkerStatusCode status); |
106 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status); | 132 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status); |
107 void InstallAndContinue(); | 133 void InstallAndContinue(); |
108 void OnInstallFinished(ServiceWorkerStatusCode status); | 134 void OnInstallFinished(ServiceWorkerStatusCode status); |
109 void ActivateAndContinue(); | 135 void ActivateAndContinue(); |
110 void OnActivateFinished(ServiceWorkerStatusCode status); | 136 void OnActivateFinished(ServiceWorkerStatusCode status); |
111 void Complete(ServiceWorkerStatusCode status); | 137 void Complete(ServiceWorkerStatusCode status); |
112 void CompleteInternal(ServiceWorkerStatusCode status); | 138 void CompleteInternal(ServiceWorkerStatusCode status); |
113 | |
114 void ResolvePromise(ServiceWorkerStatusCode status, | 139 void ResolvePromise(ServiceWorkerStatusCode status, |
115 ServiceWorkerRegistration* registration, | 140 ServiceWorkerRegistration* registration, |
116 ServiceWorkerVersion* version); | 141 ServiceWorkerVersion* version); |
117 | 142 |
118 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload. | 143 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload. |
119 virtual void OnPausedAfterDownload() OVERRIDE; | 144 virtual void OnPausedAfterDownload() OVERRIDE; |
120 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 145 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
121 | 146 |
122 // Associates an installing version to documents matched with a scope of the | 147 void OnCompareScriptResourcesComplete( |
123 // version. | 148 ServiceWorkerVersion* current_version, |
124 CONTENT_EXPORT static void AssociateInstallingVersionToDocuments( | 149 ServiceWorkerStatusCode status, |
125 base::WeakPtr<ServiceWorkerContextCore> context, | 150 bool are_equal); |
126 ServiceWorkerVersion* version); | |
127 | |
128 // Associates a waiting version to documents matched with a scope of the | |
129 // version. | |
130 static void AssociateWaitingVersionToDocuments( | |
131 base::WeakPtr<ServiceWorkerContextCore> context, | |
132 ServiceWorkerVersion* version); | |
133 | |
134 // Associates an active version to documents matched with a scope of the | |
135 // version. | |
136 static void AssociateActiveVersionToDocuments( | |
137 base::WeakPtr<ServiceWorkerContextCore> context, | |
138 ServiceWorkerVersion* version); | |
139 | |
140 // Disassociates a version specified by |version_id| from documents. | |
141 CONTENT_EXPORT static void DisassociateVersionFromDocuments( | |
142 base::WeakPtr<ServiceWorkerContextCore> context, | |
143 ServiceWorkerVersion* version); | |
144 | 151 |
145 // The ServiceWorkerContextCore object should always outlive this. | 152 // The ServiceWorkerContextCore object should always outlive this. |
146 base::WeakPtr<ServiceWorkerContextCore> context_; | 153 base::WeakPtr<ServiceWorkerContextCore> context_; |
147 | 154 |
| 155 RegistrationJobType job_type_; |
148 const GURL pattern_; | 156 const GURL pattern_; |
149 const GURL script_url_; | 157 const GURL script_url_; |
150 std::vector<RegistrationCallback> callbacks_; | 158 std::vector<RegistrationCallback> callbacks_; |
151 std::vector<int> pending_process_ids_; | 159 std::vector<int> pending_process_ids_; |
152 Phase phase_; | 160 Phase phase_; |
153 Internal internal_; | 161 Internal internal_; |
154 bool is_promise_resolved_; | 162 bool is_promise_resolved_; |
155 ServiceWorkerStatusCode promise_resolved_status_; | 163 ServiceWorkerStatusCode promise_resolved_status_; |
156 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_; | 164 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_; |
157 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_; | 165 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_; |
158 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; | 166 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; |
159 | 167 |
160 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); | 168 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); |
161 }; | 169 }; |
162 | 170 |
163 } // namespace content | 171 } // namespace content |
164 | 172 |
165 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ | 173 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ |
OLD | NEW |