2013-03-29 07:49:41 +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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { AppConstants } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/AppConstants.jsm"
|
|
|
|
);
|
|
|
|
const { Preferences } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Preferences.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { clearTimeout, setTimeout } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Timer.jsm"
|
|
|
|
);
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2013-06-13 04:26:44 +04:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
var PushServiceWebSocket, PushServiceHttp2;
|
|
|
|
|
2016-03-05 02:48:09 +03:00
|
|
|
const CONNECTION_PROTOCOLS = (function() {
|
2019-04-09 13:05:51 +03:00
|
|
|
if ("android" != AppConstants.MOZ_WIDGET_TOOLKIT) {
|
2019-01-17 21:18:31 +03:00
|
|
|
({ PushServiceWebSocket } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/PushServiceWebSocket.jsm"
|
|
|
|
));
|
|
|
|
({ PushServiceHttp2 } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/PushServiceHttp2.jsm"
|
|
|
|
));
|
2016-03-05 02:48:09 +03:00
|
|
|
return [PushServiceWebSocket, PushServiceHttp2];
|
|
|
|
}
|
2019-04-09 13:05:51 +03:00
|
|
|
const { PushServiceAndroidGCM } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/PushServiceAndroidGCM.jsm"
|
|
|
|
);
|
|
|
|
return [PushServiceAndroidGCM];
|
2016-03-05 02:48:09 +03:00
|
|
|
})();
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2016-01-14 08:24:37 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
|
|
|
this,
|
|
|
|
"gPushNotifier",
|
|
|
|
"@mozilla.org/push/Notifier;1",
|
|
|
|
"nsIPushNotifier"
|
|
|
|
);
|
2018-06-14 14:31:14 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
|
|
|
this,
|
|
|
|
"eTLDService",
|
|
|
|
"@mozilla.org/network/effective-tld-service;1",
|
|
|
|
"nsIEffectiveTLDService"
|
|
|
|
);
|
2018-04-11 17:53:20 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"pushBroadcastService",
|
|
|
|
"resource://gre/modules/PushBroadcastService.jsm"
|
|
|
|
);
|
2019-04-09 13:05:51 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"PushCrypto",
|
|
|
|
"resource://gre/modules/PushCrypto.jsm"
|
|
|
|
);
|
2016-01-14 08:24:37 +03:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["PushService"];
|
2013-05-02 09:15:18 +04:00
|
|
|
|
2015-11-10 00:58:32 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "console", () => {
|
2019-01-17 21:18:31 +03:00
|
|
|
let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm");
|
2015-11-10 00:58:32 +03:00
|
|
|
return new ConsoleAPI({
|
|
|
|
maxLogLevelPref: "dom.push.loglevel",
|
|
|
|
prefix: "PushService",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-11 06:19:28 +03:00
|
|
|
const prefs = new Preferences("dom.push.");
|
2013-04-09 22:02:00 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
const PUSH_SERVICE_UNINIT = 0;
|
|
|
|
const PUSH_SERVICE_INIT = 1; // No serverURI
|
2019-04-09 13:05:51 +03:00
|
|
|
const PUSH_SERVICE_ACTIVATING = 2; // activating db
|
2015-06-03 15:05:00 +03:00
|
|
|
const PUSH_SERVICE_CONNECTION_DISABLE = 3;
|
|
|
|
const PUSH_SERVICE_ACTIVE_OFFLINE = 4;
|
|
|
|
const PUSH_SERVICE_RUNNING = 5;
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
/**
|
|
|
|
* State is change only in couple of functions:
|
|
|
|
* init - change state to PUSH_SERVICE_INIT if state was PUSH_SERVICE_UNINIT
|
|
|
|
* changeServerURL - change state to PUSH_SERVICE_ACTIVATING if serverURL
|
|
|
|
* present or PUSH_SERVICE_INIT if not present.
|
|
|
|
* changeStateConnectionEnabledEvent - it is call on pref change or during
|
|
|
|
* the service activation and it can
|
|
|
|
* change state to
|
|
|
|
* PUSH_SERVICE_CONNECTION_DISABLE
|
|
|
|
* changeStateOfflineEvent - it is called when offline state changes or during
|
|
|
|
* the service activation and it change state to
|
|
|
|
* PUSH_SERVICE_ACTIVE_OFFLINE or
|
|
|
|
* PUSH_SERVICE_RUNNING.
|
|
|
|
* uninit - change state to PUSH_SERVICE_UNINIT.
|
|
|
|
**/
|
|
|
|
|
|
|
|
// This is for starting and stopping service.
|
|
|
|
const STARTING_SERVICE_EVENT = 0;
|
|
|
|
const CHANGING_SERVICE_EVENT = 1;
|
|
|
|
const STOPPING_SERVICE_EVENT = 2;
|
|
|
|
const UNINIT_EVENT = 3;
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2016-03-16 12:53:13 +03:00
|
|
|
/**
|
|
|
|
* Annotates an error with an XPCOM result code. We use this helper
|
|
|
|
* instead of `Components.Exception` because the latter can assert in
|
|
|
|
* `nsXPCComponents_Exception::HasInstance` when inspected at shutdown.
|
|
|
|
*/
|
|
|
|
function errorWithResult(message, result = Cr.NS_ERROR_FAILURE) {
|
|
|
|
let error = new Error(message);
|
|
|
|
error.result = result;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-05-02 09:15:18 +04:00
|
|
|
/**
|
2015-06-03 15:05:00 +03:00
|
|
|
* The implementation of the push system. It uses WebSockets
|
|
|
|
* (PushServiceWebSocket) to communicate with the server and PushDB (IndexedDB)
|
|
|
|
* for persistence.
|
2013-05-02 09:15:18 +04:00
|
|
|
*/
|
2018-02-23 22:50:01 +03:00
|
|
|
var PushService = {
|
2015-06-03 15:04:00 +03:00
|
|
|
_service: null,
|
2015-06-03 15:05:00 +03:00
|
|
|
_state: PUSH_SERVICE_UNINIT,
|
|
|
|
_db: null,
|
|
|
|
_options: null,
|
2015-11-17 08:33:11 +03:00
|
|
|
_visibleNotifications: new Map(),
|
|
|
|
|
|
|
|
// Callback that is called after attempting to
|
|
|
|
// reduce the quota for a record. Used for testing purposes.
|
|
|
|
_updateQuotaTestCallback: null,
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2016-03-16 20:03:02 +03:00
|
|
|
// Set of timeout ID of tasks to reduce quota.
|
|
|
|
_updateQuotaTimeouts: new Set(),
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
// When serverURI changes (this is used for testing), db is cleaned up and a
|
|
|
|
// a new db is started. This events must be sequential.
|
2015-10-20 02:33:00 +03:00
|
|
|
_stateChangeProcessQueue: null,
|
2019-04-09 13:05:51 +03:00
|
|
|
_stateChangeProcessEnqueue(op) {
|
2015-10-20 02:33:00 +03:00
|
|
|
if (!this._stateChangeProcessQueue) {
|
|
|
|
this._stateChangeProcessQueue = Promise.resolve();
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-20 02:33:00 +03:00
|
|
|
this._stateChangeProcessQueue = this._stateChangeProcessQueue
|
2016-03-22 04:07:16 +03:00
|
|
|
.then(op)
|
|
|
|
.catch(error => {
|
|
|
|
console.error(
|
|
|
|
"stateChangeProcessEnqueue: Error transitioning state",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
return this._shutdownService();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(
|
|
|
|
"stateChangeProcessEnqueue: Error shutting down service",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
});
|
2016-04-08 21:39:00 +03:00
|
|
|
return this._stateChangeProcessQueue;
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Pending request. If a worker try to register for the same scope again, do
|
|
|
|
// not send a new registration request. Therefore we need queue of pending
|
|
|
|
// register requests. This is the list of scopes which pending registration.
|
|
|
|
_pendingRegisterRequest: {},
|
|
|
|
_notifyActivated: null,
|
|
|
|
_activated: null,
|
2019-04-09 13:05:51 +03:00
|
|
|
_checkActivated() {
|
2015-06-03 15:05:00 +03:00
|
|
|
if (this._state < PUSH_SERVICE_ACTIVATING) {
|
2015-11-10 00:58:50 +03:00
|
|
|
return Promise.reject(new Error("Push service not active"));
|
2019-04-09 13:05:51 +03:00
|
|
|
}
|
|
|
|
if (this._state > PUSH_SERVICE_ACTIVATING) {
|
2015-06-03 15:05:00 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2019-04-09 13:05:51 +03:00
|
|
|
if (!this._activated) {
|
|
|
|
this._activated = new Promise((resolve, reject) => {
|
|
|
|
this._notifyActivated = { resolve, reject };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._activated;
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_makePendingKey(aPageRecord) {
|
2015-06-24 23:34:54 +03:00
|
|
|
return aPageRecord.scope + "|" + aPageRecord.originAttributes;
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_lookupOrPutPendingRequest(aPageRecord) {
|
2015-06-24 23:34:54 +03:00
|
|
|
let key = this._makePendingKey(aPageRecord);
|
|
|
|
if (this._pendingRegisterRequest[key]) {
|
|
|
|
return this._pendingRegisterRequest[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return (this._pendingRegisterRequest[key] = this._registerWithServer(
|
|
|
|
aPageRecord
|
|
|
|
));
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_deletePendingRequest(aPageRecord) {
|
2015-06-24 23:34:54 +03:00
|
|
|
let key = this._makePendingKey(aPageRecord);
|
|
|
|
if (this._pendingRegisterRequest[key]) {
|
|
|
|
delete this._pendingRegisterRequest[key];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_setState(aNewState) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug(
|
|
|
|
"setState()",
|
|
|
|
"new state",
|
|
|
|
aNewState,
|
|
|
|
"old state",
|
|
|
|
this._state
|
|
|
|
);
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (this._state == aNewState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._state == PUSH_SERVICE_ACTIVATING) {
|
|
|
|
// It is not important what is the new state as soon as we leave
|
|
|
|
// PUSH_SERVICE_ACTIVATING
|
|
|
|
if (this._notifyActivated) {
|
|
|
|
if (aNewState < PUSH_SERVICE_ACTIVATING) {
|
2015-11-10 00:58:50 +03:00
|
|
|
this._notifyActivated.reject(new Error("Push service not active"));
|
2015-06-03 15:05:00 +03:00
|
|
|
} else {
|
|
|
|
this._notifyActivated.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._notifyActivated = null;
|
|
|
|
this._activated = null;
|
|
|
|
}
|
|
|
|
this._state = aNewState;
|
|
|
|
},
|
|
|
|
|
2018-02-23 06:58:20 +03:00
|
|
|
async _changeStateOfflineEvent(offline, calledFromConnEnabledEvent) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("changeStateOfflineEvent()", offline);
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (
|
|
|
|
this._state < PUSH_SERVICE_ACTIVE_OFFLINE &&
|
|
|
|
this._state != PUSH_SERVICE_ACTIVATING &&
|
|
|
|
!calledFromConnEnabledEvent
|
|
|
|
) {
|
2018-02-23 06:58:20 +03:00
|
|
|
return;
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offline) {
|
|
|
|
if (this._state == PUSH_SERVICE_RUNNING) {
|
|
|
|
this._service.disconnect();
|
|
|
|
}
|
|
|
|
this._setState(PUSH_SERVICE_ACTIVE_OFFLINE);
|
2018-02-23 06:58:20 +03:00
|
|
|
return;
|
2016-03-22 04:07:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._state == PUSH_SERVICE_RUNNING) {
|
|
|
|
// PushService was not in the offline state, but got notification to
|
|
|
|
// go online (a offline notification has not been sent).
|
|
|
|
// Disconnect first.
|
|
|
|
this._service.disconnect();
|
2016-04-07 15:22:33 +03:00
|
|
|
}
|
2018-02-23 06:58:20 +03:00
|
|
|
|
2018-04-11 17:53:20 +03:00
|
|
|
let broadcastListeners = await pushBroadcastService.getListeners();
|
|
|
|
|
|
|
|
// In principle, a listener could be added to the
|
|
|
|
// pushBroadcastService here, after we have gotten listeners and
|
|
|
|
// before we're RUNNING, but this can't happen in practice because
|
|
|
|
// the only caller that can add listeners is PushBroadcastService,
|
|
|
|
// and it waits on the same promise we are before it can add
|
|
|
|
// listeners. If PushBroadcastService gets woken first, it will
|
|
|
|
// update the value that is eventually returned from
|
|
|
|
// getListeners.
|
2018-02-23 06:58:20 +03:00
|
|
|
this._setState(PUSH_SERVICE_RUNNING);
|
|
|
|
|
2019-03-15 01:37:51 +03:00
|
|
|
this._service.connect(broadcastListeners);
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_changeStateConnectionEnabledEvent(enabled) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("changeStateConnectionEnabledEvent()", enabled);
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (
|
|
|
|
this._state < PUSH_SERVICE_CONNECTION_DISABLE &&
|
|
|
|
this._state != PUSH_SERVICE_ACTIVATING
|
|
|
|
) {
|
2016-03-22 04:07:16 +03:00
|
|
|
return Promise.resolve();
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled) {
|
2016-03-22 04:07:16 +03:00
|
|
|
return this._changeStateOfflineEvent(Services.io.offline, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._state == PUSH_SERVICE_RUNNING) {
|
|
|
|
this._service.disconnect();
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
2016-03-22 04:07:16 +03:00
|
|
|
this._setState(PUSH_SERVICE_CONNECTION_DISABLE);
|
|
|
|
return Promise.resolve();
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
2015-06-03 15:04:00 +03:00
|
|
|
|
2016-03-18 03:11:22 +03:00
|
|
|
// Used for testing.
|
|
|
|
changeTestServer(url, options = {}) {
|
|
|
|
console.debug("changeTestServer()");
|
|
|
|
|
2016-04-08 21:39:00 +03:00
|
|
|
return this._stateChangeProcessEnqueue(_ => {
|
2016-03-18 03:11:22 +03:00
|
|
|
if (this._state < PUSH_SERVICE_ACTIVATING) {
|
|
|
|
console.debug("changeTestServer: PushService not activated?");
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._changeServerURL(url, CHANGING_SERVICE_EVENT, options);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-29 07:49:41 +04:00
|
|
|
observe: function observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
2013-06-13 04:26:44 +04:00
|
|
|
/*
|
2015-06-03 15:05:00 +03:00
|
|
|
* We need to call uninit() on shutdown to clean up things that modules
|
|
|
|
* aren't very good at automatically cleaning up, so we don't get shutdown
|
|
|
|
* leaks on browser shutdown.
|
2013-06-13 04:26:44 +04:00
|
|
|
*/
|
2016-03-16 20:03:02 +03:00
|
|
|
case "quit-application":
|
2013-06-13 04:26:44 +04:00
|
|
|
this.uninit();
|
2015-04-21 21:10:50 +03:00
|
|
|
break;
|
2016-09-29 20:53:44 +03:00
|
|
|
case "network:offline-status-changed":
|
2015-10-20 02:33:00 +03:00
|
|
|
this._stateChangeProcessEnqueue(_ =>
|
|
|
|
this._changeStateOfflineEvent(aData === "offline", false)
|
|
|
|
);
|
2013-03-29 07:49:41 +04:00
|
|
|
break;
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2013-03-29 07:49:41 +04:00
|
|
|
case "nsPref:changed":
|
2015-04-11 06:19:28 +03:00
|
|
|
if (aData == "dom.push.serverURL") {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug(
|
|
|
|
"observe: dom.push.serverURL changed for websocket",
|
2013-04-09 22:02:00 +04:00
|
|
|
prefs.get("serverURL")
|
|
|
|
);
|
2015-10-20 02:33:00 +03:00
|
|
|
this._stateChangeProcessEnqueue(_ =>
|
2015-06-03 15:05:00 +03:00
|
|
|
this._changeServerURL(
|
|
|
|
prefs.get("serverURL"),
|
|
|
|
CHANGING_SERVICE_EVENT
|
|
|
|
)
|
|
|
|
);
|
2019-03-15 01:37:51 +03:00
|
|
|
} else if (aData == "dom.push.connection.enabled") {
|
2015-10-20 02:33:00 +03:00
|
|
|
this._stateChangeProcessEnqueue(_ =>
|
|
|
|
this._changeStateConnectionEnabledEvent(
|
|
|
|
prefs.get("connection.enabled")
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2015-10-20 02:33:00 +03:00
|
|
|
);
|
2013-03-29 07:49:41 +04:00
|
|
|
}
|
|
|
|
break;
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
case "idle-daily":
|
2016-04-07 01:46:41 +03:00
|
|
|
this._dropExpiredRegistrations().catch(error => {
|
|
|
|
console.error("Failed to drop expired registrations on idle", error);
|
|
|
|
});
|
2015-06-26 00:52:57 +03:00
|
|
|
break;
|
|
|
|
|
2015-10-06 18:14:25 +03:00
|
|
|
case "perm-changed":
|
|
|
|
this._onPermissionChange(aSubject, aData).catch(error => {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error(
|
|
|
|
"onPermissionChange: Error updating registrations:",
|
2015-10-06 18:14:25 +03:00
|
|
|
error
|
|
|
|
);
|
2019-04-09 13:05:51 +03:00
|
|
|
});
|
2015-10-06 18:14:25 +03:00
|
|
|
break;
|
|
|
|
|
2016-10-05 12:58:25 +03:00
|
|
|
case "clear-origin-attributes-data":
|
2015-11-26 18:52:54 +03:00
|
|
|
this._clearOriginData(aData).catch(error => {
|
2015-11-14 02:18:10 +03:00
|
|
|
console.error("clearOriginData: Error clearing origin data:", error);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2013-07-24 17:03:25 +04:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_clearOriginData(data) {
|
2015-11-14 02:18:10 +03:00
|
|
|
console.log("clearOriginData()");
|
2013-04-05 00:42:35 +04:00
|
|
|
|
2015-11-14 02:18:10 +03:00
|
|
|
if (!data) {
|
|
|
|
return Promise.resolve();
|
2013-03-29 07:49:41 +04:00
|
|
|
}
|
2015-11-14 02:18:10 +03:00
|
|
|
|
|
|
|
let pattern = JSON.parse(data);
|
2016-05-20 19:38:26 +03:00
|
|
|
return this._dropRegistrationsIf(record =>
|
|
|
|
record.matchesOriginAttributes(pattern)
|
|
|
|
);
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
/**
|
|
|
|
* Sends an unregister request to the server in the background. If the
|
|
|
|
* service is not connected, this function is a no-op.
|
|
|
|
*
|
|
|
|
* @param {PushRecord} record The record to unregister.
|
2016-03-28 22:29:25 +03:00
|
|
|
* @param {Number} reason An `nsIPushErrorReporter` unsubscribe reason,
|
|
|
|
* indicating why this record was removed.
|
2015-11-11 01:27:47 +03:00
|
|
|
*/
|
2016-03-28 22:29:25 +03:00
|
|
|
_backgroundUnregister(record, reason) {
|
2015-11-11 01:27:47 +03:00
|
|
|
console.debug("backgroundUnregister()");
|
|
|
|
|
|
|
|
if (!this._service.isConnected() || !record) {
|
|
|
|
return;
|
2015-10-06 18:14:25 +03:00
|
|
|
}
|
2015-11-11 01:27:47 +03:00
|
|
|
|
|
|
|
console.debug("backgroundUnregister: Notifying server", record);
|
2016-04-21 22:04:15 +03:00
|
|
|
this._sendUnregister(record, reason)
|
|
|
|
.then(() => {
|
2016-05-02 18:20:14 +03:00
|
|
|
gPushNotifier.notifySubscriptionModified(
|
|
|
|
record.scope,
|
|
|
|
record.principal
|
|
|
|
);
|
2016-04-21 22:04:15 +03:00
|
|
|
})
|
|
|
|
.catch(e => {
|
2015-11-11 01:27:47 +03:00
|
|
|
console.error("backgroundUnregister: Error notifying server", e);
|
|
|
|
});
|
2015-10-06 18:14:25 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_findService(serverURL) {
|
2015-11-10 21:50:46 +03:00
|
|
|
console.debug("findService()");
|
|
|
|
|
|
|
|
let uri;
|
|
|
|
let service;
|
|
|
|
|
|
|
|
if (!serverURL) {
|
|
|
|
console.warn("findService: No dom.push.serverURL found");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-01-09 22:27:26 +03:00
|
|
|
uri = Services.io.newURI(serverURL);
|
2015-11-10 21:50:46 +03:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
|
|
|
"findService: Error creating valid URI from",
|
|
|
|
"dom.push.serverURL",
|
|
|
|
serverURL
|
|
|
|
);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let connProtocol of CONNECTION_PROTOCOLS) {
|
|
|
|
if (connProtocol.validServerURI(uri)) {
|
|
|
|
service = connProtocol;
|
|
|
|
break;
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [service, uri];
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_changeServerURL(serverURI, event, options = {}) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("changeServerURL()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
switch (event) {
|
2015-06-03 15:05:00 +03:00
|
|
|
case UNINIT_EVENT:
|
|
|
|
return this._stopService(event);
|
|
|
|
|
|
|
|
case STARTING_SERVICE_EVENT: {
|
|
|
|
let [service, uri] = this._findService(serverURI);
|
|
|
|
if (!service) {
|
|
|
|
this._setState(PUSH_SERVICE_INIT);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2016-03-22 04:07:16 +03:00
|
|
|
return this._startService(service, uri, options).then(_ =>
|
2016-04-08 21:39:00 +03:00
|
|
|
this._changeStateConnectionEnabledEvent(
|
|
|
|
prefs.get("connection.enabled")
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2015-06-03 15:05:00 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
case CHANGING_SERVICE_EVENT:
|
|
|
|
let [service, uri] = this._findService(serverURI);
|
|
|
|
if (service) {
|
|
|
|
if (this._state == PUSH_SERVICE_INIT) {
|
|
|
|
this._setState(PUSH_SERVICE_ACTIVATING);
|
|
|
|
// The service has not been running - start it.
|
2016-03-18 03:11:22 +03:00
|
|
|
return this._startService(service, uri, options).then(_ =>
|
2016-04-08 21:39:00 +03:00
|
|
|
this._changeStateConnectionEnabledEvent(
|
|
|
|
prefs.get("connection.enabled")
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2015-06-03 15:05:00 +03:00
|
|
|
);
|
|
|
|
}
|
2019-04-09 13:05:51 +03:00
|
|
|
this._setState(PUSH_SERVICE_ACTIVATING);
|
|
|
|
// If we already had running service - stop service, start the new
|
|
|
|
// one and check connection.enabled and offline state(offline state
|
|
|
|
// check is called in changeStateConnectionEnabledEvent function)
|
|
|
|
return this._stopService(CHANGING_SERVICE_EVENT)
|
|
|
|
.then(_ => this._startService(service, uri, options))
|
|
|
|
.then(_ =>
|
|
|
|
this._changeStateConnectionEnabledEvent(
|
|
|
|
prefs.get("connection.enabled")
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2019-04-09 13:05:51 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this._state == PUSH_SERVICE_INIT) {
|
|
|
|
return Promise.resolve();
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
2019-04-09 13:05:51 +03:00
|
|
|
// The new serverUri is empty or misconfigured - stop service.
|
|
|
|
this._setState(PUSH_SERVICE_INIT);
|
|
|
|
return this._stopService(STOPPING_SERVICE_EVENT);
|
|
|
|
|
2016-04-07 01:46:41 +03:00
|
|
|
default:
|
|
|
|
console.error("Unexpected event in _changeServerURL", event);
|
|
|
|
return Promise.reject(new Error(`Unexpected event ${event}`));
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PushService initialization is divided into 4 parts:
|
2016-03-16 20:03:02 +03:00
|
|
|
* init() - start listening for quit-application and serverURL changes.
|
2015-06-03 15:05:00 +03:00
|
|
|
* state is change to PUSH_SERVICE_INIT
|
|
|
|
* startService() - if serverURL is present this function is called. It starts
|
|
|
|
* listening for broadcasted messages, starts db and
|
|
|
|
* PushService connection (WebSocket).
|
|
|
|
* state is change to PUSH_SERVICE_ACTIVATING.
|
|
|
|
* startObservers() - start other observers.
|
|
|
|
* changeStateConnectionEnabledEvent - checks prefs and offline state.
|
|
|
|
* It changes state to:
|
|
|
|
* PUSH_SERVICE_RUNNING,
|
|
|
|
* PUSH_SERVICE_ACTIVE_OFFLINE or
|
|
|
|
* PUSH_SERVICE_CONNECTION_DISABLE.
|
|
|
|
*/
|
2019-03-15 01:37:51 +03:00
|
|
|
async init(options = {}) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("init()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (this._state > PUSH_SERVICE_UNINIT) {
|
2015-04-21 21:10:50 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-04-19 13:06:21 +03:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
this._setState(PUSH_SERVICE_ACTIVATING);
|
|
|
|
|
2016-10-20 18:43:11 +03:00
|
|
|
prefs.observe("serverURL", this);
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "quit-application");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (options.serverURI) {
|
|
|
|
// this is use for xpcshell test.
|
|
|
|
|
2019-03-15 01:37:51 +03:00
|
|
|
await this._stateChangeProcessEnqueue(_ =>
|
2016-03-22 04:07:16 +03:00
|
|
|
this._changeServerURL(
|
|
|
|
options.serverURI,
|
|
|
|
STARTING_SERVICE_EVENT,
|
|
|
|
options
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2016-03-22 04:07:16 +03:00
|
|
|
);
|
2015-06-03 15:05:00 +03:00
|
|
|
} else {
|
|
|
|
// This is only used for testing. Different tests require connecting to
|
|
|
|
// slightly different URLs.
|
2019-03-15 01:37:51 +03:00
|
|
|
await this._stateChangeProcessEnqueue(_ =>
|
2015-06-03 15:05:00 +03:00
|
|
|
this._changeServerURL(prefs.get("serverURL"), STARTING_SERVICE_EVENT)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_startObservers() {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("startObservers()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (this._state != PUSH_SERVICE_ACTIVATING) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "clear-origin-attributes-data");
|
2013-06-13 04:26:44 +04:00
|
|
|
|
2016-09-29 20:53:44 +03:00
|
|
|
// The offline-status-changed event is used to know
|
2013-06-13 04:26:44 +04:00
|
|
|
// when to (dis)connect. It may not fire if the underlying OS changes
|
|
|
|
// networks; in such a case we rely on timeout.
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "network:offline-status-changed");
|
2013-06-13 04:26:44 +04:00
|
|
|
|
2013-06-17 22:36:58 +04:00
|
|
|
// Used to monitor if the user wishes to disable Push.
|
|
|
|
prefs.observe("connection.enabled", this);
|
2015-06-26 00:52:57 +03:00
|
|
|
|
2015-10-06 18:14:25 +03:00
|
|
|
// Prunes expired registrations and notifies dormant service workers.
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "idle-daily");
|
2015-10-06 18:14:25 +03:00
|
|
|
|
|
|
|
// Prunes registrations for sites for which the user revokes push
|
|
|
|
// permissions.
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "perm-changed");
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2016-03-22 04:07:16 +03:00
|
|
|
_startService(service, serverURI, options) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("startService()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (this._state != PUSH_SERVICE_ACTIVATING) {
|
2015-11-19 02:55:13 +03:00
|
|
|
return Promise.reject();
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this._service = service;
|
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
this._db = options.db;
|
|
|
|
if (!this._db) {
|
2015-06-03 15:05:00 +03:00
|
|
|
this._db = this._service.newPushDB();
|
2015-06-03 15:04:00 +03:00
|
|
|
}
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2015-11-19 02:55:13 +03:00
|
|
|
return this._service.init(options, this, serverURI).then(() => {
|
|
|
|
this._startObservers();
|
|
|
|
return this._dropExpiredRegistrations();
|
|
|
|
});
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
/**
|
|
|
|
* PushService uninitialization is divided into 3 parts:
|
|
|
|
* stopObservers() - stot observers started in startObservers.
|
|
|
|
* stopService() - It stops listening for broadcasted messages, stops db and
|
|
|
|
* PushService connection (WebSocket).
|
|
|
|
* state is changed to PUSH_SERVICE_INIT.
|
2016-03-16 20:03:02 +03:00
|
|
|
* uninit() - stop listening for quit-application and serverURL changes.
|
2015-06-03 15:05:00 +03:00
|
|
|
* state is change to PUSH_SERVICE_UNINIT
|
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
_stopService(event) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("stopService()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (this._state < PUSH_SERVICE_ACTIVATING) {
|
2016-04-07 01:46:41 +03:00
|
|
|
return Promise.resolve();
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
2013-06-13 04:26:44 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
this._stopObservers();
|
2013-05-02 09:15:18 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
this._service.disconnect();
|
|
|
|
this._service.uninit();
|
2015-06-03 15:06:00 +03:00
|
|
|
this._service = null;
|
2013-05-02 09:15:18 +04:00
|
|
|
|
2016-03-16 20:03:02 +03:00
|
|
|
this._updateQuotaTimeouts.forEach(timeoutID => clearTimeout(timeoutID));
|
|
|
|
this._updateQuotaTimeouts.clear();
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
if (!this._db) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
if (event == UNINIT_EVENT) {
|
|
|
|
// If it is uninitialized just close db.
|
2015-06-03 15:04:00 +03:00
|
|
|
this._db.close();
|
|
|
|
this._db = null;
|
2015-06-03 15:05:00 +03:00
|
|
|
return Promise.resolve();
|
2013-06-13 04:26:44 +04:00
|
|
|
}
|
2013-05-02 09:15:18 +04:00
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
return this.dropUnexpiredRegistrations().then(
|
2015-06-03 15:05:00 +03:00
|
|
|
_ => {
|
|
|
|
this._db.close();
|
|
|
|
this._db = null;
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this._db.close();
|
|
|
|
this._db = null;
|
|
|
|
}
|
|
|
|
);
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_stopObservers() {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("stopObservers()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (this._state < PUSH_SERVICE_ACTIVATING) {
|
2013-03-29 07:49:41 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
prefs.ignore("connection.enabled", this);
|
|
|
|
|
2016-09-29 20:53:44 +03:00
|
|
|
Services.obs.removeObserver(this, "network:offline-status-changed");
|
2016-10-05 12:58:25 +03:00
|
|
|
Services.obs.removeObserver(this, "clear-origin-attributes-data");
|
2015-06-26 00:52:57 +03:00
|
|
|
Services.obs.removeObserver(this, "idle-daily");
|
2015-10-06 18:14:25 +03:00
|
|
|
Services.obs.removeObserver(this, "perm-changed");
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2016-03-22 04:07:16 +03:00
|
|
|
_shutdownService() {
|
|
|
|
let promiseChangeURL = this._changeServerURL("", UNINIT_EVENT);
|
|
|
|
this._setState(PUSH_SERVICE_UNINIT);
|
|
|
|
console.debug("shutdownService: shutdown complete!");
|
|
|
|
return promiseChangeURL;
|
|
|
|
},
|
|
|
|
|
2019-03-15 01:37:51 +03:00
|
|
|
async uninit() {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("uninit()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
if (this._state == PUSH_SERVICE_UNINIT) {
|
2013-03-29 07:49:41 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
prefs.ignore("serverURL", this);
|
2016-03-16 20:03:02 +03:00
|
|
|
Services.obs.removeObserver(this, "quit-application");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2019-03-15 01:37:51 +03:00
|
|
|
await this._stateChangeProcessEnqueue(_ => this._shutdownService());
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
/**
|
|
|
|
* Drops all active registrations and notifies the associated service
|
|
|
|
* workers. This function is called when the user switches Push servers,
|
|
|
|
* or when the server invalidates all existing registrations.
|
|
|
|
*
|
|
|
|
* We ignore expired registrations because they're already handled in other
|
|
|
|
* code paths. Registrations that expired after exceeding their quotas are
|
|
|
|
* evicted at startup, or on the next `idle-daily` event. Registrations that
|
|
|
|
* expired because the user revoked the notification permission are evicted
|
|
|
|
* once the permission is reinstated.
|
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
dropUnexpiredRegistrations() {
|
2015-11-11 01:27:47 +03:00
|
|
|
return this._db.clearIf(record => {
|
|
|
|
if (record.isExpired()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-05 19:12:35 +03:00
|
|
|
this._notifySubscriptionChangeObservers(record);
|
2015-11-11 01:27:47 +03:00
|
|
|
return true;
|
|
|
|
});
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_notifySubscriptionChangeObservers(record) {
|
2015-11-11 01:27:47 +03:00
|
|
|
if (!record) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-14 08:24:37 +03:00
|
|
|
gPushNotifier.notifySubscriptionChange(record.scope, record.principal);
|
2015-06-26 00:52:57 +03:00
|
|
|
},
|
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
/**
|
|
|
|
* Drops a registration and notifies the associated service worker. If the
|
|
|
|
* registration does not exist, this function is a no-op.
|
|
|
|
*
|
|
|
|
* @param {String} keyID The registration ID to remove.
|
|
|
|
* @returns {Promise} Resolves once the worker has been notified.
|
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
dropRegistrationAndNotifyApp(aKeyID) {
|
2015-11-11 01:27:47 +03:00
|
|
|
return this._db
|
|
|
|
.delete(aKeyID)
|
|
|
|
.then(record => this._notifySubscriptionChangeObservers(record));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces an existing registration and notifies the associated service
|
|
|
|
* worker.
|
|
|
|
*
|
|
|
|
* @param {String} aOldKey The registration ID to replace.
|
|
|
|
* @param {PushRecord} aNewRecord The new record.
|
|
|
|
* @returns {Promise} Resolves once the worker has been notified.
|
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
updateRegistrationAndNotifyApp(aOldKey, aNewRecord) {
|
2015-11-11 01:27:47 +03:00
|
|
|
return this.updateRecordAndNotifyApp(aOldKey, _ => aNewRecord);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Updates a registration and notifies the associated service worker.
|
|
|
|
*
|
|
|
|
* @param {String} keyID The registration ID to update.
|
|
|
|
* @param {Function} updateFunc Returns the updated record.
|
|
|
|
* @returns {Promise} Resolves with the updated record once the worker
|
|
|
|
* has been notified.
|
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
updateRecordAndNotifyApp(aKeyID, aUpdateFunc) {
|
2015-11-11 01:27:47 +03:00
|
|
|
return this._db.update(aKeyID, aUpdateFunc).then(record => {
|
2015-09-17 15:08:50 +03:00
|
|
|
this._notifySubscriptionChangeObservers(record);
|
2015-11-11 01:27:47 +03:00
|
|
|
return record;
|
2015-09-17 15:08:50 +03:00
|
|
|
});
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
ensureCrypto(record) {
|
2016-03-23 05:20:36 +03:00
|
|
|
if (
|
|
|
|
record.hasAuthenticationSecret() &&
|
2015-12-08 23:26:42 +03:00
|
|
|
record.p256dhPublicKey &&
|
|
|
|
record.p256dhPrivateKey
|
|
|
|
) {
|
2015-09-17 15:08:50 +03:00
|
|
|
return Promise.resolve(record);
|
|
|
|
}
|
2015-12-08 23:26:42 +03:00
|
|
|
|
|
|
|
let keygen = Promise.resolve([]);
|
|
|
|
if (!record.p256dhPublicKey || !record.p256dhPrivateKey) {
|
|
|
|
keygen = PushCrypto.generateKeys();
|
|
|
|
}
|
2015-09-17 15:08:50 +03:00
|
|
|
// We do not have a encryption key. so we need to generate it. This
|
|
|
|
// is only going to happen on db upgrade from version 4 to higher.
|
2015-12-08 23:26:42 +03:00
|
|
|
return keygen.then(
|
|
|
|
([pubKey, privKey]) => {
|
2015-09-17 15:08:50 +03:00
|
|
|
return this.updateRecordAndNotifyApp(record.keyID, record => {
|
2015-12-08 23:26:42 +03:00
|
|
|
if (!record.p256dhPublicKey || !record.p256dhPrivateKey) {
|
|
|
|
record.p256dhPublicKey = pubKey;
|
|
|
|
record.p256dhPrivateKey = privKey;
|
|
|
|
}
|
2016-03-23 05:20:36 +03:00
|
|
|
if (!record.hasAuthenticationSecret()) {
|
2015-12-08 23:26:42 +03:00
|
|
|
record.authenticationSecret = PushCrypto.generateAuthenticationSecret();
|
|
|
|
}
|
2015-09-17 15:08:50 +03:00
|
|
|
return record;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
return this.dropRegistrationAndNotifyApp(record.keyID).then(() =>
|
|
|
|
Promise.reject(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
/**
|
|
|
|
* Dispatches an incoming message to a service worker, recalculating the
|
|
|
|
* quota for the associated push registration. If the quota is exceeded,
|
|
|
|
* the registration and message will be dropped, and the worker will not
|
|
|
|
* be notified.
|
|
|
|
*
|
|
|
|
* @param {String} keyID The push registration ID.
|
2016-03-28 22:29:25 +03:00
|
|
|
* @param {String} messageID The message ID, used to report service worker
|
|
|
|
* delivery failures. For Web Push messages, this is the version. If empty,
|
|
|
|
* failures will not be reported.
|
2016-10-05 18:56:55 +03:00
|
|
|
* @param {Object} headers The encryption headers.
|
2016-03-28 22:29:25 +03:00
|
|
|
* @param {ArrayBuffer|Uint8Array} data The encrypted message data.
|
2015-06-26 00:52:57 +03:00
|
|
|
* @param {Function} updateFunc A function that receives the existing
|
|
|
|
* registration record as its argument, and returns a new record. If the
|
|
|
|
* function returns `null` or `undefined`, the record will not be updated.
|
|
|
|
* `PushServiceWebSocket` uses this to drop incoming updates with older
|
|
|
|
* versions.
|
2016-03-28 22:29:25 +03:00
|
|
|
* @returns {Promise} Resolves with an `nsIPushErrorReporter` ack status
|
|
|
|
* code, indicating whether the message was delivered successfully.
|
2015-06-26 00:52:57 +03:00
|
|
|
*/
|
2016-10-05 18:56:55 +03:00
|
|
|
receivedPushMessage(keyID, messageID, headers, data, updateFunc) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receivedPushMessage()");
|
2015-06-26 00:52:57 +03:00
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
return this._updateRecordAfterPush(keyID, updateFunc)
|
|
|
|
.then(record => {
|
2016-06-03 21:45:46 +03:00
|
|
|
if (record.quotaApplies()) {
|
|
|
|
// Update quota after the delay, at which point
|
|
|
|
// we check for visible notifications.
|
2019-04-09 13:05:51 +03:00
|
|
|
let timeoutID = setTimeout(_ => {
|
2016-06-03 21:45:46 +03:00
|
|
|
this._updateQuota(keyID);
|
|
|
|
if (!this._updateQuotaTimeouts.delete(timeoutID)) {
|
|
|
|
console.debug(
|
|
|
|
"receivedPushMessage: quota update timeout missing?"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, prefs.get("quotaUpdateDelay"));
|
|
|
|
this._updateQuotaTimeouts.add(timeoutID);
|
|
|
|
}
|
2016-10-05 18:56:55 +03:00
|
|
|
return this._decryptAndNotifyApp(record, messageID, headers, data);
|
2016-03-28 22:29:25 +03:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error("receivedPushMessage: Error notifying app", error);
|
|
|
|
return Ci.nsIPushErrorReporter.ACK_NOT_DELIVERED;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-04-11 17:53:20 +03:00
|
|
|
/**
|
|
|
|
* Dispatches a broadcast notification to the BroadcastService.
|
2019-04-10 19:55:52 +03:00
|
|
|
*
|
|
|
|
* @param {Object} message The reply received by PushServiceWebSocket
|
|
|
|
* @param {Object} context Additional information about the context in which the
|
|
|
|
* notification was received.
|
2018-04-11 17:53:20 +03:00
|
|
|
*/
|
2019-04-10 19:55:52 +03:00
|
|
|
receivedBroadcastMessage(message, context) {
|
|
|
|
pushBroadcastService
|
|
|
|
.receivedBroadcastMessage(message.broadcasts, context)
|
2018-04-11 17:53:20 +03:00
|
|
|
.catch(e => {
|
|
|
|
console.error(e);
|
2019-04-09 13:05:51 +03:00
|
|
|
});
|
2018-04-11 17:53:20 +03:00
|
|
|
},
|
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
/**
|
|
|
|
* Updates a registration record after receiving a push message.
|
|
|
|
*
|
|
|
|
* @param {String} keyID The push registration ID.
|
|
|
|
* @param {Function} updateFunc The function passed to `receivedPushMessage`.
|
2016-10-05 18:56:55 +03:00
|
|
|
* @returns {Promise} Resolves with the updated record, or rejects if the
|
2016-03-28 22:29:25 +03:00
|
|
|
* record was not updated.
|
|
|
|
*/
|
|
|
|
_updateRecordAfterPush(keyID, updateFunc) {
|
2015-09-11 17:51:32 +03:00
|
|
|
return this.getByKeyID(keyID)
|
|
|
|
.then(record => {
|
2015-06-26 00:52:57 +03:00
|
|
|
if (!record) {
|
|
|
|
throw new Error("No record for key ID " + keyID);
|
2015-09-17 15:08:50 +03:00
|
|
|
}
|
2016-03-28 22:29:25 +03:00
|
|
|
return record
|
|
|
|
.getLastVisit()
|
|
|
|
.then(lastVisit => {
|
|
|
|
// As a special case, don't notify the service worker if the user
|
|
|
|
// cleared their history.
|
|
|
|
if (!isFinite(lastVisit)) {
|
|
|
|
throw new Error("Ignoring message sent to unvisited origin");
|
2019-07-05 11:44:55 +03:00
|
|
|
}
|
2016-03-28 22:29:25 +03:00
|
|
|
return lastVisit;
|
|
|
|
})
|
|
|
|
.then(lastVisit => {
|
|
|
|
// Update the record, resetting the quota if the user has visited the
|
|
|
|
// site since the last push.
|
|
|
|
return this._db.update(keyID, record => {
|
|
|
|
let newRecord = updateFunc(record);
|
|
|
|
if (!newRecord) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Because `unregister` is advisory only, we can still receive messages
|
|
|
|
// for stale Simple Push registrations from the server. To work around
|
|
|
|
// this, we check if the record has expired before *and* after updating
|
|
|
|
// the quota.
|
|
|
|
if (newRecord.isExpired()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
newRecord.receivedPush(lastVisit);
|
|
|
|
return newRecord;
|
2019-07-05 11:44:55 +03:00
|
|
|
});
|
2016-03-28 22:29:25 +03:00
|
|
|
});
|
2019-07-05 11:44:55 +03:00
|
|
|
})
|
2015-06-26 00:52:57 +03:00
|
|
|
.then(record => {
|
2016-10-20 18:45:44 +03:00
|
|
|
gPushNotifier.notifySubscriptionModified(
|
2016-07-20 20:47:04 +03:00
|
|
|
record.scope,
|
2016-10-20 18:45:44 +03:00
|
|
|
record.principal
|
2019-07-05 11:44:55 +03:00
|
|
|
);
|
2015-11-17 08:33:11 +03:00
|
|
|
return record;
|
2015-09-17 15:08:50 +03:00
|
|
|
});
|
2016-03-28 22:29:25 +03:00
|
|
|
},
|
|
|
|
|
2016-07-20 20:47:04 +03:00
|
|
|
/**
|
|
|
|
* Decrypts an incoming message and notifies the associated service worker.
|
|
|
|
*
|
|
|
|
* @param {PushRecord} record The receiving registration.
|
|
|
|
* @param {String} messageID The message ID.
|
2016-10-05 18:56:55 +03:00
|
|
|
* @param {Object} headers The encryption headers.
|
2016-07-20 20:47:04 +03:00
|
|
|
* @param {ArrayBuffer|Uint8Array} data The encrypted message data.
|
|
|
|
* @returns {Promise} Resolves with an ack status code.
|
|
|
|
*/
|
2016-10-05 18:56:55 +03:00
|
|
|
_decryptAndNotifyApp(record, messageID, headers, data) {
|
2016-10-05 18:57:52 +03:00
|
|
|
return PushCrypto.decrypt(
|
|
|
|
record.p256dhPrivateKey,
|
|
|
|
record.p256dhPublicKey,
|
|
|
|
record.authenticationSecret,
|
|
|
|
headers,
|
|
|
|
data
|
2016-07-20 20:47:04 +03:00
|
|
|
).then(
|
|
|
|
message => this._notifyApp(record, messageID, message),
|
|
|
|
error => {
|
2016-10-05 18:57:52 +03:00
|
|
|
console.warn(
|
|
|
|
"decryptAndNotifyApp: Error decrypting message",
|
|
|
|
record.scope,
|
|
|
|
messageID,
|
|
|
|
error
|
|
|
|
);
|
2019-07-05 11:44:55 +03:00
|
|
|
|
2016-10-05 18:57:52 +03:00
|
|
|
let message = error.format(record.scope);
|
2016-07-20 20:47:04 +03:00
|
|
|
gPushNotifier.notifyError(
|
|
|
|
record.scope,
|
|
|
|
record.principal,
|
|
|
|
message,
|
|
|
|
Ci.nsIScriptError.errorFlag
|
|
|
|
);
|
|
|
|
return Ci.nsIPushErrorReporter.ACK_DECRYPTION_ERROR;
|
|
|
|
}
|
|
|
|
);
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_updateQuota(keyID) {
|
2015-11-17 08:33:11 +03:00
|
|
|
console.debug("updateQuota()");
|
|
|
|
|
|
|
|
this._db
|
|
|
|
.update(keyID, record => {
|
|
|
|
// Record may have expired from an earlier quota update.
|
|
|
|
if (record.isExpired()) {
|
|
|
|
console.debug(
|
|
|
|
"updateQuota: Trying to update quota for expired record",
|
|
|
|
record
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// If there are visible notifications, don't apply the quota penalty
|
|
|
|
// for the message.
|
2015-12-16 20:21:22 +03:00
|
|
|
if (record.uri && !this._visibleNotifications.has(record.uri.prePath)) {
|
2015-11-17 08:33:11 +03:00
|
|
|
record.reduceQuota();
|
|
|
|
}
|
|
|
|
return record;
|
|
|
|
})
|
|
|
|
.then(record => {
|
2016-10-20 18:45:44 +03:00
|
|
|
if (record.isExpired()) {
|
|
|
|
// Drop the registration in the background. If the user returns to the
|
|
|
|
// site, the service worker will be notified on the next `idle-daily`
|
|
|
|
// event.
|
|
|
|
this._backgroundUnregister(
|
|
|
|
record,
|
|
|
|
Ci.nsIPushErrorReporter.UNSUBSCRIBE_QUOTA_EXCEEDED
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
gPushNotifier.notifySubscriptionModified(
|
|
|
|
record.scope,
|
|
|
|
record.principal
|
|
|
|
);
|
2015-11-17 08:33:11 +03:00
|
|
|
}
|
|
|
|
if (this._updateQuotaTestCallback) {
|
|
|
|
// Callback so that test may be notified when the quota update is complete.
|
|
|
|
this._updateQuotaTestCallback();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.debug("updateQuota: Error while trying to update quota", error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
notificationForOriginShown(origin) {
|
2015-11-17 08:33:11 +03:00
|
|
|
console.debug("notificationForOriginShown()", origin);
|
|
|
|
let count;
|
|
|
|
if (this._visibleNotifications.has(origin)) {
|
|
|
|
count = this._visibleNotifications.get(origin);
|
|
|
|
} else {
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
this._visibleNotifications.set(origin, count + 1);
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
notificationForOriginClosed(origin) {
|
2015-11-17 08:33:11 +03:00
|
|
|
console.debug("notificationForOriginClosed()", origin);
|
|
|
|
let count;
|
|
|
|
if (this._visibleNotifications.has(origin)) {
|
|
|
|
count = this._visibleNotifications.get(origin);
|
|
|
|
} else {
|
|
|
|
console.debug(
|
|
|
|
"notificationForOriginClosed: closing notification that has not been shown?"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (count > 1) {
|
|
|
|
this._visibleNotifications.set(origin, count - 1);
|
|
|
|
} else {
|
|
|
|
this._visibleNotifications.delete(origin);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
reportDeliveryError(messageID, reason) {
|
|
|
|
console.debug("reportDeliveryError()", messageID, reason);
|
|
|
|
if (this._state == PUSH_SERVICE_RUNNING && this._service.isConnected()) {
|
|
|
|
// Only report errors if we're initialized and connected.
|
|
|
|
this._service.reportDeliveryError(messageID, reason);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_notifyApp(aPushRecord, messageID, message) {
|
2015-06-24 23:34:54 +03:00
|
|
|
if (
|
|
|
|
!aPushRecord ||
|
|
|
|
!aPushRecord.scope ||
|
|
|
|
aPushRecord.originAttributes === undefined
|
|
|
|
) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("notifyApp: Invalid record", aPushRecord);
|
2016-03-28 22:29:25 +03:00
|
|
|
return Ci.nsIPushErrorReporter.ACK_NOT_DELIVERED;
|
2013-03-29 07:49:41 +04:00
|
|
|
}
|
|
|
|
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("notifyApp()", aPushRecord.scope);
|
2015-04-11 06:19:28 +03:00
|
|
|
|
|
|
|
// If permission has been revoked, trash the message.
|
2015-08-20 02:03:15 +03:00
|
|
|
if (!aPushRecord.hasPermission()) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.warn("notifyApp: Missing push permission", aPushRecord);
|
2016-03-28 22:29:25 +03:00
|
|
|
return Ci.nsIPushErrorReporter.ACK_NOT_DELIVERED;
|
2015-04-11 06:19:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-14 08:24:37 +03:00
|
|
|
let payload = ArrayBuffer.isView(message)
|
|
|
|
? new Uint8Array(message.buffer)
|
|
|
|
: message;
|
|
|
|
|
|
|
|
if (aPushRecord.quotaApplies()) {
|
|
|
|
// Don't record telemetry for chrome push messages.
|
|
|
|
Services.telemetry.getHistogramById("PUSH_API_NOTIFY").add();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload) {
|
|
|
|
gPushNotifier.notifyPushWithData(
|
|
|
|
aPushRecord.scope,
|
|
|
|
aPushRecord.principal,
|
2019-05-10 10:15:08 +03:00
|
|
|
messageID,
|
|
|
|
payload
|
|
|
|
);
|
2016-01-14 08:24:37 +03:00
|
|
|
} else {
|
2016-03-28 22:29:25 +03:00
|
|
|
gPushNotifier.notifyPush(
|
|
|
|
aPushRecord.scope,
|
|
|
|
aPushRecord.principal,
|
|
|
|
messageID
|
|
|
|
);
|
2016-01-14 08:24:37 +03:00
|
|
|
}
|
2015-04-11 06:19:28 +03:00
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
return Ci.nsIPushErrorReporter.ACK_DELIVERED;
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
getByKeyID(aKeyID) {
|
2015-06-03 15:06:00 +03:00
|
|
|
return this._db.getByKeyID(aKeyID);
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
getAllUnexpired() {
|
2015-06-26 00:52:57 +03:00
|
|
|
return this._db.getAllUnexpired();
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
_sendRequest(action, ...params) {
|
2015-06-03 15:05:00 +03:00
|
|
|
if (this._state == PUSH_SERVICE_CONNECTION_DISABLE) {
|
2015-11-10 00:58:50 +03:00
|
|
|
return Promise.reject(new Error("Push service disabled"));
|
2016-06-08 16:13:15 +03:00
|
|
|
}
|
|
|
|
if (this._state == PUSH_SERVICE_ACTIVE_OFFLINE) {
|
2015-11-10 00:58:50 +03:00
|
|
|
return Promise.reject(new Error("Push service offline"));
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
2016-04-08 21:39:00 +03:00
|
|
|
// Ensure the backend is ready. `getByPageRecord` already checks this, but
|
|
|
|
// we need to check again here in case the service was restarted in the
|
|
|
|
// meantime.
|
|
|
|
return this._checkActivated().then(_ => {
|
|
|
|
switch (action) {
|
|
|
|
case "register":
|
|
|
|
return this._service.register(...params);
|
|
|
|
case "unregister":
|
|
|
|
return this._service.unregister(...params);
|
|
|
|
}
|
|
|
|
return Promise.reject(new Error("Unknown request type: " + action));
|
|
|
|
});
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
/**
|
2013-03-29 07:49:41 +04:00
|
|
|
* Called on message from the child process. aPageRecord is an object sent by
|
2016-09-01 21:17:03 +03:00
|
|
|
* the push manager, identifying the sending page and other fields.
|
2013-03-29 07:49:41 +04:00
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
_registerWithServer(aPageRecord) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("registerWithServer()", aPageRecord);
|
2015-04-19 13:06:11 +03:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
return this._sendRequest("register", aPageRecord)
|
2015-06-26 00:52:57 +03:00
|
|
|
.then(
|
|
|
|
record => this._onRegisterSuccess(record),
|
2015-06-03 15:05:00 +03:00
|
|
|
err => this._onRegisterError(err)
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2015-06-26 00:52:57 +03:00
|
|
|
.then(
|
|
|
|
record => {
|
2015-06-24 23:34:54 +03:00
|
|
|
this._deletePendingRequest(aPageRecord);
|
2016-05-02 18:20:14 +03:00
|
|
|
gPushNotifier.notifySubscriptionModified(
|
|
|
|
record.scope,
|
|
|
|
record.principal
|
|
|
|
);
|
2015-11-11 01:27:47 +03:00
|
|
|
return record.toSubscription();
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
err => {
|
2015-06-24 23:34:54 +03:00
|
|
|
this._deletePendingRequest(aPageRecord);
|
2015-06-03 15:05:00 +03:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
);
|
2015-04-21 21:10:50 +03:00
|
|
|
},
|
2015-04-19 13:06:11 +03:00
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
_sendUnregister(aRecord, aReason) {
|
2017-03-07 06:06:32 +03:00
|
|
|
return this._sendRequest("unregister", aRecord, aReason);
|
2015-08-06 03:03:49 +03:00
|
|
|
},
|
|
|
|
|
2013-03-29 07:49:41 +04:00
|
|
|
/**
|
|
|
|
* Exceptions thrown in _onRegisterSuccess are caught by the promise obtained
|
2015-06-03 15:05:00 +03:00
|
|
|
* from _service.request, causing the promise to be rejected instead.
|
2013-03-29 07:49:41 +04:00
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
_onRegisterSuccess(aRecord) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("_onRegisterSuccess()");
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
return this._db.put(aRecord).catch(error => {
|
2015-06-07 08:17:04 +03:00
|
|
|
// Unable to save. Destroy the subscription in the background.
|
2016-03-28 22:29:25 +03:00
|
|
|
this._backgroundUnregister(
|
|
|
|
aRecord,
|
|
|
|
Ci.nsIPushErrorReporter.UNSUBSCRIBE_MANUAL
|
|
|
|
);
|
2015-06-03 15:05:00 +03:00
|
|
|
throw error;
|
|
|
|
});
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exceptions thrown in _onRegisterError are caught by the promise obtained
|
2015-06-03 15:05:00 +03:00
|
|
|
* from _service.request, causing the promise to be rejected instead.
|
2013-03-29 07:49:41 +04:00
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
_onRegisterError(reply) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("_onRegisterError()");
|
2017-03-07 06:06:32 +03:00
|
|
|
|
2013-09-17 21:10:36 +04:00
|
|
|
if (!reply.error) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.warn(
|
|
|
|
"onRegisterError: Called without valid error message!",
|
2016-04-12 00:51:33 +03:00
|
|
|
reply
|
|
|
|
);
|
2015-11-10 00:58:32 +03:00
|
|
|
throw new Error("Registration error");
|
2013-03-29 07:49:41 +04:00
|
|
|
}
|
2015-04-21 21:10:50 +03:00
|
|
|
throw reply.error;
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2016-01-14 08:24:37 +03:00
|
|
|
notificationsCleared() {
|
2015-12-08 23:41:41 +03:00
|
|
|
this._visibleNotifications.clear();
|
|
|
|
},
|
2015-06-24 23:34:54 +03:00
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
_getByPageRecord(pageRecord) {
|
|
|
|
return this._checkActivated().then(_ =>
|
|
|
|
this._db.getByIdentifiers(pageRecord)
|
|
|
|
);
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
register(aPageRecord) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("register()", aPageRecord);
|
2015-06-03 15:04:00 +03:00
|
|
|
|
2016-03-16 12:53:13 +03:00
|
|
|
let keyPromise;
|
2019-05-10 10:15:31 +03:00
|
|
|
if (aPageRecord.appServerKey && aPageRecord.appServerKey.length != 0) {
|
2016-03-16 12:53:13 +03:00
|
|
|
let keyView = new Uint8Array(aPageRecord.appServerKey);
|
|
|
|
keyPromise = PushCrypto.validateAppServerKey(keyView).catch(error => {
|
|
|
|
// Normalize Web Crypto exceptions. `nsIPushService` will forward the
|
|
|
|
// error result to the DOM API implementation in `PushManager.cpp` or
|
|
|
|
// `Push.js`, which will convert it to the correct `DOMException`.
|
|
|
|
throw errorWithResult(
|
|
|
|
"Invalid app server key",
|
|
|
|
Cr.NS_ERROR_DOM_PUSH_INVALID_KEY_ERR
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
keyPromise = Promise.resolve(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all([keyPromise, this._getByPageRecord(aPageRecord)]).then(
|
|
|
|
([appServerKey, record]) => {
|
|
|
|
aPageRecord.appServerKey = appServerKey;
|
|
|
|
if (!record) {
|
|
|
|
return this._lookupOrPutPendingRequest(aPageRecord);
|
|
|
|
}
|
|
|
|
if (!record.matchesAppServerKey(appServerKey)) {
|
|
|
|
throw errorWithResult(
|
|
|
|
"Mismatched app server key",
|
|
|
|
Cr.NS_ERROR_DOM_PUSH_MISMATCHED_KEY_ERR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (record.isExpired()) {
|
|
|
|
return record
|
|
|
|
.quotaChanged()
|
|
|
|
.then(isChanged => {
|
|
|
|
if (isChanged) {
|
|
|
|
// If the user revisited the site, drop the expired push
|
|
|
|
// registration and re-register.
|
|
|
|
return this.dropRegistrationAndNotifyApp(record.keyID);
|
|
|
|
}
|
|
|
|
throw new Error("Push subscription expired");
|
|
|
|
})
|
|
|
|
.then(_ => this._lookupOrPutPendingRequest(aPageRecord));
|
2019-07-05 11:44:55 +03:00
|
|
|
}
|
2015-11-11 01:27:47 +03:00
|
|
|
return record.toSubscription();
|
2016-03-16 12:53:13 +03:00
|
|
|
}
|
|
|
|
);
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
|
|
|
|
2018-04-11 17:53:20 +03:00
|
|
|
/*
|
|
|
|
* Called only by the PushBroadcastService on the receipt of a new
|
|
|
|
* subscription. Don't call this directly. Go through PushBroadcastService.
|
|
|
|
*/
|
|
|
|
async subscribeBroadcast(broadcastId, version) {
|
|
|
|
if (this._state != PUSH_SERVICE_RUNNING) {
|
|
|
|
// Ignore any request to subscribe before we send a hello.
|
|
|
|
// We'll send all the broadcast listeners as part of the hello
|
|
|
|
// anyhow.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this._service.sendSubscribeBroadcast(broadcastId, version);
|
|
|
|
},
|
|
|
|
|
2013-03-29 07:49:41 +04:00
|
|
|
/**
|
|
|
|
* Called on message from the child process.
|
|
|
|
*
|
|
|
|
* Why is the record being deleted from the local database before the server
|
|
|
|
* is told?
|
|
|
|
*
|
|
|
|
* Unregistration is for the benefit of the app and the AppServer
|
|
|
|
* so that the AppServer does not keep pinging a channel the UserAgent isn't
|
|
|
|
* watching The important part of the transaction in this case is left to the
|
|
|
|
* app, to tell its server of the unregistration. Even if the request to the
|
|
|
|
* PushServer were to fail, it would not affect correctness of the protocol,
|
2015-06-03 15:06:00 +03:00
|
|
|
* and the server GC would just clean up the channelID/subscription
|
|
|
|
* eventually. Since the appserver doesn't ping it, no data is lost.
|
2013-03-29 07:49:41 +04:00
|
|
|
*
|
|
|
|
* If rather we were to unregister at the server and update the database only
|
|
|
|
* on success: If the server receives the unregister, and deletes the
|
2015-06-03 15:06:00 +03:00
|
|
|
* channelID/subscription, but the response is lost because of network
|
|
|
|
* failure, the application is never informed. In addition the application may
|
|
|
|
* retry the unregister when it fails due to timeout (websocket) or any other
|
|
|
|
* reason at which point the server will say it does not know of this
|
|
|
|
* unregistration. We'll have to make the registration/unregistration phases
|
|
|
|
* have retries and attempts to resend messages from the server, and have the
|
|
|
|
* client acknowledge. On a server, data is cheap, reliable notification is
|
|
|
|
* not.
|
2013-03-29 07:49:41 +04:00
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
unregister(aPageRecord) {
|
2015-11-10 00:58:50 +03:00
|
|
|
console.debug("unregister()", aPageRecord);
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
return this._getByPageRecord(aPageRecord).then(record => {
|
2019-04-09 13:05:51 +03:00
|
|
|
if (record === null) {
|
2015-07-15 01:01:41 +03:00
|
|
|
return false;
|
2015-05-12 19:08:00 +03:00
|
|
|
}
|
2015-04-21 21:10:50 +03:00
|
|
|
|
2016-04-21 22:04:15 +03:00
|
|
|
let reason = Ci.nsIPushErrorReporter.UNSUBSCRIBE_MANUAL;
|
2015-06-03 15:06:00 +03:00
|
|
|
return Promise.all([
|
2016-04-21 22:04:15 +03:00
|
|
|
this._sendUnregister(record, reason),
|
2019-04-09 13:05:51 +03:00
|
|
|
this._db.delete(record.keyID).then(rec => {
|
|
|
|
if (rec) {
|
|
|
|
gPushNotifier.notifySubscriptionModified(rec.scope, rec.principal);
|
2016-06-08 16:13:15 +03:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
]).then(([success]) => success);
|
2015-05-12 19:08:00 +03:00
|
|
|
});
|
2015-04-21 21:10:50 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
clear(info) {
|
2015-07-29 21:33:48 +03:00
|
|
|
return this._checkActivated()
|
2016-05-20 19:38:26 +03:00
|
|
|
.then(_ => {
|
|
|
|
return this._dropRegistrationsIf(
|
|
|
|
record =>
|
|
|
|
info.domain == "*" ||
|
2018-06-14 14:31:14 +03:00
|
|
|
(record.uri &&
|
|
|
|
eTLDService.hasRootDomain(record.uri.prePath, info.domain))
|
2016-05-20 19:38:26 +03:00
|
|
|
);
|
|
|
|
})
|
2015-07-29 21:33:48 +03:00
|
|
|
.catch(e => {
|
2016-05-20 19:38:26 +03:00
|
|
|
console.warn(
|
|
|
|
"clear: Error dropping subscriptions for domain",
|
|
|
|
info.domain,
|
|
|
|
e
|
|
|
|
);
|
2015-06-03 15:05:00 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2015-04-21 21:10:51 +03:00
|
|
|
},
|
2015-05-12 19:08:00 +03:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
registration(aPageRecord) {
|
2015-11-10 00:58:50 +03:00
|
|
|
console.debug("registration()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
return this._getByPageRecord(aPageRecord).then(record => {
|
|
|
|
if (!record) {
|
2015-10-30 19:37:22 +03:00
|
|
|
return null;
|
2019-07-05 11:44:55 +03:00
|
|
|
}
|
2015-06-26 00:52:57 +03:00
|
|
|
if (record.isExpired()) {
|
|
|
|
return record.quotaChanged().then(isChanged => {
|
|
|
|
if (isChanged) {
|
|
|
|
return this.dropRegistrationAndNotifyApp(record.keyID).then(
|
2019-07-05 11:44:55 +03:00
|
|
|
_ => null
|
|
|
|
);
|
|
|
|
}
|
2015-06-03 15:05:00 +03:00
|
|
|
return null;
|
2015-10-30 19:37:22 +03:00
|
|
|
});
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
2015-11-11 01:27:47 +03:00
|
|
|
return record.toSubscription();
|
2015-06-03 15:05:00 +03:00
|
|
|
});
|
2015-04-19 17:20:24 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_dropExpiredRegistrations() {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("dropExpiredRegistrations()");
|
2015-06-26 00:52:57 +03:00
|
|
|
|
2015-10-06 18:14:25 +03:00
|
|
|
return this._db.getAllExpired().then(records => {
|
|
|
|
return Promise.all(
|
|
|
|
records.map(record =>
|
|
|
|
record
|
|
|
|
.quotaChanged()
|
|
|
|
.then(isChanged => {
|
|
|
|
if (isChanged) {
|
2015-06-26 00:52:57 +03:00
|
|
|
// If the user revisited the site, drop the expired push
|
|
|
|
// registration and notify the associated service worker.
|
2019-04-09 13:05:51 +03:00
|
|
|
this.dropRegistrationAndNotifyApp(record.keyID);
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error(
|
|
|
|
"dropExpiredRegistrations: Error dropping registration",
|
|
|
|
record.keyID,
|
|
|
|
error
|
|
|
|
);
|
2015-10-06 18:14:25 +03:00
|
|
|
})
|
2019-07-05 11:44:55 +03:00
|
|
|
)
|
2015-10-06 18:14:25 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_onPermissionChange(subject, data) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("onPermissionChange()");
|
2015-10-06 18:14:25 +03:00
|
|
|
|
|
|
|
if (data == "cleared") {
|
2016-05-02 18:20:14 +03:00
|
|
|
return this._clearPermissions();
|
2015-10-06 18:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let permission = subject.QueryInterface(Ci.nsIPermission);
|
2015-10-31 04:16:19 +03:00
|
|
|
if (permission.type != "desktop-notification") {
|
2015-10-06 18:14:25 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._updatePermission(permission, data);
|
|
|
|
},
|
|
|
|
|
2016-05-02 18:20:14 +03:00
|
|
|
_clearPermissions() {
|
|
|
|
console.debug("clearPermissions()");
|
|
|
|
|
|
|
|
return this._db.clearIf(record => {
|
|
|
|
if (!record.quotaApplies()) {
|
|
|
|
// Only drop registrations that are subject to quota.
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-20 19:38:26 +03:00
|
|
|
this._backgroundUnregister(
|
|
|
|
record,
|
|
|
|
Ci.nsIPushErrorReporter.UNSUBSCRIBE_PERMISSION_REVOKED
|
|
|
|
);
|
2016-05-02 18:20:14 +03:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_updatePermission(permission, type) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("updatePermission()");
|
2015-10-06 18:14:25 +03:00
|
|
|
|
|
|
|
let isAllow = permission.capability == Ci.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
let isChange = type == "added" || type == "changed";
|
|
|
|
|
|
|
|
if (isAllow && isChange) {
|
|
|
|
// Permission set to "allow". Drop all expired registrations for this
|
|
|
|
// site, notify the associated service workers, and reset the quota
|
|
|
|
// for active registrations.
|
2016-05-05 19:12:35 +03:00
|
|
|
return this._forEachPrincipal(permission.principal, (record, cursor) =>
|
|
|
|
this._permissionAllowed(record, cursor)
|
|
|
|
);
|
2015-10-06 18:14:25 +03:00
|
|
|
} else if (isChange || (isAllow && type == "deleted")) {
|
|
|
|
// Permission set to "block" or "always ask," or "allow" permission
|
|
|
|
// removed. Expire all registrations for this site.
|
2016-05-05 19:12:35 +03:00
|
|
|
return this._forEachPrincipal(permission.principal, (record, cursor) =>
|
|
|
|
this._permissionDenied(record, cursor)
|
2015-10-31 04:15:48 +03:00
|
|
|
);
|
2015-10-06 18:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_forEachPrincipal(principal, callback) {
|
2016-05-05 19:12:35 +03:00
|
|
|
return this._db.forEachOrigin(
|
2015-10-06 18:14:25 +03:00
|
|
|
principal.URI.prePath,
|
2015-10-31 04:15:48 +03:00
|
|
|
ChromeUtils.originAttributesToSuffix(principal.originAttributes),
|
2016-05-05 19:12:35 +03:00
|
|
|
callback
|
2015-10-06 18:14:25 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-11-11 01:27:47 +03:00
|
|
|
* The update function called for each registration record if the push
|
|
|
|
* permission is revoked. We only expire the record so we can notify the
|
|
|
|
* service worker as soon as the permission is reinstated. If we just
|
|
|
|
* deleted the record, the worker wouldn't be notified until the next visit
|
|
|
|
* to the site.
|
2015-10-06 18:14:25 +03:00
|
|
|
*
|
2015-11-11 01:27:47 +03:00
|
|
|
* @param {PushRecord} record The record to expire.
|
|
|
|
* @param {IDBCursor} cursor The IndexedDB cursor.
|
2015-10-06 18:14:25 +03:00
|
|
|
*/
|
2019-04-09 13:05:51 +03:00
|
|
|
_permissionDenied(record, cursor) {
|
2015-11-11 01:27:47 +03:00
|
|
|
console.debug("permissionDenied()");
|
|
|
|
|
2015-10-31 04:15:48 +03:00
|
|
|
if (!record.quotaApplies() || record.isExpired()) {
|
2015-10-06 18:14:25 +03:00
|
|
|
// Ignore already-expired records.
|
2015-11-11 01:27:47 +03:00
|
|
|
return;
|
2015-10-31 04:15:48 +03:00
|
|
|
}
|
|
|
|
// Drop the registration in the background.
|
2016-03-28 22:29:25 +03:00
|
|
|
this._backgroundUnregister(
|
|
|
|
record,
|
|
|
|
Ci.nsIPushErrorReporter.UNSUBSCRIBE_PERMISSION_REVOKED
|
|
|
|
);
|
2015-10-31 04:15:48 +03:00
|
|
|
record.setQuota(0);
|
2015-11-11 01:27:47 +03:00
|
|
|
cursor.update(record);
|
2015-10-06 18:14:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-11-11 01:27:47 +03:00
|
|
|
* The update function called for each registration record if the push
|
|
|
|
* permission is granted. If the record has expired, it will be dropped;
|
|
|
|
* otherwise, its quota will be reset to the default value.
|
2015-10-06 18:14:25 +03:00
|
|
|
*
|
2015-11-11 01:27:47 +03:00
|
|
|
* @param {PushRecord} record The record to update.
|
|
|
|
* @param {IDBCursor} cursor The IndexedDB cursor.
|
2015-10-06 18:14:25 +03:00
|
|
|
*/
|
2016-05-05 19:12:35 +03:00
|
|
|
_permissionAllowed(record, cursor) {
|
2015-11-11 01:27:47 +03:00
|
|
|
console.debug("permissionAllowed()");
|
|
|
|
|
2015-10-31 04:15:48 +03:00
|
|
|
if (!record.quotaApplies()) {
|
2015-11-11 01:27:47 +03:00
|
|
|
return;
|
2015-10-31 04:15:48 +03:00
|
|
|
}
|
|
|
|
if (record.isExpired()) {
|
|
|
|
// If the registration has expired, drop and notify the worker
|
|
|
|
// unconditionally.
|
2016-05-05 19:12:35 +03:00
|
|
|
this._notifySubscriptionChangeObservers(record);
|
2015-11-11 01:27:47 +03:00
|
|
|
cursor.delete();
|
|
|
|
return;
|
2015-10-31 04:15:48 +03:00
|
|
|
}
|
|
|
|
record.resetQuota();
|
2015-11-11 01:27:47 +03:00
|
|
|
cursor.update(record);
|
2015-06-26 00:52:57 +03:00
|
|
|
},
|
2016-05-20 19:38:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Drops all matching registrations from the database. Notifies the
|
|
|
|
* associated service workers if permission is granted, and removes
|
|
|
|
* unexpired registrations from the server.
|
|
|
|
*
|
|
|
|
* @param {Function} predicate A function called for each record.
|
|
|
|
* @returns {Promise} Resolves once the registrations have been dropped.
|
|
|
|
*/
|
|
|
|
_dropRegistrationsIf(predicate) {
|
|
|
|
return this._db.clearIf(record => {
|
|
|
|
if (!predicate(record)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (record.hasPermission()) {
|
|
|
|
// "Clear Recent History" and the Forget button remove permissions
|
|
|
|
// before clearing registrations, but it's possible for the worker to
|
|
|
|
// resubscribe if the "dom.push.testing.ignorePermission" pref is set.
|
|
|
|
this._notifySubscriptionChangeObservers(record);
|
|
|
|
}
|
|
|
|
if (!record.isExpired()) {
|
|
|
|
// Only unregister active registrations, since we already told the
|
|
|
|
// server about expired ones.
|
|
|
|
this._backgroundUnregister(
|
|
|
|
record,
|
|
|
|
Ci.nsIPushErrorReporter.UNSUBSCRIBE_MANUAL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
},
|
2015-04-21 21:10:50 +03:00
|
|
|
};
|