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

Side by Side Diff: components/image_fetcher/ios/raw_image_fetcher.mm

Issue 2521253002: Move ImageFetcher to its own target. (Closed)
Patch Set: Cleanup Created 4 years 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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 #import "ios/chrome/browser/net/image_fetcher.h" 5 #import "components/image_fetcher/ios/raw_image_fetcher.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/mac/scoped_nsobject.h" 12 #include "base/mac/scoped_nsobject.h"
13 #include "base/task_runner.h" 13 #include "base/task_runner.h"
14 #include "ios/chrome/browser/webp_transcode/webp_decoder.h" 14 #include "components/image_fetcher/ios/webp_decoder.h"
15 #include "ios/web/public/web_thread.h" 15 #include "ios/web/public/web_thread.h"
16 #include "net/base/load_flags.h" 16 #include "net/base/load_flags.h"
17 #include "net/http/http_response_headers.h" 17 #include "net/http/http_response_headers.h"
18 #include "net/url_request/url_fetcher.h" 18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_request_context_getter.h" 19 #include "net/url_request/url_request_context_getter.h"
20 20
21 namespace { 21 namespace {
22 22
23 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate { 23 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate {
24 public: 24 public:
25 NSData* data() const { return decoded_image_; } 25 NSData* data() const { return decoded_image_; }
26 26
27 // WebpDecoder::Delegate methods 27 // WebpDecoder::Delegate methods
28 void OnFinishedDecoding(bool success) override { 28 void OnFinishedDecoding(bool success) override {
29 if (!success) 29 if (!success)
30 decoded_image_.reset(); 30 decoded_image_.reset();
31 } 31 }
32 void SetImageFeatures( 32 void SetImageFeatures(
33 size_t total_size, 33 size_t total_size,
34 webp_transcode::WebpDecoder::DecodedImageFormat format) override { 34 webp_transcode::WebpDecoder::DecodedImageFormat format) override {
35 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); 35 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]);
36 } 36 }
37 void OnDataDecoded(NSData* data) override { 37 void OnDataDecoded(NSData* data) override {
38 DCHECK(decoded_image_); 38 DCHECK(decoded_image_);
39 [decoded_image_ appendData:data]; 39 [decoded_image_ appendData:data];
40 } 40 }
41
41 private: 42 private:
42 ~WebpDecoderDelegate() override {} 43 ~WebpDecoderDelegate() override {}
43 base::scoped_nsobject<NSMutableData> decoded_image_; 44 base::scoped_nsobject<NSMutableData> decoded_image_;
44 }; 45 };
45 46
46 // Content-type header for WebP images. 47 // Content-type header for WebP images.
47 static const char kWEBPMimeType[] = "image/webp"; 48 static const char kWEBPMimeType[] = "image/webp";
48 49
49 // Returns a NSData object containing the decoded image. 50 // Returns a NSData object containing the decoded image.
50 // Returns nil in case of failure. 51 // Returns nil in case of failure.
51 base::scoped_nsobject<NSData> DecodeWebpImage( 52 base::scoped_nsobject<NSData> DecodeWebpImage(
52 const base::scoped_nsobject<NSData>& webp_image) { 53 const base::scoped_nsobject<NSData>& webp_image) {
53 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); 54 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate);
54 scoped_refptr<webp_transcode::WebpDecoder> decoder( 55 scoped_refptr<webp_transcode::WebpDecoder> decoder(
55 new webp_transcode::WebpDecoder(delegate.get())); 56 new webp_transcode::WebpDecoder(delegate.get()));
56 decoder->OnDataReceived(webp_image); 57 decoder->OnDataReceived(webp_image);
57 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; 58 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed.";
58 return base::scoped_nsobject<NSData>([delegate->data() retain]); 59 return base::scoped_nsobject<NSData>([delegate->data() retain]);
59 } 60 }
60 61
61 } // namespace 62 } // namespace
62 63
63 ImageFetcher::ImageFetcher(const scoped_refptr<base::TaskRunner>& task_runner) 64 namespace image_fetcher {
65
66 RawImageFetcher::RawImageFetcher(
67 const scoped_refptr<base::TaskRunner>& task_runner)
64 : request_context_getter_(nullptr), 68 : request_context_getter_(nullptr),
65 task_runner_(task_runner), 69 task_runner_(task_runner),
66 weak_factory_(this) { 70 weak_factory_(this) {
67 DCHECK(task_runner_.get()); 71 DCHECK(task_runner_.get());
68 } 72 }
69 73
70 ImageFetcher::~ImageFetcher() { 74 RawImageFetcher::~RawImageFetcher() {
71 // Delete all the entries in the |downloads_in_progress_| map. This will in 75 // Delete all the entries in the |downloads_in_progress_| map. This will in
72 // turn cancel all of the requests. 76 // turn cancel all of the requests.
73 for (const auto& pair : downloads_in_progress_) { 77 for (const auto& pair : downloads_in_progress_) {
74 [pair.second release]; 78 [pair.second release];
75 delete pair.first; 79 delete pair.first;
76 } 80 }
77 } 81 }
78 82
79 void ImageFetcher::StartDownload( 83 void RawImageFetcher::StartDownload(
80 const GURL& url, 84 const GURL& url,
81 ImageFetchedCallback callback, 85 ImageFetchedCallback callback,
82 const std::string& referrer, 86 const std::string& referrer,
83 net::URLRequest::ReferrerPolicy referrer_policy) { 87 net::URLRequest::ReferrerPolicy referrer_policy) {
84 DCHECK(request_context_getter_.get()); 88 DCHECK(request_context_getter_.get());
85 net::URLFetcher* fetcher = 89 net::URLFetcher* fetcher =
86 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); 90 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release();
87 downloads_in_progress_[fetcher] = [callback copy]; 91 downloads_in_progress_[fetcher] = [callback copy];
88 fetcher->SetLoadFlags( 92 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
89 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | 93 net::LOAD_DO_NOT_SAVE_COOKIES |
90 net::LOAD_DO_NOT_SEND_AUTH_DATA); 94 net::LOAD_DO_NOT_SEND_AUTH_DATA);
91 fetcher->SetRequestContext(request_context_getter_.get()); 95 fetcher->SetRequestContext(request_context_getter_.get());
92 fetcher->SetReferrer(referrer); 96 fetcher->SetReferrer(referrer);
93 fetcher->SetReferrerPolicy(referrer_policy); 97 fetcher->SetReferrerPolicy(referrer_policy);
94 fetcher->Start(); 98 fetcher->Start();
95 } 99 }
96 100
97 void ImageFetcher::StartDownload( 101 void RawImageFetcher::StartDownload(const GURL& url,
98 const GURL& url, ImageFetchedCallback callback) { 102 ImageFetchedCallback callback) {
99 ImageFetcher::StartDownload( 103 RawImageFetcher::StartDownload(url, callback, std::string(),
100 url, callback, std::string(), net::URLRequest::NEVER_CLEAR_REFERRER); 104 net::URLRequest::NEVER_CLEAR_REFERRER);
101 } 105 }
102 106
103 // Delegate callback that is called when URLFetcher completes. If the image 107 // Delegate callback that is called when URLFetcher completes. If the image
104 // was fetched successfully, creates a new NSData and returns it to the 108 // was fetched successfully, creates a new NSData and returns it to the
105 // callback, otherwise returns nil to the callback. 109 // callback, otherwise returns nil to the callback.
106 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) { 110 void RawImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) {
107 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { 111 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) {
108 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; 112 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher;
109 return; 113 return;
110 } 114 }
111 115
112 // Ensures that |fetcher| will be deleted in the event of early return. 116 // Ensures that |fetcher| will be deleted in the event of early return.
113 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher); 117 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher);
114 118
115 // Retrieves the callback and ensures that it will be deleted in the event 119 // Retrieves the callback and ensures that it will be deleted in the event
116 // of early return. 120 // of early return.
117 base::mac::ScopedBlock<ImageFetchedCallback> callback( 121 base::mac::ScopedBlock<ImageFetchedCallback> callback(
118 downloads_in_progress_[fetcher]); 122 downloads_in_progress_[fetcher]);
119 123
120 // Remove |fetcher| from the map. 124 // Remove |fetcher| from the map.
121 downloads_in_progress_.erase(fetcher); 125 downloads_in_progress_.erase(fetcher);
122 126
123 // Make sure the request was successful. For "data" requests, the response 127 // Make sure the request was successful. For "data" requests, the response
124 // code has no meaning, because there is no actual server (data is encoded 128 // code has no meaning, because there is no actual server (data is encoded
125 // directly in the URL). In that case, set the response code to 200 (OK). 129 // directly in the URL). In that case, set the response code to 200 (OK).
126 const GURL& original_url = fetcher->GetOriginalURL(); 130 const GURL& original_url = fetcher->GetOriginalURL();
127 const int http_response_code = original_url.SchemeIs("data") ? 131 const int http_response_code =
128 200 : fetcher->GetResponseCode(); 132 original_url.SchemeIs("data") ? 200 : fetcher->GetResponseCode();
129 if (http_response_code != 200) { 133 if (http_response_code != 200) {
130 (callback.get())(original_url, http_response_code, nil); 134 (callback.get())(original_url, http_response_code, nil);
131 return; 135 return;
132 } 136 }
133 137
134 std::string response; 138 std::string response;
135 if (!fetcher->GetResponseAsString(&response)) { 139 if (!fetcher->GetResponseAsString(&response)) {
136 (callback.get())(original_url, http_response_code, nil); 140 (callback.get())(original_url, http_response_code, nil);
137 return; 141 return;
138 } 142 }
139 143
140 // Create a NSData from the returned data and notify the callback. 144 // Create a NSData from the returned data and notify the callback.
141 base::scoped_nsobject<NSData> data([[NSData alloc] 145 base::scoped_nsobject<NSData> data([[NSData alloc]
142 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) 146 initWithBytes:reinterpret_cast<const unsigned char*>(response.data())
143 length:response.size()]); 147 length:response.size()]);
144 148
145 if (fetcher->GetResponseHeaders()) { 149 if (fetcher->GetResponseHeaders()) {
146 std::string mime_type; 150 std::string mime_type;
147 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); 151 fetcher->GetResponseHeaders()->GetMimeType(&mime_type);
148 if (mime_type == kWEBPMimeType) { 152 if (mime_type == kWEBPMimeType) {
149 base::PostTaskAndReplyWithResult(task_runner_.get(), 153 base::PostTaskAndReplyWithResult(
150 FROM_HERE, 154 task_runner_.get(), FROM_HERE, base::Bind(&DecodeWebpImage, data),
151 base::Bind(&DecodeWebpImage, data), 155 base::Bind(&RawImageFetcher::RunCallback, weak_factory_.GetWeakPtr(),
152 base::Bind(&ImageFetcher::RunCallback, 156 callback, original_url, http_response_code));
153 weak_factory_.GetWeakPtr(),
154 callback,
155 original_url,
156 http_response_code));
157 return; 157 return;
158 } 158 }
159 } 159 }
160 (callback.get())(original_url, http_response_code, data); 160 (callback.get())(original_url, http_response_code, data);
161 } 161 }
162 162
163 void ImageFetcher::RunCallback( 163 void RawImageFetcher::RunCallback(
164 const base::mac::ScopedBlock<ImageFetchedCallback>& callback, 164 const base::mac::ScopedBlock<ImageFetchedCallback>& callback,
165 const GURL& url, 165 const GURL& url,
166 int http_response_code, 166 int http_response_code,
167 NSData* data) { 167 NSData* data) {
168 (callback.get())(url, http_response_code, data); 168 (callback.get())(url, http_response_code, data);
169 } 169 }
170 170
171 void ImageFetcher::SetRequestContextGetter( 171 void RawImageFetcher::SetRequestContextGetter(
172 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { 172 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) {
173 request_context_getter_ = request_context_getter; 173 request_context_getter_ = request_context_getter;
174 } 174 }
175
176 } // namespace image_fetcher
OLDNEW
« no previous file with comments | « components/image_fetcher/ios/raw_image_fetcher.h ('k') | components/image_fetcher/ios/raw_image_fetcher_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698