Bug 1397128 P3 Add an OpenStreamAction class to open the file stream on the io thread. r=tt
☠☠ backed out by 3011315fd77c ☠ ☠
authorBen Kelly <ben@wanderview.com>
Fri, 15 Sep 2017 09:11:22 -0700
changeset 665630 bfbcc07fcb4bdb198e42424702c90d128c0ffaa7
parent 665629 15edfaab92f6d812256252e8efc6b8574cd7c381
child 665631 389e9f39d6b8b2e7614511281ede2d7ce83839b5
push id80127
push userbmo:ato@sny.no
push dateFri, 15 Sep 2017 19:43:59 +0000
reviewerstt
bugs1397128
milestone57.0a1
Bug 1397128 P3 Add an OpenStreamAction class to open the file stream on the io thread. r=tt
dom/cache/Manager.cpp
dom/cache/Manager.h
--- a/dom/cache/Manager.cpp
+++ b/dom/cache/Manager.cpp
@@ -1447,16 +1447,53 @@ public:
 
 private:
   const Namespace mNamespace;
   nsTArray<nsString> mKeys;
 };
 
 // ----------------------------------------------------------------------------
 
+class Manager::OpenStreamAction final : public Manager::BaseAction
+{
+public:
+  OpenStreamAction(Manager* aManager, ListenerId aListenerId,
+                   OpenStreamResolver&& aResolver, const nsID& aBodyId)
+    : BaseAction(aManager, aListenerId)
+    , mResolver(Move(aResolver))
+    , mBodyId(aBodyId)
+  { }
+
+  virtual nsresult
+  RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
+                        mozIStorageConnection* aConn) override
+  {
+    nsresult rv = BodyOpen(aQuotaInfo, aDBDir, mBodyId,
+                           getter_AddRefs(mBodyStream));
+    if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
+    if (NS_WARN_IF(!mBodyStream)) { return NS_ERROR_FILE_NOT_FOUND; }
+
+    return rv;
+  }
+
+  virtual void
+  Complete(Listener* aListener, ErrorResult&& aRv) override
+  {
+    mResolver(Move(mBodyStream));
+    mResolver = nullptr;
+  }
+
+private:
+  OpenStreamResolver mResolver;
+  const nsID mBodyId;
+  nsCOMPtr<nsIInputStream> mBodyStream;
+};
+
+// ----------------------------------------------------------------------------
+
 //static
 Manager::ListenerId Manager::sNextListenerId = 0;
 
 void
 Manager::Listener::OnOpComplete(ErrorResult&& aRv, const CacheOpResult& aResult)
 {
   OnOpComplete(Move(aRv), aResult, INVALID_CACHE_ID, nsTArray<SavedResponse>(),
                nsTArray<SavedRequest>(), nullptr);
@@ -1814,16 +1851,44 @@ Manager::ExecuteStorageOp(Listener* aLis
     default:
       MOZ_CRASH("Unknown CacheStorage operation!");
   }
 
   context->Dispatch(action);
 }
 
 void
+Manager::ExecuteOpenStream(Listener* aListener, OpenStreamResolver&& aResolver,
+                           const nsID& aBodyId)
+{
+  NS_ASSERT_OWNINGTHREAD(Manager);
+  MOZ_DIAGNOSTIC_ASSERT(aListener);
+  MOZ_DIAGNOSTIC_ASSERT(aResolver);
+
+  if (NS_WARN_IF(mState == Closing)) {
+    aResolver(nullptr);
+    return;
+  }
+
+  RefPtr<Context> context = mContext;
+  MOZ_DIAGNOSTIC_ASSERT(!context->IsCanceled());
+
+  // We save the listener simply to track the existence of the caller here.
+  // Our returned value will really be passed to the resolver when the
+  // operation completes.  In the future we should remove the Listener
+  // mechanism in favor of std::function or MozPromise.
+  ListenerId listenerId = SaveListener(aListener);
+
+  RefPtr<Action> action =
+    new OpenStreamAction(this, listenerId, Move(aResolver), aBodyId);
+
+  context->Dispatch(action);
+}
+
+void
 Manager::ExecutePutAll(Listener* aListener, CacheId aCacheId,
                        const nsTArray<CacheRequestResponse>& aPutList,
                        const nsTArray<nsCOMPtr<nsIInputStream>>& aRequestStreamList,
                        const nsTArray<nsCOMPtr<nsIInputStream>>& aResponseStreamList)
 {
   NS_ASSERT_OWNINGTHREAD(Manager);
   MOZ_DIAGNOSTIC_ASSERT(aListener);
 
--- a/dom/cache/Manager.h
+++ b/dom/cache/Manager.h
@@ -172,16 +172,24 @@ public:
   void ExecutePutAll(Listener* aListener, CacheId aCacheId,
                      const nsTArray<CacheRequestResponse>& aPutList,
                      const nsTArray<nsCOMPtr<nsIInputStream>>& aRequestStreamList,
                      const nsTArray<nsCOMPtr<nsIInputStream>>& aResponseStreamList);
 
   void ExecuteStorageOp(Listener* aListener, Namespace aNamespace,
                         const CacheOpArgs& aOpArgs);
 
+  typedef std::function<void(nsCOMPtr<nsIInputStream>&&)> OpenStreamResolver;
+  void ExecuteOpenStream(Listener* aListener, OpenStreamResolver&& aResolver,
+                         const nsID& aBodyId);
+
+  void
+  NoteStreamOpenComplete(const nsID& aBodyId, ErrorResult&& aRv,
+                         nsCOMPtr<nsIInputStream>&& aBodyStream);
+
 private:
   class Factory;
   class BaseAction;
   class DeleteOrphanedCacheAction;
 
   class CacheMatchAction;
   class CacheMatchAllAction;
   class CachePutAllAction;
@@ -189,16 +197,18 @@ private:
   class CacheKeysAction;
 
   class StorageMatchAction;
   class StorageHasAction;
   class StorageOpenAction;
   class StorageDeleteAction;
   class StorageKeysAction;
 
+  class OpenStreamAction;
+
   typedef uint64_t ListenerId;
 
   Manager(ManagerId* aManagerId, nsIThread* aIOThread);
   ~Manager();
   void Init(Manager* aOldManager);
   void Shutdown();
 
   void Abort();