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

Side by Side Diff: ios/web/public/image_fetcher/image_data_fetcher.mm

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

Powered by Google App Engine
This is Rietveld 408576698