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

Side by Side Diff: webkit/appcache/appcache_storage_impl.cc

Issue 7031065: AppCache + Quota integration (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 6 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "webkit/appcache/appcache_storage_impl.h" 5 #include "webkit/appcache/appcache_storage_impl.h"
6 6
7 #include <set>
8
7 #include "app/sql/connection.h" 9 #include "app/sql/connection.h"
8 #include "app/sql/transaction.h" 10 #include "app/sql/transaction.h"
9 #include "base/file_util.h" 11 #include "base/file_util.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/message_loop.h" 13 #include "base/message_loop.h"
12 #include "base/stl_util-inl.h" 14 #include "base/stl_util-inl.h"
13 #include "base/string_util.h" 15 #include "base/string_util.h"
14 #include "net/base/cache_type.h" 16 #include "net/base/cache_type.h"
15 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
16 #include "webkit/appcache/appcache.h" 18 #include "webkit/appcache/appcache.h"
17 #include "webkit/appcache/appcache_database.h" 19 #include "webkit/appcache/appcache_database.h"
18 #include "webkit/appcache/appcache_entry.h" 20 #include "webkit/appcache/appcache_entry.h"
19 #include "webkit/appcache/appcache_group.h" 21 #include "webkit/appcache/appcache_group.h"
20 #include "webkit/appcache/appcache_histograms.h" 22 #include "webkit/appcache/appcache_histograms.h"
21 #include "webkit/appcache/appcache_policy.h" 23 #include "webkit/appcache/appcache_policy.h"
24 #include "webkit/appcache/appcache_quota_client.h"
22 #include "webkit/appcache/appcache_response.h" 25 #include "webkit/appcache/appcache_response.h"
23 #include "webkit/appcache/appcache_service.h" 26 #include "webkit/appcache/appcache_service.h"
24 #include "webkit/appcache/appcache_thread.h" 27 #include "webkit/appcache/appcache_thread.h"
28 #include "webkit/quota/quota_client.h"
29 #include "webkit/quota/quota_manager.h"
25 #include "webkit/quota/special_storage_policy.h" 30 #include "webkit/quota/special_storage_policy.h"
26 31
27 namespace { 32 namespace {
28 // Helper with no return value for use with NewRunnableFunction. 33 // Helper with no return value for use with NewRunnableFunction.
29 void DeleteDirectory(const FilePath& path) { 34 void DeleteDirectory(const FilePath& path) {
30 file_util::Delete(path, true); 35 file_util::Delete(path, true);
31 } 36 }
32 } 37 }
33 38
34 namespace appcache { 39 namespace appcache {
35 40
41 // Hard coded default when not using quota management.
42 static const int kDefaultQuota = 5 * 1024 * 1024;
43
36 static const int kMaxDiskCacheSize = 250 * 1024 * 1024; 44 static const int kMaxDiskCacheSize = 250 * 1024 * 1024;
37 static const int kMaxMemDiskCacheSize = 10 * 1024 * 1024; 45 static const int kMaxMemDiskCacheSize = 10 * 1024 * 1024;
38 static const FilePath::CharType kDiskCacheDirectoryName[] = 46 static const FilePath::CharType kDiskCacheDirectoryName[] =
39 FILE_PATH_LITERAL("Cache"); 47 FILE_PATH_LITERAL("Cache");
40 48
49
41 // DatabaseTask ----------------------------------------- 50 // DatabaseTask -----------------------------------------
42 51
43 class AppCacheStorageImpl::DatabaseTask 52 class AppCacheStorageImpl::DatabaseTask
44 : public base::RefCountedThreadSafe<DatabaseTask> { 53 : public base::RefCountedThreadSafe<DatabaseTask> {
45 public: 54 public:
46 explicit DatabaseTask(AppCacheStorageImpl* storage) 55 explicit DatabaseTask(AppCacheStorageImpl* storage)
47 : storage_(storage), database_(storage->database_) {} 56 : storage_(storage), database_(storage->database_) {}
48 57
49 virtual ~DatabaseTask() {} 58 virtual ~DatabaseTask() {}
50 59
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 last_cache_id_(0), last_response_id_(0), 148 last_cache_id_(0), last_response_id_(0),
140 last_deletable_response_rowid_(0) {} 149 last_deletable_response_rowid_(0) {}
141 150
142 virtual void Run(); 151 virtual void Run();
143 virtual void RunCompleted(); 152 virtual void RunCompleted();
144 153
145 int64 last_group_id_; 154 int64 last_group_id_;
146 int64 last_cache_id_; 155 int64 last_cache_id_;
147 int64 last_response_id_; 156 int64 last_response_id_;
148 int64 last_deletable_response_rowid_; 157 int64 last_deletable_response_rowid_;
149 std::set<GURL> origins_with_groups_; 158 std::map<GURL, int64> usage_map_;
150 }; 159 };
151 160
152 void AppCacheStorageImpl::InitTask::Run() { 161 void AppCacheStorageImpl::InitTask::Run() {
153 database_->FindLastStorageIds( 162 database_->FindLastStorageIds(
154 &last_group_id_, &last_cache_id_, &last_response_id_, 163 &last_group_id_, &last_cache_id_, &last_response_id_,
155 &last_deletable_response_rowid_); 164 &last_deletable_response_rowid_);
156 database_->FindOriginsWithGroups(&origins_with_groups_); 165 database_->GetAllOriginUsage(&usage_map_);
157 } 166 }
158 167
159 void AppCacheStorageImpl::InitTask::RunCompleted() { 168 void AppCacheStorageImpl::InitTask::RunCompleted() {
160 storage_->last_group_id_ = last_group_id_; 169 storage_->last_group_id_ = last_group_id_;
161 storage_->last_cache_id_ = last_cache_id_; 170 storage_->last_cache_id_ = last_cache_id_;
162 storage_->last_response_id_ = last_response_id_; 171 storage_->last_response_id_ = last_response_id_;
163 storage_->last_deletable_response_rowid_ = last_deletable_response_rowid_; 172 storage_->last_deletable_response_rowid_ = last_deletable_response_rowid_;
164 173
165 if (!storage_->is_disabled()) { 174 if (!storage_->is_disabled()) {
166 storage_->origins_with_groups_.swap(origins_with_groups_); 175 storage_->usage_map_.swap(usage_map_);
167
168 const int kDelayMillis = 5 * 60 * 1000; // Five minutes. 176 const int kDelayMillis = 5 * 60 * 1000; // Five minutes.
169 MessageLoop::current()->PostDelayedTask(FROM_HERE, 177 MessageLoop::current()->PostDelayedTask(FROM_HERE,
170 storage_->method_factory_.NewRunnableMethod( 178 storage_->method_factory_.NewRunnableMethod(
171 &AppCacheStorageImpl::DelayedStartDeletingUnusedResponses), 179 &AppCacheStorageImpl::DelayedStartDeletingUnusedResponses),
172 kDelayMillis); 180 kDelayMillis);
173 } 181 }
182
183 if (storage_->service()->quota_client())
184 storage_->service()->quota_client()->NotifyAppCacheReady();
174 } 185 }
175 186
176 // CloseConnectionTask ------- 187 // CloseConnectionTask -------
177 188
178 class AppCacheStorageImpl::CloseConnectionTask : public DatabaseTask { 189 class AppCacheStorageImpl::CloseConnectionTask : public DatabaseTask {
179 public: 190 public:
180 explicit CloseConnectionTask(AppCacheStorageImpl* storage) 191 explicit CloseConnectionTask(AppCacheStorageImpl* storage)
181 : DatabaseTask(storage) {} 192 : DatabaseTask(storage) {}
182 193
183 virtual void Run() { database_->CloseConnection(); } 194 virtual void Run() { database_->CloseConnection(); }
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 // We have to update foriegn entries if MarkEntryAsForeignTasks 306 // We have to update foriegn entries if MarkEntryAsForeignTasks
296 // are in flight. 307 // are in flight.
297 std::vector<GURL> urls; 308 std::vector<GURL> urls;
298 storage_->GetPendingForeignMarkingsForCache(cache->get()->cache_id(), &urls); 309 storage_->GetPendingForeignMarkingsForCache(cache->get()->cache_id(), &urls);
299 for (std::vector<GURL>::iterator iter = urls.begin(); 310 for (std::vector<GURL>::iterator iter = urls.begin();
300 iter != urls.end(); ++iter) { 311 iter != urls.end(); ++iter) {
301 DCHECK(cache->get()->GetEntry(*iter)); 312 DCHECK(cache->get()->GetEntry(*iter));
302 cache->get()->GetEntry(*iter)->add_types(AppCacheEntry::FOREIGN); 313 cache->get()->GetEntry(*iter)->add_types(AppCacheEntry::FOREIGN);
303 } 314 }
304 315
316 storage_->NotifyStorageAccessed(group_record_.origin);
317
305 // TODO(michaeln): Maybe verify that the responses we expect to exist 318 // TODO(michaeln): Maybe verify that the responses we expect to exist
306 // do actually exist in the disk_cache (and if not then what?) 319 // do actually exist in the disk_cache (and if not then what?)
307 } 320 }
308 321
309 // CacheLoadTask ------- 322 // CacheLoadTask -------
310 323
311 class AppCacheStorageImpl::CacheLoadTask : public StoreOrLoadTask { 324 class AppCacheStorageImpl::CacheLoadTask : public StoreOrLoadTask {
312 public: 325 public:
313 CacheLoadTask(int64 cache_id, AppCacheStorageImpl* storage) 326 CacheLoadTask(int64 cache_id, AppCacheStorageImpl* storage)
314 : StoreOrLoadTask(storage), cache_id_(cache_id), 327 : StoreOrLoadTask(storage), cache_id_(cache_id),
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 FOR_EACH_DELEGATE(delegates_, OnGroupLoaded(group, manifest_url_)); 401 FOR_EACH_DELEGATE(delegates_, OnGroupLoaded(group, manifest_url_));
389 } 402 }
390 403
391 // StoreGroupAndCacheTask ------- 404 // StoreGroupAndCacheTask -------
392 405
393 class AppCacheStorageImpl::StoreGroupAndCacheTask : public StoreOrLoadTask { 406 class AppCacheStorageImpl::StoreGroupAndCacheTask : public StoreOrLoadTask {
394 public: 407 public:
395 StoreGroupAndCacheTask(AppCacheStorageImpl* storage, AppCacheGroup* group, 408 StoreGroupAndCacheTask(AppCacheStorageImpl* storage, AppCacheGroup* group,
396 AppCache* newest_cache); 409 AppCache* newest_cache);
397 410
411 void GetQuotaThenSchedule();
412 void OnQuotaCallback(
413 quota::QuotaStatusCode status, int64 usage, int64 quota);
414
398 virtual void Run(); 415 virtual void Run();
399 virtual void RunCompleted(); 416 virtual void RunCompleted();
400 virtual void CancelCompletion(); 417 virtual void CancelCompletion();
401 418
402 scoped_refptr<AppCacheGroup> group_; 419 scoped_refptr<AppCacheGroup> group_;
403 scoped_refptr<AppCache> cache_; 420 scoped_refptr<AppCache> cache_;
404 bool success_; 421 bool success_;
405 bool would_exceed_quota_; 422 bool would_exceed_quota_;
406 int64 quota_override_; 423 int64 space_available_;
424 int64 new_origin_usage_;
407 std::vector<int64> newly_deletable_response_ids_; 425 std::vector<int64> newly_deletable_response_ids_;
408 }; 426 };
409 427
410 AppCacheStorageImpl::StoreGroupAndCacheTask::StoreGroupAndCacheTask( 428 AppCacheStorageImpl::StoreGroupAndCacheTask::StoreGroupAndCacheTask(
411 AppCacheStorageImpl* storage, AppCacheGroup* group, AppCache* newest_cache) 429 AppCacheStorageImpl* storage, AppCacheGroup* group, AppCache* newest_cache)
412 : StoreOrLoadTask(storage), group_(group), cache_(newest_cache), 430 : StoreOrLoadTask(storage), group_(group), cache_(newest_cache),
413 success_(false), would_exceed_quota_(false), 431 success_(false), would_exceed_quota_(false),
414 quota_override_(-1) { 432 space_available_(-1), new_origin_usage_(-1) {
415 group_record_.group_id = group->group_id(); 433 group_record_.group_id = group->group_id();
416 group_record_.manifest_url = group->manifest_url(); 434 group_record_.manifest_url = group->manifest_url();
417 group_record_.origin = group_record_.manifest_url.GetOrigin(); 435 group_record_.origin = group_record_.manifest_url.GetOrigin();
418 newest_cache->ToDatabaseRecords( 436 newest_cache->ToDatabaseRecords(
419 group, 437 group,
420 &cache_record_, &entry_records_, &fallback_namespace_records_, 438 &cache_record_, &entry_records_, &fallback_namespace_records_,
421 &online_whitelist_records_); 439 &online_whitelist_records_);
440 }
422 441
423 if (storage->service()->special_storage_policy() && 442 void AppCacheStorageImpl::StoreGroupAndCacheTask::GetQuotaThenSchedule() {
424 storage->service()->special_storage_policy()->IsStorageUnlimited( 443 quota::QuotaManager* quota_manager = NULL;
425 group_record_.origin)) { 444 if (storage_->service()->quota_manager_proxy()) {
426 quota_override_ = kint64max; 445 quota_manager =
446 storage_->service()->quota_manager_proxy()->quota_manager();
427 } 447 }
448
449 if (!quota_manager) {
450 if (storage_->service()->special_storage_policy() &&
451 storage_->service()->special_storage_policy()->IsStorageUnlimited(
452 group_record_.origin))
453 space_available_ = kint64max;
454 Schedule();
455 return;
456 }
457
458 // We have to ask the quota manager for the value.
459 AddRef(); // balanced in the OnQuotaCallback
460 storage_->pending_quota_queries_.insert(this);
461 quota_manager->GetUsageAndQuota(
462 group_record_.origin, quota::kStorageTypeTemporary,
463 NewCallback(this, &StoreGroupAndCacheTask::OnQuotaCallback));
464 }
465
466 void AppCacheStorageImpl::StoreGroupAndCacheTask::OnQuotaCallback(
467 quota::QuotaStatusCode status, int64 usage, int64 quota) {
468 // TODO(michaeln): what do we do if status is not OK, fail the store op?
michaeln 2011/06/08 18:09:54 Took care of this TODO by setting space_available_
469 DCHECK_EQ(quota::kQuotaStatusOk, status);
470 space_available_ = std::max(static_cast<int64>(0), quota - usage);
471 if (storage_) {
472 storage_->pending_quota_queries_.erase(this);
473 Schedule();
474 }
475 Release(); // balanced in GetQuotaThenSchedule
428 } 476 }
429 477
430 void AppCacheStorageImpl::StoreGroupAndCacheTask::Run() { 478 void AppCacheStorageImpl::StoreGroupAndCacheTask::Run() {
431 DCHECK(!success_); 479 DCHECK(!success_);
432 sql::Connection* connection = database_->db_connection(); 480 sql::Connection* connection = database_->db_connection();
433 if (!connection) 481 if (!connection)
434 return; 482 return;
435 483
436 sql::Transaction transaction(connection); 484 sql::Transaction transaction(connection);
437 if (!transaction.Begin()) 485 if (!transaction.Begin())
438 return; 486 return;
439 487
488 int64 old_origin_usage = database_->GetOriginUsage(group_record_.origin);
489
440 AppCacheDatabase::GroupRecord existing_group; 490 AppCacheDatabase::GroupRecord existing_group;
441 success_ = database_->FindGroup(group_record_.group_id, &existing_group); 491 success_ = database_->FindGroup(group_record_.group_id, &existing_group);
442 if (!success_) { 492 if (!success_) {
443 group_record_.creation_time = base::Time::Now(); 493 group_record_.creation_time = base::Time::Now();
444 group_record_.last_access_time = base::Time::Now(); 494 group_record_.last_access_time = base::Time::Now();
445 success_ = database_->InsertGroup(&group_record_); 495 success_ = database_->InsertGroup(&group_record_);
446 } else { 496 } else {
447 DCHECK(group_record_.group_id == existing_group.group_id); 497 DCHECK(group_record_.group_id == existing_group.group_id);
448 DCHECK(group_record_.manifest_url == existing_group.manifest_url); 498 DCHECK(group_record_.manifest_url == existing_group.manifest_url);
449 DCHECK(group_record_.origin == existing_group.origin); 499 DCHECK(group_record_.origin == existing_group.origin);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 success_ = 537 success_ =
488 success_ && 538 success_ &&
489 database_->InsertCache(&cache_record_) && 539 database_->InsertCache(&cache_record_) &&
490 database_->InsertEntryRecords(entry_records_) && 540 database_->InsertEntryRecords(entry_records_) &&
491 database_->InsertFallbackNameSpaceRecords(fallback_namespace_records_)&& 541 database_->InsertFallbackNameSpaceRecords(fallback_namespace_records_)&&
492 database_->InsertOnlineWhiteListRecords(online_whitelist_records_); 542 database_->InsertOnlineWhiteListRecords(online_whitelist_records_);
493 543
494 if (!success_) 544 if (!success_)
495 return; 545 return;
496 546
497 int64 quota = (quota_override_ >= 0) ? 547 new_origin_usage_ = database_->GetOriginUsage(group_record_.origin);
498 quota_override_ :
499 database_->GetOriginQuota(group_record_.origin);
500 548
501 if (database_->GetOriginUsage(group_record_.origin) > quota) { 549 // Only check quota when the new usage exceeds the old usage.
550 if (new_origin_usage_ <= old_origin_usage) {
551 success_ = transaction.Commit();
552 return;
553 }
554
555 // Use a simple hard-coded value when not using quota managment.
556 if (space_available_ == -1) {
557 if (new_origin_usage_ > kDefaultQuota) {
558 would_exceed_quota_ = true;
559 success_ = false;
560 return;
561 }
562 success_ = transaction.Commit();
563 return;
564 }
565
566 // Check limits based on the space availbable given to us via the
567 // quota system.
568 int64 delta = new_origin_usage_ - old_origin_usage;
569 if (delta > space_available_) {
502 would_exceed_quota_ = true; 570 would_exceed_quota_ = true;
503 success_ = false; 571 success_ = false;
504 return; 572 return;
505 } 573 }
506 574
507 success_ = transaction.Commit(); 575 success_ = transaction.Commit();
508 } 576 }
509 577
510 void AppCacheStorageImpl::StoreGroupAndCacheTask::RunCompleted() { 578 void AppCacheStorageImpl::StoreGroupAndCacheTask::RunCompleted() {
511 if (success_) { 579 if (success_) {
512 storage_->origins_with_groups_.insert(group_->manifest_url().GetOrigin()); 580 storage_->UpdateUsageMapAndNotify(
581 group_->manifest_url().GetOrigin(), new_origin_usage_);
513 if (cache_ != group_->newest_complete_cache()) { 582 if (cache_ != group_->newest_complete_cache()) {
514 cache_->set_complete(true); 583 cache_->set_complete(true);
515 group_->AddCache(cache_); 584 group_->AddCache(cache_);
516 } 585 }
517 if (group_->creation_time().is_null()) 586 if (group_->creation_time().is_null())
518 group_->set_creation_time(group_record_.creation_time); 587 group_->set_creation_time(group_record_.creation_time);
519 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_); 588 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_);
520 } 589 }
521 FOR_EACH_DELEGATE(delegates_, 590 FOR_EACH_DELEGATE(delegates_,
522 OnGroupAndNewestCacheStored(group_, cache_, success_, 591 OnGroupAndNewestCacheStored(group_, cache_, success_,
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 class AppCacheStorageImpl::MakeGroupObsoleteTask : public DatabaseTask { 899 class AppCacheStorageImpl::MakeGroupObsoleteTask : public DatabaseTask {
831 public: 900 public:
832 MakeGroupObsoleteTask(AppCacheStorageImpl* storage, AppCacheGroup* group); 901 MakeGroupObsoleteTask(AppCacheStorageImpl* storage, AppCacheGroup* group);
833 902
834 virtual void Run(); 903 virtual void Run();
835 virtual void RunCompleted(); 904 virtual void RunCompleted();
836 virtual void CancelCompletion(); 905 virtual void CancelCompletion();
837 906
838 scoped_refptr<AppCacheGroup> group_; 907 scoped_refptr<AppCacheGroup> group_;
839 int64 group_id_; 908 int64 group_id_;
909 GURL origin_;
840 bool success_; 910 bool success_;
841 std::set<GURL> origins_with_groups_; 911 int64 new_origin_usage_;
842 std::vector<int64> newly_deletable_response_ids_; 912 std::vector<int64> newly_deletable_response_ids_;
843 }; 913 };
844 914
845 AppCacheStorageImpl::MakeGroupObsoleteTask::MakeGroupObsoleteTask( 915 AppCacheStorageImpl::MakeGroupObsoleteTask::MakeGroupObsoleteTask(
846 AppCacheStorageImpl* storage, AppCacheGroup* group) 916 AppCacheStorageImpl* storage, AppCacheGroup* group)
847 : DatabaseTask(storage), group_(group), group_id_(group->group_id()), 917 : DatabaseTask(storage), group_(group), group_id_(group->group_id()),
848 success_(false) { 918 origin_(group->manifest_url().GetOrigin()),
919 success_(false), new_origin_usage_(-1) {
849 } 920 }
850 921
851 void AppCacheStorageImpl::MakeGroupObsoleteTask::Run() { 922 void AppCacheStorageImpl::MakeGroupObsoleteTask::Run() {
852 DCHECK(!success_); 923 DCHECK(!success_);
853 sql::Connection* connection = database_->db_connection(); 924 sql::Connection* connection = database_->db_connection();
854 if (!connection) 925 if (!connection)
855 return; 926 return;
856 927
857 sql::Transaction transaction(connection); 928 sql::Transaction transaction(connection);
858 if (!transaction.Begin()) 929 if (!transaction.Begin())
859 return; 930 return;
860 931
861 AppCacheDatabase::GroupRecord group_record; 932 AppCacheDatabase::GroupRecord group_record;
862 if (!database_->FindGroup(group_id_, &group_record)) { 933 if (!database_->FindGroup(group_id_, &group_record)) {
863 // This group doesn't exists in the database, nothing todo here. 934 // This group doesn't exists in the database, nothing todo here.
935 new_origin_usage_ = database_->GetOriginUsage(origin_);
864 success_ = true; 936 success_ = true;
865 return; 937 return;
866 } 938 }
867 939
940 DCHECK_EQ(group_record.origin, origin_);
941
868 AppCacheDatabase::CacheRecord cache_record; 942 AppCacheDatabase::CacheRecord cache_record;
869 if (database_->FindCacheForGroup(group_id_, &cache_record)) { 943 if (database_->FindCacheForGroup(group_id_, &cache_record)) {
870 database_->FindResponseIdsForCacheAsVector(cache_record.cache_id, 944 database_->FindResponseIdsForCacheAsVector(cache_record.cache_id,
871 &newly_deletable_response_ids_); 945 &newly_deletable_response_ids_);
872 success_ = 946 success_ =
873 database_->DeleteGroup(group_id_) && 947 database_->DeleteGroup(group_id_) &&
874 database_->DeleteCache(cache_record.cache_id) && 948 database_->DeleteCache(cache_record.cache_id) &&
875 database_->DeleteEntriesForCache(cache_record.cache_id) && 949 database_->DeleteEntriesForCache(cache_record.cache_id) &&
876 database_->DeleteFallbackNameSpacesForCache(cache_record.cache_id) && 950 database_->DeleteFallbackNameSpacesForCache(cache_record.cache_id) &&
877 database_->DeleteOnlineWhiteListForCache(cache_record.cache_id) && 951 database_->DeleteOnlineWhiteListForCache(cache_record.cache_id) &&
878 database_->InsertDeletableResponseIds(newly_deletable_response_ids_); 952 database_->InsertDeletableResponseIds(newly_deletable_response_ids_);
879 } else { 953 } else {
880 NOTREACHED() << "A existing group without a cache is unexpected"; 954 NOTREACHED() << "A existing group without a cache is unexpected";
881 success_ = database_->DeleteGroup(group_id_); 955 success_ = database_->DeleteGroup(group_id_);
882 } 956 }
883 957
884 success_ = success_ && 958 new_origin_usage_ = database_->GetOriginUsage(origin_);
885 database_->FindOriginsWithGroups(&origins_with_groups_) && 959 success_ = success_ && transaction.Commit();
886 transaction.Commit();
887 } 960 }
888 961
889 void AppCacheStorageImpl::MakeGroupObsoleteTask::RunCompleted() { 962 void AppCacheStorageImpl::MakeGroupObsoleteTask::RunCompleted() {
890 if (success_) { 963 if (success_) {
891 group_->set_obsolete(true); 964 group_->set_obsolete(true);
892 if (!storage_->is_disabled()) { 965 if (!storage_->is_disabled()) {
893 storage_->origins_with_groups_.swap(origins_with_groups_); 966 storage_->UpdateUsageMapAndNotify(origin_, new_origin_usage_);
894 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_); 967 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_);
895 968
896 // Also remove from the working set, caches for an 'obsolete' group 969 // Also remove from the working set, caches for an 'obsolete' group
897 // may linger in use, but the group itself cannot be looked up by 970 // may linger in use, but the group itself cannot be looked up by
898 // 'manifest_url' in the working set any longer. 971 // 'manifest_url' in the working set any longer.
899 storage_->working_set()->RemoveGroup(group_); 972 storage_->working_set()->RemoveGroup(group_);
900 } 973 }
901 } 974 }
902 FOR_EACH_DELEGATE(delegates_, OnGroupMadeObsolete(group_, success_)); 975 FOR_EACH_DELEGATE(delegates_, OnGroupMadeObsolete(group_, success_));
903 group_ = NULL; 976 group_ = NULL;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 void AppCacheStorageImpl::DeleteDeletableResponseIdsTask::Run() { 1036 void AppCacheStorageImpl::DeleteDeletableResponseIdsTask::Run() {
964 database_->DeleteDeletableResponseIds(response_ids_); 1037 database_->DeleteDeletableResponseIds(response_ids_);
965 } 1038 }
966 1039
967 // UpdateGroupLastAccessTimeTask ------- 1040 // UpdateGroupLastAccessTimeTask -------
968 1041
969 class AppCacheStorageImpl::UpdateGroupLastAccessTimeTask 1042 class AppCacheStorageImpl::UpdateGroupLastAccessTimeTask
970 : public DatabaseTask { 1043 : public DatabaseTask {
971 public: 1044 public:
972 UpdateGroupLastAccessTimeTask( 1045 UpdateGroupLastAccessTimeTask(
973 AppCacheStorageImpl* storage, int64 group_id, base::Time time) 1046 AppCacheStorageImpl* storage, AppCacheGroup* group, base::Time time)
974 : DatabaseTask(storage), group_id_(group_id), last_access_time_(time) {} 1047 : DatabaseTask(storage), group_id_(group->group_id()),
1048 last_access_time_(time) {
1049 storage->NotifyStorageAccessed(group->manifest_url().GetOrigin());
1050 }
975 1051
976 virtual void Run(); 1052 virtual void Run();
977 1053
978 int64 group_id_; 1054 int64 group_id_;
979 base::Time last_access_time_; 1055 base::Time last_access_time_;
980 }; 1056 };
981 1057
982 void AppCacheStorageImpl::UpdateGroupLastAccessTimeTask::Run() { 1058 void AppCacheStorageImpl::UpdateGroupLastAccessTimeTask::Run() {
983 database_->UpdateGroupLastAccessTime(group_id_, last_access_time_); 1059 database_->UpdateGroupLastAccessTime(group_id_, last_access_time_);
984 } 1060 }
(...skipping 10 matching lines...) Expand all
995 this, &AppCacheStorageImpl::OnDeletedOneResponse)), 1071 this, &AppCacheStorageImpl::OnDeletedOneResponse)),
996 ALLOW_THIS_IN_INITIALIZER_LIST(init_callback_( 1072 ALLOW_THIS_IN_INITIALIZER_LIST(init_callback_(
997 this, &AppCacheStorageImpl::OnDiskCacheInitialized)), 1073 this, &AppCacheStorageImpl::OnDiskCacheInitialized)),
998 database_(NULL), is_disabled_(false), 1074 database_(NULL), is_disabled_(false),
999 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { 1075 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
1000 } 1076 }
1001 1077
1002 AppCacheStorageImpl::~AppCacheStorageImpl() { 1078 AppCacheStorageImpl::~AppCacheStorageImpl() {
1003 STLDeleteElements(&pending_simple_tasks_); 1079 STLDeleteElements(&pending_simple_tasks_);
1004 1080
1081 std::for_each(pending_quota_queries_.begin(),
1082 pending_quota_queries_.end(),
1083 std::mem_fun(&DatabaseTask::CancelCompletion));
1005 std::for_each(scheduled_database_tasks_.begin(), 1084 std::for_each(scheduled_database_tasks_.begin(),
1006 scheduled_database_tasks_.end(), 1085 scheduled_database_tasks_.end(),
1007 std::mem_fun(&DatabaseTask::CancelCompletion)); 1086 std::mem_fun(&DatabaseTask::CancelCompletion));
1008 1087
1009 if (database_) 1088 if (database_)
1010 AppCacheThread::DeleteSoon(AppCacheThread::db(), FROM_HERE, database_); 1089 AppCacheThread::DeleteSoon(AppCacheThread::db(), FROM_HERE, database_);
1011 } 1090 }
1012 1091
1013 void AppCacheStorageImpl::Initialize(const FilePath& cache_directory, 1092 void AppCacheStorageImpl::Initialize(const FilePath& cache_directory,
1014 base::MessageLoopProxy* cache_thread) { 1093 base::MessageLoopProxy* cache_thread) {
1015 cache_directory_ = cache_directory; 1094 cache_directory_ = cache_directory;
1016 cache_thread_ = cache_thread; 1095 cache_thread_ = cache_thread;
1017 is_incognito_ = cache_directory_.empty(); 1096 is_incognito_ = cache_directory_.empty();
1018 1097
1019 FilePath db_file_path; 1098 FilePath db_file_path;
1020 if (!is_incognito_) 1099 if (!is_incognito_)
1021 db_file_path = cache_directory_.Append(kAppCacheDatabaseName); 1100 db_file_path = cache_directory_.Append(kAppCacheDatabaseName);
1022 database_ = new AppCacheDatabase(db_file_path); 1101 database_ = new AppCacheDatabase(db_file_path);
1023 1102
1024 scoped_refptr<InitTask> task(new InitTask(this)); 1103 scoped_refptr<InitTask> task(new InitTask(this));
1025 task->Schedule(); 1104 task->Schedule();
1026 } 1105 }
1027 1106
1028 void AppCacheStorageImpl::Disable() { 1107 void AppCacheStorageImpl::Disable() {
1029 if (is_disabled_) 1108 if (is_disabled_)
1030 return; 1109 return;
1031 VLOG(1) << "Disabling appcache storage."; 1110 VLOG(1) << "Disabling appcache storage.";
1032 is_disabled_ = true; 1111 is_disabled_ = true;
1033 origins_with_groups_.clear(); 1112 usage_map_.clear(); // TODO(michaeln): What to tell the QuotaManager?
michaeln 2011/06/08 18:09:54 Took are of this TODO with ClearUsageMapAndNotify(
1034 working_set()->Disable(); 1113 working_set()->Disable();
1035 if (disk_cache_.get()) 1114 if (disk_cache_.get())
1036 disk_cache_->Disable(); 1115 disk_cache_->Disable();
1037 scoped_refptr<DisableDatabaseTask> task(new DisableDatabaseTask(this)); 1116 scoped_refptr<DisableDatabaseTask> task(new DisableDatabaseTask(this));
1038 task->Schedule(); 1117 task->Schedule();
1039 } 1118 }
1040 1119
1041 void AppCacheStorageImpl::GetAllInfo(Delegate* delegate) { 1120 void AppCacheStorageImpl::GetAllInfo(Delegate* delegate) {
1042 DCHECK(delegate); 1121 DCHECK(delegate);
1043 scoped_refptr<GetAllInfoTask> task(new GetAllInfoTask(this)); 1122 scoped_refptr<GetAllInfoTask> task(new GetAllInfoTask(this));
1044 task->AddDelegate(GetOrCreateDelegateReference(delegate)); 1123 task->AddDelegate(GetOrCreateDelegateReference(delegate));
1045 task->Schedule(); 1124 task->Schedule();
1046 } 1125 }
1047 1126
1048 void AppCacheStorageImpl::LoadCache(int64 id, Delegate* delegate) { 1127 void AppCacheStorageImpl::LoadCache(int64 id, Delegate* delegate) {
1049 DCHECK(delegate); 1128 DCHECK(delegate);
1050 if (is_disabled_) { 1129 if (is_disabled_) {
1051 delegate->OnCacheLoaded(NULL, id); 1130 delegate->OnCacheLoaded(NULL, id);
1052 return; 1131 return;
1053 } 1132 }
1054 1133
1055 AppCache* cache = working_set_.GetCache(id); 1134 AppCache* cache = working_set_.GetCache(id);
1056 if (cache) { 1135 if (cache) {
1057 delegate->OnCacheLoaded(cache, id); 1136 delegate->OnCacheLoaded(cache, id);
1058 if (cache->owning_group()) { 1137 if (cache->owning_group()) {
1059 scoped_refptr<DatabaseTask> update_task( 1138 scoped_refptr<DatabaseTask> update_task(
1060 new UpdateGroupLastAccessTimeTask( 1139 new UpdateGroupLastAccessTimeTask(
1061 this, cache->owning_group()->group_id(), base::Time::Now())); 1140 this, cache->owning_group(), base::Time::Now()));
1062 update_task->Schedule(); 1141 update_task->Schedule();
1063 } 1142 }
1064 return; 1143 return;
1065 } 1144 }
1066 scoped_refptr<CacheLoadTask> task(GetPendingCacheLoadTask(id)); 1145 scoped_refptr<CacheLoadTask> task(GetPendingCacheLoadTask(id));
1067 if (task) { 1146 if (task) {
1068 task->AddDelegate(GetOrCreateDelegateReference(delegate)); 1147 task->AddDelegate(GetOrCreateDelegateReference(delegate));
1069 return; 1148 return;
1070 } 1149 }
1071 task = new CacheLoadTask(id, this); 1150 task = new CacheLoadTask(id, this);
1072 task->AddDelegate(GetOrCreateDelegateReference(delegate)); 1151 task->AddDelegate(GetOrCreateDelegateReference(delegate));
1073 task->Schedule(); 1152 task->Schedule();
1074 pending_cache_loads_[id] = task; 1153 pending_cache_loads_[id] = task;
1075 } 1154 }
1076 1155
1077 void AppCacheStorageImpl::LoadOrCreateGroup( 1156 void AppCacheStorageImpl::LoadOrCreateGroup(
1078 const GURL& manifest_url, Delegate* delegate) { 1157 const GURL& manifest_url, Delegate* delegate) {
1079 DCHECK(delegate); 1158 DCHECK(delegate);
1080 if (is_disabled_) { 1159 if (is_disabled_) {
1081 delegate->OnGroupLoaded(NULL, manifest_url); 1160 delegate->OnGroupLoaded(NULL, manifest_url);
1082 return; 1161 return;
1083 } 1162 }
1084 1163
1085 AppCacheGroup* group = working_set_.GetGroup(manifest_url); 1164 AppCacheGroup* group = working_set_.GetGroup(manifest_url);
1086 if (group) { 1165 if (group) {
1087 delegate->OnGroupLoaded(group, manifest_url); 1166 delegate->OnGroupLoaded(group, manifest_url);
1088 scoped_refptr<DatabaseTask> update_task( 1167 scoped_refptr<DatabaseTask> update_task(
1089 new UpdateGroupLastAccessTimeTask( 1168 new UpdateGroupLastAccessTimeTask(
1090 this, group->group_id(), base::Time::Now())); 1169 this, group, base::Time::Now()));
1091 update_task->Schedule(); 1170 update_task->Schedule();
1092 return; 1171 return;
1093 } 1172 }
1094 1173
1095 scoped_refptr<GroupLoadTask> task(GetPendingGroupLoadTask(manifest_url)); 1174 scoped_refptr<GroupLoadTask> task(GetPendingGroupLoadTask(manifest_url));
1096 if (task) { 1175 if (task) {
1097 task->AddDelegate(GetOrCreateDelegateReference(delegate)); 1176 task->AddDelegate(GetOrCreateDelegateReference(delegate));
1098 return; 1177 return;
1099 } 1178 }
1100 1179
1101 if (origins_with_groups_.find(manifest_url.GetOrigin()) == 1180 if (usage_map_.find(manifest_url.GetOrigin()) == usage_map_.end()) {
1102 origins_with_groups_.end()) {
1103 // No need to query the database, return a new group immediately. 1181 // No need to query the database, return a new group immediately.
1104 scoped_refptr<AppCacheGroup> group(new AppCacheGroup( 1182 scoped_refptr<AppCacheGroup> group(new AppCacheGroup(
1105 service_, manifest_url, NewGroupId())); 1183 service_, manifest_url, NewGroupId()));
1106 delegate->OnGroupLoaded(group, manifest_url); 1184 delegate->OnGroupLoaded(group, manifest_url);
1107 return; 1185 return;
1108 } 1186 }
1109 1187
1110 task = new GroupLoadTask(manifest_url, this); 1188 task = new GroupLoadTask(manifest_url, this);
1111 task->AddDelegate(GetOrCreateDelegateReference(delegate)); 1189 task->AddDelegate(GetOrCreateDelegateReference(delegate));
1112 task->Schedule(); 1190 task->Schedule();
1113 pending_group_loads_[manifest_url] = task.get(); 1191 pending_group_loads_[manifest_url] = task.get();
1114 } 1192 }
1115 1193
1116 void AppCacheStorageImpl::StoreGroupAndNewestCache( 1194 void AppCacheStorageImpl::StoreGroupAndNewestCache(
1117 AppCacheGroup* group, AppCache* newest_cache, Delegate* delegate) { 1195 AppCacheGroup* group, AppCache* newest_cache, Delegate* delegate) {
1118 // TODO(michaeln): distinguish between a simple update of an existing 1196 // TODO(michaeln): distinguish between a simple update of an existing
1119 // cache that just adds new master entry(s), and the insertion of a 1197 // cache that just adds new master entry(s), and the insertion of a
1120 // whole new cache. The StoreGroupAndCacheTask as written will handle 1198 // whole new cache. The StoreGroupAndCacheTask as written will handle
1121 // the simple update case in a very heavy weight way (delete all and 1199 // the simple update case in a very heavy weight way (delete all and
1122 // the reinsert all over again). 1200 // the reinsert all over again).
1123 DCHECK(group && delegate && newest_cache); 1201 DCHECK(group && delegate && newest_cache);
1124 scoped_refptr<StoreGroupAndCacheTask> task( 1202 scoped_refptr<StoreGroupAndCacheTask> task(
1125 new StoreGroupAndCacheTask(this, group, newest_cache)); 1203 new StoreGroupAndCacheTask(this, group, newest_cache));
1126 task->AddDelegate(GetOrCreateDelegateReference(delegate)); 1204 task->AddDelegate(GetOrCreateDelegateReference(delegate));
1127 task->Schedule(); 1205 task->GetQuotaThenSchedule();
1128 } 1206 }
1129 1207
1130 void AppCacheStorageImpl::FindResponseForMainRequest( 1208 void AppCacheStorageImpl::FindResponseForMainRequest(
1131 const GURL& url, const GURL& preferred_manifest_url, 1209 const GURL& url, const GURL& preferred_manifest_url,
1132 Delegate* delegate) { 1210 Delegate* delegate) {
1133 DCHECK(delegate); 1211 DCHECK(delegate);
1134 1212
1135 const GURL* url_ptr = &url; 1213 const GURL* url_ptr = &url;
1136 GURL url_no_ref; 1214 GURL url_no_ref;
1137 if (url.has_ref()) { 1215 if (url.has_ref()) {
(...skipping 23 matching lines...) Expand all
1161 groups_in_use->begin(); 1239 groups_in_use->begin();
1162 it != groups_in_use->end(); ++it) { 1240 it != groups_in_use->end(); ++it) {
1163 if (FindResponseForMainRequestInGroup( 1241 if (FindResponseForMainRequestInGroup(
1164 it->second, *url_ptr, delegate)) { 1242 it->second, *url_ptr, delegate)) {
1165 return; 1243 return;
1166 } 1244 }
1167 } 1245 }
1168 } 1246 }
1169 } 1247 }
1170 1248
1171 if (IsInitTaskComplete() && 1249 if (IsInitTaskComplete() && usage_map_.find(origin) == usage_map_.end()) {
1172 origins_with_groups_.find(origin) == origins_with_groups_.end()) {
1173 // No need to query the database, return async'ly but without going thru 1250 // No need to query the database, return async'ly but without going thru
1174 // the DB thread. 1251 // the DB thread.
1175 scoped_refptr<AppCacheGroup> no_group; 1252 scoped_refptr<AppCacheGroup> no_group;
1176 scoped_refptr<AppCache> no_cache; 1253 scoped_refptr<AppCache> no_cache;
1177 ScheduleSimpleTask(method_factory_.NewRunnableMethod( 1254 ScheduleSimpleTask(method_factory_.NewRunnableMethod(
1178 &AppCacheStorageImpl::DeliverShortCircuitedFindMainResponse, 1255 &AppCacheStorageImpl::DeliverShortCircuitedFindMainResponse,
1179 url, AppCacheEntry(), no_group, no_cache, 1256 url, AppCacheEntry(), no_group, no_cache,
1180 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); 1257 make_scoped_refptr(GetOrCreateDelegateReference(delegate))));
1181 return; 1258 return;
1182 } 1259 }
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
1489 Disable(); 1566 Disable();
1490 if (!is_incognito_) { 1567 if (!is_incognito_) {
1491 VLOG(1) << "Deleting existing appcache data and starting over."; 1568 VLOG(1) << "Deleting existing appcache data and starting over.";
1492 AppCacheThread::PostTask(AppCacheThread::db(), FROM_HERE, 1569 AppCacheThread::PostTask(AppCacheThread::db(), FROM_HERE,
1493 NewRunnableFunction(DeleteDirectory, cache_directory_)); 1570 NewRunnableFunction(DeleteDirectory, cache_directory_));
1494 } 1571 }
1495 } 1572 }
1496 } 1573 }
1497 1574
1498 } // namespace appcache 1575 } // namespace appcache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698