From 41a7e2e406552c67091493bcc4f9fe729a09f42c Mon Sep 17 00:00:00 2001 From: Barret Rennie Date: Thu, 2 Mar 2023 18:27:14 +0000 Subject: [PATCH] Bug 1805509 - Add SET_DEFAULT_PDF_HANDLER special message action r=omc-reviewers,emcminn Differential Revision: https://phabricator.services.mozilla.com/D169818 --- .../lib/SpecialMessageActions.jsm | 15 +++ .../SpecialMessageActionSchemas.json | 22 +++++ .../SpecialMessageActionSchemas/index.md | 15 ++- .../test/browser/browser.ini | 1 + .../browser_sma_default_pdf_handler.js | 92 +++++++++++++++++++ 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser_sma_default_pdf_handler.js diff --git a/toolkit/components/messaging-system/lib/SpecialMessageActions.jsm b/toolkit/components/messaging-system/lib/SpecialMessageActions.jsm index 93b1dda27ecd..73a12f63e03c 100644 --- a/toolkit/components/messaging-system/lib/SpecialMessageActions.jsm +++ b/toolkit/components/messaging-system/lib/SpecialMessageActions.jsm @@ -104,6 +104,15 @@ const SpecialMessageActions = { window.getShellService().setAsDefault(); }, + /** + * Set browser as the default PDF handler. + * + * @param {Window} window Reference to a window object + */ + setDefaultPDFHandler(window, onlyIfKnownBrowser = false) { + window.getShellService().setAsDefaultPDFHandler(onlyIfKnownBrowser); + }, + /** * Reset browser homepage and newtab to default with a certain section configuration * @@ -305,6 +314,12 @@ const SpecialMessageActions = { case "SET_DEFAULT_BROWSER": this.setDefaultBrowser(window); break; + case "SET_DEFAULT_PDF_HANDLER": + this.setDefaultPDFHandler( + window, + action.data?.onlyIfKnownBrowser ?? false + ); + break; case "PIN_CURRENT_TAB": let tab = window.gBrowser.selectedTab; window.gBrowser.pinTab(tab); diff --git a/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/SpecialMessageActionSchemas.json b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/SpecialMessageActionSchemas.json index fe35649f06bd..32e5f5ebb333 100644 --- a/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/SpecialMessageActionSchemas.json +++ b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/SpecialMessageActionSchemas.json @@ -367,6 +367,28 @@ "additionalProperties": false, "description": "Message action to set Firefox as default browser" }, + { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": ["SET_DEFAULT_PDF_HANDLER"] + }, + "data": { + "type": "object", + "properties": { + "onlyIfKnownBrowser": { + "type": "boolean", + "description": "Only set Firefox as the default PDF handler if the current PDF handler is a known browser." + } + }, + "additionalProperties": false + } + }, + "required": ["type"], + "additionalProperties": false, + "description": "Message action to set Firefox as the default PDF handler" + }, { "type": "object", "properties": { diff --git a/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/index.md b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/index.md index e3fcc6146db4..99a2e7692a98 100644 --- a/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/index.md +++ b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/index.md @@ -245,10 +245,23 @@ Action for pinning Firefox to the user's taskbar. ### `SET_DEFAULT_BROWSER` -Action for configuring the default browser to Firefox on the user's system. +Action for setting the default browser to Firefox on the user's system. - args: (none) +### `SET_DEFAULT_PDF_HANDLER` + +Action for setting the default PDF handler to Firefox on the user's system. + +- args: +```ts +{ + // Only set Firefox as the default PDF handler if the current PDF handler is a + // known browser. + onlyIfKnownBrowser?: boolean; +} +``` + ### `SHOW_SPOTLIGHT` Action for opening a spotlight tab or window modal using the content passed to the dialog. diff --git a/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser.ini b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser.ini index 8ffe0fc0cde8..b6118daab74d 100644 --- a/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser.ini +++ b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser.ini @@ -27,6 +27,7 @@ skip-if = os != "win" [browser_sma_cfrmessageprovider.js] [browser_sma_configure_homepage.js] [browser_sma_default_browser.js] +[browser_sma_default_pdf_handler.js] [browser_sma_set_prefs.js] [browser_sma_click_element.js] [browser_sma_handle_multiaction.js] diff --git a/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser_sma_default_pdf_handler.js b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser_sma_default_pdf_handler.js new file mode 100644 index 000000000000..adb77b93e2e2 --- /dev/null +++ b/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/browser_sma_default_pdf_handler.js @@ -0,0 +1,92 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_set_default_pdf_handler_no_data() { + const sandbox = sinon.createSandbox(); + const stub = sandbox.stub(); + + await SMATestUtils.executeAndValidateAction( + { type: "SET_DEFAULT_PDF_HANDLER" }, + { + ownerGlobal: { + getShellService: () => ({ + setAsDefaultPDFHandler: stub, + }), + }, + } + ); + + Assert.equal( + stub.callCount, + 1, + "setAsDefaultPDFHandler was called by the action" + ); + Assert.ok( + stub.calledWithExactly(false), + "setAsDefaultPDFHandler called with onlyIfKnownBrowser = false" + ); +}); + +add_task(async function test_set_default_pdf_handler_data_false() { + const sandbox = sinon.createSandbox(); + const stub = sandbox.stub(); + + await SMATestUtils.executeAndValidateAction( + { + type: "SET_DEFAULT_PDF_HANDLER", + data: { + onlyIfKnownBrowser: false, + }, + }, + { + ownerGlobal: { + getShellService: () => ({ + setAsDefaultPDFHandler: stub, + }), + }, + } + ); + + Assert.equal( + stub.callCount, + 1, + "setAsDefaultPDFHandler was called by the action" + ); + Assert.ok( + stub.calledWithExactly(false), + "setAsDefaultPDFHandler called with onlyIfKnownBrowser = false" + ); +}); + +add_task(async function test_set_default_pdf_handler_data_true() { + const sandbox = sinon.createSandbox(); + const stub = sandbox.stub(); + + await SMATestUtils.executeAndValidateAction( + { + type: "SET_DEFAULT_PDF_HANDLER", + data: { + onlyIfKnownBrowser: true, + }, + }, + { + ownerGlobal: { + getShellService: () => ({ + setAsDefaultPDFHandler: stub, + }), + }, + } + ); + + Assert.equal( + stub.callCount, + 1, + "setAsDefaultPDFHandler was called by the action" + ); + Assert.ok( + stub.calledWithExactly(true), + "setAsDefaultPDFHandler called with onlyIfKnownBrowser = true" + ); +});