OLD | NEW |
---|---|
(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 | |
9 #include "base/bind.h" | |
10 #include "base/compiler_specific.h" | |
11 #include "base/location.h" | |
12 #include "base/logging.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" | |
17 #include "ios/web/public/webp_decoder.h" | |
18 #include "net/base/load_flags.h" | |
19 #include "net/http/http_response_headers.h" | |
20 #include "net/url_request/url_fetcher.h" | |
21 #include "url/gurl.h" | |
22 | |
23 namespace { | |
24 | |
25 class WebpDecoderDelegate : public web::WebpDecoder::Delegate { | |
26 public: | |
27 NSData* data() const { return decoded_image_; } | |
28 | |
29 // WebpDecoder::Delegate methods | |
30 void OnFinishedDecoding(bool success) override { | |
31 if (!success) | |
32 decoded_image_.reset(); | |
33 } | |
34 void SetImageFeatures( | |
35 size_t total_size, | |
36 web::WebpDecoder::DecodedImageFormat format) override { | |
37 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); | |
38 } | |
39 void OnDataDecoded(NSData* data) override { | |
40 DCHECK(decoded_image_); | |
41 [decoded_image_ appendData:data]; | |
42 } | |
43 private: | |
44 ~WebpDecoderDelegate() override {} | |
45 base::scoped_nsobject<NSMutableData> decoded_image_; | |
46 }; | |
47 | |
48 // Returns a NSData object containing the decoded image. | |
49 // Returns nil in case of failure. | |
50 base::scoped_nsobject<NSData> DecodeWebpImage( | |
51 const base::scoped_nsobject<NSData>& webp_image) { | |
52 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); | |
53 scoped_refptr<web::WebpDecoder> decoder(new web::WebpDecoder(delegate.get())); | |
54 decoder->OnDataReceived(webp_image); | |
55 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; | |
56 return base::scoped_nsobject<NSData>([delegate->data() retain]); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 namespace image_fetcher { | |
62 | |
63 ImageFetcher::ImageFetcher( | |
64 const scoped_refptr<base::SequencedWorkerPool> decoding_pool) | |
65 : request_context_getter_(nullptr), | |
66 weak_factory_(this), | |
67 decoding_pool_(decoding_pool) { | |
68 DCHECK(decoding_pool_.get()); | |
69 } | |
70 | |
71 ImageFetcher::~ImageFetcher() { | |
72 // Delete all the entries in the |downloads_in_progress_| map. This will in | |
73 // turn cancel all of the requests. | |
74 for (std::map<const net::URLFetcher*, Callback>::iterator it = | |
75 downloads_in_progress_.begin(); | |
76 it != downloads_in_progress_.end(); ++it) { | |
77 [it->second release]; | |
78 delete it->first; | |
79 } | |
80 } | |
81 | |
82 void ImageFetcher::StartDownload( | |
83 const GURL& url, | |
84 Callback callback, | |
85 const std::string& referrer, | |
86 net::URLRequest::ReferrerPolicy referrer_policy) { | |
87 DCHECK(request_context_getter_.get()); | |
88 net::URLFetcher* fetcher = net::URLFetcher::Create(url, | |
89 net::URLFetcher::GET, | |
90 this); | |
91 downloads_in_progress_[fetcher] = [callback copy]; | |
92 fetcher->SetLoadFlags( | |
93 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | |
Ryan Sleevi
2014/12/12 23:32:01
| net::LOAD_DO_NOT_SEND_AUTH_DATA | net::LOAD_DO_
sdefresne
2014/12/15 13:11:58
Done.
sdefresne
2014/12/15 13:11:58
Done.
| |
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(const GURL& url, Callback callback) { | |
101 ImageFetcher::StartDownload( | |
102 url, callback, "", net::URLRequest::NEVER_CLEAR_REFERRER); | |
Ryan Sleevi
2014/12/12 23:32:01
s/""/std::string()/
sdefresne
2014/12/15 13:11:58
Done.
| |
103 } | |
104 | |
105 // Delegate callback that is called when URLFetcher completes. If the image | |
106 // was fetched successfully, creates a new NSData and returns it to the | |
107 // callback, otherwise returns nil to the callback. | |
108 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) { | |
109 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { | |
110 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; | |
111 return; | |
112 } | |
113 | |
114 // Ensures that |fetcher| will be deleted even if we return early. | |
Ryan Sleevi
2014/12/12 23:32:01
nit: Pronouns in comments considered harmful
http
sdefresne
2014/12/15 13:11:58
Done.
sdefresne
2014/12/15 13:11:58
Done.
| |
115 scoped_ptr<const net::URLFetcher> fetcher_deleter(fetcher); | |
116 | |
117 // Retrieves the callback and ensures that it will be deleted even if we | |
118 // return early. | |
Ryan Sleevi
2014/12/12 23:32:00
https://groups.google.com/a/chromium.org/d/topic/c
sdefresne
2014/12/15 13:11:58
Done.
| |
119 base::mac::ScopedBlock<Callback> callback(downloads_in_progress_[fetcher]); | |
120 | |
121 // Remove |fetcher| from the map. | |
122 downloads_in_progress_.erase(fetcher); | |
123 | |
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 | |
126 // directly in the URL). In that case, we set the response code to 200. | |
Ryan Sleevi
2014/12/12 23:32:01
https://groups.google.com/a/chromium.org/d/topic/c
sdefresne
2014/12/15 13:11:58
Done.
| |
127 const GURL& original_url = fetcher->GetOriginalURL(); | |
128 const int http_response_code = original_url.SchemeIs("data") ? | |
129 200 : fetcher->GetResponseCode(); | |
130 if (http_response_code != 200) { | |
131 (callback.get())(original_url, http_response_code, nil); | |
132 return; | |
133 } | |
134 | |
135 std::string response; | |
136 if (!fetcher->GetResponseAsString(&response)) { | |
137 (callback.get())(original_url, http_response_code, nil); | |
138 return; | |
139 } | |
140 | |
141 // Create a NSData from the returned data and notify the callback. | |
142 base::scoped_nsobject<NSData> data([[NSData alloc] | |
143 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) | |
144 length:response.size()]); | |
145 | |
146 if (fetcher->GetResponseHeaders()) { | |
147 std::string mime_type; | |
148 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); | |
149 if (mime_type == "image/webp") { | |
Ryan Sleevi
2014/12/12 23:32:01
Should the constant be a static const char[] kCons
sdefresne
2014/12/15 13:11:58
Done.
| |
150 base::PostTaskAndReplyWithResult(decoding_pool_.get(), | |
151 FROM_HERE, | |
152 base::Bind(&DecodeWebpImage, data), | |
153 base::Bind(&ImageFetcher::RunCallback, | |
154 weak_factory_.GetWeakPtr(), | |
155 callback, | |
156 original_url, | |
157 http_response_code)); | |
158 return; | |
159 } | |
160 } | |
161 (callback.get())(original_url, http_response_code, data); | |
Ryan Sleevi
2014/12/12 23:32:01
My gut instinct is a little nervous here about avo
| |
162 } | |
163 | |
164 void ImageFetcher::RunCallback(const base::mac::ScopedBlock<Callback>& callback, | |
165 const GURL& url, | |
166 int http_response_code, | |
167 NSData* data) { | |
168 (callback.get())(url, http_response_code, data); | |
169 } | |
170 | |
171 void ImageFetcher::SetRequestContextGetter( | |
172 net::URLRequestContextGetter* request_context_getter) { | |
173 request_context_getter_ = request_context_getter; | |
174 } | |
175 | |
176 } // namespace image_fetcher | |
OLD | NEW |