Chromium Code Reviews| 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/android/favicon_sql_handler.h" | 5 #include "chrome/browser/history/android/favicon_sql_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "chrome/browser/history/thumbnail_database.h" | 10 #include "chrome/browser/history/thumbnail_database.h" |
| 11 #include "ui/gfx/favicon_size.h" | |
| 11 | 12 |
| 12 using base::Time; | 13 using base::Time; |
| 13 | 14 |
| 14 namespace history { | 15 namespace history { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // The interesting columns of this handler. | 19 // The interesting columns of this handler. |
| 19 const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = { | 20 const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = { |
| 20 HistoryAndBookmarkRow::FAVICON}; | 21 HistoryAndBookmarkRow::FAVICON}; |
| 21 | 22 |
| 23 // This size is used by FaviconSQLHandler as the favicon's "requested size". | |
| 24 // See the comment in History::SetFavicon for a definition of "requested size". | |
| 25 const gfx::Size kDefaultFaviconSize = | |
|
stevenjb
2012/07/30 23:58:16
We should define this with gfx::kFaviconSize, i.e.
| |
| 26 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); | |
| 27 | |
| 22 } // namespace | 28 } // namespace |
| 23 | 29 |
| 24 FaviconSQLHandler::FaviconSQLHandler(ThumbnailDatabase* thumbnail_db) | 30 FaviconSQLHandler::FaviconSQLHandler(ThumbnailDatabase* thumbnail_db) |
| 25 : SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)), | 31 : SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)), |
| 26 thumbnail_db_(thumbnail_db) { | 32 thumbnail_db_(thumbnail_db) { |
| 27 } | 33 } |
| 28 | 34 |
| 29 FaviconSQLHandler::~FaviconSQLHandler() { | 35 FaviconSQLHandler::~FaviconSQLHandler() { |
| 30 } | 36 } |
| 31 | 37 |
| 32 bool FaviconSQLHandler::Update(const HistoryAndBookmarkRow& row, | 38 bool FaviconSQLHandler::Update(const HistoryAndBookmarkRow& row, |
| 33 const TableIDRows& ids_set) { | 39 const TableIDRows& ids_set) { |
| 34 FaviconID favicon_id = 0; | 40 FaviconID favicon_id = 0; |
| 35 if (!row.favicon().empty()) { | 41 if (!row.favicon().empty()) { |
| 36 // If the image_data will be updated, it is not reasonable to find if the | 42 // If the image_data will be updated, it is not reasonable to find if the |
| 37 // icon is already in database, just create a new favicon. | 43 // icon is already in database, just create a new favicon. |
| 38 favicon_id = thumbnail_db_->AddFavicon(GURL(), history::FAVICON); | 44 favicon_id = thumbnail_db_->AddFavicon(GURL(), kDefaultFaviconSize, |
| 45 history::FAVICON); | |
| 39 if (!favicon_id) | 46 if (!favicon_id) |
| 40 return false; | 47 return false; |
| 41 | 48 |
| 42 scoped_refptr<base::RefCountedMemory> image_data = | 49 scoped_refptr<base::RefCountedMemory> image_data = |
| 43 new base::RefCountedBytes(row.favicon()); | 50 new base::RefCountedBytes(row.favicon()); |
| 44 if (!thumbnail_db_->SetFavicon(favicon_id, image_data, Time::Now())) | 51 if (!thumbnail_db_->SetFavicon(favicon_id, image_data, Time::Now())) |
| 45 return false; | 52 return false; |
| 46 } | 53 } |
| 47 | 54 |
| 48 std::vector<FaviconID> favicon_ids; | 55 std::vector<FaviconID> favicon_ids; |
| 49 for (TableIDRows::const_iterator i = ids_set.begin(); | 56 for (TableIDRows::const_iterator i = ids_set.begin(); |
| 50 i != ids_set.end(); ++i) { | 57 i != ids_set.end(); ++i) { |
| 51 IconMapping icon_mapping; | 58 std::vector<IconMapping> icon_mappings; |
| 52 if (thumbnail_db_->GetIconMappingForPageURL(i->url, FAVICON, | 59 if (thumbnail_db_->GetIconMappingsForPageURL(i->url, FAVICON, |
| 53 &icon_mapping)) { | 60 &icon_mappings)) { |
| 54 if (favicon_id) { | 61 for (std::vector<IconMapping>::const_iterator m = icon_mappings.begin(); |
| 55 if (!thumbnail_db_->UpdateIconMapping(icon_mapping.mapping_id, | 62 m != icon_mappings.end(); ++m) { |
| 56 favicon_id)) | 63 if (favicon_id) { |
| 57 return false; | 64 if (!thumbnail_db_->UpdateIconMapping(m->mapping_id, favicon_id)) |
| 58 } else { | 65 return false; |
| 59 // Require to delete the icon mapping. | 66 } else { |
| 60 if (!thumbnail_db_->DeleteIconMappings(i->url)) | 67 // Require to delete the icon mapping. |
| 61 return false; | 68 if (!thumbnail_db_->DeleteIconMappings(i->url)) |
| 69 return false; | |
| 70 } | |
| 71 // Keep the old icon for deleting it later if possible. | |
| 72 favicon_ids.push_back(m->icon_id); | |
| 62 } | 73 } |
| 63 // Keep the old icon for deleting it later if possible. | |
| 64 favicon_ids.push_back(icon_mapping.icon_id); | |
| 65 } else if (favicon_id) { | 74 } else if (favicon_id) { |
| 66 // The URL doesn't have icon before, add the icon mapping. | 75 // The URL doesn't have icon before, add the icon mapping. |
| 67 if (!thumbnail_db_->AddIconMapping(i->url, favicon_id)) | 76 if (!thumbnail_db_->AddIconMapping(i->url, favicon_id)) |
| 68 return false; | 77 return false; |
| 69 } | 78 } |
| 70 } | 79 } |
| 71 // As we update the favicon, Let's remove unused favicons if any. | 80 // As we update the favicon, Let's remove unused favicons if any. |
| 72 if (!favicon_ids.empty() && !DeleteUnusedFavicon(favicon_ids)) | 81 if (!favicon_ids.empty() && !DeleteUnusedFavicon(favicon_ids)) |
| 73 return false; | 82 return false; |
| 74 | 83 |
| 75 return true; | 84 return true; |
| 76 } | 85 } |
| 77 | 86 |
| 78 bool FaviconSQLHandler::Delete(const TableIDRows& ids_set) { | 87 bool FaviconSQLHandler::Delete(const TableIDRows& ids_set) { |
| 79 std::vector<FaviconID> favicon_ids; | 88 std::vector<FaviconID> favicon_ids; |
| 80 for (TableIDRows::const_iterator i = ids_set.begin(); | 89 for (TableIDRows::const_iterator i = ids_set.begin(); |
| 81 i != ids_set.end(); ++i) { | 90 i != ids_set.end(); ++i) { |
| 82 // Since the URL was delete, we delete all type of icon mapping. | 91 // Since the URL was delete, we delete all types of icon mappings. |
|
stevenjb
2012/07/30 23:58:16
s/was delete/was deleted/
| |
| 83 IconMapping icon_mapping; | 92 std::vector<IconMapping> icon_mappings; |
| 84 if (thumbnail_db_->GetIconMappingForPageURL(i->url, FAVICON, | 93 if (thumbnail_db_->GetIconMappingsForPageURL(i->url, &icon_mappings)) { |
| 85 &icon_mapping)) | 94 for (std::vector<IconMapping>::const_iterator m = icon_mappings.begin(); |
| 86 favicon_ids.push_back(icon_mapping.icon_id); | 95 m != icon_mappings.end(); ++m) { |
| 87 if (thumbnail_db_->GetIconMappingForPageURL(i->url, TOUCH_ICON, | 96 favicon_ids.push_back(m->icon_id); |
| 88 &icon_mapping)) | 97 } |
| 89 favicon_ids.push_back(icon_mapping.icon_id); | 98 } |
| 90 if (thumbnail_db_->GetIconMappingForPageURL(i->url, | |
| 91 TOUCH_PRECOMPOSED_ICON, &icon_mapping)) | |
| 92 favicon_ids.push_back(icon_mapping.icon_id); | |
| 93 if (!thumbnail_db_->DeleteIconMappings(i->url)) | 99 if (!thumbnail_db_->DeleteIconMappings(i->url)) |
| 94 return false; | 100 return false; |
| 95 } | 101 } |
| 96 | 102 |
| 97 if (favicon_ids.empty()) | 103 if (favicon_ids.empty()) |
| 98 return true; | 104 return true; |
| 99 | 105 |
| 100 if (!DeleteUnusedFavicon(favicon_ids)) | 106 if (!DeleteUnusedFavicon(favicon_ids)) |
| 101 return false; | 107 return false; |
| 102 | 108 |
| 103 return true; | 109 return true; |
| 104 } | 110 } |
| 105 | 111 |
| 106 bool FaviconSQLHandler::Insert(HistoryAndBookmarkRow* row) { | 112 bool FaviconSQLHandler::Insert(HistoryAndBookmarkRow* row) { |
| 107 if (!row->is_value_set_explicitly(HistoryAndBookmarkRow::FAVICON) || | 113 if (!row->is_value_set_explicitly(HistoryAndBookmarkRow::FAVICON) || |
| 108 row->favicon().empty()) | 114 row->favicon().empty()) |
| 109 return true; | 115 return true; |
| 110 | 116 |
| 111 DCHECK(row->is_value_set_explicitly(HistoryAndBookmarkRow::URL)); | 117 DCHECK(row->is_value_set_explicitly(HistoryAndBookmarkRow::URL)); |
| 112 | 118 |
| 113 // Is it a problem to give a empty URL? | 119 // Is it a problem to give a empty URL? |
| 114 FaviconID id = thumbnail_db_->AddFavicon(GURL(), history::FAVICON); | 120 FaviconID id = thumbnail_db_->AddFavicon(GURL(), kDefaultFaviconSize, |
| 121 history::FAVICON); | |
| 115 if (!id) | 122 if (!id) |
| 116 return false; | 123 return false; |
| 117 | 124 |
| 118 scoped_refptr<base::RefCountedMemory> image_data = | 125 scoped_refptr<base::RefCountedMemory> image_data = |
| 119 new base::RefCountedBytes(row->favicon()); | 126 new base::RefCountedBytes(row->favicon()); |
| 120 if (!thumbnail_db_->SetFavicon(id, image_data, Time::Now())) | 127 if (!thumbnail_db_->SetFavicon(id, image_data, Time::Now())) |
| 121 return false; | 128 return false; |
| 122 return thumbnail_db_->AddIconMapping(row->url(), id); | 129 return thumbnail_db_->AddIconMapping(row->url(), id); |
| 123 } | 130 } |
| 124 | 131 |
| 125 bool FaviconSQLHandler::DeleteUnusedFavicon(const std::vector<FaviconID>& ids) { | 132 bool FaviconSQLHandler::DeleteUnusedFavicon(const std::vector<FaviconID>& ids) { |
| 126 for (std::vector<FaviconID>::const_iterator i = ids.begin(); i != ids.end(); | 133 for (std::vector<FaviconID>::const_iterator i = ids.begin(); i != ids.end(); |
| 127 ++i) { | 134 ++i) { |
| 128 if (!thumbnail_db_->HasMappingFor(*i) && !thumbnail_db_->DeleteFavicon(*i)) | 135 if (!thumbnail_db_->HasMappingFor(*i) && !thumbnail_db_->DeleteFavicon(*i)) |
| 129 return false; | 136 return false; |
| 130 } | 137 } |
| 131 return true; | 138 return true; |
| 132 } | 139 } |
| 133 | 140 |
| 134 } // namespace history. | 141 } // namespace history. |
| OLD | NEW |