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

Side by Side Diff: ios/chrome/browser/net/image_fetcher.mm

Issue 2520373002: Remove the old image fetcher (Closed)
Patch Set: Remove the files 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
« no previous file with comments | « ios/chrome/browser/net/image_fetcher.h ('k') | ios/chrome/browser/net/mock_image_fetcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/chrome/browser/net/image_fetcher.h"
6
7 #import <Foundation/Foundation.h>
8 #include <stddef.h>
9
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/task_runner.h"
14 #include "ios/web/public/image_fetcher/webp_decoder.h"
15 #include "ios/web/public/web_thread.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 private:
46 ~WebpDecoderDelegate() override {}
47 base::scoped_nsobject<NSMutableData> decoded_image_;
48 };
49
50 // Content-type header for WebP images.
51 static const char kWEBPMimeType[] = "image/webp";
52
53 // Returns a NSData object containing the decoded image.
54 // Returns nil in case of failure.
55 base::scoped_nsobject<NSData> DecodeWebpImage(
56 const base::scoped_nsobject<NSData>& webp_image) {
57 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate);
58 scoped_refptr<webp_transcode::WebpDecoder> decoder(
59 new webp_transcode::WebpDecoder(delegate.get()));
60 decoder->OnDataReceived(webp_image);
61 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed.";
62 return base::scoped_nsobject<NSData>(delegate->data());
63 }
64
65 } // namespace
66
67 ImageFetcher::ImageFetcher(const scoped_refptr<base::TaskRunner>& task_runner)
68 : request_context_getter_(nullptr),
69 task_runner_(task_runner),
70 weak_factory_(this) {
71 DCHECK(task_runner_.get());
72 }
73
74 ImageFetcher::~ImageFetcher() {
75 // Delete all the entries in the |downloads_in_progress_| map. This will in
76 // turn cancel all of the requests.
77 for (const auto& pair : downloads_in_progress_) {
78 delete pair.first;
79 }
80 }
81
82 void ImageFetcher::StartDownload(
83 const GURL& url,
84 ImageFetchedCallback callback,
85 const std::string& referrer,
86 net::URLRequest::ReferrerPolicy referrer_policy) {
87 DCHECK(request_context_getter_.get());
88 net::URLFetcher* fetcher =
89 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release();
90 downloads_in_progress_[fetcher] = [callback copy];
91 fetcher->SetLoadFlags(
92 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES |
93 net::LOAD_DO_NOT_SEND_AUTH_DATA);
94 fetcher->SetRequestContext(request_context_getter_.get());
95 fetcher->SetReferrer(referrer);
96 fetcher->SetReferrerPolicy(referrer_policy);
97 fetcher->Start();
98 }
99
100 void ImageFetcher::StartDownload(
101 const GURL& url, ImageFetchedCallback callback) {
102 ImageFetcher::StartDownload(
103 url, callback, std::string(), net::URLRequest::NEVER_CLEAR_REFERRER);
104 }
105
106 // Delegate callback that is called when URLFetcher completes. If the image
107 // was fetched successfully, creates a new NSData and returns it to the
108 // callback, otherwise returns nil to the callback.
109 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) {
110 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) {
111 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher;
112 return;
113 }
114
115 // Ensures that |fetcher| will be deleted in the event of early return.
116 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher);
117
118 // Retrieves the callback and ensures that it will be deleted in the event
119 // of early return.
120 base::mac::ScopedBlock<ImageFetchedCallback> callback(
121 downloads_in_progress_[fetcher]);
122
123 // Remove |fetcher| from the map.
124 downloads_in_progress_.erase(fetcher);
125
126 // 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
128 // directly in the URL). In that case, set the response code to 200 (OK).
129 const GURL& original_url = fetcher->GetOriginalURL();
130 const int http_response_code = original_url.SchemeIs("data") ?
131 200 : fetcher->GetResponseCode();
132 if (http_response_code != 200) {
133 (callback.get())(original_url, http_response_code, nil);
134 return;
135 }
136
137 std::string response;
138 if (!fetcher->GetResponseAsString(&response)) {
139 (callback.get())(original_url, http_response_code, nil);
140 return;
141 }
142
143 // Create a NSData from the returned data and notify the callback.
144 base::scoped_nsobject<NSData> data([[NSData alloc]
145 initWithBytes:reinterpret_cast<const unsigned char*>(response.data())
146 length:response.size()]);
147
148 if (fetcher->GetResponseHeaders()) {
149 std::string mime_type;
150 fetcher->GetResponseHeaders()->GetMimeType(&mime_type);
151 if (mime_type == kWEBPMimeType) {
152 base::PostTaskAndReplyWithResult(task_runner_.get(),
153 FROM_HERE,
154 base::Bind(&DecodeWebpImage, data),
155 base::Bind(&ImageFetcher::RunCallback,
156 weak_factory_.GetWeakPtr(),
157 callback,
158 original_url,
159 http_response_code));
160 return;
161 }
162 }
163 (callback.get())(original_url, http_response_code, data);
164 }
165
166 void ImageFetcher::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 ImageFetcher::SetRequestContextGetter(
175 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) {
176 request_context_getter_ = request_context_getter;
177 }
OLDNEW
« no previous file with comments | « ios/chrome/browser/net/image_fetcher.h ('k') | ios/chrome/browser/net/mock_image_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698