From 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:47:29 +0200 Subject: Adding upstream version 115.8.0esr. Signed-off-by: Daniel Baumann --- .../WebProtocolHandlerRegistrar.sys.mjs | 177 +++++++++++++++++++++ browser/components/protocolhandler/components.conf | 15 ++ browser/components/protocolhandler/moz.build | 18 +++ .../protocolhandler/test/browser/browser.ini | 5 + ...owser_registerProtocolHandler_notification.html | 15 ++ ...browser_registerProtocolHandler_notification.js | 61 +++++++ .../protocolhandler/test/test_registerHandler.html | 88 ++++++++++ 7 files changed, 379 insertions(+) create mode 100644 browser/components/protocolhandler/WebProtocolHandlerRegistrar.sys.mjs create mode 100644 browser/components/protocolhandler/components.conf create mode 100644 browser/components/protocolhandler/moz.build create mode 100644 browser/components/protocolhandler/test/browser/browser.ini create mode 100644 browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.html create mode 100644 browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.js create mode 100644 browser/components/protocolhandler/test/test_registerHandler.html (limited to 'browser/components/protocolhandler') diff --git a/browser/components/protocolhandler/WebProtocolHandlerRegistrar.sys.mjs b/browser/components/protocolhandler/WebProtocolHandlerRegistrar.sys.mjs new file mode 100644 index 0000000000..023dad6855 --- /dev/null +++ b/browser/components/protocolhandler/WebProtocolHandlerRegistrar.sys.mjs @@ -0,0 +1,177 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 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/. */ + +const STRING_BUNDLE_URI = "chrome://browser/locale/feeds/subscribe.properties"; + +export function WebProtocolHandlerRegistrar() {} + +WebProtocolHandlerRegistrar.prototype = { + get stringBundle() { + let sb = Services.strings.createBundle(STRING_BUNDLE_URI); + delete WebProtocolHandlerRegistrar.prototype.stringBundle; + return (WebProtocolHandlerRegistrar.prototype.stringBundle = sb); + }, + + _getFormattedString(key, params) { + return this.stringBundle.formatStringFromName(key, params); + }, + + _getString(key) { + return this.stringBundle.GetStringFromName(key); + }, + + /** + * See nsIWebProtocolHandlerRegistrar + */ + removeProtocolHandler(aProtocol, aURITemplate) { + let eps = Cc[ + "@mozilla.org/uriloader/external-protocol-service;1" + ].getService(Ci.nsIExternalProtocolService); + let handlerInfo = eps.getProtocolHandlerInfo(aProtocol); + let handlers = handlerInfo.possibleApplicationHandlers; + for (let i = 0; i < handlers.length; i++) { + try { + // We only want to test web handlers + let handler = handlers.queryElementAt(i, Ci.nsIWebHandlerApp); + if (handler.uriTemplate == aURITemplate) { + handlers.removeElementAt(i); + let hs = Cc["@mozilla.org/uriloader/handler-service;1"].getService( + Ci.nsIHandlerService + ); + hs.store(handlerInfo); + return; + } + } catch (e) { + /* it wasn't a web handler */ + } + } + }, + + /** + * Determines if a web handler is already registered. + * + * @param aProtocol + * The scheme of the web handler we are checking for. + * @param aURITemplate + * The URI template that the handler uses to handle the protocol. + * @return true if it is already registered, false otherwise. + */ + _protocolHandlerRegistered(aProtocol, aURITemplate) { + let eps = Cc[ + "@mozilla.org/uriloader/external-protocol-service;1" + ].getService(Ci.nsIExternalProtocolService); + let handlerInfo = eps.getProtocolHandlerInfo(aProtocol); + let handlers = handlerInfo.possibleApplicationHandlers; + for (let i = 0; i < handlers.length; i++) { + try { + // We only want to test web handlers + let handler = handlers.queryElementAt(i, Ci.nsIWebHandlerApp); + if (handler.uriTemplate == aURITemplate) { + return true; + } + } catch (e) { + /* it wasn't a web handler */ + } + } + return false; + }, + + /** + * See nsIWebProtocolHandlerRegistrar + */ + registerProtocolHandler( + aProtocol, + aURI, + aTitle, + aDocumentURI, + aBrowserOrWindow + ) { + aProtocol = (aProtocol || "").toLowerCase(); + if (!aURI || !aDocumentURI) { + return; + } + + let browser = aBrowserOrWindow; // This is the e10s case. + if (aBrowserOrWindow instanceof Ci.nsIDOMWindow) { + // In the non-e10s case, grab the browser off the same-process window. + let rootDocShell = aBrowserOrWindow.docShell.sameTypeRootTreeItem; + browser = rootDocShell.QueryInterface(Ci.nsIDocShell).chromeEventHandler; + } + + let browserWindow = browser.ownerGlobal; + try { + browserWindow.navigator.checkProtocolHandlerAllowed( + aProtocol, + aURI, + aDocumentURI + ); + } catch (ex) { + // We should have already shown the user an error. + return; + } + + // If the protocol handler is already registered, just return early. + if (this._protocolHandlerRegistered(aProtocol, aURI.spec)) { + return; + } + + // Now Ask the user and provide the proper callback + let message = this._getFormattedString("addProtocolHandlerMessage", [ + aURI.host, + aProtocol, + ]); + + let notificationIcon = aURI.prePath + "/favicon.ico"; + let notificationValue = "Protocol Registration: " + aProtocol; + let addButton = { + label: this._getString("addProtocolHandlerAddButton"), + accessKey: this._getString("addProtocolHandlerAddButtonAccesskey"), + protocolInfo: { protocol: aProtocol, uri: aURI.spec, name: aTitle }, + + callback(aNotification, aButtonInfo) { + let protocol = aButtonInfo.protocolInfo.protocol; + let name = aButtonInfo.protocolInfo.name; + + let handler = Cc[ + "@mozilla.org/uriloader/web-handler-app;1" + ].createInstance(Ci.nsIWebHandlerApp); + handler.name = name; + handler.uriTemplate = aButtonInfo.protocolInfo.uri; + + let eps = Cc[ + "@mozilla.org/uriloader/external-protocol-service;1" + ].getService(Ci.nsIExternalProtocolService); + let handlerInfo = eps.getProtocolHandlerInfo(protocol); + handlerInfo.possibleApplicationHandlers.appendElement(handler); + + // Since the user has agreed to add a new handler, chances are good + // that the next time they see a handler of this type, they're going + // to want to use it. Reset the handlerInfo to ask before the next + // use. + handlerInfo.alwaysAskBeforeHandling = true; + + let hs = Cc["@mozilla.org/uriloader/handler-service;1"].getService( + Ci.nsIHandlerService + ); + hs.store(handlerInfo); + }, + }; + let notificationBox = browser.getTabBrowser().getNotificationBox(browser); + notificationBox.appendNotification( + notificationValue, + { + label: message, + image: notificationIcon, + priority: notificationBox.PRIORITY_INFO_LOW, + }, + [addButton] + ); + }, + + /** + * See nsISupports + */ + QueryInterface: ChromeUtils.generateQI(["nsIWebProtocolHandlerRegistrar"]), +}; diff --git a/browser/components/protocolhandler/components.conf b/browser/components/protocolhandler/components.conf new file mode 100644 index 0000000000..6fd4f03595 --- /dev/null +++ b/browser/components/protocolhandler/components.conf @@ -0,0 +1,15 @@ +# -*- 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': '{efbd7b87-9b15-4684-abf0-dc2679daadb1}', + 'contract_ids': ['@mozilla.org/embeddor.implemented/web-protocol-handler-registrar;1'], + 'esModule': 'resource:///modules/WebProtocolHandlerRegistrar.sys.mjs', + 'constructor': 'WebProtocolHandlerRegistrar', + 'processes': ProcessSelector.MAIN_PROCESS_ONLY, + }, +] diff --git a/browser/components/protocolhandler/moz.build b/browser/components/protocolhandler/moz.build new file mode 100644 index 0000000000..84fd43d18a --- /dev/null +++ b/browser/components/protocolhandler/moz.build @@ -0,0 +1,18 @@ +# -*- 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/. + +BROWSER_CHROME_MANIFESTS += ["test/browser/browser.ini"] + +EXTRA_JS_MODULES += [ + "WebProtocolHandlerRegistrar.sys.mjs", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +with Files("**"): + BUG_COMPONENT = ("Firefox", "General") diff --git a/browser/components/protocolhandler/test/browser/browser.ini b/browser/components/protocolhandler/test/browser/browser.ini new file mode 100644 index 0000000000..4ed1c68974 --- /dev/null +++ b/browser/components/protocolhandler/test/browser/browser.ini @@ -0,0 +1,5 @@ +[browser_registerProtocolHandler_notification.js] +support-files = + browser_registerProtocolHandler_notification.html +skip-if = + verify diff --git a/browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.html b/browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.html new file mode 100644 index 0000000000..6ffacd2e85 --- /dev/null +++ b/browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.html @@ -0,0 +1,15 @@ + + + + Protocol registrar page + + + + + + + diff --git a/browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.js b/browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.js new file mode 100644 index 0000000000..0f4e41f1a3 --- /dev/null +++ b/browser/components/protocolhandler/test/browser/browser_registerProtocolHandler_notification.js @@ -0,0 +1,61 @@ +/* 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/. */ + +const TEST_PATH = getRootDirectory(gTestPath).replace( + "chrome://mochitests/content", + "https://example.com" +); +add_task(async function () { + let notificationValue = "Protocol Registration: web+testprotocol"; + let testURI = TEST_PATH + "browser_registerProtocolHandler_notification.html"; + + BrowserTestUtils.loadURIString(window.gBrowser.selectedBrowser, testURI); + await TestUtils.waitForCondition( + function () { + // Do not start until the notification is up + let notificationBox = window.gBrowser.getNotificationBox(); + let notification = + notificationBox.getNotificationWithValue(notificationValue); + return notification; + }, + "Still can not get notification after retrying 100 times.", + 100, + 100 + ); + + let notificationBox = window.gBrowser.getNotificationBox(); + let notification = + notificationBox.getNotificationWithValue(notificationValue); + ok(notification, "Notification box should be displayed"); + if (notification == null) { + finish(); + return; + } + is( + notification.getAttribute("type"), + "info", + "We expect this notification to have the type of 'info'." + ); + + // Make sure the CSS is fully loaded... + ok( + await TestUtils.waitForCondition( + () => + notification.ownerGlobal.getComputedStyle( + notification.messageImage, + "::after" + ).content == 'url("chrome://global/skin/icons/info-filled.svg")' + ), + "We expect this notification to have an icon." + ); + + let buttons = notification.buttonContainer.getElementsByClassName( + "notification-button" + ); + is(buttons.length, 1, "We expect see one button."); + + let button = buttons[0]; + isnot(button.label, null, "We expect the add button to have a label."); + todo(button.accesskey, "We expect the add button to have a accesskey."); +}); diff --git a/browser/components/protocolhandler/test/test_registerHandler.html b/browser/components/protocolhandler/test/test_registerHandler.html new file mode 100644 index 0000000000..2eb4f7dbdc --- /dev/null +++ b/browser/components/protocolhandler/test/test_registerHandler.html @@ -0,0 +1,88 @@ + + + + + Test for Bug 402788 + + + + +Mozilla Bug 402788 +

+ +
+
+
+ + -- cgit v1.2.3