bool
WorkerMessagePort::MaybeDispatchEvent(
                                JSContext* aCx,
                                JSAutoStructuredCloneBuffer& aBuffer,
                                nsTArray<nsCOMPtr<nsISupports>>& aClonedObjects)
{
  if (mClosed) {
    NS_WARNING("Not going to ever run this event!");
    aBuffer.clear();
    aClonedObjects.Clear();
    return true;
  }

  if (!mStarted) {
    // Queue the message for later.
    MessageInfo* info = mQueuedMessages.AppendElement();
    info->mBuffer.swap(aBuffer);
    info->mClonedObjects.SwapElements(aClonedObjects);
    return true;
  }

  // Go ahead and dispatch the event.
  JS::Rooted<JSObject*> target(aCx, GetJSObject());
  return DispatchMessageEvent(aCx, target, aBuffer, aClonedObjects);
}
// static
nsresult
AsyncConnectionHelper::ConvertCloneBufferToJSVal(
                                           JSContext* aCx,
                                           JSAutoStructuredCloneBuffer& aBuffer,
                                           jsval* aResult)
{
  NS_ASSERTION(aCx, "Null context!");
  NS_ASSERTION(aResult, "Null pointer!");

  JSAutoRequest ar(aCx);

  if (aBuffer.data()) {
    JSBool ok = aBuffer.read(aResult, aCx);

    aBuffer.clear(aCx);

    if (!ok) {
      NS_ERROR("Failed to decode!");
      return NS_ERROR_DOM_DATA_CLONE_ERR;
    }
  }
  else {
    *aResult = JSVAL_VOID;
  }

  return NS_OK;
}