| 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 "components/image_fetcher/ios/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 #import "base/mac/bind_objc_block.h" | 10 #import "base/mac/bind_objc_block.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 "components/image_fetcher/ios/webp_decoder.h" | 14 #import "components/image_fetcher/ios/webp_decoder.h" |
| 15 #include "ios/web/public/web_thread.h" | 15 #include "ios/web/public/web_thread.h" |
| 16 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
| 17 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
| 18 | 18 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) | 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." | 20 #error "This file requires ARC support." |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 namespace suggestions { | 23 namespace image_fetcher { |
| 24 | 24 |
| 25 class IOSImageDecoderImpl : public image_fetcher::ImageDecoder { | 25 class IOSImageDecoderImpl : public ImageDecoder { |
| 26 public: | 26 public: |
| 27 explicit IOSImageDecoderImpl(scoped_refptr<base::TaskRunner> task_runner); | 27 explicit IOSImageDecoderImpl(scoped_refptr<base::TaskRunner> task_runner); |
| 28 ~IOSImageDecoderImpl() override; | 28 ~IOSImageDecoderImpl() override; |
| 29 | 29 |
| 30 // Note, that |desired_image_frame_size| is not supported | 30 // Note, that |desired_image_frame_size| is not supported |
| 31 // (http://crbug/697596). | 31 // (http://crbug/697596). |
| 32 void DecodeImage( | 32 void DecodeImage(const std::string& image_data, |
| 33 const std::string& image_data, | 33 const gfx::Size& desired_image_frame_size, |
| 34 const gfx::Size& desired_image_frame_size, | 34 const ImageDecodedCallback& callback) override; |
| 35 const image_fetcher::ImageDecodedCallback& callback) override; | |
| 36 | 35 |
| 37 private: | 36 private: |
| 38 void CreateUIImageAndRunCallback( | 37 void CreateUIImageAndRunCallback(const ImageDecodedCallback& callback, |
| 39 const image_fetcher::ImageDecodedCallback& callback, | 38 NSData* image_data); |
| 40 NSData* image_data); | |
| 41 | 39 |
| 42 // The task runner used to decode images if necessary. | 40 // The task runner used to decode images if necessary. |
| 43 const scoped_refptr<base::TaskRunner> task_runner_; | 41 const scoped_refptr<base::TaskRunner> task_runner_; |
| 44 | 42 |
| 45 // The WeakPtrFactory is used to cancel callbacks if ImageFetcher is | 43 // The WeakPtrFactory is used to cancel callbacks if ImageFetcher is |
| 46 // destroyed during WebP decoding. | 44 // destroyed during WebP decoding. |
| 47 base::WeakPtrFactory<IOSImageDecoderImpl> weak_factory_; | 45 base::WeakPtrFactory<IOSImageDecoderImpl> weak_factory_; |
| 48 | 46 |
| 49 DISALLOW_COPY_AND_ASSIGN(IOSImageDecoderImpl); | 47 DISALLOW_COPY_AND_ASSIGN(IOSImageDecoderImpl); |
| 50 }; | 48 }; |
| 51 | 49 |
| 52 IOSImageDecoderImpl::IOSImageDecoderImpl( | 50 IOSImageDecoderImpl::IOSImageDecoderImpl( |
| 53 scoped_refptr<base::TaskRunner> task_runner) | 51 scoped_refptr<base::TaskRunner> task_runner) |
| 54 : task_runner_(std::move(task_runner)), weak_factory_(this) { | 52 : task_runner_(std::move(task_runner)), weak_factory_(this) { |
| 55 DCHECK(task_runner_.get()); | 53 DCHECK(task_runner_.get()); |
| 56 } | 54 } |
| 57 | 55 |
| 58 IOSImageDecoderImpl::~IOSImageDecoderImpl() {} | 56 IOSImageDecoderImpl::~IOSImageDecoderImpl() {} |
| 59 | 57 |
| 60 void IOSImageDecoderImpl::DecodeImage( | 58 void IOSImageDecoderImpl::DecodeImage(const std::string& image_data, |
| 61 const std::string& image_data, | 59 const gfx::Size& desired_image_frame_size, |
| 62 const gfx::Size& desired_image_frame_size, | 60 const ImageDecodedCallback& callback) { |
| 63 const image_fetcher::ImageDecodedCallback& callback) { | |
| 64 // Convert the |image_data| std::string to an NSData buffer. | 61 // Convert the |image_data| std::string to an NSData buffer. |
| 65 // The data is copied as it may have to outlive the caller in | 62 // The data is copied as it may have to outlive the caller in |
| 66 // PostTaskAndReplyWithResult. | 63 // PostTaskAndReplyWithResult. |
| 67 NSData* data = | 64 NSData* data = |
| 68 [NSData dataWithBytes:image_data.data() length:image_data.size()]; | 65 [NSData dataWithBytes:image_data.data() length:image_data.size()]; |
| 69 | 66 |
| 70 // The WebP image format is not supported by iOS natively. Therefore WebP | 67 // The WebP image format is not supported by iOS natively. Therefore WebP |
| 71 // images need to be decoded explicitly, | 68 // images need to be decoded explicitly, |
| 72 if (webp_transcode::WebpDecoder::IsWebpImage(image_data)) { | 69 if (webp_transcode::WebpDecoder::IsWebpImage(image_data)) { |
| 73 base::PostTaskAndReplyWithResult( | 70 base::PostTaskAndReplyWithResult( |
| 74 task_runner_.get(), FROM_HERE, base::BindBlockArc(^NSData*() { | 71 task_runner_.get(), FROM_HERE, base::BindBlockArc(^NSData*() { |
| 75 return webp_transcode::WebpDecoder::DecodeWebpImage(data); | 72 return webp_transcode::WebpDecoder::DecodeWebpImage(data); |
| 76 }), | 73 }), |
| 77 base::Bind(&IOSImageDecoderImpl::CreateUIImageAndRunCallback, | 74 base::Bind(&IOSImageDecoderImpl::CreateUIImageAndRunCallback, |
| 78 weak_factory_.GetWeakPtr(), callback)); | 75 weak_factory_.GetWeakPtr(), callback)); |
| 79 } else { | 76 } else { |
| 80 CreateUIImageAndRunCallback(callback, data); | 77 CreateUIImageAndRunCallback(callback, data); |
| 81 } | 78 } |
| 82 } | 79 } |
| 83 | 80 |
| 84 void IOSImageDecoderImpl::CreateUIImageAndRunCallback( | 81 void IOSImageDecoderImpl::CreateUIImageAndRunCallback( |
| 85 const image_fetcher::ImageDecodedCallback& callback, | 82 const ImageDecodedCallback& callback, |
| 86 NSData* image_data) { | 83 NSData* image_data) { |
| 87 // Decode the image data using UIImage. | 84 // Decode the image data using UIImage. |
| 88 if (image_data) { | 85 if (image_data) { |
| 89 // "Most likely" always returns 1x images. | 86 // "Most likely" always returns 1x images. |
| 90 UIImage* ui_image = [UIImage imageWithData:image_data scale:1]; | 87 UIImage* ui_image = [UIImage imageWithData:image_data scale:1]; |
| 91 if (ui_image) { | 88 if (ui_image) { |
| 92 // This constructor does not retain the image, but expects to take the | 89 // This constructor does not retain the image, but expects to take the |
| 93 // ownership, therefore, |ui_image| is retained here, but not released | 90 // ownership, therefore, |ui_image| is retained here, but not released |
| 94 // afterwards. | 91 // afterwards. |
| 95 gfx::Image gfx_image(ui_image, base::scoped_policy::RETAIN); | 92 gfx::Image gfx_image(ui_image, base::scoped_policy::RETAIN); |
| 96 callback.Run(gfx_image); | 93 callback.Run(gfx_image); |
| 97 return; | 94 return; |
| 98 } | 95 } |
| 99 } | 96 } |
| 100 gfx::Image empty_image; | 97 gfx::Image empty_image; |
| 101 callback.Run(empty_image); | 98 callback.Run(empty_image); |
| 102 } | 99 } |
| 103 | 100 |
| 104 std::unique_ptr<image_fetcher::ImageDecoder> CreateIOSImageDecoder( | 101 std::unique_ptr<ImageDecoder> CreateIOSImageDecoder( |
| 105 scoped_refptr<base::TaskRunner> task_runner) { | 102 scoped_refptr<base::TaskRunner> task_runner) { |
| 106 return base::MakeUnique<IOSImageDecoderImpl>(std::move(task_runner)); | 103 return base::MakeUnique<IOSImageDecoderImpl>(std::move(task_runner)); |
| 107 } | 104 } |
| 108 | 105 |
| 109 } // namespace suggestions | 106 } // namespace image_fetcher |
| OLD | NEW |