diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /mobile/android/components/geckoview/ShareDelegate.jsm | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | mobile/android/components/geckoview/ShareDelegate.jsm | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/mobile/android/components/geckoview/ShareDelegate.jsm b/mobile/android/components/geckoview/ShareDelegate.jsm new file mode 100644 index 0000000000..d10a8fdd5a --- /dev/null +++ b/mobile/android/components/geckoview/ShareDelegate.jsm @@ -0,0 +1,85 @@ +/* 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"; + +var EXPORTED_SYMBOLS = ["ShareDelegate"]; + +const { GeckoViewUtils } = ChromeUtils.import( + "resource://gre/modules/GeckoViewUtils.jsm" +); + +const { XPCOMUtils } = ChromeUtils.import( + "resource://gre/modules/XPCOMUtils.jsm" +); + +XPCOMUtils.defineLazyModuleGetters(this, { + GeckoViewPrompter: "resource://gre/modules/GeckoViewPrompter.jsm", + Services: "resource://gre/modules/Services.jsm", +}); + +const domBundle = Services.strings.createBundle( + "chrome://global/locale/dom/dom.properties" +); + +const { debug, warn } = GeckoViewUtils.initLogging("ShareDelegate"); + +class ShareDelegate { + init(aParent) { + this._openerWindow = aParent; + } + + get openerWindow() { + return this._openerWindow; + } + + async share(aTitle, aText, aUri) { + const ABORT = 2; + const FAILURE = 1; + const SUCCESS = 0; + + const msg = { + type: "share", + title: aTitle, + text: aText, + uri: aUri ? aUri.displaySpec : null, + }; + const prompt = new GeckoViewPrompter(this._openerWindow); + const result = await new Promise(resolve => { + prompt.asyncShowPrompt(msg, resolve); + }); + + if (!result) { + // A null result is treated as a dismissal in GeckoViewPrompter. + throw new DOMException( + domBundle.GetStringFromName("WebShareAPI_Aborted"), + "AbortError" + ); + } + + const res = result && result.response; + switch (res) { + case FAILURE: + throw new DOMException( + domBundle.GetStringFromName("WebShareAPI_Failed"), + "DataError" + ); + case ABORT: // Handle aborted attempt and invalid responses the same. + throw new DOMException( + domBundle.GetStringFromName("WebShareAPI_Aborted"), + "AbortError" + ); + case SUCCESS: + return; + default: + throw new DOMException("Unknown error.", "UnknownError"); + } + } +} + +ShareDelegate.prototype.classID = Components.ID( + "{1201d357-8417-4926-a694-e6408fbedcf8}" +); +ShareDelegate.prototype.QueryInterface = ChromeUtils.generateQI([ + "nsISharePicker", +]); |