summaryrefslogtreecommitdiffstats
path: root/dom/ipc/jsactor/JSActor.h
blob: 54af93d1cee4432c469ff7a087438aa07b3b0446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* -*- 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_JSActor_h
#define mozilla_dom_JSActor_h

#include "js/TypeDecls.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "nsCycleCollectionParticipant.h"
#include "nsTHashMap.h"
#include "nsWrapperCache.h"

class nsIGlobalObject;
class nsQueryJSActor;

namespace mozilla {
class ErrorResult;

namespace dom {

namespace ipc {
class StructuredCloneData;
}

class JSActorManager;
class JSActorMessageMeta;
class QueryPromiseHandler;

enum class JSActorMessageKind {
  Message,
  Query,
  QueryResolve,
  QueryReject,
  EndGuard_,
};

// Common base class for JSWindowActor{Parent,Child}.
class JSActor : public nsISupports, public nsWrapperCache {
 public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(JSActor)

  explicit JSActor(nsISupports* aGlobal = nullptr);

  const nsCString& Name() const { return mName; }
  void GetName(nsCString& aName) { aName = Name(); }

  void SendAsyncMessage(JSContext* aCx, const nsAString& aMessageName,
                        JS::Handle<JS::Value> aObj, ErrorResult& aRv);

  already_AddRefed<Promise> SendQuery(JSContext* aCx,
                                      const nsAString& aMessageName,
                                      JS::Handle<JS::Value> aObj,
                                      ErrorResult& aRv);

  nsIGlobalObject* GetParentObject() const { return mGlobal; };

 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 JSActorMessageMeta& aMetadata,
                              Maybe<ipc::StructuredCloneData>&& aData,
                              Maybe<ipc::StructuredCloneData>&& aStack,
                              ErrorResult& aRv) = 0;

  // Helper method to send an in-process raw message.
  using OtherSideCallback = std::function<already_AddRefed<JSActorManager>()>;
  static void SendRawMessageInProcess(const JSActorMessageMeta& aMeta,
                                      Maybe<ipc::StructuredCloneData>&& aData,
                                      Maybe<ipc::StructuredCloneData>&& aStack,
                                      OtherSideCallback&& aGetOtherSide);

  virtual ~JSActor() = default;

  void SetName(const nsACString& aName);

  bool CanSend() const { return mCanSend; }

  void ThrowStateErrorForGetter(const char* aName, ErrorResult& aRv) const;

  void StartDestroy();
  void AfterDestroy();

  enum class CallbackFunction { DidDestroy, ActorCreated };
  void InvokeCallback(CallbackFunction callback);

  virtual void ClearManager() = 0;

 private:
  friend class JSActorManager;
  friend class ::nsQueryJSActor;  // for QueryInterfaceActor

  nsresult QueryInterfaceActor(const nsIID& aIID, void** aPtr);

  // Called by JSActorManager when they receive raw message data destined for
  // this actor.
  void ReceiveMessage(JSContext* aCx, const JSActorMessageMeta& aMetadata,
                      JS::Handle<JS::Value> aData, ErrorResult& aRv);
  void ReceiveQuery(JSContext* aCx, const JSActorMessageMeta& aMetadata,
                    JS::Handle<JS::Value> aData, ErrorResult& aRv);
  void ReceiveQueryReply(JSContext* aCx, const JSActorMessageMeta& aMetadata,
                         JS::Handle<JS::Value> aData, ErrorResult& aRv);

  // Call the actual `ReceiveMessage` method, and get the return value.
  void CallReceiveMessage(JSContext* aCx, const JSActorMessageMeta& aMetadata,
                          JS::Handle<JS::Value> aData,
                          JS::MutableHandle<JS::Value> aRetVal,
                          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(JSActor* aActor, const JSActorMessageMeta& aMetadata,
                 Promise* aPromise);

    void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
                          ErrorResult& aRv) override;

    void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
                          ErrorResult& aRv) override;

   private:
    ~QueryHandler() = default;

    void SendReply(JSContext* aCx, JSActorMessageKind aKind,
                   Maybe<ipc::StructuredCloneData>&& aData);

    RefPtr<JSActor> mActor;
    RefPtr<Promise> mPromise;
    nsString mMessageName;
    uint64_t mQueryId;
  };

  // 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;
  };

  nsCOMPtr<nsIGlobalObject> mGlobal;
  nsCOMPtr<nsISupports> mWrappedJS;
  nsCString mName;
  nsTHashMap<nsUint64HashKey, PendingQuery> mPendingQueries;
  uint64_t mNextQueryId = 0;
  bool mCanSend = true;
};

}  // namespace dom
}  // namespace mozilla

namespace IPC {

template <>
struct ParamTraits<mozilla::dom::JSActorMessageKind>
    : public ContiguousEnumSerializer<
          mozilla::dom::JSActorMessageKind,
          mozilla::dom::JSActorMessageKind::Message,
          mozilla::dom::JSActorMessageKind::EndGuard_> {};

}  // namespace IPC

#endif  // !defined(mozilla_dom_JSActor_h)