OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "ios/chrome/browser/suggestions/ios_image_decoder_impl.h" | 5 #include "ios/chrome/browser/suggestions/ios_image_decoder_impl.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #import "ios/web/public/image_fetcher/webp_decoder.h" | 14 #import "ios/web/public/image_fetcher/webp_decoder.h" |
15 #include "ios/web/public/web_thread.h" | 15 #include "ios/web/public/web_thread.h" |
16 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
17 | 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate { | 24 class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate { |
21 public: | 25 public: |
22 WebpDecoderDelegate() = default; | 26 WebpDecoderDelegate() = default; |
23 | 27 |
24 NSData* data() const { return decoded_image_; } | 28 NSData* data() const { return decoded_image_; } |
25 | 29 |
26 // WebpDecoder::Delegate methods | 30 // WebpDecoder::Delegate methods |
27 void OnFinishedDecoding(bool success) override { | 31 void OnFinishedDecoding(bool success) override { |
28 if (!success) | 32 if (!success) |
29 decoded_image_.reset(); | 33 decoded_image_ = nil; |
30 } | 34 } |
31 | 35 |
32 void SetImageFeatures( | 36 void SetImageFeatures( |
33 size_t total_size, | 37 size_t total_size, |
34 webp_transcode::WebpDecoder::DecodedImageFormat format) override { | 38 webp_transcode::WebpDecoder::DecodedImageFormat format) override { |
35 decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]); | 39 decoded_image_ = [[NSMutableData alloc] initWithCapacity:total_size]; |
36 } | 40 } |
37 | 41 |
38 void OnDataDecoded(NSData* data) override { | 42 void OnDataDecoded(NSData* data) override { |
39 DCHECK(decoded_image_); | 43 DCHECK(decoded_image_); |
40 [decoded_image_ appendData:data]; | 44 [decoded_image_ appendData:data]; |
41 } | 45 } |
42 | 46 |
43 private: | 47 private: |
44 ~WebpDecoderDelegate() override {} | 48 ~WebpDecoderDelegate() override {} |
45 base::scoped_nsobject<NSMutableData> decoded_image_; | 49 NSMutableData* decoded_image_; |
46 | 50 |
47 DISALLOW_COPY_AND_ASSIGN(WebpDecoderDelegate); | 51 DISALLOW_COPY_AND_ASSIGN(WebpDecoderDelegate); |
48 }; | 52 }; |
49 | 53 |
50 // Returns an NSData object containing the decoded image data of the given | 54 // Returns an NSData object containing the decoded image data of the given |
51 // webp_image. Returns nil in case of failure. | 55 // webp_image. Returns nil in case of failure. |
52 base::scoped_nsobject<NSData> DecodeWebpImage( | 56 base::scoped_nsobject<NSData> DecodeWebpImage( |
53 const base::scoped_nsobject<NSData>& webp_image) { | 57 const base::scoped_nsobject<NSData>& webp_image) { |
54 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); | 58 scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate); |
55 scoped_refptr<webp_transcode::WebpDecoder> decoder( | 59 scoped_refptr<webp_transcode::WebpDecoder> decoder( |
56 new webp_transcode::WebpDecoder(delegate.get())); | 60 new webp_transcode::WebpDecoder(delegate.get())); |
57 decoder->OnDataReceived(webp_image); | 61 decoder->OnDataReceived(webp_image); |
58 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; | 62 DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed."; |
59 return base::scoped_nsobject<NSData>([delegate->data() retain]); | 63 return base::scoped_nsobject<NSData>(delegate->data()); |
60 } | 64 } |
61 | 65 |
62 // Returns true if the given image_data is a WebP image. | 66 // Returns true if the given image_data is a WebP image. |
63 // | 67 // |
64 // Every WebP file contains a 12 byte file header in the beginning of the file. | 68 // Every WebP file contains a 12 byte file header in the beginning of the file. |
65 // A WebP file header starts with the four ASCII characters "RIFF". The next | 69 // A WebP file header starts with the four ASCII characters "RIFF". The next |
66 // four bytes contain the image size and the last four header bytes contain the | 70 // four bytes contain the image size and the last four header bytes contain the |
67 // four ASCII characters "WEBP". | 71 // four ASCII characters "WEBP". |
68 // | 72 // |
69 // WebP file header: | 73 // WebP file header: |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 : task_runner_(std::move(task_runner)), weak_factory_(this) { | 117 : task_runner_(std::move(task_runner)), weak_factory_(this) { |
114 DCHECK(task_runner_.get()); | 118 DCHECK(task_runner_.get()); |
115 } | 119 } |
116 | 120 |
117 IOSImageDecoderImpl::~IOSImageDecoderImpl() {} | 121 IOSImageDecoderImpl::~IOSImageDecoderImpl() {} |
118 | 122 |
119 void IOSImageDecoderImpl::DecodeImage( | 123 void IOSImageDecoderImpl::DecodeImage( |
120 const std::string& image_data, | 124 const std::string& image_data, |
121 const image_fetcher::ImageDecodedCallback& callback) { | 125 const image_fetcher::ImageDecodedCallback& callback) { |
122 // Convert the |image_data| std::string to an NSData buffer. | 126 // Convert the |image_data| std::string to an NSData buffer. |
123 base::scoped_nsobject<NSData> data( | 127 base::scoped_nsobject<NSData> data([NSData |
124 [[NSData dataWithBytesNoCopy:const_cast<char*>(image_data.c_str()) | 128 dataWithBytesNoCopy:const_cast<char*>(image_data.c_str()) |
125 length:image_data.length() | 129 length:image_data.length() |
126 freeWhenDone:NO] retain]); | 130 freeWhenDone:NO]); |
127 | 131 |
128 // The WebP image format is not supported by iOS natively. Therefore WebP | 132 // The WebP image format is not supported by iOS natively. Therefore WebP |
129 // images need to be decoded explicitly, | 133 // images need to be decoded explicitly, |
130 if (IsWebpImage(image_data)) { | 134 if (IsWebpImage(image_data)) { |
131 base::PostTaskAndReplyWithResult( | 135 base::PostTaskAndReplyWithResult( |
132 task_runner_.get(), FROM_HERE, base::Bind(&DecodeWebpImage, data), | 136 task_runner_.get(), FROM_HERE, base::Bind(&DecodeWebpImage, data), |
133 base::Bind(&IOSImageDecoderImpl::CreateUIImageAndRunCallback, | 137 base::Bind(&IOSImageDecoderImpl::CreateUIImageAndRunCallback, |
134 weak_factory_.GetWeakPtr(), callback)); | 138 weak_factory_.GetWeakPtr(), callback)); |
135 } else { | 139 } else { |
136 CreateUIImageAndRunCallback(callback, data); | 140 CreateUIImageAndRunCallback(callback, data); |
(...skipping 19 matching lines...) Expand all Loading... |
156 gfx::Image empty_image; | 160 gfx::Image empty_image; |
157 callback.Run(empty_image); | 161 callback.Run(empty_image); |
158 } | 162 } |
159 | 163 |
160 std::unique_ptr<image_fetcher::ImageDecoder> CreateIOSImageDecoder( | 164 std::unique_ptr<image_fetcher::ImageDecoder> CreateIOSImageDecoder( |
161 scoped_refptr<base::TaskRunner> task_runner) { | 165 scoped_refptr<base::TaskRunner> task_runner) { |
162 return base::MakeUnique<IOSImageDecoderImpl>(std::move(task_runner)); | 166 return base::MakeUnique<IOSImageDecoderImpl>(std::move(task_runner)); |
163 } | 167 } |
164 | 168 |
165 } // namespace suggestions | 169 } // namespace suggestions |
OLD | NEW |