Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | this.EXPORTED_SYMBOLS = ["Notifications", "Notification", "NotificationButton"]; |
michael@0 | 6 | |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cr = Components.results; |
michael@0 | 10 | const Cu = Components.utils; |
michael@0 | 11 | |
michael@0 | 12 | Cu.import("resource://services-common/observers.js"); |
michael@0 | 13 | Cu.import("resource://gre/modules/Log.jsm"); |
michael@0 | 14 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 15 | |
michael@0 | 16 | this.Notifications = { |
michael@0 | 17 | // Match the referenced values in toolkit/content/widgets/notification.xml. |
michael@0 | 18 | get PRIORITY_INFO() 1, // PRIORITY_INFO_LOW |
michael@0 | 19 | get PRIORITY_WARNING() 4, // PRIORITY_WARNING_LOW |
michael@0 | 20 | get PRIORITY_ERROR() 7, // PRIORITY_CRITICAL_LOW |
michael@0 | 21 | |
michael@0 | 22 | // FIXME: instead of making this public, dress the Notifications object |
michael@0 | 23 | // to behave like an iterator (using generators?) and have callers access |
michael@0 | 24 | // this array through the Notifications object. |
michael@0 | 25 | notifications: [], |
michael@0 | 26 | |
michael@0 | 27 | _observers: [], |
michael@0 | 28 | |
michael@0 | 29 | // XXX Should we have a helper method for adding a simple notification? |
michael@0 | 30 | // I.e. something like |function notify(title, description, priority)|. |
michael@0 | 31 | |
michael@0 | 32 | add: function Notifications_add(notification) { |
michael@0 | 33 | this.notifications.push(notification); |
michael@0 | 34 | Observers.notify("weave:notification:added", notification, null); |
michael@0 | 35 | }, |
michael@0 | 36 | |
michael@0 | 37 | remove: function Notifications_remove(notification) { |
michael@0 | 38 | let index = this.notifications.indexOf(notification); |
michael@0 | 39 | |
michael@0 | 40 | if (index != -1) { |
michael@0 | 41 | this.notifications.splice(index, 1); |
michael@0 | 42 | Observers.notify("weave:notification:removed", notification, null); |
michael@0 | 43 | } |
michael@0 | 44 | }, |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * Replace an existing notification. |
michael@0 | 48 | */ |
michael@0 | 49 | replace: function Notifications_replace(oldNotification, newNotification) { |
michael@0 | 50 | let index = this.notifications.indexOf(oldNotification); |
michael@0 | 51 | |
michael@0 | 52 | if (index != -1) |
michael@0 | 53 | this.notifications.splice(index, 1, newNotification); |
michael@0 | 54 | else { |
michael@0 | 55 | this.notifications.push(newNotification); |
michael@0 | 56 | // XXX Should we throw because we didn't find the existing notification? |
michael@0 | 57 | // XXX Should we notify observers about weave:notification:added? |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | // XXX Should we notify observers about weave:notification:replaced? |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Remove all notifications that match a title. If no title is provided, all |
michael@0 | 65 | * notifications are removed. |
michael@0 | 66 | * |
michael@0 | 67 | * @param title [optional] |
michael@0 | 68 | * Title of notifications to remove; falsy value means remove all |
michael@0 | 69 | */ |
michael@0 | 70 | removeAll: function Notifications_removeAll(title) { |
michael@0 | 71 | this.notifications.filter(function(old) old.title == title || !title). |
michael@0 | 72 | forEach(function(old) this.remove(old), this); |
michael@0 | 73 | }, |
michael@0 | 74 | |
michael@0 | 75 | // replaces all existing notifications with the same title as the new one |
michael@0 | 76 | replaceTitle: function Notifications_replaceTitle(notification) { |
michael@0 | 77 | this.removeAll(notification.title); |
michael@0 | 78 | this.add(notification); |
michael@0 | 79 | } |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * A basic notification. Subclass this to create more complex notifications. |
michael@0 | 85 | */ |
michael@0 | 86 | this.Notification = |
michael@0 | 87 | function Notification(title, description, iconURL, priority, buttons) { |
michael@0 | 88 | this.title = title; |
michael@0 | 89 | this.description = description; |
michael@0 | 90 | |
michael@0 | 91 | if (iconURL) |
michael@0 | 92 | this.iconURL = iconURL; |
michael@0 | 93 | |
michael@0 | 94 | if (priority) |
michael@0 | 95 | this.priority = priority; |
michael@0 | 96 | |
michael@0 | 97 | if (buttons) |
michael@0 | 98 | this.buttons = buttons; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | // We set each prototype property individually instead of redefining |
michael@0 | 102 | // the entire prototype to avoid blowing away existing properties |
michael@0 | 103 | // of the prototype like the the "constructor" property, which we use |
michael@0 | 104 | // to bind notification objects to their XBL representations. |
michael@0 | 105 | Notification.prototype.priority = Notifications.PRIORITY_INFO; |
michael@0 | 106 | Notification.prototype.iconURL = null; |
michael@0 | 107 | Notification.prototype.buttons = []; |
michael@0 | 108 | |
michael@0 | 109 | /** |
michael@0 | 110 | * A button to display in a notification. |
michael@0 | 111 | */ |
michael@0 | 112 | this.NotificationButton = |
michael@0 | 113 | function NotificationButton(label, accessKey, callback) { |
michael@0 | 114 | function callbackWrapper() { |
michael@0 | 115 | try { |
michael@0 | 116 | callback.apply(this, arguments); |
michael@0 | 117 | } catch (e) { |
michael@0 | 118 | let logger = Log.repository.getLogger("Sync.Notifications"); |
michael@0 | 119 | logger.error("An exception occurred: " + Utils.exceptionStr(e)); |
michael@0 | 120 | logger.info(Utils.stackTrace(e)); |
michael@0 | 121 | throw e; |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | this.label = label; |
michael@0 | 126 | this.accessKey = accessKey; |
michael@0 | 127 | this.callback = callbackWrapper; |
michael@0 | 128 | } |