| 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 #include <utility> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // Cancel the current asynchronous operation, if any. | 150 // Cancel the current asynchronous operation, if any. |
| 151 fetcher_.reset(); | 151 fetcher_.reset(); |
| 152 weak_ptr_factory_.InvalidateWeakPtrs(); | 152 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 153 | 153 |
| 154 // Reset state. | 154 // Reset state. |
| 155 is_idle_ = true; | 155 is_idle_ = true; |
| 156 cached_logo_.reset(); | 156 cached_logo_.reset(); |
| 157 is_cached_logo_valid_ = false; | 157 is_cached_logo_valid_ = false; |
| 158 | 158 |
| 159 // Clear obsevers. | 159 // Clear obsevers. |
| 160 FOR_EACH_OBSERVER(LogoObserver, logo_observers_, OnObserverRemoved()); | 160 for (auto& observer : logo_observers_) |
| 161 observer.OnObserverRemoved(); |
| 161 logo_observers_.Clear(); | 162 logo_observers_.Clear(); |
| 162 } | 163 } |
| 163 | 164 |
| 164 void LogoTracker::OnCachedLogoRead(std::unique_ptr<EncodedLogo> cached_logo) { | 165 void LogoTracker::OnCachedLogoRead(std::unique_ptr<EncodedLogo> cached_logo) { |
| 165 DCHECK(!is_idle_); | 166 DCHECK(!is_idle_); |
| 166 | 167 |
| 167 if (cached_logo) { | 168 if (cached_logo) { |
| 168 logo_delegate_->DecodeUntrustedImage( | 169 logo_delegate_->DecodeUntrustedImage( |
| 169 cached_logo->encoded_image, | 170 cached_logo->encoded_image, |
| 170 base::Bind(&LogoTracker::OnCachedLogoAvailable, | 171 base::Bind(&LogoTracker::OnCachedLogoAvailable, |
| 171 weak_ptr_factory_.GetWeakPtr(), | 172 weak_ptr_factory_.GetWeakPtr(), |
| 172 cached_logo->metadata)); | 173 cached_logo->metadata)); |
| 173 } else { | 174 } else { |
| 174 OnCachedLogoAvailable(LogoMetadata(), SkBitmap()); | 175 OnCachedLogoAvailable(LogoMetadata(), SkBitmap()); |
| 175 } | 176 } |
| 176 } | 177 } |
| 177 | 178 |
| 178 void LogoTracker::OnCachedLogoAvailable(const LogoMetadata& metadata, | 179 void LogoTracker::OnCachedLogoAvailable(const LogoMetadata& metadata, |
| 179 const SkBitmap& image) { | 180 const SkBitmap& image) { |
| 180 DCHECK(!is_idle_); | 181 DCHECK(!is_idle_); |
| 181 | 182 |
| 182 if (!image.isNull()) { | 183 if (!image.isNull()) { |
| 183 cached_logo_.reset(new Logo()); | 184 cached_logo_.reset(new Logo()); |
| 184 cached_logo_->metadata = metadata; | 185 cached_logo_->metadata = metadata; |
| 185 cached_logo_->image = image; | 186 cached_logo_->image = image; |
| 186 } | 187 } |
| 187 is_cached_logo_valid_ = true; | 188 is_cached_logo_valid_ = true; |
| 188 Logo* logo = cached_logo_.get(); | 189 Logo* logo = cached_logo_.get(); |
| 189 FOR_EACH_OBSERVER(LogoObserver, logo_observers_, OnLogoAvailable(logo, true)); | 190 for (auto& observer : logo_observers_) |
| 191 observer.OnLogoAvailable(logo, true); |
| 190 FetchLogo(); | 192 FetchLogo(); |
| 191 } | 193 } |
| 192 | 194 |
| 193 void LogoTracker::SetCachedLogo(std::unique_ptr<EncodedLogo> logo) { | 195 void LogoTracker::SetCachedLogo(std::unique_ptr<EncodedLogo> logo) { |
| 194 file_task_runner_->PostTask( | 196 file_task_runner_->PostTask( |
| 195 FROM_HERE, | 197 FROM_HERE, |
| 196 base::Bind(&LogoCache::SetCachedLogo, | 198 base::Bind(&LogoCache::SetCachedLogo, |
| 197 base::Unretained(logo_cache_), | 199 base::Unretained(logo_cache_), |
| 198 base::Owned(logo.release()))); | 200 base::Owned(logo.release()))); |
| 199 } | 201 } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 } else { | 289 } else { |
| 288 if (parsing_failed) | 290 if (parsing_failed) |
| 289 download_outcome = DOWNLOAD_OUTCOME_PARSING_FAILED; | 291 download_outcome = DOWNLOAD_OUTCOME_PARSING_FAILED; |
| 290 else | 292 else |
| 291 download_outcome = DOWNLOAD_OUTCOME_NO_LOGO_TODAY; | 293 download_outcome = DOWNLOAD_OUTCOME_NO_LOGO_TODAY; |
| 292 } | 294 } |
| 293 | 295 |
| 294 // Notify observers if a new logo was fetched, or if the new logo is NULL | 296 // Notify observers if a new logo was fetched, or if the new logo is NULL |
| 295 // but the cached logo was non-NULL. | 297 // but the cached logo was non-NULL. |
| 296 if (logo || cached_logo_) { | 298 if (logo || cached_logo_) { |
| 297 FOR_EACH_OBSERVER(LogoObserver, | 299 for (auto& observer : logo_observers_) |
| 298 logo_observers_, | 300 observer.OnLogoAvailable(logo.get(), false); |
| 299 OnLogoAvailable(logo.get(), false)); | |
| 300 SetCachedLogo(std::move(encoded_logo)); | 301 SetCachedLogo(std::move(encoded_logo)); |
| 301 } | 302 } |
| 302 } | 303 } |
| 303 | 304 |
| 304 DCHECK_NE(kDownloadOutcomeNotTracked, download_outcome); | 305 DCHECK_NE(kDownloadOutcomeNotTracked, download_outcome); |
| 305 ReturnToIdle(download_outcome); | 306 ReturnToIdle(download_outcome); |
| 306 } | 307 } |
| 307 | 308 |
| 308 void LogoTracker::OnURLFetchComplete(const net::URLFetcher* source) { | 309 void LogoTracker::OnURLFetchComplete(const net::URLFetcher* source) { |
| 309 DCHECK(!is_idle_); | 310 DCHECK(!is_idle_); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 int64_t current, | 344 int64_t current, |
| 344 int64_t total, | 345 int64_t total, |
| 345 int64_t current_network_bytes) { | 346 int64_t current_network_bytes) { |
| 346 if (total > kMaxDownloadBytes || current > kMaxDownloadBytes) { | 347 if (total > kMaxDownloadBytes || current > kMaxDownloadBytes) { |
| 347 LOG(WARNING) << "Search provider logo exceeded download size limit"; | 348 LOG(WARNING) << "Search provider logo exceeded download size limit"; |
| 348 ReturnToIdle(DOWNLOAD_OUTCOME_DOWNLOAD_FAILED); | 349 ReturnToIdle(DOWNLOAD_OUTCOME_DOWNLOAD_FAILED); |
| 349 } | 350 } |
| 350 } | 351 } |
| 351 | 352 |
| 352 } // namespace search_provider_logos | 353 } // namespace search_provider_logos |
| OLD | NEW |