This source file includes following definitions.
- thumbnail_db_
- Update
- Delete
- Insert
- DeleteUnusedFavicon
#include "chrome/browser/history/android/favicon_sql_handler.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "chrome/browser/history/thumbnail_database.h"
using base::Time;
namespace history {
namespace {
const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = {
HistoryAndBookmarkRow::FAVICON};
}
FaviconSQLHandler::FaviconSQLHandler(ThumbnailDatabase* thumbnail_db)
: SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)),
thumbnail_db_(thumbnail_db) {
}
FaviconSQLHandler::~FaviconSQLHandler() {
}
bool FaviconSQLHandler::Update(const HistoryAndBookmarkRow& row,
const TableIDRows& ids_set) {
if (!row.favicon_valid())
return Delete(ids_set);
chrome::FaviconID favicon_id = thumbnail_db_->AddFavicon(
GURL(),
chrome::FAVICON,
row.favicon(),
Time::Now(),
gfx::Size());
if (!favicon_id)
return false;
std::vector<chrome::FaviconID> favicon_ids;
for (TableIDRows::const_iterator i = ids_set.begin();
i != ids_set.end(); ++i) {
std::vector<IconMapping> icon_mappings;
thumbnail_db_->GetIconMappingsForPageURL(
i->url, chrome::FAVICON, &icon_mappings);
for (std::vector<IconMapping>::const_iterator m = icon_mappings.begin();
m != icon_mappings.end(); ++m) {
if (!thumbnail_db_->DeleteIconMapping(m->mapping_id))
return false;
favicon_ids.push_back(m->icon_id);
}
if (!thumbnail_db_->AddIconMapping(i->url, favicon_id))
return false;
}
if (!favicon_ids.empty() && !DeleteUnusedFavicon(favicon_ids))
return false;
return true;
}
bool FaviconSQLHandler::Delete(const TableIDRows& ids_set) {
std::vector<chrome::FaviconID> favicon_ids;
for (TableIDRows::const_iterator i = ids_set.begin();
i != ids_set.end(); ++i) {
std::vector<IconMapping> icon_mappings;
thumbnail_db_->GetIconMappingsForPageURL(i->url, &icon_mappings);
for (std::vector<IconMapping>::const_iterator m = icon_mappings.begin();
m != icon_mappings.end(); ++m) {
favicon_ids.push_back(m->icon_id);
}
if (!thumbnail_db_->DeleteIconMappings(i->url))
return false;
}
if (favicon_ids.empty())
return true;
if (!DeleteUnusedFavicon(favicon_ids))
return false;
return true;
}
bool FaviconSQLHandler::Insert(HistoryAndBookmarkRow* row) {
if (!row->is_value_set_explicitly(HistoryAndBookmarkRow::FAVICON) ||
!row->favicon_valid())
return true;
DCHECK(row->is_value_set_explicitly(HistoryAndBookmarkRow::URL));
chrome::FaviconID id = thumbnail_db_->AddFavicon(
GURL(),
chrome::FAVICON,
row->favicon(),
Time::Now(),
gfx::Size());
if (!id)
return false;
return thumbnail_db_->AddIconMapping(row->url(), id);
}
bool FaviconSQLHandler::DeleteUnusedFavicon(
const std::vector<chrome::FaviconID>& ids) {
for (std::vector<chrome::FaviconID>::const_iterator i = ids.begin();
i != ids.end();
++i) {
if (!thumbnail_db_->HasMappingFor(*i) && !thumbnail_db_->DeleteFavicon(*i))
return false;
}
return true;
}
}