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