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/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/favicon_size.h" | 23 #include "ui/gfx/favicon_size.h" |
24 #include "ui/gfx/image/image_util.h" | 24 #include "ui/gfx/image/image_util.h" |
| 25 #include "ui/gfx/size.h" |
25 | 26 |
26 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
27 #include "base/mac/mac_util.h" | 28 #include "base/mac/mac_util.h" |
28 #endif | 29 #endif |
29 | 30 |
30 static void FillIconMapping(const sql::Statement& statement, | 31 static void FillIconMapping(const sql::Statement& statement, |
31 const GURL& page_url, | 32 const GURL& page_url, |
32 history::IconMapping* icon_mapping) { | 33 history::IconMapping* icon_mapping) { |
33 icon_mapping->mapping_id = statement.ColumnInt64(0); | 34 icon_mapping->mapping_id = statement.ColumnInt64(0); |
34 icon_mapping->icon_id = statement.ColumnInt64(1); | 35 icon_mapping->icon_id = statement.ColumnInt64(1); |
35 icon_mapping->icon_type = | 36 icon_mapping->icon_type = |
36 static_cast<history::IconType>(statement.ColumnInt(2)); | 37 static_cast<history::IconType>(statement.ColumnInt(2)); |
| 38 icon_mapping->icon_pixel_size = |
| 39 gfx::Size(statement.ColumnInt(3), statement.ColumnInt(4)); |
37 icon_mapping->page_url = page_url; | 40 icon_mapping->page_url = page_url; |
38 } | 41 } |
39 | 42 |
40 namespace history { | 43 namespace history { |
41 | 44 |
42 // Version number of the database. | 45 // Version number of the database. |
43 static const int kCurrentVersionNumber = 6; | 46 static const int kCurrentVersionNumber = 6; |
44 static const int kCompatibleVersionNumber = 6; | 47 static const int kCompatibleVersionNumber = 6; |
45 | 48 |
46 // Use 90 quality (out of 100) which is pretty high, because we're very | 49 // Use 90 quality (out of 100) which is pretty high, because we're very |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id, | 399 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id, |
397 base::Time time) { | 400 base::Time time) { |
398 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 401 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
399 "UPDATE favicons SET last_updated=? WHERE id=?")); | 402 "UPDATE favicons SET last_updated=? WHERE id=?")); |
400 statement.BindInt64(0, time.ToTimeT()); | 403 statement.BindInt64(0, time.ToTimeT()); |
401 statement.BindInt64(1, icon_id); | 404 statement.BindInt64(1, icon_id); |
402 | 405 |
403 return statement.Run(); | 406 return statement.Run(); |
404 } | 407 } |
405 | 408 |
406 FaviconID ThumbnailDatabase::GetFaviconIDForFaviconURL(const GURL& icon_url, | 409 void ThumbnailDatabase::GetFaviconIDsForFaviconURL( |
407 int required_icon_type, | 410 const GURL& icon_url, |
408 IconType* icon_type) { | 411 int required_icon_type, |
| 412 std::vector<FaviconIDAndSize>* favicon_id_size_listing) { |
409 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 413 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
410 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) " | 414 "SELECT id, icon_type, width, height FROM favicons " |
411 "ORDER BY icon_type DESC")); | 415 "WHERE url=? AND (icon_type & ? > 0) ORDER BY icon_type DESC")); |
412 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); | 416 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); |
413 statement.BindInt(1, required_icon_type); | 417 statement.BindInt(1, required_icon_type); |
414 | 418 |
415 if (!statement.Step()) | 419 IconType chosen_icon_type = INVALID_ICON; |
416 return 0; // not cached | 420 while (statement.Step()) { |
| 421 IconType icon_type = static_cast<IconType>(statement.ColumnInt(1)); |
| 422 if (chosen_icon_type == INVALID_ICON) |
| 423 chosen_icon_type = icon_type; |
| 424 else if (icon_type != chosen_icon_type) |
| 425 break; |
417 | 426 |
418 if (icon_type) | 427 FaviconIDAndSize favicon_id_size; |
419 *icon_type = static_cast<IconType>(statement.ColumnInt(1)); | 428 favicon_id_size.icon_id = statement.ColumnInt64(0); |
420 return statement.ColumnInt64(0); | 429 favicon_id_size.icon_size = gfx::Size(statement.ColumnInt(2), |
| 430 statement.ColumnInt(3)); |
| 431 favicon_id_size_listing->push_back(favicon_id_size); |
| 432 } |
421 } | 433 } |
422 | 434 |
423 bool ThumbnailDatabase::GetFavicon( | 435 bool ThumbnailDatabase::GetFavicon( |
424 FaviconID icon_id, | 436 FaviconID icon_id, |
425 base::Time* last_updated, | 437 base::Time* last_updated, |
426 std::vector<unsigned char>* png_icon_data, | 438 std::vector<unsigned char>* png_icon_data, |
427 GURL* icon_url, | 439 GURL* icon_url, |
| 440 gfx::Size* pixel_size, |
428 IconType* icon_type) { | 441 IconType* icon_type) { |
429 DCHECK(icon_id); | 442 DCHECK(icon_id); |
430 | 443 |
431 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 444 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
432 "SELECT last_updated, image_data, url, icon_type " | 445 "SELECT last_updated, image_data, url, icon_type, width, height " |
433 "FROM favicons WHERE id=?")); | 446 "FROM favicons WHERE id=?")); |
434 statement.BindInt64(0, icon_id); | 447 statement.BindInt64(0, icon_id); |
435 | 448 |
436 if (!statement.Step()) | 449 if (!statement.Step()) |
437 return false; // No entry for the id. | 450 return false; // No entry for the id. |
438 | 451 |
439 if (last_updated) | 452 if (last_updated) |
440 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0)); | 453 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0)); |
441 if (statement.ColumnByteLength(1) > 0) | 454 if (statement.ColumnByteLength(1) > 0) |
442 statement.ColumnBlobAsVector(1, png_icon_data); | 455 statement.ColumnBlobAsVector(1, png_icon_data); |
443 if (icon_url) | 456 if (icon_url) |
444 *icon_url = GURL(statement.ColumnString(2)); | 457 *icon_url = GURL(statement.ColumnString(2)); |
445 if (icon_type) | 458 if (icon_type) |
446 *icon_type = static_cast<history::IconType>(statement.ColumnInt(3)); | 459 *icon_type = static_cast<history::IconType>(statement.ColumnInt(3)); |
| 460 if (pixel_size) |
| 461 *pixel_size = gfx::Size(statement.ColumnInt(4), statement.ColumnInt(5)); |
447 | 462 |
448 return true; | 463 return true; |
449 } | 464 } |
450 | 465 |
451 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url, | 466 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url, |
| 467 const gfx::Size& pixel_size, |
452 IconType icon_type) { | 468 IconType icon_type) { |
453 int default_size = icon_type == FAVICON ? gfx::kFaviconSize : 0; | |
454 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 469 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
455 "INSERT INTO favicons (url, icon_type, width, height) VALUES " | 470 "INSERT INTO favicons (url, icon_type, width, height) VALUES " |
456 "(?, ?, ?, ?)")); | 471 "(?, ?, ?, ?)")); |
457 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); | 472 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); |
458 statement.BindInt(1, icon_type); | 473 statement.BindInt(1, icon_type); |
459 statement.BindInt(2, default_size); | 474 statement.BindInt(2, pixel_size.width()); |
460 statement.BindInt(3, default_size); | 475 statement.BindInt(3, pixel_size.height()); |
461 | 476 |
462 if (!statement.Run()) | 477 if (!statement.Run()) |
463 return 0; | 478 return 0; |
464 return db_.GetLastInsertRowId(); | 479 return db_.GetLastInsertRowId(); |
465 } | 480 } |
466 | 481 |
467 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) { | 482 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) { |
468 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 483 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
469 "DELETE FROM favicons WHERE id = ?")); | 484 "DELETE FROM favicons WHERE id = ?")); |
470 statement.BindInt64(0, id); | 485 statement.BindInt64(0, id); |
471 | 486 |
472 return statement.Run(); | 487 return statement.Run(); |
473 } | 488 } |
474 | 489 |
475 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url, | 490 bool ThumbnailDatabase::GetIconMappingsForPageURL( |
476 IconType required_icon_type, | 491 const GURL& page_url, |
477 IconMapping* icon_mapping) { | 492 int required_icon_types, |
478 std::vector<IconMapping> icon_mappings; | 493 std::vector<IconMapping>* icon_mappings) { |
479 if (!GetIconMappingsForPageURL(page_url, &icon_mappings)) | 494 std::vector<IconMapping> unfiltered_icon_mappings; |
| 495 if (!GetIconMappingsForPageURL(page_url, &unfiltered_icon_mappings)) |
480 return false; | 496 return false; |
481 | 497 |
482 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); | 498 for (std::vector<IconMapping>::iterator m = unfiltered_icon_mappings.begin(); |
483 m != icon_mappings.end(); ++m) { | 499 m != unfiltered_icon_mappings.end(); ++m) { |
484 if (m->icon_type == required_icon_type) { | 500 if (m->icon_type & required_icon_types) { |
485 if (icon_mapping != NULL) | 501 if (icon_mappings == NULL) |
486 *icon_mapping = *m; | 502 return true; |
487 return true; | 503 |
| 504 icon_mappings->push_back(*m); |
488 } | 505 } |
489 } | 506 } |
490 | 507 |
491 return false; | 508 return (icon_mappings == NULL) ? false : !icon_mappings->empty(); |
492 } | 509 } |
493 | 510 |
494 bool ThumbnailDatabase::GetIconMappingsForPageURL( | 511 bool ThumbnailDatabase::GetIconMappingsForPageURL( |
495 const GURL& page_url, | 512 const GURL& page_url, |
496 std::vector<IconMapping>* mapping_data) { | 513 std::vector<IconMapping>* mapping_data) { |
497 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 514 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
498 "SELECT icon_mapping.id, icon_mapping.icon_id, favicons.icon_type " | 515 "SELECT icon_mapping.id, icon_mapping.icon_id, favicons.icon_type, " |
| 516 "favicons.width, favicons.height " |
499 "FROM icon_mapping " | 517 "FROM icon_mapping " |
500 "INNER JOIN favicons " | 518 "INNER JOIN favicons " |
501 "ON icon_mapping.icon_id = favicons.id " | 519 "ON icon_mapping.icon_id = favicons.id " |
502 "WHERE icon_mapping.page_url=? " | 520 "WHERE icon_mapping.page_url=? " |
503 "ORDER BY favicons.icon_type DESC")); | 521 "ORDER BY favicons.icon_type DESC")); |
504 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); | 522 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); |
505 | 523 |
506 bool result = false; | 524 bool result = false; |
507 while (statement.Step()) { | 525 while (statement.Step()) { |
508 result = true; | 526 result = true; |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 // have width & height of 16px. | 838 // have width & height of 16px. |
821 success &= db_.Execute( | 839 success &= db_.Execute( |
822 "UPDATE favicons SET width='16', height='16' WHERE icon_type='1'"); | 840 "UPDATE favicons SET width='16', height='16' WHERE icon_type='1'"); |
823 | 841 |
824 meta_table_.SetVersionNumber(6); | 842 meta_table_.SetVersionNumber(6); |
825 meta_table_.SetCompatibleVersionNumber(std::min(6, kCompatibleVersionNumber)); | 843 meta_table_.SetCompatibleVersionNumber(std::min(6, kCompatibleVersionNumber)); |
826 return success; | 844 return success; |
827 } | 845 } |
828 | 846 |
829 } // namespace history | 847 } // namespace history |
OLD | NEW |