2015-06-03 15:06:00 +03:00
|
|
|
/* jshint moz: true, esnext: true */
|
|
|
|
/* 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";
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
const {PushDB} = Cu.import("resource://gre/modules/PushDB.jsm");
|
2015-06-26 00:52:57 +03:00
|
|
|
const {PushRecord} = Cu.import("resource://gre/modules/PushRecord.jsm");
|
2015-06-03 15:06:00 +03:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2016-01-20 07:00:18 +03:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
2015-06-03 15:06:00 +03:00
|
|
|
Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Timer.jsm");
|
|
|
|
|
2015-09-17 15:08:50 +03:00
|
|
|
const {
|
|
|
|
PushCrypto,
|
|
|
|
concatArray,
|
|
|
|
} = Cu.import("resource://gre/modules/PushCrypto.jsm");
|
2015-09-11 17:51:32 +03:00
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["PushServiceHttp2"];
|
|
|
|
|
2015-11-10 00:58:32 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "console", () => {
|
|
|
|
let {ConsoleAPI} = Cu.import("resource://gre/modules/Console.jsm", {});
|
|
|
|
return new ConsoleAPI({
|
|
|
|
maxLogLevelPref: "dom.push.loglevel",
|
|
|
|
prefix: "PushServiceHttp2",
|
|
|
|
});
|
|
|
|
});
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2017-07-29 00:13:57 +03:00
|
|
|
const prefs = Services.prefs.getBranch("dom.push.");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
const kPUSHHTTP2DB_DB_NAME = "pushHttp2";
|
2015-09-11 17:51:32 +03:00
|
|
|
const kPUSHHTTP2DB_DB_VERSION = 5; // Change this if the IndexedDB format changes
|
2015-06-03 15:06:00 +03:00
|
|
|
const kPUSHHTTP2DB_STORE_NAME = "pushHttp2";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A proxy between the PushService and connections listening for incoming push
|
|
|
|
* messages. The PushService can silence messages from the connections by
|
|
|
|
* setting PushSubscriptionListener._pushService to null. This is required
|
|
|
|
* because it can happen that there is an outstanding push message that will
|
|
|
|
* be send on OnStopRequest but the PushService may not be interested in these.
|
|
|
|
* It's easier to stop listening than to have checks at specific points.
|
|
|
|
*/
|
|
|
|
var PushSubscriptionListener = function(pushService, uri) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushSubscriptionListener()");
|
2015-06-03 15:06:00 +03:00
|
|
|
this._pushService = pushService;
|
|
|
|
this.uri = uri;
|
|
|
|
};
|
|
|
|
|
|
|
|
PushSubscriptionListener.prototype = {
|
|
|
|
|
|
|
|
QueryInterface: function (aIID) {
|
|
|
|
if (aIID.equals(Ci.nsIHttpPushListener) ||
|
|
|
|
aIID.equals(Ci.nsIStreamListener)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
getInterface: function(aIID) {
|
|
|
|
return this.QueryInterface(aIID);
|
|
|
|
},
|
|
|
|
|
|
|
|
onStartRequest: function(aRequest, aContext) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushSubscriptionListener: onStartRequest()");
|
2015-06-03 15:06:00 +03:00
|
|
|
// We do not do anything here.
|
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function(aRequest, aContext, aStream, aOffset, aCount) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushSubscriptionListener: onDataAvailable()");
|
2015-06-03 15:06:00 +03:00
|
|
|
// Nobody should send data, but just to be sure, otherwise necko will
|
|
|
|
// complain.
|
|
|
|
if (aCount === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let inputStream = Cc["@mozilla.org/scriptableinputstream;1"]
|
|
|
|
.createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
|
|
|
|
inputStream.init(aStream);
|
|
|
|
var data = inputStream.read(aCount);
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function(aRequest, aContext, aStatusCode) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushSubscriptionListener: onStopRequest()");
|
2015-06-03 15:06:00 +03:00
|
|
|
if (!this._pushService) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._pushService.connOnStop(aRequest,
|
|
|
|
Components.isSuccessCode(aStatusCode),
|
|
|
|
this.uri);
|
|
|
|
},
|
|
|
|
|
|
|
|
onPush: function(associatedChannel, pushChannel) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushSubscriptionListener: onPush()");
|
2015-06-03 15:06:00 +03:00
|
|
|
var pushChannelListener = new PushChannelListener(this);
|
2016-01-20 07:00:18 +03:00
|
|
|
pushChannel.asyncOpen2(pushChannelListener);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
disconnect: function() {
|
|
|
|
this._pushService = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The listener for pushed messages. The message data is collected in
|
|
|
|
* OnDataAvailable and send to the app in OnStopRequest.
|
|
|
|
*/
|
|
|
|
var PushChannelListener = function(pushSubscriptionListener) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushChannelListener()");
|
2015-06-03 15:06:00 +03:00
|
|
|
this._mainListener = pushSubscriptionListener;
|
2015-09-11 17:51:32 +03:00
|
|
|
this._message = [];
|
|
|
|
this._ackUri = null;
|
2015-06-03 15:06:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
PushChannelListener.prototype = {
|
|
|
|
|
|
|
|
onStartRequest: function(aRequest, aContext) {
|
|
|
|
this._ackUri = aRequest.URI.spec;
|
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function(aRequest, aContext, aStream, aOffset, aCount) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushChannelListener: onDataAvailable()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
if (aCount === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
let inputStream = Cc["@mozilla.org/binaryinputstream;1"]
|
|
|
|
.createInstance(Ci.nsIBinaryInputStream);
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
inputStream.setInputStream(aStream);
|
|
|
|
let chunk = new ArrayBuffer(aCount);
|
|
|
|
inputStream.readArrayBuffer(aCount, chunk);
|
|
|
|
this._message.push(chunk);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function(aRequest, aContext, aStatusCode) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushChannelListener: onStopRequest()", "status code",
|
|
|
|
aStatusCode);
|
2015-06-03 15:06:00 +03:00
|
|
|
if (Components.isSuccessCode(aStatusCode) &&
|
|
|
|
this._mainListener &&
|
|
|
|
this._mainListener._pushService) {
|
2015-12-04 00:24:47 +03:00
|
|
|
let headers = {
|
|
|
|
encryption_key: getHeaderField(aRequest, "Encryption-Key"),
|
|
|
|
crypto_key: getHeaderField(aRequest, "Crypto-Key"),
|
|
|
|
encryption: getHeaderField(aRequest, "Encryption"),
|
2016-03-18 19:01:50 +03:00
|
|
|
encoding: getHeaderField(aRequest, "Content-Encoding"),
|
2015-12-08 23:26:42 +03:00
|
|
|
};
|
2015-12-04 00:24:47 +03:00
|
|
|
let msg = concatArray(this._message);
|
2015-12-08 23:26:42 +03:00
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
this._mainListener._pushService._pushChannelOnStop(this._mainListener.uri,
|
|
|
|
this._ackUri,
|
2016-10-05 18:56:55 +03:00
|
|
|
headers,
|
|
|
|
msg);
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-04 00:24:47 +03:00
|
|
|
function getHeaderField(aRequest, name) {
|
2015-09-11 17:51:32 +03:00
|
|
|
try {
|
2015-12-04 00:24:47 +03:00
|
|
|
return aRequest.getRequestHeader(name);
|
2015-09-11 17:51:32 +03:00
|
|
|
} catch(e) {
|
|
|
|
// getRequestHeader can throw.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
var PushServiceDelete = function(resolve, reject) {
|
|
|
|
this._resolve = resolve;
|
|
|
|
this._reject = reject;
|
|
|
|
};
|
|
|
|
|
|
|
|
PushServiceDelete.prototype = {
|
|
|
|
|
|
|
|
onStartRequest: function(aRequest, aContext) {},
|
|
|
|
|
|
|
|
onDataAvailable: function(aRequest, aContext, aStream, aOffset, aCount) {
|
|
|
|
// Nobody should send data, but just to be sure, otherwise necko will
|
|
|
|
// complain.
|
|
|
|
if (aCount === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let inputStream = Cc["@mozilla.org/scriptableinputstream;1"]
|
|
|
|
.createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
|
|
|
|
inputStream.init(aStream);
|
|
|
|
var data = inputStream.read(aCount);
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function(aRequest, aContext, aStatusCode) {
|
|
|
|
|
|
|
|
if (Components.isSuccessCode(aStatusCode)) {
|
|
|
|
this._resolve();
|
|
|
|
} else {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Error removing subscription: " + aStatusCode));
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
var SubscriptionListener = function(aSubInfo, aResolve, aReject,
|
|
|
|
aServerURI, aPushServiceHttp2) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("SubscriptionListener()");
|
2015-06-03 15:06:00 +03:00
|
|
|
this._subInfo = aSubInfo;
|
2015-09-11 17:51:32 +03:00
|
|
|
this._resolve = aResolve;
|
|
|
|
this._reject = aReject;
|
2015-06-03 15:06:00 +03:00
|
|
|
this._data = '';
|
|
|
|
this._serverURI = aServerURI;
|
|
|
|
this._service = aPushServiceHttp2;
|
2015-08-06 22:05:47 +03:00
|
|
|
this._ctime = Date.now();
|
2016-03-16 20:03:02 +03:00
|
|
|
this._retryTimeoutID = null;
|
2015-06-03 15:06:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
SubscriptionListener.prototype = {
|
|
|
|
|
|
|
|
onStartRequest: function(aRequest, aContext) {},
|
|
|
|
|
|
|
|
onDataAvailable: function(aRequest, aContext, aStream, aOffset, aCount) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("SubscriptionListener: onDataAvailable()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
// We do not expect any data, but necko will complain if we do not consume
|
|
|
|
// it.
|
|
|
|
if (aCount === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let inputStream = Cc["@mozilla.org/scriptableinputstream;1"]
|
|
|
|
.createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
|
|
|
|
inputStream.init(aStream);
|
|
|
|
this._data.concat(inputStream.read(aCount));
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function(aRequest, aContext, aStatus) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("SubscriptionListener: onStopRequest()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
// Check if pushService is still active.
|
|
|
|
if (!this._service.hasmainPushService()) {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Push service unavailable"));
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Components.isSuccessCode(aStatus)) {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Error listening for messages: " + aStatus));
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var statusCode = aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus;
|
|
|
|
|
|
|
|
if (Math.floor(statusCode / 100) == 5) {
|
2017-07-29 00:13:57 +03:00
|
|
|
if (this._subInfo.retries < prefs.getIntPref("http2.maxRetries")) {
|
2015-06-03 15:06:00 +03:00
|
|
|
this._subInfo.retries++;
|
|
|
|
var retryAfter = retryAfterParser(aRequest);
|
2016-03-16 20:03:02 +03:00
|
|
|
this._retryTimeoutID = setTimeout(_ =>
|
2015-09-11 17:51:32 +03:00
|
|
|
{
|
2016-03-16 20:03:02 +03:00
|
|
|
this._reject(
|
|
|
|
{
|
|
|
|
retry: true,
|
|
|
|
subInfo: this._subInfo
|
|
|
|
});
|
|
|
|
this._service.removeListenerPendingRetry(this);
|
|
|
|
this._retryTimeoutID = null;
|
|
|
|
}, retryAfter);
|
|
|
|
this._service.addListenerPendingRetry(this);
|
2015-06-03 15:06:00 +03:00
|
|
|
} else {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Unexpected server response: " + statusCode));
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (statusCode != 201) {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Unexpected server response: " + statusCode));
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var subscriptionUri;
|
|
|
|
try {
|
|
|
|
subscriptionUri = aRequest.getResponseHeader("location");
|
|
|
|
} catch (err) {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Missing Location header"));
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("onStopRequest: subscriptionUri", subscriptionUri);
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
var linkList;
|
|
|
|
try {
|
|
|
|
linkList = aRequest.getResponseHeader("link");
|
|
|
|
} catch (err) {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Missing Link header"));
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-10 00:58:50 +03:00
|
|
|
var linkParserResult;
|
|
|
|
try {
|
|
|
|
linkParserResult = linkParser(linkList, this._serverURI);
|
|
|
|
} catch (e) {
|
|
|
|
this._reject(e);
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!subscriptionUri) {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Invalid Location header"));
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2017-01-09 22:27:26 +03:00
|
|
|
let uriTry = Services.io.newURI(subscriptionUri);
|
2015-06-03 15:06:00 +03:00
|
|
|
} catch (e) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("onStopRequest: Invalid subscription URI",
|
|
|
|
subscriptionUri);
|
2015-11-10 00:58:50 +03:00
|
|
|
this._reject(new Error("Invalid subscription endpoint: " +
|
|
|
|
subscriptionUri));
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
let reply = new PushRecordHttp2({
|
2015-06-03 15:06:00 +03:00
|
|
|
subscriptionUri: subscriptionUri,
|
|
|
|
pushEndpoint: linkParserResult.pushEndpoint,
|
|
|
|
pushReceiptEndpoint: linkParserResult.pushReceiptEndpoint,
|
|
|
|
scope: this._subInfo.record.scope,
|
2015-06-24 23:34:54 +03:00
|
|
|
originAttributes: this._subInfo.record.originAttributes,
|
2015-12-16 20:21:22 +03:00
|
|
|
systemRecord: this._subInfo.record.systemRecord,
|
2016-03-23 02:29:16 +03:00
|
|
|
appServerKey: this._subInfo.record.appServerKey,
|
2015-08-06 22:05:47 +03:00
|
|
|
ctime: Date.now(),
|
2015-06-26 00:52:57 +03:00
|
|
|
});
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
this._resolve(reply);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
2016-03-16 20:03:02 +03:00
|
|
|
|
|
|
|
abortRetry: function() {
|
|
|
|
if (this._retryTimeoutID != null) {
|
|
|
|
clearTimeout(this._retryTimeoutID);
|
|
|
|
this._retryTimeoutID = null;
|
|
|
|
} else {
|
|
|
|
console.debug("SubscriptionListener.abortRetry: aborting non-existent retry?");
|
|
|
|
}
|
|
|
|
},
|
2015-06-03 15:06:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function retryAfterParser(aRequest) {
|
|
|
|
var retryAfter = 0;
|
|
|
|
try {
|
|
|
|
var retryField = aRequest.getResponseHeader("retry-after");
|
|
|
|
if (isNaN(retryField)) {
|
|
|
|
retryAfter = Date.parse(retryField) - (new Date().getTime());
|
|
|
|
} else {
|
|
|
|
retryAfter = parseInt(retryField, 10) * 1000;
|
|
|
|
}
|
|
|
|
retryAfter = (retryAfter > 0) ? retryAfter : 0;
|
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
return retryAfter;
|
|
|
|
}
|
|
|
|
|
|
|
|
function linkParser(linkHeader, serverURI) {
|
|
|
|
|
|
|
|
var linkList = linkHeader.split(',');
|
|
|
|
if ((linkList.length < 1)) {
|
2015-11-10 00:58:50 +03:00
|
|
|
throw new Error("Invalid Link header");
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var pushEndpoint;
|
|
|
|
var pushReceiptEndpoint;
|
|
|
|
|
|
|
|
linkList.forEach(link => {
|
|
|
|
var linkElems = link.split(';');
|
|
|
|
|
|
|
|
if (linkElems.length == 2) {
|
|
|
|
if (linkElems[1].trim() === 'rel="urn:ietf:params:push"') {
|
|
|
|
pushEndpoint = linkElems[0].substring(linkElems[0].indexOf('<') + 1,
|
|
|
|
linkElems[0].indexOf('>'));
|
|
|
|
|
|
|
|
} else if (linkElems[1].trim() === 'rel="urn:ietf:params:push:receipt"') {
|
|
|
|
pushReceiptEndpoint = linkElems[0].substring(linkElems[0].indexOf('<') + 1,
|
|
|
|
linkElems[0].indexOf('>'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("linkParser: pushEndpoint", pushEndpoint);
|
|
|
|
console.debug("linkParser: pushReceiptEndpoint", pushReceiptEndpoint);
|
2015-06-03 15:06:00 +03:00
|
|
|
// Missing pushReceiptEndpoint is allowed.
|
|
|
|
if (!pushEndpoint) {
|
2015-11-10 00:58:50 +03:00
|
|
|
throw new Error("Missing push endpoint");
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-10 00:58:50 +03:00
|
|
|
var pushURI = Services.io.newURI(pushEndpoint, null, serverURI);
|
|
|
|
var pushReceiptURI;
|
|
|
|
if (pushReceiptEndpoint) {
|
|
|
|
pushReceiptURI = Services.io.newURI(pushReceiptEndpoint, null,
|
|
|
|
serverURI);
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2015-11-10 00:58:50 +03:00
|
|
|
pushEndpoint: pushURI.spec,
|
|
|
|
pushReceiptEndpoint: (pushReceiptURI) ? pushReceiptURI.spec : "",
|
2015-06-03 15:06:00 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The implementation of the WebPush.
|
|
|
|
*/
|
|
|
|
this.PushServiceHttp2 = {
|
|
|
|
_mainPushService: null,
|
|
|
|
_serverURI: null,
|
|
|
|
|
|
|
|
// Keep information about all connections, e.g. the channel, listener...
|
|
|
|
_conns: {},
|
|
|
|
_started: false,
|
|
|
|
|
2016-03-16 20:03:02 +03:00
|
|
|
// Set of SubscriptionListeners that are pending a subscription retry attempt.
|
|
|
|
_listenersPendingRetry: new Set(),
|
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
newPushDB: function() {
|
|
|
|
return new PushDB(kPUSHHTTP2DB_DB_NAME,
|
|
|
|
kPUSHHTTP2DB_DB_VERSION,
|
|
|
|
kPUSHHTTP2DB_STORE_NAME,
|
2015-06-26 00:52:57 +03:00
|
|
|
"subscriptionUri",
|
|
|
|
PushRecordHttp2);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
hasmainPushService: function() {
|
|
|
|
return this._mainPushService !== null;
|
|
|
|
},
|
|
|
|
|
2015-11-10 21:50:46 +03:00
|
|
|
validServerURI: function(serverURI) {
|
2016-04-26 06:53:06 +03:00
|
|
|
if (serverURI.scheme == "http") {
|
2017-07-29 00:13:57 +03:00
|
|
|
return !!prefs.getBoolPref("testing.allowInsecureServerURL", false);
|
2016-04-26 06:53:06 +03:00
|
|
|
}
|
|
|
|
return serverURI.scheme == "https";
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
connect: function(subscriptions) {
|
|
|
|
this.startConnections(subscriptions);
|
|
|
|
},
|
|
|
|
|
2015-10-06 18:14:25 +03:00
|
|
|
isConnected: function() {
|
|
|
|
return this._mainPushService != null;
|
|
|
|
},
|
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
disconnect: function() {
|
|
|
|
this._shutdownConnections(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
_makeChannel: function(aUri) {
|
2016-01-20 07:00:18 +03:00
|
|
|
var chan = NetUtil.newChannel({uri: aUri, loadUsingSystemPrincipal: true})
|
|
|
|
.QueryInterface(Ci.nsIHttpChannel);
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
var loadGroup = Cc["@mozilla.org/network/load-group;1"]
|
|
|
|
.createInstance(Ci.nsILoadGroup);
|
|
|
|
chan.loadGroup = loadGroup;
|
|
|
|
return chan;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe new resource.
|
|
|
|
*/
|
2016-03-28 22:29:25 +03:00
|
|
|
register: function(aRecord) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("subscribeResource()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
return this._subscribeResourceInternal({
|
|
|
|
record: aRecord,
|
|
|
|
retries: 0
|
2015-06-03 15:06:00 +03:00
|
|
|
})
|
2015-09-11 17:51:32 +03:00
|
|
|
.then(result =>
|
2015-09-17 15:08:50 +03:00
|
|
|
PushCrypto.generateKeys()
|
2015-12-08 23:26:42 +03:00
|
|
|
.then(([publicKey, privateKey]) => {
|
|
|
|
result.p256dhPublicKey = publicKey;
|
|
|
|
result.p256dhPrivateKey = privateKey;
|
|
|
|
result.authenticationSecret = PushCrypto.generateAuthenticationSecret();
|
2015-09-11 17:51:32 +03:00
|
|
|
this._conns[result.subscriptionUri] = {
|
|
|
|
channel: null,
|
|
|
|
listener: null,
|
|
|
|
countUnableToConnect: 0,
|
|
|
|
lastStartListening: 0,
|
2016-02-04 05:16:10 +03:00
|
|
|
retryTimerID: 0,
|
2015-09-11 17:51:32 +03:00
|
|
|
};
|
|
|
|
this._listenForMsgs(result.subscriptionUri);
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_subscribeResourceInternal: function(aSubInfo) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("subscribeResourceInternal()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
var listener = new SubscriptionListener(aSubInfo,
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
this._serverURI,
|
|
|
|
this);
|
|
|
|
|
|
|
|
var chan = this._makeChannel(this._serverURI.spec);
|
|
|
|
chan.requestMethod = "POST";
|
2016-01-20 07:00:18 +03:00
|
|
|
chan.asyncOpen2(listener);
|
2015-09-11 17:51:32 +03:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if ("retry" in err) {
|
|
|
|
return this._subscribeResourceInternal(err.subInfo);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
})
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_deleteResource: function(aUri) {
|
|
|
|
|
|
|
|
return new Promise((resolve,reject) => {
|
|
|
|
var chan = this._makeChannel(aUri);
|
|
|
|
chan.requestMethod = "DELETE";
|
2016-01-20 07:00:18 +03:00
|
|
|
chan.asyncOpen2(new PushServiceDelete(resolve, reject));
|
2015-06-03 15:06:00 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsubscribe the resource with a subscription uri aSubscriptionUri.
|
|
|
|
* We can't do anything about it if it fails, so we don't listen for response.
|
|
|
|
*/
|
|
|
|
_unsubscribeResource: function(aSubscriptionUri) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("unsubscribeResource()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
return this._deleteResource(aSubscriptionUri);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start listening for messages.
|
|
|
|
*/
|
|
|
|
_listenForMsgs: function(aSubscriptionUri) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("listenForMsgs()", aSubscriptionUri);
|
2015-06-03 15:06:00 +03:00
|
|
|
if (!this._conns[aSubscriptionUri]) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.warn("listenForMsgs: We do not have this subscription",
|
|
|
|
aSubscriptionUri);
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var chan = this._makeChannel(aSubscriptionUri);
|
|
|
|
var conn = {};
|
|
|
|
conn.channel = chan;
|
|
|
|
var listener = new PushSubscriptionListener(this, aSubscriptionUri);
|
|
|
|
conn.listener = listener;
|
|
|
|
|
|
|
|
chan.notificationCallbacks = listener;
|
|
|
|
|
|
|
|
try {
|
2016-01-20 07:00:18 +03:00
|
|
|
chan.asyncOpen2(listener);
|
2015-06-03 15:06:00 +03:00
|
|
|
} catch (e) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("listenForMsgs: Error connecting to push server.",
|
2016-01-20 07:00:18 +03:00
|
|
|
"asyncOpen2 failed", e);
|
2015-06-03 15:06:00 +03:00
|
|
|
conn.listener.disconnect();
|
|
|
|
chan.cancel(Cr.NS_ERROR_ABORT);
|
|
|
|
this._retryAfterBackoff(aSubscriptionUri, -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._conns[aSubscriptionUri].lastStartListening = Date.now();
|
|
|
|
this._conns[aSubscriptionUri].channel = conn.channel;
|
|
|
|
this._conns[aSubscriptionUri].listener = conn.listener;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
_ackMsgRecv: function(aAckUri) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("ackMsgRecv()", aAckUri);
|
2016-02-08 08:40:00 +03:00
|
|
|
return this._deleteResource(aAckUri);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
init: function(aOptions, aMainPushService, aServerURL) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("init()");
|
2015-06-03 15:06:00 +03:00
|
|
|
this._mainPushService = aMainPushService;
|
|
|
|
this._serverURI = aServerURL;
|
2015-11-19 02:55:13 +03:00
|
|
|
|
|
|
|
return Promise.resolve();
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_retryAfterBackoff: function(aSubscriptionUri, retryAfter) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("retryAfterBackoff()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2017-07-29 00:13:57 +03:00
|
|
|
var resetRetryCount = prefs.getIntPref("http2.reset_retry_count_after_ms");
|
2015-06-03 15:06:00 +03:00
|
|
|
// If it was running for some time, reset retry counter.
|
|
|
|
if ((Date.now() - this._conns[aSubscriptionUri].lastStartListening) >
|
|
|
|
resetRetryCount) {
|
|
|
|
this._conns[aSubscriptionUri].countUnableToConnect = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-29 00:13:57 +03:00
|
|
|
let maxRetries = prefs.getIntPref("http2.maxRetries");
|
2015-06-03 15:06:00 +03:00
|
|
|
if (this._conns[aSubscriptionUri].countUnableToConnect >= maxRetries) {
|
|
|
|
this._shutdownSubscription(aSubscriptionUri);
|
|
|
|
this._resubscribe(aSubscriptionUri);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retryAfter !== -1) {
|
|
|
|
// This is a 5xx response.
|
|
|
|
this._conns[aSubscriptionUri].countUnableToConnect++;
|
2016-02-04 05:16:10 +03:00
|
|
|
this._conns[aSubscriptionUri].retryTimerID =
|
|
|
|
setTimeout(_ => this._listenForMsgs(aSubscriptionUri), retryAfter);
|
2015-06-03 15:06:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-29 00:13:57 +03:00
|
|
|
retryAfter = prefs.getIntPref("http2.retryInterval") *
|
2015-06-03 15:06:00 +03:00
|
|
|
Math.pow(2, this._conns[aSubscriptionUri].countUnableToConnect);
|
|
|
|
|
|
|
|
retryAfter = retryAfter * (0.8 + Math.random() * 0.4); // add +/-20%.
|
|
|
|
|
|
|
|
this._conns[aSubscriptionUri].countUnableToConnect++;
|
2016-02-04 05:16:10 +03:00
|
|
|
this._conns[aSubscriptionUri].retryTimerID =
|
|
|
|
setTimeout(_ => this._listenForMsgs(aSubscriptionUri), retryAfter);
|
2015-11-10 00:58:32 +03:00
|
|
|
|
|
|
|
console.debug("retryAfterBackoff: Retry in", retryAfter);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Close connections.
|
|
|
|
_shutdownConnections: function(deleteInfo) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("shutdownConnections()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
for (let subscriptionUri in this._conns) {
|
|
|
|
if (this._conns[subscriptionUri]) {
|
|
|
|
if (this._conns[subscriptionUri].listener) {
|
|
|
|
this._conns[subscriptionUri].listener._pushService = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._conns[subscriptionUri].channel) {
|
|
|
|
try {
|
|
|
|
this._conns[subscriptionUri].channel.cancel(Cr.NS_ERROR_ABORT);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
this._conns[subscriptionUri].listener = null;
|
|
|
|
this._conns[subscriptionUri].channel = null;
|
2016-02-04 05:16:10 +03:00
|
|
|
|
|
|
|
if (this._conns[subscriptionUri].retryTimerID > 0) {
|
|
|
|
clearTimeout(this._conns[subscriptionUri].retryTimerID);
|
|
|
|
}
|
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
if (deleteInfo) {
|
|
|
|
delete this._conns[subscriptionUri];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Start listening if subscriptions present.
|
|
|
|
startConnections: function(aSubscriptions) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("startConnections()", aSubscriptions.length);
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < aSubscriptions.length; i++) {
|
|
|
|
let record = aSubscriptions[i];
|
2015-12-08 23:26:42 +03:00
|
|
|
this._mainPushService.ensureCrypto(record).then(record => {
|
2015-09-11 17:51:32 +03:00
|
|
|
this._startSingleConnection(record);
|
2015-09-17 15:08:50 +03:00
|
|
|
}, error => {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("startConnections: Error updating record",
|
|
|
|
record.keyID, error);
|
2015-09-17 15:08:50 +03:00
|
|
|
});
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
_startSingleConnection: function(record) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("_startSingleConnection()");
|
2015-09-11 17:51:32 +03:00
|
|
|
if (typeof this._conns[record.subscriptionUri] != "object") {
|
|
|
|
this._conns[record.subscriptionUri] = {channel: null,
|
|
|
|
listener: null,
|
|
|
|
countUnableToConnect: 0,
|
2016-02-04 05:16:10 +03:00
|
|
|
retryTimerID: 0};
|
2015-09-11 17:51:32 +03:00
|
|
|
}
|
|
|
|
if (!this._conns[record.subscriptionUri].conn) {
|
|
|
|
this._listenForMsgs(record.subscriptionUri);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
// Close connection and notify apps that subscription are gone.
|
|
|
|
_shutdownSubscription: function(aSubscriptionUri) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("shutdownSubscriptions()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
if (typeof this._conns[aSubscriptionUri] == "object") {
|
|
|
|
if (this._conns[aSubscriptionUri].listener) {
|
|
|
|
this._conns[aSubscriptionUri].listener._pushService = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._conns[aSubscriptionUri].channel) {
|
|
|
|
try {
|
|
|
|
this._conns[aSubscriptionUri].channel.cancel(Cr.NS_ERROR_ABORT);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
delete this._conns[aSubscriptionUri];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function() {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("uninit()");
|
2016-03-16 20:03:02 +03:00
|
|
|
this._abortPendingSubscriptionRetries();
|
2015-06-03 15:06:00 +03:00
|
|
|
this._shutdownConnections(true);
|
|
|
|
this._mainPushService = null;
|
|
|
|
},
|
|
|
|
|
2016-03-16 20:03:02 +03:00
|
|
|
_abortPendingSubscriptionRetries: function() {
|
|
|
|
this._listenersPendingRetry.forEach((listener) => listener.abortRetry());
|
|
|
|
this._listenersPendingRetry.clear();
|
|
|
|
},
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
unregister: function(aRecord) {
|
|
|
|
this._shutdownSubscription(aRecord.subscriptionUri);
|
|
|
|
return this._unsubscribeResource(aRecord.subscriptionUri);
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
2016-04-21 23:35:26 +03:00
|
|
|
reportDeliveryError: function(messageID, reason) {
|
|
|
|
console.warn("reportDeliveryError: Ignoring message delivery error",
|
|
|
|
messageID, reason);
|
|
|
|
},
|
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
/** Push server has deleted subscription.
|
|
|
|
* Re-subscribe - if it succeeds send update db record and send
|
|
|
|
* pushsubscriptionchange,
|
|
|
|
* - on error delete record and send pushsubscriptionchange
|
|
|
|
* TODO: maybe pushsubscriptionerror will be included.
|
|
|
|
*/
|
|
|
|
_resubscribe: function(aSubscriptionUri) {
|
|
|
|
this._mainPushService.getByKeyID(aSubscriptionUri)
|
2016-03-28 22:29:25 +03:00
|
|
|
.then(record => this.register(record)
|
2015-06-03 15:06:00 +03:00
|
|
|
.then(recordNew => {
|
|
|
|
if (this._mainPushService) {
|
2016-02-03 15:58:03 +03:00
|
|
|
this._mainPushService
|
|
|
|
.updateRegistrationAndNotifyApp(aSubscriptionUri, recordNew)
|
|
|
|
.catch(Cu.reportError);
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
}, error => {
|
|
|
|
if (this._mainPushService) {
|
2016-02-03 15:58:03 +03:00
|
|
|
this._mainPushService
|
|
|
|
.dropRegistrationAndNotifyApp(aSubscriptionUri)
|
|
|
|
.catch(Cu.reportError);
|
2015-06-03 15:06:00 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
connOnStop: function(aRequest, aSuccess,
|
|
|
|
aSubscriptionUri) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("connOnStop() succeeded", aSuccess);
|
2015-06-03 15:06:00 +03:00
|
|
|
|
|
|
|
var conn = this._conns[aSubscriptionUri];
|
|
|
|
if (!conn) {
|
|
|
|
// there is no connection description that means that we closed
|
|
|
|
// connection, so do nothing. But we should have already deleted
|
|
|
|
// the listener.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.channel = null;
|
|
|
|
conn.listener = null;
|
|
|
|
|
|
|
|
if (!aSuccess) {
|
|
|
|
this._retryAfterBackoff(aSubscriptionUri, -1);
|
|
|
|
|
|
|
|
} else if (Math.floor(aRequest.responseStatus / 100) == 5) {
|
|
|
|
var retryAfter = retryAfterParser(aRequest);
|
|
|
|
this._retryAfterBackoff(aSubscriptionUri, retryAfter);
|
|
|
|
|
|
|
|
} else if (Math.floor(aRequest.responseStatus / 100) == 4) {
|
|
|
|
this._shutdownSubscription(aSubscriptionUri);
|
|
|
|
this._resubscribe(aSubscriptionUri);
|
|
|
|
} else if (Math.floor(aRequest.responseStatus / 100) == 2) { // This should be 204
|
|
|
|
setTimeout(_ => this._listenForMsgs(aSubscriptionUri), 0);
|
|
|
|
} else {
|
|
|
|
this._retryAfterBackoff(aSubscriptionUri, -1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-16 20:03:02 +03:00
|
|
|
addListenerPendingRetry: function(aListener) {
|
|
|
|
this._listenersPendingRetry.add(aListener);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeListenerPendingRetry: function(aListener) {
|
|
|
|
if (!this._listenersPendingRetry.remove(aListener)) {
|
|
|
|
console.debug("removeListenerPendingRetry: listener not in list?");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-05 18:56:55 +03:00
|
|
|
_pushChannelOnStop: function(aUri, aAckUri, aHeaders, aMessage) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("pushChannelOnStop()");
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2015-09-17 15:08:50 +03:00
|
|
|
this._mainPushService.receivedPushMessage(
|
2016-10-05 18:56:55 +03:00
|
|
|
aUri, "", aHeaders, aMessage, record => {
|
2015-09-17 15:08:50 +03:00
|
|
|
// Always update the stored record.
|
|
|
|
return record;
|
|
|
|
}
|
2015-09-11 17:51:32 +03:00
|
|
|
)
|
|
|
|
.then(_ => this._ackMsgRecv(aAckUri))
|
|
|
|
.catch(err => {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("pushChannelOnStop: Error receiving message",
|
|
|
|
err);
|
2015-06-26 00:52:57 +03:00
|
|
|
});
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
2015-06-26 00:52:57 +03:00
|
|
|
};
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
function PushRecordHttp2(record) {
|
|
|
|
PushRecord.call(this, record);
|
|
|
|
this.subscriptionUri = record.subscriptionUri;
|
|
|
|
this.pushReceiptEndpoint = record.pushReceiptEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
PushRecordHttp2.prototype = Object.create(PushRecord.prototype, {
|
|
|
|
keyID: {
|
|
|
|
get() {
|
|
|
|
return this.subscriptionUri;
|
|
|
|
},
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
2015-06-26 00:52:57 +03:00
|
|
|
});
|
2015-06-03 15:06:00 +03:00
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
PushRecordHttp2.prototype.toSubscription = function() {
|
|
|
|
let subscription = PushRecord.prototype.toSubscription.call(this);
|
|
|
|
subscription.pushReceiptEndpoint = this.pushReceiptEndpoint;
|
|
|
|
return subscription;
|
2015-06-03 15:06:00 +03:00
|
|
|
};
|