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 #ifndef WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
| 6 #define WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/file_path.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "base/ref_counted.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/string16.h" |
| 15 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 16 |
| 17 namespace sql { |
| 18 class Connection; |
| 19 class MetaTable; |
| 20 } |
| 21 |
| 22 namespace webkit_database { |
| 23 |
| 24 class DatabasesTable; |
| 25 |
| 26 // This class manages the main database, and keeps track of per origin quotas. |
| 27 // |
| 28 // The data in this class is not thread-safe, so all methods of this class |
| 29 // should be called on the same thread. The only exception is |
| 30 // database_directory() which returns a constant that is initialized when |
| 31 // the DatabaseTracker instance is created. |
| 32 // |
| 33 // Furthermore, some methods of this class have to read/write data from/to |
| 34 // the disk. Therefore, in a multi-threaded application, all methods of this |
| 35 // class should be called on the thread dedicated to file operations (file |
| 36 // thread in the browser process, for example), if such a thread exists. |
| 37 class DatabaseTracker |
| 38 : public base::RefCountedThreadSafe<DatabaseTracker> { |
| 39 public: |
| 40 class Observer { |
| 41 public: |
| 42 virtual void OnDatabaseSizeChanged(const string16& origin_identifier, |
| 43 const string16& database_name, |
| 44 int64 database_size, |
| 45 int64 space_available) = 0; |
| 46 virtual ~Observer() {} |
| 47 }; |
| 48 |
| 49 explicit DatabaseTracker(const FilePath& profile_path); |
| 50 ~DatabaseTracker(); |
| 51 |
| 52 void DatabaseOpened(const string16& origin_identifier, |
| 53 const string16& database_name, |
| 54 const string16& database_details, |
| 55 int64 estimated_size, |
| 56 int64* database_size, |
| 57 int64* space_available); |
| 58 void DatabaseModified(const string16& origin_identifier, |
| 59 const string16& database_name); |
| 60 void DatabaseClosed(const string16& origin_identifier, |
| 61 const string16& database_name); |
| 62 |
| 63 void AddObserver(Observer* observer); |
| 64 void RemoveObserver(Observer* observer); |
| 65 |
| 66 void CloseTrackerDatabaseAndClearCaches(); |
| 67 |
| 68 const FilePath& DatabaseDirectory() const { return db_dir_; } |
| 69 FilePath GetFullDBFilePath(const string16& origin_identifier, |
| 70 const string16& database_name) const; |
| 71 |
| 72 private: |
| 73 class CachedOriginInfo { |
| 74 public: |
| 75 CachedOriginInfo() : total_size_(0) { } |
| 76 int64 TotalSize() const { return total_size_; } |
| 77 int64 GetCachedDatabaseSize(const string16& database_name) { |
| 78 return cached_database_sizes_[database_name]; |
| 79 } |
| 80 void SetCachedDatabaseSize(const string16& database_name, int64 new_size) { |
| 81 int64 old_size = cached_database_sizes_[database_name]; |
| 82 cached_database_sizes_[database_name] = new_size; |
| 83 if (new_size != old_size) |
| 84 total_size_ += new_size - old_size; |
| 85 } |
| 86 |
| 87 private: |
| 88 int64 total_size_; |
| 89 std::map<string16, int64> cached_database_sizes_; |
| 90 }; |
| 91 |
| 92 bool LazyInit(); |
| 93 void InsertOrUpdateDatabaseDetails(const string16& origin_identifier, |
| 94 const string16& database_name, |
| 95 const string16& database_details, |
| 96 int64 estimated_size); |
| 97 |
| 98 void ClearAllCachedOriginInfo(); |
| 99 CachedOriginInfo* GetCachedOriginInfo(const string16& origin_identifier); |
| 100 int64 GetCachedDatabaseFileSize(const string16& origin_identifier, |
| 101 const string16& database_name); |
| 102 int64 UpdateCachedDatabaseFileSize(const string16& origin_identifier, |
| 103 const string16& database_name); |
| 104 int64 GetDBFileSize(const string16& origin_identifier, |
| 105 const string16& database_name) const; |
| 106 int64 GetOriginUsage(const string16& origin_identifer); |
| 107 int64 GetOriginQuota(const string16& origin_identifier) const; |
| 108 int64 GetOriginSpaceAvailable(const string16& origin_identifier); |
| 109 |
| 110 bool initialized_; |
| 111 const FilePath db_dir_; |
| 112 scoped_ptr<sql::Connection> db_; |
| 113 scoped_ptr<DatabasesTable> databases_table_; |
| 114 scoped_ptr<sql::MetaTable> meta_table_; |
| 115 ObserverList<Observer> observers_; |
| 116 std::map<string16, CachedOriginInfo> origins_info_map_; |
| 117 |
| 118 FRIEND_TEST(DatabaseTrackerTest, TestIt); |
| 119 }; |
| 120 |
| 121 } // namespace webkit_database |
| 122 |
| 123 #endif // WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
OLD | NEW |