summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/webidl-api/ExtensionAPIBase.h
blob: cef4c77887df89273de2a5b7498969df848f6264 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* -*- 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_ExtensionAPIBase_h
#define mozilla_extensions_ExtensionAPIBase_h

#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/FunctionBinding.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/ErrorResult.h"

class nsIGlobalObject;

namespace mozilla {

namespace dom {
class Function;
}

namespace extensions {

#define NS_IMPL_WEBEXT_EVENTMGR_WITH_DATAMEMBER(            \
    _class, _eventName, _eventGetterName, _eventDataMember) \
  ExtensionEventManager* _class::_eventGetterName() {       \
    if (!(_eventDataMember)) {                              \
      (_eventDataMember) = CreateEventManager(_eventName);  \
    }                                                       \
    return (_eventDataMember);                              \
  }
#define NS_IMPL_WEBEXT_EVENTMGR(_class, _eventName, _eventGetterName) \
  NS_IMPL_WEBEXT_EVENTMGR_WITH_DATAMEMBER(                            \
      _class, _eventName, _eventGetterName, m##_eventGetterName##EventMgr)

#define NS_IMPL_WEBEXT_SETTING_WITH_DATAMEMBER(                   \
    _class, _settingName, _settingGetterName, _settingDataMember) \
  ExtensionSetting* _class::_settingGetterName() {                \
    if (!(_settingDataMember)) {                                  \
      (_settingDataMember) = CreateSetting(_settingName);         \
    }                                                             \
    return (_settingDataMember);                                  \
  }
#define NS_IMPL_WEBEXT_SETTING(_class, _settingName, _settingGetterName) \
  NS_IMPL_WEBEXT_SETTING_WITH_DATAMEMBER(_class, _settingName,           \
                                         _settingGetterName,             \
                                         m##_settingGetterName##Setting)

class ExtensionAPIAddRemoveListener;
class ExtensionAPICallFunctionNoReturn;
class ExtensionAPICallSyncFunction;
class ExtensionAPICallAsyncFunction;
class ExtensionAPIGetProperty;
class ExtensionBrowser;
class ExtensionEventManager;
class ExtensionPort;
class ExtensionSetting;

class ExtensionAPIBase {
 public:
  // WebExtensionStub methods shared between multiple API namespaces.

  virtual void CallWebExtMethodNotImplementedNoReturn(
      JSContext* aCx, const nsAString& aApiMethod,
      const dom::Sequence<JS::Value>& aArgs, ErrorResult& aRv);

  virtual void CallWebExtMethodNotImplementedAsync(
      JSContext* aCx, const nsAString& aApiMethod,
      const dom::Sequence<JS::Value>& aArgs,
      const dom::Optional<OwningNonNull<dom::Function>>& aCallback,
      JS::MutableHandle<JS::Value> aRetval, ErrorResult& aRv);

  virtual void CallWebExtMethodNotImplemented(
      JSContext* aCx, const nsAString& aApiMethod,
      const dom::Sequence<JS::Value>& aArgs,
      JS::MutableHandle<JS::Value> aRetval, ErrorResult& aRv);

  virtual void CallWebExtMethodNoReturn(JSContext* aCx,
                                        const nsAString& aApiMethod,
                                        const dom::Sequence<JS::Value>& aArgs,
                                        ErrorResult& aRv);
  virtual void CallWebExtMethod(JSContext* aCx, const nsAString& aApiMethod,
                                const dom::Sequence<JS::Value>& aArgs,
                                JS::MutableHandle<JS::Value> aRetVal,
                                ErrorResult& aRv);

  virtual void CallWebExtMethodReturnsString(
      JSContext* aCx, const nsAString& aApiMethod,
      const dom::Sequence<JS::Value>& aArgs, nsAString& aRetVal,
      ErrorResult& aRv);

  virtual already_AddRefed<ExtensionPort> CallWebExtMethodReturnsPort(
      JSContext* aCx, const nsAString& aApiMethod,
      const dom::Sequence<JS::Value>& aArgs, ErrorResult& aRv);

  virtual void CallWebExtMethodAsync(
      JSContext* aCx, const nsAString& aApiMethod,
      const dom::Sequence<JS::Value>& aArgs,
      const dom::Optional<OwningNonNull<dom::Function>>& aCallback,
      JS::MutableHandle<JS::Value> aRetVal, ErrorResult& aRv);

  virtual void CallWebExtMethodAsyncAmbiguous(
      JSContext* aCx, const nsAString& aApiMethod,
      const dom::Sequence<JS::Value>& aArgs,
      JS::MutableHandle<JS::Value> aRetVal, ErrorResult& aRv);

  virtual void GetWebExtPropertyAsString(const nsString& aPropertyName,
                                         dom::DOMString& aRetval);

  virtual void GetWebExtPropertyAsJSValue(JSContext* aCx,
                                          const nsAString& aPropertyName,
                                          JS::MutableHandle<JS::Value> aRetval);

  // API Requests helpers.
  already_AddRefed<ExtensionEventManager> CreateEventManager(
      const nsAString& aEventName);

  already_AddRefed<ExtensionSetting> CreateSetting(
      const nsAString& aSettingName);

  RefPtr<ExtensionAPICallFunctionNoReturn> CallFunctionNoReturn(
      const nsAString& aApiMethod);

  RefPtr<ExtensionAPICallSyncFunction> CallSyncFunction(
      const nsAString& aApiMethod);

  RefPtr<ExtensionAPICallAsyncFunction> CallAsyncFunction(
      const nsAString& aApiMethod);

  RefPtr<ExtensionAPIGetProperty> GetProperty(const nsAString& aApiProperty);

  RefPtr<ExtensionAPIAddRemoveListener> SendAddListener(
      const nsAString& aEventName);

  RefPtr<ExtensionAPIAddRemoveListener> SendRemoveListener(
      const nsAString& aEventName);

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

 protected:
  virtual nsIGlobalObject* GetGlobalObject() const = 0;
  virtual ExtensionBrowser* GetExtensionBrowser() const = 0;
  virtual nsString GetAPINamespace() const = 0;
  virtual nsString GetAPIObjectType() const = 0;
  virtual nsString GetAPIObjectId() const = 0;

 private:
  void CallWebExtMethodAsyncInternal(JSContext* aCx,
                                     const nsAString& aApiMethod,
                                     const dom::Sequence<JS::Value>& aArgs,
                                     const RefPtr<dom::Function>& aCallback,
                                     JS::MutableHandle<JS::Value> aRetval,
                                     ErrorResult& aRv);
};

class ExtensionAPINamespace : public ExtensionAPIBase {
 protected:
  nsString GetAPIObjectType() const override { return VoidString(); }

  nsString GetAPIObjectId() const override { return VoidString(); };
};

class ChromeCompatCallbackHandler final : public dom::PromiseNativeHandler {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS

  static void Create(ExtensionBrowser* aExtensionBrowser,
                     dom::Promise* aPromise,
                     const RefPtr<dom::Function>& aCallback);

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

 private:
  ChromeCompatCallbackHandler(ExtensionBrowser* aExtensionBrowser,
                              const RefPtr<dom::Function>& aCallback)
      : mCallback(aCallback), mExtensionBrowser(aExtensionBrowser) {
    MOZ_ASSERT(aCallback);
    MOZ_ASSERT(aExtensionBrowser);
  }

  ~ChromeCompatCallbackHandler() = default;

  void ReportUncheckedLastError(JSContext* aCx, JS::Handle<JS::Value> aValue);

  RefPtr<dom::Function> mCallback;
  RefPtr<ExtensionBrowser> mExtensionBrowser;
};

}  // namespace extensions
}  // namespace mozilla

#endif  // mozilla_extensions_ExtensionAPIBase_h