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