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 "components/history/core/browser/download_database.h" | 5 #include "components/history/core/browser/download_database.h" |
6 | 6 |
| 7 #include <inttypes.h> |
| 8 |
7 #include <limits> | 9 #include <limits> |
8 #include <memory> | 10 #include <memory> |
9 #include <string> | 11 #include <string> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/debug/alias.h" | 14 #include "base/debug/alias.h" |
13 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
14 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/rand_util.h" |
15 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
16 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
18 #include "base/time/time.h" | 21 #include "base/time/time.h" |
19 #include "build/build_config.h" | 22 #include "build/build_config.h" |
20 #include "components/history/core/browser/download_constants.h" | 23 #include "components/history/core/browser/download_constants.h" |
21 #include "components/history/core/browser/download_row.h" | 24 #include "components/history/core/browser/download_row.h" |
22 #include "components/history/core/browser/history_types.h" | 25 #include "components/history/core/browser/history_types.h" |
23 #include "sql/statement.h" | 26 #include "sql/statement.h" |
24 | 27 |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 // This GUID generation scheme is only used for migrated download rows and | 234 // This GUID generation scheme is only used for migrated download rows and |
232 // assumes that the likelihood of a collision with a GUID generated via | 235 // assumes that the likelihood of a collision with a GUID generated via |
233 // base::GenerateGUID() will be vanishingly small. | 236 // base::GenerateGUID() will be vanishingly small. |
234 // | 237 // |
235 // A previous version of this code generated GUIDs that used random bits for | 238 // A previous version of this code generated GUIDs that used random bits for |
236 // all but the first 32-bits. I.e. the scheme didn't respect the 6 fixed bits | 239 // all but the first 32-bits. I.e. the scheme didn't respect the 6 fixed bits |
237 // as prescribed for type 4 GUIDs. The resulting GUIDs are not believed to | 240 // as prescribed for type 4 GUIDs. The resulting GUIDs are not believed to |
238 // have an elevated risk of collision with GUIDs generated via | 241 // have an elevated risk of collision with GUIDs generated via |
239 // base::GenerateGUID() and are considered valid by all known consumers. Hence | 242 // base::GenerateGUID() and are considered valid by all known consumers. Hence |
240 // no additional migration logic is being introduced to fix those GUIDs. | 243 // no additional migration logic is being introduced to fix those GUIDs. |
241 const char kMigrateGuidsQuery[] = | 244 sql::Statement select(GetDB().GetUniqueStatement("SELECT id FROM downloads")); |
242 "UPDATE downloads SET guid = printf" | 245 sql::Statement update( |
243 "(\"%08X-%s-4%s-%01X%s-%s\"," | 246 GetDB().GetUniqueStatement("UPDATE downloads SET guid = ? WHERE id = ?")); |
244 " id," | 247 while (select.Step()) { |
245 " hex(randomblob(2))," | 248 uint32_t id = select.ColumnInt(0); |
246 " substr(hex(randomblob(2)),2)," | 249 uint64_t r1 = base::RandUint64(); |
247 " (8 | (random() & 3))," | 250 uint64_t r2 = base::RandUint64(); |
248 " substr(hex(randomblob(2)),2)," | 251 std::string guid = base::StringPrintf( |
249 " hex(randomblob(6)))"; | 252 "%08" PRIX32 "-%04" PRIX64 "-4%03" PRIX64 "-%04" PRIX64 "-%012" PRIX64, |
250 return GetDB().Execute(kMigrateGuidsQuery); | 253 id, r1 >> 48, |
| 254 (r1 >> 36) & 0xfff, |
| 255 ((8 | ((r1 >> 34) & 3)) << 12) | ((r1 >> 22) & 0xfff), |
| 256 r2 & 0xffffffffffff); |
| 257 update.BindString(0, guid); |
| 258 update.BindInt(1, id); |
| 259 if (!update.Run()) |
| 260 return false; |
| 261 update.Reset(true); |
| 262 } |
| 263 return true; |
251 } | 264 } |
252 | 265 |
253 bool DownloadDatabase::MigrateDownloadTabUrl() { | 266 bool DownloadDatabase::MigrateDownloadTabUrl() { |
254 return EnsureColumnExists("tab_url", "VARCHAR NOT NULL DEFAULT ''") && | 267 return EnsureColumnExists("tab_url", "VARCHAR NOT NULL DEFAULT ''") && |
255 EnsureColumnExists("tab_referrer_url", "VARCHAR NOT NULL DEFAULT ''"); | 268 EnsureColumnExists("tab_referrer_url", "VARCHAR NOT NULL DEFAULT ''"); |
256 } | 269 } |
257 | 270 |
258 bool DownloadDatabase::InitDownloadTable() { | 271 bool DownloadDatabase::InitDownloadTable() { |
259 const char kSchema[] = | 272 const char kSchema[] = |
260 "CREATE TABLE downloads (" | 273 "CREATE TABLE downloads (" |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 size_t DownloadDatabase::CountDownloads() { | 673 size_t DownloadDatabase::CountDownloads() { |
661 EnsureInProgressEntriesCleanedUp(); | 674 EnsureInProgressEntriesCleanedUp(); |
662 | 675 |
663 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 676 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
664 "SELECT count(*) from downloads")); | 677 "SELECT count(*) from downloads")); |
665 statement.Step(); | 678 statement.Step(); |
666 return statement.ColumnInt(0); | 679 return statement.ColumnInt(0); |
667 } | 680 } |
668 | 681 |
669 } // namespace history | 682 } // namespace history |
OLD | NEW |