Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "components/image_fetcher/ios/ios_image_data_fetcher.h" | |
| 6 | |
| 7 #import "base/mac/bind_objc_block.h" | |
| 8 #import "base/mac/scoped_nsobject.h" | |
| 9 #include "base/task_runner.h" | |
| 10 #include "base/task_runner_util.h" | |
| 11 #import "ios/web/public/image_fetcher/webp_decoder.h" | |
| 12 #include "net/http/http_response_headers.h" | |
| 13 #include "net/http/http_status_code.h" | |
| 14 #include "net/url_request/url_fetcher.h" | |
| 15 #include "url/url_constants.h" | |
| 16 | |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 18 #error "This file requires ARC support." | |
| 19 #endif | |
| 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 | |
| 42 private: | |
| 43 ~WebpDecoderDelegate() override {} | |
| 44 base::scoped_nsobject<NSMutableData> decoded_image_; | |
| 45 }; | |
| 46 | |
| 47 // Content-type header for WebP images. | |
| 48 static const char kWEBPMimeType[] = "image/webp"; | |
| 49 | |
| 50 // Returns a NSData object containing the decoded image. | |
| 51 // Returns nil in case of failure. | |
| 52 NSData* DecodeWebpImage(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()); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 namespace image_fetcher { | |
| 64 | |
| 65 IOSImageDataFetcher::IOSImageDataFetcher( | |
| 66 net::URLRequestContextGetter* url_request_context_getter, | |
| 67 const scoped_refptr<base::TaskRunner>& task_runner) | |
| 68 : ImageDataFetcher(url_request_context_getter), task_runner_(task_runner) { | |
| 69 DCHECK(task_runner_.get()); | |
| 70 } | |
| 71 | |
| 72 IOSImageDataFetcher::~IOSImageDataFetcher() {} | |
| 73 | |
| 74 void IOSImageDataFetcher::FetchImageDataWebpDecoded( | |
| 75 const GURL& image_url, | |
| 76 IOSImageDataFetcherCallback callback) { | |
| 77 DCHECK(callback); | |
| 78 if (!callback) | |
| 79 return; | |
| 80 | |
| 81 scoped_refptr<base::TaskRunner> task_runner = task_runner_; | |
| 82 ImageDataFetcherCallback local_callback = base::BindBlockArc( | |
| 83 ^(const std::string& image_data, const net::URLFetcher* source) { | |
| 84 // Make sure the request was successful. For "data" requests, the | |
| 85 // response | |
|
Marc Treib
2017/01/25 10:37:55
nit: misformatted comment
gambard
2017/01/30 13:37:53
Done.
| |
| 86 // code has no meaning, because there is no actual server (data is | |
| 87 // encoded | |
| 88 // directly in the URL). In that case, set the response code to 200 | |
| 89 // (OK). | |
| 90 const GURL& original_url = source->GetOriginalURL(); | |
| 91 const int http_response_code = original_url.SchemeIs(url::kDataScheme) | |
| 92 ? net::HTTP_OK | |
| 93 : source->GetResponseCode(); | |
| 94 if (http_response_code != net::HTTP_OK) { | |
|
Marc Treib
2017/01/25 10:37:55
nit: I think this would be clearer as
if (response
gambard
2017/01/30 13:37:53
I am returning the response_code (I forgot in this
| |
| 95 callback(nil); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 // Use scoped_nsobject to be retained with base::Bind. | |
| 100 // Create a NSData from the returned data and notify the callback. | |
| 101 base::scoped_nsobject<NSData> data([[NSData alloc] | |
| 102 initWithBytes:image_data.data() | |
| 103 length:image_data.size()]); | |
| 104 | |
| 105 if (source->GetResponseHeaders()) { | |
| 106 std::string mime_type; | |
| 107 source->GetResponseHeaders()->GetMimeType(&mime_type); | |
| 108 if (mime_type == kWEBPMimeType) { | |
| 109 base::PostTaskAndReplyWithResult(task_runner.get(), FROM_HERE, | |
| 110 base::Bind(&DecodeWebpImage, data), | |
| 111 base::BindBlockArc(callback)); | |
| 112 return; | |
| 113 } | |
| 114 } | |
| 115 callback(data); | |
| 116 }); | |
| 117 FetchImageData(image_url, local_callback); | |
| 118 } | |
| 119 | |
| 120 } // namespace image_fetcher | |
| OLD | NEW |