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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// 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 "chrome/browser/thumbnail_store.h"
#include <string.h>
#include <algorithm>
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/gfx/jpeg_codec.h"
#include "base/md5.h"
#include "base/string_util.h"
#include "base/thread.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profile.h"
#include "chrome/common/pref_service.h"
#include "chrome/common/sqlite_utils.h"
#include "googleurl/src/gurl.h"
#include "third_party/skia/include/core/SkBitmap.h"
ThumbnailStore::ThumbnailStore()
: cache_(NULL),
db_(NULL),
hs_(NULL),
url_blacklist_(NULL) {
}
ThumbnailStore::~ThumbnailStore() {
CommitCacheToDB(NULL);
}
void ThumbnailStore::Init(const FilePath& db_name,
Profile* profile) {
// Load thumbnails already in the database.
g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
NewRunnableMethod(this, &ThumbnailStore::InitializeFromDB,
db_name, MessageLoop::current()));
// Take ownership of a reference to the HistoryService.
hs_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS);
// Store a pointer to a persistent table of blacklisted URLs.
url_blacklist_ = profile->GetPrefs()->
GetMutableDictionary(prefs::kNTPMostVisitedURLsBlacklist);
// Get the list of most visited URLs and redirect information from the
// HistoryService.
timer_.Start(base::TimeDelta::FromMinutes(30), this,
&ThumbnailStore::UpdateURLData);
UpdateURLData();
}
bool ThumbnailStore::SetPageThumbnail(const GURL& url,
const SkBitmap& thumbnail,
const ThumbnailScore& score) {
if (!cache_.get())
return false;
if (!ShouldStoreThumbnailForURL(url) ||
(cache_->find(url) != cache_->end() &&
!ShouldReplaceThumbnailWith((*cache_)[url].score_, score)))
return true;
base::TimeTicks encode_start = base::TimeTicks::Now();
// Encode the SkBitmap to jpeg.
scoped_refptr<RefCountedBytes> jpeg_data = new RefCountedBytes;
SkAutoLockPixels thumbnail_lock(thumbnail);
bool encoded = JPEGCodec::Encode(
reinterpret_cast<unsigned char*>(thumbnail.getAddr32(0, 0)),
JPEGCodec::FORMAT_BGRA, thumbnail.width(),
thumbnail.height(),
static_cast<int>(thumbnail.rowBytes()), 90,
&jpeg_data->data);
base::TimeDelta delta = base::TimeTicks::Now() - encode_start;
HISTOGRAM_TIMES("Thumbnail.Encode", delta);
if (!encoded)
return false;
// Update the cache_ with the new thumbnail.
(*cache_)[url] = CacheEntry(jpeg_data, score, true);
return true;
}
bool ThumbnailStore::GetPageThumbnail(
const GURL& url,
RefCountedBytes** data) {
if (!cache_.get() || IsURLBlacklisted(url))
return false;
// Look up the |url| in the redirect list to find the final destination
// which is the key into the |cache_|.
history::RedirectMap::iterator it = redirect_urls_->find(url);
if (it != redirect_urls_->end()) {
// Return the first available thumbnail starting at the end of the
// redirect list.
history::RedirectList::reverse_iterator rit;
for (rit = it->second->data.rbegin();
rit != it->second->data.rend(); ++rit) {
if (cache_->find(*rit) != cache_->end()) {
*data = (*cache_)[*rit].data_.get();
(*data)->AddRef();
return true;
}
}
}
// TODO(meelapshah) bug 14643: check past redirect lists
if (cache_->find(url) == cache_->end())
return false;
*data = (*cache_)[url].data_.get();
(*data)->AddRef();
return true;
}
void ThumbnailStore::UpdateURLData() {
int result_count = ThumbnailStore::kMaxCacheSize + url_blacklist_->GetSize();
hs_->QueryTopURLsAndRedirects(result_count, &consumer_,
NewCallback(this, &ThumbnailStore::OnURLDataAvailable));
}
void ThumbnailStore::OnURLDataAvailable(std::vector<GURL>* urls,
history::RedirectMap* redirects) {
DCHECK(urls);
DCHECK(redirects);
most_visited_urls_.reset(new std::vector<GURL>(*urls));
redirect_urls_.reset(new history::RedirectMap(*redirects));
CleanCacheData();
}
void ThumbnailStore::CleanCacheData() {
if (!cache_.get())
return;
// For each URL in the cache, search the RedirectMap for the originating URL.
// If this URL is blacklisted or not in the most visited list, delete the
// thumbnail data for it from the cache and from disk in the background.
scoped_refptr<RefCountedVector<GURL> > old_urls = new RefCountedVector<GURL>;
for (Cache::iterator cache_it = cache_->begin();
cache_it != cache_->end();) {
const GURL* url = NULL;
for (history::RedirectMap::iterator it = redirect_urls_->begin();
it != redirect_urls_->end(); ++it) {
if (cache_it->first == it->first ||
(it->second->data.size() &&
cache_it->first == it->second->data.back())) {
url = &it->first;
break;
}
}
if (url == NULL || IsURLBlacklisted(*url) || !IsPopular(*url)) {
old_urls->data.push_back(cache_it->first);
cache_->erase(cache_it++);
} else {
cache_it++;
}
}
if (old_urls->data.size()) {
g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
NewRunnableMethod(this, &ThumbnailStore::CommitCacheToDB, old_urls));
}
}
void ThumbnailStore::CommitCacheToDB(
scoped_refptr<RefCountedVector<GURL> > stale_urls) const {
if (!db_)
return;
int rv = sqlite3_exec(db_, "BEGIN TRANSACTION", NULL, NULL, NULL);
DCHECK(rv == SQLITE_OK) << "Failed to begin transaction";
// Delete old thumbnails.
if (stale_urls.get()) {
for (std::vector<GURL>::iterator it = stale_urls->data.begin();
it != stale_urls->data.end(); ++it) {
SQLITE_UNIQUE_STATEMENT(statement, *statement_cache_,
"DELETE FROM thumbnails WHERE url=?");
statement->bind_string(0, it->spec());
if (statement->step() != SQLITE_DONE)
NOTREACHED();
}
}
// Update cached thumbnails.
for (Cache::iterator it = cache_->begin(); it != cache_->end(); ++it) {
if (!it->second.dirty_)
continue;
SQLITE_UNIQUE_STATEMENT(statement, *statement_cache_,
"INSERT OR REPLACE INTO thumbnails "
"(url, boring_score, good_clipping, at_top, time_taken, data) "
"VALUES (?,?,?,?,?,?)");
statement->bind_string(0, it->first.spec());
statement->bind_double(1, it->second.score_.boring_score);
statement->bind_bool(2, it->second.score_.good_clipping);
statement->bind_bool(3, it->second.score_.at_top);
statement->bind_int64(4, it->second.score_.time_at_snapshot.
ToInternalValue());
statement->bind_blob(5, &it->second.data_->data[0],
static_cast<int>(it->second.data_->data.size()));
if (statement->step() != SQLITE_DONE)
DLOG(WARNING) << "Unable to insert thumbnail for URL";
else
it->second.dirty_ = false;
}
rv = sqlite3_exec(db_, "COMMIT", NULL, NULL, NULL);
DCHECK(rv == SQLITE_OK) << "Failed to commit transaction";
}
void ThumbnailStore::InitializeFromDB(const FilePath& db_name,
MessageLoop* cb_loop) {
if (OpenSqliteDb(db_name, &db_) != SQLITE_OK)
return;
// Use a large page size since the thumbnails we are storing are typically
// large, a small cache size since we cache in memory and don't go to disk
// often, and take exclusive access since nobody else uses this db.
sqlite3_exec(db_, "PRAGMA page_size=4096 "
"PRAGMA cache_size=64 "
"PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, NULL);
statement_cache_ = new SqliteStatementCache;
// Use local DBCloseScoper so that if we cannot create the table and
// need to return, the |db_| and |statement_cache_| are closed properly.
history::DBCloseScoper scoper(&db_, &statement_cache_);
if (!DoesSqliteTableExist(db_, "thumbnails")) {
if (sqlite3_exec(db_, "CREATE TABLE thumbnails ("
"url LONGVARCHAR PRIMARY KEY,"
"boring_score DOUBLE DEFAULT 1.0,"
"good_clipping INTEGER DEFAULT 0,"
"at_top INTEGER DEFAULT 0,"
"time_taken INTEGER DEFAULT 0,"
"data BLOB)", NULL, NULL, NULL) != SQLITE_OK)
return;
}
statement_cache_->set_db(db_);
// Now we can use a DBCloseScoper at the object scope.
scoper.Detach();
close_scoper_.Attach(&db_, &statement_cache_);
if (cb_loop)
GetAllThumbnailsFromDisk(cb_loop);
}
void ThumbnailStore::GetAllThumbnailsFromDisk(MessageLoop* cb_loop) {
ThumbnailStore::Cache* cache = new ThumbnailStore::Cache;
SQLITE_UNIQUE_STATEMENT(statement, *statement_cache_,
"SELECT * FROM thumbnails");
while (statement->step() == SQLITE_ROW) {
GURL url(statement->column_string(0));
ThumbnailScore score(statement->column_double(1), // Boring score
statement->column_bool(2), // Good clipping
statement->column_bool(3), // At top
base::Time::FromInternalValue(
statement->column_int64(4))); // Time taken
scoped_refptr<RefCountedBytes> data = new RefCountedBytes;
if (statement->column_blob_as_vector(5, &data->data))
(*cache)[url] = CacheEntry(data, score, false);
}
cb_loop->PostTask(FROM_HERE,
NewRunnableMethod(this, &ThumbnailStore::OnDiskDataAvailable, cache));
}
void ThumbnailStore::OnDiskDataAvailable(ThumbnailStore::Cache* cache) {
if (cache)
cache_.reset(cache);
}
bool ThumbnailStore::ShouldStoreThumbnailForURL(const GURL& url) const {
if (IsURLBlacklisted(url) || cache_->size() >= ThumbnailStore::kMaxCacheSize)
return false;
return most_visited_urls_->size() < ThumbnailStore::kMaxCacheSize ||
IsPopular(url);
}
bool ThumbnailStore::IsURLBlacklisted(const GURL& url) const {
if (url_blacklist_)
return url_blacklist_->HasKey(GetDictionaryKeyForURL(url.spec()));
return false;
}
std::wstring ThumbnailStore::GetDictionaryKeyForURL(
const std::string& url) const {
return ASCIIToWide(MD5String(url));
}
bool ThumbnailStore::IsPopular(const GURL& url) const {
return most_visited_urls_->end() != find(most_visited_urls_->begin(),
most_visited_urls_->end(),
url);
}
|