Bug 1619953 - P6 - BlobURLChannel creates BlobURLInputStream only when content stream is opened r=baku,#dom-workers-and-storage draft
authorssengupta <ssengupta@mozilla.com>
Wed, 01 Jul 2020 13:07:31 +0000
changeset 3016295 54a4d396756e760d54adf776b0a562f8f95fc3e5
parent 3016294 d7b277d3fec0c377b9e3ff24caf37e544d1fd7fe
child 3016296 c4b761c2ba319f38539334c1873cab577c648432
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 - P6 - BlobURLChannel creates BlobURLInputStream only when content stream is opened r=baku,#dom-workers-and-storage Summary: Additionally, BlobURLChannel holds no strong reference to BlobURLInputStream. This is to avoid circular dependency. Differential Revision: https://phabricator.services.mozilla.com/D81452 Depends on D81126 Test Plan: Reviewers: baku, #dom-workers-and-storage Subscribers: Bug #: 1619953 Differential Diff: PHID-DIFF-ylyogfgkf5idn4ly3xjq
dom/file/uri/BlobURLChannel.cpp
dom/file/uri/BlobURLChannel.h
dom/file/uri/BlobURLProtocolHandler.cpp
--- a/dom/file/uri/BlobURLChannel.cpp
+++ b/dom/file/uri/BlobURLChannel.cpp
@@ -8,84 +8,70 @@
 #include "mozilla/dom/BlobImpl.h"
 #include "mozilla/dom/BlobURL.h"
 #include "mozilla/dom/BlobURLInputStream.h"
 #include "mozilla/ScopeExit.h"
 
 using namespace mozilla::dom;
 
 BlobURLChannel::BlobURLChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo)
-    : mInitialized(false) {
+    : 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;
 
-void BlobURLChannel::InitFailed() {
-  MOZ_ASSERT(!mInitialized);
-  MOZ_ASSERT(!mInputStream);
-  mInitialized = true;
-}
+nsresult BlobURLChannel::OpenContentStream(bool aAsync,
+                                           nsIInputStream** aResult,
+                                           nsIChannel** aChannel) {
+  if (mContentStreamOpened) {
+    return NS_ERROR_ALREADY_OPENED;
+  }
 
-void BlobURLChannel::Initialize() {
-  MOZ_ASSERT(!mInitialized);
-
-  auto cleanupOnEarlyExit = MakeScopeExit([&] { InitFailed(); });
+  mContentStreamOpened = true;
 
   nsCOMPtr<nsIURI> uri;
   nsresult rv = GetURI(getter_AddRefs(uri));
-  NS_ENSURE_SUCCESS_VOID(rv);
+  NS_ENSURE_SUCCESS(rv, NS_ERROR_MALFORMED_URI);
 
   RefPtr<BlobURL> blobURL;
   rv = uri->QueryInterface(kHOSTOBJECTURICID, getter_AddRefs(blobURL));
 
   if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!blobURL)) {
-    return;
+    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;
+      return NS_ERROR_MALFORMED_URI;
     }
 #else
-    return;
+    return NS_ERROR_MALFORMED_URI;
 #endif
   }
 
-  mInputStream = BlobURLInputStream::Create(this, blobURL);
-  if (NS_WARN_IF(!mInputStream)) {
-    return;
-  }
-  cleanupOnEarlyExit.release();
-  mInitialized = true;
-}
-
-nsresult BlobURLChannel::OpenContentStream(bool aAsync,
-                                           nsIInputStream** aResult,
-                                           nsIChannel** aChannel) {
-  MOZ_ASSERT(mInitialized);
-
-  if (!mInputStream) {
+  nsCOMPtr<nsIInputStream> inputStream =
+      BlobURLInputStream::Create(this, blobURL);
+  if (NS_WARN_IF(!inputStream)) {
     return NS_ERROR_MALFORMED_URI;
   }
 
   EnableSynthesizedProgressEvents(true);
 
-  nsCOMPtr<nsIInputStream> stream = mInputStream;
-  stream.forget(aResult);
+  inputStream.forget(aResult);
 
   return NS_OK;
 }
 
-void BlobURLChannel::OnChannelDone() { mInputStream = nullptr; }
+void BlobURLChannel::OnChannelDone() {}
--- a/dom/file/uri/BlobURLChannel.h
+++ b/dom/file/uri/BlobURLChannel.h
@@ -17,36 +17,23 @@ namespace mozilla {
 namespace dom {
 
 class BlobImpl;
 
 class BlobURLChannel final : public nsBaseChannel {
  public:
   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();
-
  private:
   ~BlobURLChannel();
 
   nsresult OpenContentStream(bool aAsync, nsIInputStream** aResult,
                              nsIChannel** aChannel) override;
 
   void OnChannelDone() override;
 
-  // If Initialize() is called, this will contain the blob's inputStream.
-  nsCOMPtr<nsIInputStream> mInputStream;
-
-  // This boolean is used to check that InitFailed() or Initialize() are called
-  // just once.
-  bool mInitialized;
+  bool mContentStreamOpened;
 };
 
 }  // namespace dom
 }  // namespace mozilla
 
 #endif /* mozilla_dom_BlobURLChannel_h */
--- a/dom/file/uri/BlobURLProtocolHandler.cpp
+++ b/dom/file/uri/BlobURLProtocolHandler.cpp
@@ -780,17 +780,16 @@ BlobURLProtocolHandler::GetFlagsForURI(n
 
 NS_IMETHODIMP
 BlobURLProtocolHandler::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
                                    nsIChannel** aResult) {
   auto channel = MakeRefPtr<BlobURLChannel>(aURI, aLoadInfo);
   if (!channel) {
     return NS_ERROR_NOT_INITIALIZED;
   }
-  channel->Initialize();
   channel.forget(aResult);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BlobURLProtocolHandler::AllowPort(int32_t port, const char* scheme,
                                   bool* _retval) {
   // don't override anything.