mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-12 19:48:30 +08:00
Summary: Sometimes, when we deal with ImageRequest and ImageResponseObserverCoordinator we subscribe for status (or access the coordinator) without owning an ImageRequest. In those cases, we have to retain the coordinator explicitly. For those cases, ImageRequest now exposes `ImageResponseObserverCoordinator` as a `std::shared_ptr`. Eg, concretely in the code, `completionBlock` and `progressBlock` copied a raw pointer to the observer inside which can lead to a crash when ImageRequest is being deallocated before we received an image data. Reviewed By: JoshuaGross Differential Revision: D14072079 fbshipit-source-id: e10120bc05bf685e288f7b3d69092714dcd91d43
93 lines
2.4 KiB
C++
93 lines
2.4 KiB
C++
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <react/imagemanager/ImageResponse.h>
|
|
#include <react/imagemanager/ImageResponseObserver.h>
|
|
#include <react/imagemanager/ImageResponseObserverCoordinator.h>
|
|
#include <react/imagemanager/primitives.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/*
|
|
* Represents ongoing request for an image resource.
|
|
* The separate object must be constructed for every single separate
|
|
* image request. The object cannot be copied because it would make managing of
|
|
* event listeners hard and inefficient; the object can be moved though.
|
|
* Destroy to cancel the underlying request.
|
|
*/
|
|
class ImageRequest final {
|
|
public:
|
|
/*
|
|
* The exception which is thrown when `ImageRequest` is being deallocated
|
|
* if the future is not ready yet.
|
|
*/
|
|
class ImageNoLongerNeededException;
|
|
|
|
/*
|
|
* The default constructor
|
|
*/
|
|
ImageRequest(const ImageSource &imageSource);
|
|
|
|
/*
|
|
* The move constructor.
|
|
*/
|
|
ImageRequest(ImageRequest &&other) noexcept;
|
|
|
|
/*
|
|
* `ImageRequest` does not support copying by design.
|
|
*/
|
|
ImageRequest(const ImageRequest &other) = delete;
|
|
|
|
~ImageRequest();
|
|
|
|
/**
|
|
* Set cancelation function.
|
|
*/
|
|
void setCancelationFunction(std::function<void(void)> cancelationFunction);
|
|
|
|
/*
|
|
* Returns stored observer coordinator as a shared pointer.
|
|
* Retain this *or* `ImageRequest` to ensure a correct lifetime of the object.
|
|
*/
|
|
const std::shared_ptr<const ImageResponseObserverCoordinator>
|
|
&getSharedObserverCoordinator() const;
|
|
|
|
/*
|
|
* Returns stored observer coordinator as a reference.
|
|
* Use this if a correct lifetime of the object is ensured in some other way
|
|
* (e.g. by retaining an `ImageRequest`).
|
|
*/
|
|
const ImageResponseObserverCoordinator &getObserverCoordinator() const;
|
|
|
|
private:
|
|
/*
|
|
* Image source assosiated with the request.
|
|
*/
|
|
ImageSource imageSource_;
|
|
|
|
/*
|
|
* Event coordinator associated with the reqest.
|
|
*/
|
|
std::shared_ptr<const ImageResponseObserverCoordinator> coordinator_{};
|
|
|
|
/*
|
|
* Function we can call to cancel image request (see destructor).
|
|
*/
|
|
std::function<void(void)> cancelRequest_;
|
|
|
|
/*
|
|
* Indicates that the object was moved and hence cannot be used anymore.
|
|
*/
|
|
bool moved_{false};
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|