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 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
11 #include "components/image_fetcher/image_fetcher_delegate.h" | 11 #include "components/image_fetcher/image_fetcher_delegate.h" |
12 #import "ios/web/public/image_fetcher/image_data_fetcher.h" | 12 #include "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h" |
13 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
14 #include "skia/ext/skia_utils_ios.h" | 14 #include "skia/ext/skia_utils_ios.h" |
15 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
16 | 16 |
17 #if !defined(__has_feature) || !__has_feature(objc_arc) | 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
18 #error "This file requires ARC support." | 18 #error "This file requires ARC support." |
19 #endif | 19 #endif |
20 | 20 |
21 namespace suggestions { | 21 namespace suggestions { |
22 | 22 |
23 ImageFetcherImpl::ImageFetcherImpl( | 23 ImageFetcherImpl::ImageFetcherImpl( |
24 net::URLRequestContextGetter* url_request_context, | 24 net::URLRequestContextGetter* url_request_context, |
25 base::SequencedWorkerPool* blocking_pool) | 25 base::SequencedWorkerPool* blocking_pool) |
26 : image_fetcher_(base::MakeUnique<web::ImageDataFetcher>(blocking_pool)) { | 26 : image_fetcher_( |
27 image_fetcher_->SetRequestContextGetter(url_request_context); | 27 base::MakeUnique<image_fetcher::IOSImageDataFetcherWrapper>( |
28 } | 28 url_request_context, |
| 29 blocking_pool)) {} |
29 | 30 |
30 ImageFetcherImpl::~ImageFetcherImpl() { | 31 ImageFetcherImpl::~ImageFetcherImpl() { |
31 } | 32 } |
32 | 33 |
33 void ImageFetcherImpl::SetImageFetcherDelegate( | 34 void ImageFetcherImpl::SetImageFetcherDelegate( |
34 image_fetcher::ImageFetcherDelegate* delegate) { | 35 image_fetcher::ImageFetcherDelegate* delegate) { |
35 DCHECK(delegate); | 36 DCHECK(delegate); |
36 delegate_ = delegate; | 37 delegate_ = delegate; |
37 } | 38 } |
38 | 39 |
39 void ImageFetcherImpl::SetDataUseServiceName( | 40 void ImageFetcherImpl::SetDataUseServiceName( |
40 DataUseServiceName data_use_service_name) { | 41 DataUseServiceName data_use_service_name) { |
41 // Not implemented - will be obsolete once iOS also uses | 42 image_fetcher_->SetDataUseServiceName(data_use_service_name); |
42 // image_fetcher::ImageDataFetcher. | |
43 } | 43 } |
44 | 44 |
45 void ImageFetcherImpl::StartOrQueueNetworkRequest( | 45 void ImageFetcherImpl::StartOrQueueNetworkRequest( |
46 const std::string& id, | 46 const std::string& id, |
47 const GURL& image_url, | 47 const GURL& image_url, |
48 base::Callback<void(const std::string&, const gfx::Image&)> callback) { | 48 base::Callback<void(const std::string&, const gfx::Image&)> callback) { |
49 if (image_url.is_empty()) { | 49 if (image_url.is_empty()) { |
50 gfx::Image empty_image; | 50 gfx::Image empty_image; |
51 callback.Run(id, empty_image); | 51 callback.Run(id, empty_image); |
52 if (delegate_) { | 52 if (delegate_) { |
53 delegate_->OnImageFetched(id, empty_image); | 53 delegate_->OnImageFetched(id, empty_image); |
54 } | 54 } |
55 return; | 55 return; |
56 } | 56 } |
57 // Copy string reference so it's retained. | 57 // Copy string reference so it's retained. |
58 const std::string fetch_id(id); | 58 const std::string fetch_id(id); |
59 // If image_fetcher_ is destroyed the request will be cancelled and this block | 59 // If image_fetcher_ is destroyed the request will be cancelled and this block |
60 // will never be called. A reference to delegate_ can be kept. | 60 // will never be called. A reference to delegate_ can be kept. |
61 web::ImageFetchedCallback fetcher_callback = | 61 image_fetcher::IOSImageDataFetcherCallback fetcher_callback = |
62 ^(const GURL& original_url, int response_code, NSData* data) { | 62 ^(NSData* data) { |
63 if (data) { | 63 if (data) { |
64 // Most likely always returns 1x images. | 64 // Most likely always returns 1x images. |
65 UIImage* ui_image = [UIImage imageWithData:data scale:1]; | 65 UIImage* ui_image = [UIImage imageWithData:data scale:1]; |
66 if (ui_image) { | 66 if (ui_image) { |
67 gfx::Image gfx_image(ui_image, base::scoped_policy::ASSUME); | 67 gfx::Image gfx_image(ui_image, base::scoped_policy::ASSUME); |
68 callback.Run(fetch_id, gfx_image); | 68 callback.Run(fetch_id, gfx_image); |
69 if (delegate_) { | 69 if (delegate_) { |
70 delegate_->OnImageFetched(fetch_id, gfx_image); | 70 delegate_->OnImageFetched(fetch_id, gfx_image); |
71 } | 71 } |
72 return; | 72 return; |
73 } | 73 } |
74 } | 74 } |
75 gfx::Image empty_image; | 75 gfx::Image empty_image; |
76 callback.Run(fetch_id, empty_image); | 76 callback.Run(fetch_id, empty_image); |
77 if (delegate_) { | 77 if (delegate_) { |
78 delegate_->OnImageFetched(fetch_id, empty_image); | 78 delegate_->OnImageFetched(fetch_id, empty_image); |
79 } | 79 } |
80 }; | 80 }; |
81 image_fetcher_->StartDownload(image_url, fetcher_callback); | 81 image_fetcher_->FetchImageDataWebpDecoded(image_url, fetcher_callback); |
82 } | 82 } |
83 | 83 |
84 } // namespace suggestions | 84 } // namespace suggestions |
OLD | NEW |