ImageRequest.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <react/imagemanager/ImageInstrumentation.h>
  9. #include <react/imagemanager/ImageResponse.h>
  10. #include <react/imagemanager/ImageResponseObserver.h>
  11. #include <react/imagemanager/ImageResponseObserverCoordinator.h>
  12. #include <react/imagemanager/primitives.h>
  13. namespace facebook {
  14. namespace react {
  15. /*
  16. * Represents ongoing request for an image resource.
  17. * The separate object must be constructed for every single separate
  18. * image request. The object cannot be copied because it would make managing of
  19. * event listeners hard and inefficient; the object can be moved though.
  20. * Destroy to cancel the underlying request.
  21. */
  22. class ImageRequest final {
  23. public:
  24. /*
  25. * The default constructor
  26. */
  27. ImageRequest(
  28. const ImageSource &imageSource,
  29. std::shared_ptr<const ImageInstrumentation> instrumentation);
  30. /*
  31. * The move constructor.
  32. */
  33. ImageRequest(ImageRequest &&other) noexcept;
  34. /*
  35. * `ImageRequest` does not support copying by design.
  36. */
  37. ImageRequest(const ImageRequest &other) = delete;
  38. ~ImageRequest();
  39. /**
  40. * Set cancelation function.
  41. */
  42. void setCancelationFunction(std::function<void(void)> cancelationFunction);
  43. /*
  44. * Returns stored observer coordinator as a shared pointer.
  45. * Retain this *or* `ImageRequest` to ensure a correct lifetime of the object.
  46. */
  47. const std::shared_ptr<const ImageResponseObserverCoordinator>
  48. &getSharedObserverCoordinator() const;
  49. /*
  50. * Returns stored observer coordinator as a reference.
  51. * Use this if a correct lifetime of the object is ensured in some other way
  52. * (e.g. by retaining an `ImageRequest`).
  53. */
  54. const ImageResponseObserverCoordinator &getObserverCoordinator() const;
  55. /*
  56. * Returns stored image instrumentation object as a shared pointer.
  57. * Retain this *or* `ImageRequest` to ensure a correct lifetime of the object.
  58. */
  59. const std::shared_ptr<const ImageInstrumentation>
  60. &getSharedImageInstrumentation() const;
  61. /*
  62. * Returns the image instrumentation object specific to this request.
  63. * Use this if a correct lifetime of the object is ensured in some other way
  64. * (e.g. by retaining an `ImageRequest`).
  65. */
  66. const ImageInstrumentation &getImageInstrumentation() const;
  67. private:
  68. /*
  69. * Image source associated with the request.
  70. */
  71. ImageSource imageSource_;
  72. /*
  73. * Event coordinator associated with the reqest.
  74. */
  75. std::shared_ptr<const ImageResponseObserverCoordinator> coordinator_{};
  76. /*
  77. * Image instrumentation specific to the request.
  78. */
  79. std::shared_ptr<const ImageInstrumentation> instrumentation_;
  80. /*
  81. * Function we can call to cancel image request (see destructor).
  82. */
  83. std::function<void(void)> cancelRequest_;
  84. /*
  85. * Indicates that the object was moved and hence cannot be used anymore.
  86. */
  87. bool moved_{false};
  88. };
  89. } // namespace react
  90. } // namespace facebook