Bug 1194891. P1 - plumb 'isPrivateBrowsing' down to MediaCacheStream. draft
authorJW Wang <jwwang@mozilla.com>
Thu, 06 Apr 2017 16:20:00 +0800
changeset 557702 6275a979aeaa51a6db3e5770aaa7cc0a78fb641b
parent 557660 36a9db5c9107bdefafa2090b3cfb4dfcafb6bcea
child 557703 66b817dfa81a7af16d6836fc5cf2658b7afc4ace
push id52791
push userjwwang@mozilla.com
push dateFri, 07 Apr 2017 06:23:27 +0000
bugs1194891
milestone55.0a1
Bug 1194891. P1 - plumb 'isPrivateBrowsing' down to MediaCacheStream. MozReview-Commit-ID: EBKkOfK7K1p
dom/html/HTMLMediaElement.cpp
dom/media/MediaCache.cpp
dom/media/MediaCache.h
dom/media/MediaResource.cpp
dom/media/MediaResource.h
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -4662,18 +4662,19 @@ nsresult HTMLMediaElement::InitializeDec
     NS_ConvertUTF8toUTF16 mimeUTF16(mimeType);
     const char16_t* params[] = { mimeUTF16.get(), src.get() };
     ReportLoadError("MediaLoadUnsupportedMimeType", params, ArrayLength(params));
     return NS_ERROR_FAILURE;
   }
 
   LOG(LogLevel::Debug, ("%p Created decoder %p for type %s", this, decoder.get(), mimeType.get()));
 
-  RefPtr<MediaResource> resource =
-    MediaResource::Create(decoder->GetResourceCallback(), aChannel);
+  bool isPrivateBrowsing = nsContentUtils::IsInPrivateBrowsing(OwnerDoc());
+  RefPtr<MediaResource> resource = MediaResource::Create(
+    decoder->GetResourceCallback(), aChannel, isPrivateBrowsing);
 
   if (!resource) {
     decoder->Shutdown();
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
   if (mChannelLoader) {
     mChannelLoader->Done();
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -358,34 +358,36 @@ MediaCacheFlusher::Observe(nsISupports *
     MediaCache::Flush();
   }
   if (strcmp(aTopic, "cacheservice:empty-cache") == 0) {
     MediaCache::Flush();
   }
   return NS_OK;
 }
 
-MediaCacheStream::MediaCacheStream(ChannelMediaResource* aClient)
+MediaCacheStream::MediaCacheStream(ChannelMediaResource* aClient,
+                                   bool aIsPrivateBrowsing)
   : mClient(aClient),
     mInitialized(false),
     mHasHadUpdate(false),
     mClosed(false),
     mDidNotifyDataEnded(false),
     mResourceID(0),
     mIsTransportSeekable(false),
     mCacheSuspended(false),
     mChannelEnded(false),
     mChannelOffset(0),
     mStreamLength(-1),
     mStreamOffset(0),
     mPlaybackBytesPerSecond(10000),
     mPinCount(0),
     mCurrentMode(MODE_PLAYBACK),
     mMetadataInPartialBlockBuffer(false),
-    mPartialBlockBuffer(MakeUnique<int64_t[]>(BLOCK_SIZE/sizeof(int64_t)))
+    mPartialBlockBuffer(MakeUnique<int64_t[]>(BLOCK_SIZE/sizeof(int64_t))),
+    mIsPrivateBrowsing(aIsPrivateBrowsing)
 {
 }
 
 size_t MediaCacheStream::SizeOfExcludingThis(
                                 MallocSizeOf aMallocSizeOf) const
 {
   // Looks like these are not owned:
   // - mClient
--- a/dom/media/MediaCache.h
+++ b/dom/media/MediaCache.h
@@ -190,17 +190,17 @@ public:
 
   enum ReadMode {
     MODE_METADATA,
     MODE_PLAYBACK
   };
 
   // aClient provides the underlying transport that cache will use to read
   // data for this stream.
-  explicit MediaCacheStream(ChannelMediaResource* aClient);
+  MediaCacheStream(ChannelMediaResource* aClient, bool aIsPrivateBrowsing);
   ~MediaCacheStream();
 
   // Set up this stream with the cache. Can fail on OOM. One
   // of InitAsClone or Init must be called before any other method on
   // this class. Does nothing if already initialized.
   nsresult Init();
 
   // Set up this stream with the cache, assuming it's for the same data
@@ -507,13 +507,16 @@ private:
   // Data received for the block containing mChannelOffset. Data needs
   // to wait here so we can write back a complete block. The first
   // mChannelOffset%BLOCK_SIZE bytes have been filled in with good data,
   // the rest are garbage.
   // Use int64_t so that the data is well-aligned.
   // Heap allocate this buffer since the exact power-of-2 will cause allocation
   // slop when combined with the rest of the object members.
   UniquePtr<int64_t[]> mPartialBlockBuffer;
+
+  // True if associated with a private browsing window.
+  const bool mIsPrivateBrowsing;
 };
 
 } // namespace mozilla
 
 #endif
