| 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 #ifndef WEBKIT_DATABASE_DATABASE_TRACKER_H_ | 5 #ifndef WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
| 6 #define WEBKIT_DATABASE_DATABASE_TRACKER_H_ | 6 #define WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 class QuotaManagerProxy; | 33 class QuotaManagerProxy; |
| 34 class SpecialStoragePolicy; | 34 class SpecialStoragePolicy; |
| 35 } | 35 } |
| 36 | 36 |
| 37 namespace webkit_database { | 37 namespace webkit_database { |
| 38 | 38 |
| 39 extern const FilePath::CharType kDatabaseDirectoryName[]; | 39 extern const FilePath::CharType kDatabaseDirectoryName[]; |
| 40 extern const FilePath::CharType kTrackerDatabaseFileName[]; | 40 extern const FilePath::CharType kTrackerDatabaseFileName[]; |
| 41 | 41 |
| 42 class DatabasesTable; | 42 class DatabasesTable; |
| 43 class QuotaTable; | |
| 44 | 43 |
| 45 // This class is used to store information about all databases in an origin. | 44 // This class is used to store information about all databases in an origin. |
| 46 class OriginInfo { | 45 class OriginInfo { |
| 47 public: | 46 public: |
| 48 OriginInfo(); | 47 OriginInfo(); |
| 49 OriginInfo(const OriginInfo& origin_info); | 48 OriginInfo(const OriginInfo& origin_info); |
| 50 ~OriginInfo(); | 49 ~OriginInfo(); |
| 51 | 50 |
| 52 const string16& GetOrigin() const { return origin_; } | 51 const string16& GetOrigin() const { return origin_; } |
| 53 int64 TotalSize() const { return total_size_; } | 52 int64 TotalSize() const { return total_size_; } |
| 54 int64 Quota() const { return quota_; } | |
| 55 void GetAllDatabaseNames(std::vector<string16>* databases) const; | 53 void GetAllDatabaseNames(std::vector<string16>* databases) const; |
| 56 int64 GetDatabaseSize(const string16& database_name) const; | 54 int64 GetDatabaseSize(const string16& database_name) const; |
| 57 string16 GetDatabaseDescription(const string16& database_name) const; | 55 string16 GetDatabaseDescription(const string16& database_name) const; |
| 58 | 56 |
| 59 protected: | 57 protected: |
| 60 typedef std::map<string16, std::pair<int64, string16> > DatabaseInfoMap; | 58 typedef std::map<string16, std::pair<int64, string16> > DatabaseInfoMap; |
| 61 | 59 |
| 62 OriginInfo(const string16& origin, int64 total_size, int64 quota); | 60 OriginInfo(const string16& origin, int64 total_size); |
| 63 | 61 |
| 64 string16 origin_; | 62 string16 origin_; |
| 65 int64 total_size_; | 63 int64 total_size_; |
| 66 int64 quota_; | |
| 67 DatabaseInfoMap database_info_; | 64 DatabaseInfoMap database_info_; |
| 68 }; | 65 }; |
| 69 | 66 |
| 70 // This class manages the main database, and keeps track of per origin quotas. | 67 // This class manages the main database and keeps track of open databases. |
| 71 // | 68 // |
| 72 // The data in this class is not thread-safe, so all methods of this class | 69 // The data in this class is not thread-safe, so all methods of this class |
| 73 // should be called on the same thread. The only exceptions are the ctor(), | 70 // should be called on the same thread. The only exceptions are the ctor(), |
| 74 // the dtor() and the database_directory() and quota_manager_proxy() getters. | 71 // the dtor() and the database_directory() and quota_manager_proxy() getters. |
| 75 // | 72 // |
| 76 // Furthermore, some methods of this class have to read/write data from/to | 73 // Furthermore, some methods of this class have to read/write data from/to |
| 77 // the disk. Therefore, in a multi-threaded application, all methods of this | 74 // the disk. Therefore, in a multi-threaded application, all methods of this |
| 78 // class should be called on the thread dedicated to file operations (file | 75 // class should be called on the thread dedicated to file operations (file |
| 79 // thread in the browser process, for example), if such a thread exists. | 76 // thread in the browser process, for example), if such a thread exists. |
| 80 class DatabaseTracker | 77 class DatabaseTracker |
| 81 : public base::RefCountedThreadSafe<DatabaseTracker> { | 78 : public base::RefCountedThreadSafe<DatabaseTracker> { |
| 82 public: | 79 public: |
| 83 class Observer { | 80 class Observer { |
| 84 public: | 81 public: |
| 85 virtual void OnDatabaseSizeChanged(const string16& origin_identifier, | 82 virtual void OnDatabaseSizeChanged(const string16& origin_identifier, |
| 86 const string16& database_name, | 83 const string16& database_name, |
| 87 int64 database_size, | 84 int64 database_size) = 0; |
| 88 int64 space_available) = 0; | |
| 89 virtual void OnDatabaseScheduledForDeletion( | 85 virtual void OnDatabaseScheduledForDeletion( |
| 90 const string16& origin_identifier, | 86 const string16& origin_identifier, |
| 91 const string16& database_name) = 0; | 87 const string16& database_name) = 0; |
| 92 virtual ~Observer() {} | 88 virtual ~Observer() {} |
| 93 }; | 89 }; |
| 94 | 90 |
| 95 DatabaseTracker(const FilePath& profile_path, bool is_incognito, | 91 DatabaseTracker(const FilePath& profile_path, bool is_incognito, |
| 96 quota::SpecialStoragePolicy* special_storage_policy, | 92 quota::SpecialStoragePolicy* special_storage_policy, |
| 97 quota::QuotaManagerProxy* quota_manager_proxy, | 93 quota::QuotaManagerProxy* quota_manager_proxy, |
| 98 base::MessageLoopProxy* db_tracker_thread); | 94 base::MessageLoopProxy* db_tracker_thread); |
| 99 | 95 |
| 100 void DatabaseOpened(const string16& origin_identifier, | 96 void DatabaseOpened(const string16& origin_identifier, |
| 101 const string16& database_name, | 97 const string16& database_name, |
| 102 const string16& database_details, | 98 const string16& database_details, |
| 103 int64 estimated_size, | 99 int64 estimated_size, |
| 104 int64* database_size, | 100 int64* database_size); |
| 105 int64* space_available); | |
| 106 void DatabaseModified(const string16& origin_identifier, | 101 void DatabaseModified(const string16& origin_identifier, |
| 107 const string16& database_name); | 102 const string16& database_name); |
| 108 void DatabaseClosed(const string16& origin_identifier, | 103 void DatabaseClosed(const string16& origin_identifier, |
| 109 const string16& database_name); | 104 const string16& database_name); |
| 110 void CloseDatabases(const DatabaseConnections& connections); | 105 void CloseDatabases(const DatabaseConnections& connections); |
| 111 | 106 |
| 112 void AddObserver(Observer* observer); | 107 void AddObserver(Observer* observer); |
| 113 void RemoveObserver(Observer* observer); | 108 void RemoveObserver(Observer* observer); |
| 114 | 109 |
| 115 void CloseTrackerDatabaseAndClearCaches(); | 110 void CloseTrackerDatabaseAndClearCaches(); |
| 116 | 111 |
| 117 const FilePath& DatabaseDirectory() const { return db_dir_; } | 112 const FilePath& DatabaseDirectory() const { return db_dir_; } |
| 118 FilePath GetFullDBFilePath(const string16& origin_identifier, | 113 FilePath GetFullDBFilePath(const string16& origin_identifier, |
| 119 const string16& database_name); | 114 const string16& database_name); |
| 120 | 115 |
| 121 // virtual for unittesting only | 116 // virtual for unittesting only |
| 122 virtual bool GetOriginInfo(const string16& origin_id, OriginInfo* info); | 117 virtual bool GetOriginInfo(const string16& origin_id, OriginInfo* info); |
| 123 virtual bool GetAllOriginIdentifiers(std::vector<string16>* origin_ids); | 118 virtual bool GetAllOriginIdentifiers(std::vector<string16>* origin_ids); |
| 124 virtual bool GetAllOriginsInfo(std::vector<OriginInfo>* origins_info); | 119 virtual bool GetAllOriginsInfo(std::vector<OriginInfo>* origins_info); |
| 125 | 120 |
| 126 // TODO(michaeln): remove quota related stuff when quota manager | |
| 127 // integration is complete | |
| 128 void SetOriginQuota(const string16& origin_identifier, int64 new_quota); | |
| 129 int64 GetDefaultQuota() { return default_quota_; } | |
| 130 void SetDefaultQuota(int64 quota); // for testing | |
| 131 | |
| 132 // Safe to call on any thread. | 121 // Safe to call on any thread. |
| 133 quota::QuotaManagerProxy* quota_manager_proxy() const { | 122 quota::QuotaManagerProxy* quota_manager_proxy() const { |
| 134 return quota_manager_proxy_.get(); | 123 return quota_manager_proxy_.get(); |
| 135 } | 124 } |
| 136 | 125 |
| 137 bool IsDatabaseScheduledForDeletion(const string16& origin_identifier, | 126 bool IsDatabaseScheduledForDeletion(const string16& origin_identifier, |
| 138 const string16& database_name); | 127 const string16& database_name); |
| 139 | 128 |
| 140 // Deletes a single database. Returns net::OK on success, net::FAILED on | 129 // Deletes a single database. Returns net::OK on success, net::FAILED on |
| 141 // failure, or net::ERR_IO_PENDING and |callback| is invoked upon completion, | 130 // failure, or net::ERR_IO_PENDING and |callback| is invoked upon completion, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 friend class base::RefCountedThreadSafe<DatabaseTracker>; | 167 friend class base::RefCountedThreadSafe<DatabaseTracker>; |
| 179 friend class MockDatabaseTracker; // for testing | 168 friend class MockDatabaseTracker; // for testing |
| 180 | 169 |
| 181 typedef std::map<string16, std::set<string16> > DatabaseSet; | 170 typedef std::map<string16, std::set<string16> > DatabaseSet; |
| 182 typedef std::map<net::CompletionCallback*, DatabaseSet> PendingCompletionMap; | 171 typedef std::map<net::CompletionCallback*, DatabaseSet> PendingCompletionMap; |
| 183 typedef std::map<string16, base::PlatformFile> FileHandlesMap; | 172 typedef std::map<string16, base::PlatformFile> FileHandlesMap; |
| 184 typedef std::map<string16, string16> OriginDirectoriesMap; | 173 typedef std::map<string16, string16> OriginDirectoriesMap; |
| 185 | 174 |
| 186 class CachedOriginInfo : public OriginInfo { | 175 class CachedOriginInfo : public OriginInfo { |
| 187 public: | 176 public: |
| 188 CachedOriginInfo() : OriginInfo(string16(), 0, 0) {} | 177 CachedOriginInfo() : OriginInfo(string16(), 0) {} |
| 189 void SetOrigin(const string16& origin) { origin_ = origin; } | 178 void SetOrigin(const string16& origin) { origin_ = origin; } |
| 190 void SetQuota(int64 new_quota) { quota_ = new_quota; } | |
| 191 void SetDatabaseSize(const string16& database_name, int64 new_size) { | 179 void SetDatabaseSize(const string16& database_name, int64 new_size) { |
| 192 int64 old_size = 0; | 180 int64 old_size = 0; |
| 193 if (database_info_.find(database_name) != database_info_.end()) | 181 if (database_info_.find(database_name) != database_info_.end()) |
| 194 old_size = database_info_[database_name].first; | 182 old_size = database_info_[database_name].first; |
| 195 database_info_[database_name].first = new_size; | 183 database_info_[database_name].first = new_size; |
| 196 if (new_size != old_size) | 184 if (new_size != old_size) |
| 197 total_size_ += new_size - old_size; | 185 total_size_ += new_size - old_size; |
| 198 } | 186 } |
| 199 void SetDatabaseDescription(const string16& database_name, | 187 void SetDatabaseDescription(const string16& database_name, |
| 200 const string16& description) { | 188 const string16& description) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 216 void InsertOrUpdateDatabaseDetails(const string16& origin_identifier, | 204 void InsertOrUpdateDatabaseDetails(const string16& origin_identifier, |
| 217 const string16& database_name, | 205 const string16& database_name, |
| 218 const string16& database_details, | 206 const string16& database_details, |
| 219 int64 estimated_size); | 207 int64 estimated_size); |
| 220 | 208 |
| 221 void ClearAllCachedOriginInfo(); | 209 void ClearAllCachedOriginInfo(); |
| 222 CachedOriginInfo* GetCachedOriginInfo(const string16& origin_identifier); | 210 CachedOriginInfo* GetCachedOriginInfo(const string16& origin_identifier); |
| 223 | 211 |
| 224 int64 GetDBFileSize(const string16& origin_identifier, | 212 int64 GetDBFileSize(const string16& origin_identifier, |
| 225 const string16& database_name); | 213 const string16& database_name); |
| 214 int64 SeedOpenDatabaseSize(const string16& origin_identifier, |
| 215 const string16& database_name); |
| 216 int64 UpdateOpenDatabaseSizeAndNotify(const string16& origin_identifier, |
| 217 const string16& database_name); |
| 226 | 218 |
| 227 int64 GetOriginSpaceAvailable(const string16& origin_identifier); | |
| 228 | |
| 229 int64 UpdateCachedDatabaseFileSize(const string16& origin_identifier, | |
| 230 const string16& database_name); | |
| 231 void ScheduleDatabaseForDeletion(const string16& origin_identifier, | 219 void ScheduleDatabaseForDeletion(const string16& origin_identifier, |
| 232 const string16& database_name); | 220 const string16& database_name); |
| 233 // Schedule a set of open databases for deletion. If non-null, callback is | 221 // Schedule a set of open databases for deletion. If non-null, callback is |
| 234 // invoked upon completion. | 222 // invoked upon completion. |
| 235 void ScheduleDatabasesForDeletion(const DatabaseSet& databases, | 223 void ScheduleDatabasesForDeletion(const DatabaseSet& databases, |
| 236 net::CompletionCallback* callback); | 224 net::CompletionCallback* callback); |
| 237 | 225 |
| 238 // Returns the directory where all DB files for the given origin are stored. | 226 // Returns the directory where all DB files for the given origin are stored. |
| 239 string16 GetOriginDirectory(const string16& origin_identifier); | 227 string16 GetOriginDirectory(const string16& origin_identifier); |
| 240 | 228 |
| 241 bool is_initialized_; | 229 bool is_initialized_; |
| 242 const bool is_incognito_; | 230 const bool is_incognito_; |
| 243 bool shutting_down_; | 231 bool shutting_down_; |
| 244 const FilePath profile_path_; | 232 const FilePath profile_path_; |
| 245 const FilePath db_dir_; | 233 const FilePath db_dir_; |
| 246 scoped_ptr<sql::Connection> db_; | 234 scoped_ptr<sql::Connection> db_; |
| 247 scoped_ptr<DatabasesTable> databases_table_; | 235 scoped_ptr<DatabasesTable> databases_table_; |
| 248 scoped_ptr<QuotaTable> quota_table_; | |
| 249 scoped_ptr<sql::MetaTable> meta_table_; | 236 scoped_ptr<sql::MetaTable> meta_table_; |
| 250 ObserverList<Observer, true> observers_; | 237 ObserverList<Observer, true> observers_; |
| 251 std::map<string16, CachedOriginInfo> origins_info_map_; | 238 std::map<string16, CachedOriginInfo> origins_info_map_; |
| 252 DatabaseConnections database_connections_; | 239 DatabaseConnections database_connections_; |
| 253 | 240 |
| 254 // The set of databases that should be deleted but are still opened | 241 // The set of databases that should be deleted but are still opened |
| 255 DatabaseSet dbs_to_be_deleted_; | 242 DatabaseSet dbs_to_be_deleted_; |
| 256 PendingCompletionMap deletion_callbacks_; | 243 PendingCompletionMap deletion_callbacks_; |
| 257 | 244 |
| 258 // Default quota for all origins; changed only by tests | |
| 259 int64 default_quota_; | |
| 260 | |
| 261 // Apps and Extensions can have special rights. | 245 // Apps and Extensions can have special rights. |
| 262 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | 246 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; |
| 263 | 247 |
| 264 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; | 248 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; |
| 265 | 249 |
| 266 // When in incognito mode, store a DELETE_ON_CLOSE handle to each | 250 // When in incognito mode, store a DELETE_ON_CLOSE handle to each |
| 267 // main DB and journal file that was accessed. When the incognito profile | 251 // main DB and journal file that was accessed. When the incognito profile |
| 268 // goes away (or when the browser crashes), all these handles will be | 252 // goes away (or when the browser crashes), all these handles will be |
| 269 // closed, and the files will be deleted. | 253 // closed, and the files will be deleted. |
| 270 FileHandlesMap incognito_file_handles_; | 254 FileHandlesMap incognito_file_handles_; |
| 271 | 255 |
| 272 // In a non-incognito profile, all DBs in an origin are stored in a directory | 256 // In a non-incognito profile, all DBs in an origin are stored in a directory |
| 273 // named after the origin. In an incognito profile though, we do not want the | 257 // named after the origin. In an incognito profile though, we do not want the |
| 274 // directory structure to reveal the origins visited by the user (in case the | 258 // directory structure to reveal the origins visited by the user (in case the |
| 275 // browser process crashes and those directories are not deleted). So we use | 259 // browser process crashes and those directories are not deleted). So we use |
| 276 // this map to assign directory names that do not reveal this information. | 260 // this map to assign directory names that do not reveal this information. |
| 277 OriginDirectoriesMap incognito_origin_directories_; | 261 OriginDirectoriesMap incognito_origin_directories_; |
| 278 int incognito_origin_directories_generator_; | 262 int incognito_origin_directories_generator_; |
| 279 | 263 |
| 280 FRIEND_TEST_ALL_PREFIXES(DatabaseTracker, TestHelper); | 264 FRIEND_TEST_ALL_PREFIXES(DatabaseTracker, TestHelper); |
| 281 }; | 265 }; |
| 282 | 266 |
| 283 } // namespace webkit_database | 267 } // namespace webkit_database |
| 284 | 268 |
| 285 #endif // WEBKIT_DATABASE_DATABASE_TRACKER_H_ | 269 #endif // WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
| OLD | NEW |