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