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

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

Issue 805713004: Cleanup image_fetcher::ImageFetcher (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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/chrome/browser/net/image_fetcher.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 8
9 #include "base/bind.h"
droger 2014/12/15 13:23:25 Looks like bind.h is still needed.
sdefresne 2014/12/15 13:43:32 Done.
10 #include "base/compiler_specific.h"
11 #include "base/location.h" 9 #include "base/location.h"
12 #include "base/logging.h" 10 #include "base/mac/scoped_nsobject.h"
13 #include "base/mac/scoped_block.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/task_runner_util.h"
16 #include "base/threading/sequenced_worker_pool.h" 11 #include "base/threading/sequenced_worker_pool.h"
12 #include "ios/web/public/web_thread.h"
17 #include "ios/web/public/webp_decoder.h" 13 #include "ios/web/public/webp_decoder.h"
18 #include "net/base/load_flags.h" 14 #include "net/base/load_flags.h"
19 #include "net/http/http_response_headers.h" 15 #include "net/http/http_response_headers.h"
20 #include "net/url_request/url_fetcher.h" 16 #include "net/url_request/url_fetcher.h"
21 #include "url/gurl.h" 17 #include "net/url_request/url_request_context_getter.h"
22 18
23 namespace { 19 namespace {
24 20
25 class WebpDecoderDelegate : public web::WebpDecoder::Delegate { 21 class WebpDecoderDelegate : public web::WebpDecoder::Delegate {
26 public: 22 public:
27 NSData* data() const { return decoded_image_; } 23 NSData* data() const { return decoded_image_; }
28 24
29 // WebpDecoder::Delegate methods 25 // WebpDecoder::Delegate methods
30 void OnFinishedDecoding(bool success) override { 26 void OnFinishedDecoding(bool success) override {
31 if (!success) 27 if (!success)
(...skipping 17 matching lines...) Expand all
49 // Returns nil in case of failure. 45 // Returns nil in case of failure.
50 base::scoped_nsobject<NSData> DecodeWebpImage( 46 base::scoped_nsobject<NSData> DecodeWebpImage(
51 const base::scoped_nsobject<NSData>& webp_image) { 47 const base::scoped_nsobject<NSData>& webp_image) {
52 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); 48 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate);
53 scoped_refptr<web::WebpDecoder> decoder(new web::WebpDecoder(delegate.get())); 49 scoped_refptr<web::WebpDecoder> decoder(new web::WebpDecoder(delegate.get()));
54 decoder->OnDataReceived(webp_image); 50 decoder->OnDataReceived(webp_image);
55 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; 51 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed.";
56 return base::scoped_nsobject<NSData>([delegate->data() retain]); 52 return base::scoped_nsobject<NSData>([delegate->data() retain]);
57 } 53 }
58 54
55 // Content-type header for WebP images.
56 static const char kWEBPMimeType[] = "image/webp";
droger 2014/12/15 13:23:25 The constant should be above the functions.
sdefresne 2014/12/15 13:43:32 Done.
57
59 } // namespace 58 } // namespace
60 59
61 namespace image_fetcher { 60 namespace image_fetcher {
62 61
63 ImageFetcher::ImageFetcher( 62 ImageFetcher::ImageFetcher(
64 const scoped_refptr<base::SequencedWorkerPool> decoding_pool) 63 const scoped_refptr<base::SequencedWorkerPool>& decoding_pool)
65 : request_context_getter_(nullptr), 64 : request_context_getter_(nullptr),
66 weak_factory_(this), 65 weak_factory_(this),
67 decoding_pool_(decoding_pool) { 66 decoding_pool_(decoding_pool) {
68 DCHECK(decoding_pool_.get()); 67 DCHECK(decoding_pool_.get());
69 } 68 }
70 69
71 ImageFetcher::~ImageFetcher() { 70 ImageFetcher::~ImageFetcher() {
72 // Delete all the entries in the |downloads_in_progress_| map. This will in 71 // Delete all the entries in the |downloads_in_progress_| map. This will in
73 // turn cancel all of the requests. 72 // turn cancel all of the requests.
74 for (std::map<const net::URLFetcher*, Callback>::iterator it = 73 for (const auto& pair : downloads_in_progress_) {
75 downloads_in_progress_.begin(); 74 [pair.second release];
76 it != downloads_in_progress_.end(); ++it) { 75 delete pair.first;
77 [it->second release];
78 delete it->first;
79 } 76 }
80 } 77 }
81 78
82 void ImageFetcher::StartDownload( 79 void ImageFetcher::StartDownload(
83 const GURL& url, 80 const GURL& url,
84 Callback callback, 81 ImageFetchedCallback callback,
85 const std::string& referrer, 82 const std::string& referrer,
86 net::URLRequest::ReferrerPolicy referrer_policy) { 83 net::URLRequest::ReferrerPolicy referrer_policy) {
87 DCHECK(request_context_getter_.get()); 84 DCHECK(request_context_getter_.get());
88 net::URLFetcher* fetcher = net::URLFetcher::Create(url, 85 net::URLFetcher* fetcher = net::URLFetcher::Create(url,
89 net::URLFetcher::GET, 86 net::URLFetcher::GET,
90 this); 87 this);
91 downloads_in_progress_[fetcher] = [callback copy]; 88 downloads_in_progress_[fetcher] = [callback copy];
92 fetcher->SetLoadFlags( 89 fetcher->SetLoadFlags(
93 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); 90 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES |
91 net::LOAD_DO_NOT_SEND_AUTH_DATA | net::LOAD_DO_NOT_PROMPT_FOR_LOGIN);
94 fetcher->SetRequestContext(request_context_getter_.get()); 92 fetcher->SetRequestContext(request_context_getter_.get());
95 fetcher->SetReferrer(referrer); 93 fetcher->SetReferrer(referrer);
96 fetcher->SetReferrerPolicy(referrer_policy); 94 fetcher->SetReferrerPolicy(referrer_policy);
97 fetcher->Start(); 95 fetcher->Start();
98 } 96 }
99 97
100 void ImageFetcher::StartDownload(const GURL& url, Callback callback) { 98 void ImageFetcher::StartDownload(
99 const GURL& url, ImageFetchedCallback callback) {
101 ImageFetcher::StartDownload( 100 ImageFetcher::StartDownload(
102 url, callback, "", net::URLRequest::NEVER_CLEAR_REFERRER); 101 url, callback, std::string(), net::URLRequest::NEVER_CLEAR_REFERRER);
103 } 102 }
104 103
105 // Delegate callback that is called when URLFetcher completes. If the image 104 // Delegate callback that is called when URLFetcher completes. If the image
106 // was fetched successfully, creates a new NSData and returns it to the 105 // was fetched successfully, creates a new NSData and returns it to the
107 // callback, otherwise returns nil to the callback. 106 // callback, otherwise returns nil to the callback.
108 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) { 107 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) {
109 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { 108 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) {
110 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; 109 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher;
111 return; 110 return;
112 } 111 }
113 112
114 // Ensures that |fetcher| will be deleted even if we return early. 113 // Ensures that |fetcher| will be deleted in the event of early return.
115 scoped_ptr<const net::URLFetcher> fetcher_deleter(fetcher); 114 scoped_ptr<const net::URLFetcher> fetcher_deleter(fetcher);
116 115
117 // Retrieves the callback and ensures that it will be deleted even if we 116 // Retrieves the callback and ensures that it will be deleted in the event
118 // return early. 117 // of early return.
119 base::mac::ScopedBlock<Callback> callback(downloads_in_progress_[fetcher]); 118 base::mac::ScopedBlock<ImageFetchedCallback> callback(
119 downloads_in_progress_[fetcher]);
120 120
121 // Remove |fetcher| from the map. 121 // Remove |fetcher| from the map.
122 downloads_in_progress_.erase(fetcher); 122 downloads_in_progress_.erase(fetcher);
123 123
124 // Make sure the request was successful. For "data" requests, the response 124 // Make sure the request was successful. For "data" requests, the response
125 // code has no meaning, because there is no actual server (data is encoded 125 // code has no meaning, because there is no actual server (data is encoded
126 // directly in the URL). In that case, we set the response code to 200. 126 // directly in the URL). In that case, set the response code to 200 (OK).
127 const GURL& original_url = fetcher->GetOriginalURL(); 127 const GURL& original_url = fetcher->GetOriginalURL();
128 const int http_response_code = original_url.SchemeIs("data") ? 128 const int http_response_code = original_url.SchemeIs("data") ?
129 200 : fetcher->GetResponseCode(); 129 200 : fetcher->GetResponseCode();
130 if (http_response_code != 200) { 130 if (http_response_code != 200) {
131 (callback.get())(original_url, http_response_code, nil); 131 (callback.get())(original_url, http_response_code, nil);
132 return; 132 return;
133 } 133 }
134 134
135 std::string response; 135 std::string response;
136 if (!fetcher->GetResponseAsString(&response)) { 136 if (!fetcher->GetResponseAsString(&response)) {
137 (callback.get())(original_url, http_response_code, nil); 137 (callback.get())(original_url, http_response_code, nil);
138 return; 138 return;
139 } 139 }
140 140
141 // Create a NSData from the returned data and notify the callback. 141 // Create a NSData from the returned data and notify the callback.
142 base::scoped_nsobject<NSData> data([[NSData alloc] 142 base::scoped_nsobject<NSData> data([[NSData alloc]
143 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) 143 initWithBytes:reinterpret_cast<const unsigned char*>(response.data())
144 length:response.size()]); 144 length:response.size()]);
145 145
146 if (fetcher->GetResponseHeaders()) { 146 if (fetcher->GetResponseHeaders()) {
147 std::string mime_type; 147 std::string mime_type;
148 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); 148 fetcher->GetResponseHeaders()->GetMimeType(&mime_type);
149 if (mime_type == "image/webp") { 149 if (mime_type == kWEBPMimeType) {
150 base::PostTaskAndReplyWithResult(decoding_pool_.get(), 150 base::PostTaskAndReplyWithResult(decoding_pool_.get(),
151 FROM_HERE, 151 FROM_HERE,
152 base::Bind(&DecodeWebpImage, data), 152 base::Bind(&DecodeWebpImage, data),
153 base::Bind(&ImageFetcher::RunCallback, 153 base::Bind(&ImageFetcher::RunCallback,
154 weak_factory_.GetWeakPtr(), 154 weak_factory_.GetWeakPtr(),
155 callback, 155 callback,
156 original_url, 156 original_url,
157 http_response_code)); 157 http_response_code));
158 return; 158 return;
159 } 159 }
160 } 160 }
161 (callback.get())(original_url, http_response_code, data); 161 (callback.get())(original_url, http_response_code, data);
162 } 162 }
163 163
164 void ImageFetcher::RunCallback(const base::mac::ScopedBlock<Callback>& callback, 164 void ImageFetcher::RunCallback(
165 const GURL& url, 165 const base::mac::ScopedBlock<ImageFetchedCallback>& callback,
166 int http_response_code, 166 const GURL& url,
167 NSData* data) { 167 int http_response_code,
168 NSData* data) {
168 (callback.get())(url, http_response_code, data); 169 (callback.get())(url, http_response_code, data);
169 } 170 }
170 171
171 void ImageFetcher::SetRequestContextGetter( 172 void ImageFetcher::SetRequestContextGetter(
172 net::URLRequestContextGetter* request_context_getter) { 173 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) {
173 request_context_getter_ = request_context_getter; 174 request_context_getter_ = request_context_getter;
174 } 175 }
175 176
176 } // namespace image_fetcher 177 } // namespace image_fetcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698