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