Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(596)

Side by Side Diff: ios/chrome/browser/suggestions/image_fetcher_impl.mm

Issue 2663213002: Create the IOSImageDataFetcherWrapper (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
Marc Treib 2017/01/31 16:17:25 So is the plan to eventually remove the ImageDataF
gambard 2017/01/31 16:55:54 Yes, the first step is to remove the ImageDataFetc
Marc Treib 2017/01/31 17:25:24 Alright. I don't quite see the path yet; the Image
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 NOTREACHED();
44 } 43 }
45 44
46 void ImageFetcherImpl::StartOrQueueNetworkRequest( 45 void ImageFetcherImpl::StartOrQueueNetworkRequest(
47 const std::string& id, 46 const std::string& id,
48 const GURL& image_url, 47 const GURL& image_url,
49 base::Callback<void(const std::string&, const gfx::Image&)> callback) { 48 base::Callback<void(const std::string&, const gfx::Image&)> callback) {
50 if (image_url.is_empty()) { 49 if (image_url.is_empty()) {
51 gfx::Image empty_image; 50 gfx::Image empty_image;
52 callback.Run(id, empty_image); 51 callback.Run(id, empty_image);
53 if (delegate_) { 52 if (delegate_) {
54 delegate_->OnImageFetched(id, empty_image); 53 delegate_->OnImageFetched(id, empty_image);
55 } 54 }
56 return; 55 return;
57 } 56 }
58 // Copy string reference so it's retained. 57 // Copy string reference so it's retained.
59 const std::string fetch_id(id); 58 const std::string fetch_id(id);
60 // 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
61 // will never be called. A reference to delegate_ can be kept. 60 // will never be called. A reference to delegate_ can be kept.
62 web::ImageFetchedCallback fetcher_callback = 61 image_fetcher::IOSImageDataFetcherCallback fetcher_callback =
63 ^(const GURL& original_url, int response_code, NSData* data) { 62 ^(NSData* data) {
64 if (data) { 63 if (data) {
65 // Most likely always returns 1x images. 64 // Most likely always returns 1x images.
66 UIImage* ui_image = [UIImage imageWithData:data scale:1]; 65 UIImage* ui_image = [UIImage imageWithData:data scale:1];
67 if (ui_image) { 66 if (ui_image) {
68 gfx::Image gfx_image(ui_image, base::scoped_policy::ASSUME); 67 gfx::Image gfx_image(ui_image, base::scoped_policy::ASSUME);
69 callback.Run(fetch_id, gfx_image); 68 callback.Run(fetch_id, gfx_image);
70 if (delegate_) { 69 if (delegate_) {
71 delegate_->OnImageFetched(fetch_id, gfx_image); 70 delegate_->OnImageFetched(fetch_id, gfx_image);
72 } 71 }
73 return; 72 return;
74 } 73 }
75 } 74 }
76 gfx::Image empty_image; 75 gfx::Image empty_image;
77 callback.Run(fetch_id, empty_image); 76 callback.Run(fetch_id, empty_image);
78 if (delegate_) { 77 if (delegate_) {
79 delegate_->OnImageFetched(fetch_id, empty_image); 78 delegate_->OnImageFetched(fetch_id, empty_image);
80 } 79 }
81 }; 80 };
82 image_fetcher_->StartDownload(image_url, fetcher_callback); 81 image_fetcher_->FetchImageDataWebpDecoded(image_url, fetcher_callback);
83 } 82 }
84 83
85 } // namespace suggestions 84 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698