[mq]: alt-data-stub.patch draft
authorValentin Gosu <valentin.gosu@gmail.com>
Fri, 20 May 2016 02:56:17 +0200
changeset 368991 ad706c54a6a1f165f7b9225f802cfa4e31822eff
parent 368990 7965752d2b214e0bc60abd052ec4eb53b0131d05
child 368992 0d86e9623e2e095ad1303c5c459201e017076df8
push id18689
push uservalentin.gosu@gmail.com
push dateFri, 20 May 2016 00:56:51 +0000
milestone49.0a1
[mq]: alt-data-stub.patch MozReview-Commit-ID: 7P0c8m3jWmy * * * [mq]: diffy.patch MozReview-Commit-ID: 1LddDUSbsCO
netwerk/cache2/CacheEntry.cpp
netwerk/cache2/CacheEntry.h
--- a/netwerk/cache2/CacheEntry.cpp
+++ b/netwerk/cache2/CacheEntry.cpp
@@ -1200,18 +1200,22 @@ nsresult CacheEntry::OpenOutputStreamInt
 
   // No need to sync on mUseDisk here, we don't need to be consistent
   // with content of the memory storage entries hash table.
   if (!mUseDisk) {
     rv = mFile->SetMemoryOnly();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  RefPtr<CacheOutputCloseListener> listener =
-    new CacheOutputCloseListener(this);
+  RefPtr<CacheOutputCloseListener> listener;
+  if (mCloseListener) {
+    listener = mCloseListener;
+  } else {
+    listener = new CacheOutputCloseListener(this);
+  }
 
   nsCOMPtr<nsIOutputStream> stream;
   rv = mFile->OpenOutputStream(listener, getter_AddRefs(stream));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsISeekableStream> seekable =
     do_QueryInterface(stream, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -1221,23 +1225,41 @@ nsresult CacheEntry::OpenOutputStreamInt
 
   // Prevent opening output stream again.
   mHasData = true;
 
   stream.swap(*_retval);
   return NS_OK;
 }
 
+class AltDataCloseListener
+  : public CacheOutputCloseListener
+{
+public:
+  explicit AltDataCloseListener(CacheEntry* aEntry)
+    : CacheOutputCloseListener(aEntry) { }
+  NS_IMETHOD Run() override
+  {
+    printf("====== Setting alt-data header\n");
+    mEntry->SetMetaDataElement("alt-data", "text/binary");
+    return NS_OK;
+  }
+private:
+  virtual ~AltDataCloseListener() {}
+};
+
 NS_IMETHODIMP CacheEntry::OpenAlternativeOutputStream(const nsACString & type, nsIOutputStream * *_retval)
 {
   // Check metadata
   // Open output stream
   // When closing output stream set the metadata offset+type using CacheOutputCloseListener
   // TODO: fail if there is an alternative input stream open
-  return NS_ERROR_NOT_IMPLEMENTED;
+
+  mCloseListener = new AltDataCloseListener(this);
+  return OpenOutputStream(0, _retval);
 }
 
 NS_IMETHODIMP CacheEntry::OpenAlternativeInputStream(const nsACString & type, nsIInputStream * *_retval)
 {
   nsresult rv;
   nsXPIDLCString altDataMetadata;
   rv = GetMetaDataElement("alt-data", getter_Copies(altDataMetadata));
 
@@ -1273,18 +1295,19 @@ NS_IMETHODIMP CacheEntry::OpenAlternativ
   mozilla::Unused << p.ReadUntil(Tokenizer::Token::Char(';'), availableAltData);
 
   // If the alt-data type we find in the metadata matches the type
   // preferred by consumers, we will serve that one.
   if (availableAltData != type) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
+  printf("Opening alt-data output stream\n");
   // TODO: Open the actual input stream
-  return NS_OK;
+  return OpenInputStream(0, _retval);
 }
 
 NS_IMETHODIMP CacheEntry::GetPredictedDataSize(int64_t *aPredictedDataSize)
 {
   *aPredictedDataSize = mPredictedDataSize;
   return NS_OK;
 }
 NS_IMETHODIMP CacheEntry::SetPredictedDataSize(int64_t aPredictedDataSize)
--- a/netwerk/cache2/CacheEntry.h
+++ b/netwerk/cache2/CacheEntry.h
@@ -276,16 +276,17 @@ private:
 
   mozilla::Mutex mLock;
 
   // Reflects the number of existing handles for this entry
   ::mozilla::ThreadSafeAutoRefCnt mHandlesCount;
 
   nsTArray<Callback> mCallbacks;
   nsCOMPtr<nsICacheEntryDoomCallback> mDoomCallback;
+  RefPtr<CacheOutputCloseListener> mCloseListener;
 
   RefPtr<CacheFile> mFile;
 
   // Using ReleaseAcquire since we only control access to mFile with this.
   // When mFileStatus is read and found success it is ensured there is mFile and
   // that it is after a successful call to Init().
   ::mozilla::Atomic<nsresult, ::mozilla::ReleaseAcquire> mFileStatus;
   nsCString mURI;
@@ -387,29 +388,29 @@ public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_FORWARD_NSICACHEENTRY(mEntry->)
 private:
   virtual ~CacheEntryHandle();
   RefPtr<CacheEntry> mEntry;
 };
 
 
-class CacheOutputCloseListener final : public Runnable
+class CacheOutputCloseListener : public Runnable
 {
 public:
   void OnOutputClosed();
 
-private:
+protected:
   friend class CacheEntry;
 
   virtual ~CacheOutputCloseListener();
 
   NS_DECL_NSIRUNNABLE
   explicit CacheOutputCloseListener(CacheEntry* aEntry);
 
-private:
+protected:
   RefPtr<CacheEntry> mEntry;
 };
 
 } // namespace net
 } // namespace mozilla
 
 #endif