| 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/image_fetcher/image_fetcher_impl.h" | 5 #include "components/image_fetcher/image_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 void ImageFetcherImpl::SetImageFetcherDelegate(ImageFetcherDelegate* delegate) { | 32 void ImageFetcherImpl::SetImageFetcherDelegate(ImageFetcherDelegate* delegate) { |
| 33 DCHECK(delegate); | 33 DCHECK(delegate); |
| 34 delegate_ = delegate; | 34 delegate_ = delegate; |
| 35 } | 35 } |
| 36 | 36 |
| 37 void ImageFetcherImpl::SetDataUseServiceName( | 37 void ImageFetcherImpl::SetDataUseServiceName( |
| 38 DataUseServiceName data_use_service_name) { | 38 DataUseServiceName data_use_service_name) { |
| 39 image_data_fetcher_->SetDataUseServiceName(data_use_service_name); | 39 image_data_fetcher_->SetDataUseServiceName(data_use_service_name); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void ImageFetcherImpl::SetDesiredImageFrameSize(const gfx::Size& size) { |
| 43 desired_image_frame_size_ = size; |
| 44 } |
| 45 |
| 42 void ImageFetcherImpl::StartOrQueueNetworkRequest( | 46 void ImageFetcherImpl::StartOrQueueNetworkRequest( |
| 43 const std::string& id, | 47 const std::string& id, |
| 44 const GURL& image_url, | 48 const GURL& image_url, |
| 45 base::Callback<void(const std::string&, const gfx::Image&)> callback) { | 49 base::Callback<void(const std::string&, const gfx::Image&)> callback) { |
| 46 // Before starting to fetch the image. Look for a request in progress for | 50 // Before starting to fetch the image. Look for a request in progress for |
| 47 // |image_url|, and queue if appropriate. | 51 // |image_url|, and queue if appropriate. |
| 48 ImageRequestMap::iterator it = pending_net_requests_.find(image_url); | 52 ImageRequestMap::iterator it = pending_net_requests_.find(image_url); |
| 49 if (it == pending_net_requests_.end()) { | 53 if (it == pending_net_requests_.end()) { |
| 50 ImageRequest request; | 54 ImageRequest request; |
| 51 request.id = id; | 55 request.id = id; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 65 void ImageFetcherImpl::OnImageURLFetched(const GURL& image_url, | 69 void ImageFetcherImpl::OnImageURLFetched(const GURL& image_url, |
| 66 const std::string& image_data, | 70 const std::string& image_data, |
| 67 const RequestMetadata& metadata) { | 71 const RequestMetadata& metadata) { |
| 68 // Inform the ImageFetcherDelegate. | 72 // Inform the ImageFetcherDelegate. |
| 69 if (delegate_) { | 73 if (delegate_) { |
| 70 auto it = pending_net_requests_.find(image_url); | 74 auto it = pending_net_requests_.find(image_url); |
| 71 DCHECK(it != pending_net_requests_.end()); | 75 DCHECK(it != pending_net_requests_.end()); |
| 72 delegate_->OnImageDataFetched(it->second.id, image_data); | 76 delegate_->OnImageDataFetched(it->second.id, image_data); |
| 73 } | 77 } |
| 74 | 78 |
| 75 image_decoder_->DecodeImage( | 79 image_decoder_->DecodeImage(image_data, desired_image_frame_size_, |
| 76 image_data, | 80 base::Bind(&ImageFetcherImpl::OnImageDecoded, |
| 77 base::Bind(&ImageFetcherImpl::OnImageDecoded, | 81 base::Unretained(this), image_url)); |
| 78 base::Unretained(this), image_url)); | |
| 79 } | 82 } |
| 80 | 83 |
| 81 void ImageFetcherImpl::OnImageDecoded(const GURL& image_url, | 84 void ImageFetcherImpl::OnImageDecoded(const GURL& image_url, |
| 82 const gfx::Image& image) { | 85 const gfx::Image& image) { |
| 83 // Get request for the given image_url from the request queue. | 86 // Get request for the given image_url from the request queue. |
| 84 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url); | 87 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url); |
| 85 DCHECK(image_iter != pending_net_requests_.end()); | 88 DCHECK(image_iter != pending_net_requests_.end()); |
| 86 ImageRequest* request = &image_iter->second; | 89 ImageRequest* request = &image_iter->second; |
| 87 | 90 |
| 88 // Run all callbacks | 91 // Run all callbacks |
| 89 for (const auto& callback : request->callbacks) { | 92 for (const auto& callback : request->callbacks) { |
| 90 callback.Run(request->id, image); | 93 callback.Run(request->id, image); |
| 91 } | 94 } |
| 92 | 95 |
| 93 // Inform the ImageFetcherDelegate. | 96 // Inform the ImageFetcherDelegate. |
| 94 if (delegate_) { | 97 if (delegate_) { |
| 95 delegate_->OnImageFetched(request->id, image); | 98 delegate_->OnImageFetched(request->id, image); |
| 96 } | 99 } |
| 97 | 100 |
| 98 // Erase the completed ImageRequest. | 101 // Erase the completed ImageRequest. |
| 99 pending_net_requests_.erase(image_iter); | 102 pending_net_requests_.erase(image_iter); |
| 100 } | 103 } |
| 101 | 104 |
| 102 } // namespace image_fetcher | 105 } // namespace image_fetcher |
| OLD | NEW |