Bug 1315913 - Part 2: Convert BroadcastChannel to use StructuredCloneDataNoTransfers instead of copy-and-paste. r=baku
authorAndrew Sutherland <asutherland@asutherland.org>
Tue, 08 Nov 2016 03:09:31 -0500
changeset 323606 ae4bdab067693e304e7e924ba5a3332e69a5b2d3
parent 323605 06e8705b9542e52f6aa81471d568cb6559f7114d
child 323607 1eb14c93e19767628f50051ade4362941f8c85d8
push id21
push usermaklebus@msu.edu
push dateThu, 01 Dec 2016 06:22:08 +0000
reviewersbaku
bugs1315913
milestone53.0a1
Bug 1315913 - Part 2: Convert BroadcastChannel to use StructuredCloneDataNoTransfers instead of copy-and-paste. r=baku
dom/broadcastchannel/BroadcastChannel.cpp
dom/broadcastchannel/BroadcastChannelChild.cpp
--- a/dom/broadcastchannel/BroadcastChannel.cpp
+++ b/dom/broadcastchannel/BroadcastChannel.cpp
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "BroadcastChannel.h"
 #include "BroadcastChannelChild.h"
 #include "mozilla/dom/BroadcastChannelBinding.h"
 #include "mozilla/dom/Navigator.h"
 #include "mozilla/dom/File.h"
 #include "mozilla/dom/StructuredCloneHolder.h"
+#include "mozilla/dom/ipc/StructuredCloneData.h"
 #include "mozilla/ipc/BackgroundChild.h"
 #include "mozilla/ipc/BackgroundUtils.h"
 #include "mozilla/ipc/PBackgroundChild.h"
 #include "nsContentUtils.h"
 #include "WorkerPrivate.h"
 #include "WorkerRunnable.h"
 
 #include "nsIBFCacheEntry.h"
@@ -27,25 +28,25 @@
 
 namespace mozilla {
 
 using namespace ipc;
 
 namespace dom {
 
 using namespace workers;
+using namespace ipc;
 
-class BroadcastChannelMessage final : public StructuredCloneHolder
+class BroadcastChannelMessage final : public StructuredCloneDataNoTransfers
 {
 public:
   NS_INLINE_DECL_REFCOUNTING(BroadcastChannelMessage)
 
   BroadcastChannelMessage()
-    : StructuredCloneHolder(CloningSupported, TransferringNotSupported,
-                            StructuredCloneScope::DifferentProcess)
+    : StructuredCloneDataNoTransfers()
   {}
 
 private:
   ~BroadcastChannelMessage()
   {}
 };
 
 namespace {
@@ -146,43 +147,17 @@ public:
   NS_IMETHOD Run() override
   {
     MOZ_ASSERT(mActor);
     if (mActor->IsActorDestroyed()) {
       return NS_OK;
     }
 
     ClonedMessageData message;
-
-    bool success;
-    SerializedStructuredCloneBuffer& buffer = message.data();
-    auto iter = mData->BufferData().Iter();
-    buffer.data = mData->BufferData().Borrow<js::SystemAllocPolicy>(iter, mData->BufferData().Size(), &success);
-    if (NS_WARN_IF(!success)) {
-      return NS_OK;
-    }
-
-    PBackgroundChild* backgroundManager = mActor->Manager();
-    MOZ_ASSERT(backgroundManager);
-
-    const nsTArray<RefPtr<BlobImpl>>& blobImpls = mData->BlobImpls();
-
-    if (!blobImpls.IsEmpty()) {
-      message.blobsChild().SetCapacity(blobImpls.Length());
-
-      for (uint32_t i = 0, len = blobImpls.Length(); i < len; ++i) {
-        PBlobChild* blobChild =
-          BackgroundChild::GetOrCreateActorForBlobImpl(backgroundManager,
-                                                       blobImpls[i]);
-        MOZ_ASSERT(blobChild);
-
-        message.blobsChild().AppendElement(blobChild);
-      }
-    }
-
+    mData->BuildClonedMessageDataForBackgroundChild(mActor->Manager(), message);
     mActor->SendPostMessage(message);
     return NS_OK;
   }
 
   nsresult Cancel() override
   {
     mActor = nullptr;
     return NS_OK;
--- a/dom/broadcastchannel/BroadcastChannelChild.cpp
+++ b/dom/broadcastchannel/BroadcastChannelChild.cpp
@@ -38,27 +38,18 @@ BroadcastChannelChild::~BroadcastChannel
   MOZ_ASSERT(!mBC);
 }
 
 mozilla::ipc::IPCResult
 BroadcastChannelChild::RecvNotify(const ClonedMessageData& aData)
 {
   // Make sure to retrieve all blobs from the message before returning to avoid
   // leaking their actors.
-  nsTArray<RefPtr<BlobImpl>> blobs;
-  if (!aData.blobsChild().IsEmpty()) {
-    blobs.SetCapacity(aData.blobsChild().Length());
-
-    for (uint32_t i = 0, len = aData.blobsChild().Length(); i < len; ++i) {
-      RefPtr<BlobImpl> impl =
-        static_cast<BlobChild*>(aData.blobsChild()[i])->GetBlobImpl();
-
-      blobs.AppendElement(impl);
-    }
-  }
+  ipc::StructuredCloneDataNoTransfers cloneData;
+  cloneData.BorrowFromClonedMessageDataForBackgroundChild(aData);
 
   nsCOMPtr<DOMEventTargetHelper> helper = mBC;
   nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(helper);
 
   // The object is going to be deleted soon. No notify is required.
   if (!eventTarget) {
     return IPC_OK();
   }
@@ -82,25 +73,20 @@ BroadcastChannelChild::RecvNotify(const 
     globalObject = workerPrivate->GlobalScope();
   }
 
   if (!globalObject || !jsapi.Init(globalObject)) {
     NS_WARNING("Failed to initialize AutoJSAPI object.");
     return IPC_OK();
   }
 
-  ipc::StructuredCloneData cloneData;
-  cloneData.BlobImpls().AppendElements(blobs);
-
-  const SerializedStructuredCloneBuffer& buffer = aData.data();
   JSContext* cx = jsapi.cx();
   JS::Rooted<JS::Value> value(cx, JS::NullValue());
-  if (buffer.data.Size()) {
+  if (cloneData.DataLength()) {
     ErrorResult rv;
-    cloneData.UseExternalData(buffer.data);
     cloneData.Read(cx, &value, rv);
     if (NS_WARN_IF(rv.Failed())) {
       rv.SuppressException();
       return IPC_OK();
     }
   }
 
   RootedDictionary<MessageEventInit> init(cx);