Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: chrome/browser/history/thumbnail_database.cc

Issue 10815068: Changes favicon database to support storing bitmaps of different sizes for the same icon_url (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes as per Sky's suggestions on Aug 1 Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/thumbnail_database.h" 5 #include "chrome/browser/history/thumbnail_database.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
13 #include "base/string_util.h" 13 #include "base/string_util.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 "chrome/browser/diagnostics/sqlite_diagnostics.h" 16 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
17 #include "chrome/browser/history/history_publisher.h" 17 #include "chrome/browser/history/history_publisher.h"
18 #include "chrome/browser/history/top_sites.h" 18 #include "chrome/browser/history/top_sites.h"
19 #include "chrome/browser/history/url_database.h" 19 #include "chrome/browser/history/url_database.h"
20 #include "chrome/common/thumbnail_score.h" 20 #include "chrome/common/thumbnail_score.h"
21 #include "sql/statement.h" 21 #include "sql/statement.h"
22 #include "sql/transaction.h" 22 #include "sql/transaction.h"
23 #include "ui/gfx/image/image_util.h" 23 #include "ui/gfx/image/image_util.h"
24 24
25 #if defined(OS_MACOSX) 25 #if defined(OS_MACOSX)
26 #include "base/mac/mac_util.h" 26 #include "base/mac/mac_util.h"
27 #endif 27 #endif
28 28
29 static void FillIconMapping(const sql::Statement& statement, 29 static void FillIconMapping(const sql::Statement& statement,
sky 2012/08/02 19:50:31 Add a comment here detailing the tables that are c
30 const GURL& page_url, 30 const GURL& page_url,
31 history::IconMapping* icon_mapping) { 31 history::IconMapping* icon_mapping) {
32 icon_mapping->mapping_id = statement.ColumnInt64(0); 32 icon_mapping->mapping_id = statement.ColumnInt64(0);
33 icon_mapping->icon_id = statement.ColumnInt64(1); 33 icon_mapping->icon_id = statement.ColumnInt64(1);
34 icon_mapping->icon_type = 34 icon_mapping->icon_type =
35 static_cast<history::IconType>(statement.ColumnInt(2)); 35 static_cast<history::IconType>(statement.ColumnInt(2));
36 icon_mapping->page_url = page_url; 36 icon_mapping->page_url = page_url;
37 } 37 }
38 38
39 namespace history { 39 namespace history {
40 40
41 // Version number of the database. 41 // Version number of the database.
42 static const int kCurrentVersionNumber = 5; 42 static const int kCurrentVersionNumber = 6;
43 static const int kCompatibleVersionNumber = 5; 43 static const int kCompatibleVersionNumber = 6;
44 44
45 // Use 90 quality (out of 100) which is pretty high, because we're very 45 // Use 90 quality (out of 100) which is pretty high, because we're very
46 // sensitive to artifacts for these small sized, highly detailed images. 46 // sensitive to artifacts for these small sized, highly detailed images.
47 static const int kImageQuality = 90; 47 static const int kImageQuality = 90;
48 48
49 ThumbnailDatabase::IconMappingEnumerator::IconMappingEnumerator() { 49 ThumbnailDatabase::IconMappingEnumerator::IconMappingEnumerator() {
50 } 50 }
51 51
52 ThumbnailDatabase::IconMappingEnumerator::~IconMappingEnumerator() { 52 ThumbnailDatabase::IconMappingEnumerator::~IconMappingEnumerator() {
53 } 53 }
54 54
55 bool ThumbnailDatabase::IconMappingEnumerator::GetNextIconMapping( 55 bool ThumbnailDatabase::IconMappingEnumerator::GetNextIconMapping(
56 IconMapping* icon_mapping) { 56 IconMapping* icon_mapping) {
57 if (!statement_.Step()) 57 if (!statement_.Step())
58 return false; 58 return false;
59 FillIconMapping(statement_, GURL(statement_.ColumnString(3)), icon_mapping); 59 FillIconMapping(statement_, GURL(statement_.ColumnString(3)), icon_mapping);
60 return true; 60 return true;
61 } 61 }
62 62
63 ThumbnailDatabase::ThumbnailDatabase() 63 ThumbnailDatabase::ThumbnailDatabase()
64 : history_publisher_(NULL), 64 : history_publisher_(NULL),
65 use_top_sites_(false) { 65 use_top_sites_(false) {
66 } 66 }
67 67
68 sql::InitStatus ThumbnailDatabase::CantUpgradeToVersion(int cur_version) { 68 sql::InitStatus ThumbnailDatabase::CantUpgradeToVersion(int cur_version) {
69 LOG(WARNING) << "Unable to update to thumbnail database to version 4" << 69 LOG(WARNING) << "Unable to update to thumbnail database to version " <<
70 cur_version << "."; 70 cur_version << ".";
71 db_.Close(); 71 db_.Close();
72 return sql::INIT_FAILURE; 72 return sql::INIT_FAILURE;
73 } 73 }
74 74
75 ThumbnailDatabase::~ThumbnailDatabase() { 75 ThumbnailDatabase::~ThumbnailDatabase() {
76 // The DBCloseScoper will delete the DB and the cache. 76 // The DBCloseScoper will delete the DB and the cache.
77 } 77 }
78 78
79 sql::InitStatus ThumbnailDatabase::Init( 79 sql::InitStatus ThumbnailDatabase::Init(
(...skipping 11 matching lines...) Expand all
91 91
92 #if defined(OS_MACOSX) 92 #if defined(OS_MACOSX)
93 // Exclude the thumbnails file from backups. 93 // Exclude the thumbnails file from backups.
94 base::mac::SetFileBackupExclusion(db_name); 94 base::mac::SetFileBackupExclusion(db_name);
95 #endif 95 #endif
96 96
97 // Create the tables. 97 // Create the tables.
98 if (!meta_table_.Init(&db_, kCurrentVersionNumber, 98 if (!meta_table_.Init(&db_, kCurrentVersionNumber,
99 kCompatibleVersionNumber) || 99 kCompatibleVersionNumber) ||
100 !InitThumbnailTable() || 100 !InitThumbnailTable() ||
101 !InitFaviconFramesTable(&db_, false) ||
102 !InitFaviconFramesIndex() ||
101 !InitFaviconsTable(&db_, false) || 103 !InitFaviconsTable(&db_, false) ||
102 !InitFaviconsIndex() || 104 !InitFaviconsIndex() ||
103 !InitIconMappingTable(&db_, false) || 105 !InitIconMappingTable(&db_, false) ||
104 !InitIconMappingIndex()) { 106 !InitIconMappingIndex()) {
105 db_.Close(); 107 db_.Close();
106 return sql::INIT_FAILURE; 108 return sql::INIT_FAILURE;
107 } 109 }
108 110
109 // Version check. We should not encounter a database too old for us to handle 111 // Version check. We should not encounter a database too old for us to handle
110 // in the wild, so we try to continue in that case. 112 // in the wild, so we try to continue in that case.
111 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { 113 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
112 LOG(WARNING) << "Thumbnail database is too new."; 114 LOG(WARNING) << "Thumbnail database is too new.";
113 return sql::INIT_TOO_NEW; 115 return sql::INIT_TOO_NEW;
114 } 116 }
115 117
116 int cur_version = meta_table_.GetVersionNumber(); 118 int cur_version = meta_table_.GetVersionNumber();
117 if (cur_version == 2) { 119 if (cur_version == 2) {
118 ++cur_version; 120 ++cur_version;
119 if (!UpgradeToVersion3()) 121 if (!UpgradeToVersion3())
120 return CantUpgradeToVersion(cur_version); 122 return CantUpgradeToVersion(cur_version);
121 } 123 }
122 124
123 if (cur_version == 3) { 125 if (cur_version == 3) {
124 ++cur_version; 126 ++cur_version;
125 if (!UpgradeToVersion4() || !MigrateIconMappingData(url_db)) 127 if (!UpgradeToVersion4() || !MigrateIconMappingData(url_db))
126 return CantUpgradeToVersion(cur_version); 128 return CantUpgradeToVersion(cur_version);
127 } 129 }
128 130
129 if (cur_version == 4) { 131 if (cur_version == 4) {
132 ++cur_version;
130 if (!UpgradeToVersion5()) 133 if (!UpgradeToVersion5())
131 return CantUpgradeToVersion(cur_version); 134 return CantUpgradeToVersion(cur_version);
132 } 135 }
133 136
137 if (cur_version == 5) {
138 ++cur_version;
139 if (!UpgradeToVersion6())
140 return CantUpgradeToVersion(cur_version);
141 }
142
134 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) << 143 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) <<
135 "Thumbnail database version " << cur_version << " is too old to handle."; 144 "Thumbnail database version " << cur_version << " is too old to handle.";
136 145
137 // Initialization is complete. 146 // Initialization is complete.
138 if (!transaction.Commit()) { 147 if (!transaction.Commit()) {
139 db_.Close(); 148 db_.Close();
140 return sql::INIT_FAILURE; 149 return sql::INIT_FAILURE;
141 } 150 }
142 151
143 return sql::INIT_OK; 152 return sql::INIT_OK;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 // Note: if you update the schema, don't forget to update 222 // Note: if you update the schema, don't forget to update
214 // CopyToTemporaryFaviconTable as well. 223 // CopyToTemporaryFaviconTable as well.
215 const char* name = is_temporary ? "temp_favicons" : "favicons"; 224 const char* name = is_temporary ? "temp_favicons" : "favicons";
216 if (!db->DoesTableExist(name)) { 225 if (!db->DoesTableExist(name)) {
217 std::string sql; 226 std::string sql;
218 sql.append("CREATE TABLE "); 227 sql.append("CREATE TABLE ");
219 sql.append(name); 228 sql.append(name);
220 sql.append("(" 229 sql.append("("
221 "id INTEGER PRIMARY KEY," 230 "id INTEGER PRIMARY KEY,"
222 "url LONGVARCHAR NOT NULL," 231 "url LONGVARCHAR NOT NULL,"
223 "last_updated INTEGER DEFAULT 0,"
224 "image_data BLOB,"
225 // Set the default icon_type as FAVICON to be consistent with 232 // Set the default icon_type as FAVICON to be consistent with
226 // table upgrade in UpgradeToVersion4(). 233 // table upgrade in UpgradeToVersion4().
227 "icon_type INTEGER DEFAULT 1," 234 "icon_type INTEGER DEFAULT 1,"
228 "sizes LONGVARCHAR)"); 235 "sizes LONGVARCHAR)");
229 if (!db->Execute(sql.c_str())) 236 if (!db->Execute(sql.c_str()))
230 return false; 237 return false;
231 } 238 }
232 return true; 239 return true;
233 } 240 }
234 241
235 bool ThumbnailDatabase::InitFaviconsIndex() { 242 bool ThumbnailDatabase::InitFaviconsIndex() {
236 // Add an index on the url column. 243 // Add an index on the url column.
237 return 244 return
238 db_.Execute("CREATE INDEX IF NOT EXISTS favicons_url ON favicons(url)"); 245 db_.Execute("CREATE INDEX IF NOT EXISTS favicons_url ON favicons(url)");
239 } 246 }
240 247
248 bool ThumbnailDatabase::InitFaviconFramesTable(sql::Connection* db,
249 bool is_temporary) {
250 // Note: if you update the schema, don't forget to update
251 // CopyToTemporaryFaviconFramesTable as well.
252 const char* name = is_temporary ? "temp_favicon_frames" : "favicon_frames";
253 if (!db->DoesTableExist(name)) {
254 std::string sql;
255 sql.append("CREATE TABLE ");
256 sql.append(name);
257 sql.append("("
258 "id INTEGER PRIMARY KEY,"
259 "icon_id INTEGER,"
260 "last_updated INTEGER DEFAULT 0,"
261 "image_data BLOB,"
262 "width INTEGER DEFAULT 0,"
263 "height INTEGER DEFAULT 0)");
264 if (!db->Execute(sql.c_str()))
265 return false;
266 }
267 return true;
268 }
269
270 bool ThumbnailDatabase::InitFaviconFramesIndex() {
271 // Add an index on the icon_id column.
272 return db_.Execute("CREATE INDEX IF NOT EXISTS favicon_frames_icon_id ON "
273 "favicon_frames(icon_id)");
274 }
275
241 void ThumbnailDatabase::BeginTransaction() { 276 void ThumbnailDatabase::BeginTransaction() {
242 db_.BeginTransaction(); 277 db_.BeginTransaction();
243 } 278 }
244 279
245 void ThumbnailDatabase::CommitTransaction() { 280 void ThumbnailDatabase::CommitTransaction() {
246 db_.CommitTransaction(); 281 db_.CommitTransaction();
247 } 282 }
248 283
249 void ThumbnailDatabase::RollbackTransaction() { 284 void ThumbnailDatabase::RollbackTransaction() {
250 db_.RollbackTransaction(); 285 db_.RollbackTransaction();
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 double current_boring_score = select_statement.ColumnDouble(0); 393 double current_boring_score = select_statement.ColumnDouble(0);
359 bool current_clipping = select_statement.ColumnBool(1); 394 bool current_clipping = select_statement.ColumnBool(1);
360 bool current_at_top = select_statement.ColumnBool(2); 395 bool current_at_top = select_statement.ColumnBool(2);
361 base::Time last_updated = 396 base::Time last_updated =
362 base::Time::FromTimeT(select_statement.ColumnInt64(3)); 397 base::Time::FromTimeT(select_statement.ColumnInt64(3));
363 *score = ThumbnailScore(current_boring_score, current_clipping, 398 *score = ThumbnailScore(current_boring_score, current_clipping,
364 current_at_top, last_updated); 399 current_at_top, last_updated);
365 return true; 400 return true;
366 } 401 }
367 402
368 bool ThumbnailDatabase::SetFavicon( 403 bool ThumbnailDatabase::AddFaviconFrame(
369 URLID icon_id, 404 URLID icon_id,
370 scoped_refptr<base::RefCountedMemory> icon_data, 405 scoped_refptr<base::RefCountedMemory> icon_data,
371 base::Time time) { 406 base::Time time) {
372 DCHECK(icon_id); 407 DCHECK(icon_id);
373 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 408 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
374 "UPDATE favicons SET image_data=?, last_updated=? WHERE id=?")); 409 "INSERT INTO favicon_frames (icon_id, image_data, last_updated) VALUES "
410 "(?, ?, ?)"));
411 statement.BindInt64(0, icon_id);
375 if (icon_data->size()) { 412 if (icon_data->size()) {
376 statement.BindBlob(0, icon_data->front(), 413 statement.BindBlob(1, icon_data->front(),
377 static_cast<int>(icon_data->size())); 414 static_cast<int>(icon_data->size()));
378 } else { 415 } else {
379 statement.BindNull(0); 416 statement.BindNull(1);
380 } 417 }
381 statement.BindInt64(1, time.ToTimeT()); 418 statement.BindInt64(2, time.ToTimeT());
382 statement.BindInt64(2, icon_id);
383 419
384 return statement.Run(); 420 return statement.Run();
385 } 421 }
386 422
387 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id, 423 bool ThumbnailDatabase::SetFaviconOutOfDate(FaviconID icon_id) {
388 base::Time time) {
389 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 424 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
390 "UPDATE favicons SET last_updated=? WHERE id=?")); 425 "UPDATE favicon_frames SET last_updated=? WHERE icon_id=?"));
391 statement.BindInt64(0, time.ToTimeT()); 426 statement.BindInt64(0, 0);
392 statement.BindInt64(1, icon_id); 427 statement.BindInt64(1, icon_id);
393 428
394 return statement.Run(); 429 return statement.Run();
395 } 430 }
396 431
397 FaviconID ThumbnailDatabase::GetFaviconIDForFaviconURL(const GURL& icon_url, 432 FaviconID ThumbnailDatabase::GetFaviconIDForFaviconURL(const GURL& icon_url,
398 int required_icon_type, 433 int required_icon_type,
399 IconType* icon_type) { 434 IconType* icon_type) {
400 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 435 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
401 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) " 436 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) "
(...skipping 10 matching lines...) Expand all
412 } 447 }
413 448
414 bool ThumbnailDatabase::GetFavicon( 449 bool ThumbnailDatabase::GetFavicon(
415 FaviconID icon_id, 450 FaviconID icon_id,
416 base::Time* last_updated, 451 base::Time* last_updated,
417 std::vector<unsigned char>* png_icon_data, 452 std::vector<unsigned char>* png_icon_data,
418 GURL* icon_url, 453 GURL* icon_url,
419 IconType* icon_type) { 454 IconType* icon_type) {
420 DCHECK(icon_id); 455 DCHECK(icon_id);
421 456
457 if (!GetFaviconFrame(icon_id, last_updated, png_icon_data))
458 return false;
459
460 if (icon_url || icon_type) {
461 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
462 "SELECT url, icon_type FROM favicons WHERE id=?"));
463 statement.BindInt64(0, icon_id);
464
465 if (!statement.Step())
466 return false; // No entry for the id.
467
468 if (icon_url)
469 *icon_url = GURL(statement.ColumnString(0));
470 if (icon_type)
471 *icon_type = static_cast<history::IconType>(statement.ColumnInt(1));
472 }
473
474 return true;
475 }
476
477 bool ThumbnailDatabase::GetFaviconFrame(
478 FaviconID icon_id,
479 base::Time* last_updated,
480 std::vector<unsigned char>* png_icon_data) {
481 DCHECK(icon_id);
422 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 482 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
423 "SELECT last_updated, image_data, url, icon_type " 483 "SELECT last_updated, image_data FROM favicon_frames WHERE icon_id=?"));
424 "FROM favicons WHERE id=?"));
425 statement.BindInt64(0, icon_id); 484 statement.BindInt64(0, icon_id);
426 485
427 if (!statement.Step()) 486 if (!statement.Step())
428 return false; // No entry for the id. 487 return false; // No entry for the id.
429 488
430 if (last_updated) 489 if (last_updated)
431 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0)); 490 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0));
432 if (statement.ColumnByteLength(1) > 0) 491 if (statement.ColumnByteLength(1) > 0)
433 statement.ColumnBlobAsVector(1, png_icon_data); 492 statement.ColumnBlobAsVector(1, png_icon_data);
434 if (icon_url)
435 *icon_url = GURL(statement.ColumnString(2));
436 if (icon_type)
437 *icon_type = static_cast<history::IconType>(statement.ColumnInt(3));
438
439 return true; 493 return true;
440 } 494 }
441 495
442 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url, 496 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url,
443 IconType icon_type) { 497 IconType icon_type) {
444 498
445 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 499 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
446 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)")); 500 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)"));
447 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); 501 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url));
448 statement.BindInt(1, icon_type); 502 statement.BindInt(1, icon_type);
449 503
450 if (!statement.Run()) 504 if (!statement.Run())
451 return 0; 505 return 0;
452 return db_.GetLastInsertRowId(); 506 return db_.GetLastInsertRowId();
453 } 507 }
454 508
455 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) { 509 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) {
456 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 510 bool success = true;
511 sql::Statement statement;
512 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
457 "DELETE FROM favicons WHERE id = ?")); 513 "DELETE FROM favicons WHERE id = ?"));
458 statement.BindInt64(0, id); 514 statement.BindInt64(0, id);
515 success &= statement.Run();
sky 2012/08/02 19:50:31 If this fails, shouldn't this return?
459 516
460 return statement.Run(); 517 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
518 "DELETE FROM favicon_frames WHERE icon_id = ?"));
519 statement.BindInt64(0, id);
520 success &= statement.Run();
521
522 return success;
461 } 523 }
462 524
463 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url, 525 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url,
464 IconType required_icon_type, 526 IconType required_icon_type,
465 IconMapping* icon_mapping) { 527 IconMapping* icon_mapping) {
466 std::vector<IconMapping> icon_mappings; 528 std::vector<IconMapping> icon_mappings;
467 if (!GetIconMappingsForPageURL(page_url, &icon_mappings)) 529 if (!GetIconMappingsForPageURL(page_url, &icon_mappings))
468 return false; 530 return false;
469 531
470 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); 532 for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 // Rename the temporary one. 663 // Rename the temporary one.
602 if (!db_.Execute("ALTER TABLE temp_icon_mapping RENAME TO icon_mapping")) 664 if (!db_.Execute("ALTER TABLE temp_icon_mapping RENAME TO icon_mapping"))
603 return false; 665 return false;
604 666
605 // The renamed table needs the index (the temporary table doesn't have one). 667 // The renamed table needs the index (the temporary table doesn't have one).
606 return InitIconMappingIndex(); 668 return InitIconMappingIndex();
607 } 669 }
608 670
609 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) { 671 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) {
610 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 672 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
611 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)" 673 "INSERT INTO temp_favicons (url, icon_type, sizes) "
612 "SELECT url, last_updated, image_data, icon_type " 674 "SELECT url, icon_type, sizes FROM favicons WHERE id = ?"));
613 "FROM favicons WHERE id = ?"));
614 statement.BindInt64(0, source); 675 statement.BindInt64(0, source);
615 676
616 if (!statement.Run()) 677 if (!statement.Run())
617 return 0; 678 return 0;
618 679
619 // We return the ID of the newly inserted favicon. 680 // We return the ID of the newly inserted favicon.
620 return db_.GetLastInsertRowId(); 681 return db_.GetLastInsertRowId();
621 } 682 }
622 683
623 bool ThumbnailDatabase::CommitTemporaryFaviconTable() { 684 bool ThumbnailDatabase::CommitTemporaryFaviconTable() {
624 // Delete the old favicons table. 685 // Delete the old favicons table.
625 if (!db_.Execute("DROP TABLE favicons")) 686 if (!db_.Execute("DROP TABLE favicons"))
626 return false; 687 return false;
627 688
628 // Rename the temporary one. 689 // Rename the temporary one.
629 if (!db_.Execute("ALTER TABLE temp_favicons RENAME TO favicons")) 690 if (!db_.Execute("ALTER TABLE temp_favicons RENAME TO favicons"))
630 return false; 691 return false;
631 692
632 // The renamed table needs the index (the temporary table doesn't have one). 693 // The renamed table needs the index (the temporary table doesn't have one).
633 return InitFaviconsIndex(); 694 return InitFaviconsIndex();
634 } 695 }
635 696
697 void ThumbnailDatabase::CopyToTemporaryFaviconFramesTable(
698 FaviconID source) {
699 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
700 "INSERT INTO temp_favicon_frames (icon_id, last_updated, image_data, "
701 "width, height) "
702 "SELECT icon_id, last_updated, image_data, width, height "
sky 2012/08/02 19:50:31 This is inserting the old id when it needs to inse
703 "FROM favicon_frames WHERE id = ?"));
704 statement.BindInt64(0, source);
705 statement.Run();
706 }
707
708 bool ThumbnailDatabase::CommitTemporaryFaviconFrameTable() {
709 // Delete the old favicon frames table.
710 if (!db_.Execute("DROP TABLE favicon_frames"))
711 return false;
712
713 // Rename the temporary one.
714 if (!db_.Execute("ALTER TABLE temp_favicon_frames RENAME TO favicon_frames"))
715 return false;
716
717 // The renamed table needs an index (the temporary table doesn't have one).
718 return InitFaviconFramesIndex();
719 }
720
636 bool ThumbnailDatabase::NeedsMigrationToTopSites() { 721 bool ThumbnailDatabase::NeedsMigrationToTopSites() {
637 return !use_top_sites_; 722 return !use_top_sites_;
638 } 723 }
639 724
640 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file, 725 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file,
641 const FilePath& new_db_file) { 726 const FilePath& new_db_file) {
642 // Init favicons table - same schema as the thumbnails. 727 // Init favicons table - same schema as the thumbnails.
sky 2012/08/02 19:50:31 tables
643 sql::Connection favicons; 728 sql::Connection favicons;
644 if (OpenDatabase(&favicons, new_db_file) != sql::INIT_OK) 729 if (OpenDatabase(&favicons, new_db_file) != sql::INIT_OK)
645 return false; 730 return false;
646 731
647 if (!InitFaviconsTable(&favicons, false) || 732 if (!InitFaviconFramesTable(&favicons, false) ||
733 !InitFaviconsTable(&favicons, false) ||
648 !InitIconMappingTable(&favicons, false)) { 734 !InitIconMappingTable(&favicons, false)) {
649 favicons.Close(); 735 favicons.Close();
650 return false; 736 return false;
651 } 737 }
652 favicons.Close(); 738 favicons.Close();
653 739
654 // Can't attach within a transaction. 740 // Can't attach within a transaction.
655 if (transaction_nesting()) 741 if (transaction_nesting())
656 CommitTransaction(); 742 CommitTransaction();
657 743
(...skipping 13 matching lines...) Expand all
671 #else 757 #else
672 attach.BindString(0, WideToUTF8(new_db_file.value())); 758 attach.BindString(0, WideToUTF8(new_db_file.value()));
673 #endif 759 #endif
674 760
675 if (!attach.Run()) { 761 if (!attach.Run()) {
676 BeginTransaction(); 762 BeginTransaction();
677 return false; 763 return false;
678 } 764 }
679 } 765 }
680 766
681 // Move favicons to the new DB. 767 // Move favicons and frame_frames to new DB.
682 if (!db_.Execute("INSERT OR REPLACE INTO new_favicons.favicons " 768 bool successfully_moved_data =
683 "SELECT * FROM favicons")) { 769 db_.Execute("INSERT OR REPLACE INTO new_favicons.favicon_frames "
684 DLOG(FATAL) << "Unable to copy favicons."; 770 "SELECT * FROM favicon_frames") &&
771 db_.Execute("INSERT OR REPLACE INTO new_favicons.favicons "
772 "SELECT * FROM favicons");
773 if (!successfully_moved_data) {
774 DLOG(FATAL) << "Unable to copy favicons and frame_frames.";
685 BeginTransaction(); 775 BeginTransaction();
686 return false; 776 return false;
687 } 777 }
688 778
689 if (!db_.Execute("DETACH new_favicons")) { 779 if (!db_.Execute("DETACH new_favicons")) {
690 DLOG(FATAL) << "Unable to detach database."; 780 DLOG(FATAL) << "Unable to detach database.";
691 BeginTransaction(); 781 BeginTransaction();
692 return false; 782 return false;
693 } 783 }
694 784
695 db_.Close(); 785 db_.Close();
696 786
697 // Reset the DB to point to new file. 787 // Reset the DB to point to new file.
698 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK) 788 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK)
699 return false; 789 return false;
700 790
701 file_util::Delete(old_db_file, false); 791 file_util::Delete(old_db_file, false);
702 792
703 meta_table_.Reset(); 793 meta_table_.Reset();
704 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) 794 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber))
705 return false; 795 return false;
706 796
707 if (!InitFaviconsIndex()) 797 if (!InitFaviconFramesIndex() || !InitFaviconsIndex())
708 return false; 798 return false;
709 799
710 // Reopen the transaction. 800 // Reopen the transaction.
711 BeginTransaction(); 801 BeginTransaction();
712 use_top_sites_ = true; 802 use_top_sites_ = true;
713 return true; 803 return true;
714 } 804 }
715 805
716 bool ThumbnailDatabase::InitIconMappingTable(sql::Connection* db, 806 bool ThumbnailDatabase::InitIconMappingTable(sql::Connection* db,
717 bool is_temporary) { 807 bool is_temporary) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 869
780 bool ThumbnailDatabase::UpgradeToVersion5() { 870 bool ThumbnailDatabase::UpgradeToVersion5() {
781 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) { 871 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) {
782 return false; 872 return false;
783 } 873 }
784 meta_table_.SetVersionNumber(5); 874 meta_table_.SetVersionNumber(5);
785 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber)); 875 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber));
786 return true; 876 return true;
787 } 877 }
788 878
879 bool ThumbnailDatabase::UpgradeToVersion6() {
880 bool success =
881 db_.Execute("INSERT INTO favicon_frames (icon_id, last_updated, "
882 "image_data)"
883 "SELECT id, last_updated, image_data FROM favicons") &&
sky 2012/08/02 19:50:31 You need to update width/height here too.
884 db_.Execute("CREATE TABLE temp_favicons ("
885 "id INTEGER PRIMARY KEY,"
886 "url LONGVARCHAR NOT NULL,"
887 "icon_type INTEGER DEFAULT 1,"
888 "sizes LONGVARCHAR)") &&
889 db_.Execute("INSERT INTO temp_favicons (id, url, icon_type) "
890 "SELECT id, url, icon_type FROM favicons") &&
891 db_.Execute("DROP TABLE favicons") &&
892 db_.Execute("ALTER TABLE temp_favicons RENAME TO favicons");
893
894 meta_table_.SetVersionNumber(6);
895 meta_table_.SetCompatibleVersionNumber(std::min(6, kCompatibleVersionNumber));
896 return success;
897 }
898
789 } // namespace history 899 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698