summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/parent/ext-protocolHandlers.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /toolkit/components/extensions/parent/ext-protocolHandlers.js
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/extensions/parent/ext-protocolHandlers.js')
-rw-r--r--toolkit/components/extensions/parent/ext-protocolHandlers.js100
1 files changed, 100 insertions, 0 deletions
diff --git a/toolkit/components/extensions/parent/ext-protocolHandlers.js b/toolkit/components/extensions/parent/ext-protocolHandlers.js
new file mode 100644
index 0000000000..36cdf25d42
--- /dev/null
+++ b/toolkit/components/extensions/parent/ext-protocolHandlers.js
@@ -0,0 +1,100 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et 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/. */
+
+"use strict";
+
+XPCOMUtils.defineLazyServiceGetter(
+ this,
+ "handlerService",
+ "@mozilla.org/uriloader/handler-service;1",
+ "nsIHandlerService"
+);
+XPCOMUtils.defineLazyServiceGetter(
+ this,
+ "protocolService",
+ "@mozilla.org/uriloader/external-protocol-service;1",
+ "nsIExternalProtocolService"
+);
+
+const hasHandlerApp = handlerConfig => {
+ let protoInfo = protocolService.getProtocolHandlerInfo(
+ handlerConfig.protocol
+ );
+ let appHandlers = protoInfo.possibleApplicationHandlers;
+ for (let i = 0; i < appHandlers.length; i++) {
+ let handler = appHandlers.queryElementAt(i, Ci.nsISupports);
+ if (
+ handler instanceof Ci.nsIWebHandlerApp &&
+ handler.uriTemplate === handlerConfig.uriTemplate
+ ) {
+ return true;
+ }
+ }
+ return false;
+};
+
+this.protocolHandlers = class extends ExtensionAPI {
+ onManifestEntry(entryName) {
+ let { extension } = this;
+ let { manifest } = extension;
+
+ for (let handlerConfig of manifest.protocol_handlers) {
+ if (hasHandlerApp(handlerConfig)) {
+ continue;
+ }
+
+ let handler = Cc[
+ "@mozilla.org/uriloader/web-handler-app;1"
+ ].createInstance(Ci.nsIWebHandlerApp);
+ handler.name = handlerConfig.name;
+ handler.uriTemplate = handlerConfig.uriTemplate;
+
+ let protoInfo = protocolService.getProtocolHandlerInfo(
+ handlerConfig.protocol
+ );
+ let handlers = protoInfo.possibleApplicationHandlers;
+ if (protoInfo.preferredApplicationHandler || handlers.length) {
+ protoInfo.alwaysAskBeforeHandling = true;
+ } else {
+ protoInfo.preferredApplicationHandler = handler;
+ protoInfo.alwaysAskBeforeHandling = false;
+ }
+ handlers.appendElement(handler);
+ handlerService.store(protoInfo);
+ }
+ }
+
+ onShutdown(isAppShutdown) {
+ let { extension } = this;
+ let { manifest } = extension;
+
+ if (isAppShutdown) {
+ return;
+ }
+
+ for (let handlerConfig of manifest.protocol_handlers) {
+ let protoInfo = protocolService.getProtocolHandlerInfo(
+ handlerConfig.protocol
+ );
+ let appHandlers = protoInfo.possibleApplicationHandlers;
+ for (let i = 0; i < appHandlers.length; i++) {
+ let handler = appHandlers.queryElementAt(i, Ci.nsISupports);
+ if (
+ handler instanceof Ci.nsIWebHandlerApp &&
+ handler.uriTemplate === handlerConfig.uriTemplate
+ ) {
+ appHandlers.removeElementAt(i);
+ if (protoInfo.preferredApplicationHandler === handler) {
+ protoInfo.preferredApplicationHandler = null;
+ protoInfo.alwaysAskBeforeHandling = true;
+ }
+ handlerService.store(protoInfo);
+ break;
+ }
+ }
+ }
+ }
+};