OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
12 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
13 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
14 #include "base/string16.h" | 14 #include "base/string16.h" |
| 15 #include "base/string_util.h" |
15 #include "testing/gtest/include/gtest/gtest_prod.h" | 16 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 17 #include "webkit/database/database_connections.h" |
16 | 18 |
17 namespace sql { | 19 namespace sql { |
18 class Connection; | 20 class Connection; |
19 class MetaTable; | 21 class MetaTable; |
20 } | 22 } |
21 | 23 |
22 namespace webkit_database { | 24 namespace webkit_database { |
23 | 25 |
24 class DatabasesTable; | 26 class DatabasesTable; |
| 27 class QuotaTable; |
| 28 |
| 29 // This class is used to store information about all databases in an origin. |
| 30 class OriginInfo { |
| 31 public: |
| 32 OriginInfo(const OriginInfo& origin_info) |
| 33 : origin_(origin_info.origin_), |
| 34 total_size_(origin_info.total_size_), |
| 35 quota_(origin_info.quota_), |
| 36 database_sizes_(origin_info.database_sizes_) {} |
| 37 string16 GetOrigin() const { return origin_; } |
| 38 int64 TotalSize() const { return total_size_; } |
| 39 int64 Quota() const { return quota_; } |
| 40 void GetAllDatabaseNames(std::vector<string16>* databases) const { |
| 41 for (std::map<string16, int64>::const_iterator it = database_sizes_.begin(); |
| 42 it != database_sizes_.end(); it++) { |
| 43 databases->push_back(it->first); |
| 44 } |
| 45 } |
| 46 int64 GetDatabaseSize(const string16& database_name) const { |
| 47 std::map<string16, int64>::const_iterator it = |
| 48 database_sizes_.find(database_name); |
| 49 if (it != database_sizes_.end()) |
| 50 return it->second; |
| 51 return 0; |
| 52 } |
| 53 |
| 54 protected: |
| 55 OriginInfo(const string16& origin, int64 total_size, int64 quota) |
| 56 : origin_(origin), total_size_(total_size), quota_(quota) { } |
| 57 |
| 58 string16 origin_; |
| 59 int64 total_size_; |
| 60 int64 quota_; |
| 61 std::map<string16, int64> database_sizes_; |
| 62 }; |
25 | 63 |
26 // This class manages the main database, and keeps track of per origin quotas. | 64 // This class manages the main database, and keeps track of per origin quotas. |
27 // | 65 // |
28 // The data in this class is not thread-safe, so all methods of this class | 66 // 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 | 67 // should be called on the same thread. The only exception is |
30 // database_directory() which returns a constant that is initialized when | 68 // database_directory() which returns a constant that is initialized when |
31 // the DatabaseTracker instance is created. | 69 // the DatabaseTracker instance is created. |
32 // | 70 // |
33 // Furthermore, some methods of this class have to read/write data from/to | 71 // 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 | 72 // the disk. Therefore, in a multi-threaded application, all methods of this |
(...skipping 16 matching lines...) Expand all Loading... |
51 void DatabaseOpened(const string16& origin_identifier, | 89 void DatabaseOpened(const string16& origin_identifier, |
52 const string16& database_name, | 90 const string16& database_name, |
53 const string16& database_details, | 91 const string16& database_details, |
54 int64 estimated_size, | 92 int64 estimated_size, |
55 int64* database_size, | 93 int64* database_size, |
56 int64* space_available); | 94 int64* space_available); |
57 void DatabaseModified(const string16& origin_identifier, | 95 void DatabaseModified(const string16& origin_identifier, |
58 const string16& database_name); | 96 const string16& database_name); |
59 void DatabaseClosed(const string16& origin_identifier, | 97 void DatabaseClosed(const string16& origin_identifier, |
60 const string16& database_name); | 98 const string16& database_name); |
| 99 void CloseDatabases(const DatabaseConnections& connections); |
61 | 100 |
62 void AddObserver(Observer* observer); | 101 void AddObserver(Observer* observer); |
63 void RemoveObserver(Observer* observer); | 102 void RemoveObserver(Observer* observer); |
64 | 103 |
65 void CloseTrackerDatabaseAndClearCaches(); | 104 void CloseTrackerDatabaseAndClearCaches(); |
66 | 105 |
67 const FilePath& DatabaseDirectory() const { return db_dir_; } | 106 const FilePath& DatabaseDirectory() const { return db_dir_; } |
68 FilePath GetFullDBFilePath(const string16& origin_identifier, | 107 FilePath GetFullDBFilePath(const string16& origin_identifier, |
69 const string16& database_name) const; | 108 const string16& database_name) const; |
70 | 109 |
| 110 bool GetAllOriginsInfo(std::vector<OriginInfo>* origins_info); |
| 111 void SetOriginQuota(const string16& origin_identifier, int64 new_quota); |
| 112 bool DeleteDatabase(const string16& origin_identifier, |
| 113 const string16& database_name); |
| 114 bool DeleteOrigin(const string16& origin_identifier); |
| 115 |
71 private: | 116 private: |
| 117 // Need this here to allow RefCountedThreadSafe to call ~DatabaseTracker(). |
72 friend class base::RefCountedThreadSafe<DatabaseTracker>; | 118 friend class base::RefCountedThreadSafe<DatabaseTracker>; |
73 | 119 |
| 120 class CachedOriginInfo : public OriginInfo { |
| 121 public: |
| 122 CachedOriginInfo() : OriginInfo(EmptyString16(), 0, 0) {} |
| 123 void SetOrigin(const string16& origin) { origin_ = origin; } |
| 124 void SetQuota(int64 new_quota) { quota_ = new_quota; } |
| 125 void SetDatabaseSize(const string16& database_name, int64 new_size) { |
| 126 int64 old_size = database_sizes_[database_name]; |
| 127 database_sizes_[database_name] = new_size; |
| 128 if (new_size != old_size) |
| 129 total_size_ += new_size - old_size; |
| 130 } |
| 131 }; |
| 132 |
74 ~DatabaseTracker(); | 133 ~DatabaseTracker(); |
75 | 134 |
76 class CachedOriginInfo { | |
77 public: | |
78 CachedOriginInfo() : total_size_(0) { } | |
79 int64 TotalSize() const { return total_size_; } | |
80 int64 GetCachedDatabaseSize(const string16& database_name) { | |
81 return cached_database_sizes_[database_name]; | |
82 } | |
83 void SetCachedDatabaseSize(const string16& database_name, int64 new_size) { | |
84 int64 old_size = cached_database_sizes_[database_name]; | |
85 cached_database_sizes_[database_name] = new_size; | |
86 if (new_size != old_size) | |
87 total_size_ += new_size - old_size; | |
88 } | |
89 | |
90 private: | |
91 int64 total_size_; | |
92 std::map<string16, int64> cached_database_sizes_; | |
93 }; | |
94 | |
95 bool LazyInit(); | 135 bool LazyInit(); |
| 136 bool UpgradeToCurrentVersion(); |
96 void InsertOrUpdateDatabaseDetails(const string16& origin_identifier, | 137 void InsertOrUpdateDatabaseDetails(const string16& origin_identifier, |
97 const string16& database_name, | 138 const string16& database_name, |
98 const string16& database_details, | 139 const string16& database_details, |
99 int64 estimated_size); | 140 int64 estimated_size); |
100 | 141 |
101 void ClearAllCachedOriginInfo(); | 142 void ClearAllCachedOriginInfo(); |
102 CachedOriginInfo* GetCachedOriginInfo(const string16& origin_identifier); | 143 CachedOriginInfo* GetCachedOriginInfo(const string16& origin_identifier); |
103 int64 GetCachedDatabaseFileSize(const string16& origin_identifier, | 144 |
104 const string16& database_name); | 145 int64 GetDBFileSize(const string16& origin_identifier, |
| 146 const string16& database_name) const; |
| 147 |
| 148 int64 GetOriginSpaceAvailable(const string16& origin_identifier); |
| 149 |
105 int64 UpdateCachedDatabaseFileSize(const string16& origin_identifier, | 150 int64 UpdateCachedDatabaseFileSize(const string16& origin_identifier, |
106 const string16& database_name); | 151 const string16& database_name); |
107 int64 GetDBFileSize(const string16& origin_identifier, | |
108 const string16& database_name) const; | |
109 int64 GetOriginUsage(const string16& origin_identifer); | |
110 int64 GetOriginQuota(const string16& origin_identifier) const; | |
111 int64 GetOriginSpaceAvailable(const string16& origin_identifier); | |
112 | 152 |
113 bool initialized_; | 153 bool initialized_; |
114 const FilePath db_dir_; | 154 const FilePath db_dir_; |
115 scoped_ptr<sql::Connection> db_; | 155 scoped_ptr<sql::Connection> db_; |
116 scoped_ptr<DatabasesTable> databases_table_; | 156 scoped_ptr<DatabasesTable> databases_table_; |
| 157 scoped_ptr<QuotaTable> quota_table_; |
117 scoped_ptr<sql::MetaTable> meta_table_; | 158 scoped_ptr<sql::MetaTable> meta_table_; |
118 ObserverList<Observer> observers_; | 159 ObserverList<Observer> observers_; |
119 std::map<string16, CachedOriginInfo> origins_info_map_; | 160 std::map<string16, CachedOriginInfo> origins_info_map_; |
| 161 DatabaseConnections database_connections_; |
120 | 162 |
121 FRIEND_TEST(DatabaseTrackerTest, TestIt); | 163 FRIEND_TEST(DatabaseTrackerTest, TestIt); |
122 }; | 164 }; |
123 | 165 |
124 } // namespace webkit_database | 166 } // namespace webkit_database |
125 | 167 |
126 #endif // WEBKIT_DATABASE_DATABASE_TRACKER_H_ | 168 #endif // WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
OLD | NEW |