Bug 1184557 - part 2 - StructuredCloneHelperInternal::Shutdown, r=smaug

This commit is contained in:
Andrea Marchesini 2015-07-24 12:12:51 +01:00
parent e3ad172cba
commit 5b0ddb9562
3 changed files with 46 additions and 2 deletions

View File

@ -207,6 +207,8 @@ public:
virtual
~ConsoleRunnable()
{
// Shutdown the StructuredCloneHelperInternal class.
Shutdown();
}
bool

View File

@ -89,7 +89,7 @@ void
StructuredCloneCallbacksError(JSContext* aCx,
uint32_t aErrorId)
{
NS_WARNING("Failed to clone data for the Console API in workers.");
NS_WARNING("Failed to clone data.");
}
const JSStructuredCloneCallbacks gCallbacks = {
@ -105,11 +105,36 @@ const JSStructuredCloneCallbacks gCallbacks = {
// StructuredCloneHelperInternal class
StructuredCloneHelperInternal::StructuredCloneHelperInternal()
#ifdef DEBUG
: mShutdownCalled(false)
#endif
{}
StructuredCloneHelperInternal::~StructuredCloneHelperInternal()
{
#ifdef DEBUG
MOZ_ASSERT(mShutdownCalled);
#endif
}
void
StructuredCloneHelperInternal::Shutdown()
{
#ifdef DEBUG
MOZ_ASSERT(!mShutdownCalled, "Shutdown already called!");
mShutdownCalled = true;
#endif
mBuffer = nullptr;
}
bool
StructuredCloneHelperInternal::Write(JSContext* aCx,
JS::Handle<JS::Value> aValue)
{
MOZ_ASSERT(!mBuffer, "Double Write is not allowed");
MOZ_ASSERT(!mShutdownCalled, "This method cannot be called after Shutdown.");
mBuffer = new JSAutoStructuredCloneBuffer(&gCallbacks, this);
return mBuffer->write(aCx, aValue, &gCallbacks, this);
@ -121,6 +146,7 @@ StructuredCloneHelperInternal::Write(JSContext* aCx,
JS::Handle<JS::Value> aTransfer)
{
MOZ_ASSERT(!mBuffer, "Double Write is not allowed");
MOZ_ASSERT(!mShutdownCalled, "This method cannot be called after Shutdown.");
mBuffer = new JSAutoStructuredCloneBuffer(&gCallbacks, this);
return mBuffer->write(aCx, aValue, aTransfer, &gCallbacks, this);
@ -131,6 +157,7 @@ StructuredCloneHelperInternal::Read(JSContext* aCx,
JS::MutableHandle<JS::Value> aValue)
{
MOZ_ASSERT(mBuffer, "Read() without Write() is not allowed.");
MOZ_ASSERT(!mShutdownCalled, "This method cannot be called after Shutdown.");
bool ok = mBuffer->read(aCx, aValue, &gCallbacks, this);
mBuffer = nullptr;
@ -178,7 +205,9 @@ StructuredCloneHelper::StructuredCloneHelper(uint32_t aFlags)
{}
StructuredCloneHelper::~StructuredCloneHelper()
{}
{
Shutdown();
}
bool
StructuredCloneHelper::Write(JSContext* aCx,

View File

@ -17,6 +17,9 @@ namespace dom {
class StructuredCloneHelperInternal
{
public:
StructuredCloneHelperInternal();
virtual ~StructuredCloneHelperInternal();
// These methods should be implemented in order to clone data.
// Read more documentation in js/public/StructuredClone.h.
@ -29,6 +32,12 @@ public:
JSStructuredCloneWriter* aWriter,
JS::Handle<JSObject*> aObj) = 0;
// This method has to be called when this object is not needed anymore.
// It will free memory and the buffer. This has to be called because
// otherwise the buffer will be freed in the DTOR of this class and at that
// point we cannot use the overridden methods.
void Shutdown();
// If these 3 methods are not implement, transfering objects will not be
// allowed.
@ -69,6 +78,10 @@ public:
protected:
nsAutoPtr<JSAutoStructuredCloneBuffer> mBuffer;
#ifdef DEBUG
bool mShutdownCalled;
#endif
};
class MessagePortBase;