Bug 1619953 - P4 - BlobURLChannel now creates BlobURLInputStream to acquire blob data r=baku,#dom-workers-and-storage draft
authorssengupta <ssengupta@mozilla.com>
Wed, 01 Jul 2020 13:07:24 +0000
changeset 3016293 9a506a6f9e4b01ea4935361ca4af33fc808ca5cd
parent 3016292 033cf041d0d51e7dfd7cda37c99f318324b62647
child 3016294 d7b277d3fec0c377b9e3ff24caf37e544d1fd7fe
push id562769
push userreviewbot
push dateWed, 01 Jul 2020 13:07:57 +0000
treeherdertry@0172f1324dac [default view] [failures only]
reviewersbaku
bugs1619953
milestone80.0a1
Bug 1619953 - P4 - BlobURLChannel now creates BlobURLInputStream to acquire blob data r=baku,#dom-workers-and-storage Summary: BlobURLProtocolHandler no longer passes blobImpl pointer to BlobURLChannel. Instead, acquisition and handling of blob data is offloaded to BlobURLInputStream. Differential Revision: https://phabricator.services.mozilla.com/D75293 Depends on D75292 Test Plan: Reviewers: baku, #dom-workers-and-storage Subscribers: Bug #: 1619953 Differential Diff: PHID-DIFF-jubqdy6houdddxweodju
dom/file/uri/BlobURLChannel.cpp
dom/file/uri/BlobURLChannel.h
dom/file/uri/BlobURLInputStream.cpp
dom/file/uri/BlobURLProtocolHandler.cpp
--- a/dom/file/uri/BlobURLChannel.cpp
+++ b/dom/file/uri/BlobURLChannel.cpp
@@ -1,16 +1,18 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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"
 #include "mozilla/dom/BlobImpl.h"
