From 157648b3fe41a5dc245c6b8a40969592d7c3e0ea Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Wed, 23 Jan 2013 21:39:27 -0500 Subject: [PATCH] Bug 834106 - consolidate ClonedMessageData building code into MessageManagerCallback; r=smaug --- content/base/src/nsFrameLoader.cpp | 26 +----- content/base/src/nsFrameMessageManager.cpp | 102 +++++++++++++++------ content/base/src/nsFrameMessageManager.h | 12 ++- dom/ipc/Blob.h | 6 ++ dom/ipc/ContentParent.cpp | 18 +--- dom/ipc/TabChild.cpp | 37 +------- 6 files changed, 98 insertions(+), 103 deletions(-) diff --git a/content/base/src/nsFrameLoader.cpp b/content/base/src/nsFrameLoader.cpp index 6cde5c611361..0a857b705c3c 100644 --- a/content/base/src/nsFrameLoader.cpp +++ b/content/base/src/nsFrameLoader.cpp @@ -2248,30 +2248,10 @@ nsFrameLoader::DoSendAsyncMessage(const nsAString& aMessage, PBrowserParent* tabParent = GetRemoteBrowser(); if (tabParent) { ClonedMessageData data; - - SerializedStructuredCloneBuffer& buffer = data.data(); - buffer.data = aData.mData; - buffer.dataLength = aData.mDataLength; - - const nsTArray >& blobs = aData.mClosure.mBlobs; - if (!blobs.IsEmpty()) { - InfallibleTArray& blobParents = data.blobsParent(); - - uint32_t length = blobs.Length(); - blobParents.SetCapacity(length); - - ContentParent* cp = static_cast(tabParent->Manager()); - - for (uint32_t i = 0; i < length; ++i) { - BlobParent* blobParent = cp->GetOrCreateActorForBlob(blobs[i]); - if (!blobParent) { - return false; - } - - blobParents.AppendElement(blobParent); - } + ContentParent* cp = static_cast(tabParent->Manager()); + if (!BuildClonedMessageDataForParent(cp, aData, data)) { + return false; } - return tabParent->SendAsyncMessage(nsString(aMessage), data); } diff --git a/content/base/src/nsFrameMessageManager.cpp b/content/base/src/nsFrameMessageManager.cpp index 768511466023..9f0fdbfa88fc 100644 --- a/content/base/src/nsFrameMessageManager.cpp +++ b/content/base/src/nsFrameMessageManager.cpp @@ -119,6 +119,74 @@ NS_INTERFACE_MAP_END NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFrameMessageManager) NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFrameMessageManager) +template +struct DataBlobs +{ }; + +template<> +struct DataBlobs +{ + typedef BlobTraits::ProtocolType ProtocolType; + + static InfallibleTArray& Blobs(ClonedMessageData& aData) + { + return aData.blobsParent(); + } +}; + +template<> +struct DataBlobs +{ + typedef BlobTraits::ProtocolType ProtocolType; + + static InfallibleTArray& Blobs(ClonedMessageData& aData) + { + return aData.blobsChild(); + } +}; + +template +static bool +BuildClonedMessageData(typename BlobTraits::ConcreteContentManagerType* aManager, + const StructuredCloneData& aData, + ClonedMessageData& aClonedData) +{ + SerializedStructuredCloneBuffer& buffer = aClonedData.data(); + buffer.data = aData.mData; + buffer.dataLength = aData.mDataLength; + const nsTArray >& blobs = aData.mClosure.mBlobs; + if (!blobs.IsEmpty()) { + typedef typename BlobTraits::ProtocolType ProtocolType; + InfallibleTArray& blobList = DataBlobs::Blobs(aClonedData); + uint32_t length = blobs.Length(); + blobList.SetCapacity(length); + for (uint32_t i = 0; i < length; ++i) { + Blob* protocolActor = aManager->GetOrCreateActorForBlob(blobs[i]); + if (!protocolActor) { + return false; + } + blobList.AppendElement(protocolActor); + } + } + return true; +} + +bool +MessageManagerCallback::BuildClonedMessageDataForParent(ContentParent* aParent, + const StructuredCloneData& aData, + ClonedMessageData& aClonedData) +{ + return BuildClonedMessageData(aParent, aData, aClonedData); +} + +bool +MessageManagerCallback::BuildClonedMessageDataForChild(ContentChild* aChild, + const StructuredCloneData& aData, + ClonedMessageData& aClonedData) +{ + return BuildClonedMessageData(aChild, aData, aClonedData); +} + // nsIMessageListenerManager NS_IMETHODIMP @@ -1179,21 +1247,8 @@ public: return true; } ClonedMessageData data; - SerializedStructuredCloneBuffer& buffer = data.data(); - buffer.data = aData.mData; - buffer.dataLength = aData.mDataLength; - const nsTArray >& blobs = aData.mClosure.mBlobs; - if (!blobs.IsEmpty()) { - InfallibleTArray& blobChildList = data.blobsChild(); - uint32_t length = blobs.Length(); - blobChildList.SetCapacity(length); - for (uint32_t i = 0; i < length; ++i) { - BlobChild* blobChild = cc->GetOrCreateActorForBlob(blobs[i]); - if (!blobChild) { - return false; - } - blobChildList.AppendElement(blobChild); - } + if (!BuildClonedMessageDataForChild(cc, aData, data)) { + return false; } return cc->SendSyncMessage(nsString(aMessage), data, aJSONRetVal); } @@ -1207,21 +1262,8 @@ public: return true; } ClonedMessageData data; - SerializedStructuredCloneBuffer& buffer = data.data(); - buffer.data = aData.mData; - buffer.dataLength = aData.mDataLength; - const nsTArray >& blobs = aData.mClosure.mBlobs; - if (!blobs.IsEmpty()) { - InfallibleTArray& blobChildList = data.blobsChild(); - uint32_t length = blobs.Length(); - blobChildList.SetCapacity(length); - for (uint32_t i = 0; i < length; ++i) { - BlobChild* blobChild = cc->GetOrCreateActorForBlob(blobs[i]); - if (!blobChild) { - return false; - } - blobChildList.AppendElement(blobChild); - } + if (!BuildClonedMessageDataForChild(cc, aData, data)) { + return false; } return cc->SendAsyncMessage(nsString(aMessage), data); } diff --git a/content/base/src/nsFrameMessageManager.h b/content/base/src/nsFrameMessageManager.h index 1c6d6ab41281..a787393716cb 100644 --- a/content/base/src/nsFrameMessageManager.h +++ b/content/base/src/nsFrameMessageManager.h @@ -1,4 +1,4 @@ -/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -26,7 +26,9 @@ namespace mozilla { namespace dom { class ContentParent; +class ContentChild; struct StructuredCloneData; +class ClonedMessageData; namespace ipc { @@ -76,6 +78,14 @@ public: { return false; } + +protected: + bool BuildClonedMessageDataForParent(ContentParent* aParent, + const StructuredCloneData& aData, + ClonedMessageData& aClonedData); + bool BuildClonedMessageDataForChild(ContentChild* aChild, + const StructuredCloneData& aData, + ClonedMessageData& aClonedData); }; } // namespace ipc diff --git a/dom/ipc/Blob.h b/dom/ipc/Blob.h index e1e569d999e9..120064b64e3a 100644 --- a/dom/ipc/Blob.h +++ b/dom/ipc/Blob.h @@ -25,6 +25,10 @@ class nsIInputStream; namespace mozilla { namespace dom { + +class ContentParent; +class ContentChild; + namespace ipc { enum ActorFlavorEnum @@ -43,6 +47,7 @@ struct BlobTraits typedef mozilla::dom::PBlobParent ProtocolType; typedef mozilla::dom::PBlobStreamParent StreamType; typedef mozilla::dom::PContentParent ContentManagerType; + typedef mozilla::dom::ContentParent ConcreteContentManagerType; typedef ProtocolType BlobManagerType; // BaseType on the parent side is a bit more complicated than for the child @@ -83,6 +88,7 @@ struct BlobTraits typedef mozilla::dom::PBlobChild ProtocolType; typedef mozilla::dom::PBlobStreamChild StreamType; typedef mozilla::dom::PContentChild ContentManagerType; + typedef mozilla::dom::ContentChild ConcreteContentManagerType; typedef ProtocolType BlobManagerType; class BaseType : public ProtocolType diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index 66761b22ac7a..df89a69f75a5 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -2323,23 +2323,9 @@ ContentParent::DoSendAsyncMessage(const nsAString& aMessage, const mozilla::dom::StructuredCloneData& aData) { ClonedMessageData data; - SerializedStructuredCloneBuffer& buffer = data.data(); - buffer.data = aData.mData; - buffer.dataLength = aData.mDataLength; - const nsTArray >& blobs = aData.mClosure.mBlobs; - if (!blobs.IsEmpty()) { - InfallibleTArray& blobParents = data.blobsParent(); - uint32_t length = blobs.Length(); - blobParents.SetCapacity(length); - for (uint32_t i = 0; i < length; ++i) { - BlobParent* blobParent = GetOrCreateActorForBlob(blobs[i]); - if (!blobParent) { - return false; - } - blobParents.AppendElement(blobParent); - } + if (!BuildClonedMessageDataForParent(this, aData, data)) { + return false; } - return SendAsyncMessage(nsString(aMessage), data); } diff --git a/dom/ipc/TabChild.cpp b/dom/ipc/TabChild.cpp index aed29f2accf1..0436c7a6f166 100644 --- a/dom/ipc/TabChild.cpp +++ b/dom/ipc/TabChild.cpp @@ -2018,22 +2018,8 @@ TabChild::DoSendSyncMessage(const nsAString& aMessage, { ContentChild* cc = static_cast(Manager()); ClonedMessageData data; - SerializedStructuredCloneBuffer& buffer = data.data(); - buffer.data = aData.mData; - buffer.dataLength = aData.mDataLength; - - const nsTArray >& blobs = aData.mClosure.mBlobs; - if (!blobs.IsEmpty()) { - InfallibleTArray& blobChildList = data.blobsChild(); - uint32_t length = blobs.Length(); - blobChildList.SetCapacity(length); - for (uint32_t i = 0; i < length; ++i) { - BlobChild* blobChild = cc->GetOrCreateActorForBlob(blobs[i]); - if (!blobChild) { - return false; - } - blobChildList.AppendElement(blobChild); - } + if (!BuildClonedMessageDataForChild(cc, aData, data)) { + return false; } return SendSyncMessage(nsString(aMessage), data, aJSONRetVal); } @@ -2044,24 +2030,9 @@ TabChild::DoSendAsyncMessage(const nsAString& aMessage, { ContentChild* cc = static_cast(Manager()); ClonedMessageData data; - SerializedStructuredCloneBuffer& buffer = data.data(); - buffer.data = aData.mData; - buffer.dataLength = aData.mDataLength; - - const nsTArray >& blobs = aData.mClosure.mBlobs; - if (!blobs.IsEmpty()) { - InfallibleTArray& blobChildList = data.blobsChild(); - uint32_t length = blobs.Length(); - blobChildList.SetCapacity(length); - for (uint32_t i = 0; i < length; ++i) { - BlobChild* blobChild = cc->GetOrCreateActorForBlob(blobs[i]); - if (!blobChild) { - return false; - } - blobChildList.AppendElement(blobChild); - } + if (!BuildClonedMessageDataForChild(cc, aData, data)) { + return false; } - return SendAsyncMessage(nsString(aMessage), data); }