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
|
// Copyright 2014 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 "components/search_provider_logos/logo_tracker.h"
#include <algorithm>
#include "base/message_loop/message_loop.h"
#include "base/task_runner_util.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/default_clock.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
namespace search_provider_logos {
namespace {
const int64 kMaxDownloadBytes = 1024 * 1024;
//const int kDecodeLogoTimeoutSeconds = 30;
// Returns whether the metadata for the cached logo indicates that the logo is
// OK to show, i.e. it's not expired or it's allowed to be shown temporarily
// after expiration.
bool IsLogoOkToShow(const LogoMetadata& metadata, base::Time now) {
base::TimeDelta offset =
base::TimeDelta::FromMilliseconds(kMaxTimeToLiveMS * 3 / 2);
base::Time distant_past = now - offset;
base::Time distant_future = now + offset;
// Sanity check so logos aren't accidentally cached forever.
if (metadata.expiration_time < distant_past ||
metadata.expiration_time > distant_future) {
return false;
}
return metadata.can_show_after_expiration || metadata.expiration_time >= now;
}
// Reads the logo from the cache and returns it. Returns NULL if the cache is
// empty, corrupt, expired, or doesn't apply to the current logo URL.
scoped_ptr<EncodedLogo> GetLogoFromCacheOnFileThread(LogoCache* logo_cache,
const GURL& logo_url,
base::Time now) {
const LogoMetadata* metadata = logo_cache->GetCachedLogoMetadata();
if (!metadata)
return scoped_ptr<EncodedLogo>();
if (metadata->source_url != logo_url.spec() ||
!IsLogoOkToShow(*metadata, now)) {
logo_cache->SetCachedLogo(NULL);
return scoped_ptr<EncodedLogo>();
}
return logo_cache->GetCachedLogo().Pass();
}
void DeleteLogoCacheOnFileThread(LogoCache* logo_cache) {
delete logo_cache;
}
} // namespace
LogoTracker::LogoTracker(
base::FilePath cached_logo_directory,
scoped_refptr<base::SequencedTaskRunner> file_task_runner,
scoped_refptr<base::TaskRunner> background_task_runner,
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
scoped_ptr<LogoDelegate> delegate)
: is_idle_(true),
is_cached_logo_valid_(false),
logo_delegate_(delegate.Pass()),
logo_cache_(new LogoCache(cached_logo_directory)),
clock_(new base::DefaultClock()),
file_task_runner_(file_task_runner),
background_task_runner_(background_task_runner),
request_context_getter_(request_context_getter),
weak_ptr_factory_(this) {}
LogoTracker::~LogoTracker() {
ReturnToIdle();
file_task_runner_->PostTask(
FROM_HERE, base::Bind(&DeleteLogoCacheOnFileThread, logo_cache_));
logo_cache_ = NULL;
}
void LogoTracker::SetServerAPI(
const GURL& logo_url,
const ParseLogoResponse& parse_logo_response_func,
const AppendQueryparamsToLogoURL& append_queryparams_func,
bool wants_cta) {
if (logo_url == logo_url_)
return;
ReturnToIdle();
logo_url_ = logo_url;
parse_logo_response_func_ = parse_logo_response_func;
append_queryparams_func_ = append_queryparams_func;
wants_cta_ = wants_cta;
}
void LogoTracker::GetLogo(LogoObserver* observer) {
DCHECK(!logo_url_.is_empty());
logo_observers_.AddObserver(observer);
if (is_idle_) {
is_idle_ = false;
base::PostTaskAndReplyWithResult(
file_task_runner_.get(),
FROM_HERE,
base::Bind(&GetLogoFromCacheOnFileThread,
logo_cache_,
logo_url_,
clock_->Now()),
base::Bind(&LogoTracker::OnCachedLogoRead,
weak_ptr_factory_.GetWeakPtr()));
} else if (is_cached_logo_valid_) {
observer->OnLogoAvailable(cached_logo_.get(), true);
}
}
void LogoTracker::RemoveObserver(LogoObserver* observer) {
logo_observers_.RemoveObserver(observer);
}
void LogoTracker::SetLogoCacheForTests(scoped_ptr<LogoCache> cache) {
DCHECK(cache);
file_task_runner_->PostTask(
FROM_HERE, base::Bind(&DeleteLogoCacheOnFileThread, logo_cache_));
logo_cache_ = cache.release();
}
void LogoTracker::SetClockForTests(scoped_ptr<base::Clock> clock) {
clock_ = clock.Pass();
}
void LogoTracker::ReturnToIdle() {
// Cancel the current asynchronous operation, if any.
fetcher_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
// Reset state.
is_idle_ = true;
cached_logo_.reset();
is_cached_logo_valid_ = false;
// Clear obsevers.
FOR_EACH_OBSERVER(LogoObserver, logo_observers_, OnObserverRemoved());
logo_observers_.Clear();
}
void LogoTracker::OnCachedLogoRead(scoped_ptr<EncodedLogo> cached_logo) {
DCHECK(!is_idle_);
if (cached_logo) {
logo_delegate_->DecodeUntrustedImage(
cached_logo->encoded_image,
base::Bind(&LogoTracker::OnCachedLogoAvailable,
weak_ptr_factory_.GetWeakPtr(),
cached_logo->metadata));
} else {
OnCachedLogoAvailable(LogoMetadata(), SkBitmap());
}
}
void LogoTracker::OnCachedLogoAvailable(const LogoMetadata& metadata,
const SkBitmap& image) {
DCHECK(!is_idle_);
if (!image.isNull()) {
cached_logo_.reset(new Logo());
cached_logo_->metadata = metadata;
cached_logo_->image = image;
}
is_cached_logo_valid_ = true;
Logo* logo = cached_logo_.get();
FOR_EACH_OBSERVER(LogoObserver, logo_observers_, OnLogoAvailable(logo, true));
FetchLogo();
}
void LogoTracker::SetCachedLogo(scoped_ptr<EncodedLogo> logo) {
file_task_runner_->PostTask(
FROM_HERE,
base::Bind(&LogoCache::SetCachedLogo,
base::Unretained(logo_cache_),
base::Owned(logo.release())));
}
void LogoTracker::SetCachedMetadata(const LogoMetadata& metadata) {
file_task_runner_->PostTask(FROM_HERE,
base::Bind(&LogoCache::UpdateCachedLogoMetadata,
base::Unretained(logo_cache_),
metadata));
}
void LogoTracker::FetchLogo() {
DCHECK(!fetcher_);
DCHECK(!is_idle_);
GURL url;
std::string fingerprint;
if (cached_logo_ && !cached_logo_->metadata.fingerprint.empty() &&
cached_logo_->metadata.expiration_time >= clock_->Now()) {
fingerprint = cached_logo_->metadata.fingerprint;
}
url = append_queryparams_func_.Run(logo_url_, fingerprint, wants_cta_);
fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::GET, this);
fetcher_->SetRequestContext(request_context_getter_.get());
fetcher_->Start();
}
void LogoTracker::OnFreshLogoParsed(scoped_ptr<EncodedLogo> logo) {
DCHECK(!is_idle_);
if (logo)
logo->metadata.source_url = logo_url_.spec();
if (!logo || !logo->encoded_image.get()) {
OnFreshLogoAvailable(logo.Pass(), SkBitmap());
} else {
// Store the value of logo->encoded_image for use below. This ensures that
// logo->encoded_image is evaulated before base::Passed(&logo), which sets
// logo to NULL.
scoped_refptr<base::RefCountedString> encoded_image = logo->encoded_image;
logo_delegate_->DecodeUntrustedImage(
encoded_image,
base::Bind(&LogoTracker::OnFreshLogoAvailable,
weak_ptr_factory_.GetWeakPtr(),
base::Passed(&logo)));
}
}
void LogoTracker::OnFreshLogoAvailable(scoped_ptr<EncodedLogo> encoded_logo,
const SkBitmap& image) {
DCHECK(!is_idle_);
if (encoded_logo && !encoded_logo->encoded_image.get() && cached_logo_ &&
!encoded_logo->metadata.fingerprint.empty() &&
encoded_logo->metadata.fingerprint ==
cached_logo_->metadata.fingerprint) {
// The cached logo was revalidated, i.e. its fingerprint was verified.
// mime_type isn't sent when revalidating, so copy it from the cached logo.
encoded_logo->metadata.mime_type = cached_logo_->metadata.mime_type;
SetCachedMetadata(encoded_logo->metadata);
} else if (encoded_logo && image.isNull()) {
// Image decoding failed. Do nothing.
} else {
scoped_ptr<Logo> logo;
// Check if the server returned a valid, non-empty response.
if (encoded_logo) {
DCHECK(!image.isNull());
logo.reset(new Logo());
logo->metadata = encoded_logo->metadata;
logo->image = image;
}
// Notify observers if a new logo was fetched, or if the new logo is NULL
// but the cached logo was non-NULL.
if (logo || cached_logo_) {
FOR_EACH_OBSERVER(LogoObserver,
logo_observers_,
OnLogoAvailable(logo.get(), false));
SetCachedLogo(encoded_logo.Pass());
}
}
ReturnToIdle();
}
void LogoTracker::OnURLFetchComplete(const net::URLFetcher* source) {
DCHECK(!is_idle_);
scoped_ptr<net::URLFetcher> cleanup_fetcher(fetcher_.release());
if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) {
ReturnToIdle();
return;
}
scoped_ptr<std::string> response(new std::string());
source->GetResponseAsString(response.get());
base::Time response_time = clock_->Now();
base::PostTaskAndReplyWithResult(
background_task_runner_.get(),
FROM_HERE,
base::Bind(
parse_logo_response_func_, base::Passed(&response), response_time),
base::Bind(&LogoTracker::OnFreshLogoParsed,
weak_ptr_factory_.GetWeakPtr()));
}
void LogoTracker::OnURLFetchDownloadProgress(const net::URLFetcher* source,
int64 current,
int64 total) {
if (total > kMaxDownloadBytes || current > kMaxDownloadBytes) {
LOG(WARNING) << "Search provider logo exceeded download size limit";
ReturnToIdle();
}
}
} // namespace search_provider_logos
|