Chromium Code Reviews| Index: webkit/appcache/appcache_storage_impl.cc |
| =================================================================== |
| --- webkit/appcache/appcache_storage_impl.cc (revision 87543) |
| +++ webkit/appcache/appcache_storage_impl.cc (working copy) |
| @@ -4,6 +4,8 @@ |
| #include "webkit/appcache/appcache_storage_impl.h" |
| +#include <set> |
| + |
| #include "app/sql/connection.h" |
| #include "app/sql/transaction.h" |
| #include "base/file_util.h" |
| @@ -19,9 +21,12 @@ |
| #include "webkit/appcache/appcache_group.h" |
| #include "webkit/appcache/appcache_histograms.h" |
| #include "webkit/appcache/appcache_policy.h" |
| +#include "webkit/appcache/appcache_quota_client.h" |
| #include "webkit/appcache/appcache_response.h" |
| #include "webkit/appcache/appcache_service.h" |
| #include "webkit/appcache/appcache_thread.h" |
| +#include "webkit/quota/quota_client.h" |
| +#include "webkit/quota/quota_manager.h" |
| #include "webkit/quota/special_storage_policy.h" |
| namespace { |
| @@ -146,14 +151,14 @@ |
| int64 last_cache_id_; |
| int64 last_response_id_; |
| int64 last_deletable_response_rowid_; |
| - std::set<GURL> origins_with_groups_; |
| + std::map<GURL, int64> usage_map_; |
| }; |
| void AppCacheStorageImpl::InitTask::Run() { |
| database_->FindLastStorageIds( |
| &last_group_id_, &last_cache_id_, &last_response_id_, |
| &last_deletable_response_rowid_); |
| - database_->FindOriginsWithGroups(&origins_with_groups_); |
| + database_->GetAllOriginUsage(&usage_map_); |
| } |
| void AppCacheStorageImpl::InitTask::RunCompleted() { |
| @@ -163,14 +168,16 @@ |
| storage_->last_deletable_response_rowid_ = last_deletable_response_rowid_; |
| if (!storage_->is_disabled()) { |
| - storage_->origins_with_groups_.swap(origins_with_groups_); |
| - |
| + storage_->usage_map_.swap(usage_map_); |
| const int kDelayMillis = 5 * 60 * 1000; // Five minutes. |
| MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| storage_->method_factory_.NewRunnableMethod( |
| &AppCacheStorageImpl::DelayedStartDeletingUnusedResponses), |
| kDelayMillis); |
| } |
| + |
| + if (storage_->service()->quota_client()) |
| + storage_->service()->quota_client()->NotifyAppCacheReady(); |
| } |
| // CloseConnectionTask ------- |
| @@ -302,6 +309,8 @@ |
| cache->get()->GetEntry(*iter)->add_types(AppCacheEntry::FOREIGN); |
| } |
| + storage_->NotifyStorageAccessed(group_record_.origin); |
| + |
| // TODO(michaeln): Maybe verify that the responses we expect to exist |
| // do actually exist in the disk_cache (and if not then what?) |
| } |
| @@ -404,6 +413,7 @@ |
| bool success_; |
| bool would_exceed_quota_; |
| int64 quota_override_; |
| + int64 new_origin_usage_; |
| std::vector<int64> newly_deletable_response_ids_; |
| }; |
| @@ -411,7 +421,7 @@ |
| AppCacheStorageImpl* storage, AppCacheGroup* group, AppCache* newest_cache) |
| : StoreOrLoadTask(storage), group_(group), cache_(newest_cache), |
| success_(false), would_exceed_quota_(false), |
| - quota_override_(-1) { |
| + quota_override_(-1), new_origin_usage_(-1) { |
| group_record_.group_id = group->group_id(); |
| group_record_.manifest_url = group->manifest_url(); |
| group_record_.origin = group_record_.manifest_url.GetOrigin(); |
| @@ -498,7 +508,8 @@ |
| quota_override_ : |
| database_->GetOriginQuota(group_record_.origin); |
| - if (database_->GetOriginUsage(group_record_.origin) > quota) { |
| + new_origin_usage_ = database_->GetOriginUsage(group_record_.origin); |
| + if (new_origin_usage_ > quota) { |
| would_exceed_quota_ = true; |
| success_ = false; |
| return; |
| @@ -509,7 +520,8 @@ |
| void AppCacheStorageImpl::StoreGroupAndCacheTask::RunCompleted() { |
| if (success_) { |
| - storage_->origins_with_groups_.insert(group_->manifest_url().GetOrigin()); |
| + storage_->UpdateUsageMapAndNotify( |
| + group_->manifest_url().GetOrigin(), new_origin_usage_); |
| if (cache_ != group_->newest_complete_cache()) { |
| cache_->set_complete(true); |
| group_->AddCache(cache_); |
| @@ -837,15 +849,17 @@ |
| scoped_refptr<AppCacheGroup> group_; |
| int64 group_id_; |
| + GURL origin_; |
| bool success_; |
| - std::set<GURL> origins_with_groups_; |
| + int64 new_origin_usage_; |
| std::vector<int64> newly_deletable_response_ids_; |
| }; |
| AppCacheStorageImpl::MakeGroupObsoleteTask::MakeGroupObsoleteTask( |
| AppCacheStorageImpl* storage, AppCacheGroup* group) |
| : DatabaseTask(storage), group_(group), group_id_(group->group_id()), |
| - success_(false) { |
| + origin_(group->manifest_url().GetOrigin()), |
| + success_(false), new_origin_usage_(-1) { |
| } |
| void AppCacheStorageImpl::MakeGroupObsoleteTask::Run() { |
| @@ -861,10 +875,13 @@ |
| AppCacheDatabase::GroupRecord group_record; |
| if (!database_->FindGroup(group_id_, &group_record)) { |
| // This group doesn't exists in the database, nothing todo here. |
| + new_origin_usage_ = database_->GetOriginUsage(origin_); |
| success_ = true; |
| return; |
| } |
| + DCHECK_EQ(group_record.origin, origin_); |
| + |
| AppCacheDatabase::CacheRecord cache_record; |
| if (database_->FindCacheForGroup(group_id_, &cache_record)) { |
| database_->FindResponseIdsForCacheAsVector(cache_record.cache_id, |
| @@ -881,16 +898,15 @@ |
| success_ = database_->DeleteGroup(group_id_); |
| } |
| - success_ = success_ && |
| - database_->FindOriginsWithGroups(&origins_with_groups_) && |
| - transaction.Commit(); |
| + new_origin_usage_ = database_->GetOriginUsage(origin_); |
| + success_ = success_ && transaction.Commit(); |
| } |
| void AppCacheStorageImpl::MakeGroupObsoleteTask::RunCompleted() { |
| if (success_) { |
| group_->set_obsolete(true); |
| if (!storage_->is_disabled()) { |
| - storage_->origins_with_groups_.swap(origins_with_groups_); |
| + storage_->UpdateUsageMapAndNotify(origin_, new_origin_usage_); |
| group_->AddNewlyDeletableResponseIds(&newly_deletable_response_ids_); |
| // Also remove from the working set, caches for an 'obsolete' group |
| @@ -970,8 +986,11 @@ |
| : public DatabaseTask { |
| public: |
| UpdateGroupLastAccessTimeTask( |
| - AppCacheStorageImpl* storage, int64 group_id, base::Time time) |
| - : DatabaseTask(storage), group_id_(group_id), last_access_time_(time) {} |
| + AppCacheStorageImpl* storage, AppCacheGroup* group, base::Time time) |
| + : DatabaseTask(storage), group_id_(group->group_id()), |
| + last_access_time_(time) { |
| + storage->NotifyStorageAccessed(group->manifest_url().GetOrigin()); |
| + } |
| virtual void Run(); |
| @@ -1030,7 +1049,7 @@ |
| return; |
| VLOG(1) << "Disabling appcache storage."; |
| is_disabled_ = true; |
| - origins_with_groups_.clear(); |
| + usage_map_.clear(); // TODO(michaeln): What to tell the QuotaManager? |
|
kinuko
2011/06/02 08:38:06
hmm
do we actually delete data when we disable th
michaeln
2011/06/02 22:46:55
Being disabled is being in an error condition.
By
|
| working_set()->Disable(); |
| if (disk_cache_.get()) |
| disk_cache_->Disable(); |
| @@ -1058,7 +1077,7 @@ |
| if (cache->owning_group()) { |
| scoped_refptr<DatabaseTask> update_task( |
| new UpdateGroupLastAccessTimeTask( |
| - this, cache->owning_group()->group_id(), base::Time::Now())); |
| + this, cache->owning_group(), base::Time::Now())); |
| update_task->Schedule(); |
| } |
| return; |
| @@ -1087,7 +1106,7 @@ |
| delegate->OnGroupLoaded(group, manifest_url); |
| scoped_refptr<DatabaseTask> update_task( |
| new UpdateGroupLastAccessTimeTask( |
| - this, group->group_id(), base::Time::Now())); |
| + this, group, base::Time::Now())); |
| update_task->Schedule(); |
| return; |
| } |
| @@ -1098,8 +1117,7 @@ |
| return; |
| } |
| - if (origins_with_groups_.find(manifest_url.GetOrigin()) == |
| - origins_with_groups_.end()) { |
| + if (usage_map_.find(manifest_url.GetOrigin()) == usage_map_.end()) { |
| // No need to query the database, return a new group immediately. |
| scoped_refptr<AppCacheGroup> group(new AppCacheGroup( |
| service_, manifest_url, NewGroupId())); |
| @@ -1115,6 +1133,8 @@ |
| void AppCacheStorageImpl::StoreGroupAndNewestCache( |
| AppCacheGroup* group, AppCache* newest_cache, Delegate* delegate) { |
| + // TODO: get quota from quotamanager, then continue. |
| + |
| // TODO(michaeln): distinguish between a simple update of an existing |
| // cache that just adds new master entry(s), and the insertion of a |
| // whole new cache. The StoreGroupAndCacheTask as written will handle |
| @@ -1168,8 +1188,7 @@ |
| } |
| } |
| - if (IsInitTaskComplete() && |
| - origins_with_groups_.find(origin) == origins_with_groups_.end()) { |
| + if (IsInitTaskComplete() && usage_map_.find(origin) == usage_map_.end()) { |
| // No need to query the database, return async'ly but without going thru |
| // the DB thread. |
| scoped_refptr<AppCacheGroup> no_group; |