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

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

Issue 633873002: Service Worker: Respect the "clear on exit" content setting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@contentsettings
Patch Set: self review Created 6 years, 2 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
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_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/debug/trace_event.h" 10 #include "base/debug/trace_event.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/hash.h" 12 #include "base/hash.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/task_runner_util.h" 16 #include "base/task_runner_util.h"
17 #include "content/browser/service_worker/service_worker_context_core.h" 17 #include "content/browser/service_worker/service_worker_context_core.h"
18 #include "content/browser/service_worker/service_worker_disk_cache.h" 18 #include "content/browser/service_worker/service_worker_disk_cache.h"
19 #include "content/browser/service_worker/service_worker_info.h" 19 #include "content/browser/service_worker/service_worker_info.h"
20 #include "content/browser/service_worker/service_worker_metrics.h" 20 #include "content/browser/service_worker/service_worker_metrics.h"
21 #include "content/browser/service_worker/service_worker_registration.h" 21 #include "content/browser/service_worker/service_worker_registration.h"
22 #include "content/browser/service_worker/service_worker_utils.h" 22 #include "content/browser/service_worker/service_worker_utils.h"
23 #include "content/browser/service_worker/service_worker_version.h" 23 #include "content/browser/service_worker/service_worker_version.h"
24 #include "content/common/service_worker/service_worker_types.h" 24 #include "content/common/service_worker/service_worker_types.h"
25 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
26 #include "net/base/completion_callback.h" 26 #include "net/base/completion_callback.h"
27 #include "net/base/io_buffer.h" 27 #include "net/base/io_buffer.h"
28 #include "net/base/net_errors.h" 28 #include "net/base/net_errors.h"
29 #include "storage/browser/quota/quota_manager_proxy.h" 29 #include "storage/browser/quota/quota_manager_proxy.h"
30 #include "storage/browser/quota/special_storage_policy.h"
30 31
31 namespace content { 32 namespace content {
32 33
33 namespace { 34 namespace {
34 35
35 void RunSoon(const tracked_objects::Location& from_here, 36 void RunSoon(const tracked_objects::Location& from_here,
36 const base::Closure& closure) { 37 const base::Closure& closure) {
37 base::MessageLoop::current()->PostTask(from_here, closure); 38 base::MessageLoop::current()->PostTask(from_here, closure);
38 } 39 }
39 40
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 ServiceWorkerStorage:: 212 ServiceWorkerStorage::
212 DidDeleteRegistrationParams::DidDeleteRegistrationParams() 213 DidDeleteRegistrationParams::DidDeleteRegistrationParams()
213 : registration_id(kInvalidServiceWorkerRegistrationId) { 214 : registration_id(kInvalidServiceWorkerRegistrationId) {
214 } 215 }
215 216
216 ServiceWorkerStorage:: 217 ServiceWorkerStorage::
217 DidDeleteRegistrationParams::~DidDeleteRegistrationParams() { 218 DidDeleteRegistrationParams::~DidDeleteRegistrationParams() {
218 } 219 }
219 220
220 ServiceWorkerStorage::~ServiceWorkerStorage() { 221 ServiceWorkerStorage::~ServiceWorkerStorage() {
222 ClearSessionOnlyOrigins();
221 weak_factory_.InvalidateWeakPtrs(); 223 weak_factory_.InvalidateWeakPtrs();
222 database_task_runner_->DeleteSoon(FROM_HERE, database_.release()); 224 database_task_manager_->GetTaskRunner()->DeleteSoon(FROM_HERE,
225 database_.release());
223 } 226 }
224 227
225 // static 228 // static
226 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( 229 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create(
227 const base::FilePath& path, 230 const base::FilePath& path,
228 base::WeakPtr<ServiceWorkerContextCore> context, 231 base::WeakPtr<ServiceWorkerContextCore> context,
229 const scoped_refptr<base::SequencedTaskRunner>& database_task_runner, 232 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager,
230 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, 233 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread,
231 storage::QuotaManagerProxy* quota_manager_proxy) { 234 storage::QuotaManagerProxy* quota_manager_proxy,
235 storage::SpecialStoragePolicy* special_storage_policy) {
232 return make_scoped_ptr(new ServiceWorkerStorage(path, 236 return make_scoped_ptr(new ServiceWorkerStorage(path,
233 context, 237 context,
234 database_task_runner, 238 database_task_manager.Pass(),
235 disk_cache_thread, 239 disk_cache_thread,
236 quota_manager_proxy)); 240 quota_manager_proxy,
241 special_storage_policy));
237 } 242 }
238 243
239 // static 244 // static
240 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( 245 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create(
241 base::WeakPtr<ServiceWorkerContextCore> context, 246 base::WeakPtr<ServiceWorkerContextCore> context,
242 ServiceWorkerStorage* old_storage) { 247 ServiceWorkerStorage* old_storage) {
243 return make_scoped_ptr( 248 return make_scoped_ptr(
244 new ServiceWorkerStorage(old_storage->path_, 249 new ServiceWorkerStorage(old_storage->path_,
245 context, 250 context,
246 old_storage->database_task_runner_, 251 old_storage->database_task_manager_->Clone(),
michaeln 2014/10/16 01:15:04 Ah, I was wondering what clone() was for.
247 old_storage->disk_cache_thread_, 252 old_storage->disk_cache_thread_,
248 old_storage->quota_manager_proxy_.get())); 253 old_storage->quota_manager_proxy_.get(),
254 old_storage->special_storage_policy_.get()));
249 } 255 }
250 256
251 void ServiceWorkerStorage::FindRegistrationForDocument( 257 void ServiceWorkerStorage::FindRegistrationForDocument(
252 const GURL& document_url, 258 const GURL& document_url,
253 const FindRegistrationCallback& callback) { 259 const FindRegistrationCallback& callback) {
254 DCHECK(!document_url.has_ref()); 260 DCHECK(!document_url.has_ref());
255 TRACE_EVENT_ASYNC_BEGIN1( 261 TRACE_EVENT_ASYNC_BEGIN1(
256 "ServiceWorker", 262 "ServiceWorker",
257 "ServiceWorkerStorage::FindRegistrationForDocument", 263 "ServiceWorkerStorage::FindRegistrationForDocument",
258 base::Hash(document_url.spec()), 264 base::Hash(document_url.spec()),
(...skipping 15 matching lines...) Expand all
274 scoped_refptr<ServiceWorkerRegistration> installing_registration = 280 scoped_refptr<ServiceWorkerRegistration> installing_registration =
275 FindInstallingRegistrationForDocument(document_url); 281 FindInstallingRegistrationForDocument(document_url);
276 CompleteFindNow(installing_registration, 282 CompleteFindNow(installing_registration,
277 installing_registration.get() 283 installing_registration.get()
278 ? SERVICE_WORKER_OK 284 ? SERVICE_WORKER_OK
279 : SERVICE_WORKER_ERROR_NOT_FOUND, 285 : SERVICE_WORKER_ERROR_NOT_FOUND,
280 callback); 286 callback);
281 return; 287 return;
282 } 288 }
283 289
284 database_task_runner_->PostTask( 290 database_task_manager_->GetTaskRunner()->PostTask(
285 FROM_HERE, 291 FROM_HERE,
286 base::Bind( 292 base::Bind(
287 &FindForDocumentInDB, 293 &FindForDocumentInDB,
288 database_.get(), 294 database_.get(),
289 base::MessageLoopProxy::current(), 295 base::MessageLoopProxy::current(),
290 document_url, 296 document_url,
291 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForDocument, 297 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForDocument,
292 weak_factory_.GetWeakPtr(), document_url, callback))); 298 weak_factory_.GetWeakPtr(), document_url, callback)));
293 } 299 }
294 300
(...skipping 18 matching lines...) Expand all
313 FindInstallingRegistrationForPattern(scope); 319 FindInstallingRegistrationForPattern(scope);
314 CompleteFindSoon(FROM_HERE, 320 CompleteFindSoon(FROM_HERE,
315 installing_registration, 321 installing_registration,
316 installing_registration.get() 322 installing_registration.get()
317 ? SERVICE_WORKER_OK 323 ? SERVICE_WORKER_OK
318 : SERVICE_WORKER_ERROR_NOT_FOUND, 324 : SERVICE_WORKER_ERROR_NOT_FOUND,
319 callback); 325 callback);
320 return; 326 return;
321 } 327 }
322 328
323 database_task_runner_->PostTask( 329 database_task_manager_->GetTaskRunner()->PostTask(
324 FROM_HERE, 330 FROM_HERE,
325 base::Bind( 331 base::Bind(
326 &FindForPatternInDB, 332 &FindForPatternInDB,
327 database_.get(), 333 database_.get(),
328 base::MessageLoopProxy::current(), 334 base::MessageLoopProxy::current(),
329 scope, 335 scope,
330 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern, 336 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForPattern,
331 weak_factory_.GetWeakPtr(), scope, callback))); 337 weak_factory_.GetWeakPtr(), scope, callback)));
332 } 338 }
333 339
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 return; 381 return;
376 } 382 }
377 383
378 scoped_refptr<ServiceWorkerRegistration> registration = 384 scoped_refptr<ServiceWorkerRegistration> registration =
379 context_->GetLiveRegistration(registration_id); 385 context_->GetLiveRegistration(registration_id);
380 if (registration.get()) { 386 if (registration.get()) {
381 CompleteFindNow(registration, SERVICE_WORKER_OK, callback); 387 CompleteFindNow(registration, SERVICE_WORKER_OK, callback);
382 return; 388 return;
383 } 389 }
384 390
385 database_task_runner_->PostTask( 391 database_task_manager_->GetTaskRunner()->PostTask(
386 FROM_HERE, 392 FROM_HERE,
387 base::Bind(&FindForIdInDB, 393 base::Bind(&FindForIdInDB,
388 database_.get(), 394 database_.get(),
389 base::MessageLoopProxy::current(), 395 base::MessageLoopProxy::current(),
390 registration_id, origin, 396 registration_id, origin,
391 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForId, 397 base::Bind(&ServiceWorkerStorage::DidFindRegistrationForId,
392 weak_factory_.GetWeakPtr(), callback))); 398 weak_factory_.GetWeakPtr(), callback)));
393 } 399 }
394 400
395 void ServiceWorkerStorage::GetAllRegistrations( 401 void ServiceWorkerStorage::GetAllRegistrations(
396 const GetAllRegistrationInfosCallback& callback) { 402 const GetAllRegistrationInfosCallback& callback) {
397 if (!LazyInitialize(base::Bind( 403 if (!LazyInitialize(base::Bind(
398 &ServiceWorkerStorage::GetAllRegistrations, 404 &ServiceWorkerStorage::GetAllRegistrations,
399 weak_factory_.GetWeakPtr(), callback))) { 405 weak_factory_.GetWeakPtr(), callback))) {
400 if (state_ != INITIALIZING || !context_) { 406 if (state_ != INITIALIZING || !context_) {
401 RunSoon(FROM_HERE, base::Bind( 407 RunSoon(FROM_HERE, base::Bind(
402 callback, std::vector<ServiceWorkerRegistrationInfo>())); 408 callback, std::vector<ServiceWorkerRegistrationInfo>()));
403 } 409 }
404 return; 410 return;
405 } 411 }
406 DCHECK_EQ(INITIALIZED, state_); 412 DCHECK_EQ(INITIALIZED, state_);
407 413
408 RegistrationList* registrations = new RegistrationList; 414 RegistrationList* registrations = new RegistrationList;
409 PostTaskAndReplyWithResult( 415 PostTaskAndReplyWithResult(
410 database_task_runner_.get(), 416 database_task_manager_->GetTaskRunner(),
411 FROM_HERE, 417 FROM_HERE,
412 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, 418 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations,
413 base::Unretained(database_.get()), 419 base::Unretained(database_.get()),
414 base::Unretained(registrations)), 420 base::Unretained(registrations)),
415 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations, 421 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations,
416 weak_factory_.GetWeakPtr(), 422 weak_factory_.GetWeakPtr(),
417 callback, 423 callback,
418 base::Owned(registrations))); 424 base::Owned(registrations)));
419 } 425 }
420 426
(...skipping 18 matching lines...) Expand all
439 data.version_id = version->version_id(); 445 data.version_id = version->version_id();
440 data.last_update_check = registration->last_update_check(); 446 data.last_update_check = registration->last_update_check();
441 data.is_active = (version == registration->active_version()); 447 data.is_active = (version == registration->active_version());
442 448
443 ResourceList resources; 449 ResourceList resources;
444 version->script_cache_map()->GetResources(&resources); 450 version->script_cache_map()->GetResources(&resources);
445 451
446 if (!has_checked_for_stale_resources_) 452 if (!has_checked_for_stale_resources_)
447 DeleteStaleResources(); 453 DeleteStaleResources();
448 454
449 database_task_runner_->PostTask( 455 database_task_manager_->GetTaskRunner()->PostTask(
450 FROM_HERE, 456 FROM_HERE,
451 base::Bind(&WriteRegistrationInDB, 457 base::Bind(&WriteRegistrationInDB,
452 database_.get(), 458 database_.get(),
453 base::MessageLoopProxy::current(), 459 base::MessageLoopProxy::current(),
454 data, resources, 460 data, resources,
455 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, 461 base::Bind(&ServiceWorkerStorage::DidStoreRegistration,
456 weak_factory_.GetWeakPtr(), 462 weak_factory_.GetWeakPtr(),
457 callback))); 463 callback)));
458 464
459 registration->set_is_deleted(false); 465 registration->set_is_deleted(false);
460 } 466 }
461 467
462 void ServiceWorkerStorage::UpdateToActiveState( 468 void ServiceWorkerStorage::UpdateToActiveState(
463 ServiceWorkerRegistration* registration, 469 ServiceWorkerRegistration* registration,
464 const StatusCallback& callback) { 470 const StatusCallback& callback) {
465 DCHECK(registration); 471 DCHECK(registration);
466 472
467 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; 473 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_;
468 if (IsDisabled() || !context_) { 474 if (IsDisabled() || !context_) {
469 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); 475 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
470 return; 476 return;
471 } 477 }
472 478
473 PostTaskAndReplyWithResult( 479 PostTaskAndReplyWithResult(
474 database_task_runner_.get(), 480 database_task_manager_->GetTaskRunner(),
475 FROM_HERE, 481 FROM_HERE,
476 base::Bind(&ServiceWorkerDatabase::UpdateVersionToActive, 482 base::Bind(&ServiceWorkerDatabase::UpdateVersionToActive,
477 base::Unretained(database_.get()), 483 base::Unretained(database_.get()),
478 registration->id(), 484 registration->id(),
479 registration->pattern().GetOrigin()), 485 registration->pattern().GetOrigin()),
480 base::Bind(&ServiceWorkerStorage::DidUpdateToActiveState, 486 base::Bind(&ServiceWorkerStorage::DidUpdateToActiveState,
481 weak_factory_.GetWeakPtr(), 487 weak_factory_.GetWeakPtr(),
482 callback)); 488 callback));
483 } 489 }
484 490
485 void ServiceWorkerStorage::UpdateLastUpdateCheckTime( 491 void ServiceWorkerStorage::UpdateLastUpdateCheckTime(
486 ServiceWorkerRegistration* registration) { 492 ServiceWorkerRegistration* registration) {
487 DCHECK(registration); 493 DCHECK(registration);
488 494
489 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; 495 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_;
490 if (IsDisabled() || !context_) 496 if (IsDisabled() || !context_)
491 return; 497 return;
492 498
493 database_task_runner_->PostTask( 499 database_task_manager_->GetTaskRunner()->PostTask(
494 FROM_HERE, 500 FROM_HERE,
495 base::Bind( 501 base::Bind(
496 base::IgnoreResult(&ServiceWorkerDatabase::UpdateLastCheckTime), 502 base::IgnoreResult(&ServiceWorkerDatabase::UpdateLastCheckTime),
497 base::Unretained(database_.get()), 503 base::Unretained(database_.get()),
498 registration->id(), 504 registration->id(),
499 registration->pattern().GetOrigin(), 505 registration->pattern().GetOrigin(),
500 registration->last_update_check())); 506 registration->last_update_check()));
501 } 507 }
502 508
503 void ServiceWorkerStorage::DeleteRegistration( 509 void ServiceWorkerStorage::DeleteRegistration(
504 int64 registration_id, 510 int64 registration_id,
505 const GURL& origin, 511 const GURL& origin,
506 const StatusCallback& callback) { 512 const StatusCallback& callback) {
507 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_; 513 DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_;
508 if (IsDisabled() || !context_) { 514 if (IsDisabled() || !context_) {
509 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); 515 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
510 return; 516 return;
511 } 517 }
512 518
513 if (!has_checked_for_stale_resources_) 519 if (!has_checked_for_stale_resources_)
514 DeleteStaleResources(); 520 DeleteStaleResources();
515 521
516 DidDeleteRegistrationParams params; 522 DidDeleteRegistrationParams params;
517 params.registration_id = registration_id; 523 params.registration_id = registration_id;
518 params.origin = origin; 524 params.origin = origin;
519 params.callback = callback; 525 params.callback = callback;
520 526
521 database_task_runner_->PostTask( 527 database_task_manager_->GetTaskRunner()->PostTask(
522 FROM_HERE, 528 FROM_HERE,
523 base::Bind(&DeleteRegistrationFromDB, 529 base::Bind(&DeleteRegistrationFromDB,
524 database_.get(), 530 database_.get(),
525 base::MessageLoopProxy::current(), 531 base::MessageLoopProxy::current(),
526 registration_id, origin, 532 registration_id, origin,
527 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, 533 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration,
528 weak_factory_.GetWeakPtr(), params))); 534 weak_factory_.GetWeakPtr(), params)));
529 535
530 // The registration should no longer be findable. 536 // The registration should no longer be findable.
531 pending_deletions_.insert(registration_id); 537 pending_deletions_.insert(registration_id);
(...skipping 15 matching lines...) Expand all
547 new ServiceWorkerResponseWriter(response_id, disk_cache())); 553 new ServiceWorkerResponseWriter(response_id, disk_cache()));
548 } 554 }
549 555
550 void ServiceWorkerStorage::StoreUncommittedResponseId(int64 id) { 556 void ServiceWorkerStorage::StoreUncommittedResponseId(int64 id) {
551 DCHECK_NE(kInvalidServiceWorkerResponseId, id); 557 DCHECK_NE(kInvalidServiceWorkerResponseId, id);
552 DCHECK_EQ(INITIALIZED, state_); 558 DCHECK_EQ(INITIALIZED, state_);
553 559
554 if (!has_checked_for_stale_resources_) 560 if (!has_checked_for_stale_resources_)
555 DeleteStaleResources(); 561 DeleteStaleResources();
556 562
557 database_task_runner_->PostTask( 563 database_task_manager_->GetTaskRunner()->PostTask(
558 FROM_HERE, 564 FROM_HERE,
559 base::Bind(base::IgnoreResult( 565 base::Bind(base::IgnoreResult(
560 &ServiceWorkerDatabase::WriteUncommittedResourceIds), 566 &ServiceWorkerDatabase::WriteUncommittedResourceIds),
561 base::Unretained(database_.get()), 567 base::Unretained(database_.get()),
562 std::set<int64>(&id, &id + 1))); 568 std::set<int64>(&id, &id + 1)));
563 } 569 }
564 570
565 void ServiceWorkerStorage::DoomUncommittedResponse(int64 id) { 571 void ServiceWorkerStorage::DoomUncommittedResponse(int64 id) {
566 DCHECK_NE(kInvalidServiceWorkerResponseId, id); 572 DCHECK_NE(kInvalidServiceWorkerResponseId, id);
567 database_task_runner_->PostTask( 573 database_task_manager_->GetTaskRunner()->PostTask(
568 FROM_HERE, 574 FROM_HERE,
569 base::Bind(base::IgnoreResult( 575 base::Bind(base::IgnoreResult(
570 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), 576 &ServiceWorkerDatabase::PurgeUncommittedResourceIds),
571 base::Unretained(database_.get()), 577 base::Unretained(database_.get()),
572 std::set<int64>(&id, &id + 1))); 578 std::set<int64>(&id, &id + 1)));
573 StartPurgingResources(std::vector<int64>(1, id)); 579 StartPurgingResources(std::vector<int64>(1, id));
574 } 580 }
575 581
576 void ServiceWorkerStorage::CompareScriptResources( 582 void ServiceWorkerStorage::CompareScriptResources(
577 int64 lhs_id, int64 rhs_id, 583 int64 lhs_id, int64 rhs_id,
578 const CompareCallback& callback) { 584 const CompareCallback& callback) {
579 DCHECK(!callback.is_null()); 585 DCHECK(!callback.is_null());
580 scoped_refptr<ResponseComparer> comparer = 586 scoped_refptr<ResponseComparer> comparer =
581 new ResponseComparer(weak_factory_.GetWeakPtr(), 587 new ResponseComparer(weak_factory_.GetWeakPtr(),
582 CreateResponseReader(lhs_id), 588 CreateResponseReader(lhs_id),
583 CreateResponseReader(rhs_id), 589 CreateResponseReader(rhs_id),
584 callback); 590 callback);
585 comparer->Start(); // It deletes itself when done. 591 comparer->Start(); // It deletes itself when done.
586 } 592 }
587 593
588 void ServiceWorkerStorage::DeleteAndStartOver(const StatusCallback& callback) { 594 void ServiceWorkerStorage::DeleteAndStartOver(const StatusCallback& callback) {
589 Disable(); 595 Disable();
590 596
591 // Delete the database on the database thread. 597 // Delete the database on the database thread.
592 PostTaskAndReplyWithResult( 598 PostTaskAndReplyWithResult(
593 database_task_runner_.get(), 599 database_task_manager_->GetTaskRunner(),
594 FROM_HERE, 600 FROM_HERE,
595 base::Bind(&ServiceWorkerDatabase::DestroyDatabase, 601 base::Bind(&ServiceWorkerDatabase::DestroyDatabase,
596 base::Unretained(database_.get())), 602 base::Unretained(database_.get())),
597 base::Bind(&ServiceWorkerStorage::DidDeleteDatabase, 603 base::Bind(&ServiceWorkerStorage::DidDeleteDatabase,
598 weak_factory_.GetWeakPtr(), 604 weak_factory_.GetWeakPtr(),
599 callback)); 605 callback));
600 } 606 }
601 607
602 int64 ServiceWorkerStorage::NewRegistrationId() { 608 int64 ServiceWorkerStorage::NewRegistrationId() {
603 if (state_ == DISABLED) 609 if (state_ == DISABLED)
(...skipping 29 matching lines...) Expand all
633 ServiceWorkerStatusCode status) { 639 ServiceWorkerStatusCode status) {
634 installing_registrations_.erase(registration->id()); 640 installing_registrations_.erase(registration->id());
635 if (status != SERVICE_WORKER_OK && version) { 641 if (status != SERVICE_WORKER_OK && version) {
636 ResourceList resources; 642 ResourceList resources;
637 version->script_cache_map()->GetResources(&resources); 643 version->script_cache_map()->GetResources(&resources);
638 644
639 std::set<int64> ids; 645 std::set<int64> ids;
640 for (size_t i = 0; i < resources.size(); ++i) 646 for (size_t i = 0; i < resources.size(); ++i)
641 ids.insert(resources[i].resource_id); 647 ids.insert(resources[i].resource_id);
642 648
643 database_task_runner_->PostTask( 649 database_task_manager_->GetTaskRunner()->PostTask(
644 FROM_HERE, 650 FROM_HERE,
645 base::Bind(base::IgnoreResult( 651 base::Bind(base::IgnoreResult(
646 &ServiceWorkerDatabase::PurgeUncommittedResourceIds), 652 &ServiceWorkerDatabase::PurgeUncommittedResourceIds),
647 base::Unretained(database_.get()), 653 base::Unretained(database_.get()),
648 ids)); 654 ids));
649 } 655 }
650 } 656 }
651 657
652 void ServiceWorkerStorage::NotifyUninstallingRegistration( 658 void ServiceWorkerStorage::NotifyUninstallingRegistration(
653 ServiceWorkerRegistration* registration) { 659 ServiceWorkerRegistration* registration) {
(...skipping 19 matching lines...) Expand all
673 679
674 void ServiceWorkerStorage::PurgeResources(const ResourceList& resources) { 680 void ServiceWorkerStorage::PurgeResources(const ResourceList& resources) {
675 if (!has_checked_for_stale_resources_) 681 if (!has_checked_for_stale_resources_)
676 DeleteStaleResources(); 682 DeleteStaleResources();
677 StartPurgingResources(resources); 683 StartPurgingResources(resources);
678 } 684 }
679 685
680 ServiceWorkerStorage::ServiceWorkerStorage( 686 ServiceWorkerStorage::ServiceWorkerStorage(
681 const base::FilePath& path, 687 const base::FilePath& path,
682 base::WeakPtr<ServiceWorkerContextCore> context, 688 base::WeakPtr<ServiceWorkerContextCore> context,
683 const scoped_refptr<base::SequencedTaskRunner>& database_task_runner, 689 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager,
684 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, 690 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread,
685 storage::QuotaManagerProxy* quota_manager_proxy) 691 storage::QuotaManagerProxy* quota_manager_proxy,
692 storage::SpecialStoragePolicy* special_storage_policy)
686 : next_registration_id_(kInvalidServiceWorkerRegistrationId), 693 : next_registration_id_(kInvalidServiceWorkerRegistrationId),
687 next_version_id_(kInvalidServiceWorkerVersionId), 694 next_version_id_(kInvalidServiceWorkerVersionId),
688 next_resource_id_(kInvalidServiceWorkerResourceId), 695 next_resource_id_(kInvalidServiceWorkerResourceId),
689 state_(UNINITIALIZED), 696 state_(UNINITIALIZED),
690 path_(path), 697 path_(path),
691 context_(context), 698 context_(context),
692 database_task_runner_(database_task_runner), 699 database_task_manager_(database_task_manager.Pass()),
693 disk_cache_thread_(disk_cache_thread), 700 disk_cache_thread_(disk_cache_thread),
694 quota_manager_proxy_(quota_manager_proxy), 701 quota_manager_proxy_(quota_manager_proxy),
702 special_storage_policy_(special_storage_policy),
695 is_purge_pending_(false), 703 is_purge_pending_(false),
696 has_checked_for_stale_resources_(false), 704 has_checked_for_stale_resources_(false),
697 weak_factory_(this) { 705 weak_factory_(this) {
698 database_.reset(new ServiceWorkerDatabase(GetDatabasePath())); 706 database_.reset(new ServiceWorkerDatabase(GetDatabasePath()));
699 } 707 }
700 708
701 base::FilePath ServiceWorkerStorage::GetDatabasePath() { 709 base::FilePath ServiceWorkerStorage::GetDatabasePath() {
702 if (path_.empty()) 710 if (path_.empty())
703 return base::FilePath(); 711 return base::FilePath();
704 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) 712 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory)
(...skipping 18 matching lines...) Expand all
723 return false; 731 return false;
724 case INITIALIZING: 732 case INITIALIZING:
725 pending_tasks_.push_back(callback); 733 pending_tasks_.push_back(callback);
726 return false; 734 return false;
727 case UNINITIALIZED: 735 case UNINITIALIZED:
728 pending_tasks_.push_back(callback); 736 pending_tasks_.push_back(callback);
729 // Fall-through. 737 // Fall-through.
730 } 738 }
731 739
732 state_ = INITIALIZING; 740 state_ = INITIALIZING;
733 database_task_runner_->PostTask( 741 database_task_manager_->GetTaskRunner()->PostTask(
734 FROM_HERE, 742 FROM_HERE,
735 base::Bind(&ReadInitialDataFromDB, 743 base::Bind(&ReadInitialDataFromDB,
736 database_.get(), 744 database_.get(),
737 base::MessageLoopProxy::current(), 745 base::MessageLoopProxy::current(),
738 base::Bind(&ServiceWorkerStorage::DidReadInitialData, 746 base::Bind(&ServiceWorkerStorage::DidReadInitialData,
739 weak_factory_.GetWeakPtr()))); 747 weak_factory_.GetWeakPtr())));
740 return false; 748 return false;
741 } 749 }
742 750
743 void ServiceWorkerStorage::DidReadInitialData( 751 void ServiceWorkerStorage::DidReadInitialData(
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged, 1137 id, base::Bind(&ServiceWorkerStorage::OnResourcePurged,
1130 weak_factory_.GetWeakPtr(), id)); 1138 weak_factory_.GetWeakPtr(), id));
1131 if (rv != net::ERR_IO_PENDING) 1139 if (rv != net::ERR_IO_PENDING)
1132 OnResourcePurged(id, rv); 1140 OnResourcePurged(id, rv);
1133 } 1141 }
1134 1142
1135 void ServiceWorkerStorage::OnResourcePurged(int64 id, int rv) { 1143 void ServiceWorkerStorage::OnResourcePurged(int64 id, int rv) {
1136 DCHECK(is_purge_pending_); 1144 DCHECK(is_purge_pending_);
1137 is_purge_pending_ = false; 1145 is_purge_pending_ = false;
1138 1146
1139 database_task_runner_->PostTask( 1147 database_task_manager_->GetTaskRunner()->PostTask(
1140 FROM_HERE, 1148 FROM_HERE,
1141 base::Bind(base::IgnoreResult( 1149 base::Bind(base::IgnoreResult(
1142 &ServiceWorkerDatabase::ClearPurgeableResourceIds), 1150 &ServiceWorkerDatabase::ClearPurgeableResourceIds),
1143 base::Unretained(database_.get()), 1151 base::Unretained(database_.get()),
1144 std::set<int64>(&id, &id + 1))); 1152 std::set<int64>(&id, &id + 1)));
1145 1153
1146 ContinuePurgingResources(); 1154 ContinuePurgingResources();
1147 } 1155 }
1148 1156
1149 void ServiceWorkerStorage::DeleteStaleResources() { 1157 void ServiceWorkerStorage::DeleteStaleResources() {
1150 DCHECK(!has_checked_for_stale_resources_); 1158 DCHECK(!has_checked_for_stale_resources_);
1151 has_checked_for_stale_resources_ = true; 1159 has_checked_for_stale_resources_ = true;
1152 database_task_runner_->PostTask( 1160 database_task_manager_->GetTaskRunner()->PostTask(
1153 FROM_HERE, 1161 FROM_HERE,
1154 base::Bind(&ServiceWorkerStorage::CollectStaleResourcesFromDB, 1162 base::Bind(&ServiceWorkerStorage::CollectStaleResourcesFromDB,
1155 database_.get(), 1163 database_.get(),
1156 base::MessageLoopProxy::current(), 1164 base::MessageLoopProxy::current(),
1157 base::Bind(&ServiceWorkerStorage::DidCollectStaleResources, 1165 base::Bind(&ServiceWorkerStorage::DidCollectStaleResources,
1158 weak_factory_.GetWeakPtr()))); 1166 weak_factory_.GetWeakPtr())));
1159 } 1167 }
1160 1168
1161 void ServiceWorkerStorage::DidCollectStaleResources( 1169 void ServiceWorkerStorage::DidCollectStaleResources(
1162 const std::vector<int64>& stale_resource_ids, 1170 const std::vector<int64>& stale_resource_ids,
1163 ServiceWorkerDatabase::Status status) { 1171 ServiceWorkerDatabase::Status status) {
1164 DCHECK_EQ(ServiceWorkerDatabase::STATUS_OK, status); 1172 DCHECK_EQ(ServiceWorkerDatabase::STATUS_OK, status);
1165 if (status != ServiceWorkerDatabase::STATUS_OK) 1173 if (status != ServiceWorkerDatabase::STATUS_OK)
1166 return; 1174 return;
1167 StartPurgingResources(stale_resource_ids); 1175 StartPurgingResources(stale_resource_ids);
1168 } 1176 }
1169 1177
1178 void ServiceWorkerStorage::ClearSessionOnlyOrigins() {
1179 // Can be null in tests.
1180 if (!special_storage_policy_.get())
1181 return;
1182
1183 if (!special_storage_policy_->HasSessionOnlyOrigins())
1184 return;
1185
1186 std::set<GURL> session_only_origins;
1187 for (const GURL& origin : registered_origins_) {
1188 if (special_storage_policy_->IsStorageSessionOnly(origin))
1189 session_only_origins.insert(origin);
1190 }
1191
1192 database_task_manager_->GetShutdownBlockingTaskRunner()->PostTask(
1193 FROM_HERE,
1194 base::Bind(&DeleteAllDataForOriginsFromDB,
1195 database_.get(),
1196 session_only_origins));
1197 }
1198
1170 void ServiceWorkerStorage::CollectStaleResourcesFromDB( 1199 void ServiceWorkerStorage::CollectStaleResourcesFromDB(
1171 ServiceWorkerDatabase* database, 1200 ServiceWorkerDatabase* database,
1172 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 1201 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
1173 const GetResourcesCallback& callback) { 1202 const GetResourcesCallback& callback) {
1174 std::set<int64> ids; 1203 std::set<int64> ids;
1175 ServiceWorkerDatabase::Status status = 1204 ServiceWorkerDatabase::Status status =
1176 database->GetUncommittedResourceIds(&ids); 1205 database->GetUncommittedResourceIds(&ids);
1177 if (status != ServiceWorkerDatabase::STATUS_OK) { 1206 if (status != ServiceWorkerDatabase::STATUS_OK) {
1178 original_task_runner->PostTask( 1207 original_task_runner->PostTask(
1179 FROM_HERE, 1208 FROM_HERE,
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 const GURL& origin, 1395 const GURL& origin,
1367 const FindInDBCallback& callback) { 1396 const FindInDBCallback& callback) {
1368 ServiceWorkerDatabase::RegistrationData data; 1397 ServiceWorkerDatabase::RegistrationData data;
1369 ResourceList resources; 1398 ResourceList resources;
1370 ServiceWorkerDatabase::Status status = 1399 ServiceWorkerDatabase::Status status =
1371 database->ReadRegistration(registration_id, origin, &data, &resources); 1400 database->ReadRegistration(registration_id, origin, &data, &resources);
1372 original_task_runner->PostTask( 1401 original_task_runner->PostTask(
1373 FROM_HERE, base::Bind(callback, data, resources, status)); 1402 FROM_HERE, base::Bind(callback, data, resources, status));
1374 } 1403 }
1375 1404
1405 void ServiceWorkerStorage::DeleteAllDataForOriginsFromDB(
1406 ServiceWorkerDatabase* database,
1407 const std::set<GURL>& origins) {
1408 DCHECK(database);
1409
1410 std::vector<int64> newly_purgeable_resources;
1411 database->DeleteAllDataForOrigins(origins, &newly_purgeable_resources);
1412 }
1413
1376 // TODO(nhiroki): The corruption recovery should not be scheduled if the error 1414 // TODO(nhiroki): The corruption recovery should not be scheduled if the error
1377 // is transient and it can get healed soon (e.g. IO error). To do that, the 1415 // is transient and it can get healed soon (e.g. IO error). To do that, the
1378 // database should not disable itself when an error occurs and the storage 1416 // database should not disable itself when an error occurs and the storage
1379 // controls it instead. 1417 // controls it instead.
1380 void ServiceWorkerStorage::ScheduleDeleteAndStartOver() { 1418 void ServiceWorkerStorage::ScheduleDeleteAndStartOver() {
1381 if (state_ == DISABLED) { 1419 if (state_ == DISABLED) {
1382 // Recovery process has already been scheduled. 1420 // Recovery process has already been scheduled.
1383 return; 1421 return;
1384 } 1422 }
1385 Disable(); 1423 Disable();
(...skipping 12 matching lines...) Expand all
1398 callback.Run(DatabaseStatusToStatusCode(status)); 1436 callback.Run(DatabaseStatusToStatusCode(status));
1399 return; 1437 return;
1400 } 1438 }
1401 DVLOG(1) << "Deleted ServiceWorkerDatabase successfully."; 1439 DVLOG(1) << "Deleted ServiceWorkerDatabase successfully.";
1402 1440
1403 // Delete the disk cache on the cache thread. 1441 // Delete the disk cache on the cache thread.
1404 // TODO(nhiroki): What if there is a bunch of files in the cache directory? 1442 // TODO(nhiroki): What if there is a bunch of files in the cache directory?
1405 // Deleting the directory could take a long time and restart could be delayed. 1443 // Deleting the directory could take a long time and restart could be delayed.
1406 // We should probably rename the directory and delete it later. 1444 // We should probably rename the directory and delete it later.
1407 PostTaskAndReplyWithResult( 1445 PostTaskAndReplyWithResult(
1408 database_task_runner_.get(), 1446 database_task_manager_->GetTaskRunner(),
1409 FROM_HERE, 1447 FROM_HERE,
1410 base::Bind(&base::DeleteFile, GetDiskCachePath(), true), 1448 base::Bind(&base::DeleteFile, GetDiskCachePath(), true),
1411 base::Bind(&ServiceWorkerStorage::DidDeleteDiskCache, 1449 base::Bind(&ServiceWorkerStorage::DidDeleteDiskCache,
1412 weak_factory_.GetWeakPtr(), 1450 weak_factory_.GetWeakPtr(),
1413 callback)); 1451 callback));
1414 } 1452 }
1415 1453
1416 void ServiceWorkerStorage::DidDeleteDiskCache( 1454 void ServiceWorkerStorage::DidDeleteDiskCache(
1417 const StatusCallback& callback, bool result) { 1455 const StatusCallback& callback, bool result) {
1418 DCHECK_EQ(DISABLED, state_); 1456 DCHECK_EQ(DISABLED, state_);
1419 if (!result) { 1457 if (!result) {
1420 // Give up the corruption recovery until the browser restarts. 1458 // Give up the corruption recovery until the browser restarts.
1421 LOG(ERROR) << "Failed to delete the diskcache."; 1459 LOG(ERROR) << "Failed to delete the diskcache.";
1422 callback.Run(SERVICE_WORKER_ERROR_FAILED); 1460 callback.Run(SERVICE_WORKER_ERROR_FAILED);
1423 return; 1461 return;
1424 } 1462 }
1425 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; 1463 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully.";
1426 callback.Run(SERVICE_WORKER_OK); 1464 callback.Run(SERVICE_WORKER_OK);
1427 } 1465 }
1428 1466
1429 } // namespace content 1467 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698