| 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 "components/image_fetcher/content/image_decoder_impl.h" |
| 6 |
| 5 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> |
| 6 #include <vector> | 9 #include <vector> |
| 7 | 10 |
| 8 #include "base/callback.h" | 11 #include "base/callback.h" |
| 9 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | |
| 10 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 11 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
| 12 | 14 |
| 13 namespace suggestions { | 15 namespace image_fetcher { |
| 14 | 16 |
| 15 // A request for decoding an image. | 17 // A request for decoding an image. |
| 16 class ImageDecoderImpl::DecodeImageRequest | 18 class ImageDecoderImpl::DecodeImageRequest |
| 17 : public ::ImageDecoder::ImageRequest { | 19 : public ::ImageDecoder::ImageRequest { |
| 18 public: | 20 public: |
| 19 DecodeImageRequest(ImageDecoderImpl* decoder, | 21 DecodeImageRequest(ImageDecoderImpl* decoder, |
| 20 const image_fetcher::ImageDecodedCallback& callback) | 22 const ImageDecodedCallback& callback) |
| 21 : decoder_(decoder), callback_(callback) {} | 23 : decoder_(decoder), callback_(callback) {} |
| 22 ~DecodeImageRequest() override {} | 24 ~DecodeImageRequest() override {} |
| 23 | 25 |
| 24 private: | 26 private: |
| 25 // Runs the callback and remove the request from the internal request queue. | 27 // Runs the callback and remove the request from the internal request queue. |
| 26 void RunCallbackAndRemoveRequest(const gfx::Image& image); | 28 void RunCallbackAndRemoveRequest(const gfx::Image& image); |
| 27 | 29 |
| 28 // Methods inherited from ImageDecoder::ImageRequest | 30 // Methods inherited from ImageDecoder::ImageRequest |
| 29 | 31 |
| 30 void OnImageDecoded(const SkBitmap& decoded_image) override; | 32 void OnImageDecoded(const SkBitmap& decoded_image) override; |
| 31 | 33 |
| 32 void OnDecodeImageFailed() override; | 34 void OnDecodeImageFailed() override; |
| 33 | 35 |
| 34 ImageDecoderImpl* decoder_; | 36 ImageDecoderImpl* decoder_; |
| 35 | 37 |
| 36 // The callback to call after the request completed. | 38 // The callback to call after the request completed. |
| 37 image_fetcher::ImageDecodedCallback callback_; | 39 ImageDecodedCallback callback_; |
| 38 | 40 |
| 39 DISALLOW_COPY_AND_ASSIGN(DecodeImageRequest); | 41 DISALLOW_COPY_AND_ASSIGN(DecodeImageRequest); |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 void ImageDecoderImpl::DecodeImageRequest::OnImageDecoded( | 44 void ImageDecoderImpl::DecodeImageRequest::OnImageDecoded( |
| 43 const SkBitmap& decoded_bitmap) { | 45 const SkBitmap& decoded_bitmap) { |
| 44 // TODO(markusheintz): Check whether high res displays that require 2x and 3x | 46 // TODO(markusheintz): Check whether high res displays that require 2x and 3x |
| 45 // image versions need to be supported here. | 47 // image versions need to be supported here. |
| 46 gfx::Image image(gfx::Image::CreateFrom1xBitmap(decoded_bitmap)); | 48 gfx::Image image(gfx::Image::CreateFrom1xBitmap(decoded_bitmap)); |
| 47 RunCallbackAndRemoveRequest(image); | 49 RunCallbackAndRemoveRequest(image); |
| 48 } | 50 } |
| 49 | 51 |
| 50 // Called when decoding image failed. | 52 // Called when decoding image failed. |
| 51 void ImageDecoderImpl::DecodeImageRequest::OnDecodeImageFailed() { | 53 void ImageDecoderImpl::DecodeImageRequest::OnDecodeImageFailed() { |
| 52 RunCallbackAndRemoveRequest(gfx::Image()); | 54 RunCallbackAndRemoveRequest(gfx::Image()); |
| 53 } | 55 } |
| 54 | 56 |
| 55 void ImageDecoderImpl::DecodeImageRequest::RunCallbackAndRemoveRequest( | 57 void ImageDecoderImpl::DecodeImageRequest::RunCallbackAndRemoveRequest( |
| 56 const gfx::Image& image) { | 58 const gfx::Image& image) { |
| 57 callback_.Run(image); | 59 callback_.Run(image); |
| 58 | 60 |
| 59 // This must be the last line in the method body. | 61 // This must be the last line in the method body. |
| 60 decoder_->RemoveDecodeImageRequest(this); | 62 decoder_->RemoveDecodeImageRequest(this); |
| 61 } | 63 } |
| 62 | 64 |
| 63 ImageDecoderImpl::ImageDecoderImpl() {} | 65 ImageDecoderImpl::ImageDecoderImpl() {} |
| 64 | 66 |
| 65 ImageDecoderImpl::~ImageDecoderImpl() {} | 67 ImageDecoderImpl::~ImageDecoderImpl() {} |
| 66 | 68 |
| 67 void ImageDecoderImpl::DecodeImage( | 69 void ImageDecoderImpl::DecodeImage(const std::string& image_data, |
| 68 const std::string& image_data, | 70 const gfx::Size& desired_image_frame_size, |
| 69 const gfx::Size& desired_image_frame_size, | 71 const ImageDecodedCallback& callback) { |
| 70 const image_fetcher::ImageDecodedCallback& callback) { | |
| 71 std::unique_ptr<DecodeImageRequest> decode_image_request( | 72 std::unique_ptr<DecodeImageRequest> decode_image_request( |
| 72 new DecodeImageRequest(this, callback)); | 73 new DecodeImageRequest(this, callback)); |
| 73 | 74 |
| 74 ::ImageDecoder::StartWithOptions( | 75 ::ImageDecoder::StartWithOptions( |
| 75 decode_image_request.get(), | 76 decode_image_request.get(), |
| 76 std::vector<uint8_t>(image_data.begin(), image_data.end()), | 77 std::vector<uint8_t>(image_data.begin(), image_data.end()), |
| 77 ::ImageDecoder::DEFAULT_CODEC, | 78 ::ImageDecoder::DEFAULT_CODEC, |
| 78 /*shrink_to_fit=*/false, desired_image_frame_size); | 79 /*shrink_to_fit=*/false, desired_image_frame_size); |
| 79 | 80 |
| 80 decode_image_requests_.push_back(std::move(decode_image_request)); | 81 decode_image_requests_.push_back(std::move(decode_image_request)); |
| 81 } | 82 } |
| 82 | 83 |
| 83 void ImageDecoderImpl::RemoveDecodeImageRequest(DecodeImageRequest* request) { | 84 void ImageDecoderImpl::RemoveDecodeImageRequest(DecodeImageRequest* request) { |
| 84 // Remove the finished request from the request queue. | 85 // Remove the finished request from the request queue. |
| 85 auto request_it = | 86 auto request_it = |
| 86 std::find_if(decode_image_requests_.begin(), | 87 std::find_if(decode_image_requests_.begin(), decode_image_requests_.end(), |
| 87 decode_image_requests_.end(), | |
| 88 [request](const std::unique_ptr<DecodeImageRequest>& r) { | 88 [request](const std::unique_ptr<DecodeImageRequest>& r) { |
| 89 return r.get() == request; | 89 return r.get() == request; |
| 90 }); | 90 }); |
| 91 DCHECK(request_it != decode_image_requests_.end()); | 91 DCHECK(request_it != decode_image_requests_.end()); |
| 92 decode_image_requests_.erase(request_it); | 92 decode_image_requests_.erase(request_it); |
| 93 } | 93 } |
| 94 | 94 |
| 95 } // namespace suggestions | 95 } // namespace image_fetcher |
| OLD | NEW |