OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/suggestions/suggestions_service.h" | 5 #include "components/suggestions/suggestions_service.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 kSuggestionsFieldTrialStateEnabled; | 149 kSuggestionsFieldTrialStateEnabled; |
150 } | 150 } |
151 | 151 |
152 // static | 152 // static |
153 bool SuggestionsService::IsControlGroup() { | 153 bool SuggestionsService::IsControlGroup() { |
154 return GetExperimentParam(kSuggestionsFieldTrialControlParam) == | 154 return GetExperimentParam(kSuggestionsFieldTrialControlParam) == |
155 kSuggestionsFieldTrialStateEnabled; | 155 kSuggestionsFieldTrialStateEnabled; |
156 } | 156 } |
157 | 157 |
158 void SuggestionsService::FetchSuggestionsData( | 158 void SuggestionsService::FetchSuggestionsData( |
159 SyncState sync_state, | |
159 SuggestionsService::ResponseCallback callback) { | 160 SuggestionsService::ResponseCallback callback) { |
160 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
162 if (sync_state == NOT_INITIALIZED_ENABLED) { | |
163 // Sync is not initialized yet, but enabled. Serve previously cached | |
164 // suggestions if available. | |
165 waiting_requestors_.push_back(callback); | |
166 ServeFromCache(); | |
167 return; | |
168 } else if (sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) { | |
169 // Cancel any ongoing request (and the timeout closure). We must no longer | |
170 // interact with the server. | |
171 pending_request_.reset(NULL); | |
172 pending_timeout_closure_.reset(NULL); | |
173 suggestions_store_->ClearSuggestions(); | |
174 callback.Run(SuggestionsProfile()); | |
manzagop (departed)
2014/08/20 19:28:14
Also serve the waiting requestors?
Mathieu
2014/08/20 21:37:48
Done.
| |
175 return; | |
176 } | |
161 | 177 |
162 FetchSuggestionsDataNoTimeout(callback); | 178 FetchSuggestionsDataNoTimeout(callback); |
163 | 179 |
164 // Post a task to serve the cached suggestions if the request hasn't completed | 180 // Post a task to serve the cached suggestions if the request hasn't completed |
165 // after some time. Cancels the previous such task, if one existed. | 181 // after some time. Cancels the previous such task, if one existed. |
166 pending_timeout_closure_.reset(new CancelableClosure(base::Bind( | 182 pending_timeout_closure_.reset(new CancelableClosure(base::Bind( |
167 &SuggestionsService::OnRequestTimeout, weak_ptr_factory_.GetWeakPtr()))); | 183 &SuggestionsService::OnRequestTimeout, weak_ptr_factory_.GetWeakPtr()))); |
168 base::MessageLoopProxy::current()->PostDelayedTask( | 184 base::MessageLoopProxy::current()->PostDelayedTask( |
169 FROM_HERE, pending_timeout_closure_->callback(), | 185 FROM_HERE, pending_timeout_closure_->callback(), |
170 base::TimeDelta::FromMilliseconds(request_timeout_ms_)); | 186 base::TimeDelta::FromMilliseconds(request_timeout_ms_)); |
171 } | 187 } |
172 | 188 |
173 void SuggestionsService::FetchSuggestionsDataNoTimeout( | |
174 SuggestionsService::ResponseCallback callback) { | |
175 DCHECK(thread_checker_.CalledOnValidThread()); | |
176 if (pending_request_.get()) { | |
177 // Request already exists, so just add requestor to queue. | |
178 waiting_requestors_.push_back(callback); | |
179 return; | |
180 } | |
181 | |
182 // Form new request. | |
183 DCHECK(waiting_requestors_.empty()); | |
184 waiting_requestors_.push_back(callback); | |
185 IssueRequest(suggestions_url_); | |
186 } | |
187 | |
188 void SuggestionsService::GetPageThumbnail( | 189 void SuggestionsService::GetPageThumbnail( |
189 const GURL& url, | 190 const GURL& url, |
190 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | 191 base::Callback<void(const GURL&, const SkBitmap*)> callback) { |
191 thumbnail_manager_->GetImageForURL(url, callback); | 192 thumbnail_manager_->GetImageForURL(url, callback); |
192 } | 193 } |
193 | 194 |
194 void SuggestionsService::BlacklistURL( | 195 void SuggestionsService::BlacklistURL( |
195 const GURL& candidate_url, | 196 const GURL& candidate_url, |
196 const SuggestionsService::ResponseCallback& callback) { | 197 const SuggestionsService::ResponseCallback& callback) { |
197 DCHECK(thread_checker_.CalledOnValidThread()); | 198 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 30 matching lines...) Expand all Loading... | |
228 return true; | 229 return true; |
229 } | 230 } |
230 | 231 |
231 // static | 232 // static |
232 void SuggestionsService::RegisterProfilePrefs( | 233 void SuggestionsService::RegisterProfilePrefs( |
233 user_prefs::PrefRegistrySyncable* registry) { | 234 user_prefs::PrefRegistrySyncable* registry) { |
234 SuggestionsStore::RegisterProfilePrefs(registry); | 235 SuggestionsStore::RegisterProfilePrefs(registry); |
235 BlacklistStore::RegisterProfilePrefs(registry); | 236 BlacklistStore::RegisterProfilePrefs(registry); |
236 } | 237 } |
237 | 238 |
239 void SuggestionsService::SetDefaultExpiryTimestamp( | |
240 SuggestionsProfile* suggestions, int64 default_timestamp_usec) { | |
241 for (int i = 0; i < suggestions->suggestions_size(); ++i) { | |
242 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); | |
243 // Do not set expiry if the server has already provided a more specific | |
244 // expiry time for this suggestion. | |
245 if (!suggestion->has_expiry_ts()) { | |
246 suggestion->set_expiry_ts(default_timestamp_usec); | |
247 } | |
248 } | |
249 } | |
250 | |
251 void SuggestionsService::FetchSuggestionsDataNoTimeout( | |
252 SuggestionsService::ResponseCallback callback) { | |
253 DCHECK(thread_checker_.CalledOnValidThread()); | |
254 if (pending_request_.get()) { | |
255 // Request already exists, so just add requestor to queue. | |
256 waiting_requestors_.push_back(callback); | |
257 return; | |
258 } | |
259 | |
260 // Form new request. | |
261 DCHECK(waiting_requestors_.empty()); | |
262 waiting_requestors_.push_back(callback); | |
263 IssueRequest(suggestions_url_); | |
264 } | |
265 | |
238 void SuggestionsService::IssueRequest(const GURL& url) { | 266 void SuggestionsService::IssueRequest(const GURL& url) { |
239 pending_request_.reset(CreateSuggestionsRequest(url)); | 267 pending_request_.reset(CreateSuggestionsRequest(url)); |
240 pending_request_->Start(); | 268 pending_request_->Start(); |
241 last_request_started_time_ = base::TimeTicks::Now(); | 269 last_request_started_time_ = base::TimeTicks::Now(); |
242 } | 270 } |
243 | 271 |
244 net::URLFetcher* SuggestionsService::CreateSuggestionsRequest(const GURL& url) { | 272 net::URLFetcher* SuggestionsService::CreateSuggestionsRequest(const GURL& url) { |
245 net::URLFetcher* request = | 273 net::URLFetcher* request = |
246 net::URLFetcher::Create(0, url, net::URLFetcher::GET, this); | 274 net::URLFetcher::Create(0, url, net::URLFetcher::GET, this); |
247 request->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 275 request->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 } else { | 351 } else { |
324 LogResponseState(RESPONSE_INVALID); | 352 LogResponseState(RESPONSE_INVALID); |
325 suggestions_store_->LoadSuggestions(&suggestions); | 353 suggestions_store_->LoadSuggestions(&suggestions); |
326 thumbnail_manager_->Initialize(suggestions); | 354 thumbnail_manager_->Initialize(suggestions); |
327 } | 355 } |
328 | 356 |
329 FilterAndServe(&suggestions); | 357 FilterAndServe(&suggestions); |
330 ScheduleBlacklistUpload(true); | 358 ScheduleBlacklistUpload(true); |
331 } | 359 } |
332 | 360 |
333 void SuggestionsService::SetDefaultExpiryTimestamp( | |
334 SuggestionsProfile* suggestions, int64 default_timestamp_usec) { | |
335 for (int i = 0; i < suggestions->suggestions_size(); ++i) { | |
336 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); | |
337 // Do not set expiry if the server has already provided a more specific | |
338 // expiry time for this suggestion. | |
339 if (!suggestion->has_expiry_ts()) { | |
340 suggestion->set_expiry_ts(default_timestamp_usec); | |
341 } | |
342 } | |
343 } | |
344 | |
345 void SuggestionsService::Shutdown() { | 361 void SuggestionsService::Shutdown() { |
346 // Cancel pending request and timeout closure, then serve existing requestors | 362 // Cancel pending request and timeout closure, then serve existing requestors |
347 // from cache. | 363 // from cache. |
348 pending_request_.reset(NULL); | 364 pending_request_.reset(NULL); |
349 pending_timeout_closure_.reset(NULL); | 365 pending_timeout_closure_.reset(NULL); |
350 ServeFromCache(); | 366 ServeFromCache(); |
351 } | 367 } |
352 | 368 |
353 void SuggestionsService::ServeFromCache() { | 369 void SuggestionsService::ServeFromCache() { |
354 SuggestionsProfile suggestions; | 370 SuggestionsProfile suggestions; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 if (last_request_successful) { | 415 if (last_request_successful) { |
400 blacklist_delay_sec_ = kBlacklistDefaultDelaySec; | 416 blacklist_delay_sec_ = kBlacklistDefaultDelaySec; |
401 } else { | 417 } else { |
402 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier; | 418 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier; |
403 if (candidate_delay < kBlacklistMaxDelaySec) | 419 if (candidate_delay < kBlacklistMaxDelaySec) |
404 blacklist_delay_sec_ = candidate_delay; | 420 blacklist_delay_sec_ = candidate_delay; |
405 } | 421 } |
406 } | 422 } |
407 | 423 |
408 } // namespace suggestions | 424 } // namespace suggestions |
OLD | NEW |