+#include "mozilla/dom/BlobURL.h"
+#include "mozilla/dom/BlobURLInputStream.h"
 
 using namespace mozilla::dom;
 
 BlobURLChannel::BlobURLChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo)
     : mInitialized(false) {
   SetURI(aURI);
   SetOriginalURI(aURI);
   SetLoadInfo(aLoadInfo);
@@ -25,45 +27,40 @@ BlobURLChannel::BlobURLChannel(nsIURI* a
 BlobURLChannel::~BlobURLChannel() = default;
 
 void BlobURLChannel::InitFailed() {
   MOZ_ASSERT(!mInitialized);
   MOZ_ASSERT(!mInputStream);
   mInitialized = true;
 }
 
-void BlobURLChannel::Initialize(BlobImpl* aBlobImpl) {
+void BlobURLChannel::Initialize() {
   MOZ_ASSERT(!mInitialized);
 
-  nsAutoString contentType;
-  aBlobImpl->GetType(contentType);
-  SetContentType(NS_ConvertUTF16toUTF8(contentType));
-
-  if (aBlobImpl->IsFile()) {
-    nsString filename;
-    aBlobImpl->GetName(filename);
-    SetContentDispositionFilename(filename);
-  }
-
-  ErrorResult rv;
-  uint64_t size = aBlobImpl->GetSize(rv);
-  if (NS_WARN_IF(rv.Failed())) {
+  nsCOMPtr<nsIURI> uri;
+  nsresult rv = GetURI(getter_AddRefs(uri));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
     InitFailed();
     return;
   }
 
-  SetContentLength(size);
+  RefPtr<BlobURL> blobURL;
+  rv = uri->QueryInterface(kHOSTOBJECTURICID, getter_AddRefs(blobURL));
 
-  aBlobImpl->CreateInputStream(getter_AddRefs(mInputStream), rv);
-  if (NS_WARN_IF(rv.Failed())) {
+  if (NS_FAILED(rv) || !blobURL) {
     InitFailed();
     return;
   }
 
-  MOZ_ASSERT(mInputStream);
+  mInputStream = BlobURLInputStream::Create(this, blobURL);
+  if (NS_WARN_IF(!mInputStream)) {
+    InitFailed();
+    return;
+  }
+
   mInitialized = true;
 }
 
 nsresult BlobURLChannel::OpenContentStream(bool aAsync,
                                            nsIInputStream** aResult,
                                            nsIChannel** aChannel) {
   MOZ_ASSERT(mInitialized);
 
--- a/dom/file/uri/BlobURLChannel.h
+++ b/dom/file/uri/BlobURLChannel.h
@@ -23,17 +23,17 @@ class BlobURLChannel final : public nsBa
   BlobURLChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo);
 
   // This method is called when there is not a valid BlobImpl for this channel.
   // This method will make ::OpenContentStream to return NS_ERROR_MALFORMED_URI.
   void InitFailed();
 
   // There is a valid BlobImpl for the channel. The blob's inputStream will be
   // used when ::OpenContentStream is called.
-  void Initialize(BlobImpl* aBlobImpl);
+  void Initialize();
 
  private:
   ~BlobURLChannel();
 
   nsresult OpenContentStream(bool aAsync, nsIInputStream** aResult,
                              nsIChannel** aChannel) override;
 
   void OnChannelDone() override;
--- a/dom/file/uri/BlobURLInputStream.cpp
+++ b/dom/file/uri/BlobURLInputStream.cpp
@@ -438,43 +438,43 @@ void BlobURLInputStream::RetrieveBlobDat
             self->mState = State::ERROR;
             self->mError = NS_ERROR_FAILURE;
             self->NotifyWaitTargets(lock);
           });
 }
 
 nsresult BlobURLInputStream::StoreBlobImplStream(
     already_AddRefed<BlobImpl> aBlobImpl, const MutexAutoLock& aProofOfLock) {
-  const RefPtr<BlobImpl> blobImppl = aBlobImpl;
+  const RefPtr<BlobImpl> blobImpl = aBlobImpl;
   nsAutoString contentType;
-  blobImppl->GetType(contentType);
+  blobImpl->GetType(contentType);
   mChannel->SetContentType(NS_ConvertUTF16toUTF8(contentType));
 
   auto cleanupOnExit = MakeScopeExit([&] { mChannel = nullptr; });
 
-  if (blobImppl->IsFile()) {
+  if (blobImpl->IsFile()) {
     nsAutoString filename;
-    blobImppl->GetName(filename);
+    blobImpl->GetName(filename);
     if (!filename.IsEmpty()) {
       mChannel->SetContentDispositionFilename(filename);
     }
   }
 
   mozilla::ErrorResult errorResult;
 
-  mBlobSize = blobImppl->GetSize(errorResult);
+  mBlobSize = blobImpl->GetSize(errorResult);
 
   if (NS_WARN_IF(errorResult.Failed())) {
     return errorResult.StealNSResult();
   }
 
   mChannel->SetContentLength(mBlobSize);
 
   nsCOMPtr<nsIInputStream> inputStream;
-  blobImppl->CreateInputStream(getter_AddRefs(inputStream), errorResult);
+  blobImpl->CreateInputStream(getter_AddRefs(inputStream), errorResult);
 
   if (NS_WARN_IF(errorResult.Failed())) {
     return errorResult.StealNSResult();
   }
 
   if (NS_WARN_IF(!inputStream)) {
     return NS_ERROR_NOT_AVAILABLE;
   }
--- a/dom/file/uri/BlobURLProtocolHandler.cpp
+++ b/dom/file/uri/BlobURLProtocolHandler.cpp
@@ -829,17 +829,17 @@ BlobURLProtocolHandler::NewChannel(nsIUR
       !ChromeUtils::IsOriginAttributesEqualIgnoringFPD(
           aLoadInfo->GetOriginAttributes(),
           BasePrincipal::Cast(info->mPrincipal)->OriginAttributesRef())) {
     return NS_OK;
   }
 
   raii.release();
 
-  channel->Initialize(info->mBlobImpl);
+  channel->Initialize();
   channel.forget(aResult);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BlobURLProtocolHandler::AllowPort(int32_t port, const char* scheme,
                                   bool* _retval) {
   // don't override anything.