DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (882de07e4cbe)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DOM_MEDIA_MEDIACONTROL_FETCHIMAGEHELPER_H_
#define DOM_MEDIA_MEDIACONTROL_FETCHIMAGEHELPER_H_

#include "imgIContainer.h"
#include "imgITools.h"
#include "imgITools.h"
#include "imgINotificationObserver.h"
#include "mozilla/dom/MediaSessionBinding.h"
#include "mozilla/MozPromise.h"

namespace mozilla {
namespace mozilla {
namespace dom {
/**
 * FetchImageHelper is used to fetch image data from MediaImage, and the fetched
 * image data would be used to show on the virtual control inferface. The URL of
 * MediaImage is defined by websites by using MediaSession API [1].
 * MediaImage is defined by websites by using MediaSession API [1].
 *
 * By using `FetchImage()`, it would return a promise that would resolve with a
 * `DataSourceSurface`, then we can get the decoded image data from the surface.
 * `DataSourceSurface`, then we can get the decoded image data from the surface.
 *
 * [1] https://w3c.github.io/mediasession/#dictdef-mediaimage
 */
using ImagePromise = MozPromise<RefPtr<mozilla::gfx::DataSourceSurface>, bool,
                                /* IsExclusive = */ true>;
                                /* IsExclusive = */ true>;
class FetchImageHelper final {
 public:
  NS_INLINE_DECL_REFCOUNTING(FetchImageHelper)

  explicit FetchImageHelper(const MediaImage& aImage);
  explicit FetchImageHelper(const MediaImage& aImage);

  // Return a promise which would be resolved with the decoded image surface
  // when we finish fetching and decoding image data, and it would be rejected
  // when we fail to fecth the image.
  RefPtr<ImagePromise> FetchImage();
  RefPtr<ImagePromise> FetchImage();

  // Stop fetching and decoding image and reject the image promise. If we have
  // not started yet fetching image, then nothing would happen.
  void AbortFetchingImage();

  // Return true if we're fecthing image.
  bool IsFetchingImage() const;

 private:
  /**
  /**
   * ImageFetchListener is used to listen the notification of finishing fetching
   * image data (via `OnImageReady()`) and finishing decoding image data (via
   * `Notify()`).
   */
  class ImageFetchListener final : public imgIContainerCallback,
  class ImageFetchListener final : public imgIContainerCallback,
                                   public imgINotificationObserver {
   public:
    NS_DECL_ISUPPORTS
    ImageFetchListener() = default;


    // Start an async channel to load the image, and return error if the channel
    // opens failed. It would use `aHelper::HandleFetchSuccess/Fail()` to notify
    // the result asynchronously.
    nsresult FetchDecodedImageFromURI(nsIURI* aURI, FetchImageHelper* aHelper);
    void Clear();
    void Clear();
    bool IsFetchingImage() const;

    // Methods of imgIContainerCallback and imgINotificationObserver
    NS_IMETHOD OnImageReady(imgIContainer* aImage, nsresult aStatus) override;
    void Notify(imgIRequest* aRequest, int32_t aType,
                const nsIntRect* aData) override;

   private:
    ~ImageFetchListener();
    ~ImageFetchListener();

    FetchImageHelper* MOZ_NON_OWNING_REF mHelper = nullptr;
    nsCOMPtr<nsIChannel> mChannel;
    nsCOMPtr<imgIContainer> mImage;
  };
  };

  ~FetchImageHelper();

  void ClearListenerIfNeeded();
  void HandleFetchSuccess(imgIContainer* aImage);
  void HandleFetchSuccess(imgIContainer* aImage);
  void HandleFetchFail();

  nsString mSrc;
  MozPromiseHolder<ImagePromise> mPromise;
  RefPtr<ImageFetchListener> mListener;
  RefPtr<ImageFetchListener> mListener;
};

}  // namespace dom
}  // namespace mozilla

#endif  // DOM_MEDIA_MEDIACONTROL_FETCHIMAGEHELPER_H_