addon-sdk/source/lib/sdk/notifications.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "stable"
michael@0 9 };
michael@0 10
michael@0 11 const { Cc, Ci, Cr } = require("chrome");
michael@0 12 const apiUtils = require("./deprecated/api-utils");
michael@0 13 const errors = require("./deprecated/errors");
michael@0 14 const { isString, isUndefined, instanceOf } = require('./lang/type');
michael@0 15 const { URL } = require('./url');
michael@0 16
michael@0 17 const NOTIFICATION_DIRECTIONS = ["auto", "ltr", "rtl"];
michael@0 18
michael@0 19 try {
michael@0 20 let alertServ = Cc["@mozilla.org/alerts-service;1"].
michael@0 21 getService(Ci.nsIAlertsService);
michael@0 22
michael@0 23 // The unit test sets this to a mock notification function.
michael@0 24 var notify = alertServ.showAlertNotification.bind(alertServ);
michael@0 25 }
michael@0 26 catch (err) {
michael@0 27 // An exception will be thrown if the platform doesn't provide an alert
michael@0 28 // service, e.g., if Growl is not installed on OS X. In that case, use a
michael@0 29 // mock notification function that just logs to the console.
michael@0 30 notify = notifyUsingConsole;
michael@0 31 }
michael@0 32
michael@0 33 exports.notify = function notifications_notify(options) {
michael@0 34 let valOpts = validateOptions(options);
michael@0 35 let clickObserver = !valOpts.onClick ? null : {
michael@0 36 observe: function notificationClickObserved(subject, topic, data) {
michael@0 37 if (topic === "alertclickcallback")
michael@0 38 errors.catchAndLog(valOpts.onClick).call(exports, valOpts.data);
michael@0 39 }
michael@0 40 };
michael@0 41 function notifyWithOpts(notifyFn) {
michael@0 42 notifyFn(valOpts.iconURL, valOpts.title, valOpts.text, !!clickObserver,
michael@0 43 valOpts.data, clickObserver, valOpts.tag, valOpts.dir, valOpts.lang);
michael@0 44 }
michael@0 45 try {
michael@0 46 notifyWithOpts(notify);
michael@0 47 }
michael@0 48 catch (err) {
michael@0 49 if (err instanceof Ci.nsIException && err.result == Cr.NS_ERROR_FILE_NOT_FOUND) {
michael@0 50 console.warn("The notification icon named by " + valOpts.iconURL +
michael@0 51 " does not exist. A default icon will be used instead.");
michael@0 52 delete valOpts.iconURL;
michael@0 53 notifyWithOpts(notify);
michael@0 54 }
michael@0 55 else {
michael@0 56 notifyWithOpts(notifyUsingConsole);
michael@0 57 }
michael@0 58 }
michael@0 59 };
michael@0 60
michael@0 61 function notifyUsingConsole(iconURL, title, text) {
michael@0 62 title = title ? "[" + title + "]" : "";
michael@0 63 text = text || "";
michael@0 64 let str = [title, text].filter(function (s) s).join(" ");
michael@0 65 console.log(str);
michael@0 66 }
michael@0 67
michael@0 68 function validateOptions(options) {
michael@0 69 return apiUtils.validateOptions(options, {
michael@0 70 data: {
michael@0 71 is: ["string", "undefined"]
michael@0 72 },
michael@0 73 iconURL: {
michael@0 74 is: ["string", "undefined", "object"],
michael@0 75 ok: function(value) {
michael@0 76 return isUndefined(value) || isString(value) || (value instanceof URL);
michael@0 77 },
michael@0 78 msg: "`iconURL` must be a string or an URL instance."
michael@0 79 },
michael@0 80 onClick: {
michael@0 81 is: ["function", "undefined"]
michael@0 82 },
michael@0 83 text: {
michael@0 84 is: ["string", "undefined", "number"]
michael@0 85 },
michael@0 86 title: {
michael@0 87 is: ["string", "undefined", "number"]
michael@0 88 },
michael@0 89 tag: {
michael@0 90 is: ["string", "undefined", "number"]
michael@0 91 },
michael@0 92 dir: {
michael@0 93 is: ["string", "undefined"],
michael@0 94 ok: function(value) {
michael@0 95 return isUndefined(value) || ~NOTIFICATION_DIRECTIONS.indexOf(value);
michael@0 96 },
michael@0 97 msg: '`dir` option must be one of: "auto", "ltr" or "rtl".'
michael@0 98 },
michael@0 99 lang: {
michael@0 100 is: ["string", "undefined"]
michael@0 101 }
michael@0 102 });
michael@0 103 }

mercurial