OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/history/download_database.h" | 5 #include "chrome/browser/history/download_database.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "chrome/browser/history/download_row.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
19 #include "content/public/browser/download_persistent_store_info.h" | |
20 #include "sql/statement.h" | 20 #include "sql/statement.h" |
21 | 21 |
22 using content::DownloadItem; | 22 using content::DownloadItem; |
23 using content::DownloadPersistentStoreInfo; | |
24 | 23 |
25 namespace history { | 24 namespace history { |
26 | 25 |
| 26 // static |
| 27 const int64 DownloadDatabase::kUninitializedHandle = -1; |
| 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 static const char kSchema[] = | 31 static const char kSchema[] = |
30 "CREATE TABLE downloads (" | 32 "CREATE TABLE downloads (" |
31 "id INTEGER PRIMARY KEY," // SQLite-generated primary key. | 33 "id INTEGER PRIMARY KEY," // SQLite-generated primary key. |
32 "full_path LONGVARCHAR NOT NULL," // Location of the download on disk. | 34 "full_path LONGVARCHAR NOT NULL," // Location of the download on disk. |
33 "url LONGVARCHAR NOT NULL," // URL of the downloaded file. | 35 "url LONGVARCHAR NOT NULL," // URL of the downloaded file. |
34 "start_time INTEGER NOT NULL," // When the download was started. | 36 "start_time INTEGER NOT NULL," // When the download was started. |
35 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 37 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
36 "total_bytes INTEGER NOT NULL," // Total size of the download. | 38 "total_bytes INTEGER NOT NULL," // Total size of the download. |
37 "state INTEGER NOT NULL," // 1=complete, 2=cancelled, 4=interrupted | 39 "state INTEGER NOT NULL," // 1=complete, 2=cancelled, 4=interrupted |
38 "end_time INTEGER NOT NULL," // When the download completed. | 40 "end_time INTEGER NOT NULL," // When the download completed. |
39 "opened INTEGER NOT NULL)"; // 1 if it has ever been opened else 0 | 41 "opened INTEGER NOT NULL)"; // 1 if it has ever been opened else 0 |
40 | 42 |
41 // These constants and next two functions are used to allow | 43 // These constants and next two functions are used to allow |
42 // DownloadItem::DownloadState to change without breaking the database schema. | 44 // DownloadItem::DownloadState to change without breaking the database schema. |
43 // They guarantee that the values of the |state| field in the database are one | 45 // They guarantee that the values of the |state| field in the database are one |
44 // of the values returned by StateToInt, and that the values of the |state| | 46 // of the values returned by StateToInt, and that the values of the |state| |
45 // field of the DownloadPersistentStoreInfos returned by QueryDownloads() are | 47 // field of the DownloadRows returned by QueryDownloads() are one of the values |
46 // one of the values returned by IntToState(). | 48 // returned by IntToState(). |
47 static const int kStateInvalid = -1; | 49 static const int kStateInvalid = -1; |
48 static const int kStateInProgress = 0; | 50 static const int kStateInProgress = 0; |
49 static const int kStateComplete = 1; | 51 static const int kStateComplete = 1; |
50 static const int kStateCancelled = 2; | 52 static const int kStateCancelled = 2; |
51 static const int kStateBug140687 = 3; | 53 static const int kStateBug140687 = 3; |
52 static const int kStateInterrupted = 4; | 54 static const int kStateInterrupted = 4; |
53 | 55 |
54 int StateToInt(DownloadItem::DownloadState state) { | 56 int StateToInt(DownloadItem::DownloadState state) { |
55 switch (state) { | 57 switch (state) { |
56 case DownloadItem::IN_PROGRESS: return kStateInProgress; | 58 case DownloadItem::IN_PROGRESS: return kStateInProgress; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 DownloadDatabase::DownloadDatabase() | 107 DownloadDatabase::DownloadDatabase() |
106 : owning_thread_set_(false), | 108 : owning_thread_set_(false), |
107 owning_thread_(0), | 109 owning_thread_(0), |
108 next_id_(0), | 110 next_id_(0), |
109 next_db_handle_(0) { | 111 next_db_handle_(0) { |
110 } | 112 } |
111 | 113 |
112 DownloadDatabase::~DownloadDatabase() { | 114 DownloadDatabase::~DownloadDatabase() { |
113 } | 115 } |
114 | 116 |
115 void DownloadDatabase::CheckThread() { | |
116 if (owning_thread_set_) { | |
117 DCHECK(owning_thread_ == base::PlatformThread::CurrentId()); | |
118 } else { | |
119 owning_thread_ = base::PlatformThread::CurrentId(); | |
120 owning_thread_set_ = true; | |
121 } | |
122 } | |
123 | |
124 bool DownloadDatabase::EnsureColumnExists( | 117 bool DownloadDatabase::EnsureColumnExists( |
125 const std::string& name, const std::string& type) { | 118 const std::string& name, const std::string& type) { |
126 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; | 119 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; |
127 return GetDB().DoesColumnExist("downloads", name.c_str()) || | 120 return GetDB().DoesColumnExist("downloads", name.c_str()) || |
128 GetDB().Execute(add_col.c_str()); | 121 GetDB().Execute(add_col.c_str()); |
129 } | 122 } |
130 | 123 |
131 bool DownloadDatabase::MigrateDownloadsState() { | 124 bool DownloadDatabase::MigrateDownloadsState() { |
132 sql::Statement statement(GetDB().GetUniqueStatement( | 125 sql::Statement statement(GetDB().GetUniqueStatement( |
133 "UPDATE downloads SET state=? WHERE state=?")); | 126 "UPDATE downloads SET state=? WHERE state=?")); |
134 statement.BindInt(0, kStateInterrupted); | 127 statement.BindInt(0, kStateInterrupted); |
135 statement.BindInt(1, kStateBug140687); | 128 statement.BindInt(1, kStateBug140687); |
136 return statement.Run(); | 129 return statement.Run(); |
137 } | 130 } |
138 | 131 |
139 bool DownloadDatabase::InitDownloadTable() { | 132 bool DownloadDatabase::InitDownloadTable() { |
140 CheckThread(); | |
141 GetMetaTable().GetValue(kNextDownloadId, &next_id_); | 133 GetMetaTable().GetValue(kNextDownloadId, &next_id_); |
142 if (GetDB().DoesTableExist("downloads")) { | 134 if (GetDB().DoesTableExist("downloads")) { |
143 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && | 135 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
144 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); | 136 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); |
145 } else { | 137 } else { |
146 return GetDB().Execute(kSchema); | 138 return GetDB().Execute(kSchema); |
147 } | 139 } |
148 } | 140 } |
149 | 141 |
150 bool DownloadDatabase::DropDownloadTable() { | 142 bool DownloadDatabase::DropDownloadTable() { |
151 CheckThread(); | |
152 return GetDB().Execute("DROP TABLE downloads"); | 143 return GetDB().Execute("DROP TABLE downloads"); |
153 } | 144 } |
154 | 145 |
155 void DownloadDatabase::QueryDownloads( | 146 void DownloadDatabase::QueryDownloads( |
156 std::vector<DownloadPersistentStoreInfo>* results) { | 147 std::vector<DownloadRow>* results) { |
157 CheckThread(); | |
158 results->clear(); | 148 results->clear(); |
159 if (next_db_handle_ < 1) | 149 if (next_db_handle_ < 1) |
160 next_db_handle_ = 1; | 150 next_db_handle_ = 1; |
161 std::set<DownloadID> db_handles; | 151 std::set<int64> db_handles; |
162 | 152 |
163 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 153 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
164 "SELECT id, full_path, url, start_time, received_bytes, " | 154 "SELECT id, full_path, url, start_time, received_bytes, " |
165 "total_bytes, state, end_time, opened " | 155 "total_bytes, state, end_time, opened " |
166 "FROM downloads " | 156 "FROM downloads " |
167 "ORDER BY start_time")); | 157 "ORDER BY start_time")); |
168 | 158 |
169 while (statement.Step()) { | 159 while (statement.Step()) { |
170 DownloadPersistentStoreInfo info; | 160 DownloadRow info; |
171 info.db_handle = statement.ColumnInt64(0); | 161 info.db_handle = statement.ColumnInt64(0); |
172 info.path = ColumnFilePath(statement, 1); | 162 info.path = ColumnFilePath(statement, 1); |
173 info.url = GURL(statement.ColumnString(2)); | 163 info.url = GURL(statement.ColumnString(2)); |
174 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); | 164 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); |
175 info.received_bytes = statement.ColumnInt64(4); | 165 info.received_bytes = statement.ColumnInt64(4); |
176 info.total_bytes = statement.ColumnInt64(5); | 166 info.total_bytes = statement.ColumnInt64(5); |
177 int state = statement.ColumnInt(6); | 167 int state = statement.ColumnInt(6); |
178 info.state = IntToState(state); | 168 info.state = IntToState(state); |
179 info.end_time = base::Time::FromTimeT(statement.ColumnInt64(7)); | 169 info.end_time = base::Time::FromTimeT(statement.ColumnInt64(7)); |
180 info.opened = statement.ColumnInt(8) != 0; | 170 info.opened = statement.ColumnInt(8) != 0; |
181 if (info.db_handle >= next_db_handle_) | 171 if (info.db_handle >= next_db_handle_) |
182 next_db_handle_ = info.db_handle + 1; | 172 next_db_handle_ = info.db_handle + 1; |
183 if (!db_handles.insert(info.db_handle).second) { | 173 if (!db_handles.insert(info.db_handle).second) { |
184 // info.db_handle was already in db_handles. The database is corrupt. | 174 // info.db_handle was already in db_handles. The database is corrupt. |
185 base::debug::Alias(&info.db_handle); | 175 base::debug::Alias(&info.db_handle); |
186 DCHECK(false); | 176 DCHECK(false); |
187 } | 177 } |
188 if (info.state == DownloadItem::MAX_DOWNLOAD_STATE) { | 178 if (info.state == DownloadItem::MAX_DOWNLOAD_STATE) { |
189 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 179 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
190 continue; | 180 continue; |
191 } | 181 } |
192 results->push_back(info); | 182 results->push_back(info); |
193 } | 183 } |
194 } | 184 } |
195 | 185 |
196 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) { | 186 bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { |
197 CheckThread(); | |
198 DCHECK(data.db_handle > 0); | 187 DCHECK(data.db_handle > 0); |
199 int state = StateToInt(data.state); | 188 int state = StateToInt(data.state); |
200 if (state == kStateInvalid) { | 189 if (state == kStateInvalid) { |
201 // TODO(benjhayden) [D]CHECK instead. | 190 // TODO(benjhayden) [D]CHECK instead. |
202 return false; | 191 return false; |
203 } | 192 } |
204 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 193 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
205 "UPDATE downloads " | 194 "UPDATE downloads " |
206 "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?")); | 195 "SET full_path=?, received_bytes=?, state=?, end_time=?, total_bytes=?, " |
207 statement.BindInt64(0, data.received_bytes); | 196 "opened=? WHERE id=?")); |
208 statement.BindInt(1, state); | 197 BindFilePath(statement, data.path, 0); |
209 statement.BindInt64(2, data.end_time.ToTimeT()); | 198 statement.BindInt64(1, data.received_bytes); |
210 statement.BindInt(3, (data.opened ? 1 : 0)); | 199 statement.BindInt(2, state); |
211 statement.BindInt64(4, data.db_handle); | 200 statement.BindInt64(3, data.end_time.ToTimeT()); |
212 | 201 statement.BindInt(4, data.total_bytes); |
213 return statement.Run(); | 202 statement.BindInt(5, (data.opened ? 1 : 0)); |
214 } | 203 statement.BindInt64(6, data.db_handle); |
215 | |
216 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, | |
217 DownloadID db_handle) { | |
218 CheckThread(); | |
219 DCHECK(db_handle > 0); | |
220 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
221 "UPDATE downloads SET full_path=? WHERE id=?")); | |
222 BindFilePath(statement, path, 0); | |
223 statement.BindInt64(1, db_handle); | |
224 | 204 |
225 return statement.Run(); | 205 return statement.Run(); |
226 } | 206 } |
227 | 207 |
228 bool DownloadDatabase::CleanUpInProgressEntries() { | 208 bool DownloadDatabase::CleanUpInProgressEntries() { |
229 CheckThread(); | |
230 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 209 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
231 "UPDATE downloads SET state=? WHERE state=?")); | 210 "UPDATE downloads SET state=? WHERE state=?")); |
232 statement.BindInt(0, kStateCancelled); | 211 statement.BindInt(0, kStateCancelled); |
233 statement.BindInt(1, kStateInProgress); | 212 statement.BindInt(1, kStateInProgress); |
234 | 213 |
235 return statement.Run(); | 214 return statement.Run(); |
236 } | 215 } |
237 | 216 |
238 int64 DownloadDatabase::CreateDownload( | 217 int64 DownloadDatabase::CreateDownload( |
239 const DownloadPersistentStoreInfo& info) { | 218 const DownloadRow& info) { |
240 CheckThread(); | |
241 | |
242 if (next_db_handle_ == 0) { | 219 if (next_db_handle_ == 0) { |
243 // This is unlikely. All current known tests and users already call | 220 // This is unlikely. All current known tests and users already call |
244 // QueryDownloads() before CreateDownload(). | 221 // QueryDownloads() before CreateDownload(). |
245 std::vector<DownloadPersistentStoreInfo> results; | 222 std::vector<DownloadRow> results; |
246 QueryDownloads(&results); | 223 QueryDownloads(&results); |
247 CHECK_NE(0, next_db_handle_); | 224 CHECK_NE(0, next_db_handle_); |
248 } | 225 } |
249 | 226 |
250 int state = StateToInt(info.state); | 227 int state = StateToInt(info.state); |
251 if (state == kStateInvalid) | 228 if (state == kStateInvalid) |
252 return false; | 229 return kUninitializedHandle; |
253 | 230 |
254 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 231 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
255 "INSERT INTO downloads " | 232 "INSERT INTO downloads " |
256 "(id, full_path, url, start_time, received_bytes, total_bytes, state, " | 233 "(id, full_path, url, start_time, received_bytes, total_bytes, state, " |
257 "end_time, opened) " | 234 "end_time, opened) " |
258 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")); | 235 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
259 | 236 |
260 int db_handle = next_db_handle_++; | 237 int db_handle = next_db_handle_++; |
261 | 238 |
262 statement.BindInt64(0, db_handle); | 239 statement.BindInt64(0, db_handle); |
263 BindFilePath(statement, info.path, 1); | 240 BindFilePath(statement, info.path, 1); |
264 statement.BindString(2, info.url.spec()); | 241 statement.BindString(2, info.url.spec()); |
265 statement.BindInt64(3, info.start_time.ToTimeT()); | 242 statement.BindInt64(3, info.start_time.ToTimeT()); |
266 statement.BindInt64(4, info.received_bytes); | 243 statement.BindInt64(4, info.received_bytes); |
267 statement.BindInt64(5, info.total_bytes); | 244 statement.BindInt64(5, info.total_bytes); |
268 statement.BindInt(6, state); | 245 statement.BindInt(6, state); |
269 statement.BindInt64(7, info.end_time.ToTimeT()); | 246 statement.BindInt64(7, info.end_time.ToTimeT()); |
270 statement.BindInt(8, info.opened ? 1 : 0); | 247 statement.BindInt(8, info.opened ? 1 : 0); |
271 | 248 |
272 if (statement.Run()) { | 249 if (statement.Run()) { |
273 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} | 250 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} |
274 GetMetaTable().SetValue(kNextDownloadId, ++next_id_); | 251 GetMetaTable().SetValue(kNextDownloadId, ++next_id_); |
275 | 252 |
276 return db_handle; | 253 return db_handle; |
277 } | 254 } |
278 return 0; | 255 return kUninitializedHandle; |
279 } | 256 } |
280 | 257 |
281 void DownloadDatabase::RemoveDownload(DownloadID db_handle) { | 258 void DownloadDatabase::RemoveDownload(int64 handle) { |
282 CheckThread(); | |
283 | |
284 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 259 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
285 "DELETE FROM downloads WHERE id=?")); | 260 "DELETE FROM downloads WHERE id=?")); |
286 statement.BindInt64(0, db_handle); | 261 statement.BindInt64(0, handle); |
287 | |
288 statement.Run(); | 262 statement.Run(); |
289 } | 263 } |
290 | 264 |
291 bool DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin, | 265 int DownloadDatabase::CountDownloads() { |
292 base::Time delete_end) { | 266 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
293 CheckThread(); | 267 "SELECT count(*) from downloads")); |
294 time_t start_time = delete_begin.ToTimeT(); | 268 statement.Step(); |
295 time_t end_time = delete_end.ToTimeT(); | 269 return statement.ColumnInt(0); |
296 | |
297 int num_downloads_deleted = -1; | |
298 { | |
299 sql::Statement count(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
300 "SELECT count(*) FROM downloads WHERE start_time >= ? " | |
301 "AND start_time < ? AND (State = ? OR State = ? OR State = ?)")); | |
302 count.BindInt64(0, start_time); | |
303 count.BindInt64( | |
304 1, | |
305 end_time ? end_time : std::numeric_limits<int64>::max()); | |
306 count.BindInt(2, kStateComplete); | |
307 count.BindInt(3, kStateCancelled); | |
308 count.BindInt(4, kStateInterrupted); | |
309 if (count.Step()) | |
310 num_downloads_deleted = count.ColumnInt(0); | |
311 } | |
312 | |
313 | |
314 bool success = false; | |
315 base::TimeTicks started_removing = base::TimeTicks::Now(); | |
316 { | |
317 // This does not use an index. We currently aren't likely to have enough | |
318 // downloads where an index by time will give us a lot of benefit. | |
319 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
320 "DELETE FROM downloads WHERE start_time >= ? AND start_time < ? " | |
321 "AND (State = ? OR State = ? OR State = ?)")); | |
322 statement.BindInt64(0, start_time); | |
323 statement.BindInt64( | |
324 1, | |
325 end_time ? end_time : std::numeric_limits<int64>::max()); | |
326 statement.BindInt(2, kStateComplete); | |
327 statement.BindInt(3, kStateCancelled); | |
328 statement.BindInt(4, kStateInterrupted); | |
329 | |
330 success = statement.Run(); | |
331 } | |
332 | |
333 base::TimeTicks finished_removing = base::TimeTicks::Now(); | |
334 | |
335 if (num_downloads_deleted >= 0) { | |
336 UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount", | |
337 num_downloads_deleted); | |
338 base::TimeDelta micros = (1000 * (finished_removing - started_removing)); | |
339 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros); | |
340 if (num_downloads_deleted > 0) { | |
341 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord", | |
342 (1000 * micros) / num_downloads_deleted); | |
343 } | |
344 } | |
345 | |
346 return success; | |
347 } | 270 } |
348 | 271 |
349 } // namespace history | 272 } // namespace history |
OLD | NEW |