camera_roll_thumbnail_decoder_impl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2021 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. #ifndef ASH_COMPONENTS_PHONEHUB_CAMERA_ROLL_THUMBNAIL_DECODER_IMPL_H_
  5. #define ASH_COMPONENTS_PHONEHUB_CAMERA_ROLL_THUMBNAIL_DECODER_IMPL_H_
  6. #include "ash/components/phonehub/camera_roll_thumbnail_decoder.h"
  7. #include "ash/components/phonehub/proto/phonehub_api.pb.h"
  8. #include "base/callback.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "services/data_decoder/public/cpp/data_decoder.h"
  11. #include "services/data_decoder/public/cpp/decode_image.h"
  12. #include "ui/gfx/image/image.h"
  13. namespace ash {
  14. namespace phonehub {
  15. class CameraRollThumbnailDecoderImpl : public CameraRollThumbnailDecoder {
  16. public:
  17. CameraRollThumbnailDecoderImpl();
  18. ~CameraRollThumbnailDecoderImpl() override;
  19. void BatchDecode(const proto::FetchCameraRollItemsResponse& response,
  20. const std::vector<CameraRollItem>& current_items,
  21. base::OnceCallback<void(BatchDecodeResult result,
  22. const std::vector<CameraRollItem>&)>
  23. callback) override;
  24. private:
  25. friend class CameraRollThumbnailDecoderImplTest;
  26. friend class FakeDecoderDelegate;
  27. // A pending request to decode the thumbnail of a camera roll item.
  28. class DecodeRequest {
  29. public:
  30. explicit DecodeRequest(const proto::CameraRollItem& item_proto);
  31. virtual ~DecodeRequest();
  32. const proto::CameraRollItemMetadata& GetMetadata() const;
  33. // Completes this request with thumbnail bitmap decoded from the raw bytes
  34. // in the proto.
  35. void CompleteWithDecodedBitmap(const SkBitmap& bitmap);
  36. // Completes this request with an existing image that has already been
  37. // decoded for the same item.
  38. void CompleteWithExistingImage(const gfx::Image& image);
  39. bool is_completed() const { return is_completed_; }
  40. // Returns the encoded raw bytes of the thumbnail. May return an empty
  41. // string if the thumbnail is not sent with the camera roll item proto.
  42. const std::string& GetEncodedThumbnail() const;
  43. // Returns an empty image when the request is not completed or a non-empty
  44. // image when the requests is complete.
  45. const gfx::Image& decoded_thumbnail() const { return decoded_thumbnail_; }
  46. private:
  47. const proto::CameraRollItem item_proto_;
  48. gfx::Image decoded_thumbnail_;
  49. bool is_completed_ = false;
  50. };
  51. // Delegate class that decodes camera roll item thumbnails. Can be overridden
  52. // in tests.
  53. class DecoderDelegate {
  54. public:
  55. DecoderDelegate();
  56. virtual ~DecoderDelegate();
  57. virtual void DecodeThumbnail(const DecodeRequest& request,
  58. data_decoder::DecodeImageCallback callback);
  59. private:
  60. // The instance of DataDecoder to decode thumbnail images. The underlying
  61. // service instance is started lazily when needed and torn down when not in
  62. // use.
  63. data_decoder::DataDecoder data_decoder_;
  64. };
  65. void OnThumbnailDecoded(const proto::CameraRollItemMetadata& item_metadata,
  66. const SkBitmap& thumbnail_bitmap);
  67. // Checks whether all requests to decode thumbnails have been completed for
  68. // the latest items received, and invoke the pending callback if so.
  69. void CheckPendingThumbnailRequests();
  70. // Cancels all pending requests.
  71. void CancelPendingRequests();
  72. std::unique_ptr<DecoderDelegate> decoder_delegate_;
  73. std::vector<DecodeRequest> pending_requests_;
  74. base::OnceCallback<void(BatchDecodeResult result,
  75. const std::vector<CameraRollItem>&)>
  76. pending_callback_;
  77. // Contains weak pointers to callbacks passed to the |DecoderDelegate|.
  78. base::WeakPtrFactory<CameraRollThumbnailDecoderImpl> weak_ptr_factory_{this};
  79. };
  80. } // namespace phonehub
  81. } // namespace ash
  82. #endif // ASH_COMPONENTS_PHONEHUB_CAMERA_ROLL_THUMBNAIL_DECODER_IMPL_H_