icon_decoder_impl.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2022 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. #include "ash/components/phonehub/icon_decoder_impl.h"
  5. #include <functional>
  6. #include <utility>
  7. #include "ash/components/multidevice/logging/logging.h"
  8. #include "ash/components/phonehub/proto/phonehub_api.pb.h"
  9. #include "base/barrier_closure.h"
  10. #include "base/bind.h"
  11. #include "base/callback.h"
  12. #include "base/callback_forward.h"
  13. #include "base/containers/flat_map.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "services/data_decoder/public/cpp/decode_image.h"
  16. #include "services/data_decoder/public/mojom/image_decoder.mojom.h"
  17. #include "ubidiimp.h"
  18. #include "ui/gfx/image/image.h"
  19. #include "ui/gfx/image/image_skia.h"
  20. namespace ash {
  21. namespace phonehub {
  22. IconDecoderImpl::DecoderDelegate::DecoderDelegate() = default;
  23. IconDecoderImpl::DecoderDelegate::~DecoderDelegate() = default;
  24. void IconDecoderImpl::DecoderDelegate::Decode(
  25. const DecodingData& request,
  26. data_decoder::DecodeImageCallback callback) {
  27. const std::string& encoded_icon = request.input_data;
  28. data_decoder::DecodeImage(
  29. &data_decoder_, base::as_bytes(base::make_span(encoded_icon)),
  30. data_decoder::mojom::ImageCodec::kDefault,
  31. /*shrink_to_fit=*/true, data_decoder::kDefaultMaxSizeInBytes,
  32. /*desired_image_frame_size=*/gfx::Size(), std::move(callback));
  33. }
  34. IconDecoderImpl::IconDecoderImpl()
  35. : decoder_delegate_(std::make_unique<DecoderDelegate>()) {}
  36. IconDecoderImpl::~IconDecoderImpl() = default;
  37. void IconDecoderImpl::BatchDecode(
  38. std::unique_ptr<std::vector<DecodingData>> decode_items,
  39. base::OnceCallback<void(std::unique_ptr<std::vector<DecodingData>>)>
  40. finished_callback) {
  41. CancelPendingRequests();
  42. pending_items_ = std::move(decode_items);
  43. barrier_closure_ =
  44. base::BarrierClosure(pending_items_->size(),
  45. base::BindOnce(&IconDecoderImpl::OnAllIconsDecoded,
  46. weak_ptr_factory_.GetWeakPtr(),
  47. std::move(finished_callback)));
  48. // If decode_items is empty, barrier closure must have been called by this
  49. // point and the pending_items_ pointer must be already reset.
  50. if (!pending_items_)
  51. return;
  52. for (DecodingData& request : *pending_items_) {
  53. decoder_delegate_->Decode(
  54. request,
  55. base::BindOnce(&IconDecoderImpl::OnIconDecoded,
  56. weak_ptr_factory_.GetWeakPtr(), std::ref(request)));
  57. }
  58. }
  59. void IconDecoderImpl::OnAllIconsDecoded(
  60. base::OnceCallback<void(std::unique_ptr<std::vector<DecodingData>>)>
  61. finished_callback) {
  62. std::move(finished_callback).Run(std::move(pending_items_));
  63. }
  64. void IconDecoderImpl::OnIconDecoded(DecodingData& decoding_data,
  65. const SkBitmap& result) {
  66. gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(result);
  67. // If |image_skia| is null, indicating that the data decoder failed to decode
  68. // the image, the image will be empty, and cannot be made thread safe.
  69. if (!image_skia.isNull())
  70. image_skia.MakeThreadSafe();
  71. decoding_data.result = gfx::Image(image_skia);
  72. barrier_closure_.Run();
  73. }
  74. void IconDecoderImpl::CancelPendingRequests() {
  75. weak_ptr_factory_.InvalidateWeakPtrs();
  76. }
  77. } // namespace phonehub
  78. } // namespace ash