Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2214)

Side by Side Diff: content/browser/service_worker/service_worker_register_job.cc

Issue 140743012: Start EmbeddedWorker during registration - take 2 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "content/browser/service_worker/service_worker_register_job.h" 5 #include "content/browser/service_worker/service_worker_register_job.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "content/browser/service_worker/service_worker_job_coordinator.h" 9 #include "content/browser/service_worker/service_worker_job_coordinator.h"
10 #include "content/browser/service_worker/service_worker_registration.h" 10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "url/gurl.h" 12 #include "url/gurl.h"
13 13
14 namespace content { 14 namespace content {
15 15
16 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( 16 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob(
17 ServiceWorkerStorage* storage, 17 ServiceWorkerStorage* storage,
18 EmbeddedWorkerRegistry* worker_registry,
18 ServiceWorkerJobCoordinator* coordinator, 19 ServiceWorkerJobCoordinator* coordinator,
19 const GURL& pattern, 20 const GURL& pattern,
20 const GURL& script_url, 21 const GURL& script_url,
21 RegistrationType type) 22 RegistrationType type)
22 : storage_(storage), 23 : storage_(storage),
24 worker_registry_(worker_registry),
23 coordinator_(coordinator), 25 coordinator_(coordinator),
26 pending_version_(NULL),
24 pattern_(pattern), 27 pattern_(pattern),
25 script_url_(script_url), 28 script_url_(script_url),
26 type_(type), 29 type_(type),
27 weak_factory_(this) {} 30 weak_factory_(this) {}
28 31
29 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {} 32 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {}
30 33
31 void ServiceWorkerRegisterJob::AddCallback( 34 void ServiceWorkerRegisterJob::AddCallback(
32 const RegistrationCallback& callback) { 35 const RegistrationCallback& callback,
36 ServiceWorkerProviderHost* source_provider) {
37 // if we've created a pending version, associate source_provider it with
38 // that, otherwise queue it up
33 callbacks_.push_back(callback); 39 callbacks_.push_back(callback);
40 DCHECK(source_provider);
41 if (pending_version_) {
42 pending_version_->OnAssociateProvider(source_provider);
kinuko 2014/01/20 09:39:09 How do we relate this to ServiceWorkerRegistration
43 } else {
44 pending_provider_hosts_.push_back(source_provider);
45 }
34 } 46 }
35 47
36 void ServiceWorkerRegisterJob::Start() { 48 void ServiceWorkerRegisterJob::Start() {
37 if (type_ == REGISTER) 49 if (type_ == REGISTER)
38 StartRegister(); 50 StartRegister();
39 else 51 else
40 StartUnregister(); 52 StartUnregister();
41 } 53 }
42 54
43 bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJob* job) { 55 bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJob* job) {
44 return job->type_ == type_ && 56 return job->type_ == type_ &&
45 (type_ == ServiceWorkerRegisterJob::UNREGISTER || 57 (type_ == ServiceWorkerRegisterJob::UNREGISTER ||
46 job->script_url_ == script_url_); 58 job->script_url_ == script_url_);
47 } 59 }
48 60
49 void ServiceWorkerRegisterJob::StartRegister() { 61 void ServiceWorkerRegisterJob::StartRegister() {
50 // Set up a chain of callbacks, in reverse order. Each of these 62 // Set up a chain of callbacks, in reverse order. Each of these
51 // callbacks may be called asynchronously by the previous callback. 63 // callbacks may be called asynchronously by the previous callback.
52 RegistrationCallback finish_registration(base::Bind( 64 base::Callback<void(ServiceWorkerRegistrationStatus,
53 &ServiceWorkerRegisterJob::RegisterComplete, weak_factory_.GetWeakPtr())); 65 const scoped_refptr<ServiceWorkerRegistration>&,
66 ServiceWorkerVersionStatus)>
67 finish_registration(
68 base::Bind(&ServiceWorkerRegisterJob::RegisterComplete,
69 weak_factory_.GetWeakPtr()));
70
71 RegistrationCallback start_worker(
72 base::Bind(&ServiceWorkerRegisterJob::StartWorkerAndContinue,
73 weak_factory_.GetWeakPtr(),
74 finish_registration));
54 75
55 UnregistrationCallback register_new( 76 UnregistrationCallback register_new(
56 base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue, 77 base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue,
57 weak_factory_.GetWeakPtr(), 78 weak_factory_.GetWeakPtr(),
58 finish_registration)); 79 start_worker));
59 80
60 ServiceWorkerStorage::FindRegistrationCallback unregister_old( 81 ServiceWorkerStorage::FindRegistrationCallback unregister_old(
61 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, 82 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue,
62 weak_factory_.GetWeakPtr(), 83 weak_factory_.GetWeakPtr(),
63 register_new)); 84 register_new));
kinuko 2014/01/28 06:34:55 While revisiting the installation flow this just c
64 85
65 storage_->FindRegistrationForPattern(pattern_, unregister_old); 86 storage_->FindRegistrationForPattern(pattern_, unregister_old);
66 } 87 }
67 88
68 void ServiceWorkerRegisterJob::StartUnregister() { 89 void ServiceWorkerRegisterJob::StartUnregister() {
69 // Set up a chain of callbacks, in reverse order. Each of these 90 // Set up a chain of callbacks, in reverse order. Each of these
70 // callbacks may be called asynchronously by the previous callback. 91 // callbacks may be called asynchronously by the previous callback.
71 UnregistrationCallback finish_unregistration( 92 UnregistrationCallback finish_unregistration(
72 base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete, 93 base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete,
73 weak_factory_.GetWeakPtr())); 94 weak_factory_.GetWeakPtr()));
(...skipping 16 matching lines...) Expand all
90 base::Bind(callback, 111 base::Bind(callback,
91 previous_status, 112 previous_status,
92 scoped_refptr<ServiceWorkerRegistration>())); 113 scoped_refptr<ServiceWorkerRegistration>()));
93 return; 114 return;
94 } 115 }
95 116
96 // TODO: Eventually RegisterInternal will be replaced by an asynchronous 117 // TODO: Eventually RegisterInternal will be replaced by an asynchronous
97 // operation. Pass its resulting status through 'callback'. 118 // operation. Pass its resulting status through 'callback'.
98 scoped_refptr<ServiceWorkerRegistration> registration = 119 scoped_refptr<ServiceWorkerRegistration> registration =
99 storage_->RegisterInternal(pattern_, script_url_); 120 storage_->RegisterInternal(pattern_, script_url_);
121
100 BrowserThread::PostTask(BrowserThread::IO, 122 BrowserThread::PostTask(BrowserThread::IO,
101 FROM_HERE, 123 FROM_HERE,
102 base::Bind(callback, REGISTRATION_OK, registration)); 124 base::Bind(callback, REGISTRATION_OK, registration));
103 } 125 }
104 126
105 void ServiceWorkerRegisterJob::UnregisterPatternAndContinue( 127 void ServiceWorkerRegisterJob::UnregisterPatternAndContinue(
106 const UnregistrationCallback& callback, 128 const UnregistrationCallback& callback,
107 bool found, 129 bool found,
108 ServiceWorkerRegistrationStatus previous_status, 130 ServiceWorkerRegistrationStatus previous_status,
109 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) { 131 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) {
110 132
111 // The previous registration may not exist, which is ok. 133 // The previous registration may not exist, which is ok.
112 if (previous_status == REGISTRATION_OK && found && 134 if (previous_status == REGISTRATION_OK && found &&
113 (script_url_.is_empty() || 135 (script_url_.is_empty() ||
114 previous_registration->script_url() != script_url_)) { 136 previous_registration->script_url() != script_url_)) {
115 // TODO: Eventually UnregisterInternal will be replaced by an 137 // TODO: Eventually UnregisterInternal will be replaced by an
116 // asynchronous operation. Pass its resulting status though 138 // asynchronous operation. Pass its resulting status though
117 // 'callback'. 139 // 'callback'.
118 storage_->UnregisterInternal(pattern_); 140 storage_->UnregisterInternal(pattern_);
119 DCHECK(previous_registration->is_shutdown()); 141 DCHECK(previous_registration->is_shutdown());
120 } 142 }
121 BrowserThread::PostTask( 143 BrowserThread::PostTask(
122 BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status)); 144 BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status));
123 } 145 }
124 146
147 void ServiceWorkerRegisterJob::StartWorkerAndContinue(
148 const base::Callback<void(ServiceWorkerRegistrationStatus,
149 const scoped_refptr<ServiceWorkerRegistration>&,
150 ServiceWorkerVersionStatus)> callback,
151 ServiceWorkerRegistrationStatus status,
152 const scoped_refptr<ServiceWorkerRegistration>& registration) {
153 pending_version_ = new ServiceWorkerVersion(
154 registration, worker_registry_, registration->next_version_id(), -1L);
155 LOG(ERROR) << "StartWorkerAndContinue...";
156 for (std::vector<ServiceWorkerProviderHost*>::const_iterator it =
157 pending_provider_hosts_.begin();
158 it != pending_provider_hosts_.end();
159 ++it)
160 pending_version_->OnAssociateProvider(*it);
kinuko 2014/01/20 09:39:09 As I wrote above I feel this association should be
161
162 pending_version_->StartWorker(base::Bind(callback, status, registration));
163 }
164
125 void ServiceWorkerRegisterJob::RunCallbacks( 165 void ServiceWorkerRegisterJob::RunCallbacks(
126 ServiceWorkerRegistrationStatus status, 166 ServiceWorkerRegistrationStatus status,
127 const scoped_refptr<ServiceWorkerRegistration>& registration) { 167 const scoped_refptr<ServiceWorkerRegistration>& registration) {
128 for (std::vector<RegistrationCallback>::iterator it = callbacks_.begin(); 168 for (std::vector<RegistrationCallback>::iterator it = callbacks_.begin();
129 it != callbacks_.end(); 169 it != callbacks_.end();
130 ++it) { 170 ++it) {
131 it->Run(status, registration); 171 it->Run(status, registration);
132 } 172 }
133 } 173 }
134 void ServiceWorkerRegisterJob::RegisterComplete( 174 void ServiceWorkerRegisterJob::RegisterComplete(
135 ServiceWorkerRegistrationStatus status, 175 ServiceWorkerRegistrationStatus status,
136 const scoped_refptr<ServiceWorkerRegistration>& registration) { 176 const scoped_refptr<ServiceWorkerRegistration>& registration,
137 RunCallbacks(status, registration); 177 ServiceWorkerVersionStatus version_status) {
178 ServiceWorkerRegistrationStatus registration_status;
179 switch (version_status) {
180 case SERVICE_WORKER_VERSION_OK:
181 registration_status = REGISTRATION_OK;
182 break;
183
184 case SERVICE_WORKER_VERSION_ERROR_FAILED:
185 case SERVICE_WORKER_VERSION_ERROR_START_FAILED:
186 case SERVICE_WORKER_VERSION_ERROR_INSTALL_FAILED:
187 registration_status = REGISTRATION_INSTALL_FAILED;
188 break;
189 }
190 RunCallbacks(registration_status, registration);
138 coordinator_->FinishJob(pattern_, this); 191 coordinator_->FinishJob(pattern_, this);
139 } 192 }
140 193
141 void ServiceWorkerRegisterJob::UnregisterComplete( 194 void ServiceWorkerRegisterJob::UnregisterComplete(
142 ServiceWorkerRegistrationStatus status) { 195 ServiceWorkerRegistrationStatus status) {
143 RunCallbacks(status, NULL); 196 RunCallbacks(status, NULL);
144 coordinator_->FinishJob(pattern_, this); 197 coordinator_->FinishJob(pattern_, this);
145 } 198 }
146 199
147 } // namespace content 200 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698