diff --git a/dom/bindings/Bindings.conf b/dom/bindings/Bindings.conf index dbe534a413e3..2dbe0e6fc251 100644 --- a/dom/bindings/Bindings.conf +++ b/dom/bindings/Bindings.conf @@ -1687,6 +1687,11 @@ DOMInterfaces = { 'nativeType': 'mozilla::extensions::ExtensionMockAPI', }, +'ExtensionDns': { + 'headerFile': 'mozilla/extensions/ExtensionDns.h', + 'nativeType': 'mozilla::extensions::ExtensionDns', +}, + 'ExtensionEventManager': { 'headerFile': 'mozilla/extensions/ExtensionEventManager.h', 'nativeType': 'mozilla::extensions::ExtensionEventManager', diff --git a/dom/webidl/ExtensionBrowser.webidl b/dom/webidl/ExtensionBrowser.webidl index 0544a16bb3ab..1abd2760935d 100644 --- a/dom/webidl/ExtensionBrowser.webidl +++ b/dom/webidl/ExtensionBrowser.webidl @@ -35,6 +35,11 @@ interface ExtensionBrowser { Func="mozilla::extensions::ExtensionAlarms::IsAllowed"] readonly attribute ExtensionAlarms alarms; + // `browser.dns` API namespace + [Replaceable, SameObject, BinaryName="GetExtensionDns", + Func="mozilla::extensions::ExtensionDns::IsAllowed"] + readonly attribute ExtensionDns dns; + // `browser.runtime` API namespace [Replaceable, SameObject, BinaryName="GetExtensionRuntime", Func="mozilla::extensions::ExtensionRuntime::IsAllowed"] diff --git a/dom/webidl/ExtensionDns.webidl b/dom/webidl/ExtensionDns.webidl new file mode 100644 index 000000000000..21d296820a5d --- /dev/null +++ b/dom/webidl/ExtensionDns.webidl @@ -0,0 +1,30 @@ +/* + * 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 -- dns + * + * 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 "dns" WebExtensions API +[Exposed=(ServiceWorker), LegacyNoInterfaceObject] +interface ExtensionDns { + // API methods. + + [Throws, WebExtensionStub="AsyncAmbiguous"] + any resolve(any... args); +}; diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index 41fd233a2de8..4bb339c31b98 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -1110,6 +1110,7 @@ WEBIDL_FILES += [ WEBIDL_FILES += [ "ExtensionAlarms.webidl", "ExtensionBrowser.webidl", + "ExtensionDns.webidl", "ExtensionEventManager.webidl", # ExtensionMockAPI is not a real WebExtensions API, and it is only enabled in tests. "ExtensionMockAPI.webidl", diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_dns.js b/toolkit/components/extensions/test/xpcshell/test_ext_dns.js index d7f9d6efe98c..b861cc73fc5e 100644 --- a/toolkit/components/extensions/test/xpcshell/test_ext_dns.js +++ b/toolkit/components/extensions/test/xpcshell/test_ext_dns.js @@ -100,7 +100,7 @@ add_task(async function startup() { await AddonTestUtils.promiseStartupManager(); }); -add_task(async function test_dns_resolve() { +async function test_dns_resolve() { let extension = getExtension(); await extension.startup(); await extension.awaitMessage("ready"); @@ -137,9 +137,9 @@ add_task(async function test_dns_resolve() { } await extension.unload(); -}); +} -add_task(async function test_dns_resolve_socks() { +async function test_dns_resolve_socks() { let extension = getExtension(); await extension.startup(); await extension.awaitMessage("ready"); @@ -173,4 +173,13 @@ add_task(async function test_dns_resolve_socks() { `expected error ${result.message}` ); await extension.unload(); -}); +} + +add_task(test_dns_resolve); +add_task( + { + // TODO(Bug 1748313): remove the skip_if once we have introduced the proxy API namespace. + skip_if: () => ExtensionTestUtils.isInBackgroundServiceWorkerTests(), + }, + test_dns_resolve_socks +); diff --git a/toolkit/components/extensions/test/xpcshell/xpcshell-serviceworker.ini b/toolkit/components/extensions/test/xpcshell/xpcshell-serviceworker.ini index 12346a0c7553..86b9b1589d5b 100644 --- a/toolkit/components/extensions/test/xpcshell/xpcshell-serviceworker.ini +++ b/toolkit/components/extensions/test/xpcshell/xpcshell-serviceworker.ini @@ -20,6 +20,7 @@ prefs = [test_ext_alarms_replaces.js] [test_ext_background_service_worker.js] [test_ext_contentscript_dynamic_registration.js] +[test_ext_dns.js] [test_ext_runtime_getBackgroundPage.js] [test_ext_scripting_contentScripts.js] [test_ext_scripting_contentScripts_css.js] diff --git a/toolkit/components/extensions/webidl-api/ExtensionAPI.cpp.in b/toolkit/components/extensions/webidl-api/ExtensionAPI.cpp.in index c97fc87c78f7..3c3a4b331392 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionAPI.cpp.in +++ b/toolkit/components/extensions/webidl-api/ExtensionAPI.cpp.in @@ -9,8 +9,7 @@ #include "mozilla/dom/{{ webidl_name }}Binding.h" #include "nsIGlobalObject.h" -namespace mozilla { -namespace extensions { +namespace mozilla::extensions { NS_IMPL_CYCLE_COLLECTING_ADDREF({{ webidl_name }}); NS_IMPL_CYCLE_COLLECTING_RELEASE({{ webidl_name }}) @@ -51,5 +50,4 @@ ExtensionEventManager* {{ webidl_name}}::OnEVENTNAME() { } */ -} // namespace extensions -} // namespace mozilla +} // namespace mozilla::extensions diff --git a/toolkit/components/extensions/webidl-api/ExtensionAPI.h.in b/toolkit/components/extensions/webidl-api/ExtensionAPI.h.in index b3b67c83a49a..f902d98d2ec3 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionAPI.h.in +++ b/toolkit/components/extensions/webidl-api/ExtensionAPI.h.in @@ -20,9 +20,7 @@ class nsIGlobalObject; -namespace mozilla { - -namespace extensions { +namespace mozilla::extensions { class ExtensionEventManager; @@ -68,7 +66,6 @@ class {{ webidl_name }} final : public nsISupports, // RefPtr mOnEVENTNAMEEventMgr; }; -} // namespace extensions -} // namespace mozilla +} // namespace mozilla::extensions #endif // mozilla_extensions_{{ webidl_name }}_h diff --git a/toolkit/components/extensions/webidl-api/ExtensionAPI.webidl.in b/toolkit/components/extensions/webidl-api/ExtensionAPI.webidl.in index 2a7e911a79ed..544700ae6a2d 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionAPI.webidl.in +++ b/toolkit/components/extensions/webidl-api/ExtensionAPI.webidl.in @@ -1,4 +1,4 @@ -/* +/* * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT * * The content of this file has been generated based on the WebExtensions API diff --git a/toolkit/components/extensions/webidl-api/ExtensionBrowser.cpp b/toolkit/components/extensions/webidl-api/ExtensionBrowser.cpp index 896ca69cadca..4932d6b41c89 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionBrowser.cpp +++ b/toolkit/components/extensions/webidl-api/ExtensionBrowser.cpp @@ -10,6 +10,7 @@ #include "mozilla/dom/ExtensionPortBinding.h" // ExtensionPortDescriptor #include "mozilla/dom/WorkerScope.h" // GetWorkerPrivateFromContext #include "mozilla/extensions/ExtensionAlarms.h" +#include "mozilla/extensions/ExtensionDns.h" #include "mozilla/extensions/ExtensionMockAPI.h" #include "mozilla/extensions/ExtensionPort.h" #include "mozilla/extensions/ExtensionRuntime.h" @@ -32,6 +33,7 @@ NS_INTERFACE_MAP_END NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ExtensionBrowser) NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal) NS_IMPL_CYCLE_COLLECTION_UNLINK(mExtensionAlarms) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mExtensionDns) NS_IMPL_CYCLE_COLLECTION_UNLINK(mExtensionMockAPI) NS_IMPL_CYCLE_COLLECTION_UNLINK(mExtensionRuntime) NS_IMPL_CYCLE_COLLECTION_UNLINK(mExtensionScripting) @@ -44,6 +46,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ExtensionBrowser) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExtensionAlarms) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExtensionDns) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExtensionMockAPI) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExtensionRuntime) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExtensionScripting) @@ -197,6 +200,14 @@ ExtensionAlarms* ExtensionBrowser::GetExtensionAlarms() { return mExtensionAlarms; } +ExtensionDns* ExtensionBrowser::GetExtensionDns() { + if (!mExtensionDns) { + mExtensionDns = new ExtensionDns(mGlobal, this); + } + + return mExtensionDns; +} + ExtensionMockAPI* ExtensionBrowser::GetExtensionMockAPI() { if (!mExtensionMockAPI) { mExtensionMockAPI = new ExtensionMockAPI(mGlobal, this); diff --git a/toolkit/components/extensions/webidl-api/ExtensionBrowser.h b/toolkit/components/extensions/webidl-api/ExtensionBrowser.h index cf5cc9bc9510..b21d875b2ad8 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionBrowser.h +++ b/toolkit/components/extensions/webidl-api/ExtensionBrowser.h @@ -21,6 +21,7 @@ class ErrorResult; namespace extensions { class ExtensionAlarms; +class ExtensionDns; class ExtensionMockAPI; class ExtensionPort; class ExtensionRuntime; @@ -103,6 +104,7 @@ class ExtensionBrowser final : public nsISupports, public nsWrapperCache { nsIGlobalObject* GetParentObject() const; ExtensionAlarms* GetExtensionAlarms(); + ExtensionDns* GetExtensionDns(); ExtensionMockAPI* GetExtensionMockAPI(); ExtensionRuntime* GetExtensionRuntime(); ExtensionScripting* GetExtensionScripting(); @@ -118,6 +120,7 @@ class ExtensionBrowser final : public nsISupports, public nsWrapperCache { JS::Heap mLastError; bool mCheckedLastError; RefPtr mExtensionAlarms; + RefPtr mExtensionDns; RefPtr mExtensionMockAPI; RefPtr mExtensionRuntime; RefPtr mExtensionScripting; diff --git a/toolkit/components/extensions/webidl-api/ExtensionDns.cpp b/toolkit/components/extensions/webidl-api/ExtensionDns.cpp new file mode 100644 index 000000000000..b38df15ce288 --- /dev/null +++ b/toolkit/components/extensions/webidl-api/ExtensionDns.cpp @@ -0,0 +1,40 @@ +/* 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 "ExtensionDns.h" +#include "ExtensionEventManager.h" + +#include "mozilla/dom/ExtensionDnsBinding.h" +#include "nsIGlobalObject.h" + +namespace mozilla::extensions { + +NS_IMPL_CYCLE_COLLECTING_ADDREF(ExtensionDns); +NS_IMPL_CYCLE_COLLECTING_RELEASE(ExtensionDns) +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ExtensionDns, mGlobal, mExtensionBrowser); + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ExtensionDns) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +ExtensionDns::ExtensionDns(nsIGlobalObject* aGlobal, + ExtensionBrowser* aExtensionBrowser) + : mGlobal(aGlobal), mExtensionBrowser(aExtensionBrowser) { + MOZ_DIAGNOSTIC_ASSERT(mGlobal); + MOZ_DIAGNOSTIC_ASSERT(mExtensionBrowser); +} + +/* static */ +bool ExtensionDns::IsAllowed(JSContext* aCx, JSObject* aGlobal) { return true; } + +JSObject* ExtensionDns::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) { + return dom::ExtensionDns_Binding::Wrap(aCx, this, aGivenProto); +} + +nsIGlobalObject* ExtensionDns::GetParentObject() const { return mGlobal; } + +} // namespace mozilla::extensions diff --git a/toolkit/components/extensions/webidl-api/ExtensionDns.h b/toolkit/components/extensions/webidl-api/ExtensionDns.h new file mode 100644 index 000000000000..7359bbf10f34 --- /dev/null +++ b/toolkit/components/extensions/webidl-api/ExtensionDns.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_ExtensionDns_h +#define mozilla_extensions_ExtensionDns_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::extensions { + +class ExtensionEventManager; + +class ExtensionDns final : public nsISupports, + public nsWrapperCache, + public ExtensionAPINamespace { + public: + ExtensionDns(nsIGlobalObject* aGlobal, ExtensionBrowser* aExtensionBrowser); + + // ExtensionAPIBase methods + nsIGlobalObject* GetGlobalObject() const override { return mGlobal; } + + ExtensionBrowser* GetExtensionBrowser() const override { + return mExtensionBrowser; + } + + nsString GetAPINamespace() const override { return u"dns"_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; + + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ExtensionDns) + + private: + ~ExtensionDns() = default; + + nsCOMPtr mGlobal; + RefPtr mExtensionBrowser; +}; + +} // namespace mozilla::extensions + +#endif // mozilla_extensions_ExtensionDns_h diff --git a/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf b/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf index fee04dd08817..be312c80d4dd 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf +++ b/toolkit/components/extensions/webidl-api/ExtensionWebIDL.conf @@ -60,6 +60,7 @@ WEBIDL_PRIMITIVE_TYPES = set([ # # NOTE: Please keep this table in alphabetic order. WEBEXT_STUBS_MAPPING = { + "dns.resolve": "AsyncAmbiguous", "runtime.connect": "ReturnsPort", "runtime.connectNative": "ReturnsPort", "runtime.getURL": "ReturnsString", diff --git a/toolkit/components/extensions/webidl-api/moz.build b/toolkit/components/extensions/webidl-api/moz.build index f4bd19770f64..71b3d5f92931 100644 --- a/toolkit/components/extensions/webidl-api/moz.build +++ b/toolkit/components/extensions/webidl-api/moz.build @@ -28,6 +28,7 @@ EXPORTS.mozilla.extensions += [ # WebExtensions API namespaces. UNIFIED_SOURCES += [ "ExtensionAlarms.cpp", + "ExtensionDns.cpp", "ExtensionRuntime.cpp", "ExtensionScripting.cpp", "ExtensionTest.cpp", @@ -35,6 +36,7 @@ UNIFIED_SOURCES += [ EXPORTS.mozilla.extensions += [ "ExtensionAlarms.h", + "ExtensionDns.h", "ExtensionRuntime.h", "ExtensionScripting.h", "ExtensionTest.h",