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_wrapper.h" | |
| 6 | |
| 7 #import "base/mac/bind_objc_block.h" | |
| 8 #import "base/mac/scoped_nsobject.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/task_runner.h" | |
| 11 #include "base/task_runner_util.h" | |
| 12 #import "ios/web/public/image_fetcher/webp_decoder.h" | |
| 13 #include "net/http/http_response_headers.h" | |
| 14 #include "net/http/http_status_code.h" | |
| 15 #include "net/url_request/url_fetcher.h" | |
| 16 #include "url/url_constants.h" | |
| 17 | |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 19 #error "This file requires ARC support." | |
| 20 #endif | |
| 21 | |
| 22 #pragma mark - WebpDecoderDelegate | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate { | |
| 27 public: | |
| 28 WebpDecoderDelegate() = default; | |
| 29 | |
| 30 NSData* data() const { return decoded_image_; } | |
| 31 | |
| 32 // WebpDecoder::Delegate methods | |
| 33 void OnFinishedDecoding(bool success) override { | |
| 34 if (!success) | |
| 35 decoded_image_ = nil; | |
| 36 } | |
| 37 void SetImageFeatures( | |
| 38 size_t total_size, | |
| 39 webp_transcode::WebpDecoder::DecodedImageFormat format) override { | |
| 40 decoded_image_ = [[NSMutableData alloc] initWithCapacity:total_size]; | |
| 41 } | |
| 42 void OnDataDecoded(NSData* data) override { | |
| 43 DCHECK(decoded_image_); | |
| 44 [decoded_image_ appendData:data]; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 ~WebpDecoderDelegate() override {} | |
|
sdefresne
2017/02/02 10:42:05
nit: use "= default" here too for consistency
gambard
2017/02/02 11:57:02
Done.
| |
| 49 NSMutableData* decoded_image_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(WebpDecoderDelegate); | |
| 52 }; | |
| 53 | |
| 54 // Content-type header for WebP images. | |
| 55 const char kWEBPFirstMagicPattern[] = "RIFF"; | |
| 56 const char kWEBPSecondMagicPattern[] = "WEBP"; | |
| 57 | |
| 58 // Returns a NSData object containing the decoded image. | |
| 59 // Returns nil in case of failure. | |
| 60 NSData* DecodeWebpImage(NSData* webp_image) { | |
| 61 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); | |
| 62 scoped_refptr<webp_transcode::WebpDecoder> decoder( | |
| 63 new webp_transcode::WebpDecoder(delegate.get())); | |
| 64 decoder->OnDataReceived(webp_image); | |
| 65 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; | |
| 66 return delegate->data(); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 #pragma mark - IOSImageDataFetcherWrapper | |
| 72 | |
| 73 namespace image_fetcher { | |
| 74 | |
| 75 IOSImageDataFetcherWrapper::IOSImageDataFetcherWrapper( | |
| 76 net::URLRequestContextGetter* url_request_context_getter, | |
| 77 const scoped_refptr<base::TaskRunner>& task_runner) | |
| 78 : task_runner_(task_runner), | |
| 79 image_data_fetcher_(url_request_context_getter) { | |
| 80 DCHECK(task_runner_.get()); | |
| 81 } | |
| 82 | |
| 83 IOSImageDataFetcherWrapper::~IOSImageDataFetcherWrapper() {} | |
| 84 | |
| 85 void IOSImageDataFetcherWrapper::FetchImageDataWebpDecoded( | |
| 86 const GURL& image_url, | |
| 87 IOSImageDataFetcherCallback callback) { | |
| 88 DCHECK(callback); | |
| 89 | |
| 90 scoped_refptr<base::TaskRunner> task_runner = task_runner_; | |
| 91 ImageDataFetcher::ImageDataFetcherCallback local_callback = | |
| 92 base::BindBlockArc(^(const std::string& image_data) { | |
| 93 // Create a NSData from the returned data and notify the callback. | |
| 94 NSData* data = | |
| 95 [NSData dataWithBytes:image_data.data() length:image_data.size()]; | |
| 96 | |
| 97 if (data.length < 12 || | |
| 98 image_data.compare(0, 4, kWEBPFirstMagicPattern) != 0 || | |
| 99 image_data.compare(8, 4, kWEBPSecondMagicPattern) != 0) { | |
| 100 callback(data); | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 // The image is a webp image. | |
| 105 base::PostTaskAndReplyWithResult(task_runner.get(), FROM_HERE, | |
| 106 base::Bind(&DecodeWebpImage, data), | |
| 107 base::BindBlockArc(callback)); | |
| 108 }); | |
| 109 image_data_fetcher_.FetchImageData(image_url, local_callback); | |
| 110 } | |
| 111 | |
| 112 void IOSImageDataFetcherWrapper::SetDataUseServiceName( | |
| 113 DataUseServiceName data_use_service_name) { | |
| 114 image_data_fetcher_.SetDataUseServiceName(data_use_service_name); | |
| 115 } | |
| 116 | |
| 117 } // namespace image_fetcher | |
| OLD | NEW |