summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/lib/sdk/notifications.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /addon-sdk/source/lib/sdk/notifications.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'addon-sdk/source/lib/sdk/notifications.js')
-rw-r--r--addon-sdk/source/lib/sdk/notifications.js112
1 files changed, 112 insertions, 0 deletions
diff --git a/addon-sdk/source/lib/sdk/notifications.js b/addon-sdk/source/lib/sdk/notifications.js
new file mode 100644
index 000000000..752e08fb1
--- /dev/null
+++ b/addon-sdk/source/lib/sdk/notifications.js
@@ -0,0 +1,112 @@
+/* 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";
+
+module.metadata = {
+ "stability": "stable"
+};
+
+const { Cc, Ci, Cr } = require("chrome");
+const apiUtils = require("./deprecated/api-utils");
+const { isString, isUndefined, instanceOf } = require('./lang/type');
+const { URL, isLocalURL } = require('./url');
+const { data } = require('./self');
+
+const NOTIFICATION_DIRECTIONS = ["auto", "ltr", "rtl"];
+
+try {
+ let alertServ = Cc["@mozilla.org/alerts-service;1"].
+ getService(Ci.nsIAlertsService);
+
+ // The unit test sets this to a mock notification function.
+ var notify = alertServ.showAlertNotification.bind(alertServ);
+}
+catch (err) {
+ // An exception will be thrown if the platform doesn't provide an alert
+ // service, e.g., if Growl is not installed on OS X. In that case, use a
+ // mock notification function that just logs to the console.
+ notify = notifyUsingConsole;
+}
+
+exports.notify = function notifications_notify(options) {
+ let valOpts = validateOptions(options);
+ let clickObserver = !valOpts.onClick ? null : {
+ observe: (subject, topic, data) => {
+ if (topic === "alertclickcallback") {
+ try {
+ valOpts.onClick.call(exports, valOpts.data);
+ }
+ catch(e) {
+ console.exception(e);
+ }
+ }
+ }
+ };
+ function notifyWithOpts(notifyFn) {
+ let { iconURL } = valOpts;
+ iconURL = iconURL && isLocalURL(iconURL) ? data.url(iconURL) : iconURL;
+
+ notifyFn(iconURL, valOpts.title, valOpts.text, !!clickObserver,
+ valOpts.data, clickObserver, valOpts.tag, valOpts.dir, valOpts.lang);
+ }
+ try {
+ notifyWithOpts(notify);
+ }
+ catch (err) {
+ if (err instanceof Ci.nsIException && err.result == Cr.NS_ERROR_FILE_NOT_FOUND) {
+ console.warn("The notification icon named by " + iconURL +
+ " does not exist. A default icon will be used instead.");
+ delete valOpts.iconURL;
+ notifyWithOpts(notify);
+ }
+ else {
+ notifyWithOpts(notifyUsingConsole);
+ }
+ }
+};
+
+function notifyUsingConsole(iconURL, title, text) {
+ title = title ? "[" + title + "]" : "";
+ text = text || "";
+ let str = [title, text].filter(s => s).join(" ");
+ console.log(str);
+}
+
+function validateOptions(options) {
+ return apiUtils.validateOptions(options, {
+ data: {
+ is: ["string", "undefined"]
+ },
+ iconURL: {
+ is: ["string", "undefined", "object"],
+ ok: function(value) {
+ return isUndefined(value) || isString(value) || (value instanceof URL);
+ },
+ msg: "`iconURL` must be a string or an URL instance."
+ },
+ onClick: {
+ is: ["function", "undefined"]
+ },
+ text: {
+ is: ["string", "undefined", "number"]
+ },
+ title: {
+ is: ["string", "undefined", "number"]
+ },
+ tag: {
+ is: ["string", "undefined", "number"]
+ },
+ dir: {
+ is: ["string", "undefined"],
+ ok: function(value) {
+ return isUndefined(value) || ~NOTIFICATION_DIRECTIONS.indexOf(value);
+ },
+ msg: '`dir` option must be one of: "auto", "ltr" or "rtl".'
+ },
+ lang: {
+ is: ["string", "undefined"]
+ }
+ });
+}