OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 sql::Connection* connection = database->db_connection(); | 95 sql::Connection* connection = database->db_connection(); |
96 if (!connection) { | 96 if (!connection) { |
97 NOTREACHED() << "Missing database connection."; | 97 NOTREACHED() << "Missing database connection."; |
98 return; | 98 return; |
99 } | 99 } |
100 | 100 |
101 std::set<GURL>::const_iterator origin; | 101 std::set<GURL>::const_iterator origin; |
102 for (origin = origins.begin(); origin != origins.end(); ++origin) { | 102 for (origin = origins.begin(); origin != origins.end(); ++origin) { |
103 if (!special_storage_policy->IsStorageSessionOnly(*origin)) | 103 if (!special_storage_policy->IsStorageSessionOnly(*origin)) |
104 continue; | 104 continue; |
105 if (special_storage_policy && | 105 if (special_storage_policy.get() && |
106 special_storage_policy->IsStorageProtected(*origin)) | 106 special_storage_policy->IsStorageProtected(*origin)) |
107 continue; | 107 continue; |
108 | 108 |
109 std::vector<AppCacheDatabase::GroupRecord> groups; | 109 std::vector<AppCacheDatabase::GroupRecord> groups; |
110 database->FindGroupsForOrigin(*origin, &groups); | 110 database->FindGroupsForOrigin(*origin, &groups); |
111 std::vector<AppCacheDatabase::GroupRecord>::const_iterator group; | 111 std::vector<AppCacheDatabase::GroupRecord>::const_iterator group; |
112 for (group = groups.begin(); group != groups.end(); ++group) { | 112 for (group = groups.begin(); group != groups.end(); ++group) { |
113 sql::Transaction transaction(connection); | 113 sql::Transaction transaction(connection); |
114 if (!transaction.Begin()) { | 114 if (!transaction.Begin()) { |
115 NOTREACHED() << "Failed to start transaction"; | 115 NOTREACHED() << "Failed to start transaction"; |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 info.cache_id = cache_record.cache_id; | 362 info.cache_id = cache_record.cache_id; |
363 info.group_id = group->group_id; | 363 info.group_id = group->group_id; |
364 info.is_complete = true; | 364 info.is_complete = true; |
365 infos.push_back(info); | 365 infos.push_back(info); |
366 } | 366 } |
367 } | 367 } |
368 } | 368 } |
369 | 369 |
370 void AppCacheStorageImpl::GetAllInfoTask::RunCompleted() { | 370 void AppCacheStorageImpl::GetAllInfoTask::RunCompleted() { |
371 DCHECK(delegates_.size() == 1); | 371 DCHECK(delegates_.size() == 1); |
372 FOR_EACH_DELEGATE(delegates_, OnAllInfo(info_collection_)); | 372 FOR_EACH_DELEGATE(delegates_, OnAllInfo(info_collection_.get())); |
373 } | 373 } |
374 | 374 |
375 // StoreOrLoadTask ------- | 375 // StoreOrLoadTask ------- |
376 | 376 |
377 class AppCacheStorageImpl::StoreOrLoadTask : public DatabaseTask { | 377 class AppCacheStorageImpl::StoreOrLoadTask : public DatabaseTask { |
378 protected: | 378 protected: |
379 explicit StoreOrLoadTask(AppCacheStorageImpl* storage) | 379 explicit StoreOrLoadTask(AppCacheStorageImpl* storage) |
380 : DatabaseTask(storage) {} | 380 : DatabaseTask(storage) {} |
381 virtual ~StoreOrLoadTask() {} | 381 virtual ~StoreOrLoadTask() {} |
382 | 382 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 } | 506 } |
507 | 507 |
508 void AppCacheStorageImpl::CacheLoadTask::RunCompleted() { | 508 void AppCacheStorageImpl::CacheLoadTask::RunCompleted() { |
509 storage_->pending_cache_loads_.erase(cache_id_); | 509 storage_->pending_cache_loads_.erase(cache_id_); |
510 scoped_refptr<AppCache> cache; | 510 scoped_refptr<AppCache> cache; |
511 scoped_refptr<AppCacheGroup> group; | 511 scoped_refptr<AppCacheGroup> group; |
512 if (success_ && !storage_->is_disabled()) { | 512 if (success_ && !storage_->is_disabled()) { |
513 DCHECK(cache_record_.cache_id == cache_id_); | 513 DCHECK(cache_record_.cache_id == cache_id_); |
514 CreateCacheAndGroupFromRecords(&cache, &group); | 514 CreateCacheAndGroupFromRecords(&cache, &group); |
515 } | 515 } |
516 FOR_EACH_DELEGATE(delegates_, OnCacheLoaded(cache, cache_id_)); | 516 FOR_EACH_DELEGATE(delegates_, OnCacheLoaded(cache.get(), cache_id_)); |
517 } | 517 } |
518 | 518 |
519 // GroupLoadTask ------- | 519 // GroupLoadTask ------- |
520 | 520 |
521 class AppCacheStorageImpl::GroupLoadTask : public StoreOrLoadTask { | 521 class AppCacheStorageImpl::GroupLoadTask : public StoreOrLoadTask { |
522 public: | 522 public: |
523 GroupLoadTask(GURL manifest_url, AppCacheStorageImpl* storage) | 523 GroupLoadTask(GURL manifest_url, AppCacheStorageImpl* storage) |
524 : StoreOrLoadTask(storage), manifest_url_(manifest_url), | 524 : StoreOrLoadTask(storage), manifest_url_(manifest_url), |
525 success_(false) {} | 525 success_(false) {} |
526 | 526 |
(...skipping 23 matching lines...) Expand all Loading... |
550 void AppCacheStorageImpl::GroupLoadTask::RunCompleted() { | 550 void AppCacheStorageImpl::GroupLoadTask::RunCompleted() { |
551 storage_->pending_group_loads_.erase(manifest_url_); | 551 storage_->pending_group_loads_.erase(manifest_url_); |
552 scoped_refptr<AppCacheGroup> group; | 552 scoped_refptr<AppCacheGroup> group; |
553 scoped_refptr<AppCache> cache; | 553 scoped_refptr<AppCache> cache; |
554 if (!storage_->is_disabled()) { | 554 if (!storage_->is_disabled()) { |
555 if (success_) { | 555 if (success_) { |
556 DCHECK(group_record_.manifest_url == manifest_url_); | 556 DCHECK(group_record_.manifest_url == manifest_url_); |
557 CreateCacheAndGroupFromRecords(&cache, &group); | 557 CreateCacheAndGroupFromRecords(&cache, &group); |
558 } else { | 558 } else { |
559 group = storage_->working_set_.GetGroup(manifest_url_); | 559 group = storage_->working_set_.GetGroup(manifest_url_); |
560 if (!group) { | 560 if (!group.get()) { |
561 group = new AppCacheGroup( | 561 group = new AppCacheGroup( |
562 storage_->service_, manifest_url_, storage_->NewGroupId()); | 562 storage_->service_, manifest_url_, storage_->NewGroupId()); |
563 } | 563 } |
564 } | 564 } |
565 } | 565 } |
566 FOR_EACH_DELEGATE(delegates_, OnGroupLoaded(group, manifest_url_)); | 566 FOR_EACH_DELEGATE(delegates_, OnGroupLoaded(group.get(), manifest_url_)); |
567 } | 567 } |
568 | 568 |
569 // StoreGroupAndCacheTask ------- | 569 // StoreGroupAndCacheTask ------- |
570 | 570 |
571 class AppCacheStorageImpl::StoreGroupAndCacheTask : public StoreOrLoadTask { | 571 class AppCacheStorageImpl::StoreGroupAndCacheTask : public StoreOrLoadTask { |
572 public: | 572 public: |
573 StoreGroupAndCacheTask(AppCacheStorageImpl* storage, AppCacheGroup* group, | 573 StoreGroupAndCacheTask(AppCacheStorageImpl* storage, AppCacheGroup* group, |
574 AppCache* newest_cache); | 574 AppCache* newest_cache); |
575 | 575 |
576 void GetQuotaThenSchedule(); | 576 void GetQuotaThenSchedule(); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 return; | 745 return; |
746 } | 746 } |
747 | 747 |
748 success_ = transaction.Commit(); | 748 success_ = transaction.Commit(); |
749 } | 749 } |
750 | 750 |
751 void AppCacheStorageImpl::StoreGroupAndCacheTask::RunCompleted() { | 751 void AppCacheStorageImpl::StoreGroupAndCacheTask::RunCompleted() { |
752 if (success_) { | 752 if (success_) { |
753 storage_->UpdateUsageMapAndNotify( | 753 storage_->UpdateUsageMapAndNotify( |
754 group_->manifest_url().GetOrigin(), new_origin_usage_); | 754 group_->manifest_url().GetOrigin(), new_origin_usage_); |
755 if (cache_ != group_->newest_complete_cache()) { | 755 if (cache_.get() != group_->newest_complete_cache()) { |
756 cache_->set_complete(true); | 756 cache_->set_complete(true); |
757 group_->AddCache(cache_); | 757 group_->AddCache(cache_.get()); |
758 } | 758 } |
759 if (group_->creation_time().is_null()) | 759 if (group_->creation_time().is_null()) |
760 group_->set_creation_time(group_record_.creation_time); | 760 group_->set_creation_time(group_record_.creation_time); |
761 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_); | 761 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_); |
762 } | 762 } |
763 FOR_EACH_DELEGATE(delegates_, | 763 FOR_EACH_DELEGATE(delegates_, |
764 OnGroupAndNewestCacheStored(group_, cache_, success_, | 764 OnGroupAndNewestCacheStored( |
765 would_exceed_quota_)); | 765 group_.get(), cache_.get(), success_, |
| 766 would_exceed_quota_)); |
766 group_ = NULL; | 767 group_ = NULL; |
767 cache_ = NULL; | 768 cache_ = NULL; |
768 | 769 |
769 // TODO(michaeln): if (would_exceed_quota_) what if the current usage | 770 // TODO(michaeln): if (would_exceed_quota_) what if the current usage |
770 // also exceeds the quota? http://crbug.com/83968 | 771 // also exceeds the quota? http://crbug.com/83968 |
771 } | 772 } |
772 | 773 |
773 void AppCacheStorageImpl::StoreGroupAndCacheTask::CancelCompletion() { | 774 void AppCacheStorageImpl::StoreGroupAndCacheTask::CancelCompletion() { |
774 // Overriden to safely drop our reference to the group and cache | 775 // Overriden to safely drop our reference to the group and cache |
775 // which are not thread safe refcounted. | 776 // which are not thread safe refcounted. |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1161 void AppCacheStorageImpl::MakeGroupObsoleteTask::RunCompleted() { | 1162 void AppCacheStorageImpl::MakeGroupObsoleteTask::RunCompleted() { |
1162 if (success_) { | 1163 if (success_) { |
1163 group_->set_obsolete(true); | 1164 group_->set_obsolete(true); |
1164 if (!storage_->is_disabled()) { | 1165 if (!storage_->is_disabled()) { |
1165 storage_->UpdateUsageMapAndNotify(origin_, new_origin_usage_); | 1166 storage_->UpdateUsageMapAndNotify(origin_, new_origin_usage_); |
1166 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_); | 1167 group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_); |
1167 | 1168 |
1168 // Also remove from the working set, caches for an 'obsolete' group | 1169 // Also remove from the working set, caches for an 'obsolete' group |
1169 // may linger in use, but the group itself cannot be looked up by | 1170 // may linger in use, but the group itself cannot be looked up by |
1170 // 'manifest_url' in the working set any longer. | 1171 // 'manifest_url' in the working set any longer. |
1171 storage_->working_set()->RemoveGroup(group_); | 1172 storage_->working_set()->RemoveGroup(group_.get()); |
1172 } | 1173 } |
1173 } | 1174 } |
1174 FOR_EACH_DELEGATE(delegates_, OnGroupMadeObsolete(group_, success_)); | 1175 FOR_EACH_DELEGATE(delegates_, OnGroupMadeObsolete(group_.get(), success_)); |
1175 group_ = NULL; | 1176 group_ = NULL; |
1176 } | 1177 } |
1177 | 1178 |
1178 void AppCacheStorageImpl::MakeGroupObsoleteTask::CancelCompletion() { | 1179 void AppCacheStorageImpl::MakeGroupObsoleteTask::CancelCompletion() { |
1179 // Overriden to safely drop our reference to the group | 1180 // Overriden to safely drop our reference to the group |
1180 // which is not thread safe refcounted. | 1181 // which is not thread safe refcounted. |
1181 DatabaseTask::CancelCompletion(); | 1182 DatabaseTask::CancelCompletion(); |
1182 group_ = NULL; | 1183 group_ = NULL; |
1183 } | 1184 } |
1184 | 1185 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1364 delegate->OnCacheLoaded(cache, id); | 1365 delegate->OnCacheLoaded(cache, id); |
1365 if (cache->owning_group()) { | 1366 if (cache->owning_group()) { |
1366 scoped_refptr<DatabaseTask> update_task( | 1367 scoped_refptr<DatabaseTask> update_task( |
1367 new UpdateGroupLastAccessTimeTask( | 1368 new UpdateGroupLastAccessTimeTask( |
1368 this, cache->owning_group(), base::Time::Now())); | 1369 this, cache->owning_group(), base::Time::Now())); |
1369 update_task->Schedule(); | 1370 update_task->Schedule(); |
1370 } | 1371 } |
1371 return; | 1372 return; |
1372 } | 1373 } |
1373 scoped_refptr<CacheLoadTask> task(GetPendingCacheLoadTask(id)); | 1374 scoped_refptr<CacheLoadTask> task(GetPendingCacheLoadTask(id)); |
1374 if (task) { | 1375 if (task.get()) { |
1375 task->AddDelegate(GetOrCreateDelegateReference(delegate)); | 1376 task->AddDelegate(GetOrCreateDelegateReference(delegate)); |
1376 return; | 1377 return; |
1377 } | 1378 } |
1378 task = new CacheLoadTask(id, this); | 1379 task = new CacheLoadTask(id, this); |
1379 task->AddDelegate(GetOrCreateDelegateReference(delegate)); | 1380 task->AddDelegate(GetOrCreateDelegateReference(delegate)); |
1380 task->Schedule(); | 1381 task->Schedule(); |
1381 pending_cache_loads_[id] = task; | 1382 pending_cache_loads_[id] = task.get(); |
1382 } | 1383 } |
1383 | 1384 |
1384 void AppCacheStorageImpl::LoadOrCreateGroup( | 1385 void AppCacheStorageImpl::LoadOrCreateGroup( |
1385 const GURL& manifest_url, Delegate* delegate) { | 1386 const GURL& manifest_url, Delegate* delegate) { |
1386 DCHECK(delegate); | 1387 DCHECK(delegate); |
1387 if (is_disabled_) { | 1388 if (is_disabled_) { |
1388 delegate->OnGroupLoaded(NULL, manifest_url); | 1389 delegate->OnGroupLoaded(NULL, manifest_url); |
1389 return; | 1390 return; |
1390 } | 1391 } |
1391 | 1392 |
1392 AppCacheGroup* group = working_set_.GetGroup(manifest_url); | 1393 AppCacheGroup* group = working_set_.GetGroup(manifest_url); |
1393 if (group) { | 1394 if (group) { |
1394 delegate->OnGroupLoaded(group, manifest_url); | 1395 delegate->OnGroupLoaded(group, manifest_url); |
1395 scoped_refptr<DatabaseTask> update_task( | 1396 scoped_refptr<DatabaseTask> update_task( |
1396 new UpdateGroupLastAccessTimeTask( | 1397 new UpdateGroupLastAccessTimeTask( |
1397 this, group, base::Time::Now())); | 1398 this, group, base::Time::Now())); |
1398 update_task->Schedule(); | 1399 update_task->Schedule(); |
1399 return; | 1400 return; |
1400 } | 1401 } |
1401 | 1402 |
1402 scoped_refptr<GroupLoadTask> task(GetPendingGroupLoadTask(manifest_url)); | 1403 scoped_refptr<GroupLoadTask> task(GetPendingGroupLoadTask(manifest_url)); |
1403 if (task) { | 1404 if (task.get()) { |
1404 task->AddDelegate(GetOrCreateDelegateReference(delegate)); | 1405 task->AddDelegate(GetOrCreateDelegateReference(delegate)); |
1405 return; | 1406 return; |
1406 } | 1407 } |
1407 | 1408 |
1408 if (usage_map_.find(manifest_url.GetOrigin()) == usage_map_.end()) { | 1409 if (usage_map_.find(manifest_url.GetOrigin()) == usage_map_.end()) { |
1409 // No need to query the database, return a new group immediately. | 1410 // No need to query the database, return a new group immediately. |
1410 scoped_refptr<AppCacheGroup> group(new AppCacheGroup( | 1411 scoped_refptr<AppCacheGroup> group(new AppCacheGroup( |
1411 service_, manifest_url, NewGroupId())); | 1412 service_, manifest_url, NewGroupId())); |
1412 delegate->OnGroupLoaded(group, manifest_url); | 1413 delegate->OnGroupLoaded(group.get(), manifest_url); |
1413 return; | 1414 return; |
1414 } | 1415 } |
1415 | 1416 |
1416 task = new GroupLoadTask(manifest_url, this); | 1417 task = new GroupLoadTask(manifest_url, this); |
1417 task->AddDelegate(GetOrCreateDelegateReference(delegate)); | 1418 task->AddDelegate(GetOrCreateDelegateReference(delegate)); |
1418 task->Schedule(); | 1419 task->Schedule(); |
1419 pending_group_loads_[manifest_url] = task.get(); | 1420 pending_group_loads_[manifest_url] = task.get(); |
1420 } | 1421 } |
1421 | 1422 |
1422 void AppCacheStorageImpl::StoreGroupAndNewestCache( | 1423 void AppCacheStorageImpl::StoreGroupAndNewestCache( |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1769 int rv = net::OK; | 1770 int rv = net::OK; |
1770 disk_cache_.reset(new AppCacheDiskCache); | 1771 disk_cache_.reset(new AppCacheDiskCache); |
1771 if (is_incognito_) { | 1772 if (is_incognito_) { |
1772 rv = disk_cache_->InitWithMemBackend( | 1773 rv = disk_cache_->InitWithMemBackend( |
1773 kMaxMemDiskCacheSize, | 1774 kMaxMemDiskCacheSize, |
1774 base::Bind(&AppCacheStorageImpl::OnDiskCacheInitialized, | 1775 base::Bind(&AppCacheStorageImpl::OnDiskCacheInitialized, |
1775 base::Unretained(this))); | 1776 base::Unretained(this))); |
1776 } else { | 1777 } else { |
1777 rv = disk_cache_->InitWithDiskBackend( | 1778 rv = disk_cache_->InitWithDiskBackend( |
1778 cache_directory_.Append(kDiskCacheDirectoryName), | 1779 cache_directory_.Append(kDiskCacheDirectoryName), |
1779 kMaxDiskCacheSize, false, cache_thread_, | 1780 kMaxDiskCacheSize, false, cache_thread_.get(), |
1780 base::Bind(&AppCacheStorageImpl::OnDiskCacheInitialized, | 1781 base::Bind(&AppCacheStorageImpl::OnDiskCacheInitialized, |
1781 base::Unretained(this))); | 1782 base::Unretained(this))); |
1782 } | 1783 } |
1783 | 1784 |
1784 // We should not keep this reference around. | 1785 // We should not keep this reference around. |
1785 cache_thread_ = NULL; | 1786 cache_thread_ = NULL; |
1786 | 1787 |
1787 if (rv != net::ERR_IO_PENDING) | 1788 if (rv != net::ERR_IO_PENDING) |
1788 OnDiskCacheInitialized(rv); | 1789 OnDiskCacheInitialized(rv); |
1789 } | 1790 } |
(...skipping 13 matching lines...) Expand all Loading... |
1803 if (!is_incognito_) { | 1804 if (!is_incognito_) { |
1804 VLOG(1) << "Deleting existing appcache data and starting over."; | 1805 VLOG(1) << "Deleting existing appcache data and starting over."; |
1805 db_thread_->PostTask( | 1806 db_thread_->PostTask( |
1806 FROM_HERE, base::Bind(base::IgnoreResult(&file_util::Delete), | 1807 FROM_HERE, base::Bind(base::IgnoreResult(&file_util::Delete), |
1807 cache_directory_, true)); | 1808 cache_directory_, true)); |
1808 } | 1809 } |
1809 } | 1810 } |
1810 } | 1811 } |
1811 | 1812 |
1812 } // namespace appcache | 1813 } // namespace appcache |
OLD | NEW |