| 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 "ios/chrome/browser/suggestions/image_fetcher_impl.h" | 5 #include "ios/chrome/browser/suggestions/image_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <UIKit/UIKit.h> | 7 #include <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "components/image_fetcher/image_fetcher_delegate.h" | 10 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 11 #include "ios/chrome/browser/net/image_fetcher.h" | 11 #include "ios/chrome/browser/net/image_fetcher.h" |
| 12 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
| 13 #include "skia/ext/skia_utils_ios.h" | 13 #include "skia/ext/skia_utils_ios.h" |
| 14 #include "ui/gfx/image/image.h" |
| 14 | 15 |
| 15 namespace suggestions { | 16 namespace suggestions { |
| 16 | 17 |
| 17 ImageFetcherImpl::ImageFetcherImpl( | 18 ImageFetcherImpl::ImageFetcherImpl( |
| 18 net::URLRequestContextGetter* url_request_context, | 19 net::URLRequestContextGetter* url_request_context, |
| 19 base::SequencedWorkerPool* blocking_pool) { | 20 base::SequencedWorkerPool* blocking_pool) { |
| 20 imageFetcher_.reset(new ::ImageFetcher(blocking_pool)); | 21 imageFetcher_.reset(new ::ImageFetcher(blocking_pool)); |
| 21 imageFetcher_->SetRequestContextGetter(url_request_context); | 22 imageFetcher_->SetRequestContextGetter(url_request_context); |
| 22 } | 23 } |
| 23 | 24 |
| 24 ImageFetcherImpl::~ImageFetcherImpl() { | 25 ImageFetcherImpl::~ImageFetcherImpl() { |
| 25 } | 26 } |
| 26 | 27 |
| 27 void ImageFetcherImpl::SetImageFetcherDelegate( | 28 void ImageFetcherImpl::SetImageFetcherDelegate( |
| 28 image_fetcher::ImageFetcherDelegate* delegate) { | 29 image_fetcher::ImageFetcherDelegate* delegate) { |
| 29 DCHECK(delegate); | 30 DCHECK(delegate); |
| 30 delegate_ = delegate; | 31 delegate_ = delegate; |
| 31 } | 32 } |
| 32 | 33 |
| 33 void ImageFetcherImpl::StartOrQueueNetworkRequest( | 34 void ImageFetcherImpl::StartOrQueueNetworkRequest( |
| 34 const GURL& url, | 35 const GURL& url, |
| 35 const GURL& image_url, | 36 const GURL& image_url, |
| 36 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | 37 base::Callback<void(const GURL&, const gfx::Image&)> callback) { |
| 37 if (image_url.is_empty()) { | 38 if (image_url.is_empty()) { |
| 38 callback.Run(url, nullptr); | 39 gfx::Image empty_image; |
| 40 callback.Run(url, empty_image); |
| 39 if (delegate_) { | 41 if (delegate_) { |
| 40 delegate_->OnImageFetched(url, nullptr); | 42 delegate_->OnImageFetched(url, empty_image); |
| 41 } | 43 } |
| 42 return; | 44 return; |
| 43 } | 45 } |
| 44 // Copy url reference so it's retained. | 46 // Copy url reference so it's retained. |
| 45 const GURL page_url(url); | 47 const GURL page_url(url); |
| 46 ImageFetchedCallback fetcher_callback = | 48 ImageFetchedCallback fetcher_callback = |
| 47 ^(const GURL& original_url, int response_code, NSData* data) { | 49 ^(const GURL& original_url, int response_code, NSData* data) { |
| 48 if (data) { | 50 if (data) { |
| 49 // Most likely always returns 1x images. | 51 // Most likely always returns 1x images. |
| 50 UIImage* image = [UIImage imageWithData:data scale:1]; | 52 UIImage* ui_image = [UIImage imageWithData:data scale:1]; |
| 51 if (image) { | 53 if (ui_image) { |
| 52 SkBitmap bitmap = | 54 gfx::Image gfx_image(ui_image); |
| 53 skia::CGImageToSkBitmap(image.CGImage, [image size], YES); | 55 callback.Run(page_url, gfx_image); |
| 54 callback.Run(page_url, &bitmap); | |
| 55 if (delegate_) { | 56 if (delegate_) { |
| 56 delegate_->OnImageFetched(page_url, &bitmap); | 57 delegate_->OnImageFetched(page_url, gfx_image); |
| 57 } | 58 } |
| 58 return; | 59 return; |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 callback.Run(page_url, nullptr); | 62 gfx::Image empty_image; |
| 63 callback.Run(page_url, empty_image); |
| 62 if (delegate_) { | 64 if (delegate_) { |
| 63 delegate_->OnImageFetched(page_url, nullptr); | 65 delegate_->OnImageFetched(page_url, empty_image); |
| 64 } | 66 } |
| 65 }; | 67 }; |
| 66 imageFetcher_->StartDownload(image_url, fetcher_callback); | 68 imageFetcher_->StartDownload(image_url, fetcher_callback); |
| 67 } | 69 } |
| 68 | 70 |
| 69 } // namespace suggestions | 71 } // namespace suggestions |
| OLD | NEW |