File: ExtensionAPIRequestForwarder.h

package info (click to toggle)
thunderbird 1%3A91.12.0-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,008,300 kB
  • sloc: cpp: 6,084,052; javascript: 4,790,441; ansic: 3,341,486; python: 862,958; asm: 366,542; xml: 204,277; java: 152,477; sh: 111,376; makefile: 21,388; perl: 15,312; yacc: 4,583; objc: 3,026; lex: 1,720; exp: 762; pascal: 635; awk: 564; sql: 453; php: 436; lisp: 432; ruby: 99; sed: 69; csh: 45
file content (194 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download | duplicates (4)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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_extensions_ExtensionAPIRequestForwarder_h
#define mozilla_extensions_ExtensionAPIRequestForwarder_h

#include "ExtensionAPIRequest.h"

#include "mozilla/dom/PromiseWorkerProxy.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/dom/StructuredCloneHolder.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/ToJSValue.h"

namespace mozilla {
namespace dom {
class ClientInfoAndState;
class Function;
class SerializedStackHolder;
}  // namespace dom
namespace extensions {

class ExtensionAPIRequestForwarder;

// A class used to forward an API request (a method call, add/remote listener or
// a property getter) originated from a WebExtensions global (a window, a
// content script sandbox or a service worker) to the JS privileged API request
// handler available on the main thread (mozIExtensionAPIRequestHandler).
//
// Instances of this class are meant to be short-living, and destroyed when the
// caller function is exiting.
class ExtensionAPIRequestForwarder {
  friend class ExtensionAPIRequest;

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ExtensionAPIRequestForwarder)

 public:
  using APIRequestType = mozIExtensionAPIRequest::RequestType;
  using APIResultType = mozIExtensionAPIRequestResult::ResultType;

  static nsresult JSArrayToSequence(JSContext* aCx, JS::HandleValue aJSValue,
                                    dom::Sequence<JS::Value>& aResult);

  static void ThrowUnexpectedError(JSContext* aCx, ErrorResult& aRv);

  static mozIExtensionAPIRequestHandler& APIRequestHandler();

  ExtensionAPIRequestForwarder(const APIRequestType aRequestType,
                               const nsAString& aApiNamespace,
                               const nsAString& aApiMethod,
                               const nsAString& aApiObjectType = u""_ns,
                               const nsAString& aApiObjectId = u""_ns);

  mozIExtensionAPIRequest::RequestType GetRequestType() const {
    return mRequestType;
  }

  const ExtensionAPIRequestTarget* GetRequestTarget() {
    return &mRequestTarget;
  }

  void Run(nsIGlobalObject* aGlobal, JSContext* aCx,
           const dom::Sequence<JS::Value>& aArgs, ErrorResult& aRv);

  void Run(nsIGlobalObject* aGlobal, JSContext* aCx,
           const dom::Sequence<JS::Value>& aArgs,
           ExtensionEventListener* aListener, ErrorResult& aRv);

  void Run(nsIGlobalObject* aGlobal, JSContext* aCx,
           const dom::Sequence<JS::Value>& aArgs,
           JS::MutableHandleValue aRetVal, ErrorResult& aRv);

  void Run(nsIGlobalObject* aGlobal, JSContext* aCx,
           const dom::Sequence<JS::Value>& aArgs,
           ExtensionEventListener* aListener, JS::MutableHandleValue aRetVal,
           ErrorResult& aRv);

  void Run(nsIGlobalObject* aGlobal, JSContext* aCx,
           const dom::Sequence<JS::Value>& aArgs,
           const RefPtr<dom::Promise>& aPromiseRetval, ErrorResult& aRv);

  void Run(nsIGlobalObject* aGlobal, JSContext* aCx,
           JS::MutableHandleValue aRetVal, ErrorResult& aRv);

 protected:
  virtual ~ExtensionAPIRequestForwarder() = default;

 private:
  already_AddRefed<ExtensionAPIRequest> CreateAPIRequest(
      nsIGlobalObject* aGlobal, JSContext* aCx,
      const dom::Sequence<JS::Value>& aArgs, ExtensionEventListener* aListener,
      ErrorResult& aRv);

  APIRequestType mRequestType;
  ExtensionAPIRequestTarget mRequestTarget;
};

/*
 * This runnable is used internally by ExtensionAPIRequestForwader class
 * to call the JS privileged code that handle the API requests originated
 * from the WebIDL bindings instantiated in a worker thread.
 *
 * The runnable is meant to block the worker thread until we get a result
 * from the JS privileged code that handles the API request.
 *
 * For async API calls we still need to block the worker thread until
 * we get a promise (which we link to the worker thread promise and
 * at that point we unblock the worker thread), because the JS privileged
 * code handling the API request may need to throw some errors synchonously
 * (e.g. in case of additional validations based on the API schema definition
 * for the parameter, like strings that has to pass additional validation
 * or normalizations).
 */
class RequestWorkerRunnable : public dom::WorkerMainThreadRunnable {
 public:
  using APIRequestType = mozIExtensionAPIRequest::RequestType;
  using APIResultType = mozIExtensionAPIRequestResult::ResultType;

  RequestWorkerRunnable(dom::WorkerPrivate* aWorkerPrivate,
                        ExtensionAPIRequestForwarder* aOuterAPIRequest);

  /**
   * Init a request runnable for AddListener and RemoveListener API requests
   * (which do have an event callback callback and do not expect any return
   * value).
   */
  void Init(nsIGlobalObject* aGlobal, JSContext* aCx,
            const dom::Sequence<JS::Value>& aArgs,
            ExtensionEventListener* aListener, ErrorResult& aRv);

  /**
   * Init a request runnable for CallFunctionNoReturn API requests (which do
   * do not expect any return value).
   */
  void Init(nsIGlobalObject* aGlobal, JSContext* aCx,
            const dom::Sequence<JS::Value>& aArgs, ErrorResult& aRv) {
    Init(aGlobal, aCx, aArgs, nullptr, aRv);
  }

  /**
   * Init a request runnable for CallAsyncFunction API requests (which do
   * expect a promise as return value).
   */
  void Init(nsIGlobalObject* aGlobal, JSContext* aCx,
            const dom::Sequence<JS::Value>& aArgs,
            const RefPtr<dom::Promise>& aPromiseRetval, ErrorResult& aRv);

  bool MainThreadRun() override;

  void ReadResult(JSContext* aCx, JS::MutableHandleValue aResult,
                  ErrorResult& aRv);

  Maybe<mozIExtensionAPIRequestResult::ResultType> GetResultType() {
    return mResultType;
  }

 protected:
  virtual bool ProcessHandlerResult(JSContext* aCx,
                                    JS::MutableHandleValue aRetval);

  already_AddRefed<WebExtensionPolicy> GetWebExtensionPolicy();
  already_AddRefed<ExtensionAPIRequest> CreateAPIRequest(JSContext* aCx);

  void SerializeCallerStack(JSContext* aCx);
  void DeserializeCallerStack(JSContext* aCx, JS::MutableHandleValue aRetval);
  void SerializeArgs(JSContext* aCx, const dom::Sequence<JS::Value>& aArgs,
                     ErrorResult& aRv);
  nsresult DeserializeArgs(JSContext* aCx, JS::MutableHandle<JS::Value> aArgs);

  bool HandleAPIRequest(JSContext* aCx, JS::MutableHandleValue aRetval);

  Maybe<mozIExtensionAPIRequestResult::ResultType> mResultType;
  Maybe<UniquePtr<dom::StructuredCloneHolder>> mResultHolder;
  RefPtr<dom::PromiseWorkerProxy> mPromiseProxy;
  Maybe<UniquePtr<dom::StructuredCloneHolder>> mArgsHolder;
  Maybe<UniquePtr<dom::SerializedStackHolder>> mStackHolder;
  Maybe<dom::ClientInfo> mClientInfo;

  // Only set for addListener/removeListener API requests.
  RefPtr<ExtensionEventListener> mEventListener;

  // The outer request object is kept alive by the caller for the
  // entire life of the inner worker runnable.
  ExtensionAPIRequestForwarder* mOuterRequest;
};

}  // namespace extensions
}  // namespace mozilla

#endif  // mozilla_extensions_ExtensionAPIRequestForwarder_h