Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> | 7 #include <set> |
| 8 | 8 |
| 9 #include "app/sql/connection.h" | 9 #include "app/sql/connection.h" |
| 10 #include "app/sql/transaction.h" | 10 #include "app/sql/transaction.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "webkit/appcache/appcache_histograms.h" | 22 #include "webkit/appcache/appcache_histograms.h" |
| 23 #include "webkit/appcache/appcache_policy.h" | 23 #include "webkit/appcache/appcache_policy.h" |
| 24 #include "webkit/appcache/appcache_quota_client.h" | 24 #include "webkit/appcache/appcache_quota_client.h" |
| 25 #include "webkit/appcache/appcache_response.h" | 25 #include "webkit/appcache/appcache_response.h" |
| 26 #include "webkit/appcache/appcache_service.h" | 26 #include "webkit/appcache/appcache_service.h" |
| 27 #include "webkit/appcache/appcache_thread.h" | 27 #include "webkit/appcache/appcache_thread.h" |
| 28 #include "webkit/quota/quota_client.h" | 28 #include "webkit/quota/quota_client.h" |
| 29 #include "webkit/quota/quota_manager.h" | 29 #include "webkit/quota/quota_manager.h" |
| 30 #include "webkit/quota/special_storage_policy.h" | 30 #include "webkit/quota/special_storage_policy.h" |
| 31 | 31 |
| 32 namespace { | |
| 33 // Helper with no return value for use with NewRunnableFunction. | |
| 34 void DeleteDirectory(const FilePath& path) { | |
| 35 file_util::Delete(path, true); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 namespace appcache { | 32 namespace appcache { |
| 40 | 33 |
| 41 // Hard coded default when not using quota management. | 34 // Hard coded default when not using quota management. |
| 42 static const int kDefaultQuota = 5 * 1024 * 1024; | 35 static const int kDefaultQuota = 5 * 1024 * 1024; |
| 43 | 36 |
| 44 static const int kMaxDiskCacheSize = 250 * 1024 * 1024; | 37 static const int kMaxDiskCacheSize = 250 * 1024 * 1024; |
| 45 static const int kMaxMemDiskCacheSize = 10 * 1024 * 1024; | 38 static const int kMaxMemDiskCacheSize = 10 * 1024 * 1024; |
| 46 static const FilePath::CharType kDiskCacheDirectoryName[] = | 39 static const FilePath::CharType kDiskCacheDirectoryName[] = |
| 47 FILE_PATH_LITERAL("Cache"); | 40 FILE_PATH_LITERAL("Cache"); |
| 48 | 41 |
| 42 namespace { | |
| 43 // Helper with no return value for use with NewRunnableFunction. | |
| 44 void DeleteDirectory(const FilePath& path) { | |
| 45 file_util::Delete(path, true); | |
| 46 } | |
| 47 | |
| 48 // Helpers for clearing data from the AppCacheDatabase. | |
| 49 bool DeleteGroupAndRelatedRecords(AppCacheDatabase* database, | |
|
michaeln
2011/07/15 19:15:46
Thnx for refactoring!
| |
| 50 int64 group_id, | |
| 51 std::vector<int64>* deletable_response_ids) { | |
| 52 AppCacheDatabase::CacheRecord cache_record; | |
| 53 bool success = false; | |
| 54 if (database->FindCacheForGroup(group_id, &cache_record)) { | |
| 55 database->FindResponseIdsForCacheAsVector(cache_record.cache_id, | |
| 56 deletable_response_ids); | |
| 57 success = | |
| 58 database->DeleteGroup(group_id) && | |
| 59 database->DeleteCache(cache_record.cache_id) && | |
| 60 database->DeleteEntriesForCache(cache_record.cache_id) && | |
| 61 database->DeleteFallbackNameSpacesForCache(cache_record.cache_id) && | |
| 62 database->DeleteOnlineWhiteListForCache(cache_record.cache_id) && | |
| 63 database->InsertDeletableResponseIds(*deletable_response_ids); | |
| 64 } else { | |
| 65 NOTREACHED() << "A existing group without a cache is unexpected"; | |
| 66 success = database->DeleteGroup(group_id); | |
| 67 } | |
| 68 return success; | |
| 69 } | |
| 70 | |
| 71 void ClearOnExit( | |
| 72 AppCacheDatabase* database, | |
| 73 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) { | |
| 74 std::set<GURL> origins; | |
| 75 database->FindOriginsWithGroups(&origins); | |
| 76 if (origins.empty()) | |
| 77 return; // nothing to delete | |
| 78 | |
| 79 sql::Connection* connection = database->db_connection(); | |
| 80 if (!connection) { | |
| 81 NOTREACHED() << "Missing database connection."; | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 std::set<GURL>::const_iterator origin; | |
| 86 for (origin = origins.begin(); origin != origins.end(); ++origin) { | |
| 87 if (special_storage_policy && | |
| 88 special_storage_policy->IsStorageProtected(*origin)) | |
| 89 continue; | |
| 90 | |
| 91 std::vector<AppCacheDatabase::GroupRecord> groups; | |
| 92 database->FindGroupsForOrigin(*origin, &groups); | |
| 93 std::vector<AppCacheDatabase::GroupRecord>::const_iterator group; | |
| 94 for (group = groups.begin(); group != groups.end(); ++group) { | |
| 95 sql::Transaction transaction(connection); | |
| 96 if (!transaction.Begin()) { | |
| 97 NOTREACHED() << "Failed to start transaction"; | |
| 98 return; | |
| 99 } | |
| 100 std::vector<int64> deletable_response_ids; | |
| 101 bool success = DeleteGroupAndRelatedRecords(database, | |
| 102 group->group_id, | |
| 103 &deletable_response_ids); | |
| 104 success = success && transaction.Commit(); | |
| 105 DCHECK(success); | |
| 106 } // for each group | |
| 107 } // for each origin | |
| 108 } | |
| 109 | |
| 110 } // namespace | |
| 111 | |
| 49 // DatabaseTask ----------------------------------------- | 112 // DatabaseTask ----------------------------------------- |
| 50 | 113 |
| 51 class AppCacheStorageImpl::DatabaseTask | 114 class AppCacheStorageImpl::DatabaseTask |
| 52 : public base::RefCountedThreadSafe<DatabaseTask> { | 115 : public base::RefCountedThreadSafe<DatabaseTask> { |
| 53 public: | 116 public: |
| 54 explicit DatabaseTask(AppCacheStorageImpl* storage) | 117 explicit DatabaseTask(AppCacheStorageImpl* storage) |
| 55 : storage_(storage), database_(storage->database_) {} | 118 : storage_(storage), database_(storage->database_) {} |
| 56 | 119 |
| 57 virtual ~DatabaseTask() {} | 120 virtual ~DatabaseTask() {} |
| 58 | 121 |
| (...skipping 875 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 934 | 997 |
| 935 AppCacheDatabase::GroupRecord group_record; | 998 AppCacheDatabase::GroupRecord group_record; |
| 936 if (!database_->FindGroup(group_id_, &group_record)) { | 999 if (!database_->FindGroup(group_id_, &group_record)) { |
| 937 // This group doesn't exists in the database, nothing todo here. | 1000 // This group doesn't exists in the database, nothing todo here. |
| 938 new_origin_usage_ = database_->GetOriginUsage(origin_); | 1001 new_origin_usage_ = database_->GetOriginUsage(origin_); |
| 939 success_ = true; | 1002 success_ = true; |
| 940 return; | 1003 return; |
| 941 } | 1004 } |
| 942 | 1005 |
| 943 DCHECK_EQ(group_record.origin, origin_); | 1006 DCHECK_EQ(group_record.origin, origin_); |
| 944 | 1007 success_ = DeleteGroupAndRelatedRecords(database_, |
| 945 AppCacheDatabase::CacheRecord cache_record; | 1008 group_id_, |
| 946 if (database_->FindCacheForGroup(group_id_, &cache_record)) { | 1009 &newly_deletable_response_ids_); |
| 947 database_->FindResponseIdsForCacheAsVector(cache_record.cache_id, | |
| 948 &newly_deletable_response_ids_); | |
| 949 success_ = | |
| 950 database_->DeleteGroup(group_id_) && | |
| 951 database_->DeleteCache(cache_record.cache_id) && | |
| 952 database_->DeleteEntriesForCache(cache_record.cache_id) && | |
| 953 database_->DeleteFallbackNameSpacesForCache(cache_record.cache_id) && | |
| 954 database_->DeleteOnlineWhiteListForCache(cache_record.cache_id) && | |
| 955 database_->InsertDeletableResponseIds(newly_deletable_response_ids_); | |
| 956 } else { | |
| 957 NOTREACHED() << "A existing group without a cache is unexpected"; | |
| 958 success_ = database_->DeleteGroup(group_id_); | |
| 959 } | |
| 960 | 1010 |
| 961 new_origin_usage_ = database_->GetOriginUsage(origin_); | 1011 new_origin_usage_ = database_->GetOriginUsage(origin_); |
| 962 success_ = success_ && transaction.Commit(); | 1012 success_ = success_ && transaction.Commit(); |
| 963 } | 1013 } |
| 964 | 1014 |
| 965 void AppCacheStorageImpl::MakeGroupObsoleteTask::RunCompleted() { | 1015 void AppCacheStorageImpl::MakeGroupObsoleteTask::RunCompleted() { |
| 966 if (success_) { | 1016 if (success_) { |
| 967 group_->set_obsolete(true); | 1017 group_->set_obsolete(true); |
| 968 if (!storage_->is_disabled()) { | 1018 if (!storage_->is_disabled()) { |
| 969 storage_->UpdateUsageMapAndNotify(origin_, new_origin_usage_); | 1019 storage_->UpdateUsageMapAndNotify(origin_, new_origin_usage_); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1081 AppCacheStorageImpl::~AppCacheStorageImpl() { | 1131 AppCacheStorageImpl::~AppCacheStorageImpl() { |
| 1082 STLDeleteElements(&pending_simple_tasks_); | 1132 STLDeleteElements(&pending_simple_tasks_); |
| 1083 | 1133 |
| 1084 std::for_each(pending_quota_queries_.begin(), | 1134 std::for_each(pending_quota_queries_.begin(), |
| 1085 pending_quota_queries_.end(), | 1135 pending_quota_queries_.end(), |
| 1086 std::mem_fun(&DatabaseTask::CancelCompletion)); | 1136 std::mem_fun(&DatabaseTask::CancelCompletion)); |
| 1087 std::for_each(scheduled_database_tasks_.begin(), | 1137 std::for_each(scheduled_database_tasks_.begin(), |
| 1088 scheduled_database_tasks_.end(), | 1138 scheduled_database_tasks_.end(), |
| 1089 std::mem_fun(&DatabaseTask::CancelCompletion)); | 1139 std::mem_fun(&DatabaseTask::CancelCompletion)); |
| 1090 | 1140 |
| 1091 if (database_) | 1141 if (database_) { |
| 1142 if (service()->clear_local_state_on_exit()) { | |
| 1143 AppCacheThread::PostTask( | |
| 1144 AppCacheThread::db(), | |
| 1145 FROM_HERE, | |
| 1146 NewRunnableFunction( | |
| 1147 ClearOnExit, | |
| 1148 database_, | |
| 1149 make_scoped_refptr(service_->special_storage_policy()))); | |
| 1150 } | |
| 1092 AppCacheThread::DeleteSoon(AppCacheThread::db(), FROM_HERE, database_); | 1151 AppCacheThread::DeleteSoon(AppCacheThread::db(), FROM_HERE, database_); |
| 1152 } | |
| 1093 } | 1153 } |
| 1094 | 1154 |
| 1095 void AppCacheStorageImpl::Initialize(const FilePath& cache_directory, | 1155 void AppCacheStorageImpl::Initialize(const FilePath& cache_directory, |
| 1096 base::MessageLoopProxy* cache_thread) { | 1156 base::MessageLoopProxy* cache_thread) { |
| 1097 cache_directory_ = cache_directory; | 1157 cache_directory_ = cache_directory; |
| 1098 cache_thread_ = cache_thread; | 1158 cache_thread_ = cache_thread; |
| 1099 is_incognito_ = cache_directory_.empty(); | 1159 is_incognito_ = cache_directory_.empty(); |
| 1100 | 1160 |
| 1101 FilePath db_file_path; | 1161 FilePath db_file_path; |
| 1102 if (!is_incognito_) | 1162 if (!is_incognito_) |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1571 Disable(); | 1631 Disable(); |
| 1572 if (!is_incognito_) { | 1632 if (!is_incognito_) { |
| 1573 VLOG(1) << "Deleting existing appcache data and starting over."; | 1633 VLOG(1) << "Deleting existing appcache data and starting over."; |
| 1574 AppCacheThread::PostTask(AppCacheThread::db(), FROM_HERE, | 1634 AppCacheThread::PostTask(AppCacheThread::db(), FROM_HERE, |
| 1575 NewRunnableFunction(DeleteDirectory, cache_directory_)); | 1635 NewRunnableFunction(DeleteDirectory, cache_directory_)); |
| 1576 } | 1636 } |
| 1577 } | 1637 } |
| 1578 } | 1638 } |
| 1579 | 1639 |
| 1580 } // namespace appcache | 1640 } // namespace appcache |
| OLD | NEW |