OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/database/database_tracker.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "app/sql/connection.h" |
| 10 #include "app/sql/meta_table.h" |
| 11 #include "app/sql/statement.h" |
| 12 #include "base/basictypes.h" |
| 13 #include "base/file_path.h" |
| 14 #include "base/file_util.h" |
| 15 #include "base/string_util.h" |
| 16 #include "webkit/database/databases_table.h" |
| 17 |
| 18 namespace webkit_database { |
| 19 |
| 20 const FilePath::CharType kDatabaseDirectoryName[] = |
| 21 FILE_PATH_LITERAL("databases"); |
| 22 const FilePath::CharType kTrackerDatabaseFileName[] = |
| 23 FILE_PATH_LITERAL("Databases.db"); |
| 24 const int kCurrentVersion = 1; |
| 25 const int kCompatibleVersion = 1; |
| 26 const int64 kDefaultQuota = 5 * 1024 * 1024; |
| 27 |
| 28 DatabaseTracker::DatabaseTracker(const FilePath& profile_path) |
| 29 : initialized_(false), |
| 30 db_dir_(profile_path.Append(FilePath(kDatabaseDirectoryName))), |
| 31 db_(new sql::Connection()), |
| 32 databases_table_(NULL), |
| 33 meta_table_(NULL) { |
| 34 } |
| 35 |
| 36 DatabaseTracker::~DatabaseTracker() { |
| 37 DCHECK(observers_.size() == 0); |
| 38 } |
| 39 |
| 40 void DatabaseTracker::DatabaseOpened(const string16& origin_identifier, |
| 41 const string16& database_name, |
| 42 const string16& database_description, |
| 43 int64 estimated_size, |
| 44 int64* database_size, |
| 45 int64* space_available) { |
| 46 if (!LazyInit()) { |
| 47 *database_size = 0; |
| 48 *space_available = 0; |
| 49 return; |
| 50 } |
| 51 |
| 52 InsertOrUpdateDatabaseDetails(origin_identifier, database_name, |
| 53 database_description, estimated_size); |
| 54 |
| 55 *database_size = GetCachedDatabaseFileSize(origin_identifier, database_name); |
| 56 *space_available = GetOriginSpaceAvailable(origin_identifier); |
| 57 } |
| 58 |
| 59 void DatabaseTracker::DatabaseModified(const string16& origin_identifier, |
| 60 const string16& database_name) { |
| 61 if (!LazyInit()) |
| 62 return; |
| 63 |
| 64 int64 updated_db_size = |
| 65 UpdateCachedDatabaseFileSize(origin_identifier, database_name); |
| 66 int64 space_available = GetOriginSpaceAvailable(origin_identifier); |
| 67 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseSizeChanged( |
| 68 origin_identifier, database_name, updated_db_size, space_available)); |
| 69 } |
| 70 |
| 71 void DatabaseTracker::DatabaseClosed(const string16& origin_identifier, |
| 72 const string16& database_name) { |
| 73 // TODO(dumi): figure out how to use this information at a later time |
| 74 } |
| 75 |
| 76 void DatabaseTracker::AddObserver(Observer* observer) { |
| 77 observers_.AddObserver(observer); |
| 78 } |
| 79 |
| 80 void DatabaseTracker::RemoveObserver(Observer* observer) { |
| 81 // When we remove a listener, we do not know which cached information |
| 82 // is still needed and which information can be discarded. So we just |
| 83 // clear all caches and re-populate them as needed. |
| 84 observers_.RemoveObserver(observer); |
| 85 ClearAllCachedOriginInfo(); |
| 86 } |
| 87 |
| 88 void DatabaseTracker::CloseTrackerDatabaseAndClearCaches() { |
| 89 ClearAllCachedOriginInfo(); |
| 90 meta_table_.reset(NULL); |
| 91 databases_table_.reset(NULL); |
| 92 db_->Close(); |
| 93 initialized_ = false; |
| 94 } |
| 95 |
| 96 FilePath DatabaseTracker::GetFullDBFilePath( |
| 97 const string16& origin_identifier, |
| 98 const string16& database_name) const { |
| 99 return db_dir_.Append(FilePath::FromWStringHack(UTF16ToWide( |
| 100 origin_identifier + ASCIIToUTF16("_") + database_name))); |
| 101 } |
| 102 |
| 103 bool DatabaseTracker::LazyInit() { |
| 104 if (!initialized_) { |
| 105 databases_table_.reset(new DatabasesTable(db_.get())); |
| 106 meta_table_.reset(new sql::MetaTable()); |
| 107 initialized_ = |
| 108 file_util::CreateDirectory(db_dir_) && |
| 109 db_->Open(db_dir_.Append(FilePath(kTrackerDatabaseFileName))) && |
| 110 meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion) && |
| 111 (meta_table_->GetCompatibleVersionNumber() <= kCurrentVersion) && |
| 112 databases_table_->Init(); |
| 113 } |
| 114 return initialized_; |
| 115 } |
| 116 |
| 117 void DatabaseTracker::InsertOrUpdateDatabaseDetails( |
| 118 const string16& origin_identifier, |
| 119 const string16& database_name, |
| 120 const string16& database_description, |
| 121 int64 estimated_size) { |
| 122 DatabaseDetails details; |
| 123 if (!databases_table_->GetDatabaseDetails( |
| 124 origin_identifier, database_name, &details)) { |
| 125 details.origin_identifier = origin_identifier; |
| 126 details.database_name = database_name; |
| 127 details.description = database_description; |
| 128 details.estimated_size = estimated_size; |
| 129 databases_table_->InsertDatabaseDetails(details); |
| 130 } else if ((details.description != database_description) || |
| 131 (details.estimated_size != estimated_size)) { |
| 132 details.description = database_description; |
| 133 details.estimated_size = estimated_size; |
| 134 databases_table_->UpdateDatabaseDetails(details); |
| 135 } |
| 136 } |
| 137 |
| 138 int64 DatabaseTracker::GetDBFileSize(const string16& origin_identifier, |
| 139 const string16& database_name) const { |
| 140 FilePath db_file_name = GetFullDBFilePath(origin_identifier, database_name); |
| 141 int64 db_file_size = 0; |
| 142 if (!file_util::GetFileSize(db_file_name, &db_file_size)) |
| 143 db_file_size = 0; |
| 144 return db_file_size; |
| 145 } |
| 146 |
| 147 void DatabaseTracker::ClearAllCachedOriginInfo() { |
| 148 origins_info_map_.clear(); |
| 149 } |
| 150 |
| 151 DatabaseTracker::CachedOriginInfo* DatabaseTracker::GetCachedOriginInfo( |
| 152 const string16& origin_identifier) { |
| 153 // Populate the cache with data for this origin if needed. |
| 154 if (origins_info_map_.find(origin_identifier) == origins_info_map_.end()) { |
| 155 std::vector<DatabaseDetails> details; |
| 156 if (!databases_table_->GetAllDatabaseDetailsForOrigin( |
| 157 origin_identifier, &details)) { |
| 158 return NULL; |
| 159 } |
| 160 |
| 161 CachedOriginInfo& origin_info = origins_info_map_[origin_identifier]; |
| 162 for (std::vector<DatabaseDetails>::const_iterator it = details.begin(); |
| 163 it != details.end(); it++) { |
| 164 int64 db_file_size = |
| 165 GetDBFileSize(it->origin_identifier, it->database_name); |
| 166 origin_info.SetCachedDatabaseSize(it->database_name, db_file_size); |
| 167 } |
| 168 } |
| 169 |
| 170 return &origins_info_map_[origin_identifier]; |
| 171 } |
| 172 |
| 173 int64 DatabaseTracker::GetCachedDatabaseFileSize( |
| 174 const string16& origin_identifier, |
| 175 const string16& database_name) { |
| 176 CachedOriginInfo* origin_info = GetCachedOriginInfo(origin_identifier); |
| 177 if (!origin_info) |
| 178 return 0; |
| 179 return origin_info->GetCachedDatabaseSize(database_name); |
| 180 } |
| 181 |
| 182 int64 DatabaseTracker::UpdateCachedDatabaseFileSize( |
| 183 const string16& origin_identifier, |
| 184 const string16& database_name) { |
| 185 int64 new_size = GetDBFileSize(origin_identifier, database_name); |
| 186 CachedOriginInfo* origin_info = GetCachedOriginInfo(origin_identifier); |
| 187 if (origin_info) |
| 188 origin_info->SetCachedDatabaseSize(database_name, new_size); |
| 189 return new_size; |
| 190 } |
| 191 |
| 192 int64 DatabaseTracker::GetOriginUsage(const string16& origin_identifier) { |
| 193 CachedOriginInfo* origin_info = GetCachedOriginInfo(origin_identifier); |
| 194 if (!origin_info) |
| 195 return kint64max; |
| 196 return origin_info->TotalSize(); |
| 197 } |
| 198 |
| 199 int64 DatabaseTracker::GetOriginQuota( |
| 200 const string16& /*origin_identifier*/) const { |
| 201 return kDefaultQuota; |
| 202 } |
| 203 |
| 204 int64 DatabaseTracker::GetOriginSpaceAvailable( |
| 205 const string16& origin_identifier) { |
| 206 int64 space_available = GetOriginQuota(origin_identifier) - |
| 207 GetOriginUsage(origin_identifier); |
| 208 return (space_available < 0 ? 0 : space_available); |
| 209 } |
| 210 |
| 211 } // namespace webkit_database |
OLD | NEW |