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/stl_util.h" | |
14 #include "base/time.h" | 15 #include "base/time.h" |
15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
16 #include "build/build_config.h" | 17 #include "build/build_config.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 "content/public/browser/download_persistent_store_info.h" |
21 #include "content/public/browser/download_interrupt_reasons.h" | |
20 #include "sql/statement.h" | 22 #include "sql/statement.h" |
21 | 23 |
22 using content::DownloadItem; | 24 using content::DownloadItem; |
23 using content::DownloadPersistentStoreInfo; | 25 using content::DownloadPersistentStoreInfo; |
24 | 26 |
25 namespace history { | 27 namespace history { |
26 | 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," // Primary key. |
32 "full_path LONGVARCHAR NOT NULL," // Location of the download on disk. | 34 "target_path LONGVARCHAR NOT NULL," // Final location of the download on disk. |
33 "url LONGVARCHAR NOT NULL," // URL of the downloaded file. | 35 "current_path LONGVARCHAR NOT NULL," // Current location of the download |
36 // on disk. | |
34 "start_time INTEGER NOT NULL," // When the download was started. | 37 "start_time INTEGER NOT NULL," // When the download was started. |
35 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 38 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
36 "total_bytes INTEGER NOT NULL," // Total size of the download. | 39 "total_bytes INTEGER NOT NULL," // Total size of the download. |
37 "state INTEGER NOT NULL," // 1=complete, 2=cancelled, 4=interrupted | 40 "state INTEGER NOT NULL," // 1=complete, 4=interrupted |
41 "interrupt_reason INTEGER NOT NULL,"// Reason the download was interrupted. | |
38 "end_time INTEGER NOT NULL," // When the download completed. | 42 "end_time INTEGER NOT NULL," // When the download completed. |
39 "opened INTEGER NOT NULL)"; // 1 if it has ever been opened else 0 | 43 "opened INTEGER NOT NULL)"; // 1 if it has ever been opened else 0 |
40 | 44 |
45 static const char kUrlChainSchema[] = | |
46 "CREATE TABLE downloads_url_chains (" | |
47 "id INTEGER NOT NULL," // SQLite-generated primary key. | |
sky
2012/11/14 00:34:31
Your comment is a bit misleading here. Don't you m
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Right. Copy and paste error. Done.
| |
48 "chain_index INTEGER NOT NULL," // Index of url in chain | |
49 // 0 is initial target, | |
50 // MAX is target after redirects. | |
51 "url LONGVARCHAR NOT NULL, " // URL. | |
52 "PRIMARY KEY (id, chain_index) )"; | |
53 | |
41 // These constants and next two functions are used to allow | 54 // These constants and next two functions are used to allow |
42 // DownloadItem::DownloadState to change without breaking the database schema. | 55 // DownloadItem::DownloadState to change without breaking the database schema. |
43 // They guarantee that the values of the |state| field in the database are one | 56 // 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| | 57 // of the values returned by StateToInt, and that the values of the |state| |
45 // field of the DownloadPersistentStoreInfos returned by QueryDownloads() are | 58 // field of the DownloadPersistentStoreInfos returned by QueryDownloads() are |
46 // one of the values returned by IntToState(). | 59 // one of the values returned by IntToState(). |
47 static const int kStateInvalid = -1; | 60 static const int kStateInvalid = -1; |
48 static const int kStateInProgress = 0; | 61 static const int kStateInProgress = 0; |
49 static const int kStateComplete = 1; | 62 static const int kStateComplete = 1; |
50 static const int kStateCancelled = 2; | 63 static const int kStateCancelled = 2; |
51 static const int kStateBug140687 = 3; | 64 static const int kStateBug140687 = 3; |
52 static const int kStateInterrupted = 4; | 65 static const int kStateInterrupted = 4; |
53 | 66 |
54 int StateToInt(DownloadItem::DownloadState state) { | 67 int StateToInt(DownloadItem::DownloadState state) { |
55 switch (state) { | 68 switch (state) { |
56 case DownloadItem::IN_PROGRESS: return kStateInProgress; | 69 case DownloadItem::IN_PROGRESS: return kStateInProgress; |
57 case DownloadItem::COMPLETE: return kStateComplete; | 70 case DownloadItem::COMPLETE: return kStateComplete; |
58 case DownloadItem::CANCELLED: return kStateCancelled; | 71 case DownloadItem::CANCELLED: return kStateCancelled; |
59 case DownloadItem::INTERRUPTED: return kStateInterrupted; | 72 case DownloadItem::INTERRUPTED: return kStateInterrupted; |
60 case DownloadItem::MAX_DOWNLOAD_STATE: return kStateInvalid; | 73 case DownloadItem::MAX_DOWNLOAD_STATE: return kStateInvalid; |
61 default: return kStateInvalid; | 74 default: return kStateInvalid; |
62 } | 75 } |
63 } | 76 } |
64 | 77 |
65 DownloadItem::DownloadState IntToState(int state) { | 78 DownloadItem::DownloadState IntToState(int state) { |
66 switch (state) { | 79 switch (state) { |
67 case kStateInProgress: return DownloadItem::IN_PROGRESS; | 80 case kStateInProgress: return DownloadItem::IN_PROGRESS; |
68 case kStateComplete: return DownloadItem::COMPLETE; | 81 case kStateComplete: return DownloadItem::COMPLETE; |
69 case kStateCancelled: return DownloadItem::CANCELLED; | 82 case kStateCancelled: return DownloadItem::CANCELLED; |
70 // We should not need kStateBug140687 here because MigrateDownloadState() | 83 // We should not need kStateBug140687 here because MigrateDownloadsState() |
71 // is called in HistoryDatabase::Init(). | 84 // is called in HistoryDatabase::Init(). |
72 case kStateInterrupted: return DownloadItem::INTERRUPTED; | 85 case kStateInterrupted: return DownloadItem::INTERRUPTED; |
73 default: return DownloadItem::MAX_DOWNLOAD_STATE; | 86 default: return DownloadItem::MAX_DOWNLOAD_STATE; |
74 } | 87 } |
75 } | 88 } |
76 | 89 |
77 #if defined(OS_POSIX) | 90 #if defined(OS_POSIX) |
78 | 91 |
79 // Binds/reads the given file path to the given column of the given statement. | 92 // Binds/reads the given file path to the given column of the given statement. |
80 void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { | 93 void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 } | 142 } |
130 | 143 |
131 bool DownloadDatabase::MigrateDownloadsState() { | 144 bool DownloadDatabase::MigrateDownloadsState() { |
132 sql::Statement statement(GetDB().GetUniqueStatement( | 145 sql::Statement statement(GetDB().GetUniqueStatement( |
133 "UPDATE downloads SET state=? WHERE state=?")); | 146 "UPDATE downloads SET state=? WHERE state=?")); |
134 statement.BindInt(0, kStateInterrupted); | 147 statement.BindInt(0, kStateInterrupted); |
135 statement.BindInt(1, kStateBug140687); | 148 statement.BindInt(1, kStateBug140687); |
136 return statement.Run(); | 149 return statement.Run(); |
137 } | 150 } |
138 | 151 |
152 bool DownloadDatabase::MigrateReasonAndPaths() { | |
153 // We need to rename the table and copy back from it because SQLite | |
154 // provides no way to rename or delete a column. | |
155 if (!GetDB().Execute("ALTER TABLE downloads RENAME TO downloads_tmp")) | |
156 return false; | |
157 | |
158 // Recreate main table and populate it. | |
159 if (!GetDB().Execute(kSchema)) | |
160 return false; | |
161 | |
162 sql::Statement statement_populate(GetDB().GetUniqueStatement( | |
163 "INSERT INTO downloads " | |
164 "( id, target_path, current_path, start_time, received_bytes, total_bytes, " | |
165 " state, interrupt_reason, end_time, opened ) " | |
166 "SELECT id, full_path, full_path, start_time, received_bytes, total_bytes, " | |
167 " state, ?, end_time, opened " | |
168 "FROM downloads_tmp")); | |
169 statement_populate.BindInt(0, content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
sky
2012/11/14 00:34:31
I get nervous when we start persisting enums from
benjhayden
2012/11/14 19:43:22
I agree with Scott. This is how we ran into troubl
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Fair enough. There are enough errors in the enum
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Still pushing back, Milo--keeping 20 interrupt rea
benjhayden
2012/11/15 15:57:41
Comments don't do anything for me or the compiler.
| |
170 if (!statement_populate.Run()) | |
171 return false; | |
172 | |
173 // Create new chain table and populate it. | |
174 if (!GetDB().Execute(kUrlChainSchema)) | |
175 return false; | |
176 | |
177 if (!GetDB().Execute("INSERT INTO downloads_url_chains " | |
178 " ( id, chain_index, url) " | |
179 " SELECT id, 0, url from downloads_tmp")) | |
180 return false; | |
181 | |
182 // Get rid of temporary table. | |
183 if (!GetDB().Execute("DROP TABLE downloads_tmp")) | |
184 return false; | |
185 | |
186 return true; | |
187 } | |
188 | |
139 bool DownloadDatabase::InitDownloadTable() { | 189 bool DownloadDatabase::InitDownloadTable() { |
140 CheckThread(); | 190 CheckThread(); |
141 GetMetaTable().GetValue(kNextDownloadId, &next_id_); | 191 GetMetaTable().GetValue(kNextDownloadId, &next_id_); |
142 if (GetDB().DoesTableExist("downloads")) { | 192 if (GetDB().DoesTableExist("downloads")) { |
benjhayden
2012/11/14 19:43:22
&& DoesTableExist("downloads_url_chains")?
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Done, though I'm a little uncomfortable that I don
benjhayden
2012/11/15 15:57:41
The schema change represented by these EnsureColum
| |
143 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && | 193 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
144 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); | 194 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); |
145 } else { | 195 } else { |
146 return GetDB().Execute(kSchema); | 196 return GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema); |
147 } | 197 } |
148 } | 198 } |
149 | 199 |
150 bool DownloadDatabase::DropDownloadTable() { | 200 bool DownloadDatabase::DropDownloadTable() { |
151 CheckThread(); | 201 CheckThread(); |
152 return GetDB().Execute("DROP TABLE downloads"); | 202 return GetDB().Execute("DROP TABLE downloads"); |
153 } | 203 } |
154 | 204 |
155 void DownloadDatabase::QueryDownloads( | 205 void DownloadDatabase::QueryDownloads( |
156 std::vector<DownloadPersistentStoreInfo>* results) { | 206 std::vector<DownloadPersistentStoreInfo>* results) { |
157 CheckThread(); | 207 CheckThread(); |
158 results->clear(); | 208 results->clear(); |
159 if (next_db_handle_ < 1) | 209 if (next_db_handle_ < 1) |
160 next_db_handle_ = 1; | 210 next_db_handle_ = 1; |
161 std::set<DownloadID> db_handles; | 211 std::set<DownloadID> db_handles; |
162 | 212 |
163 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 213 std::map<DownloadID, DownloadPersistentStoreInfo*> info_map; |
164 "SELECT id, full_path, url, start_time, received_bytes, " | 214 |
165 "total_bytes, state, end_time, opened " | 215 sql::Statement statement_main(GetDB().GetCachedStatement(SQL_FROM_HERE, |
216 "SELECT id, target_path, current_path, start_time, received_bytes, " | |
217 "total_bytes, state, interrupt_reason, end_time, opened " | |
166 "FROM downloads " | 218 "FROM downloads " |
167 "ORDER BY start_time")); | 219 "ORDER BY start_time")); |
168 | 220 |
169 while (statement.Step()) { | 221 while (statement_main.Step()) { |
170 DownloadPersistentStoreInfo info; | 222 DownloadPersistentStoreInfo* info(new DownloadPersistentStoreInfo); |
171 info.db_handle = statement.ColumnInt64(0); | 223 int column = 0; |
benjhayden
2012/11/14 19:43:22
I've also been very tempted to do this, and it is
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Does that make it different than what it replaces?
benjhayden
2012/11/15 15:57:41
I think there's a philosophical subtlety here. Let
| |
172 info.path = ColumnFilePath(statement, 1); | 224 |
173 info.url = GURL(statement.ColumnString(2)); | 225 info->db_handle = statement_main.ColumnInt64(column++); |
174 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); | 226 info->target_path = ColumnFilePath(statement_main, column++); |
175 info.received_bytes = statement.ColumnInt64(4); | 227 info->current_path = ColumnFilePath(statement_main, column++); |
176 info.total_bytes = statement.ColumnInt64(5); | 228 info->start_time = base::Time::FromTimeT( |
177 int state = statement.ColumnInt(6); | 229 statement_main.ColumnInt64(column++)); |
178 info.state = IntToState(state); | 230 info->received_bytes = statement_main.ColumnInt64(column++); |
179 info.end_time = base::Time::FromTimeT(statement.ColumnInt64(7)); | 231 info->total_bytes = statement_main.ColumnInt64(column++); |
180 info.opened = statement.ColumnInt(8) != 0; | 232 int state = statement_main.ColumnInt(column++); |
181 if (info.db_handle >= next_db_handle_) | 233 info->state = IntToState(state); |
182 next_db_handle_ = info.db_handle + 1; | 234 info->interrupt_reason = static_cast<content::DownloadInterruptReason>( |
183 if (!db_handles.insert(info.db_handle).second) { | 235 statement_main.ColumnInt(column++)); |
184 // info.db_handle was already in db_handles. The database is corrupt. | 236 info->end_time = base::Time::FromTimeT( |
185 base::debug::Alias(&info.db_handle); | 237 statement_main.ColumnInt64(column++)); |
238 info->opened = statement_main.ColumnInt(column++) != 0; | |
239 if (info->db_handle >= next_db_handle_) | |
240 next_db_handle_ = info->db_handle + 1; | |
241 if (!db_handles.insert(info->db_handle).second) { | |
242 // info->db_handle was already in db_handles. The database is corrupt. | |
243 base::debug::Alias(&info->db_handle); | |
186 DCHECK(false); | 244 DCHECK(false); |
187 } | 245 } |
188 if (info.state == DownloadItem::MAX_DOWNLOAD_STATE) { | 246 if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) { |
189 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 247 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
190 continue; | 248 continue; |
191 } | 249 } |
192 results->push_back(info); | 250 DCHECK(!ContainsKey(info_map, info->db_handle)); |
251 info_map[info->db_handle] = info; | |
252 } | |
253 | |
254 sql::Statement statement_chain(GetDB().GetCachedStatement( | |
255 SQL_FROM_HERE, | |
256 "SELECT id, chain_index, url FROM downloads_url_chains " | |
257 "ORDER BY id, chain_index")); | |
258 | |
259 while (statement_chain.Step()) { | |
260 int64 db_handle = statement_chain.ColumnInt64(0); | |
261 int chain_index = statement_chain.ColumnInt(1); | |
262 | |
263 // Note that these DCHECKs may trip as a result of corrupted databases. | |
264 // We have them because in debug builds the chances are higher there's | |
265 // an actual bug than that the database is corrupt, but we handle the | |
266 // DB corruption case in production code. | |
267 | |
268 // Confirm the handle has already been seen--if it hasn't, discard the | |
269 // record. | |
270 DCHECK(ContainsKey(info_map, db_handle)); | |
271 if (!ContainsKey(info_map, db_handle)) | |
272 continue; | |
273 | |
274 // Confirm all previous URLs in the chain have already been seen; | |
275 // if not, fill in with null or discard record. | |
276 int current_chain_size = info_map[db_handle]->url_chain.size(); | |
277 std::vector<GURL>* url_chain(&info_map[db_handle]->url_chain); | |
278 DCHECK_EQ(chain_index, current_chain_size); | |
279 while (current_chain_size < chain_index) { | |
280 url_chain->push_back(GURL()); | |
281 current_chain_size++; | |
282 } | |
283 if (current_chain_size > chain_index) | |
284 continue; | |
benjhayden
2012/11/14 19:43:22
Why not overwrite?
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
I flipped a coin :-}. Do you have some reason to
benjhayden
2012/11/15 15:57:41
Ah, I missed the ORDER BY clause and thought that
| |
285 | |
286 // Save the record. | |
287 url_chain->push_back(GURL(statement_chain.ColumnString(2))); | |
288 } | |
289 | |
290 for (std::map<DownloadID, DownloadPersistentStoreInfo*>::iterator | |
291 it = info_map.begin(); it != info_map.end(); ++it) { | |
292 results->push_back(*it->second); | |
293 delete it->second; | |
294 it->second = NULL; | |
193 } | 295 } |
194 } | 296 } |
195 | 297 |
196 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) { | 298 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) { |
197 CheckThread(); | 299 CheckThread(); |
198 DCHECK(data.db_handle > 0); | 300 DCHECK(data.db_handle > 0); |
199 int state = StateToInt(data.state); | 301 int state = StateToInt(data.state); |
200 if (state == kStateInvalid) { | 302 if (state == kStateInvalid) { |
201 // TODO(benjhayden) [D]CHECK instead. | 303 // TODO(benjhayden) [D]CHECK instead. |
202 return false; | 304 return false; |
203 } | 305 } |
204 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 306 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
205 "UPDATE downloads " | 307 "UPDATE downloads " |
206 "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?")); | 308 "SET received_bytes=?, state=?, interrupt_reason=?, end_time=?, " |
207 statement.BindInt64(0, data.received_bytes); | 309 " opened=? WHERE id=?")); |
208 statement.BindInt(1, state); | 310 |
209 statement.BindInt64(2, data.end_time.ToTimeT()); | 311 int column = 0; |
210 statement.BindInt(3, (data.opened ? 1 : 0)); | 312 statement.BindInt64(column++, data.received_bytes); |
211 statement.BindInt64(4, data.db_handle); | 313 statement.BindInt(column++, state); |
314 statement.BindInt(column++, data.interrupt_reason); | |
315 statement.BindInt64(column++, data.end_time.ToTimeT()); | |
316 statement.BindInt(column++, (data.opened ? 1 : 0)); | |
317 statement.BindInt64(column++, data.db_handle); | |
212 | 318 |
213 return statement.Run(); | 319 return statement.Run(); |
214 } | 320 } |
215 | 321 |
216 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, | 322 bool DownloadDatabase::UpdateDownloadPath( |
217 DownloadID db_handle) { | 323 const FilePath& target_path, const FilePath& current_path, |
324 DownloadID db_handle) { | |
218 CheckThread(); | 325 CheckThread(); |
219 DCHECK(db_handle > 0); | 326 DCHECK(db_handle > 0); |
220 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 327 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
221 "UPDATE downloads SET full_path=? WHERE id=?")); | 328 "UPDATE downloads SET target_path=?, current_path=? WHERE id=?")); |
222 BindFilePath(statement, path, 0); | 329 BindFilePath(statement, target_path, 0); |
330 BindFilePath(statement, current_path, 0); | |
benjhayden
2012/11/14 19:43:22
Check the column index here.
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Whoops. Good catch. Done. I'll see if I can wri
| |
223 statement.BindInt64(1, db_handle); | 331 statement.BindInt64(1, db_handle); |
224 | 332 |
225 return statement.Run(); | 333 return statement.Run(); |
226 } | 334 } |
227 | 335 |
228 bool DownloadDatabase::CleanUpInProgressEntries() { | 336 bool DownloadDatabase::CleanUpInProgressEntries() { |
229 CheckThread(); | 337 CheckThread(); |
230 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 338 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
231 "UPDATE downloads SET state=? WHERE state=?")); | 339 "UPDATE downloads SET state=? WHERE state=?")); |
232 statement.BindInt(0, kStateCancelled); | 340 statement.BindInt(0, kStateCancelled); |
(...skipping 11 matching lines...) Expand all Loading... | |
244 // QueryDownloads() before CreateDownload(). | 352 // QueryDownloads() before CreateDownload(). |
245 std::vector<DownloadPersistentStoreInfo> results; | 353 std::vector<DownloadPersistentStoreInfo> results; |
246 QueryDownloads(&results); | 354 QueryDownloads(&results); |
247 CHECK_NE(0, next_db_handle_); | 355 CHECK_NE(0, next_db_handle_); |
248 } | 356 } |
249 | 357 |
250 int state = StateToInt(info.state); | 358 int state = StateToInt(info.state); |
251 if (state == kStateInvalid) | 359 if (state == kStateInvalid) |
252 return false; | 360 return false; |
253 | 361 |
254 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
255 "INSERT INTO downloads " | |
256 "(id, full_path, url, start_time, received_bytes, total_bytes, state, " | |
257 "end_time, opened) " | |
258 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")); | |
259 | |
260 int db_handle = next_db_handle_++; | 362 int db_handle = next_db_handle_++; |
261 | 363 |
262 statement.BindInt64(0, db_handle); | 364 { |
263 BindFilePath(statement, info.path, 1); | 365 sql::Statement statement_insert(GetDB().GetCachedStatement( |
264 statement.BindString(2, info.url.spec()); | 366 SQL_FROM_HERE, |
265 statement.BindInt64(3, info.start_time.ToTimeT()); | 367 "INSERT INTO downloads " |
sky
2012/11/14 00:34:31
Since you're writing migration code, could you mig
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Done.
| |
266 statement.BindInt64(4, info.received_bytes); | 368 "(id, target_path, current_path, start_time, " |
267 statement.BindInt64(5, info.total_bytes); | 369 " received_bytes, total_bytes, state, interrupt_reason, " |
268 statement.BindInt(6, state); | 370 " end_time, opened) " |
269 statement.BindInt64(7, info.end_time.ToTimeT()); | 371 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
270 statement.BindInt(8, info.opened ? 1 : 0); | |
271 | 372 |
272 if (statement.Run()) { | 373 int column = 0; |
273 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} | 374 statement_insert.BindInt64(column++, db_handle); |
274 GetMetaTable().SetValue(kNextDownloadId, ++next_id_); | 375 BindFilePath(statement_insert, info.target_path, column++); |
376 BindFilePath(statement_insert, info.current_path, column++); | |
377 statement_insert.BindInt64(column++, info.start_time.ToTimeT()); | |
378 statement_insert.BindInt64(column++, info.received_bytes); | |
379 statement_insert.BindInt64(column++, info.total_bytes); | |
380 statement_insert.BindInt(column++, state); | |
381 statement_insert.BindInt(column++, content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
382 statement_insert.BindInt64(column++, info.end_time.ToTimeT()); | |
383 statement_insert.BindInt(column++, info.opened ? 1 : 0); | |
384 if (!statement_insert.Run()) { | |
385 LOG(WARNING) << "Main insertion for download create failed."; | |
386 return 0; | |
benjhayden
2012/11/14 19:43:22
When you merge with my DownloadHistory, please be
Randy Smith (Not in Mondays)
2012/11/14 22:47:34
Will do.
| |
387 } | |
388 } | |
275 | 389 |
276 return db_handle; | 390 sql::Statement statement_insert_chain( |
391 GetDB().GetCachedStatement(SQL_FROM_HERE, | |
392 "INSERT INTO downloads_url_chains " | |
393 "(id, chain_index, url) " | |
394 "VALUES (?, ?, ?)")); | |
395 for (size_t i = 0; i < info.url_chain.size(); ++i) { | |
396 statement_insert_chain.BindInt64(0, db_handle); | |
397 statement_insert_chain.BindInt(1, i); | |
398 statement_insert_chain.BindString(2, info.url_chain[i].spec()); | |
399 if (!statement_insert_chain.Run()) { | |
400 LOG(WARNING) << "Url insertion for download create failed."; | |
401 return 0; | |
402 } | |
403 statement_insert_chain.Reset(true); | |
277 } | 404 } |
278 return 0; | 405 |
406 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} | |
407 GetMetaTable().SetValue(kNextDownloadId, ++next_id_); | |
408 | |
409 return db_handle; | |
279 } | 410 } |
280 | 411 |
281 void DownloadDatabase::RemoveDownload(DownloadID db_handle) { | 412 void DownloadDatabase::RemoveDownload(DownloadID db_handle) { |
282 CheckThread(); | 413 CheckThread(); |
283 | 414 |
284 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 415 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
285 "DELETE FROM downloads WHERE id=?")); | 416 "DELETE FROM downloads WHERE id=?")); |
286 statement.BindInt64(0, db_handle); | 417 statement.BindInt64(0, db_handle); |
287 | 418 |
288 statement.Run(); | 419 statement.Run(); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 if (num_downloads_deleted > 0) { | 471 if (num_downloads_deleted > 0) { |
341 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord", | 472 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord", |
342 (1000 * micros) / num_downloads_deleted); | 473 (1000 * micros) / num_downloads_deleted); |
343 } | 474 } |
344 } | 475 } |
345 | 476 |
346 return success; | 477 return success; |
347 } | 478 } |
348 | 479 |
349 } // namespace history | 480 } // namespace history |
OLD | NEW |