2012-03-17 02:23:28 +04:00
|
|
|
/* 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/. */
|
2012-11-29 10:36:15 +04:00
|
|
|
|
2012-03-17 02:23:28 +04:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2012-11-29 10:36:15 +04:00
|
|
|
const Cc = Components.classes;
|
2012-03-17 02:23:28 +04:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2012-12-03 18:42:36 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
|
|
|
|
"@mozilla.org/system-message-internal;1",
|
|
|
|
"nsISystemMessagesInternal");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "uuidGenerator",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator");
|
|
|
|
|
2014-03-18 20:01:13 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "notificationStorage",
|
|
|
|
"@mozilla.org/notificationStorage;1",
|
|
|
|
"nsINotificationStorage");
|
|
|
|
|
2012-11-29 10:36:15 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
|
|
|
|
return Cc["@mozilla.org/childprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageSender);
|
|
|
|
});
|
|
|
|
|
2012-12-03 18:42:36 +04:00
|
|
|
function debug(str) {
|
|
|
|
dump("=*= AlertsService.js : " + str + "\n");
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:23:28 +04:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// Alerts Service
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2014-05-01 13:23:00 +04:00
|
|
|
const kNotificationSystemMessageName = "notification";
|
|
|
|
|
|
|
|
const kMessageAlertNotificationSend = "alert-notification-send";
|
|
|
|
const kMessageAlertNotificationClose = "alert-notification-close";
|
|
|
|
|
2014-08-06 11:45:00 +04:00
|
|
|
const kTopicAlertShow = "alertshow";
|
2014-05-01 13:23:00 +04:00
|
|
|
const kTopicAlertFinished = "alertfinished";
|
2014-05-09 01:16:00 +04:00
|
|
|
const kTopicAlertClickCallback = "alertclickcallback";
|
2014-05-01 13:23:00 +04:00
|
|
|
|
2012-11-29 10:36:15 +04:00
|
|
|
function AlertsService() {
|
2014-05-01 13:23:00 +04:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2012-11-29 10:36:15 +04:00
|
|
|
}
|
2012-03-17 02:23:28 +04:00
|
|
|
|
|
|
|
AlertsService.prototype = {
|
2012-07-27 02:25:02 +04:00
|
|
|
classID: Components.ID("{fe33c107-82a4-41d6-8c64-5353267e04c9}"),
|
2012-11-29 10:36:15 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAlertsService,
|
2014-05-01 13:23:00 +04:00
|
|
|
Ci.nsIObserver]),
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
|
|
|
case "xpcom-shutdown":
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2012-03-17 02:23:28 +04:00
|
|
|
|
2012-11-29 10:36:15 +04:00
|
|
|
// nsIAlertsService
|
2015-11-24 02:01:57 +03:00
|
|
|
showAlert: function(aAlert, aAlertListener) {
|
|
|
|
if (!aAlert) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cpmm.sendAsyncMessage(kMessageAlertNotificationSend, {
|
|
|
|
imageURL: aAlert.imageURL,
|
|
|
|
title: aAlert.title,
|
|
|
|
text: aAlert.text,
|
|
|
|
clickable: aAlert.textClickable,
|
|
|
|
cookie: aAlert.cookie,
|
|
|
|
listener: aAlertListener,
|
|
|
|
id: aAlert.name,
|
|
|
|
dir: aAlert.dir,
|
|
|
|
lang: aAlert.lang,
|
|
|
|
dataStr: aAlert.data,
|
|
|
|
inPrivateBrowsing: aAlert.inPrivateBrowsing
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-05-01 13:23:00 +04:00
|
|
|
showAlertNotification: function(aImageUrl, aTitle, aText, aTextClickable,
|
|
|
|
aCookie, aAlertListener, aName, aBidi,
|
2014-11-28 22:08:29 +03:00
|
|
|
aLang, aDataStr, aPrincipal,
|
|
|
|
aInPrivateBrowsing) {
|
2015-11-24 02:01:57 +03:00
|
|
|
let alert = Cc["@mozilla.org/alert-notification;1"].
|
|
|
|
createInstance(Ci.nsIAlertNotification);
|
|
|
|
|
|
|
|
alert.init(aName, aImageUrl, aTitle, aText, aTextClickable, aCookie,
|
|
|
|
aBidi, aLang, aDataStr, aPrincipal, aInPrivateBrowsing);
|
|
|
|
|
|
|
|
this.showAlert(alert, aAlertListener);
|
2012-07-12 19:45:38 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
closeAlert: function(aName) {
|
2014-05-01 13:23:00 +04:00
|
|
|
cpmm.sendAsyncMessage(kMessageAlertNotificationClose, {
|
|
|
|
name: aName
|
|
|
|
});
|
2012-11-29 10:36:15 +04:00
|
|
|
},
|
|
|
|
|
2012-12-03 18:42:36 +04:00
|
|
|
// AlertsService.js custom implementation
|
|
|
|
_listeners: [],
|
|
|
|
|
2014-05-01 13:23:00 +04:00
|
|
|
receiveMessage: function(aMessage) {
|
2012-12-03 18:42:36 +04:00
|
|
|
let data = aMessage.data;
|
|
|
|
let listener = this._listeners[data.uid];
|
2016-10-18 07:23:44 +03:00
|
|
|
if (!listener) {
|
2012-12-03 18:42:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let topic = data.topic;
|
|
|
|
|
|
|
|
try {
|
|
|
|
listener.observer.observe(null, topic, null);
|
|
|
|
} catch (e) {
|
2014-09-12 18:50:00 +04:00
|
|
|
if (topic === kTopicAlertFinished && listener.dbId) {
|
|
|
|
notificationStorage.delete(listener.manifestURL, listener.dbId);
|
|
|
|
}
|
2012-12-03 18:42:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// we're done with this notification
|
2014-05-01 13:23:00 +04:00
|
|
|
if (topic === kTopicAlertFinished) {
|
2012-12-03 18:42:36 +04:00
|
|
|
delete this._listeners[data.uid];
|
|
|
|
}
|
2014-08-21 04:56:12 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
deserializeStructuredClone: function(dataString) {
|
|
|
|
if (!dataString) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let scContainer = Cc["@mozilla.org/docshell/structured-clone-container;1"].
|
|
|
|
createInstance(Ci.nsIStructuredCloneContainer);
|
|
|
|
|
|
|
|
// The maximum supported structured-clone serialization format version
|
|
|
|
// as defined in "js/public/StructuredClone.h"
|
|
|
|
let JS_STRUCTURED_CLONE_VERSION = 4;
|
|
|
|
scContainer.initFromBase64(dataString, JS_STRUCTURED_CLONE_VERSION);
|
|
|
|
let dataObj = scContainer.deserializeToVariant();
|
|
|
|
|
|
|
|
// We have to check whether dataObj contains DOM objects (supported by
|
|
|
|
// nsIStructuredCloneContainer, but not by Cu.cloneInto), e.g. ImageData.
|
|
|
|
// After the structured clone callback systems will be unified, we'll not
|
|
|
|
// have to perform this check anymore.
|
|
|
|
try {
|
|
|
|
let data = Cu.cloneInto(dataObj, {});
|
|
|
|
} catch(e) { dataObj = null; }
|
|
|
|
|
|
|
|
return dataObj;
|
2012-03-17 02:23:28 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AlertsService]);
|