| 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 "chrome/browser/search/suggestions/image_fetcher_impl.h" | 5 #include "chrome/browser/search/suggestions/image_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | |
| 11 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 12 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| 13 #include "net/url_request/url_request_context_getter.h" | 11 #include "net/url_request/url_request_context_getter.h" |
| 14 #include "ui/gfx/image/image.h" | 12 #include "ui/gfx/image/image.h" |
| 15 | 13 |
| 16 namespace suggestions { | 14 namespace suggestions { |
| 17 | 15 |
| 18 ImageFetcherImpl::ImageFetcherImpl( | 16 ImageFetcherImpl::ImageFetcherImpl( |
| 19 net::URLRequestContextGetter* url_request_context) | 17 net::URLRequestContextGetter* url_request_context) |
| 20 : delegate_(nullptr), url_request_context_(url_request_context), | 18 : delegate_(NULL), url_request_context_(url_request_context) {} |
| 21 image_decoder_(new suggestions::ImageDecoderImpl()), | |
| 22 image_data_fetcher_( | |
| 23 new image_fetcher::ImageDataFetcher(url_request_context_)) { | |
| 24 } | |
| 25 | 19 |
| 26 ImageFetcherImpl::~ImageFetcherImpl() {} | 20 ImageFetcherImpl::~ImageFetcherImpl() {} |
| 27 | 21 |
| 28 ImageFetcherImpl::ImageRequest::ImageRequest() {} | 22 ImageFetcherImpl::ImageRequest::ImageRequest() : fetcher(NULL) {} |
| 23 |
| 24 ImageFetcherImpl::ImageRequest::ImageRequest(chrome::BitmapFetcher* f) |
| 25 : fetcher(f) {} |
| 29 | 26 |
| 30 ImageFetcherImpl::ImageRequest::ImageRequest(const ImageRequest& other) = | 27 ImageFetcherImpl::ImageRequest::ImageRequest(const ImageRequest& other) = |
| 31 default; | 28 default; |
| 32 | 29 |
| 33 ImageFetcherImpl::ImageRequest::~ImageRequest() { } | 30 ImageFetcherImpl::ImageRequest::~ImageRequest() { delete fetcher; } |
| 34 | 31 |
| 35 void ImageFetcherImpl::SetImageFetcherDelegate( | 32 void ImageFetcherImpl::SetImageFetcherDelegate( |
| 36 image_fetcher::ImageFetcherDelegate* delegate) { | 33 image_fetcher::ImageFetcherDelegate* delegate) { |
| 37 DCHECK(delegate); | 34 DCHECK(delegate); |
| 38 delegate_ = delegate; | 35 delegate_ = delegate; |
| 39 } | 36 } |
| 40 | 37 |
| 41 void ImageFetcherImpl::StartOrQueueNetworkRequest( | 38 void ImageFetcherImpl::StartOrQueueNetworkRequest( |
| 42 const std::string& id, | 39 const std::string& id, |
| 43 const GURL& image_url, | 40 const GURL& image_url, |
| 44 base::Callback<void(const std::string&, const gfx::Image&)> callback) { | 41 base::Callback<void(const std::string&, const gfx::Image&)> callback) { |
| 45 // Before starting to fetch the image. Look for a request in progress for | 42 // Before starting to fetch the image. Look for a request in progress for |
| 46 // |image_url|, and queue if appropriate. | 43 // |image_url|, and queue if appropriate. |
| 47 ImageRequestMap::iterator it = pending_net_requests_.find(image_url); | 44 ImageRequestMap::iterator it = pending_net_requests_.find(image_url); |
| 48 if (it == pending_net_requests_.end()) { | 45 if (it == pending_net_requests_.end()) { |
| 49 ImageRequest request; | 46 // |image_url| is not being fetched, so create a request and initiate |
| 47 // the fetch. |
| 48 ImageRequest request(new chrome::BitmapFetcher(image_url, this)); |
| 50 request.id = id; | 49 request.id = id; |
| 51 request.callbacks.push_back(callback); | 50 request.callbacks.push_back(callback); |
| 51 request.fetcher->Init( |
| 52 url_request_context_, std::string(), |
| 53 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 54 net::LOAD_NORMAL); |
| 55 request.fetcher->Start(); |
| 52 pending_net_requests_[image_url].swap(&request); | 56 pending_net_requests_[image_url].swap(&request); |
| 53 } else { | 57 } else { |
| 54 // Request in progress. Register as an interested callback. | 58 // Request in progress. Register as an interested callback. |
| 55 it->second.callbacks.push_back(callback); | 59 it->second.callbacks.push_back(callback); |
| 56 } | 60 } |
| 57 | |
| 58 image_data_fetcher_->FetchImageData( | |
| 59 image_url, | |
| 60 base::Bind(&ImageFetcherImpl::OnImageURLFetched, | |
| 61 base::Unretained(this), image_url)); | |
| 62 } | 61 } |
| 63 | 62 |
| 64 void ImageFetcherImpl::OnImageURLFetched(const GURL& image_url, | 63 void ImageFetcherImpl::OnFetchComplete(const GURL& image_url, |
| 65 const std::string& image_data) { | 64 const SkBitmap* bitmap) { |
| 66 // TODO(markusheintz): Add a method OnImageDataFetched on the delegate and | |
| 67 // call that here. | |
| 68 image_decoder_->DecodeImage( | |
| 69 image_data, | |
| 70 base::Bind(&ImageFetcherImpl::OnImageDecoded, | |
| 71 base::Unretained(this), image_url)); | |
| 72 } | |
| 73 | |
| 74 void ImageFetcherImpl::OnImageDecoded(const GURL& image_url, | |
| 75 const gfx::Image& image) { | |
| 76 // Get request for the given image_url from the request queue. | |
| 77 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url); | 65 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url); |
| 78 DCHECK(image_iter != pending_net_requests_.end()); | 66 DCHECK(image_iter != pending_net_requests_.end()); |
| 67 |
| 79 ImageRequest* request = &image_iter->second; | 68 ImageRequest* request = &image_iter->second; |
| 80 | 69 |
| 81 // Run all callbacks | 70 // Here |bitmap| could be NULL. In this case an empty image is passed to the |
| 71 // callbacks and delegate. The pointer to the bitmap which is owned by the |
| 72 // BitmapFetcher ceases to exist after this function. The created gfx::Image |
| 73 // shares the pixels with the |bitmap|. The image is passed to the callbacks |
| 74 // and delegate that are run synchronously. |
| 75 gfx::Image image; |
| 76 if (bitmap != nullptr) |
| 77 image = gfx::Image::CreateFrom1xBitmap(*bitmap); |
| 78 |
| 82 for (const auto& callback : request->callbacks) { | 79 for (const auto& callback : request->callbacks) { |
| 83 callback.Run(request->id, image); | 80 callback.Run(request->id, image); |
| 84 } | 81 } |
| 85 | 82 |
| 86 // Inform the ImageFetcherDelegate. | 83 // Inform the ImageFetcherDelegate. |
| 87 if (delegate_) { | 84 if (delegate_) { |
| 88 delegate_->OnImageFetched(request->id, image); | 85 delegate_->OnImageFetched(request->id, image); |
| 89 } | 86 } |
| 90 | 87 |
| 91 // Erase the completed ImageRequest. | 88 // Erase the completed ImageRequest. |
| 92 pending_net_requests_.erase(image_iter); | 89 pending_net_requests_.erase(image_iter); |
| 93 } | 90 } |
| 94 | 91 |
| 95 } // namespace suggestions | 92 } // namespace suggestions |
| OLD | NEW |