2013-03-28 20:49:41 -07: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-04-10 20:19:28 -07:00
|
|
|
// Don't modify this, instead set dom.push.debug.
|
2015-08-11 12:53:14 -07:00
|
|
|
let gDebuggingEnabled = false;
|
2013-09-16 16:55:26 -07:00
|
|
|
|
2013-03-28 20:49:41 -07:00
|
|
|
function debug(s) {
|
2013-09-16 16:55:26 -07:00
|
|
|
if (gDebuggingEnabled)
|
|
|
|
dump("-*- Push.js: " + s + "\n");
|
2013-03-28 20:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2015-07-23 08:30:15 -07:00
|
|
|
const Cr = Components.results;
|
2013-03-28 20:49:41 -07:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
2015-04-10 20:19:28 -07:00
|
|
|
|
2013-08-02 17:18:35 -07:00
|
|
|
const PUSH_CID = Components.ID("{cde1d019-fad8-4044-b141-65fb4fb7a245}");
|
2013-03-28 20:49:41 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Push component runs in the child process and exposes the SimplePush API
|
|
|
|
* to the web application. The PushService running in the parent process is the
|
|
|
|
* one actually performing all operations.
|
|
|
|
*/
|
2013-08-02 17:18:35 -07:00
|
|
|
function Push() {
|
2013-03-28 20:49:41 -07:00
|
|
|
debug("Push Constructor");
|
|
|
|
}
|
|
|
|
|
|
|
|
Push.prototype = {
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2013-08-02 17:18:35 -07:00
|
|
|
contractID: "@mozilla.org/push/PushManager;1",
|
|
|
|
|
2013-03-28 20:49:41 -07:00
|
|
|
classID : PUSH_CID,
|
|
|
|
|
2013-07-08 14:55:42 -07:00
|
|
|
QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
|
2013-11-19 21:33:10 -08:00
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsIObserver]),
|
2013-03-28 20:49:41 -07:00
|
|
|
|
|
|
|
init: function(aWindow) {
|
2013-09-16 16:55:26 -07:00
|
|
|
// Set debug first so that all debugging actually works.
|
|
|
|
// NOTE: We don't add an observer here like in PushService. Flipping the
|
|
|
|
// pref will require a reload of the app/page, which seems acceptable.
|
2015-04-10 20:19:28 -07:00
|
|
|
gDebuggingEnabled = Services.prefs.getBoolPref("dom.push.debug");
|
2013-03-28 20:49:41 -07:00
|
|
|
debug("init()");
|
|
|
|
|
2015-04-10 20:19:28 -07:00
|
|
|
this._window = aWindow;
|
2013-03-28 20:49:41 -07:00
|
|
|
|
2015-07-23 08:30:15 -07:00
|
|
|
this.initDOMRequestHelper(aWindow);
|
2013-03-28 20:49:41 -07:00
|
|
|
|
2015-06-24 13:34:54 -07:00
|
|
|
this._principal = aWindow.document.nodePrincipal;
|
2015-07-23 08:30:15 -07:00
|
|
|
|
|
|
|
this._client = Cc["@mozilla.org/push/PushClient;1"].createInstance(Ci.nsIPushClient);
|
2013-03-28 20:49:41 -07:00
|
|
|
},
|
|
|
|
|
2015-04-10 20:19:28 -07:00
|
|
|
setScope: function(scope){
|
|
|
|
debug('setScope ' + scope);
|
|
|
|
this._scope = scope;
|
|
|
|
},
|
|
|
|
|
|
|
|
askPermission: function (aAllowCallback, aCancelCallback) {
|
|
|
|
debug("askPermission");
|
|
|
|
|
|
|
|
let principal = this._window.document.nodePrincipal;
|
|
|
|
let type = "push";
|
|
|
|
let permValue =
|
|
|
|
Services.perms.testExactPermissionFromPrincipal(principal, type);
|
|
|
|
|
|
|
|
if (permValue == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
aAllowCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permValue == Ci.nsIPermissionManager.DENY_ACTION) {
|
|
|
|
aCancelCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an array with a single nsIContentPermissionType element.
|
|
|
|
type = {
|
|
|
|
type: "push",
|
|
|
|
access: null,
|
|
|
|
options: [],
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionType])
|
|
|
|
};
|
|
|
|
let typeArray = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
|
|
|
|
typeArray.appendElement(type, false);
|
|
|
|
|
|
|
|
// create a nsIContentPermissionRequest
|
|
|
|
let request = {
|
|
|
|
types: typeArray,
|
|
|
|
principal: principal,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionRequest]),
|
|
|
|
allow: function() {
|
|
|
|
aAllowCallback();
|
|
|
|
},
|
|
|
|
cancel: function() {
|
|
|
|
aCancelCallback();
|
|
|
|
},
|
|
|
|
window: this._window
|
|
|
|
};
|
|
|
|
|
|
|
|
// Using askPermission from nsIDOMWindowUtils that takes care of the
|
|
|
|
// remoting if needed.
|
|
|
|
let windowUtils = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
windowUtils.askPermission(request);
|
|
|
|
},
|
|
|
|
|
2015-07-23 08:30:15 -07:00
|
|
|
getEndpointResponse: function(fn) {
|
|
|
|
debug("GetEndpointResponse " + fn.toSource());
|
|
|
|
let that = this;
|
2015-04-10 20:19:28 -07:00
|
|
|
let p = this.createPromise(function(resolve, reject) {
|
|
|
|
this.askPermission(
|
2015-07-23 08:30:15 -07:00
|
|
|
() => {
|
|
|
|
fn(that._scope, that._principal, {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPushEndpointCallback]),
|
|
|
|
onPushEndpoint: function(ok, endpoint) {
|
|
|
|
if (ok === Cr.NS_OK) {
|
|
|
|
if (endpoint) {
|
|
|
|
let sub = new that._window.PushSubscription(endpoint, that._scope);
|
|
|
|
sub.setPrincipal(that._principal);
|
|
|
|
resolve(sub);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject("AbortError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
() => {
|
2015-05-01 11:01:17 -07:00
|
|
|
reject("PermissionDeniedError");
|
2015-04-10 20:19:28 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}.bind(this));
|
|
|
|
return p;
|
2013-03-28 20:49:41 -07:00
|
|
|
},
|
|
|
|
|
2015-07-23 08:30:15 -07:00
|
|
|
subscribe: function() {
|
|
|
|
debug("subscribe()");
|
|
|
|
return this.getEndpointResponse(this._client.subscribe.bind(this._client));
|
|
|
|
},
|
|
|
|
|
2015-04-10 20:19:28 -07:00
|
|
|
getSubscription: function() {
|
|
|
|
debug("getSubscription()" + this._scope);
|
2015-07-23 08:30:15 -07:00
|
|
|
return this.getEndpointResponse(this._client.getSubscription.bind(this._client));
|
2013-03-28 20:49:41 -07:00
|
|
|
},
|
|
|
|
|
2015-07-14 14:27:32 -07:00
|
|
|
permissionState: function() {
|
|
|
|
debug("permissionState()" + this._scope);
|
|
|
|
|
|
|
|
let p = this.createPromise((resolve, reject) => {
|
|
|
|
let permission = Ci.nsIPermissionManager.DENY_ACTION;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let permissionManager = Cc["@mozilla.org/permissionmanager;1"]
|
|
|
|
.getService(Ci.nsIPermissionManager);
|
|
|
|
permission =
|
|
|
|
permissionManager.testExactPermissionFromPrincipal(this._principal,
|
|
|
|
"push");
|
|
|
|
} catch(e) {
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
2015-04-10 20:19:28 -07:00
|
|
|
|
2015-07-14 14:27:32 -07:00
|
|
|
let pushPermissionStatus = "prompt";
|
2015-04-10 20:19:28 -07:00
|
|
|
if (permission == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
pushPermissionStatus = "granted";
|
|
|
|
} else if (permission == Ci.nsIPermissionManager.DENY_ACTION) {
|
|
|
|
pushPermissionStatus = "denied";
|
|
|
|
}
|
|
|
|
resolve(pushPermissionStatus);
|
2015-07-14 14:27:32 -07:00
|
|
|
});
|
2015-04-10 20:19:28 -07:00
|
|
|
return p;
|
|
|
|
},
|
2013-03-28 20:49:41 -07:00
|
|
|
}
|
|
|
|
|
2015-07-23 08:30:15 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Push]);
|