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 #include "content/browser/service_worker/service_worker_storage.h" | 5 #include "content/browser/service_worker/service_worker_storage.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 lhs_buffer_ = new net::IOBuffer(kBufferSize); | 117 lhs_buffer_ = new net::IOBuffer(kBufferSize); |
118 lhs_info_ = new HttpResponseInfoIOBuffer(); | 118 lhs_info_ = new HttpResponseInfoIOBuffer(); |
119 rhs_buffer_ = new net::IOBuffer(kBufferSize); | 119 rhs_buffer_ = new net::IOBuffer(kBufferSize); |
120 rhs_info_ = new HttpResponseInfoIOBuffer(); | 120 rhs_info_ = new HttpResponseInfoIOBuffer(); |
121 | 121 |
122 ReadInfos(); | 122 ReadInfos(); |
123 } | 123 } |
124 | 124 |
125 void ResponseComparer::ReadInfos() { | 125 void ResponseComparer::ReadInfos() { |
126 lhs_reader_->ReadInfo( | 126 lhs_reader_->ReadInfo( |
127 lhs_info_, | 127 lhs_info_.get(), base::Bind(&ResponseComparer::OnReadInfoComplete, this)); |
128 base::Bind(&ResponseComparer::OnReadInfoComplete, | |
129 this)); | |
130 rhs_reader_->ReadInfo( | 128 rhs_reader_->ReadInfo( |
131 rhs_info_, | 129 rhs_info_.get(), base::Bind(&ResponseComparer::OnReadInfoComplete, this)); |
132 base::Bind(&ResponseComparer::OnReadInfoComplete, | |
133 this)); | |
134 } | 130 } |
135 | 131 |
136 void ResponseComparer::OnReadInfoComplete(int result) { | 132 void ResponseComparer::OnReadInfoComplete(int result) { |
137 if (completion_callback_.is_null() || !owner_) | 133 if (completion_callback_.is_null() || !owner_) |
138 return; | 134 return; |
139 if (result < 0) { | 135 if (result < 0) { |
140 completion_callback_.Run(SERVICE_WORKER_ERROR_FAILED, false); | 136 completion_callback_.Run(SERVICE_WORKER_ERROR_FAILED, false); |
141 completion_callback_.Reset(); | 137 completion_callback_.Reset(); |
142 return; | 138 return; |
143 } | 139 } |
144 if (++completion_count_ != 2) | 140 if (++completion_count_ != 2) |
145 return; | 141 return; |
146 | 142 |
147 if (lhs_info_->response_data_size != rhs_info_->response_data_size) { | 143 if (lhs_info_->response_data_size != rhs_info_->response_data_size) { |
148 completion_callback_.Run(SERVICE_WORKER_OK, false); | 144 completion_callback_.Run(SERVICE_WORKER_OK, false); |
149 return; | 145 return; |
150 } | 146 } |
151 ReadSomeData(); | 147 ReadSomeData(); |
152 } | 148 } |
153 | 149 |
154 void ResponseComparer::ReadSomeData() { | 150 void ResponseComparer::ReadSomeData() { |
155 completion_count_ = 0; | 151 completion_count_ = 0; |
156 lhs_reader_->ReadData( | 152 lhs_reader_->ReadData( |
157 lhs_buffer_, | 153 lhs_buffer_.get(), |
158 kBufferSize, | 154 kBufferSize, |
159 base::Bind(&ResponseComparer::OnReadDataComplete, this)); | 155 base::Bind(&ResponseComparer::OnReadDataComplete, this)); |
160 rhs_reader_->ReadData( | 156 rhs_reader_->ReadData( |
161 rhs_buffer_, | 157 rhs_buffer_.get(), |
162 kBufferSize, | 158 kBufferSize, |
163 base::Bind(&ResponseComparer::OnReadDataComplete, this)); | 159 base::Bind(&ResponseComparer::OnReadDataComplete, this)); |
164 } | 160 } |
165 | 161 |
166 void ResponseComparer::OnReadDataComplete(int result) { | 162 void ResponseComparer::OnReadDataComplete(int result) { |
167 if (completion_callback_.is_null() || !owner_) | 163 if (completion_callback_.is_null() || !owner_) |
168 return; | 164 return; |
169 if (result < 0) { | 165 if (result < 0) { |
170 completion_callback_.Run(SERVICE_WORKER_ERROR_FAILED, false); | 166 completion_callback_.Run(SERVICE_WORKER_ERROR_FAILED, false); |
171 completion_callback_.Reset(); | 167 completion_callback_.Reset(); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 236 |
241 // static | 237 // static |
242 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( | 238 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( |
243 base::WeakPtr<ServiceWorkerContextCore> context, | 239 base::WeakPtr<ServiceWorkerContextCore> context, |
244 ServiceWorkerStorage* old_storage) { | 240 ServiceWorkerStorage* old_storage) { |
245 return make_scoped_ptr( | 241 return make_scoped_ptr( |
246 new ServiceWorkerStorage(old_storage->path_, | 242 new ServiceWorkerStorage(old_storage->path_, |
247 context, | 243 context, |
248 old_storage->database_task_runner_, | 244 old_storage->database_task_runner_, |
249 old_storage->disk_cache_thread_, | 245 old_storage->disk_cache_thread_, |
250 old_storage->quota_manager_proxy_)); | 246 old_storage->quota_manager_proxy_.get())); |
251 } | 247 } |
252 | 248 |
253 void ServiceWorkerStorage::FindRegistrationForDocument( | 249 void ServiceWorkerStorage::FindRegistrationForDocument( |
254 const GURL& document_url, | 250 const GURL& document_url, |
255 const FindRegistrationCallback& callback) { | 251 const FindRegistrationCallback& callback) { |
256 DCHECK(!document_url.has_ref()); | 252 DCHECK(!document_url.has_ref()); |
257 if (!LazyInitialize(base::Bind( | 253 if (!LazyInitialize(base::Bind( |
258 &ServiceWorkerStorage::FindRegistrationForDocument, | 254 &ServiceWorkerStorage::FindRegistrationForDocument, |
259 weak_factory_.GetWeakPtr(), document_url, callback))) { | 255 weak_factory_.GetWeakPtr(), document_url, callback))) { |
260 if (state_ != INITIALIZING || !context_) { | 256 if (state_ != INITIALIZING || !context_) { |
261 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), | 257 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), |
262 SERVICE_WORKER_ERROR_FAILED, callback); | 258 SERVICE_WORKER_ERROR_FAILED, callback); |
263 } | 259 } |
264 return; | 260 return; |
265 } | 261 } |
266 DCHECK_EQ(INITIALIZED, state_); | 262 DCHECK_EQ(INITIALIZED, state_); |
267 | 263 |
268 // See if there are any stored registrations for the origin. | 264 // See if there are any stored registrations for the origin. |
269 if (!ContainsKey(registered_origins_, document_url.GetOrigin())) { | 265 if (!ContainsKey(registered_origins_, document_url.GetOrigin())) { |
270 // Look for something currently being installed. | 266 // Look for something currently being installed. |
271 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 267 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
272 FindInstallingRegistrationForDocument(document_url); | 268 FindInstallingRegistrationForDocument(document_url); |
273 CompleteFindNow( | 269 CompleteFindNow(installing_registration, |
274 installing_registration, | 270 installing_registration.get() |
275 installing_registration ? | 271 ? SERVICE_WORKER_OK |
276 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 272 : SERVICE_WORKER_ERROR_NOT_FOUND, |
277 callback); | 273 callback); |
278 return; | 274 return; |
279 } | 275 } |
280 | 276 |
281 database_task_runner_->PostTask( | 277 database_task_runner_->PostTask( |
282 FROM_HERE, | 278 FROM_HERE, |
283 base::Bind( | 279 base::Bind( |
284 &FindForDocumentInDB, | 280 &FindForDocumentInDB, |
285 database_.get(), | 281 database_.get(), |
286 base::MessageLoopProxy::current(), | 282 base::MessageLoopProxy::current(), |
287 document_url, | 283 document_url, |
(...skipping 13 matching lines...) Expand all Loading... |
301 } | 297 } |
302 return; | 298 return; |
303 } | 299 } |
304 DCHECK_EQ(INITIALIZED, state_); | 300 DCHECK_EQ(INITIALIZED, state_); |
305 | 301 |
306 // See if there are any stored registrations for the origin. | 302 // See if there are any stored registrations for the origin. |
307 if (!ContainsKey(registered_origins_, scope.GetOrigin())) { | 303 if (!ContainsKey(registered_origins_, scope.GetOrigin())) { |
308 // Look for something currently being installed. | 304 // Look for something currently being installed. |
309 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 305 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
310 FindInstallingRegistrationForPattern(scope); | 306 FindInstallingRegistrationForPattern(scope); |
311 CompleteFindSoon( | 307 CompleteFindSoon(FROM_HERE, |
312 FROM_HERE, installing_registration, | 308 installing_registration, |
313 installing_registration ? | 309 installing_registration.get() |
314 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 310 ? SERVICE_WORKER_OK |
315 callback); | 311 : SERVICE_WORKER_ERROR_NOT_FOUND, |
| 312 callback); |
316 return; | 313 return; |
317 } | 314 } |
318 | 315 |
319 database_task_runner_->PostTask( | 316 database_task_runner_->PostTask( |
320 FROM_HERE, | 317 FROM_HERE, |
321 base::Bind( | 318 base::Bind( |
322 &FindForPatternInDB, | 319 &FindForPatternInDB, |
323 database_.get(), | 320 database_.get(), |
324 base::MessageLoopProxy::current(), | 321 base::MessageLoopProxy::current(), |
325 scope, | 322 scope, |
326 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern, | 323 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern, |
327 weak_factory_.GetWeakPtr(), scope, callback))); | 324 weak_factory_.GetWeakPtr(), scope, callback))); |
328 } | 325 } |
329 | 326 |
330 ServiceWorkerRegistration* ServiceWorkerStorage::GetUninstallingRegistration( | 327 ServiceWorkerRegistration* ServiceWorkerStorage::GetUninstallingRegistration( |
331 const GURL& scope) { | 328 const GURL& scope) { |
332 if (state_ != INITIALIZED || !context_) | 329 if (state_ != INITIALIZED || !context_) |
333 return NULL; | 330 return NULL; |
334 for (RegistrationRefsById::const_iterator it = | 331 for (RegistrationRefsById::const_iterator it = |
335 uninstalling_registrations_.begin(); | 332 uninstalling_registrations_.begin(); |
336 it != uninstalling_registrations_.end(); | 333 it != uninstalling_registrations_.end(); |
337 ++it) { | 334 ++it) { |
338 if (it->second->pattern() == scope) { | 335 if (it->second->pattern() == scope) { |
339 DCHECK(it->second->is_uninstalling()); | 336 DCHECK(it->second->is_uninstalling()); |
340 return it->second; | 337 return it->second.get(); |
341 } | 338 } |
342 } | 339 } |
343 return NULL; | 340 return NULL; |
344 } | 341 } |
345 | 342 |
346 void ServiceWorkerStorage::FindRegistrationForId( | 343 void ServiceWorkerStorage::FindRegistrationForId( |
347 int64 registration_id, | 344 int64 registration_id, |
348 const GURL& origin, | 345 const GURL& origin, |
349 const FindRegistrationCallback& callback) { | 346 const FindRegistrationCallback& callback) { |
350 if (!LazyInitialize(base::Bind( | 347 if (!LazyInitialize(base::Bind( |
351 &ServiceWorkerStorage::FindRegistrationForId, | 348 &ServiceWorkerStorage::FindRegistrationForId, |
352 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { | 349 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { |
353 if (state_ != INITIALIZING || !context_) { | 350 if (state_ != INITIALIZING || !context_) { |
354 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), | 351 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), |
355 SERVICE_WORKER_ERROR_FAILED, callback); | 352 SERVICE_WORKER_ERROR_FAILED, callback); |
356 } | 353 } |
357 return; | 354 return; |
358 } | 355 } |
359 DCHECK_EQ(INITIALIZED, state_); | 356 DCHECK_EQ(INITIALIZED, state_); |
360 | 357 |
361 // See if there are any stored registrations for the origin. | 358 // See if there are any stored registrations for the origin. |
362 if (!ContainsKey(registered_origins_, origin)) { | 359 if (!ContainsKey(registered_origins_, origin)) { |
363 // Look for something currently being installed. | 360 // Look for something currently being installed. |
364 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 361 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
365 FindInstallingRegistrationForId(registration_id); | 362 FindInstallingRegistrationForId(registration_id); |
366 CompleteFindNow( | 363 CompleteFindNow(installing_registration, |
367 installing_registration, | 364 installing_registration.get() |
368 installing_registration ? | 365 ? SERVICE_WORKER_OK |
369 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 366 : SERVICE_WORKER_ERROR_NOT_FOUND, |
370 callback); | 367 callback); |
371 return; | 368 return; |
372 } | 369 } |
373 | 370 |
374 scoped_refptr<ServiceWorkerRegistration> registration = | 371 scoped_refptr<ServiceWorkerRegistration> registration = |
375 context_->GetLiveRegistration(registration_id); | 372 context_->GetLiveRegistration(registration_id); |
376 if (registration) { | 373 if (registration.get()) { |
377 CompleteFindNow(registration, SERVICE_WORKER_OK, callback); | 374 CompleteFindNow(registration, SERVICE_WORKER_OK, callback); |
378 return; | 375 return; |
379 } | 376 } |
380 | 377 |
381 database_task_runner_->PostTask( | 378 database_task_runner_->PostTask( |
382 FROM_HERE, | 379 FROM_HERE, |
383 base::Bind(&FindForIdInDB, | 380 base::Bind(&FindForIdInDB, |
384 database_.get(), | 381 database_.get(), |
385 base::MessageLoopProxy::current(), | 382 base::MessageLoopProxy::current(), |
386 registration_id, origin, | 383 registration_id, origin, |
387 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForId, | 384 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForId, |
388 weak_factory_.GetWeakPtr(), callback))); | 385 weak_factory_.GetWeakPtr(), callback))); |
389 } | 386 } |
390 | 387 |
391 void ServiceWorkerStorage::GetAllRegistrations( | 388 void ServiceWorkerStorage::GetAllRegistrations( |
392 const GetAllRegistrationInfosCallback& callback) { | 389 const GetAllRegistrationInfosCallback& callback) { |
393 if (!LazyInitialize(base::Bind( | 390 if (!LazyInitialize(base::Bind( |
394 &ServiceWorkerStorage::GetAllRegistrations, | 391 &ServiceWorkerStorage::GetAllRegistrations, |
395 weak_factory_.GetWeakPtr(), callback))) { | 392 weak_factory_.GetWeakPtr(), callback))) { |
396 if (state_ != INITIALIZING || !context_) { | 393 if (state_ != INITIALIZING || !context_) { |
397 RunSoon(FROM_HERE, base::Bind( | 394 RunSoon(FROM_HERE, base::Bind( |
398 callback, std::vector<ServiceWorkerRegistrationInfo>())); | 395 callback, std::vector<ServiceWorkerRegistrationInfo>())); |
399 } | 396 } |
400 return; | 397 return; |
401 } | 398 } |
402 DCHECK_EQ(INITIALIZED, state_); | 399 DCHECK_EQ(INITIALIZED, state_); |
403 | 400 |
404 RegistrationList* registrations = new RegistrationList; | 401 RegistrationList* registrations = new RegistrationList; |
405 PostTaskAndReplyWithResult( | 402 PostTaskAndReplyWithResult( |
406 database_task_runner_, | 403 database_task_runner_.get(), |
407 FROM_HERE, | 404 FROM_HERE, |
408 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, | 405 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, |
409 base::Unretained(database_.get()), | 406 base::Unretained(database_.get()), |
410 base::Unretained(registrations)), | 407 base::Unretained(registrations)), |
411 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations, | 408 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations, |
412 weak_factory_.GetWeakPtr(), | 409 weak_factory_.GetWeakPtr(), |
413 callback, | 410 callback, |
414 base::Owned(registrations))); | 411 base::Owned(registrations))); |
415 } | 412 } |
416 | 413 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 const StatusCallback& callback) { | 457 const StatusCallback& callback) { |
461 DCHECK(registration); | 458 DCHECK(registration); |
462 | 459 |
463 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; | 460 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; |
464 if (IsDisabled() || !context_) { | 461 if (IsDisabled() || !context_) { |
465 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 462 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
466 return; | 463 return; |
467 } | 464 } |
468 | 465 |
469 PostTaskAndReplyWithResult( | 466 PostTaskAndReplyWithResult( |
470 database_task_runner_, | 467 database_task_runner_.get(), |
471 FROM_HERE, | 468 FROM_HERE, |
472 base::Bind(&ServiceWorkerDatabase::UpdateVersionToActive, | 469 base::Bind(&ServiceWorkerDatabase::UpdateVersionToActive, |
473 base::Unretained(database_.get()), | 470 base::Unretained(database_.get()), |
474 registration->id(), | 471 registration->id(), |
475 registration->script_url().GetOrigin()), | 472 registration->script_url().GetOrigin()), |
476 base::Bind(&ServiceWorkerStorage::DidUpdateToActiveState, | 473 base::Bind(&ServiceWorkerStorage::DidUpdateToActiveState, |
477 weak_factory_.GetWeakPtr(), | 474 weak_factory_.GetWeakPtr(), |
478 callback)); | 475 callback)); |
479 } | 476 } |
480 | 477 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 CreateResponseReader(rhs_id), | 576 CreateResponseReader(rhs_id), |
580 callback); | 577 callback); |
581 comparer->Start(); // It deletes itself when done. | 578 comparer->Start(); // It deletes itself when done. |
582 } | 579 } |
583 | 580 |
584 void ServiceWorkerStorage::DeleteAndStartOver(const StatusCallback& callback) { | 581 void ServiceWorkerStorage::DeleteAndStartOver(const StatusCallback& callback) { |
585 Disable(); | 582 Disable(); |
586 | 583 |
587 // Delete the database on the database thread. | 584 // Delete the database on the database thread. |
588 PostTaskAndReplyWithResult( | 585 PostTaskAndReplyWithResult( |
589 database_task_runner_, | 586 database_task_runner_.get(), |
590 FROM_HERE, | 587 FROM_HERE, |
591 base::Bind(&ServiceWorkerDatabase::DestroyDatabase, | 588 base::Bind(&ServiceWorkerDatabase::DestroyDatabase, |
592 base::Unretained(database_.get())), | 589 base::Unretained(database_.get())), |
593 base::Bind(&ServiceWorkerStorage::DidDeleteDatabase, | 590 base::Bind(&ServiceWorkerStorage::DidDeleteDatabase, |
594 weak_factory_.GetWeakPtr(), callback)); | 591 weak_factory_.GetWeakPtr(), |
| 592 callback)); |
595 } | 593 } |
596 | 594 |
597 int64 ServiceWorkerStorage::NewRegistrationId() { | 595 int64 ServiceWorkerStorage::NewRegistrationId() { |
598 if (state_ == DISABLED) | 596 if (state_ == DISABLED) |
599 return kInvalidServiceWorkerRegistrationId; | 597 return kInvalidServiceWorkerRegistrationId; |
600 DCHECK_EQ(INITIALIZED, state_); | 598 DCHECK_EQ(INITIALIZED, state_); |
601 return next_registration_id_++; | 599 return next_registration_id_++; |
602 } | 600 } |
603 | 601 |
604 int64 ServiceWorkerStorage::NewVersionId() { | 602 int64 ServiceWorkerStorage::NewVersionId() { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 ServiceWorkerDatabase::Status status) { | 767 ServiceWorkerDatabase::Status status) { |
770 if (status == ServiceWorkerDatabase::STATUS_OK) { | 768 if (status == ServiceWorkerDatabase::STATUS_OK) { |
771 ReturnFoundRegistration(callback, data, resources); | 769 ReturnFoundRegistration(callback, data, resources); |
772 return; | 770 return; |
773 } | 771 } |
774 | 772 |
775 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 773 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
776 // Look for something currently being installed. | 774 // Look for something currently being installed. |
777 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 775 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
778 FindInstallingRegistrationForDocument(document_url); | 776 FindInstallingRegistrationForDocument(document_url); |
779 callback.Run(installing_registration ? | 777 callback.Run(installing_registration.get() ? SERVICE_WORKER_OK |
780 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 778 : SERVICE_WORKER_ERROR_NOT_FOUND, |
781 installing_registration); | 779 installing_registration); |
782 return; | 780 return; |
783 } | 781 } |
784 | 782 |
785 ScheduleDeleteAndStartOver(); | 783 ScheduleDeleteAndStartOver(); |
786 callback.Run(DatabaseStatusToStatusCode(status), | 784 callback.Run(DatabaseStatusToStatusCode(status), |
787 scoped_refptr<ServiceWorkerRegistration>()); | 785 scoped_refptr<ServiceWorkerRegistration>()); |
788 } | 786 } |
789 | 787 |
790 void ServiceWorkerStorage::DidFindRegistrationForPattern( | 788 void ServiceWorkerStorage::DidFindRegistrationForPattern( |
791 const GURL& scope, | 789 const GURL& scope, |
792 const FindRegistrationCallback& callback, | 790 const FindRegistrationCallback& callback, |
793 const ServiceWorkerDatabase::RegistrationData& data, | 791 const ServiceWorkerDatabase::RegistrationData& data, |
794 const ResourceList& resources, | 792 const ResourceList& resources, |
795 ServiceWorkerDatabase::Status status) { | 793 ServiceWorkerDatabase::Status status) { |
796 if (status == ServiceWorkerDatabase::STATUS_OK) { | 794 if (status == ServiceWorkerDatabase::STATUS_OK) { |
797 ReturnFoundRegistration(callback, data, resources); | 795 ReturnFoundRegistration(callback, data, resources); |
798 return; | 796 return; |
799 } | 797 } |
800 | 798 |
801 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 799 if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
802 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 800 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
803 FindInstallingRegistrationForPattern(scope); | 801 FindInstallingRegistrationForPattern(scope); |
804 callback.Run(installing_registration ? | 802 callback.Run(installing_registration.get() ? SERVICE_WORKER_OK |
805 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 803 : SERVICE_WORKER_ERROR_NOT_FOUND, |
806 installing_registration); | 804 installing_registration); |
807 return; | 805 return; |
808 } | 806 } |
809 | 807 |
810 ScheduleDeleteAndStartOver(); | 808 ScheduleDeleteAndStartOver(); |
811 callback.Run(DatabaseStatusToStatusCode(status), | 809 callback.Run(DatabaseStatusToStatusCode(status), |
812 scoped_refptr<ServiceWorkerRegistration>()); | 810 scoped_refptr<ServiceWorkerRegistration>()); |
813 } | 811 } |
814 | 812 |
815 void ServiceWorkerStorage::DidFindRegistrationForId( | 813 void ServiceWorkerStorage::DidFindRegistrationForId( |
816 const FindRegistrationCallback& callback, | 814 const FindRegistrationCallback& callback, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
960 if (!context_ || !context_->GetLiveVersion(version_id)) | 958 if (!context_ || !context_->GetLiveVersion(version_id)) |
961 StartPurgingResources(newly_purgeable_resources); | 959 StartPurgingResources(newly_purgeable_resources); |
962 } | 960 } |
963 | 961 |
964 scoped_refptr<ServiceWorkerRegistration> | 962 scoped_refptr<ServiceWorkerRegistration> |
965 ServiceWorkerStorage::GetOrCreateRegistration( | 963 ServiceWorkerStorage::GetOrCreateRegistration( |
966 const ServiceWorkerDatabase::RegistrationData& data, | 964 const ServiceWorkerDatabase::RegistrationData& data, |
967 const ResourceList& resources) { | 965 const ResourceList& resources) { |
968 scoped_refptr<ServiceWorkerRegistration> registration = | 966 scoped_refptr<ServiceWorkerRegistration> registration = |
969 context_->GetLiveRegistration(data.registration_id); | 967 context_->GetLiveRegistration(data.registration_id); |
970 if (registration) | 968 if (registration.get()) |
971 return registration; | 969 return registration; |
972 | 970 |
973 registration = new ServiceWorkerRegistration( | 971 registration = new ServiceWorkerRegistration( |
974 data.scope, data.script, data.registration_id, context_); | 972 data.scope, data.script, data.registration_id, context_); |
975 registration->set_last_update_check(data.last_update_check); | 973 registration->set_last_update_check(data.last_update_check); |
976 if (pending_deletions_.find(data.registration_id) != | 974 if (pending_deletions_.find(data.registration_id) != |
977 pending_deletions_.end()) { | 975 pending_deletions_.end()) { |
978 registration->set_is_deleted(true); | 976 registration->set_is_deleted(true); |
979 } | 977 } |
980 scoped_refptr<ServiceWorkerVersion> version = | 978 scoped_refptr<ServiceWorkerVersion> version = |
981 context_->GetLiveVersion(data.version_id); | 979 context_->GetLiveVersion(data.version_id); |
982 if (!version) { | 980 if (!version.get()) { |
983 version = new ServiceWorkerVersion(registration, data.version_id, context_); | 981 version = |
| 982 new ServiceWorkerVersion(registration.get(), data.version_id, context_); |
984 version->SetStatus(data.is_active ? | 983 version->SetStatus(data.is_active ? |
985 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED); | 984 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED); |
986 version->script_cache_map()->SetResources(resources); | 985 version->script_cache_map()->SetResources(resources); |
987 } | 986 } |
988 | 987 |
989 if (version->status() == ServiceWorkerVersion::ACTIVATED) | 988 if (version->status() == ServiceWorkerVersion::ACTIVATED) |
990 registration->SetActiveVersion(version); | 989 registration->SetActiveVersion(version.get()); |
991 else if (version->status() == ServiceWorkerVersion::INSTALLED) | 990 else if (version->status() == ServiceWorkerVersion::INSTALLED) |
992 registration->SetWaitingVersion(version); | 991 registration->SetWaitingVersion(version.get()); |
993 else | 992 else |
994 NOTREACHED(); | 993 NOTREACHED(); |
995 | 994 |
996 return registration; | 995 return registration; |
997 } | 996 } |
998 | 997 |
999 ServiceWorkerRegistration* | 998 ServiceWorkerRegistration* |
1000 ServiceWorkerStorage::FindInstallingRegistrationForDocument( | 999 ServiceWorkerStorage::FindInstallingRegistrationForDocument( |
1001 const GURL& document_url) { | 1000 const GURL& document_url) { |
1002 DCHECK(!document_url.has_ref()); | 1001 DCHECK(!document_url.has_ref()); |
1003 | 1002 |
1004 LongestScopeMatcher matcher(document_url); | 1003 LongestScopeMatcher matcher(document_url); |
1005 ServiceWorkerRegistration* match = NULL; | 1004 ServiceWorkerRegistration* match = NULL; |
1006 | 1005 |
1007 // TODO(nhiroki): This searches over installing registrations linearly and it | 1006 // TODO(nhiroki): This searches over installing registrations linearly and it |
1008 // couldn't be scalable. Maybe the regs should be partitioned by origin. | 1007 // couldn't be scalable. Maybe the regs should be partitioned by origin. |
1009 for (RegistrationRefsById::const_iterator it = | 1008 for (RegistrationRefsById::const_iterator it = |
1010 installing_registrations_.begin(); | 1009 installing_registrations_.begin(); |
1011 it != installing_registrations_.end(); ++it) { | 1010 it != installing_registrations_.end(); ++it) { |
1012 if (matcher.MatchLongest(it->second->pattern())) | 1011 if (matcher.MatchLongest(it->second->pattern())) |
1013 match = it->second; | 1012 match = it->second.get(); |
1014 } | 1013 } |
1015 return match; | 1014 return match; |
1016 } | 1015 } |
1017 | 1016 |
1018 ServiceWorkerRegistration* | 1017 ServiceWorkerRegistration* |
1019 ServiceWorkerStorage::FindInstallingRegistrationForPattern( | 1018 ServiceWorkerStorage::FindInstallingRegistrationForPattern( |
1020 const GURL& scope) { | 1019 const GURL& scope) { |
1021 for (RegistrationRefsById::const_iterator it = | 1020 for (RegistrationRefsById::const_iterator it = |
1022 installing_registrations_.begin(); | 1021 installing_registrations_.begin(); |
1023 it != installing_registrations_.end(); ++it) { | 1022 it != installing_registrations_.end(); ++it) { |
1024 if (it->second->pattern() == scope) | 1023 if (it->second->pattern() == scope) |
1025 return it->second; | 1024 return it->second.get(); |
1026 } | 1025 } |
1027 return NULL; | 1026 return NULL; |
1028 } | 1027 } |
1029 | 1028 |
1030 ServiceWorkerRegistration* | 1029 ServiceWorkerRegistration* |
1031 ServiceWorkerStorage::FindInstallingRegistrationForId( | 1030 ServiceWorkerStorage::FindInstallingRegistrationForId( |
1032 int64 registration_id) { | 1031 int64 registration_id) { |
1033 RegistrationRefsById::const_iterator found = | 1032 RegistrationRefsById::const_iterator found = |
1034 installing_registrations_.find(registration_id); | 1033 installing_registrations_.find(registration_id); |
1035 if (found == installing_registrations_.end()) | 1034 if (found == installing_registrations_.end()) |
1036 return NULL; | 1035 return NULL; |
1037 return found->second; | 1036 return found->second.get(); |
1038 } | 1037 } |
1039 | 1038 |
1040 ServiceWorkerDiskCache* ServiceWorkerStorage::disk_cache() { | 1039 ServiceWorkerDiskCache* ServiceWorkerStorage::disk_cache() { |
1041 if (disk_cache_) | 1040 if (disk_cache_) |
1042 return disk_cache_.get(); | 1041 return disk_cache_.get(); |
1043 | 1042 |
1044 disk_cache_.reset(new ServiceWorkerDiskCache); | 1043 disk_cache_.reset(new ServiceWorkerDiskCache); |
1045 | 1044 |
1046 base::FilePath path = GetDiskCachePath(); | 1045 base::FilePath path = GetDiskCachePath(); |
1047 if (path.empty()) { | 1046 if (path.empty()) { |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1378 callback.Run(DatabaseStatusToStatusCode(status)); | 1377 callback.Run(DatabaseStatusToStatusCode(status)); |
1379 return; | 1378 return; |
1380 } | 1379 } |
1381 DVLOG(1) << "Deleted ServiceWorkerDatabase successfully."; | 1380 DVLOG(1) << "Deleted ServiceWorkerDatabase successfully."; |
1382 | 1381 |
1383 // Delete the disk cache on the cache thread. | 1382 // Delete the disk cache on the cache thread. |
1384 // TODO(nhiroki): What if there is a bunch of files in the cache directory? | 1383 // TODO(nhiroki): What if there is a bunch of files in the cache directory? |
1385 // Deleting the directory could take a long time and restart could be delayed. | 1384 // Deleting the directory could take a long time and restart could be delayed. |
1386 // We should probably rename the directory and delete it later. | 1385 // We should probably rename the directory and delete it later. |
1387 PostTaskAndReplyWithResult( | 1386 PostTaskAndReplyWithResult( |
1388 database_task_runner_, | 1387 database_task_runner_.get(), |
1389 FROM_HERE, | 1388 FROM_HERE, |
1390 base::Bind(&base::DeleteFile, GetDiskCachePath(), true), | 1389 base::Bind(&base::DeleteFile, GetDiskCachePath(), true), |
1391 base::Bind(&ServiceWorkerStorage::DidDeleteDiskCache, | 1390 base::Bind(&ServiceWorkerStorage::DidDeleteDiskCache, |
1392 weak_factory_.GetWeakPtr(), callback)); | 1391 weak_factory_.GetWeakPtr(), |
| 1392 callback)); |
1393 } | 1393 } |
1394 | 1394 |
1395 void ServiceWorkerStorage::DidDeleteDiskCache( | 1395 void ServiceWorkerStorage::DidDeleteDiskCache( |
1396 const StatusCallback& callback, bool result) { | 1396 const StatusCallback& callback, bool result) { |
1397 DCHECK_EQ(DISABLED, state_); | 1397 DCHECK_EQ(DISABLED, state_); |
1398 if (!result) { | 1398 if (!result) { |
1399 // Give up the corruption recovery until the browser restarts. | 1399 // Give up the corruption recovery until the browser restarts. |
1400 LOG(ERROR) << "Failed to delete the diskcache."; | 1400 LOG(ERROR) << "Failed to delete the diskcache."; |
1401 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1401 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
1402 return; | 1402 return; |
1403 } | 1403 } |
1404 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1404 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
1405 callback.Run(SERVICE_WORKER_OK); | 1405 callback.Run(SERVICE_WORKER_OK); |
1406 } | 1406 } |
1407 | 1407 |
1408 } // namespace content | 1408 } // namespace content |
OLD | NEW |