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

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

Issue 2652893005: Create the IOSImageDataFetcher (Closed)
Patch Set: Address comments 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.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_(base::MakeUnique<image_fetcher::IOSImageDataFetcher>(
27 image_fetcher_->SetRequestContextGetter(url_request_context); 27 url_request_context,
28 } 28 blocking_pool)) {}
29 29
30 ImageFetcherImpl::~ImageFetcherImpl() { 30 ImageFetcherImpl::~ImageFetcherImpl() {
31 } 31 }
32 32
33 void ImageFetcherImpl::SetImageFetcherDelegate( 33 void ImageFetcherImpl::SetImageFetcherDelegate(
34 image_fetcher::ImageFetcherDelegate* delegate) { 34 image_fetcher::ImageFetcherDelegate* delegate) {
35 DCHECK(delegate); 35 DCHECK(delegate);
36 delegate_ = delegate; 36 delegate_ = delegate;
37 } 37 }
38 38
39 void ImageFetcherImpl::SetDataUseServiceName( 39 void ImageFetcherImpl::SetDataUseServiceName(
40 DataUseServiceName data_use_service_name) { 40 DataUseServiceName data_use_service_name) {
41 // Not implemented - will be obsolete once iOS also uses 41 image_fetcher_->SetDataUseServiceName(data_use_service_name);
42 // image_fetcher::ImageDataFetcher.
43 NOTREACHED();
44 } 42 }
45 43
46 void ImageFetcherImpl::StartOrQueueNetworkRequest( 44 void ImageFetcherImpl::StartOrQueueNetworkRequest(
47 const std::string& id, 45 const std::string& id,
48 const GURL& image_url, 46 const GURL& image_url,
49 base::Callback<void(const std::string&, const gfx::Image&)> callback) { 47 base::Callback<void(const std::string&, const gfx::Image&)> callback) {
50 if (image_url.is_empty()) { 48 if (image_url.is_empty()) {
51 gfx::Image empty_image; 49 gfx::Image empty_image;
52 callback.Run(id, empty_image); 50 callback.Run(id, empty_image);
53 if (delegate_) { 51 if (delegate_) {
54 delegate_->OnImageFetched(id, empty_image); 52 delegate_->OnImageFetched(id, empty_image);
55 } 53 }
56 return; 54 return;
57 } 55 }
58 // Copy string reference so it's retained. 56 // Copy string reference so it's retained.
59 const std::string fetch_id(id); 57 const std::string fetch_id(id);
60 // If image_fetcher_ is destroyed the request will be cancelled and this block 58 // 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. 59 // will never be called. A reference to delegate_ can be kept.
62 web::ImageFetchedCallback fetcher_callback = 60 image_fetcher::IOSImageDataFetcherCallback fetcher_callback =
63 ^(const GURL& original_url, int response_code, NSData* data) { 61 ^(int httpResponseCode, NSData* data) {
64 if (data) { 62 if (data) {
65 // Most likely always returns 1x images. 63 // Most likely always returns 1x images.
66 UIImage* ui_image = [UIImage imageWithData:data scale:1]; 64 UIImage* ui_image = [UIImage imageWithData:data scale:1];
67 if (ui_image) { 65 if (ui_image) {
68 gfx::Image gfx_image(ui_image, base::scoped_policy::ASSUME); 66 gfx::Image gfx_image(ui_image, base::scoped_policy::ASSUME);
69 callback.Run(fetch_id, gfx_image); 67 callback.Run(fetch_id, gfx_image);
70 if (delegate_) { 68 if (delegate_) {
71 delegate_->OnImageFetched(fetch_id, gfx_image); 69 delegate_->OnImageFetched(fetch_id, gfx_image);
72 } 70 }
73 return; 71 return;
74 } 72 }
75 } 73 }
76 gfx::Image empty_image; 74 gfx::Image empty_image;
77 callback.Run(fetch_id, empty_image); 75 callback.Run(fetch_id, empty_image);
78 if (delegate_) { 76 if (delegate_) {
79 delegate_->OnImageFetched(fetch_id, empty_image); 77 delegate_->OnImageFetched(fetch_id, empty_image);
80 } 78 }
81 }; 79 };
82 image_fetcher_->StartDownload(image_url, fetcher_callback); 80 image_fetcher_->FetchImageDataWebpDecoded(image_url, fetcher_callback);
83 } 81 }
84 82
85 } // namespace suggestions 83 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698