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 "components/image_fetcher/image_data_fetcher.h" | |
| 13 #import "ios/web/public/image_fetcher/webp_decoder.h" | |
| 14 #include "net/http/http_response_headers.h" | |
| 15 #include "net/http/http_status_code.h" | |
| 16 #include "net/url_request/url_fetcher.h" | |
| 17 #include "url/url_constants.h" | |
| 18 | |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 20 #error "This file requires ARC support." | |
| 21 #endif | |
| 22 | |
| 23 #pragma mark - WebpDecoderDelegate | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate { | |
|
markusheintz_
2017/01/31 15:51:41
Don't need to do this in this CL. Just want to doc
gambard
2017/01/31 16:55:53
Yes, I plan to move this code to a shared location
| |
| 28 public: | |
| 29 NSData* data() const { return decoded_image_; } | |
| 30 | |
| 31 // WebpDecoder::Delegate methods | |
| 32 void OnFinishedDecoding(bool success) override { | |
| 33 if (!success) | |
| 34 decoded_image_.reset(); | |
| 35 } | |
| 36 void SetImageFeatures( | |
| 37 size_t total_size, | |
| 38 webp_transcode::WebpDecoder::DecodedImageFormat format) override { | |
| 39 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); | |
| 40 } | |
| 41 void OnDataDecoded(NSData* data) override { | |
| 42 DCHECK(decoded_image_); | |
| 43 [decoded_image_ appendData:data]; | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 ~WebpDecoderDelegate() override {} | |
| 48 base::scoped_nsobject<NSMutableData> decoded_image_; | |
| 49 }; | |
| 50 | |
| 51 // Content-type header for WebP images. | |
| 52 static const char kWEBPFirstBytes[4] = {'R', 'I', 'F', 'F'}; | |
|
Marc Treib
2017/01/31 16:17:25
Is this enough to accurately identify WebPs? The f
gambard
2017/01/31 16:55:54
When I move the WebpDecoderDelegate to a shared lo
Marc Treib
2017/01/31 17:25:24
Yup, I think that's better :)
markusheintz_
2017/02/01 08:09:28
+1
| |
| 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()); | |
| 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 DCHECK(task_runner_.get()); | |
| 76 image_data_fetcher_ = | |
| 77 base::MakeUnique<ImageDataFetcher>(url_request_context_getter); | |
|
Marc Treib
2017/01/31 16:17:25
This could move into the initializer list above, a
gambard
2017/01/31 16:55:54
Done.
I usually use pointer to use forward declara
Marc Treib
2017/01/31 17:25:24
IIRC, the style guide recommends against using a p
| |
| 78 } | |
| 79 | |
| 80 IOSImageDataFetcherWrapper::~IOSImageDataFetcherWrapper() {} | |
| 81 | |
| 82 void IOSImageDataFetcherWrapper::FetchImageDataWebpDecoded( | |
| 83 const GURL& image_url, | |
| 84 IOSImageDataFetcherCallback callback) { | |
| 85 DCHECK(callback); | |
| 86 if (!callback) | |
|
Marc Treib
2017/01/31 16:17:25
Don't handle DCHECK failures, see https://chromium
gambard
2017/01/31 16:55:54
Done.
| |
| 87 return; | |
| 88 | |
| 89 scoped_refptr<base::TaskRunner> task_runner = task_runner_; | |
| 90 ImageDataFetcher::ImageDataFetcherCallback local_callback = | |
| 91 base::BindBlockArc(^(const std::string& image_data) { | |
| 92 // Create a NSData from the returned data and notify the callback. | |
| 93 NSData* data = | |
| 94 [NSData dataWithBytes:image_data.data() length:image_data.size()]; | |
| 95 | |
| 96 if (data.length < 4) { | |
| 97 callback(data); | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 char firstBytes[4] = {0}; | |
| 102 [data getBytes:&firstBytes length:4]; | |
| 103 | |
| 104 if (!memcmp(firstBytes, kWEBPFirstBytes, 4)) { | |
| 105 // The image is a webp image. | |
| 106 base::PostTaskAndReplyWithResult(task_runner.get(), FROM_HERE, | |
| 107 base::Bind(&DecodeWebpImage, data), | |
| 108 base::BindBlockArc(callback)); | |
| 109 return; | |
| 110 } | |
| 111 callback(data); | |
| 112 }); | |
| 113 image_data_fetcher_->FetchImageData(image_url, local_callback); | |
| 114 } | |
| 115 | |
| 116 void IOSImageDataFetcherWrapper::SetDataUseServiceName( | |
| 117 DataUseServiceName data_use_service_name) { | |
| 118 image_data_fetcher_->SetDataUseServiceName(data_use_service_name); | |
| 119 } | |
| 120 | |
| 121 } // namespace image_fetcher | |
| OLD | NEW |