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 NSData* data() const { return decoded_image_; } | |
| 29 | |
| 30 // WebpDecoder::Delegate methods | |
| 31 void OnFinishedDecoding(bool success) override { | |
| 32 if (!success) | |
| 33 decoded_image_.reset(); | |
| 34 } | |
| 35 void SetImageFeatures( | |
| 36 size_t total_size, | |
| 37 webp_transcode::WebpDecoder::DecodedImageFormat format) override { | |
| 38 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); | |
| 39 } | |
| 40 void OnDataDecoded(NSData* data) override { | |
| 41 DCHECK(decoded_image_); | |
| 42 [decoded_image_ appendData:data]; | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 ~WebpDecoderDelegate() override {} | |
| 47 base::scoped_nsobject<NSMutableData> decoded_image_; | |
|
sdefresne
2017/02/01 09:43:44
Just use a raw Objective-C pointer here, ARC takes
gambard
2017/02/02 10:03:46
Done.
| |
| 48 }; | |
|
sdefresne
2017/02/01 09:43:44
DISALLOW_COPY_AND_ASSIGN(WebpDecoderDelegate)
gambard
2017/02/02 10:03:46
Done.
| |
| 49 | |
| 50 // Content-type header for WebP images. | |
| 51 static const char kWEBPFirstByte[] = "RIFF"; | |
|
sdefresne
2017/02/01 09:43:44
nit: I do not think those are bytes (i.e. one char
gambard
2017/02/02 10:03:46
Done.
| |
| 52 static const char kWEBPSecondByte[] = "WEBP"; | |
|
Marc Treib
2017/01/31 17:25:24
nit: "static" isn't required, since this is inside
sdefresne
2017/02/01 09:43:44
+1, "static" is not required here.
gambard
2017/02/02 10:03:46
Done.
| |
| 53 | |
| 54 // Returns a NSData object containing the decoded image. | |
| 55 // Returns nil in case of failure. | |
| 56 NSData* DecodeWebpImage(NSData* webp_image) { | |
| 57 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); | |
| 58 scoped_refptr<webp_transcode::WebpDecoder> decoder( | |
| 59 new webp_transcode::WebpDecoder(delegate.get())); | |
| 60 decoder->OnDataReceived(webp_image); | |
| 61 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; | |
| 62 return base::scoped_nsobject<NSData>(delegate->data()); | |
|
sdefresne
2017/02/01 09:43:44
If this code were to be build without ARC, then th
gambard
2017/02/02 10:03:46
Done.
Thanks for the explanation!
| |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 #pragma mark - IOSImageDataFetcherWrapper | |
| 68 | |
| 69 namespace image_fetcher { | |
| 70 | |
| 71 IOSImageDataFetcherWrapper::IOSImageDataFetcherWrapper( | |
| 72 net::URLRequestContextGetter* url_request_context_getter, | |
| 73 const scoped_refptr<base::TaskRunner>& task_runner) | |
| 74 : task_runner_(task_runner), | |
| 75 image_data_fetcher_(url_request_context_getter) { | |
| 76 DCHECK(task_runner_.get()); | |
| 77 } | |
| 78 | |
| 79 IOSImageDataFetcherWrapper::~IOSImageDataFetcherWrapper() {} | |
| 80 | |
| 81 void IOSImageDataFetcherWrapper::FetchImageDataWebpDecoded( | |
| 82 const GURL& image_url, | |
| 83 IOSImageDataFetcherCallback callback) { | |
| 84 DCHECK(callback); | |
| 85 | |
| 86 scoped_refptr<base::TaskRunner> task_runner = task_runner_; | |
| 87 ImageDataFetcher::ImageDataFetcherCallback local_callback = | |
| 88 base::BindBlockArc(^(const std::string& image_data) { | |
| 89 // Create a NSData from the returned data and notify the callback. | |
| 90 NSData* data = | |
| 91 [NSData dataWithBytes:image_data.data() length:image_data.size()]; | |
| 92 | |
| 93 if (data.length < 12) { | |
| 94 callback(data); | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 if (image_data.compare(0, 4, kWEBPFirstByte) == 0 && | |
|
sdefresne
2017/02/01 09:43:44
Can you change this test in early return mode too?
gambard
2017/02/02 10:03:46
Done.
| |
| 99 image_data.compare(8, 4, kWEBPSecondByte) == 0) { | |
| 100 // The image is a webp image. | |
| 101 base::PostTaskAndReplyWithResult(task_runner.get(), FROM_HERE, | |
| 102 base::Bind(&DecodeWebpImage, data), | |
| 103 base::BindBlockArc(callback)); | |
| 104 return; | |
| 105 } | |
| 106 callback(data); | |
| 107 }); | |
| 108 image_data_fetcher_.FetchImageData(image_url, local_callback); | |
| 109 } | |
| 110 | |
| 111 void IOSImageDataFetcherWrapper::SetDataUseServiceName( | |
| 112 DataUseServiceName data_use_service_name) { | |
| 113 image_data_fetcher_.SetDataUseServiceName(data_use_service_name); | |
| 114 } | |
| 115 | |
| 116 } // namespace image_fetcher | |
| OLD | NEW |