| 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 #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/chrome/browser/webp_transcode/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 namespace { | |
| 22 | |
| 23 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate { | |
| 24 public: | |
| 25 NSData* data() const { return decoded_image_; } | |
| 26 | |
| 27 // WebpDecoder::Delegate methods | |
| 28 void OnFinishedDecoding(bool success) override { | |
| 29 if (!success) | |
| 30 decoded_image_.reset(); | |
| 31 } | |
| 32 void SetImageFeatures( | |
| 33 size_t total_size, | |
| 34 webp_transcode::WebpDecoder::DecodedImageFormat format) override { | |
| 35 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); | |
| 36 } | |
| 37 void OnDataDecoded(NSData* data) override { | |
| 38 DCHECK(decoded_image_); | |
| 39 [decoded_image_ appendData:data]; | |
| 40 } | |
| 41 private: | |
| 42 ~WebpDecoderDelegate() override {} | |
| 43 base::scoped_nsobject<NSMutableData> decoded_image_; | |
| 44 }; | |
| 45 | |
| 46 // Content-type header for WebP images. | |
| 47 static const char kWEBPMimeType[] = "image/webp"; | |
| 48 | |
| 49 // Returns a NSData object containing the decoded image. | |
| 50 // Returns nil in case of failure. | |
| 51 base::scoped_nsobject<NSData> DecodeWebpImage( | |
| 52 const base::scoped_nsobject<NSData>& webp_image) { | |
| 53 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); | |
| 54 scoped_refptr<webp_transcode::WebpDecoder> decoder( | |
| 55 new webp_transcode::WebpDecoder(delegate.get())); | |
| 56 decoder->OnDataReceived(webp_image); | |
| 57 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; | |
| 58 return base::scoped_nsobject<NSData>([delegate->data() retain]); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 ImageFetcher::ImageFetcher(const scoped_refptr<base::TaskRunner>& task_runner) | |
| 64 : request_context_getter_(nullptr), | |
| 65 task_runner_(task_runner), | |
| 66 weak_factory_(this) { | |
| 67 DCHECK(task_runner_.get()); | |
| 68 } | |
| 69 | |
| 70 ImageFetcher::~ImageFetcher() { | |
| 71 // Delete all the entries in the |downloads_in_progress_| map. This will in | |
| 72 // turn cancel all of the requests. | |
| 73 for (const auto& pair : downloads_in_progress_) { | |
| 74 [pair.second release]; | |
| 75 delete pair.first; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void ImageFetcher::StartDownload( | |
| 80 const GURL& url, | |
| 81 ImageFetchedCallback callback, | |
| 82 const std::string& referrer, | |
| 83 net::URLRequest::ReferrerPolicy referrer_policy) { | |
| 84 DCHECK(request_context_getter_.get()); | |
| 85 net::URLFetcher* fetcher = | |
| 86 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); | |
| 87 downloads_in_progress_[fetcher] = [callback copy]; | |
| 88 fetcher->SetLoadFlags( | |
| 89 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 90 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
| 91 fetcher->SetRequestContext(request_context_getter_.get()); | |
| 92 fetcher->SetReferrer(referrer); | |
| 93 fetcher->SetReferrerPolicy(referrer_policy); | |
| 94 fetcher->Start(); | |
| 95 } | |
| 96 | |
| 97 void ImageFetcher::StartDownload( | |
| 98 const GURL& url, ImageFetchedCallback callback) { | |
| 99 ImageFetcher::StartDownload( | |
| 100 url, callback, std::string(), net::URLRequest::NEVER_CLEAR_REFERRER); | |
| 101 } | |
| 102 | |
| 103 // 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 // callback, otherwise returns nil to the callback. | |
| 106 void ImageFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) { | |
| 107 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { | |
| 108 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 // Ensures that |fetcher| will be deleted in the event of early return. | |
| 113 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher); | |
| 114 | |
| 115 // Retrieves the callback and ensures that it will be deleted in the event | |
| 116 // of early return. | |
| 117 base::mac::ScopedBlock<ImageFetchedCallback> callback( | |
| 118 downloads_in_progress_[fetcher]); | |
| 119 | |
| 120 // Remove |fetcher| from the map. | |
| 121 downloads_in_progress_.erase(fetcher); | |
| 122 | |
| 123 // 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 // directly in the URL). In that case, set the response code to 200 (OK). | |
| 126 const GURL& original_url = fetcher->GetOriginalURL(); | |
| 127 const int http_response_code = original_url.SchemeIs("data") ? | |
| 128 200 : fetcher->GetResponseCode(); | |
| 129 if (http_response_code != 200) { | |
| 130 (callback.get())(original_url, http_response_code, nil); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 std::string response; | |
| 135 if (!fetcher->GetResponseAsString(&response)) { | |
| 136 (callback.get())(original_url, http_response_code, nil); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 // Create a NSData from the returned data and notify the callback. | |
| 141 base::scoped_nsobject<NSData> data([[NSData alloc] | |
| 142 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) | |
| 143 length:response.size()]); | |
| 144 | |
| 145 if (fetcher->GetResponseHeaders()) { | |
| 146 std::string mime_type; | |
| 147 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); | |
| 148 if (mime_type == kWEBPMimeType) { | |
| 149 base::PostTaskAndReplyWithResult(task_runner_.get(), | |
| 150 FROM_HERE, | |
| 151 base::Bind(&DecodeWebpImage, data), | |
| 152 base::Bind(&ImageFetcher::RunCallback, | |
| 153 weak_factory_.GetWeakPtr(), | |
| 154 callback, | |
| 155 original_url, | |
| 156 http_response_code)); | |
| 157 return; | |
| 158 } | |
| 159 } | |
| 160 (callback.get())(original_url, http_response_code, data); | |
| 161 } | |
| 162 | |
| 163 void ImageFetcher::RunCallback( | |
| 164 const base::mac::ScopedBlock<ImageFetchedCallback>& 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 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { | |
| 173 request_context_getter_ = request_context_getter; | |
| 174 } | |
| OLD | NEW |