summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history/android/favicon_sql_handler.cc
blob: b52aef421d29f906d1d53f17511928bf7506d07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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 {

// The interesting columns of this handler.
const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = {
  HistoryAndBookmarkRow::FAVICON};

} // namespace

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) {
  FaviconID favicon_id = 0;
  if (!row.favicon().empty()) {
    // If the image_data will be updated, it is not reasonable to find if the
    // icon is already in database, just create a new favicon.
    favicon_id = thumbnail_db_->AddFavicon(GURL(), history::FAVICON);
    if (!favicon_id)
      return false;

    scoped_refptr<base::RefCountedMemory> image_data =
        new base::RefCountedBytes(row.favicon());
    if (!thumbnail_db_->SetFavicon(favicon_id, image_data, Time::Now()))
      return false;
  }

  std::vector<FaviconID> favicon_ids;
  for (TableIDRows::const_iterator i = ids_set.begin();
       i != ids_set.end(); ++i) {
    IconMapping icon_mapping;
    if (thumbnail_db_->GetIconMappingForPageURL(i->url, FAVICON,
                                                &icon_mapping)) {
      if (favicon_id) {
        if (!thumbnail_db_->UpdateIconMapping(icon_mapping.mapping_id,
                                              favicon_id))
          return false;
      } else {
        // Require to delete the icon mapping.
        if (!thumbnail_db_->DeleteIconMappings(i->url))
          return false;
      }
      // Keep the old icon for deleting it later if possible.
      favicon_ids.push_back(icon_mapping.icon_id);
    } else if (favicon_id) {
      // The URL doesn't have icon before, add the icon mapping.
      if (!thumbnail_db_->AddIconMapping(i->url, favicon_id))
        return false;
    }
  }
  // As we update the favicon, Let's remove unused favicons if any.
  if (!favicon_ids.empty() && !DeleteUnusedFavicon(favicon_ids))
    return false;

  return true;
}

bool FaviconSQLHandler::Delete(const TableIDRows& ids_set) {
  std::vector<FaviconID> favicon_ids;
  for (TableIDRows::const_iterator i = ids_set.begin();
       i != ids_set.end(); ++i) {
    // Since the URL was delete, we delete all type of icon mapping.
    IconMapping icon_mapping;
    if (thumbnail_db_->GetIconMappingForPageURL(i->url, FAVICON,
                                                &icon_mapping))
      favicon_ids.push_back(icon_mapping.icon_id);
    if (thumbnail_db_->GetIconMappingForPageURL(i->url, TOUCH_ICON,
                                                &icon_mapping))
      favicon_ids.push_back(icon_mapping.icon_id);
    if (thumbnail_db_->GetIconMappingForPageURL(i->url,
            TOUCH_PRECOMPOSED_ICON, &icon_mapping))
      favicon_ids.push_back(icon_mapping.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().empty())
    return true;

  DCHECK(row->is_value_set_explicitly(HistoryAndBookmarkRow::URL));

  // Is it a problem to give a empty URL?
  FaviconID id = thumbnail_db_->AddFavicon(GURL(), history::FAVICON);
  if (!id)
    return false;

  scoped_refptr<base::RefCountedMemory> image_data =
      new base::RefCountedBytes(row->favicon());
  if (!thumbnail_db_->SetFavicon(id, image_data, Time::Now()))
    return false;
  return thumbnail_db_->AddIconMapping(row->url(), id);
}

bool FaviconSQLHandler::DeleteUnusedFavicon(const std::vector<FaviconID>& ids) {
  for (std::vector<FaviconID>::const_iterator i = ids.begin(); i != ids.end();
       ++i) {
    if (!thumbnail_db_->HasMappingFor(*i) && !thumbnail_db_->DeleteFavicon(*i))
      return false;
  }
  return true;
}

}  // namespace history.