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.

Header

Mercurial (c68fe15a81fc)

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
/* 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/. */


#include "FetchImageHelper.h"

#include "mozilla/gfx/2D.h"
#include "mozilla/Logging.h"
#include "mozilla/NullPrincipal.h"

mozilla::LazyLogModule gFetchImageLog("FetchImageHelper");
mozilla::LazyLogModule gFetchImageLog("FetchImageHelper");

#undef LOG
#define LOG(msg, ...)                      \
  MOZ_LOG(gFetchImageLog, LogLevel::Debug, \
          ("FetchImageHelper=%p, " msg, this, ##__VA_ARGS__))
          ("FetchImageHelper=%p, " msg, this, ##__VA_ARGS__))

using namespace mozilla::gfx;

namespace mozilla {
namespace dom {
namespace dom {

FetchImageHelper::FetchImageHelper(const MediaImage& aImage)
    : mSrc(aImage.mSrc) {}

FetchImageHelper::~FetchImageHelper() { AbortFetchingImage(); }
FetchImageHelper::~FetchImageHelper() { AbortFetchingImage(); }

RefPtr<ImagePromise> FetchImageHelper::FetchImage() {
  MOZ_ASSERT(NS_IsMainThread());
  if (IsFetchingImage()) {
    return mPromise.Ensure(__func__);
  }

  LOG("Start fetching image from %s", NS_ConvertUTF16toUTF8(mSrc).get());
  nsCOMPtr<nsIURI> uri;
  nsCOMPtr<nsIURI> uri;
  if (NS_FAILED(NS_NewURI(getter_AddRefs(uri), mSrc))) {
    LOG("Failed to create URI");
    return ImagePromise::CreateAndReject(false, __func__);
  }


  MOZ_ASSERT(!mListener);
  mListener = new ImageFetchListener();
  if (NS_FAILED(mListener->FetchDecodedImageFromURI(uri, this))) {
  if (NS_FAILED(mListener->FetchDecodedImageFromURI(uri, this))) {
    LOG("Failed to decode image from async channel");
    LOG("Failed to decode image from async channel");
    return ImagePromise::CreateAndReject(false, __func__);
  }
  return mPromise.Ensure(__func__);
  return mPromise.Ensure(__func__);
}

void FetchImageHelper::AbortFetchingImage() {
  MOZ_ASSERT(NS_IsMainThread());
  LOG("AbortFetchingImage");
  LOG("AbortFetchingImage");
  mPromise.RejectIfExists(false, __func__);
  ClearListenerIfNeeded();
}

void FetchImageHelper::ClearListenerIfNeeded() {
void FetchImageHelper::ClearListenerIfNeeded() {
  if (mListener) {
    mListener->Clear();
    mListener = nullptr;
  }
}
}

bool FetchImageHelper::IsFetchingImage() const {
  MOZ_ASSERT(NS_IsMainThread());
  return !mPromise.IsEmpty() && mListener;
}
}

void FetchImageHelper::HandleFetchSuccess(imgIContainer* aImage) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(IsFetchingImage());
  LOG("Finished decoding image");
  RefPtr<SourceSurface> surface = aImage->GetFrame(
      imgIContainer::FRAME_FIRST,
      imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
      imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
  RefPtr<DataSourceSurface> dataSurface = surface->GetDataSurface();
  mPromise.Resolve(dataSurface, __func__);
  ClearListenerIfNeeded();
}


void FetchImageHelper::HandleFetchFail() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(IsFetchingImage());
  LOG("Reject the promise because of fetching failed");
  mPromise.RejectIfExists(false, __func__);
  mPromise.RejectIfExists(false, __func__);
  ClearListenerIfNeeded();
}

/**
/**
 * Implementation for FetchImageHelper::ImageFetchListener
 */
NS_IMPL_ISUPPORTS(FetchImageHelper::ImageFetchListener, imgIContainerCallback,
                  imgINotificationObserver)


FetchImageHelper::ImageFetchListener::~ImageFetchListener() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!mHelper, "Cancel() should be called before desturction!");
}


nsresult FetchImageHelper::ImageFetchListener::FetchDecodedImageFromURI(
    nsIURI* aURI, FetchImageHelper* aHelper) {
  MOZ_ASSERT(!mHelper && !mChannel && !mImage,
             "Should call Clear() berfore running another fetching process!");
  RefPtr<nsIPrincipal> nullPrincipal =
  RefPtr<nsIPrincipal> nullPrincipal =
      NullPrincipal::CreateWithoutOriginAttributes();
  nsCOMPtr<nsIChannel> channel;
  nsresult rv = NS_NewChannel(getter_AddRefs(channel), aURI, nullPrincipal,
  nsresult rv = NS_NewChannel(getter_AddRefs(channel), aURI, nullPrincipal,
                              nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
                              nsIContentPolicy::TYPE_INTERNAL_IMAGE);
  if (NS_FAILED(rv)) {
    return rv;
  }
  }

  nsCOMPtr<imgITools> imgTools = do_GetService("@mozilla.org/image/tools;1");
  if (!imgTools) {
    return NS_ERROR_FAILURE;
  }

  rv = imgTools->DecodeImageFromChannelAsync(aURI, channel, this, this);
  rv = imgTools->DecodeImageFromChannelAsync(aURI, channel, this, this);
  if (NS_FAILED(rv)) {
    return NS_ERROR_FAILURE;
  }
  MOZ_ASSERT(aHelper);
  mHelper = aHelper;
  mHelper = aHelper;
  mChannel = channel;
  return NS_OK;
}

void FetchImageHelper::ImageFetchListener::Clear() {
void FetchImageHelper::ImageFetchListener::Clear() {
  MOZ_ASSERT(NS_IsMainThread());
  if (mChannel) {
    mChannel->Cancel(NS_BINDING_ABORTED);
    mChannel = nullptr;
  }
  }
  mHelper = nullptr;
  mImage = nullptr;
}

bool FetchImageHelper::ImageFetchListener::IsFetchingImage() const {
bool FetchImageHelper::ImageFetchListener::IsFetchingImage() const {
  MOZ_ASSERT(NS_IsMainThread());
  return mHelper ? mHelper->IsFetchingImage() : false;
}

NS_IMETHODIMP FetchImageHelper::ImageFetchListener::OnImageReady(
    imgIContainer* aImage, nsresult aStatus) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(NS_IsMainThread());
  if (!IsFetchingImage()) {
    return NS_OK;
  }
  // We have received image, so we don't need the channel anymore.
  mChannel = nullptr;
  mChannel = nullptr;

  MOZ_ASSERT(mHelper);
  if (NS_FAILED(aStatus)) {
    mHelper->HandleFetchFail();
    Clear();
    Clear();
    return aStatus;
  }

  mImage = aImage;
  nsresult rv = mImage->StartDecoding(
  nsresult rv = mImage->StartDecoding(
      imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY,
      imgIContainer::FRAME_FIRST);
  if (NS_FAILED(rv)) {
    mHelper->HandleFetchFail();
    mHelper->HandleFetchFail();
    Clear();
    return rv;
  }
  return NS_OK;
}
}

void FetchImageHelper::ImageFetchListener::Notify(imgIRequest* aRequest,
                                                  int32_t aType,
                                                  const nsIntRect* aData) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(NS_IsMainThread());
  if (!IsFetchingImage() ||
      aType != imgINotificationObserver::DECODE_COMPLETE) {
    return;
  }
  MOZ_ASSERT(mHelper);
  MOZ_ASSERT(mHelper);
  mHelper->HandleFetchSuccess(mImage);
  Clear();
}

}  // namespace dom
}  // namespace mozilla