diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp index 6eaa9c969125..e7e299613ceb 100644 --- a/dom/ipc/ContentChild.cpp +++ b/dom/ipc/ContentChild.cpp @@ -81,6 +81,7 @@ #include "mozilla/dom/WorkerDebugger.h" #include "mozilla/dom/WorkerDebuggerManager.h" #include "mozilla/dom/ipc/SharedMap.h" +#include "mozilla/extensions/ExtensionsChild.h" #include "mozilla/extensions/StreamFilterParent.h" #include "mozilla/gfx/Logging.h" #include "mozilla/gfx/gfxVars.h" diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index 88ab51e27469..887c19681755 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -126,6 +126,7 @@ #include "mozilla/dom/power/PowerManagerService.h" #include "mozilla/dom/quota/QuotaManagerService.h" #include "mozilla/embedding/printingui/PrintingParent.h" +#include "mozilla/extensions/ExtensionsParent.h" #include "mozilla/extensions/StreamFilterParent.h" #include "mozilla/gfx/GPUProcessManager.h" #include "mozilla/gfx/gfxVars.h" @@ -5000,6 +5001,11 @@ mozilla::ipc::IPCResult ContentParent::RecvCreateAudioIPCConnection( return IPC_OK(); } +already_AddRefed +ContentParent::AllocPExtensionsParent() { + return MakeAndAddRef(); +} + PFileDescriptorSetParent* ContentParent::AllocPFileDescriptorSetParent( const FileDescriptor& aFD) { return new FileDescriptorSetParent(aFD); diff --git a/dom/ipc/ContentParent.h b/dom/ipc/ContentParent.h index ebdf3fdd5e79..077949926b69 100644 --- a/dom/ipc/ContentParent.h +++ b/dom/ipc/ContentParent.h @@ -1177,6 +1177,8 @@ class ContentParent final mozilla::ipc::IPCResult RecvCreateAudioIPCConnection( CreateAudioIPCConnectionResolver&& aResolver); + already_AddRefed AllocPExtensionsParent(); + PFileDescriptorSetParent* AllocPFileDescriptorSetParent( const mozilla::ipc::FileDescriptor&); diff --git a/dom/ipc/PContent.ipdl b/dom/ipc/PContent.ipdl index b2772d6dd4ef..056699072831 100644 --- a/dom/ipc/PContent.ipdl +++ b/dom/ipc/PContent.ipdl @@ -10,6 +10,7 @@ include protocol PCompositorManager; include protocol PContentPermissionRequest; include protocol PCycleCollectWithLogs; include protocol PDocumentChannel; +include protocol PExtensions; include protocol PExternalHelperApp; include protocol PHandlerService; include protocol PFileDescriptorSet; @@ -406,6 +407,7 @@ nested(upto inside_cpow) sync protocol PContent manages PBrowser; manages PContentPermissionRequest; manages PCycleCollectWithLogs; + manages PExtensions; manages PExternalHelperApp; manages PFileDescriptorSet; manages PHal; @@ -470,6 +472,8 @@ parent: async CloneDocumentTreeInto(MaybeDiscardedBrowsingContext aSourceBc, MaybeDiscardedBrowsingContext aTargetBc); + async PExtensions(); + child: async ConstructBrowser(ManagedEndpoint browserEp, ManagedEndpoint windowEp, diff --git a/dom/ipc/PInProcess.ipdl b/dom/ipc/PInProcess.ipdl index 6b14ae36638d..cce87a0a528f 100644 --- a/dom/ipc/PInProcess.ipdl +++ b/dom/ipc/PInProcess.ipdl @@ -4,6 +4,7 @@ * 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 protocol PExtensions; include protocol PWindowGlobal; include DOMTypes; @@ -21,6 +22,7 @@ namespace dom { */ async refcounted protocol PInProcess { + manages PExtensions; manages PWindowGlobal; }; diff --git a/toolkit/components/extensions/ExtensionsChild.cpp b/toolkit/components/extensions/ExtensionsChild.cpp new file mode 100644 index 000000000000..1b61bafb8ce9 --- /dev/null +++ b/toolkit/components/extensions/ExtensionsChild.cpp @@ -0,0 +1,61 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* 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 "mozilla/extensions/ExtensionsChild.h" +#include "mozilla/extensions/ExtensionsParent.h" + +#include "mozilla/ClearOnShutdown.h" +#include "mozilla/dom/ContentChild.h" +#include "mozilla/dom/InProcessChild.h" +#include "mozilla/dom/InProcessParent.h" +#include "mozilla/ipc/Endpoint.h" +#include "nsXULAppAPI.h" + +using namespace mozilla::dom; + +namespace mozilla { +namespace extensions { + +NS_IMPL_ISUPPORTS0(ExtensionsChild) + +/* static */ +ExtensionsChild& ExtensionsChild::Get() { + static RefPtr sInstance; + + if (MOZ_UNLIKELY(!sInstance)) { + sInstance = new ExtensionsChild(); + sInstance->Init(); + ClearOnShutdown(&sInstance); + } + return *sInstance; +} + +/* static */ +already_AddRefed ExtensionsChild::GetSingleton() { + return do_AddRef(&Get()); +} + +void ExtensionsChild::Init() { + if (XRE_IsContentProcess()) { + ContentChild::GetSingleton()->SendPExtensionsConstructor(this); + } else { + InProcessChild* ipChild = InProcessChild::Singleton(); + InProcessParent* ipParent = InProcessParent::Singleton(); + if (!ipChild || !ipParent) { + return; + } + + RefPtr parent = new ExtensionsParent(); + + ManagedEndpoint endpoint = + ipChild->OpenPExtensionsEndpoint(this); + ipParent->BindPExtensionsEndpoint(std::move(endpoint), parent); + } +} + +void ExtensionsChild::ActorDestroy(ActorDestroyReason aWhy) {} + +} // namespace extensions +} // namespace mozilla diff --git a/toolkit/components/extensions/ExtensionsChild.h b/toolkit/components/extensions/ExtensionsChild.h new file mode 100644 index 000000000000..e83d1046fe98 --- /dev/null +++ b/toolkit/components/extensions/ExtensionsChild.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* 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_ExtensionsChild_h +#define mozilla_extensions_ExtensionsChild_h + +#include "mozilla/extensions/PExtensionsChild.h" +#include "nsISupportsImpl.h" + +namespace mozilla { +namespace extensions { + +class ExtensionsChild final : public nsISupports, public PExtensionsChild { + public: + NS_DECL_ISUPPORTS + + static already_AddRefed GetSingleton(); + + static ExtensionsChild& Get(); + + private: + ExtensionsChild() = default; + ~ExtensionsChild() = default; + + void Init(); + + protected: + void ActorDestroy(ActorDestroyReason aWhy) override; +}; + +} // namespace extensions +} // namespace mozilla + +#endif // mozilla_extensions_ExtensionsChild_h diff --git a/toolkit/components/extensions/ExtensionsParent.cpp b/toolkit/components/extensions/ExtensionsParent.cpp new file mode 100644 index 000000000000..db74b3764799 --- /dev/null +++ b/toolkit/components/extensions/ExtensionsParent.cpp @@ -0,0 +1,16 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* 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 "mozilla/extensions/ExtensionsParent.h" +#include "mozilla/dom/ContentParent.h" +#include "mozilla/RefPtr.h" + +namespace mozilla { +namespace extensions { + +void ExtensionsParent::ActorDestroy(ActorDestroyReason aWhy) {} + +} // namespace extensions +} // namespace mozilla diff --git a/toolkit/components/extensions/ExtensionsParent.h b/toolkit/components/extensions/ExtensionsParent.h new file mode 100644 index 000000000000..76c4eee9a47e --- /dev/null +++ b/toolkit/components/extensions/ExtensionsParent.h @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* 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_ExtensionsParent_h +#define mozilla_extensions_ExtensionsParent_h + +#include "mozilla/extensions/PExtensionsParent.h" +#include "nsISupportsImpl.h" + +namespace mozilla { +namespace extensions { + +class ExtensionsParent final : public PExtensionsParent { + public: + NS_INLINE_DECL_REFCOUNTING(ExtensionsParent, final) + + ExtensionsParent() = default; + + private: + ~ExtensionsParent() = default; + + protected: + void ActorDestroy(ActorDestroyReason aWhy) override; +}; + +} // namespace extensions +} // namespace mozilla + +#endif // mozilla_extensions_ExtensionsParent_h diff --git a/toolkit/components/extensions/PExtensions.ipdl b/toolkit/components/extensions/PExtensions.ipdl new file mode 100644 index 000000000000..a93cf5d97dde --- /dev/null +++ b/toolkit/components/extensions/PExtensions.ipdl @@ -0,0 +1,28 @@ +/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8 -*- */ +/* vim: set sw=4 ts=8 et tw=80 ft=cpp : */ +/* 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 protocol PContent; +include protocol PInProcess; + +namespace mozilla { +namespace extensions { + +/** + * A generic protocol used by the extension framework for process-level IPC. A + * child instance is created at startup in the parent process and each content + * child process, which can be accessed via + * `mozilla::extensions::ExtensionsChild::Get()`. + */ +refcounted protocol PExtensions +{ + manager PContent or PInProcess; + + parent: + async __delete__(); +}; + +} // namespace extensions +} // namespace mozilla diff --git a/toolkit/components/extensions/components.conf b/toolkit/components/extensions/components.conf new file mode 100644 index 000000000000..0b6461f13dd6 --- /dev/null +++ b/toolkit/components/extensions/components.conf @@ -0,0 +1,16 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +Classes = [ + { + 'cid': '{db82286d-d649-47fb-8599-ba31673a58c5}', + 'contract_ids': ['@mozilla.org/extensions/child;1'], + 'type': 'mozilla::extensions::ExtensionsChild', + 'constructor': 'mozilla::extensions::ExtensionsChild::GetSingleton', + 'headers': ['mozilla/extensions/ExtensionsChild.h'], + 'categories': {'app-startup': 'ExtensionsChild'}, + }, +] diff --git a/toolkit/components/extensions/moz.build b/toolkit/components/extensions/moz.build index 6cca82a951e3..7037b43537f3 100755 --- a/toolkit/components/extensions/moz.build +++ b/toolkit/components/extensions/moz.build @@ -69,6 +69,10 @@ DIRS += [ "webrequest", ] +IPDL_SOURCES += [ + "PExtensions.ipdl", +] + XPIDL_SOURCES += [ "mozIExtensionProcessScript.idl", ] @@ -81,6 +85,8 @@ EXPORTS.mozilla = [ EXPORTS.mozilla.extensions = [ "DocumentObserver.h", + "ExtensionsChild.h", + "ExtensionsParent.h", "MatchGlob.h", "MatchPattern.h", "WebExtensionContentScript.h", @@ -89,10 +95,16 @@ EXPORTS.mozilla.extensions = [ UNIFIED_SOURCES += [ "ExtensionPolicyService.cpp", + "ExtensionsChild.cpp", + "ExtensionsParent.cpp", "MatchPattern.cpp", "WebExtensionPolicy.cpp", ] +XPCOM_MANIFESTS += [ + "components.conf", +] + FINAL_LIBRARY = "xul"