/* 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 "BlobURLChannel.h"

#ifdef MOZ_WIDGET_ANDROID
#  include "mozilla/BasePrincipal.h"
#endif

#include "mozilla/dom/BlobImpl.h"
#include "mozilla/dom/BlobURL.h"
#include "mozilla/dom/BlobURLInputStream.h"
#include "mozilla/dom/ContentChild.h"
#include "nsQueryObject.h"

using namespace mozilla::dom;

NS_IMPL_ISUPPORTS_INHERITED(BlobURLChannel, nsBaseChannel, BlobURLChannel)

BlobURLChannel::BlobURLChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo)
    : mContentStreamOpened(false) {
  SetURI(aURI);
  SetOriginalURI(aURI);
  SetLoadInfo(aLoadInfo);

  // If we're sandboxed, make sure to clear any owner the channel
  // might already have.
  if (aLoadInfo && aLoadInfo->GetLoadingSandboxed()) {
    SetOwner(nullptr);
  }
}

BlobURLChannel::~BlobURLChannel() = default;

nsresult BlobURLChannel::SetRequestContentRangeHeader(
    const nsACString& aContentRangeHeader) {
  NS_ENSURE_FALSE(mContentStreamOpened, NS_ERROR_ALREADY_INITIALIZED);
  NS_ENSURE_FALSE(mRequestContentRange, NS_ERROR_ALREADY_INITIALIZED);

  // Just parse the range request. We can't construct the full
  // mozilla::net::ContentRange here, as the response size is unknown.
  mRequestContentRange =
      nsContentUtils::ParseSingleRangeRequest(aContentRangeHeader, true);
  if (!mRequestContentRange) {
    // https://fetch.spec.whatwg.org/#ref-for-simple-range-header-value%E2%91%A1
    // If rangeValue is failure, then return a network error.
    return NS_ERROR_NET_PARTIAL_TRANSFER;
  }
  return NS_OK;
}

nsresult BlobURLChannel::SetResponseContentRange(
    net::ContentRange* aContentRange) {
  NS_ENSURE_ARG(aContentRange);
  NS_ENSURE_FALSE(mResponseContentRange, NS_ERROR_ALREADY_INITIALIZED);
  mResponseContentRange = aContentRange;
  return NS_OK;
}

nsresult BlobURLChannel::GetBackingBlob(BlobImpl** aBlobImpl) {
  NS_ENSURE_ARG(aBlobImpl);
  NS_ENSURE_TRUE(mBlobImpl, NS_ERROR_NOT_AVAILABLE);
  *aBlobImpl = do_AddRef(mBlobImpl).take();
  return NS_OK;
}

nsresult BlobURLChannel::SetBackingBlob(BlobImpl* aBlobImpl) {
  NS_ENSURE_ARG(aBlobImpl);
  NS_ENSURE_FALSE(mBlobImpl, NS_ERROR_ALREADY_INITIALIZED);
  mBlobImpl = aBlobImpl;
  return NS_OK;
}

NS_IMETHODIMP
BlobURLChannel::SetContentType(const nsACString& aContentType) {
  // If the blob type is empty, set the content type of the channel to the
  // empty string.
  if (aContentType.IsEmpty()) {
    mContentType.Truncate();
    return NS_OK;
  }

  return nsBaseChannel::SetContentType(aContentType);
}

nsresult BlobURLChannel::OpenContentStream(bool aAsync,
                                           nsIInputStream** aResult,
                                           nsIChannel** aChannel) {
  if (mContentStreamOpened) {
    return NS_ERROR_ALREADY_OPENED;
  }

  mContentStreamOpened = true;

  nsCOMPtr<nsIURI> uri;
  nsresult rv = GetURI(getter_AddRefs(uri));
  NS_ENSURE_SUCCESS(rv, NS_ERROR_MALFORMED_URI);

  RefPtr<BlobURL> blobURL = do_QueryObject(uri);

  if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!blobURL)) {
    return NS_ERROR_MALFORMED_URI;
  }

  if (blobURL->Revoked()) {
#ifdef MOZ_WIDGET_ANDROID
    nsCOMPtr<nsILoadInfo> loadInfo;
    GetLoadInfo(getter_AddRefs(loadInfo));
    // if the channel was not triggered by the system principal,
    // then we return here because the URL had been revoked
    if (loadInfo && !loadInfo->TriggeringPrincipal()->IsSystemPrincipal()) {
      return NS_ERROR_MALFORMED_URI;
    }
#else
    return NS_ERROR_MALFORMED_URI;
#endif
  }

  // Defensively ensure this process is marked as untrusted.
  ContentChild::MaybeBecomeUntrusted();

  nsCOMPtr<nsIInputStream> inputStream =
      BlobURLInputStream::Create(this, blobURL);
  if (NS_WARN_IF(!inputStream)) {
    return NS_ERROR_MALFORMED_URI;
  }

  EnableSynthesizedProgressEvents(true);

  inputStream.forget(aResult);

  return NS_OK;
}
