OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 23 matching lines...) Expand all Loading... |
34 public: | 34 public: |
35 OriginInfo(const OriginInfo& origin_info) | 35 OriginInfo(const OriginInfo& origin_info) |
36 : origin_(origin_info.origin_), | 36 : origin_(origin_info.origin_), |
37 total_size_(origin_info.total_size_), | 37 total_size_(origin_info.total_size_), |
38 quota_(origin_info.quota_), | 38 quota_(origin_info.quota_), |
39 database_info_(origin_info.database_info_) {} | 39 database_info_(origin_info.database_info_) {} |
40 const string16& GetOrigin() const { return origin_; } | 40 const string16& GetOrigin() const { return origin_; } |
41 int64 TotalSize() const { return total_size_; } | 41 int64 TotalSize() const { return total_size_; } |
42 int64 Quota() const { return quota_; } | 42 int64 Quota() const { return quota_; } |
43 void GetAllDatabaseNames(std::vector<string16>* databases) const { | 43 void GetAllDatabaseNames(std::vector<string16>* databases) const { |
44 for (std::map<string16, DatabaseInfo>::const_iterator it = | 44 for (DatabaseInfoMap::const_iterator it = database_info_.begin(); |
45 database_info_.begin(); it != database_info_.end(); it++) { | 45 it != database_info_.end(); it++) { |
46 databases->push_back(it->first); | 46 databases->push_back(it->first); |
47 } | 47 } |
48 } | 48 } |
49 int64 GetDatabaseSize(const string16& database_name) const { | 49 int64 GetDatabaseSize(const string16& database_name) const { |
50 std::map<string16, DatabaseInfo>::const_iterator it = | 50 DatabaseInfoMap::const_iterator it = database_info_.find(database_name); |
51 database_info_.find(database_name); | |
52 if (it != database_info_.end()) | 51 if (it != database_info_.end()) |
53 return it->second.first; | 52 return it->second.first; |
54 return 0; | 53 return 0; |
55 } | 54 } |
56 string16 GetDatabaseDescription(const string16& database_name) const { | 55 string16 GetDatabaseDescription(const string16& database_name) const { |
57 std::map<string16, DatabaseInfo>::const_iterator it = | 56 DatabaseInfoMap::const_iterator it = database_info_.find(database_name); |
58 database_info_.find(database_name); | |
59 if (it != database_info_.end()) | 57 if (it != database_info_.end()) |
60 return it->second.second; | 58 return it->second.second; |
61 return string16(); | 59 return string16(); |
62 } | 60 } |
63 | 61 |
64 protected: | 62 protected: |
65 typedef std::pair<int64, string16> DatabaseInfo; | 63 typedef std::map<string16, std::pair<int64, string16> > DatabaseInfoMap; |
66 | 64 |
67 OriginInfo(const string16& origin, int64 total_size, int64 quota) | 65 OriginInfo(const string16& origin, int64 total_size, int64 quota) |
68 : origin_(origin), total_size_(total_size), quota_(quota) { } | 66 : origin_(origin), total_size_(total_size), quota_(quota) { } |
69 | 67 |
70 string16 origin_; | 68 string16 origin_; |
71 int64 total_size_; | 69 int64 total_size_; |
72 int64 quota_; | 70 int64 quota_; |
73 std::map<string16, DatabaseInfo> database_info_; | 71 DatabaseInfoMap database_info_; |
74 }; | 72 }; |
75 | 73 |
76 // This class manages the main database, and keeps track of per origin quotas. | 74 // This class manages the main database, and keeps track of per origin quotas. |
77 // | 75 // |
78 // The data in this class is not thread-safe, so all methods of this class | 76 // The data in this class is not thread-safe, so all methods of this class |
79 // should be called on the same thread. The only exception is | 77 // should be called on the same thread. The only exception is |
80 // database_directory() which returns a constant that is initialized when | 78 // database_directory() which returns a constant that is initialized when |
81 // the DatabaseTracker instance is created. | 79 // the DatabaseTracker instance is created. |
82 // | 80 // |
83 // Furthermore, some methods of this class have to read/write data from/to | 81 // Furthermore, some methods of this class have to read/write data from/to |
(...skipping 10 matching lines...) Expand all Loading... |
94 int64 database_size, | 92 int64 database_size, |
95 int64 space_available) = 0; | 93 int64 space_available) = 0; |
96 virtual void OnDatabaseScheduledForDeletion( | 94 virtual void OnDatabaseScheduledForDeletion( |
97 const string16& origin_identifier, | 95 const string16& origin_identifier, |
98 const string16& database_name) = 0; | 96 const string16& database_name) = 0; |
99 virtual ~Observer() {} | 97 virtual ~Observer() {} |
100 }; | 98 }; |
101 | 99 |
102 explicit DatabaseTracker(const FilePath& profile_path); | 100 explicit DatabaseTracker(const FilePath& profile_path); |
103 | 101 |
104 // Sets the default quota for all origins. Should be used in tests only. | |
105 void SetDefaultQuota(int64 quota); | |
106 | |
107 void DatabaseOpened(const string16& origin_identifier, | 102 void DatabaseOpened(const string16& origin_identifier, |
108 const string16& database_name, | 103 const string16& database_name, |
109 const string16& database_details, | 104 const string16& database_details, |
110 int64 estimated_size, | 105 int64 estimated_size, |
111 int64* database_size, | 106 int64* database_size, |
112 int64* space_available); | 107 int64* space_available); |
113 void DatabaseModified(const string16& origin_identifier, | 108 void DatabaseModified(const string16& origin_identifier, |
114 const string16& database_name); | 109 const string16& database_name); |
115 void DatabaseClosed(const string16& origin_identifier, | 110 void DatabaseClosed(const string16& origin_identifier, |
116 const string16& database_name); | 111 const string16& database_name); |
117 void CloseDatabases(const DatabaseConnections& connections); | 112 void CloseDatabases(const DatabaseConnections& connections); |
118 void DeleteDatabaseIfNeeded(const string16& origin_identifier, | 113 void DeleteDatabaseIfNeeded(const string16& origin_identifier, |
119 const string16& database_name); | 114 const string16& database_name); |
120 | 115 |
121 void AddObserver(Observer* observer); | 116 void AddObserver(Observer* observer); |
122 void RemoveObserver(Observer* observer); | 117 void RemoveObserver(Observer* observer); |
123 | 118 |
124 void CloseTrackerDatabaseAndClearCaches(); | 119 void CloseTrackerDatabaseAndClearCaches(); |
125 | 120 |
126 const FilePath& DatabaseDirectory() const { return db_dir_; } | 121 const FilePath& DatabaseDirectory() const { return db_dir_; } |
127 FilePath GetFullDBFilePath(const string16& origin_identifier, | 122 FilePath GetFullDBFilePath(const string16& origin_identifier, |
128 const string16& database_name) const; | 123 const string16& database_name) const; |
129 | 124 |
130 bool GetAllOriginsInfo(std::vector<OriginInfo>* origins_info); | 125 bool GetAllOriginsInfo(std::vector<OriginInfo>* origins_info); |
131 void SetOriginQuota(const string16& origin_identifier, int64 new_quota); | 126 void SetOriginQuota(const string16& origin_identifier, int64 new_quota); |
132 bool DeleteDatabase(const string16& origin_identifier, | 127 |
133 const string16& database_name); | 128 // Sets the default quota for all origins. Should be used in tests only. |
134 bool DeleteOrigin(const string16& origin_identifier); | 129 void SetDefaultQuota(int64 quota); |
| 130 |
135 bool IsDatabaseScheduledForDeletion(const string16& origin_identifier, | 131 bool IsDatabaseScheduledForDeletion(const string16& origin_identifier, |
136 const string16& database_name); | 132 const string16& database_name); |
137 | 133 |
| 134 // Deletes a single database. Returns net::OK on success, net::FAILED on |
| 135 // failure, or net::ERR_IO_PENDING and |callback| is invoked upon completion, |
| 136 // if non-NULL. |
| 137 int DeleteDatabase(const string16& origin_identifier, |
| 138 const string16& database_name, |
| 139 net::CompletionCallback* callback); |
| 140 |
138 // Delete any databases that have been touched since the cutoff date that's | 141 // Delete any databases that have been touched since the cutoff date that's |
139 // supplied. Returns net::OK on success, net::FAILED if not all databases | 142 // supplied. Returns net::OK on success, net::FAILED if not all databases |
140 // could be deleted, and net::ERR_IO_PENDING and |callback| is invoked upon | 143 // could be deleted, and net::ERR_IO_PENDING and |callback| is invoked upon |
141 // completion. | 144 // completion, if non-NULL. |
142 int DeleteDataModifiedSince(const base::Time& cutoff, | 145 int DeleteDataModifiedSince(const base::Time& cutoff, |
143 net::CompletionCallback* callback); | 146 net::CompletionCallback* callback); |
144 | 147 |
145 static void ClearLocalState(const FilePath& profile_path); | 148 static void ClearLocalState(const FilePath& profile_path); |
146 | 149 |
147 private: | 150 private: |
148 // Need this here to allow RefCountedThreadSafe to call ~DatabaseTracker(). | 151 // Need this here to allow RefCountedThreadSafe to call ~DatabaseTracker(). |
149 friend class base::RefCountedThreadSafe<DatabaseTracker>; | 152 friend class base::RefCountedThreadSafe<DatabaseTracker>; |
150 | 153 |
| 154 typedef std::map<string16, std::set<string16> > DatabaseSet; |
| 155 typedef std::map<net::CompletionCallback*, DatabaseSet> PendingCompletionMap; |
| 156 |
151 class CachedOriginInfo : public OriginInfo { | 157 class CachedOriginInfo : public OriginInfo { |
152 public: | 158 public: |
153 CachedOriginInfo() : OriginInfo(string16(), 0, 0) {} | 159 CachedOriginInfo() : OriginInfo(string16(), 0, 0) {} |
154 void SetOrigin(const string16& origin) { origin_ = origin; } | 160 void SetOrigin(const string16& origin) { origin_ = origin; } |
155 void SetQuota(int64 new_quota) { quota_ = new_quota; } | 161 void SetQuota(int64 new_quota) { quota_ = new_quota; } |
156 void SetDatabaseSize(const string16& database_name, int64 new_size) { | 162 void SetDatabaseSize(const string16& database_name, int64 new_size) { |
157 int64 old_size = 0; | 163 int64 old_size = 0; |
158 if (database_info_.find(database_name) != database_info_.end()) | 164 if (database_info_.find(database_name) != database_info_.end()) |
159 old_size = database_info_[database_name].first; | 165 old_size = database_info_[database_name].first; |
160 database_info_[database_name].first = new_size; | 166 database_info_[database_name].first = new_size; |
161 if (new_size != old_size) | 167 if (new_size != old_size) |
162 total_size_ += new_size - old_size; | 168 total_size_ += new_size - old_size; |
163 } | 169 } |
164 void SetDatabaseDescription(const string16& database_name, | 170 void SetDatabaseDescription(const string16& database_name, |
165 const string16& description) { | 171 const string16& description) { |
166 database_info_[database_name].second = description; | 172 database_info_[database_name].second = description; |
167 } | 173 } |
168 }; | 174 }; |
169 | 175 |
170 ~DatabaseTracker(); | 176 ~DatabaseTracker(); |
171 | 177 |
| 178 bool DeleteClosedDatabase(const string16& origin_identifier, |
| 179 const string16& database_name); |
| 180 bool DeleteOrigin(const string16& origin_identifier); |
| 181 |
172 bool LazyInit(); | 182 bool LazyInit(); |
173 bool UpgradeToCurrentVersion(); | 183 bool UpgradeToCurrentVersion(); |
174 void InsertOrUpdateDatabaseDetails(const string16& origin_identifier, | 184 void InsertOrUpdateDatabaseDetails(const string16& origin_identifier, |
175 const string16& database_name, | 185 const string16& database_name, |
176 const string16& database_details, | 186 const string16& database_details, |
177 int64 estimated_size); | 187 int64 estimated_size); |
178 | 188 |
179 void ClearAllCachedOriginInfo(); | 189 void ClearAllCachedOriginInfo(); |
180 CachedOriginInfo* GetCachedOriginInfo(const string16& origin_identifier); | 190 CachedOriginInfo* GetCachedOriginInfo(const string16& origin_identifier); |
181 | 191 |
(...skipping 11 matching lines...) Expand all Loading... |
193 const FilePath db_dir_; | 203 const FilePath db_dir_; |
194 scoped_ptr<sql::Connection> db_; | 204 scoped_ptr<sql::Connection> db_; |
195 scoped_ptr<DatabasesTable> databases_table_; | 205 scoped_ptr<DatabasesTable> databases_table_; |
196 scoped_ptr<QuotaTable> quota_table_; | 206 scoped_ptr<QuotaTable> quota_table_; |
197 scoped_ptr<sql::MetaTable> meta_table_; | 207 scoped_ptr<sql::MetaTable> meta_table_; |
198 ObserverList<Observer> observers_; | 208 ObserverList<Observer> observers_; |
199 std::map<string16, CachedOriginInfo> origins_info_map_; | 209 std::map<string16, CachedOriginInfo> origins_info_map_; |
200 DatabaseConnections database_connections_; | 210 DatabaseConnections database_connections_; |
201 | 211 |
202 // The set of databases that should be deleted but are still opened | 212 // The set of databases that should be deleted but are still opened |
203 std::map<string16, std::set<string16> > dbs_to_be_deleted_; | 213 DatabaseSet dbs_to_be_deleted_; |
204 net::CompletionCallback* dbs_deleted_callback_; | 214 PendingCompletionMap deletion_callbacks_; |
205 | 215 |
206 // Default quota for all origins; changed only by tests | 216 // Default quota for all origins; changed only by tests |
207 int64 default_quota_; | 217 int64 default_quota_; |
208 | 218 |
209 FRIEND_TEST(DatabaseTrackerTest, TestIt); | 219 FRIEND_TEST(DatabaseTrackerTest, TestIt); |
210 }; | 220 }; |
211 | 221 |
212 } // namespace webkit_database | 222 } // namespace webkit_database |
213 | 223 |
214 #endif // WEBKIT_DATABASE_DATABASE_TRACKER_H_ | 224 #endif // WEBKIT_DATABASE_DATABASE_TRACKER_H_ |
OLD | NEW |