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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
// 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/suggestions/suggestions_service.h"
#include <sstream>
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/histogram.h"
#include "base/metrics/sparse_histogram.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/suggestions/blacklist_store.h"
#include "components/suggestions/suggestions_store.h"
#include "components/variations/variations_associated_data.h"
#include "components/variations/variations_http_header_provider.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/base/url_util.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/http/http_util.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.h"
#include "url/gurl.h"
using base::CancelableClosure;
namespace suggestions {
namespace {
// Used to UMA log the state of the last response from the server.
enum SuggestionsResponseState {
RESPONSE_EMPTY,
RESPONSE_INVALID,
RESPONSE_VALID,
RESPONSE_STATE_SIZE
};
// Will log the supplied response |state|.
void LogResponseState(SuggestionsResponseState state) {
UMA_HISTOGRAM_ENUMERATION("Suggestions.ResponseState", state,
RESPONSE_STATE_SIZE);
}
// Obtains the experiment parameter under the supplied |key|, or empty string
// if the parameter does not exist.
std::string GetExperimentParam(const std::string& key) {
return variations::GetVariationParamValue(kSuggestionsFieldTrialName, key);
}
GURL BuildBlacklistRequestURL(const std::string& blacklist_url_prefix,
const GURL& candidate_url) {
return GURL(blacklist_url_prefix +
net::EscapeQueryParamValue(candidate_url.spec(), true));
}
// Runs each callback in |requestors| on |suggestions|, then deallocates
// |requestors|.
void DispatchRequestsAndClear(
const SuggestionsProfile& suggestions,
std::vector<SuggestionsService::ResponseCallback>* requestors) {
std::vector<SuggestionsService::ResponseCallback>::iterator it;
for (it = requestors->begin(); it != requestors->end(); ++it) {
if (!it->is_null()) it->Run(suggestions);
}
std::vector<SuggestionsService::ResponseCallback>().swap(*requestors);
}
const int kDefaultRequestTimeoutMs = 200;
// Default delay used when scheduling a blacklist request.
const int kBlacklistDefaultDelaySec = 1;
// Multiplier on the delay used when scheduling a blacklist request, in case the
// last observed request was unsuccessful.
const int kBlacklistBackoffMultiplier = 2;
// Maximum valid delay for scheduling a request. Candidate delays larger than
// this are rejected. This means the maximum backoff is at least 300 / 2, i.e.
// 2.5 minutes.
const int kBlacklistMaxDelaySec = 300; // 5 minutes
} // namespace
const char kSuggestionsFieldTrialName[] = "ChromeSuggestions";
const char kSuggestionsFieldTrialURLParam[] = "url";
const char kSuggestionsFieldTrialCommonParamsParam[] = "common_params";
const char kSuggestionsFieldTrialBlacklistPathParam[] = "blacklist_path";
const char kSuggestionsFieldTrialBlacklistUrlParam[] = "blacklist_url_param";
const char kSuggestionsFieldTrialStateParam[] = "state";
const char kSuggestionsFieldTrialControlParam[] = "control";
const char kSuggestionsFieldTrialStateEnabled[] = "enabled";
const char kSuggestionsFieldTrialTimeoutMs[] = "timeout_ms";
// The default expiry timeout is 72 hours.
const int64 kDefaultExpiryUsec = 72 * base::Time::kMicrosecondsPerHour;
namespace {
std::string GetBlacklistUrlPrefix() {
std::stringstream blacklist_url_prefix_stream;
blacklist_url_prefix_stream
<< GetExperimentParam(kSuggestionsFieldTrialURLParam)
<< GetExperimentParam(kSuggestionsFieldTrialBlacklistPathParam) << "?"
<< GetExperimentParam(kSuggestionsFieldTrialCommonParamsParam) << "&"
<< GetExperimentParam(kSuggestionsFieldTrialBlacklistUrlParam) << "=";
return blacklist_url_prefix_stream.str();
}
} // namespace
SuggestionsService::SuggestionsService(
net::URLRequestContextGetter* url_request_context,
scoped_ptr<SuggestionsStore> suggestions_store,
scoped_ptr<ImageManager> thumbnail_manager,
scoped_ptr<BlacklistStore> blacklist_store)
: suggestions_store_(suggestions_store.Pass()),
blacklist_store_(blacklist_store.Pass()),
thumbnail_manager_(thumbnail_manager.Pass()),
url_request_context_(url_request_context),
blacklist_delay_sec_(kBlacklistDefaultDelaySec),
request_timeout_ms_(kDefaultRequestTimeoutMs),
weak_ptr_factory_(this) {
// Obtain various parameters from Variations.
suggestions_url_ =
GURL(GetExperimentParam(kSuggestionsFieldTrialURLParam) + "?" +
GetExperimentParam(kSuggestionsFieldTrialCommonParamsParam));
blacklist_url_prefix_ = GetBlacklistUrlPrefix();
std::string timeout = GetExperimentParam(kSuggestionsFieldTrialTimeoutMs);
int temp_timeout;
if (!timeout.empty() && base::StringToInt(timeout, &temp_timeout)) {
request_timeout_ms_ = temp_timeout;
}
}
SuggestionsService::~SuggestionsService() {}
// static
bool SuggestionsService::IsEnabled() {
return GetExperimentParam(kSuggestionsFieldTrialStateParam) ==
kSuggestionsFieldTrialStateEnabled;
}
// static
bool SuggestionsService::IsControlGroup() {
return GetExperimentParam(kSuggestionsFieldTrialControlParam) ==
kSuggestionsFieldTrialStateEnabled;
}
void SuggestionsService::FetchSuggestionsData(
SyncState sync_state,
SuggestionsService::ResponseCallback callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (sync_state == NOT_INITIALIZED_ENABLED) {
// Sync is not initialized yet, but enabled. Serve previously cached
// suggestions if available.
waiting_requestors_.push_back(callback);
ServeFromCache();
return;
} else if (sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) {
// Cancel any ongoing request (and the timeout closure). We must no longer
// interact with the server.
pending_request_.reset(NULL);
pending_timeout_closure_.reset(NULL);
suggestions_store_->ClearSuggestions();
callback.Run(SuggestionsProfile());
DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_);
return;
}
FetchSuggestionsDataNoTimeout(callback);
// Post a task to serve the cached suggestions if the request hasn't completed
// after some time. Cancels the previous such task, if one existed.
pending_timeout_closure_.reset(new CancelableClosure(base::Bind(
&SuggestionsService::OnRequestTimeout, weak_ptr_factory_.GetWeakPtr())));
base::MessageLoopProxy::current()->PostDelayedTask(
FROM_HERE, pending_timeout_closure_->callback(),
base::TimeDelta::FromMilliseconds(request_timeout_ms_));
}
void SuggestionsService::GetPageThumbnail(
const GURL& url,
base::Callback<void(const GURL&, const SkBitmap*)> callback) {
thumbnail_manager_->GetImageForURL(url, callback);
}
void SuggestionsService::BlacklistURL(
const GURL& candidate_url,
const SuggestionsService::ResponseCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
waiting_requestors_.push_back(callback);
// Blacklist locally, for immediate effect.
if (!blacklist_store_->BlacklistUrl(candidate_url)) {
DVLOG(1) << "Failed blacklisting attempt.";
return;
}
// If there's an ongoing request, let it complete.
if (pending_request_.get()) return;
IssueRequest(BuildBlacklistRequestURL(blacklist_url_prefix_, candidate_url));
}
// static
bool SuggestionsService::GetBlacklistedUrl(const net::URLFetcher& request,
GURL* url) {
bool is_blacklist_request = StartsWithASCII(request.GetOriginalURL().spec(),
GetBlacklistUrlPrefix(), true);
if (!is_blacklist_request) return false;
// Extract the blacklisted URL from the blacklist request.
std::string blacklisted;
if (!net::GetValueForKeyInQuery(
request.GetOriginalURL(),
GetExperimentParam(kSuggestionsFieldTrialBlacklistUrlParam),
&blacklisted))
return false;
GURL blacklisted_url(blacklisted);
blacklisted_url.Swap(url);
return true;
}
// static
void SuggestionsService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
SuggestionsStore::RegisterProfilePrefs(registry);
BlacklistStore::RegisterProfilePrefs(registry);
}
void SuggestionsService::SetDefaultExpiryTimestamp(
SuggestionsProfile* suggestions, int64 default_timestamp_usec) {
for (int i = 0; i < suggestions->suggestions_size(); ++i) {
ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i);
// Do not set expiry if the server has already provided a more specific
// expiry time for this suggestion.
if (!suggestion->has_expiry_ts()) {
suggestion->set_expiry_ts(default_timestamp_usec);
}
}
}
void SuggestionsService::FetchSuggestionsDataNoTimeout(
SuggestionsService::ResponseCallback callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (pending_request_.get()) {
// Request already exists, so just add requestor to queue.
waiting_requestors_.push_back(callback);
return;
}
// Form new request.
DCHECK(waiting_requestors_.empty());
waiting_requestors_.push_back(callback);
IssueRequest(suggestions_url_);
}
void SuggestionsService::IssueRequest(const GURL& url) {
pending_request_.reset(CreateSuggestionsRequest(url));
pending_request_->Start();
last_request_started_time_ = base::TimeTicks::Now();
}
net::URLFetcher* SuggestionsService::CreateSuggestionsRequest(const GURL& url) {
net::URLFetcher* request =
net::URLFetcher::Create(0, url, net::URLFetcher::GET, this);
request->SetLoadFlags(net::LOAD_DISABLE_CACHE);
request->SetRequestContext(url_request_context_);
// Add Chrome experiment state to the request headers.
net::HttpRequestHeaders headers;
variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
request->GetOriginalURL(), false, false, &headers);
request->SetExtraRequestHeaders(headers.ToString());
return request;
}
void SuggestionsService::OnRequestTimeout() {
DCHECK(thread_checker_.CalledOnValidThread());
ServeFromCache();
}
void SuggestionsService::OnURLFetchComplete(const net::URLFetcher* source) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_EQ(pending_request_.get(), source);
// We no longer need the timeout closure. Delete it whether or not it has run.
// If it hasn't, this cancels it.
pending_timeout_closure_.reset();
// The fetcher will be deleted when the request is handled.
scoped_ptr<const net::URLFetcher> request(pending_request_.release());
const net::URLRequestStatus& request_status = request->GetStatus();
if (request_status.status() != net::URLRequestStatus::SUCCESS) {
UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FailedRequestErrorCode",
-request_status.error());
DVLOG(1) << "Suggestions server request failed with error: "
<< request_status.error() << ": "
<< net::ErrorToString(request_status.error());
// Dispatch the cached profile on error.
ServeFromCache();
ScheduleBlacklistUpload(false);
return;
}
// Log the response code.
const int response_code = request->GetResponseCode();
UMA_HISTOGRAM_SPARSE_SLOWLY("Suggestions.FetchResponseCode", response_code);
if (response_code != net::HTTP_OK) {
// Aggressively clear the store.
suggestions_store_->ClearSuggestions();
DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_);
ScheduleBlacklistUpload(false);
return;
}
const base::TimeDelta latency =
base::TimeTicks::Now() - last_request_started_time_;
UMA_HISTOGRAM_MEDIUM_TIMES("Suggestions.FetchSuccessLatency", latency);
// Handle a successful blacklisting.
GURL blacklisted_url;
if (GetBlacklistedUrl(*source, &blacklisted_url)) {
blacklist_store_->RemoveUrl(blacklisted_url);
}
std::string suggestions_data;
bool success = request->GetResponseAsString(&suggestions_data);
DCHECK(success);
// Compute suggestions, and dispatch them to requestors. On error still
// dispatch empty suggestions.
SuggestionsProfile suggestions;
if (suggestions_data.empty()) {
LogResponseState(RESPONSE_EMPTY);
suggestions_store_->ClearSuggestions();
} else if (suggestions.ParseFromString(suggestions_data)) {
LogResponseState(RESPONSE_VALID);
thumbnail_manager_->Initialize(suggestions);
int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
.ToInternalValue();
SetDefaultExpiryTimestamp(&suggestions, now_usec + kDefaultExpiryUsec);
suggestions_store_->StoreSuggestions(suggestions);
} else {
LogResponseState(RESPONSE_INVALID);
suggestions_store_->LoadSuggestions(&suggestions);
thumbnail_manager_->Initialize(suggestions);
}
FilterAndServe(&suggestions);
ScheduleBlacklistUpload(true);
}
void SuggestionsService::Shutdown() {
// Cancel pending request and timeout closure, then serve existing requestors
// from cache.
pending_request_.reset(NULL);
pending_timeout_closure_.reset(NULL);
ServeFromCache();
}
void SuggestionsService::ServeFromCache() {
SuggestionsProfile suggestions;
suggestions_store_->LoadSuggestions(&suggestions);
thumbnail_manager_->Initialize(suggestions);
FilterAndServe(&suggestions);
}
void SuggestionsService::FilterAndServe(SuggestionsProfile* suggestions) {
blacklist_store_->FilterSuggestions(suggestions);
DispatchRequestsAndClear(*suggestions, &waiting_requestors_);
}
void SuggestionsService::ScheduleBlacklistUpload(bool last_request_successful) {
DCHECK(thread_checker_.CalledOnValidThread());
UpdateBlacklistDelay(last_request_successful);
// Schedule a blacklist upload task.
GURL blacklist_url;
if (blacklist_store_->GetFirstUrlFromBlacklist(&blacklist_url)) {
base::Closure blacklist_cb =
base::Bind(&SuggestionsService::UploadOneFromBlacklist,
weak_ptr_factory_.GetWeakPtr());
base::MessageLoopProxy::current()->PostDelayedTask(
FROM_HERE, blacklist_cb,
base::TimeDelta::FromSeconds(blacklist_delay_sec_));
}
}
void SuggestionsService::UploadOneFromBlacklist() {
DCHECK(thread_checker_.CalledOnValidThread());
// If there's an ongoing request, let it complete.
if (pending_request_.get()) return;
GURL blacklist_url;
if (!blacklist_store_->GetFirstUrlFromBlacklist(&blacklist_url))
return; // Local blacklist is empty.
// Send blacklisting request.
IssueRequest(BuildBlacklistRequestURL(blacklist_url_prefix_, blacklist_url));
}
void SuggestionsService::UpdateBlacklistDelay(bool last_request_successful) {
DCHECK(thread_checker_.CalledOnValidThread());
if (last_request_successful) {
blacklist_delay_sec_ = kBlacklistDefaultDelaySec;
} else {
int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier;
if (candidate_delay < kBlacklistMaxDelaySec)
blacklist_delay_sec_ = candidate_delay;
}
}
} // namespace suggestions
|