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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/* -*- 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/SerializedStackHolder.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;
} // 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::Handle<JS::Value> 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::MutableHandle<JS::Value> aRetVal, ErrorResult& aRv);
void Run(nsIGlobalObject* aGlobal, JSContext* aCx,
const dom::Sequence<JS::Value>& aArgs,
ExtensionEventListener* aListener,
JS::MutableHandle<JS::Value> 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::MutableHandle<JS::Value> aRetVal, ErrorResult& aRv);
void SetSerializedCallerStack(
UniquePtr<dom::SerializedStackHolder> aCallerStack);
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;
Maybe<UniquePtr<dom::SerializedStackHolder>> mStackHolder;
};
/*
* 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);
void SetSerializedCallerStack(
UniquePtr<dom::SerializedStackHolder> aCallerStack);
/**
* 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::MutableHandle<JS::Value> aResult,
ErrorResult& aRv);
Maybe<mozIExtensionAPIRequestResult::ResultType> GetResultType() {
return mResultType;
}
protected:
virtual bool ProcessHandlerResult(JSContext* aCx,
JS::MutableHandle<JS::Value> aRetval);
already_AddRefed<WebExtensionPolicy> GetWebExtensionPolicy();
already_AddRefed<ExtensionAPIRequest> CreateAPIRequest(JSContext* aCx);
void SerializeCallerStack(JSContext* aCx);
void DeserializeCallerStack(JSContext* aCx,
JS::MutableHandle<JS::Value> 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::MutableHandle<JS::Value> 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;
uint64_t mSWDescriptorId;
// 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;
};
class RequestInitWorkerRunnable : public dom::WorkerMainThreadRunnable {
Maybe<dom::ClientInfo> mClientInfo;
public:
RequestInitWorkerRunnable(dom::WorkerPrivate* aWorkerPrivate,
Maybe<dom::ClientInfo>& aSWClientInfo);
bool MainThreadRun() override;
};
class NotifyWorkerLoadedRunnable : public Runnable {
uint64_t mSWDescriptorId;
nsCOMPtr<nsIURI> mSWBaseURI;
public:
explicit NotifyWorkerLoadedRunnable(const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI)
: Runnable("extensions::NotifyWorkerLoadedRunnable"),
mSWDescriptorId(aServiceWorkerDescriptorId),
mSWBaseURI(aWorkerBaseURI) {
MOZ_ASSERT(mSWDescriptorId > 0);
MOZ_ASSERT(mSWBaseURI);
}
NS_IMETHOD Run() override;
NS_INLINE_DECL_REFCOUNTING_INHERITED(NotifyWorkerLoadedRunnable, Runnable)
private:
~NotifyWorkerLoadedRunnable() = default;
};
class NotifyWorkerDestroyedRunnable : public Runnable {
uint64_t mSWDescriptorId;
nsCOMPtr<nsIURI> mSWBaseURI;
public:
explicit NotifyWorkerDestroyedRunnable(
const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI)
: Runnable("extensions::NotifyWorkerDestroyedRunnable"),
mSWDescriptorId(aServiceWorkerDescriptorId),
mSWBaseURI(aWorkerBaseURI) {
MOZ_ASSERT(mSWDescriptorId > 0);
MOZ_ASSERT(mSWBaseURI);
}
NS_IMETHOD Run() override;
NS_INLINE_DECL_REFCOUNTING_INHERITED(NotifyWorkerDestroyedRunnable, Runnable)
private:
~NotifyWorkerDestroyedRunnable() = default;
};
} // namespace extensions
} // namespace mozilla
#endif // mozilla_extensions_ExtensionAPIRequestForwarder_h
|