2019-04-18 22:37:15 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2020-04-30 19:42:53 +03:00
|
|
|
#ifndef mozilla_dom_JSActor_h
|
|
|
|
#define mozilla_dom_JSActor_h
|
2019-04-18 22:37:15 +03:00
|
|
|
|
|
|
|
#include "js/TypeDecls.h"
|
|
|
|
#include "ipc/IPCMessageUtils.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2019-12-09 15:52:53 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2020-07-08 01:14:04 +03:00
|
|
|
#include "nsDataHashtable.h"
|
2019-12-09 15:52:53 +03:00
|
|
|
#include "nsWrapperCache.h"
|
2019-04-18 22:37:15 +03:00
|
|
|
|
2019-05-03 21:12:55 +03:00
|
|
|
class nsIGlobalObject;
|
2020-03-12 19:47:57 +03:00
|
|
|
class nsQueryActorChild;
|
|
|
|
class nsQueryActorParent;
|
2019-05-03 21:12:55 +03:00
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
class JSActorManager;
|
|
|
|
class JSActorMessageMeta;
|
|
|
|
class QueryPromiseHandler;
|
|
|
|
|
2020-04-30 19:42:53 +03:00
|
|
|
enum class JSActorMessageKind {
|
2019-04-18 22:37:15 +03:00
|
|
|
Message,
|
|
|
|
Query,
|
|
|
|
QueryResolve,
|
|
|
|
QueryReject,
|
|
|
|
EndGuard_,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Common base class for JSWindowActor{Parent,Child}.
|
2020-04-30 19:42:53 +03:00
|
|
|
class JSActor : public nsISupports, public nsWrapperCache {
|
2019-04-18 22:37:15 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2020-04-30 19:42:53 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(JSActor)
|
2019-04-18 22:37:15 +03:00
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
explicit JSActor(nsISupports* aGlobal = nullptr);
|
2019-05-03 21:12:55 +03:00
|
|
|
|
2020-04-17 13:56:22 +03:00
|
|
|
const nsCString& Name() const { return mName; }
|
2019-04-18 22:37:15 +03:00
|
|
|
|
|
|
|
void SendAsyncMessage(JSContext* aCx, const nsAString& aMessageName,
|
2019-09-13 19:51:23 +03:00
|
|
|
JS::Handle<JS::Value> aObj, ErrorResult& aRv);
|
2019-04-18 22:37:15 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise> SendQuery(JSContext* aCx,
|
|
|
|
const nsAString& aMessageName,
|
|
|
|
JS::Handle<JS::Value> aObj,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
nsIGlobalObject* GetParentObject() const { return mGlobal; };
|
2019-04-18 22:37:15 +03:00
|
|
|
|
|
|
|
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.
|
2020-04-30 19:42:53 +03:00
|
|
|
virtual void SendRawMessage(const JSActorMessageMeta& aMetadata,
|
2019-04-18 22:37:15 +03:00
|
|
|
ipc::StructuredCloneData&& aData,
|
2019-12-07 21:59:23 +03:00
|
|
|
ipc::StructuredCloneData&& aStack,
|
2019-04-18 22:37:15 +03:00
|
|
|
ErrorResult& aRv) = 0;
|
|
|
|
|
2019-12-20 00:17:11 +03:00
|
|
|
// Check if a message is so large that IPC will probably crash if we try to
|
|
|
|
// send it. If it is too large, record telemetry about the message.
|
2020-04-30 19:42:53 +03:00
|
|
|
static bool AllowMessage(const JSActorMessageMeta& aMetadata,
|
2019-12-20 00:17:11 +03:00
|
|
|
size_t aDataLength);
|
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
// Helper method to send an in-process raw message.
|
|
|
|
using OtherSideCallback = std::function<already_AddRefed<JSActorManager>()>;
|
|
|
|
static void SendRawMessageInProcess(const JSActorMessageMeta& aMeta,
|
|
|
|
ipc::StructuredCloneData&& aData,
|
|
|
|
ipc::StructuredCloneData&& aStack,
|
|
|
|
OtherSideCallback&& aGetOtherSide);
|
|
|
|
|
2020-04-30 19:42:53 +03:00
|
|
|
virtual ~JSActor() = default;
|
2019-04-18 22:37:15 +03:00
|
|
|
|
2020-04-17 13:56:22 +03:00
|
|
|
void SetName(const nsACString& aName);
|
2019-04-18 22:37:15 +03:00
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
bool CanSend() const { return mCanSend; }
|
2020-07-08 04:09:27 +03:00
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
void StartDestroy();
|
2019-05-10 18:01:40 +03:00
|
|
|
void AfterDestroy();
|
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
enum class CallbackFunction { WillDestroy, DidDestroy, ActorCreated };
|
2019-08-15 02:13:13 +03:00
|
|
|
void InvokeCallback(CallbackFunction willDestroy);
|
2019-05-10 18:01:40 +03:00
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
virtual void ClearManager() = 0;
|
|
|
|
|
2019-04-18 22:37:15 +03:00
|
|
|
private:
|
2020-07-08 17:22:22 +03:00
|
|
|
friend class JSActorManager;
|
2020-03-12 19:47:57 +03:00
|
|
|
friend class ::nsQueryActorChild; // for QueryInterfaceActor
|
|
|
|
friend class ::nsQueryActorParent; // for QueryInterfaceActor
|
2019-09-20 21:01:07 +03:00
|
|
|
|
|
|
|
nsresult QueryInterfaceActor(const nsIID& aIID, void** aPtr);
|
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
// Called by JSActorManager when they receive raw message data destined for
|
|
|
|
// this actor.
|
2019-04-18 22:37:15 +03:00
|
|
|
void ReceiveMessageOrQuery(JSContext* aCx,
|
2020-04-30 19:42:53 +03:00
|
|
|
const JSActorMessageMeta& aMetadata,
|
2019-04-18 22:37:15 +03:00
|
|
|
JS::Handle<JS::Value> aData, ErrorResult& aRv);
|
2020-04-30 19:42:53 +03:00
|
|
|
void ReceiveQueryReply(JSContext* aCx, const JSActorMessageMeta& aMetadata,
|
2019-04-18 22:37:15 +03:00
|
|
|
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)
|
|
|
|
|
2020-04-30 19:42:53 +03:00
|
|
|
QueryHandler(JSActor* aActor, const JSActorMessageMeta& aMetadata,
|
|
|
|
Promise* aPromise);
|
2019-04-18 22:37:15 +03:00
|
|
|
|
|
|
|
void RejectedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
void ResolvedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
~QueryHandler() = default;
|
|
|
|
|
2020-04-30 19:42:53 +03:00
|
|
|
void SendReply(JSContext* aCx, JSActorMessageKind aKind,
|
2019-04-18 22:37:15 +03:00
|
|
|
ipc::StructuredCloneData&& aData);
|
|
|
|
|
2020-04-30 19:42:53 +03:00
|
|
|
RefPtr<JSActor> mActor;
|
2019-12-07 21:59:23 +03:00
|
|
|
RefPtr<Promise> mPromise;
|
2019-04-18 22:37:15 +03:00
|
|
|
nsString mMessageName;
|
|
|
|
uint64_t mQueryId;
|
|
|
|
};
|
|
|
|
|
2020-07-08 01:14:04 +03:00
|
|
|
// A query which hasn't been resolved yet, along with metadata about what
|
|
|
|
// query the promise is for.
|
|
|
|
struct PendingQuery {
|
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
nsString mMessageName;
|
|
|
|
};
|
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
2019-09-20 21:01:07 +03:00
|
|
|
nsCOMPtr<nsISupports> mWrappedJS;
|
2020-04-17 13:56:22 +03:00
|
|
|
nsCString mName;
|
2020-07-08 01:14:04 +03:00
|
|
|
nsDataHashtable<nsUint64HashKey, PendingQuery> mPendingQueries;
|
2020-07-08 17:22:22 +03:00
|
|
|
uint64_t mNextQueryId = 0;
|
|
|
|
bool mCanSend = true;
|
2019-04-18 22:37:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
template <>
|
2020-04-30 19:42:53 +03:00
|
|
|
struct ParamTraits<mozilla::dom::JSActorMessageKind>
|
2019-04-18 22:37:15 +03:00
|
|
|
: public ContiguousEnumSerializer<
|
2020-04-30 19:42:53 +03:00
|
|
|
mozilla::dom::JSActorMessageKind,
|
|
|
|
mozilla::dom::JSActorMessageKind::Message,
|
|
|
|
mozilla::dom::JSActorMessageKind::EndGuard_> {};
|
2019-04-18 22:37:15 +03:00
|
|
|
|
|
|
|
} // namespace IPC
|
|
|
|
|
2020-04-30 19:42:53 +03:00
|
|
|
#endif // !defined(mozilla_dom_JSActor_h)
|