Bug 1176034 - Implement JSAutoStructuredCloneBuffer::clear(with callbacks and closure), r=sfink

This commit is contained in:
Andrea Marchesini 2015-07-06 12:22:08 +01:00
parent bf7652f7f2
commit 0bb0e9c317
3 changed files with 43 additions and 13 deletions

View File

@ -3544,12 +3544,38 @@ WorkerPrivateParent<Derived>::DispatchMessageEventToMessagePort(
AssertIsOnMainThread();
JSAutoStructuredCloneBuffer buffer(Move(aBuffer));
const JSStructuredCloneCallbacks* callbacks =
WorkerStructuredCloneCallbacks(true);
class MOZ_STACK_CLASS AutoCloneBufferCleaner final
{
public:
AutoCloneBufferCleaner(JSAutoStructuredCloneBuffer& aBuffer,
const JSStructuredCloneCallbacks* aCallbacks,
WorkerStructuredCloneClosure& aClosure)
: mBuffer(aBuffer)
, mCallbacks(aCallbacks)
, mClosure(aClosure)
{}
~AutoCloneBufferCleaner()
{
mBuffer.clear(mCallbacks, &mClosure);
}
private:
JSAutoStructuredCloneBuffer& mBuffer;
const JSStructuredCloneCallbacks* mCallbacks;
WorkerStructuredCloneClosure& mClosure;
};
WorkerStructuredCloneClosure closure;
closure.mClonedObjects.SwapElements(aClosure.mClonedObjects);
MOZ_ASSERT(aClosure.mMessagePorts.IsEmpty());
closure.mMessagePortIdentifiers.SwapElements(aClosure.mMessagePortIdentifiers);
AutoCloneBufferCleaner bufferCleaner(buffer, callbacks, closure);
SharedWorker* sharedWorker;
if (!mSharedWorkers.Get(aMessagePortSerial, &sharedWorker)) {
// SharedWorker has already been unregistered?
@ -3577,8 +3603,6 @@ WorkerPrivateParent<Derived>::DispatchMessageEventToMessagePort(
return false;
}
buffer.clear();
nsRefPtr<MessageEvent> event = new MessageEvent(port, nullptr, nullptr);
nsresult rv =
event->InitMessageEvent(NS_LITERAL_STRING("message"), false, false, data,

View File

@ -192,7 +192,7 @@ class JS_PUBLIC_API(JSAutoStructuredCloneBuffer) {
uint64_t* data() const { return data_; }
size_t nbytes() const { return nbytes_; }
void clear();
void clear(const JSStructuredCloneCallbacks* optionalCallbacks=nullptr, void* closure=nullptr);
// Copy some memory. It will be automatically freed by the destructor.
bool copy(const uint64_t* data, size_t nbytes, uint32_t version=JS_STRUCTURED_CLONE_VERSION);

View File

@ -1984,17 +1984,23 @@ JSAutoStructuredCloneBuffer::operator=(JSAutoStructuredCloneBuffer&& other)
}
void
JSAutoStructuredCloneBuffer::clear()
JSAutoStructuredCloneBuffer::clear(const JSStructuredCloneCallbacks* optionalCallbacks,
void* optionalClosure)
{
if (data_) {
if (ownTransferables_ == OwnsTransferablesIfAny)
DiscardTransferables(data_, nbytes_, callbacks_, closure_);
ownTransferables_ = NoTransferables;
js_free(data_);
data_ = nullptr;
nbytes_ = 0;
version_ = 0;
}
if (!data_)
return;
const JSStructuredCloneCallbacks* callbacks =
optionalCallbacks ? optionalCallbacks : callbacks_;
void* closure = optionalClosure ? optionalClosure : closure_;
if (ownTransferables_ == OwnsTransferablesIfAny)
DiscardTransferables(data_, nbytes_, callbacks, closure);
ownTransferables_ = NoTransferables;
js_free(data_);
data_ = nullptr;
nbytes_ = 0;
version_ = 0;
}
bool