OLD | NEW |
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/image_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 "ios/chrome/browser/webp_transcode/webp_decoder.h" |
15 #include "ios/web/public/web_thread.h" | 15 #include "ios/web/public/web_thread.h" |
(...skipping 15 matching lines...) Expand all Loading... |
31 } | 31 } |
32 void SetImageFeatures( | 32 void SetImageFeatures( |
33 size_t total_size, | 33 size_t total_size, |
34 webp_transcode::WebpDecoder::DecodedImageFormat format) override { | 34 webp_transcode::WebpDecoder::DecodedImageFormat format) override { |
35 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); | 35 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); |
36 } | 36 } |
37 void OnDataDecoded(NSData* data) override { | 37 void OnDataDecoded(NSData* data) override { |
38 DCHECK(decoded_image_); | 38 DCHECK(decoded_image_); |
39 [decoded_image_ appendData:data]; | 39 [decoded_image_ appendData:data]; |
40 } | 40 } |
| 41 |
41 private: | 42 private: |
42 ~WebpDecoderDelegate() override {} | 43 ~WebpDecoderDelegate() override {} |
43 base::scoped_nsobject<NSMutableData> decoded_image_; | 44 base::scoped_nsobject<NSMutableData> decoded_image_; |
44 }; | 45 }; |
45 | 46 |
46 // Content-type header for WebP images. | 47 // Content-type header for WebP images. |
47 static const char kWEBPMimeType[] = "image/webp"; | 48 static const char kWEBPMimeType[] = "image/webp"; |
48 | 49 |
49 // Returns a NSData object containing the decoded image. | 50 // Returns a NSData object containing the decoded image. |
50 // Returns nil in case of failure. | 51 // Returns nil in case of failure. |
(...skipping 27 matching lines...) Expand all Loading... |
78 | 79 |
79 void ImageFetcher::StartDownload( | 80 void ImageFetcher::StartDownload( |
80 const GURL& url, | 81 const GURL& url, |
81 ImageFetchedCallback callback, | 82 ImageFetchedCallback callback, |
82 const std::string& referrer, | 83 const std::string& referrer, |
83 net::URLRequest::ReferrerPolicy referrer_policy) { | 84 net::URLRequest::ReferrerPolicy referrer_policy) { |
84 DCHECK(request_context_getter_.get()); | 85 DCHECK(request_context_getter_.get()); |
85 net::URLFetcher* fetcher = | 86 net::URLFetcher* fetcher = |
86 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); | 87 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); |
87 downloads_in_progress_[fetcher] = [callback copy]; | 88 downloads_in_progress_[fetcher] = [callback copy]; |
88 fetcher->SetLoadFlags( | 89 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
89 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | | 90 net::LOAD_DO_NOT_SAVE_COOKIES | |
90 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 91 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
91 fetcher->SetRequestContext(request_context_getter_.get()); | 92 fetcher->SetRequestContext(request_context_getter_.get()); |
92 fetcher->SetReferrer(referrer); | 93 fetcher->SetReferrer(referrer); |
93 fetcher->SetReferrerPolicy(referrer_policy); | 94 fetcher->SetReferrerPolicy(referrer_policy); |
94 fetcher->Start(); | 95 fetcher->Start(); |
95 } | 96 } |
96 | 97 |
97 void ImageFetcher::StartDownload( | 98 void ImageFetcher::StartDownload(const GURL& url, |
98 const GURL& url, ImageFetchedCallback callback) { | 99 ImageFetchedCallback callback) { |
99 ImageFetcher::StartDownload( | 100 ImageFetcher::StartDownload(url, callback, std::string(), |
100 url, callback, std::string(), net::URLRequest::NEVER_CLEAR_REFERRER); | 101 net::URLRequest::NEVER_CLEAR_REFERRER); |
101 } | 102 } |
102 | 103 |
103 // Delegate callback that is called when URLFetcher completes. If the image | 104 // Delegate callback that is called when URLFetcher completes. If the image |
104 // 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 |
105 // callback, otherwise returns nil to the callback. | 106 // callback, otherwise returns nil to the callback. |
106 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) { | 107 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) { |
107 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { | 108 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { |
108 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; | 109 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; |
109 return; | 110 return; |
110 } | 111 } |
111 | 112 |
112 // Ensures that |fetcher| will be deleted in the event of early return. | 113 // Ensures that |fetcher| will be deleted in the event of early return. |
113 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher); | 114 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher); |
114 | 115 |
115 // Retrieves the callback and ensures that it will be deleted in the event | 116 // Retrieves the callback and ensures that it will be deleted in the event |
116 // of early return. | 117 // of early return. |
117 base::mac::ScopedBlock<ImageFetchedCallback> callback( | 118 base::mac::ScopedBlock<ImageFetchedCallback> callback( |
118 downloads_in_progress_[fetcher]); | 119 downloads_in_progress_[fetcher]); |
119 | 120 |
120 // Remove |fetcher| from the map. | 121 // Remove |fetcher| from the map. |
121 downloads_in_progress_.erase(fetcher); | 122 downloads_in_progress_.erase(fetcher); |
122 | 123 |
123 // Make sure the request was successful. For "data" requests, the response | 124 // Make sure the request was successful. For "data" requests, the response |
124 // 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 |
125 // directly in the URL). In that case, set the response code to 200 (OK). | 126 // directly in the URL). In that case, set the response code to 200 (OK). |
126 const GURL& original_url = fetcher->GetOriginalURL(); | 127 const GURL& original_url = fetcher->GetOriginalURL(); |
127 const int http_response_code = original_url.SchemeIs("data") ? | 128 const int http_response_code = |
128 200 : fetcher->GetResponseCode(); | 129 original_url.SchemeIs("data") ? 200 : fetcher->GetResponseCode(); |
129 if (http_response_code != 200) { | 130 if (http_response_code != 200) { |
130 (callback.get())(original_url, http_response_code, nil); | 131 (callback.get())(original_url, http_response_code, nil); |
131 return; | 132 return; |
132 } | 133 } |
133 | 134 |
134 std::string response; | 135 std::string response; |
135 if (!fetcher->GetResponseAsString(&response)) { | 136 if (!fetcher->GetResponseAsString(&response)) { |
136 (callback.get())(original_url, http_response_code, nil); | 137 (callback.get())(original_url, http_response_code, nil); |
137 return; | 138 return; |
138 } | 139 } |
139 | 140 |
140 // Create a NSData from the returned data and notify the callback. | 141 // Create a NSData from the returned data and notify the callback. |
141 base::scoped_nsobject<NSData> data([[NSData alloc] | 142 base::scoped_nsobject<NSData> data([[NSData alloc] |
142 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) | 143 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) |
143 length:response.size()]); | 144 length:response.size()]); |
144 | 145 |
145 if (fetcher->GetResponseHeaders()) { | 146 if (fetcher->GetResponseHeaders()) { |
146 std::string mime_type; | 147 std::string mime_type; |
147 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); | 148 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); |
148 if (mime_type == kWEBPMimeType) { | 149 if (mime_type == kWEBPMimeType) { |
149 base::PostTaskAndReplyWithResult(task_runner_.get(), | 150 base::PostTaskAndReplyWithResult( |
150 FROM_HERE, | 151 task_runner_.get(), FROM_HERE, base::Bind(&DecodeWebpImage, data), |
151 base::Bind(&DecodeWebpImage, data), | 152 base::Bind(&ImageFetcher::RunCallback, weak_factory_.GetWeakPtr(), |
152 base::Bind(&ImageFetcher::RunCallback, | 153 callback, original_url, http_response_code)); |
153 weak_factory_.GetWeakPtr(), | |
154 callback, | |
155 original_url, | |
156 http_response_code)); | |
157 return; | 154 return; |
158 } | 155 } |
159 } | 156 } |
160 (callback.get())(original_url, http_response_code, data); | 157 (callback.get())(original_url, http_response_code, data); |
161 } | 158 } |
162 | 159 |
163 void ImageFetcher::RunCallback( | 160 void ImageFetcher::RunCallback( |
164 const base::mac::ScopedBlock<ImageFetchedCallback>& callback, | 161 const base::mac::ScopedBlock<ImageFetchedCallback>& callback, |
165 const GURL& url, | 162 const GURL& url, |
166 int http_response_code, | 163 int http_response_code, |
167 NSData* data) { | 164 NSData* data) { |
168 (callback.get())(url, http_response_code, data); | 165 (callback.get())(url, http_response_code, data); |
169 } | 166 } |
170 | 167 |
171 void ImageFetcher::SetRequestContextGetter( | 168 void ImageFetcher::SetRequestContextGetter( |
172 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { | 169 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { |
173 request_context_getter_ = request_context_getter; | 170 request_context_getter_ = request_context_getter; |
174 } | 171 } |
OLD | NEW |