Bug 966439 - BroadcastChannel API - patch 9 - Fix a memory leak of Files, r=bent

* * *
Bug 966439 - BroadcastChannel API - patch 10 - explicit constructors are needed, CLOSED_TREE
This commit is contained in:
Andrea Marchesini 2015-01-15 16:58:42 +00:00
parent 11e0a4cab1
commit 64622babb6
5 changed files with 48 additions and 19 deletions

View File

@ -21,6 +21,10 @@
#include "nsServiceManagerUtils.h"
#include "nsISupportsPrimitives.h"
#ifdef XP_WIN
#undef PostMessage
#endif
namespace mozilla {
using namespace ipc;
@ -237,7 +241,7 @@ class CloseRunnable MOZ_FINAL : public nsICancelableRunnable
public:
NS_DECL_ISUPPORTS
CloseRunnable(BroadcastChannel* aBC)
explicit CloseRunnable(BroadcastChannel* aBC)
: mBC(aBC)
{
MOZ_ASSERT(mBC);
@ -268,7 +272,7 @@ class TeardownRunnable MOZ_FINAL : public nsICancelableRunnable
public:
NS_DECL_ISUPPORTS
TeardownRunnable(BroadcastChannelChild* aActor)
explicit TeardownRunnable(BroadcastChannelChild* aActor)
: mActor(aActor)
{
MOZ_ASSERT(mActor);
@ -356,7 +360,7 @@ class CheckPermissionRunnable MOZ_FINAL
public:
bool mResult;
CheckPermissionRunnable(workers::WorkerPrivate* aWorkerPrivate)
explicit CheckPermissionRunnable(workers::WorkerPrivate* aWorkerPrivate)
: workers::WorkerMainThreadRunnable(aWorkerPrivate)
, mResult(false)
{
@ -422,7 +426,7 @@ BroadcastChannel::BroadcastChannel(nsPIDOMWindow* aWindow,
const nsAString& aChannel)
: DOMEventTargetHelper(aWindow)
, mWorkerFeature(nullptr)
, mPrincipalInfo(aPrincipalInfo)
, mPrincipalInfo(new PrincipalInfo(aPrincipalInfo))
, mOrigin(aOrigin)
, mChannel(aChannel)
, mIsKeptAlive(false)
@ -564,6 +568,14 @@ BroadcastChannel::PostMessageInternal(JSContext* aCx,
return;
}
const nsTArray<nsRefPtr<File>>& blobs = data->mClosure.mBlobs;
for (uint32_t i = 0, len = blobs.Length(); i < len; ++i) {
if (!blobs[i]->Impl()->MayBeClonedToOtherThreads()) {
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
return;
}
}
PostMessageData(data);
}
@ -624,7 +636,7 @@ BroadcastChannel::ActorCreated(PBackgroundChild* aActor)
}
PBroadcastChannelChild* actor =
aActor->SendPBroadcastChannelConstructor(mPrincipalInfo, mOrigin, mChannel);
aActor->SendPBroadcastChannelConstructor(*mPrincipalInfo, mOrigin, mChannel);
mActor = static_cast<BroadcastChannelChild*>(actor);
MOZ_ASSERT(mActor);

View File

@ -8,7 +8,6 @@
#include "mozilla/Attributes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "nsIIPCBackgroundChildCreateCallback.h"
#include "nsIObserver.h"
#include "nsTArray.h"
@ -17,6 +16,11 @@
class nsPIDOMWindow;
namespace mozilla {
namespace ipc {
class PrincipalInfo;
}
namespace dom {
namespace workers {
@ -104,7 +108,8 @@ private:
nsAutoPtr<workers::WorkerFeature> mWorkerFeature;
PrincipalInfo mPrincipalInfo;
nsAutoPtr<PrincipalInfo> mPrincipalInfo;
nsString mOrigin;
nsString mChannel;

View File

@ -41,6 +41,21 @@ BroadcastChannelChild::~BroadcastChannelChild()
bool
BroadcastChannelChild::RecvNotify(const ClonedMessageData& aData)
{
// Make sure to retrieve all blobs from the message before returning to avoid
// leaking their actors.
nsTArray<nsRefPtr<File>> files;
if (!aData.blobsChild().IsEmpty()) {
files.SetCapacity(aData.blobsChild().Length());
for (uint32_t i = 0, len = aData.blobsChild().Length(); i < len; ++i) {
nsRefPtr<FileImpl> impl =
static_cast<BlobChild*>(aData.blobsChild()[i])->GetBlobImpl();
nsRefPtr<File> file = new File(mBC ? mBC->GetOwner() : nullptr, impl);
files.AppendElement(file);
}
}
nsCOMPtr<DOMEventTargetHelper> helper = mBC;
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(helper);
@ -78,18 +93,7 @@ BroadcastChannelChild::RecvNotify(const ClonedMessageData& aData)
StructuredCloneData cloneData;
cloneData.mData = buffer.data;
cloneData.mDataLength = buffer.dataLength;
if (!aData.blobsChild().IsEmpty()) {
cloneData.mClosure.mBlobs.SetCapacity(aData.blobsChild().Length());
for (uint32_t i = 0, len = aData.blobsChild().Length(); i < len; ++i) {
nsRefPtr<FileImpl> impl =
static_cast<BlobChild*>(aData.blobsChild()[i])->GetBlobImpl();
nsRefPtr<File> blob = new File(mBC->GetOwner(), impl);
cloneData.mClosure.mBlobs.AppendElement(blob);
}
}
cloneData.mClosure.mBlobs.SwapElements(files);
JS::Rooted<JS::Value> value(cx, JS::NullValue());
if (cloneData.mDataLength && !ReadStructuredClone(cx, cloneData, &value)) {

View File

@ -7,6 +7,10 @@
#include "BroadcastChannelParent.h"
#include "mozilla/ipc/BackgroundParent.h"
#ifdef XP_WIN
#undef PostMessage
#endif
namespace mozilla {
using namespace ipc;

View File

@ -9,6 +9,10 @@
#include "nsHashKeys.h"
#include "nsTHashtable.h"
#ifdef XP_WIN
#undef PostMessage
#endif
namespace mozilla {
namespace dom {