--- a/dom/media/MediaResource.cpp
+++ b/dom/media/MediaResource.cpp
@@ -63,22 +63,23 @@ MediaResource::Destroy()
 
 NS_IMPL_ADDREF(MediaResource)
 NS_IMPL_RELEASE_WITH_DESTROY(MediaResource, Destroy())
 NS_IMPL_QUERY_INTERFACE0(MediaResource)
 
 ChannelMediaResource::ChannelMediaResource(MediaResourceCallback* aCallback,
                                            nsIChannel* aChannel,
                                            nsIURI* aURI,
-                                           const MediaContainerType& aContainerType)
+                                           const MediaContainerType& aContainerType,
+                                           bool aIsPrivateBrowsing)
   : BaseMediaResource(aCallback, aChannel, aURI, aContainerType),
     mOffset(0),
     mReopenOnError(false),
     mIgnoreClose(false),
-    mCacheStream(this),
+    mCacheStream(this, aIsPrivateBrowsing),
     mLock("ChannelMediaResource.mLock"),
     mIgnoreResume(false),
     mSuspendAgent(mChannel)
 {
 }
 
 ChannelMediaResource::~ChannelMediaResource()
 {
@@ -1492,17 +1493,18 @@ int64_t FileMediaResource::Tell()
   int64_t offset = 0;
   // Return mSize as offset (end of stream) in case of error
   if (!mSeekable || NS_FAILED(mSeekable->Tell(&offset)))
     return mSize;
   return offset;
 }
 
 already_AddRefed<MediaResource>
-MediaResource::Create(MediaResourceCallback* aCallback, nsIChannel* aChannel)
+MediaResource::Create(MediaResourceCallback* aCallback,
+                      nsIChannel* aChannel, bool aIsPrivateBrowsing)
 {
   NS_ASSERTION(NS_IsMainThread(),
                "MediaResource::Open called on non-main thread");
 
   // If the channel was redirected, we want the post-redirect URI;
   // but if the URI scheme was expanded, say from chrome: to jar:file:,
   // we want the original URI.
   nsCOMPtr<nsIURI> uri;
@@ -1516,17 +1518,18 @@ MediaResource::Create(MediaResourceCallb
     return nullptr;
   }
 
   nsCOMPtr<nsIFileChannel> fc = do_QueryInterface(aChannel);
   RefPtr<MediaResource> resource;
   if (fc || IsBlobURI(uri)) {
     resource = new FileMediaResource(aCallback, aChannel, uri, *containerType);
   } else {
-    resource = new ChannelMediaResource(aCallback, aChannel, uri, *containerType);
+    resource = new ChannelMediaResource(
+      aCallback, aChannel, uri, *containerType, aIsPrivateBrowsing);
   }
   return resource.forget();
 }
 
 void BaseMediaResource::SetLoadInBackground(bool aLoadInBackground) {
   if (aLoadInBackground == mLoadInBackground) {
     return;
   }
--- a/dom/media/MediaResource.h
+++ b/dom/media/MediaResource.h
@@ -311,17 +311,19 @@ public:
   // for an HTTP network stream this returns true if HTTP1.1 Byte Range
   // requests are supported by the connection/server.
   virtual bool IsTransportSeekable() = 0;
 
   /**
    * Create a resource, reading data from the channel. Call on main thread only.
    * The caller must follow up by calling resource->Open().
    */
-  static already_AddRefed<MediaResource> Create(MediaResourceCallback* aCallback, nsIChannel* aChannel);
+  static already_AddRefed<MediaResource>
+  Create(MediaResourceCallback* aCallback,
+         nsIChannel* aChannel, bool aIsPrivateBrowsing);
 
   /**
    * Open the stream. This creates a stream listener and returns it in
    * aStreamListener; this listener needs to be notified of incoming data.
    */
   virtual nsresult Open(nsIStreamListener** aStreamListener) = 0;
 
   /**
@@ -506,17 +508,18 @@ private:
  * thread operations are delegated directly to that object.
  */
 class ChannelMediaResource : public BaseMediaResource
 {
 public:
   ChannelMediaResource(MediaResourceCallback* aDecoder,
                        nsIChannel* aChannel,
                        nsIURI* aURI,
-                       const MediaContainerType& aContainerType);
+                       const MediaContainerType& aContainerType,
+                       bool aIsPrivateBrowsing = false);
   ~ChannelMediaResource();
 
   // These are called on the main thread by MediaCache. These must
   // not block or grab locks, because the media cache is holding its lock.
   // Notify that data is available from the cache. This can happen even
   // if this stream didn't read any data, since another stream might have
   // received data for the same resource.
   void CacheClientNotifyDataReceived();