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