gecko-dev/dom/ipc/JSWindowActor.h
Nika Layzell 6114a5f7ab Bug 1544936 - Part 1: Add a sendQuery method to send an async message with a response to JSWindowActor, r=mconley,jdai
This adds a single new method, which acts like sendAsyncMessage, but
also returns a promise. This promise is fulfilled when the promise
returned from the receiveMessage callback is resolved.

```
partial interface JSWindowActor {
  [Throws]
  Promise<any> sendQuery(DOMString messageName,
                         optional any obj,
                         optional any transfers);
}
```

Differential Revision: https://phabricator.services.mozilla.com/D27809

--HG--
extra : moz-landing-system : lando
2019-04-18 19:37:15 +00:00

126 lines
4.0 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_JSWindowActor_h
#define mozilla_dom_JSWindowActor_h
#include "js/TypeDecls.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "nsRefPtrHashtable.h"
namespace mozilla {
namespace dom {
enum class JSWindowActorMessageKind {
Message,
Query,
QueryResolve,
QueryReject,
EndGuard_,
};
class JSWindowActorMessageMeta;
class QueryPromiseHandler;
// Common base class for JSWindowActor{Parent,Child}.
class JSWindowActor : public nsISupports, public nsWrapperCache {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(JSWindowActor)
JSWindowActor();
const nsString& Name() const { return mName; }
void SendAsyncMessage(JSContext* aCx, const nsAString& aMessageName,
JS::Handle<JS::Value> aObj,
JS::Handle<JS::Value> aTransfers, ErrorResult& aRv);
already_AddRefed<Promise> SendQuery(JSContext* aCx,
const nsAString& aMessageName,
JS::Handle<JS::Value> aObj,
JS::Handle<JS::Value> aTransfers,
ErrorResult& aRv);
void ReceiveRawMessage(const JSWindowActorMessageMeta& aMetadata,
ipc::StructuredCloneData&& aData);
nsIGlobalObject* GetParentObject() const;
void RejectPendingQueries();
protected:
// Send the message described by the structured clone data |aData|, and the
// message metadata |aMetadata|. The underlying transport should call the
// |ReceiveMessage| method on the other side asynchronously.
virtual void SendRawMessage(const JSWindowActorMessageMeta& aMetadata,
ipc::StructuredCloneData&& aData,
ErrorResult& aRv) = 0;
virtual ~JSWindowActor() = default;
void SetName(const nsAString& aName);
private:
void ReceiveMessageOrQuery(JSContext* aCx,
const JSWindowActorMessageMeta& aMetadata,
JS::Handle<JS::Value> aData, ErrorResult& aRv);
void ReceiveQueryReply(JSContext* aCx,
const JSWindowActorMessageMeta& aMetadata,
JS::Handle<JS::Value> aData, ErrorResult& aRv);
// Helper object used while processing query messages to send the final reply
// message.
class QueryHandler final : public PromiseNativeHandler {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(QueryHandler)
QueryHandler(JSWindowActor* aActor,
const JSWindowActorMessageMeta& aMetadata);
void RejectedCallback(JSContext* aCx,
JS::Handle<JS::Value> aValue) override;
void ResolvedCallback(JSContext* aCx,
JS::Handle<JS::Value> aValue) override;
private:
~QueryHandler() = default;
void SendReply(JSContext* aCx, JSWindowActorMessageKind aKind,
ipc::StructuredCloneData&& aData);
RefPtr<JSWindowActor> mActor;
nsString mMessageName;
uint64_t mQueryId;
};
nsString mName;
nsRefPtrHashtable<nsUint64HashKey, Promise> mPendingQueries;
uint64_t mNextQueryId;
};
} // namespace dom
} // namespace mozilla
namespace IPC {
template <>
struct ParamTraits<mozilla::dom::JSWindowActorMessageKind>
: public ContiguousEnumSerializer<
mozilla::dom::JSWindowActorMessageKind,
mozilla::dom::JSWindowActorMessageKind::Message,
mozilla::dom::JSWindowActorMessageKind::EndGuard_> {};
} // namespace IPC
#endif // !defined(mozilla_dom_JSWindowActor_h)