summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history/top_sites_database.cc
blob: 5b13d6190b6d52d841bc8e4e1e067679d38962b9 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2009 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 "app/sql/transaction.h"
#include "base/string_util.h"
#include "chrome/browser/diagnostics/sqlite_diagnostics.h"
#include "chrome/browser/history/top_sites.h"
#include "chrome/browser/history/top_sites_database.h"

namespace history {

TopSitesDatabaseImpl::TopSitesDatabaseImpl() {
}

bool TopSitesDatabaseImpl::Init(const FilePath& db_name) {
  // Settings copied from ThumbnailDatabase.
  db_.set_error_delegate(GetErrorHandlerForThumbnailDb());
  db_.set_page_size(4096);
  db_.set_cache_size(64);

  if (!db_.Open(db_name)) {
    LOG(WARNING) << db_.GetErrorMessage();
    return sql::INIT_FAILURE;
  }

  return InitThumbnailTable();
}

bool TopSitesDatabaseImpl::InitThumbnailTable() {
  if (!db_.DoesTableExist("thumbnails")) {
    if (!db_.Execute("CREATE TABLE thumbnails ("
                     "url LONGVARCHAR PRIMARY KEY,"
                     "url_rank INTEGER ,"
                     "title LONGVARCHAR,"
                     "thumbnail BLOB,"
                     "redirects LONGVARCHAR)")) {
      LOG(WARNING) << db_.GetErrorMessage();
      return false;
    }
  }
  return true;
}

MostVisitedURLList TopSitesDatabaseImpl::GetTopURLs() {
  MostVisitedURLList result;
  sql::Statement select_statement(db_.GetCachedStatement(
      SQL_FROM_HERE,
      "SELECT url, url_rank, title, redirects "
      "FROM thumbnails ORDER BY url_rank"));

  if (!select_statement) {
    LOG(WARNING) << db_.GetErrorMessage();
    return result;
  }

  int row_count = 0;
  int max_rank = -1;  // -1 is for the case of empty DB.
  while (select_statement.Step()) {
    // Results are sorted by url_rank.
    MostVisitedURL url;
    GURL gurl(select_statement.ColumnString(0));
    url.url = gurl;
    url.title = select_statement.ColumnString16(2);
    std::string redirects = select_statement.ColumnString(3);
    SetRedirects(redirects, &url);
    result.push_back(url);
  }
  DCHECK(select_statement.Succeeded());
  DCHECK_EQ(max_rank + 1, row_count);

  return result;
}

// static
std::string TopSitesDatabaseImpl::GetRedirects(const MostVisitedURL& url) {
  std::vector<std::string> redirects;
  for (size_t i = 0; i < url.redirects.size(); i++)
    redirects.push_back(url.redirects[i].spec());
  return JoinString(redirects, ' ');
}

// static
void TopSitesDatabaseImpl::SetRedirects(const std::string& redirects,
                                        MostVisitedURL* url) {
  std::vector<std::string> redirects_vector;
  SplitStringAlongWhitespace(redirects, &redirects_vector);
  for (size_t i = 0; i < redirects_vector.size(); i++)
    url->redirects.push_back(GURL(redirects_vector[i]));
}


void TopSitesDatabaseImpl::SetPageThumbnail(const MostVisitedURL& url,
                                            int new_rank,
                                            const TopSites::Images& thumbnail) {
  sql::Transaction transaction(&db_);
  transaction.Begin();

  int prev_rank = GetURLRank(url);

  sql::Statement statement(db_.GetCachedStatement(
      SQL_FROM_HERE,
      "INSERT OR REPLACE INTO thumbnails "
      "(url, url_rank, title, thumbnail, redirects) "
      "VALUES (?, ?, ?, ?, ?)"));  // TODO(Nik): add the rest of the schema.
  if (!statement)
    return;

  statement.BindString(0, url.url.spec());
  statement.BindInt(1, -1);  // Temporary value
  statement.BindString16(2, url.title);
  if (thumbnail.thumbnail.get()) {
    statement.BindBlob(3, &thumbnail.thumbnail->data.front(),
                       static_cast<int>(thumbnail.thumbnail->data.size()));
  }
  statement.BindString(4, GetRedirects(url));

  if (!statement.Run())
    NOTREACHED() << db_.GetErrorMessage();

  // Shift the ranks.
  if (prev_rank == -1) {
    // Shift up
    sql::Statement shift_statement(db_.GetCachedStatement(
        SQL_FROM_HERE,
        "UPDATE thumbnails "
        "SET url_rank = url_rank + 1 WHERE url_rank >= ?"));
    shift_statement.BindInt(0, new_rank);
    if (shift_statement)
      shift_statement.Run();
  } else if (prev_rank > new_rank) {
    // Shift up
    sql::Statement shift_statement(db_.GetCachedStatement(
        SQL_FROM_HERE,
        "UPDATE thumbnails "
        "SET url_rank = url_rank + 1 "
        "WHERE url_rank >= ? AND url_rank < ?"));
    shift_statement.BindInt(0, new_rank);
    shift_statement.BindInt(1, prev_rank);
    if (shift_statement)
      shift_statement.Run();
  } else if (prev_rank < new_rank) {
    // Shift down
    sql::Statement shift_statement(db_.GetCachedStatement(
        SQL_FROM_HERE,
        "UPDATE thumbnails "
        "SET url_rank = url_rank - 1 "
        "WHERE url_rank > ? AND url_rank <= ?"));
    shift_statement.BindInt(0, prev_rank);
    shift_statement.BindInt(1, new_rank);
    if (shift_statement)
      shift_statement.Run();
  }

  // Set the real rank.
  sql::Statement set_statement(db_.GetCachedStatement(
      SQL_FROM_HERE,
      "UPDATE thumbnails "
      "SET url_rank = ? "
      "WHERE url_rank == -1"));
  set_statement.BindInt(0, new_rank);
  if (set_statement)
    set_statement.Run();
  transaction.Commit();
}

bool TopSitesDatabaseImpl::GetPageThumbnail(const MostVisitedURL& url,
                                            TopSites::Images* thumbnail) {
  sql::Statement select_statement(db_.GetCachedStatement(
      SQL_FROM_HERE,
      "SELECT thumbnail "
      "FROM thumbnails WHERE url=?"));

  if (!select_statement) {
    LOG(WARNING) << db_.GetErrorMessage();
    return false;
  }

  select_statement.BindString(0, url.url.spec());
  if (!select_statement.Step())
    return false;

  std::vector<unsigned char> data;
  select_statement.ColumnBlobAsVector(0, &data);
  thumbnail->thumbnail = RefCountedBytes::TakeVector(&data);
  return true;
}

int TopSitesDatabaseImpl::GetURLRank(const MostVisitedURL& url) {
  int result = -1;
  sql::Statement select_statement(db_.GetCachedStatement(
      SQL_FROM_HERE,
      "SELECT url_rank "
      "FROM thumbnails WHERE url=?"));
  if (!select_statement) {
    LOG(WARNING) << db_.GetErrorMessage();
    return result;
  }

  select_statement.BindString(0, url.url.spec());
  if (select_statement.Step())
    result = select_statement.ColumnInt(0);

  return result;
}

// Remove the record for this URL. Returns true iff removed successfully.
bool TopSitesDatabaseImpl::RemoveURL(const MostVisitedURL& url) {
  int old_rank = GetURLRank(url);
  sql::Transaction transaction(&db_);
  transaction.Begin();
  // Decrement all following ranks.
  sql::Statement shift_statement(db_.GetCachedStatement(
      SQL_FROM_HERE,
      "UPDATE thumbnails "
      "SET url_rank = url_rank - 1 "
      "WHERE url_rank > ?"));
  if (!shift_statement)
    return false;
  shift_statement.BindInt(0, old_rank);
  shift_statement.Run();

  sql::Statement delete_statement(
      db_.GetCachedStatement(SQL_FROM_HERE,
                             "DELETE FROM thumbnails WHERE url = ?"));
  if (!delete_statement)
    return false;
  delete_statement.BindString(0, url.url.spec());
  delete_statement.Run();

  return transaction.Commit();
}

}  // namespace history