| 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/web/public/image_fetcher/image_data_fetcher.h" | 5 #import "ios/web/public/image_fetcher/image_data_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" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 DCHECK(task_runner_.get()); | 75 DCHECK(task_runner_.get()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 ImageDataFetcher::~ImageDataFetcher() { | 78 ImageDataFetcher::~ImageDataFetcher() { |
| 79 // Delete all the entries in the |downloads_in_progress_| map. This will in | 79 // Delete all the entries in the |downloads_in_progress_| map. This will in |
| 80 // turn cancel all of the requests. | 80 // turn cancel all of the requests. |
| 81 for (const auto& pair : downloads_in_progress_) { | 81 for (const auto& pair : downloads_in_progress_) { |
| 82 delete pair.first; | 82 delete pair.first; |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 | |
| 86 void ImageDataFetcher::StartDownload( | 85 void ImageDataFetcher::StartDownload( |
| 87 const GURL& url, | 86 const GURL& url, |
| 88 ImageFetchedCallback callback, | 87 ImageFetchedCallback callback, |
| 89 const std::string& referrer, | 88 const std::string& referrer, |
| 90 net::URLRequest::ReferrerPolicy referrer_policy) { | 89 net::URLRequest::ReferrerPolicy referrer_policy) { |
| 90 StartDownloadWithMime(url, |
| 91 ^(const GURL& url, int http_response_code, |
| 92 const std::string& mime_type, NSData* data) { |
| 93 callback(url, http_response_code, data); |
| 94 }, |
| 95 referrer, referrer_policy); |
| 96 } |
| 97 |
| 98 void ImageDataFetcher::StartDownloadWithMime( |
| 99 const GURL& url, |
| 100 ImageFetchedCallbackWithMime callback, |
| 101 const std::string& referrer, |
| 102 net::URLRequest::ReferrerPolicy referrer_policy) { |
| 91 DCHECK(request_context_getter_.get()); | 103 DCHECK(request_context_getter_.get()); |
| 92 net::URLFetcher* fetcher = | 104 net::URLFetcher* fetcher = |
| 93 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); | 105 net::URLFetcher::Create(url, net::URLFetcher::GET, this).release(); |
| 94 downloads_in_progress_[fetcher] = [callback copy]; | 106 downloads_in_progress_[fetcher] = [callback copy]; |
| 95 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 107 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 96 net::LOAD_DO_NOT_SAVE_COOKIES | | 108 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 97 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 109 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 98 fetcher->SetRequestContext(request_context_getter_.get()); | 110 fetcher->SetRequestContext(request_context_getter_.get()); |
| 99 fetcher->SetReferrer(referrer); | 111 fetcher->SetReferrer(referrer); |
| 100 fetcher->SetReferrerPolicy(referrer_policy); | 112 fetcher->SetReferrerPolicy(referrer_policy); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 114 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { | 126 if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) { |
| 115 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; | 127 LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher; |
| 116 return; | 128 return; |
| 117 } | 129 } |
| 118 | 130 |
| 119 // Ensures that |fetcher| will be deleted in the event of early return. | 131 // Ensures that |fetcher| will be deleted in the event of early return. |
| 120 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher); | 132 std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher); |
| 121 | 133 |
| 122 // Retrieves the callback and ensures that it will be deleted in the event | 134 // Retrieves the callback and ensures that it will be deleted in the event |
| 123 // of early return. | 135 // of early return. |
| 124 base::mac::ScopedBlock<ImageFetchedCallback> callback( | 136 base::mac::ScopedBlock<ImageFetchedCallbackWithMime> callback( |
| 125 downloads_in_progress_[fetcher]); | 137 downloads_in_progress_[fetcher]); |
| 126 | 138 |
| 127 // Remove |fetcher| from the map. | 139 // Remove |fetcher| from the map. |
| 128 downloads_in_progress_.erase(fetcher); | 140 downloads_in_progress_.erase(fetcher); |
| 129 | 141 |
| 130 // Make sure the request was successful. For "data" requests, the response | 142 // Make sure the request was successful. For "data" requests, the response |
| 131 // code has no meaning, because there is no actual server (data is encoded | 143 // code has no meaning, because there is no actual server (data is encoded |
| 132 // directly in the URL). In that case, set the response code to 200 (OK). | 144 // directly in the URL). In that case, set the response code to 200 (OK). |
| 133 const GURL& original_url = fetcher->GetOriginalURL(); | 145 const GURL& original_url = fetcher->GetOriginalURL(); |
| 134 const int http_response_code = | 146 const int http_response_code = |
| 135 original_url.SchemeIs("data") ? 200 : fetcher->GetResponseCode(); | 147 original_url.SchemeIs("data") ? 200 : fetcher->GetResponseCode(); |
| 136 if (http_response_code != 200) { | 148 if (http_response_code != 200) { |
| 137 (callback.get())(original_url, http_response_code, nil); | 149 (callback.get())(original_url, http_response_code, std::string(), nil); |
| 138 return; | 150 return; |
| 139 } | 151 } |
| 140 | 152 |
| 141 std::string response; | 153 std::string response; |
| 142 if (!fetcher->GetResponseAsString(&response)) { | 154 if (!fetcher->GetResponseAsString(&response)) { |
| 143 (callback.get())(original_url, http_response_code, nil); | 155 (callback.get())(original_url, http_response_code, std::string(), nil); |
| 144 return; | 156 return; |
| 145 } | 157 } |
| 146 | 158 |
| 147 // Create a NSData from the returned data and notify the callback. | 159 // Create a NSData from the returned data and notify the callback. |
| 148 base::scoped_nsobject<NSData> data([[NSData alloc] | 160 base::scoped_nsobject<NSData> data([[NSData alloc] |
| 149 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) | 161 initWithBytes:reinterpret_cast<const unsigned char*>(response.data()) |
| 150 length:response.size()]); | 162 length:response.size()]); |
| 151 | 163 |
| 164 std::string mime_type; |
| 152 if (fetcher->GetResponseHeaders()) { | 165 if (fetcher->GetResponseHeaders()) { |
| 153 std::string mime_type; | |
| 154 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); | 166 fetcher->GetResponseHeaders()->GetMimeType(&mime_type); |
| 155 if (mime_type == kWEBPMimeType) { | 167 if (mime_type == kWEBPMimeType) { |
| 156 base::PostTaskAndReplyWithResult( | 168 base::PostTaskAndReplyWithResult( |
| 157 task_runner_.get(), FROM_HERE, base::Bind(&DecodeWebpImage, data), | 169 task_runner_.get(), FROM_HERE, base::Bind(&DecodeWebpImage, data), |
| 158 base::Bind(&ImageDataFetcher::RunCallback, weak_factory_.GetWeakPtr(), | 170 base::Bind(&ImageDataFetcher::RunCallback, weak_factory_.GetWeakPtr(), |
| 159 callback, original_url, http_response_code)); | 171 callback, original_url, http_response_code, mime_type)); |
| 160 return; | 172 return; |
| 161 } | 173 } |
| 162 } | 174 } |
| 163 (callback.get())(original_url, http_response_code, data); | 175 (callback.get())(original_url, http_response_code, mime_type, data); |
| 164 } | 176 } |
| 165 | 177 |
| 166 void ImageDataFetcher::RunCallback( | 178 void ImageDataFetcher::RunCallback( |
| 167 const base::mac::ScopedBlock<ImageFetchedCallback>& callback, | 179 const base::mac::ScopedBlock<ImageFetchedCallbackWithMime>& callback, |
| 168 const GURL& url, | 180 const GURL& url, |
| 169 int http_response_code, | 181 int http_response_code, |
| 182 const std::string& mime_type, |
| 170 NSData* data) { | 183 NSData* data) { |
| 171 (callback.get())(url, http_response_code, data); | 184 (callback.get())(url, http_response_code, mime_type, data); |
| 172 } | 185 } |
| 173 | 186 |
| 174 void ImageDataFetcher::SetRequestContextGetter( | 187 void ImageDataFetcher::SetRequestContextGetter( |
| 175 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { | 188 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) { |
| 176 request_context_getter_ = request_context_getter; | 189 request_context_getter_ = request_context_getter; |
| 177 } | 190 } |
| 178 | 191 |
| 179 } // namespace web | 192 } // namespace web |
| OLD | NEW |