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/search_provider_logos/logo_tracker.h" | 5 #include "components/search_provider_logos/logo_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
10 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
11 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
12 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
13 #include "base/time/default_clock.h" | 14 #include "base/time/default_clock.h" |
14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
15 #include "net/url_request/url_fetcher.h" | 16 #include "net/url_request/url_fetcher.h" |
16 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
17 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
(...skipping 30 matching lines...) Expand all Loading... |
48 const LogoMetadata* metadata = logo_cache->GetCachedLogoMetadata(); | 49 const LogoMetadata* metadata = logo_cache->GetCachedLogoMetadata(); |
49 if (!metadata) | 50 if (!metadata) |
50 return scoped_ptr<EncodedLogo>(); | 51 return scoped_ptr<EncodedLogo>(); |
51 | 52 |
52 if (metadata->source_url != logo_url.spec() || | 53 if (metadata->source_url != logo_url.spec() || |
53 !IsLogoOkToShow(*metadata, now)) { | 54 !IsLogoOkToShow(*metadata, now)) { |
54 logo_cache->SetCachedLogo(NULL); | 55 logo_cache->SetCachedLogo(NULL); |
55 return scoped_ptr<EncodedLogo>(); | 56 return scoped_ptr<EncodedLogo>(); |
56 } | 57 } |
57 | 58 |
58 return logo_cache->GetCachedLogo().Pass(); | 59 return logo_cache->GetCachedLogo(); |
59 } | 60 } |
60 | 61 |
61 void DeleteLogoCacheOnFileThread(LogoCache* logo_cache) { | 62 void DeleteLogoCacheOnFileThread(LogoCache* logo_cache) { |
62 delete logo_cache; | 63 delete logo_cache; |
63 } | 64 } |
64 | 65 |
65 } // namespace | 66 } // namespace |
66 | 67 |
67 LogoTracker::LogoTracker( | 68 LogoTracker::LogoTracker( |
68 base::FilePath cached_logo_directory, | 69 base::FilePath cached_logo_directory, |
69 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 70 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
70 scoped_refptr<base::TaskRunner> background_task_runner, | 71 scoped_refptr<base::TaskRunner> background_task_runner, |
71 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 72 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
72 scoped_ptr<LogoDelegate> delegate) | 73 scoped_ptr<LogoDelegate> delegate) |
73 : is_idle_(true), | 74 : is_idle_(true), |
74 is_cached_logo_valid_(false), | 75 is_cached_logo_valid_(false), |
75 logo_delegate_(delegate.Pass()), | 76 logo_delegate_(std::move(delegate)), |
76 logo_cache_(new LogoCache(cached_logo_directory)), | 77 logo_cache_(new LogoCache(cached_logo_directory)), |
77 clock_(new base::DefaultClock()), | 78 clock_(new base::DefaultClock()), |
78 file_task_runner_(file_task_runner), | 79 file_task_runner_(file_task_runner), |
79 background_task_runner_(background_task_runner), | 80 background_task_runner_(background_task_runner), |
80 request_context_getter_(request_context_getter), | 81 request_context_getter_(request_context_getter), |
81 weak_ptr_factory_(this) {} | 82 weak_ptr_factory_(this) {} |
82 | 83 |
83 LogoTracker::~LogoTracker() { | 84 LogoTracker::~LogoTracker() { |
84 ReturnToIdle(kDownloadOutcomeNotTracked); | 85 ReturnToIdle(kDownloadOutcomeNotTracked); |
85 file_task_runner_->PostTask( | 86 file_task_runner_->PostTask( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 129 } |
129 | 130 |
130 void LogoTracker::SetLogoCacheForTests(scoped_ptr<LogoCache> cache) { | 131 void LogoTracker::SetLogoCacheForTests(scoped_ptr<LogoCache> cache) { |
131 DCHECK(cache); | 132 DCHECK(cache); |
132 file_task_runner_->PostTask( | 133 file_task_runner_->PostTask( |
133 FROM_HERE, base::Bind(&DeleteLogoCacheOnFileThread, logo_cache_)); | 134 FROM_HERE, base::Bind(&DeleteLogoCacheOnFileThread, logo_cache_)); |
134 logo_cache_ = cache.release(); | 135 logo_cache_ = cache.release(); |
135 } | 136 } |
136 | 137 |
137 void LogoTracker::SetClockForTests(scoped_ptr<base::Clock> clock) { | 138 void LogoTracker::SetClockForTests(scoped_ptr<base::Clock> clock) { |
138 clock_ = clock.Pass(); | 139 clock_ = std::move(clock); |
139 } | 140 } |
140 | 141 |
141 void LogoTracker::ReturnToIdle(int outcome) { | 142 void LogoTracker::ReturnToIdle(int outcome) { |
142 if (outcome != kDownloadOutcomeNotTracked) { | 143 if (outcome != kDownloadOutcomeNotTracked) { |
143 UMA_HISTOGRAM_ENUMERATION("NewTabPage.LogoDownloadOutcome", outcome, | 144 UMA_HISTOGRAM_ENUMERATION("NewTabPage.LogoDownloadOutcome", outcome, |
144 DOWNLOAD_OUTCOME_COUNT); | 145 DOWNLOAD_OUTCOME_COUNT); |
145 } | 146 } |
146 // Cancel the current asynchronous operation, if any. | 147 // Cancel the current asynchronous operation, if any. |
147 fetcher_.reset(); | 148 fetcher_.reset(); |
148 weak_ptr_factory_.InvalidateWeakPtrs(); | 149 weak_ptr_factory_.InvalidateWeakPtrs(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 } | 221 } |
221 | 222 |
222 void LogoTracker::OnFreshLogoParsed(bool* parsing_failed, | 223 void LogoTracker::OnFreshLogoParsed(bool* parsing_failed, |
223 scoped_ptr<EncodedLogo> logo) { | 224 scoped_ptr<EncodedLogo> logo) { |
224 DCHECK(!is_idle_); | 225 DCHECK(!is_idle_); |
225 | 226 |
226 if (logo) | 227 if (logo) |
227 logo->metadata.source_url = logo_url_.spec(); | 228 logo->metadata.source_url = logo_url_.spec(); |
228 | 229 |
229 if (!logo || !logo->encoded_image.get()) { | 230 if (!logo || !logo->encoded_image.get()) { |
230 OnFreshLogoAvailable(logo.Pass(), *parsing_failed, SkBitmap()); | 231 OnFreshLogoAvailable(std::move(logo), *parsing_failed, SkBitmap()); |
231 } else { | 232 } else { |
232 // Store the value of logo->encoded_image for use below. This ensures that | 233 // Store the value of logo->encoded_image for use below. This ensures that |
233 // logo->encoded_image is evaulated before base::Passed(&logo), which sets | 234 // logo->encoded_image is evaulated before base::Passed(&logo), which sets |
234 // logo to NULL. | 235 // logo to NULL. |
235 scoped_refptr<base::RefCountedString> encoded_image = logo->encoded_image; | 236 scoped_refptr<base::RefCountedString> encoded_image = logo->encoded_image; |
236 logo_delegate_->DecodeUntrustedImage( | 237 logo_delegate_->DecodeUntrustedImage( |
237 encoded_image, | 238 encoded_image, |
238 base::Bind(&LogoTracker::OnFreshLogoAvailable, | 239 base::Bind(&LogoTracker::OnFreshLogoAvailable, |
239 weak_ptr_factory_.GetWeakPtr(), | 240 weak_ptr_factory_.GetWeakPtr(), |
240 base::Passed(&logo), | 241 base::Passed(&logo), |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 else | 280 else |
280 download_outcome = DOWNLOAD_OUTCOME_NO_LOGO_TODAY; | 281 download_outcome = DOWNLOAD_OUTCOME_NO_LOGO_TODAY; |
281 } | 282 } |
282 | 283 |
283 // Notify observers if a new logo was fetched, or if the new logo is NULL | 284 // Notify observers if a new logo was fetched, or if the new logo is NULL |
284 // but the cached logo was non-NULL. | 285 // but the cached logo was non-NULL. |
285 if (logo || cached_logo_) { | 286 if (logo || cached_logo_) { |
286 FOR_EACH_OBSERVER(LogoObserver, | 287 FOR_EACH_OBSERVER(LogoObserver, |
287 logo_observers_, | 288 logo_observers_, |
288 OnLogoAvailable(logo.get(), false)); | 289 OnLogoAvailable(logo.get(), false)); |
289 SetCachedLogo(encoded_logo.Pass()); | 290 SetCachedLogo(std::move(encoded_logo)); |
290 } | 291 } |
291 } | 292 } |
292 | 293 |
293 DCHECK(download_outcome != kDownloadOutcomeNotTracked); | 294 DCHECK(download_outcome != kDownloadOutcomeNotTracked); |
294 ReturnToIdle(download_outcome); | 295 ReturnToIdle(download_outcome); |
295 } | 296 } |
296 | 297 |
297 void LogoTracker::OnURLFetchComplete(const net::URLFetcher* source) { | 298 void LogoTracker::OnURLFetchComplete(const net::URLFetcher* source) { |
298 DCHECK(!is_idle_); | 299 DCHECK(!is_idle_); |
299 scoped_ptr<net::URLFetcher> cleanup_fetcher(fetcher_.release()); | 300 scoped_ptr<net::URLFetcher> cleanup_fetcher(fetcher_.release()); |
(...skipping 22 matching lines...) Expand all Loading... |
322 void LogoTracker::OnURLFetchDownloadProgress(const net::URLFetcher* source, | 323 void LogoTracker::OnURLFetchDownloadProgress(const net::URLFetcher* source, |
323 int64_t current, | 324 int64_t current, |
324 int64_t total) { | 325 int64_t total) { |
325 if (total > kMaxDownloadBytes || current > kMaxDownloadBytes) { | 326 if (total > kMaxDownloadBytes || current > kMaxDownloadBytes) { |
326 LOG(WARNING) << "Search provider logo exceeded download size limit"; | 327 LOG(WARNING) << "Search provider logo exceeded download size limit"; |
327 ReturnToIdle(DOWNLOAD_OUTCOME_DOWNLOAD_FAILED); | 328 ReturnToIdle(DOWNLOAD_OUTCOME_DOWNLOAD_FAILED); |
328 } | 329 } |
329 } | 330 } |
330 | 331 |
331 } // namespace search_provider_logos | 332 } // namespace search_provider_logos |
OLD | NEW |