From c86c10833d63bc8c0887a7ef5f67c531504d45d3 Mon Sep 17 00:00:00 2001 From: Luca Greco Date: Tue, 5 Oct 2021 17:05:39 +0000 Subject: [PATCH] Bug 1688040 - part4.1: Initial WebIDL-based bindings for browser.test API namespace r=baku Differential Revision: https://phabricator.services.mozilla.com/D102642 --- dom/bindings/Bindings.conf | 5 ++ dom/webidl/ExtensionTest.webidl | 68 +++++++++++++++++++ dom/webidl/moz.build | 1 + .../extensions/webidl-api/ExtensionTest.cpp | 52 ++++++++++++++ .../extensions/webidl-api/ExtensionTest.h | 63 +++++++++++++++++ .../webidl-api/ExtensionWebIDL.conf | 7 +- .../extensions/webidl-api/moz.build | 10 +++ 7 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 dom/webidl/ExtensionTest.webidl create mode 100644 toolkit/components/extensions/webidl-api/ExtensionTest.cpp create mode 100644 toolkit/components/extensions/webidl-api/ExtensionTest.h diff --git a/dom/bindings/Bindings.conf b/dom/bindings/Bindings.conf index 5cb501f8bd73..0b93288457a8 100644 --- a/dom/bindings/Bindings.conf +++ b/dom/bindings/Bindings.conf @@ -1612,6 +1612,11 @@ DOMInterfaces = { 'nativeType': 'mozilla::extensions::ExtensionPort', }, +'ExtensionTest': { + 'headerFile': 'mozilla/extensions/ExtensionTest.h', + 'nativeType': 'mozilla::extensions::ExtensionTest', +}, + #################################### # Test Interfaces of various sorts # #################################### diff --git a/dom/webidl/ExtensionTest.webidl b/dom/webidl/ExtensionTest.webidl new file mode 100644 index 000000000000..57a7e3aeac24 --- /dev/null +++ b/dom/webidl/ExtensionTest.webidl @@ -0,0 +1,68 @@ +/* + * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT + * + * The content of this file has been generated based on the WebExtensions API + * JSONSchema using the following command: + * + * export SCRIPT_DIR="toolkit/components/extensions/webidl-api" + * mach python $SCRIPT_DIR/GenerateWebIDLBindings.py -- test + * + * More info about generating webidl API bindings for WebExtensions API at: + * + * https://firefox-source-docs.mozilla.org/toolkit/components/extensions/webextensions/webidl_bindings.html + */ + +/* 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/. + * + * You are granted a license to use, reproduce and create derivative works of + * this document. + */ + +// WebIDL definition for the "test" WebExtensions API +[Exposed=(ServiceWorker), LegacyNoInterfaceObject] +interface ExtensionTest { + // API methods. + + [Throws, WebExtensionStub="NotImplementedNoReturn"] + void withHandlingUserInput(Function callback); + + [Throws, WebExtensionStub="NoReturn"] + void notifyFail(DOMString message); + + [Throws, WebExtensionStub="NoReturn"] + void notifyPass(optional DOMString message); + + [Throws, WebExtensionStub="NoReturn"] + void log(DOMString message); + + [Throws, WebExtensionStub="NoReturn"] + void sendMessage(any... args); + + [Throws, WebExtensionStub="NoReturn"] + void fail(optional any message); + + [Throws, WebExtensionStub="NoReturn"] + void succeed(optional any message); + + [Throws, WebExtensionStub="NoReturn"] + void assertTrue(any... args); + + [Throws, WebExtensionStub="NoReturn"] + void assertFalse(any... args); + + [Throws, WebExtensionStub="NotImplementedNoReturn"] + void assertEq(any... args); + + [Throws, WebExtensionStub="NotImplementedAsync"] + any assertRejects(Promise promise, any expectedError, optional DOMString message, optional Function callback); + + [Throws, WebExtensionStub="NotImplementedNoReturn"] + void assertThrows(Function func, any expectedError, optional DOMString message); + + // API events. + + [Replaceable, SameObject] + readonly attribute ExtensionEventManager onMessage; +}; diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index 0d62dc051237..3931f6aeaab8 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -1063,6 +1063,7 @@ WEBIDL_FILES += [ # ExtensionMockAPI is not a real WebExtensions API, and it is only enabled in tests. "ExtensionMockAPI.webidl", "ExtensionPort.webidl", + "ExtensionTest.webidl", ] # We only expose our prefable test interfaces in debug builds, just to be on diff --git a/toolkit/components/extensions/webidl-api/ExtensionTest.cpp b/toolkit/components/extensions/webidl-api/ExtensionTest.cpp new file mode 100644 index 000000000000..0ca6acd0948b --- /dev/null +++ b/toolkit/components/extensions/webidl-api/ExtensionTest.cpp @@ -0,0 +1,52 @@ +/* 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/. */ + +#include "ExtensionTest.h" +#include "ExtensionEventManager.h" + +#include "mozilla/dom/ExtensionTestBinding.h" +#include "nsIGlobalObject.h" + +namespace mozilla { +namespace extensions { + +NS_IMPL_CYCLE_COLLECTING_ADDREF(ExtensionTest); +NS_IMPL_CYCLE_COLLECTING_RELEASE(ExtensionTest) +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ExtensionTest, mGlobal, + mOnMessageEventMgr); + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ExtensionTest) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +ExtensionTest::ExtensionTest(nsIGlobalObject* aGlobal, + ExtensionBrowser* aExtensionBrowser) + : mGlobal(aGlobal) { + MOZ_DIAGNOSTIC_ASSERT(mGlobal); +} + +/* static */ +bool ExtensionTest::IsAllowed(JSContext* aCx, JSObject* aGlobal) { + return true; +} + +JSObject* ExtensionTest::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) { + return dom::ExtensionTest_Binding::Wrap(aCx, this, aGivenProto); +} + +nsIGlobalObject* ExtensionTest::GetParentObject() const { return mGlobal; } + +ExtensionEventManager* ExtensionTest::OnMessage() { + if (!mOnMessageEventMgr) { + mOnMessageEventMgr = CreateEventManager(u"onMessage"_ns); + } + + return mOnMessageEventMgr; +} + +} // namespace extensions +} // namespace mozilla diff --git a/toolkit/components/extensions/webidl-api/ExtensionTest.h b/toolkit/components/extensions/webidl-api/ExtensionTest.h new file mode 100644 index 000000000000..15f52146c78a --- /dev/null +++ b/toolkit/components/extensions/webidl-api/ExtensionTest.h @@ -0,0 +1,63 @@ +/* -*- 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_ExtensionTest_h +#define mozilla_extensions_ExtensionTest_h + +#include "js/TypeDecls.h" +#include "mozilla/Attributes.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "nsCycleCollectionParticipant.h" +#include "nsCOMPtr.h" +#include "nsISupports.h" +#include "nsWrapperCache.h" + +#include "ExtensionAPIBase.h" +#include "ExtensionBrowser.h" + +class nsIGlobalObject; + +namespace mozilla { + +namespace extensions { + +class ExtensionEventManager; + +class ExtensionTest final : public nsISupports, + public nsWrapperCache, + public ExtensionAPINamespace { + nsCOMPtr mGlobal; + RefPtr mOnMessageEventMgr; + + ~ExtensionTest() = default; + + public: + ExtensionTest(nsIGlobalObject* aGlobal, ExtensionBrowser* aExtensionBrowser); + + // ExtensionAPIBase methods + nsIGlobalObject* GetGlobalObject() const override { return mGlobal; } + + nsString GetAPINamespace() const override { return u"test"_ns; } + + // nsWrapperCache interface methods + JSObject* WrapObject(JSContext* aCx, + JS::Handle aGivenProto) override; + + // DOM bindings methods + static bool IsAllowed(JSContext* aCx, JSObject* aGlobal); + + nsIGlobalObject* GetParentObject() const; + + ExtensionEventManager* OnMessage(); + + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ExtensionTest) +}; + +} // namespace extensions +} // namespace mozilla + +#endif // mozilla_extensions_ExtensionTest_h diff --git a/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf b/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf index 5f8537cdeb4d..ff4739e6a49a 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf +++ b/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf @@ -47,7 +47,12 @@ WEBEXT_TYPES_MAPPING = { # "namespace.methodName": "WebExtensionStubName", # # NOTE: Please keep this table in alphabetic order. -WEBEXT_STUBS_MAPPING = {} +WEBEXT_STUBS_MAPPING = { + "test.assertEq": "NotImplementedNoReturn", + "test.assertRejects": "NotImplementedAsync", + "test.assertThrows": "NotImplementedNoReturn", + "test.withHandlingUserInput": "NotImplementedNoReturn", +} # Mapping table for the directories where the JSON API schema will be loaded # from. diff --git a/toolkit/components/extensions/webidl-api/moz.build b/toolkit/components/extensions/webidl-api/moz.build index 71581114e368..e66dbd2338ba 100644 --- a/toolkit/components/extensions/webidl-api/moz.build +++ b/toolkit/components/extensions/webidl-api/moz.build @@ -7,6 +7,7 @@ with Files("**"): BUG_COMPONENT = ("WebExtensions", "General") +# WebExtensions API objects and request handling internals. UNIFIED_SOURCES += [ "ExtensionAPIBase.cpp", "ExtensionAPIRequest.cpp", @@ -24,6 +25,15 @@ EXPORTS.mozilla.extensions += [ "ExtensionPort.h", ] +# WebExtensions API namespaces. +UNIFIED_SOURCES += [ + "ExtensionTest.cpp", +] + +EXPORTS.mozilla.extensions += [ + "ExtensionTest.h", +] + # The following is not a real WebExtensions API, it is a test WebIDL # interface that includes a collection of the cases useful to unit # test the API request forwarding mechanism without tying it to