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

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

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