mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-12-02 01:48:05 +00:00
Bug 1176034 - Implement JSAutoStructuredCloneBuffer::clear(with callbacks and closure), r=sfink
This commit is contained in:
parent
bf7652f7f2
commit
0bb0e9c317
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user