| 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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 FaviconSQLHandler::FaviconSQLHandler(ThumbnailDatabase* thumbnail_db) | 24 FaviconSQLHandler::FaviconSQLHandler(ThumbnailDatabase* thumbnail_db) |
| 25 : SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)), | 25 : SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)), |
| 26 thumbnail_db_(thumbnail_db) { | 26 thumbnail_db_(thumbnail_db) { |
| 27 } | 27 } |
| 28 | 28 |
| 29 FaviconSQLHandler::~FaviconSQLHandler() { | 29 FaviconSQLHandler::~FaviconSQLHandler() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool FaviconSQLHandler::Update(const HistoryAndBookmarkRow& row, | 32 bool FaviconSQLHandler::Update(const HistoryAndBookmarkRow& row, |
| 33 const TableIDRows& ids_set) { | 33 const TableIDRows& ids_set) { |
| 34 FaviconID favicon_id = 0; | 34 if (!row.favicon_valid()) |
| 35 if (row.favicon_valid()) { | 35 return Delete(ids_set); |
| 36 // 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. | |
| 38 // TODO(pkotwicz): Pass in real pixel size. | |
| 39 favicon_id = thumbnail_db_->AddFavicon( | |
| 40 GURL(), | |
| 41 history::FAVICON, | |
| 42 history::GetDefaultFaviconSizes(), | |
| 43 row.favicon(), | |
| 44 Time::Now(), | |
| 45 gfx::Size()); | |
| 46 | 36 |
| 47 if (!favicon_id) | 37 // If the image_data will be updated, it is not reasonable to find if the |
| 48 return false; | 38 // icon is already in database, just create a new favicon. |
| 49 } | 39 // TODO(pkotwicz): Pass in real pixel size. |
| 40 FaviconID favicon_id = thumbnail_db_->AddFavicon( |
| 41 GURL(), |
| 42 history::FAVICON, |
| 43 history::GetDefaultFaviconSizes(), |
| 44 row.favicon(), |
| 45 Time::Now(), |
| 46 gfx::Size()); |
| 47 |
| 48 if (!favicon_id) |
| 49 return false; |
| 50 | 50 |
| 51 std::vector<FaviconID> favicon_ids; | 51 std::vector<FaviconID> favicon_ids; |
| 52 for (TableIDRows::const_iterator i = ids_set.begin(); | 52 for (TableIDRows::const_iterator i = ids_set.begin(); |
| 53 i != ids_set.end(); ++i) { | 53 i != ids_set.end(); ++i) { |
| 54 // Remove all icon mappings to favicons of type FAVICON. |
| 54 std::vector<IconMapping> icon_mappings; | 55 std::vector<IconMapping> icon_mappings; |
| 55 if (thumbnail_db_->GetIconMappingsForPageURL(i->url, FAVICON, | 56 thumbnail_db_->GetIconMappingsForPageURL(i->url, FAVICON, &icon_mappings); |
| 56 &icon_mappings)) { | 57 for (std::vector<IconMapping>::const_iterator m = icon_mappings.begin(); |
| 57 if (favicon_id) { | 58 m != icon_mappings.end(); ++m) { |
| 58 if (!thumbnail_db_->UpdateIconMapping(icon_mappings[0].mapping_id, | 59 if (!thumbnail_db_->DeleteIconMapping(m->mapping_id)) |
| 59 favicon_id)) | 60 return false; |
| 60 return false; | 61 |
| 61 } else { | |
| 62 // Require to delete the icon mapping. | |
| 63 if (!thumbnail_db_->DeleteIconMappings(i->url)) | |
| 64 return false; | |
| 65 } | |
| 66 // Keep the old icon for deleting it later if possible. | 62 // Keep the old icon for deleting it later if possible. |
| 67 favicon_ids.push_back(icon_mappings[0].icon_id); | 63 favicon_ids.push_back(m->icon_id); |
| 68 } else if (favicon_id) { | |
| 69 // The URL doesn't have icon before, add the icon mapping. | |
| 70 if (!thumbnail_db_->AddIconMapping(i->url, favicon_id)) | |
| 71 return false; | |
| 72 } | 64 } |
| 65 // Add the icon mapping. |
| 66 if (!thumbnail_db_->AddIconMapping(i->url, favicon_id)) |
| 67 return false; |
| 73 } | 68 } |
| 74 // As we update the favicon, Let's remove unused favicons if any. | 69 // As we update the favicon, Let's remove unused favicons if any. |
| 75 if (!favicon_ids.empty() && !DeleteUnusedFavicon(favicon_ids)) | 70 if (!favicon_ids.empty() && !DeleteUnusedFavicon(favicon_ids)) |
| 76 return false; | 71 return false; |
| 77 | 72 |
| 78 return true; | 73 return true; |
| 79 } | 74 } |
| 80 | 75 |
| 81 bool FaviconSQLHandler::Delete(const TableIDRows& ids_set) { | 76 bool FaviconSQLHandler::Delete(const TableIDRows& ids_set) { |
| 82 std::vector<FaviconID> favicon_ids; | 77 std::vector<FaviconID> favicon_ids; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 bool FaviconSQLHandler::DeleteUnusedFavicon(const std::vector<FaviconID>& ids) { | 121 bool FaviconSQLHandler::DeleteUnusedFavicon(const std::vector<FaviconID>& ids) { |
| 127 for (std::vector<FaviconID>::const_iterator i = ids.begin(); i != ids.end(); | 122 for (std::vector<FaviconID>::const_iterator i = ids.begin(); i != ids.end(); |
| 128 ++i) { | 123 ++i) { |
| 129 if (!thumbnail_db_->HasMappingFor(*i) && !thumbnail_db_->DeleteFavicon(*i)) | 124 if (!thumbnail_db_->HasMappingFor(*i) && !thumbnail_db_->DeleteFavicon(*i)) |
| 130 return false; | 125 return false; |
| 131 } | 126 } |
| 132 return true; | 127 return true; |
| 133 } | 128 } |
| 134 | 129 |
| 135 } // namespace history. | 130 } // namespace history. |
| OLD | NEW |