2014-07-09 12:37:58 +04:00
|
|
|
/* jshint moz: true, esnext: true */
|
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";
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2013-03-29 07:49:41 +04:00
|
|
|
const Cr = Components.results;
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
const {PushDB} = Cu.import("resource://gre/modules/PushDB.jsm");
|
2013-03-29 07:49:41 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Timer.jsm");
|
2013-04-15 23:45:37 +04:00
|
|
|
Cu.import("resource://gre/modules/Preferences.jsm");
|
2014-03-18 18:16:02 +04:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2013-06-13 04:26:44 +04:00
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
const {PushServiceWebSocket} = Cu.import("resource://gre/modules/PushServiceWebSocket.jsm");
|
2015-06-03 15:06:00 +03:00
|
|
|
const {PushServiceHttp2} = Cu.import("resource://gre/modules/PushServiceHttp2.jsm");
|
2015-09-17 15:08:50 +03:00
|
|
|
const {PushCrypto} = Cu.import("resource://gre/modules/PushCrypto.jsm");
|
2014-06-09 08:32:00 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
// Currently supported protocols: WebSocket.
|
2015-06-03 15:06:00 +03:00
|
|
|
const CONNECTION_PROTOCOLS = [PushServiceWebSocket, PushServiceHttp2];
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2013-06-13 04:26:44 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "AlarmService",
|
|
|
|
"resource://gre/modules/AlarmService.jsm");
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-11-10 21:50:46 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gContentSecurityManager",
|
|
|
|
"@mozilla.org/contentsecuritymanager;1",
|
|
|
|
"nsIContentSecurityManager");
|
|
|
|
|
2013-05-02 09:15:18 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["PushService"];
|
|
|
|
|
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: "PushService",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-11 06:19:28 +03:00
|
|
|
const prefs = new Preferences("dom.push.");
|
2013-04-09 22:02:00 +04:00
|
|
|
|
2013-03-29 07:49:41 +04:00
|
|
|
const kCHILD_PROCESS_MESSAGES = ["Push:Register", "Push:Unregister",
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
"Push:Registration", "Push:RegisterEventNotificationListener",
|
2015-11-17 08:33:11 +03:00
|
|
|
"Push:NotificationForOriginShown",
|
|
|
|
"Push:NotificationForOriginClosed",
|
2015-09-19 04:15:23 +03:00
|
|
|
"child-process-shutdown"];
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
const PUSH_SERVICE_UNINIT = 0;
|
|
|
|
const PUSH_SERVICE_INIT = 1; // No serverURI
|
|
|
|
const PUSH_SERVICE_ACTIVATING = 2;//activating db
|
|
|
|
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-09-16 20:28:17 +03:00
|
|
|
// Telemetry failure to send push notification to Service Worker reasons.
|
|
|
|
// Key not found in local database.
|
|
|
|
const kDROP_NOTIFICATION_REASON_KEY_NOT_FOUND = 0;
|
|
|
|
// User cleared history.
|
|
|
|
const kDROP_NOTIFICATION_REASON_NO_HISTORY = 1;
|
|
|
|
// Version of message received not newer than previous one.
|
|
|
|
const kDROP_NOTIFICATION_REASON_NO_VERSION_INCREMENT = 2;
|
|
|
|
// Subscription has expired.
|
|
|
|
const kDROP_NOTIFICATION_REASON_EXPIRED = 3;
|
|
|
|
|
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
|
|
|
|
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
|
|
|
*/
|
|
|
|
this.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-06-26 00:52:57 +03:00
|
|
|
_alarmID: 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
|
|
|
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
_childListeners: [],
|
|
|
|
|
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,
|
|
|
|
_stateChangeProcessEnqueue: function(op) {
|
|
|
|
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
|
2015-06-03 15:05:00 +03:00
|
|
|
.then(op)
|
|
|
|
.catch(_ => {});
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
_checkActivated: function() {
|
|
|
|
if (this._state < PUSH_SERVICE_ACTIVATING) {
|
2015-11-10 00:58:50 +03:00
|
|
|
return Promise.reject(new Error("Push service not active"));
|
2015-06-03 15:05:00 +03:00
|
|
|
} else if (this._state > PUSH_SERVICE_ACTIVATING) {
|
|
|
|
return Promise.resolve();
|
|
|
|
} else {
|
|
|
|
return (this._activated) ? this._activated :
|
|
|
|
this._activated = new Promise((res, rej) =>
|
|
|
|
this._notifyActivated = {resolve: res,
|
|
|
|
reject: rej});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
_makePendingKey: function(aPageRecord) {
|
|
|
|
return aPageRecord.scope + "|" + aPageRecord.originAttributes;
|
|
|
|
},
|
|
|
|
|
|
|
|
_lookupOrPutPendingRequest: function(aPageRecord) {
|
|
|
|
let key = this._makePendingKey(aPageRecord);
|
|
|
|
if (this._pendingRegisterRequest[key]) {
|
|
|
|
return this._pendingRegisterRequest[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._pendingRegisterRequest[key] = this._registerWithServer(aPageRecord);
|
|
|
|
},
|
|
|
|
|
|
|
|
_deletePendingRequest: function(aPageRecord) {
|
|
|
|
let key = this._makePendingKey(aPageRecord);
|
|
|
|
if (this._pendingRegisterRequest[key]) {
|
|
|
|
delete this._pendingRegisterRequest[key];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
_setState: function(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
|
|
|
|
this._state = aNewState;
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
_changeStateOfflineEvent: function(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) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offline) {
|
|
|
|
if (this._state == PUSH_SERVICE_RUNNING) {
|
|
|
|
this._service.disconnect();
|
|
|
|
}
|
|
|
|
this._setState(PUSH_SERVICE_ACTIVE_OFFLINE);
|
|
|
|
} else {
|
|
|
|
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();
|
|
|
|
}
|
2015-06-26 00:52:57 +03:00
|
|
|
this._db.getAllUnexpired()
|
|
|
|
.then(records => {
|
|
|
|
if (records.length > 0) {
|
2015-06-03 15:05:00 +03:00
|
|
|
// if there are request waiting
|
2015-06-26 00:52:57 +03:00
|
|
|
this._service.connect(records);
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this._setState(PUSH_SERVICE_RUNNING);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_changeStateConnectionEnabledEvent: function(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) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
this._changeStateOfflineEvent(Services.io.offline, true);
|
|
|
|
} else {
|
|
|
|
if (this._state == PUSH_SERVICE_RUNNING) {
|
|
|
|
this._service.disconnect();
|
|
|
|
}
|
|
|
|
this._setState(PUSH_SERVICE_CONNECTION_DISABLE);
|
|
|
|
}
|
|
|
|
},
|
2015-06-03 15:04:00 +03:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
case "xpcom-shutdown":
|
|
|
|
this.uninit();
|
2015-04-21 21:10:50 +03:00
|
|
|
break;
|
2013-06-13 01:47:42 +04:00
|
|
|
case "network-active-changed": /* On B2G. */
|
|
|
|
case "network:offline-status-changed": /* On desktop. */
|
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)
|
|
|
|
);
|
|
|
|
|
2015-04-11 06:19:28 +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"))
|
|
|
|
);
|
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":
|
|
|
|
this._dropExpiredRegistrations();
|
|
|
|
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);
|
|
|
|
})
|
|
|
|
break;
|
|
|
|
|
2013-09-20 22:17:40 +04:00
|
|
|
case "webapps-clear-data":
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("webapps-clear-data");
|
2013-09-20 22:17:40 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
let data = aSubject
|
|
|
|
.QueryInterface(Ci.mozIApplicationClearPrivateDataParams);
|
2013-09-20 22:17:40 +04:00
|
|
|
if (!data) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("webapps-clear-data: Failed to get information " +
|
|
|
|
"about application");
|
2013-07-24 17:03:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
var originAttributes =
|
|
|
|
ChromeUtils.originAttributesToSuffix({ appId: data.appId,
|
|
|
|
inBrowser: data.browserOnly });
|
|
|
|
this._db.getAllByOriginAttributes(originAttributes)
|
2015-10-06 18:14:25 +03:00
|
|
|
.then(records => Promise.all(records.map(record =>
|
2015-11-11 01:27:47 +03:00
|
|
|
this._db.delete(record.keyID)
|
|
|
|
.catch(err => {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("webapps-clear-data: Error removing record",
|
|
|
|
record, err);
|
2015-11-11 01:27:47 +03:00
|
|
|
// This is the record we were unable to delete.
|
|
|
|
return record;
|
2015-10-06 18:14:25 +03:00
|
|
|
})
|
2015-11-11 01:27:47 +03:00
|
|
|
.then(maybeDeleted => this._backgroundUnregister(maybeDeleted))
|
2015-10-06 18:14:25 +03:00
|
|
|
)
|
|
|
|
));
|
2013-04-05 00:42:35 +04:00
|
|
|
|
2013-05-02 09:15:18 +04:00
|
|
|
break;
|
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.
|
|
|
|
*/
|
2015-11-10 00:58:32 +03:00
|
|
|
_backgroundUnregister: function(record) {
|
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);
|
|
|
|
this._sendUnregister(record).catch(e => {
|
|
|
|
console.error("backgroundUnregister: Error notifying server", e);
|
|
|
|
});
|
2015-10-06 18:14:25 +03:00
|
|
|
},
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
// utility function used to add/remove observers in startObservers() and
|
|
|
|
// stopObservers()
|
2015-06-03 15:04:00 +03:00
|
|
|
getNetworkStateChangeEventName: function() {
|
|
|
|
try {
|
|
|
|
Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
|
|
|
|
return "network-active-changed";
|
|
|
|
} catch (e) {
|
|
|
|
return "network:offline-status-changed";
|
2013-09-25 17:20:06 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-10 21:50:46 +03:00
|
|
|
_findService: function(serverURL) {
|
|
|
|
console.debug("findService()");
|
|
|
|
|
|
|
|
let uri;
|
|
|
|
let service;
|
|
|
|
|
|
|
|
if (!serverURL) {
|
|
|
|
console.warn("findService: No dom.push.serverURL found");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
uri = Services.io.newURI(serverURL, null, null);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("findService: Error creating valid URI from",
|
|
|
|
"dom.push.serverURL", serverURL);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gContentSecurityManager.isURIPotentiallyTrustworthy(uri)) {
|
|
|
|
console.warn("findService: Untrusted server URI", uri.spec);
|
|
|
|
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];
|
|
|
|
},
|
|
|
|
|
|
|
|
_changeServerURL: function(serverURI, event) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("changeServerURL()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
|
|
|
switch(event) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
return this._startService(service, uri, event)
|
2015-10-20 02:33:00 +03:00
|
|
|
.then(_ => this._stateChangeProcessEnqueue(_ =>
|
|
|
|
this._changeStateConnectionEnabledEvent(prefs.get("connection.enabled")))
|
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.
|
|
|
|
return this._startService(service, uri, STARTING_SERVICE_EVENT)
|
2015-10-20 02:33:00 +03:00
|
|
|
.then(_ => this._stateChangeProcessEnqueue(_ =>
|
|
|
|
this._changeStateConnectionEnabledEvent(prefs.get("connection.enabled")))
|
2015-06-03 15:05:00 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
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, CHANGING_SERVICE_EVENT)
|
|
|
|
)
|
2015-10-20 02:33:00 +03:00
|
|
|
.then(_ => this._stateChangeProcessEnqueue(_ =>
|
|
|
|
this._changeStateConnectionEnabledEvent(prefs.get("connection.enabled")))
|
2015-06-03 15:05:00 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this._state == PUSH_SERVICE_INIT) {
|
|
|
|
return Promise.resolve();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// The new serverUri is empty or misconfigured - stop service.
|
|
|
|
this._setState(PUSH_SERVICE_INIT);
|
|
|
|
return this._stopService(STOPPING_SERVICE_EVENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PushService initialization is divided into 4 parts:
|
|
|
|
* init() - start listening for xpcom-shutdown and serverURL changes.
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-04-21 21:10:50 +03:00
|
|
|
init: function(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);
|
|
|
|
|
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
|
|
|
|
if (options.serverURI) {
|
|
|
|
// this is use for xpcshell test.
|
|
|
|
|
2015-11-10 21:50:46 +03:00
|
|
|
let [service, uri] = this._findService(options.serverURI);
|
2015-06-03 15:05:00 +03:00
|
|
|
if (!service) {
|
|
|
|
this._setState(PUSH_SERVICE_INIT);
|
|
|
|
return;
|
|
|
|
}
|
2015-04-11 06:19:28 +03:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
// Start service.
|
2015-06-26 00:52:57 +03:00
|
|
|
this._startService(service, uri, false, options).then(_ => {
|
|
|
|
// Before completing the activation check prefs. This will first check
|
|
|
|
// connection.enabled pref and then check offline state.
|
|
|
|
this._changeStateConnectionEnabledEvent(prefs.get("connection.enabled"));
|
|
|
|
});
|
2015-04-11 06:19:28 +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.
|
|
|
|
prefs.observe("serverURL", this);
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-10-20 02:33:00 +03:00
|
|
|
this._stateChangeProcessEnqueue(_ =>
|
2015-06-03 15:05:00 +03:00
|
|
|
this._changeServerURL(prefs.get("serverURL"), STARTING_SERVICE_EVENT));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_startObservers: function() {
|
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
|
|
|
|
2013-09-20 22:17:40 +04:00
|
|
|
Services.obs.addObserver(this, "webapps-clear-data", false);
|
2013-06-13 04:26:44 +04:00
|
|
|
|
|
|
|
// On B2G the NetworkManager interface fires a network-active-changed
|
|
|
|
// event.
|
|
|
|
//
|
|
|
|
// The "active network" is based on priority - i.e. Wi-Fi has higher
|
|
|
|
// priority than data. The PushService should just use the preferred
|
|
|
|
// network, and not care about all interface changes.
|
|
|
|
// network-active-changed is not fired when the network goes offline, but
|
|
|
|
// socket connections time out. The check for Services.io.offline in
|
2015-06-03 15:05:00 +03:00
|
|
|
// PushServiceWebSocket._beginWSSetup() prevents unnecessary retries. When
|
|
|
|
// the network comes back online, network-active-changed is fired.
|
2013-06-13 04:26:44 +04:00
|
|
|
//
|
|
|
|
// On non-B2G platforms, the offline-status-changed event is used to know
|
|
|
|
// when to (dis)connect. It may not fire if the underlying OS changes
|
|
|
|
// networks; in such a case we rely on timeout.
|
|
|
|
//
|
|
|
|
// On B2G both events fire, one after the other, when the network goes
|
|
|
|
// online, so we explicitly check for the presence of NetworkManager and
|
|
|
|
// don't add an observer for offline-status-changed on B2G.
|
2015-06-03 15:04:00 +03:00
|
|
|
this._networkStateChangeEventName = this.getNetworkStateChangeEventName();
|
2015-04-21 21:10:50 +03:00
|
|
|
Services.obs.addObserver(this, this._networkStateChangeEventName, false);
|
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.
|
2015-06-26 00:52:57 +03:00
|
|
|
Services.obs.addObserver(this, "idle-daily", false);
|
2015-10-06 18:14:25 +03:00
|
|
|
|
|
|
|
// Prunes registrations for sites for which the user revokes push
|
|
|
|
// permissions.
|
|
|
|
Services.obs.addObserver(this, "perm-changed", false);
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_startService: function(service, serverURI, event, 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) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event != CHANGING_SERVICE_EVENT) {
|
|
|
|
// if only serverURL is changed we can keep listening for broadcast
|
|
|
|
// messages and queue them.
|
|
|
|
let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageBroadcaster);
|
|
|
|
|
|
|
|
kCHILD_PROCESS_MESSAGES.forEach(msgName =>
|
|
|
|
ppmm.addMessageListener(msgName, this)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
this._service.init(options, this, serverURI);
|
|
|
|
this._startObservers();
|
2015-10-06 18:14:25 +03:00
|
|
|
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.
|
|
|
|
* uninit() - stop listening for xpcom-shutdown and serverURL changes.
|
|
|
|
* state is change to PUSH_SERVICE_UNINIT
|
|
|
|
*/
|
|
|
|
_stopService: function(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) {
|
2013-06-13 04:26:44 +04:00
|
|
|
return;
|
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.stopAlarm();
|
|
|
|
this._stopObservers();
|
2013-05-02 09:15:18 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
if (event != CHANGING_SERVICE_EVENT) {
|
|
|
|
let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageBroadcaster);
|
2013-05-02 09:15:18 +04:00
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
kCHILD_PROCESS_MESSAGES.forEach(
|
|
|
|
msgName => ppmm.removeMessageListener(msgName, this)
|
|
|
|
);
|
|
|
|
}
|
2015-06-03 15:06:00 +03: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;
|
|
|
|
this.stopAlarm();
|
2013-05-02 09:15:18 +04:00
|
|
|
|
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()
|
2015-06-03 15:05:00 +03:00
|
|
|
.then(_ => {
|
|
|
|
this._db.close();
|
|
|
|
this._db = null;
|
|
|
|
}, err => {
|
|
|
|
this._db.close();
|
|
|
|
this._db = null;
|
|
|
|
});
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
_stopObservers: function() {
|
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);
|
|
|
|
|
|
|
|
Services.obs.removeObserver(this, this._networkStateChangeEventName);
|
2015-06-26 00:52:57 +03:00
|
|
|
Services.obs.removeObserver(this, "webapps-clear-data");
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function() {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("uninit()");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
this._childListeners = [];
|
|
|
|
|
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
|
|
|
|
|
|
|
this._setState(PUSH_SERVICE_UNINIT);
|
|
|
|
|
|
|
|
prefs.ignore("serverURL", this);
|
2015-06-26 00:52:57 +03:00
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
2015-06-03 15:05:00 +03:00
|
|
|
|
2015-10-20 02:33:00 +03:00
|
|
|
this._stateChangeProcessEnqueue(_ =>
|
2015-06-03 15:05:00 +03:00
|
|
|
this._changeServerURL("", UNINIT_EVENT));
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("uninit: shutdown complete!");
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
/** |delay| should be in milliseconds. */
|
|
|
|
setAlarm: function(delay) {
|
|
|
|
// Bug 909270: Since calls to AlarmService.add() are async, calls must be
|
|
|
|
// 'queued' to ensure only one alarm is ever active.
|
|
|
|
if (this._settingAlarm) {
|
|
|
|
// onSuccess will handle the set. Overwriting the variable enforces the
|
|
|
|
// last-writer-wins semantics.
|
|
|
|
this._queuedAlarmDelay = delay;
|
|
|
|
this._waitingForAlarmSet = true;
|
2015-04-21 21:10:50 +03:00
|
|
|
return;
|
2013-03-29 07:49:41 +04:00
|
|
|
}
|
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
// Stop any existing alarm.
|
|
|
|
this.stopAlarm();
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
this._settingAlarm = true;
|
|
|
|
AlarmService.add(
|
|
|
|
{
|
|
|
|
date: new Date(Date.now() + delay),
|
|
|
|
ignoreTimezone: true
|
|
|
|
},
|
2015-06-03 15:05:00 +03:00
|
|
|
() => {
|
2015-06-03 15:06:00 +03:00
|
|
|
if (this._state > PUSH_SERVICE_ACTIVATING) {
|
2015-06-03 15:05:00 +03:00
|
|
|
this._service.onAlarmFired();
|
|
|
|
}
|
|
|
|
}, (alarmID) => {
|
2015-06-03 15:04:00 +03:00
|
|
|
this._alarmID = alarmID;
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("setAlarm: Set alarm", delay, "in the future",
|
|
|
|
this._alarmID);
|
2015-06-03 15:04:00 +03:00
|
|
|
this._settingAlarm = false;
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
if (this._waitingForAlarmSet) {
|
|
|
|
this._waitingForAlarmSet = false;
|
|
|
|
this.setAlarm(this._queuedAlarmDelay);
|
|
|
|
}
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
);
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
stopAlarm: function() {
|
|
|
|
if (this._alarmID !== null) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("stopAlarm: Stopped existing alarm", this._alarmID);
|
2015-06-03 15:04:00 +03:00
|
|
|
AlarmService.remove(this._alarmID);
|
|
|
|
this._alarmID = null;
|
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.
|
|
|
|
*/
|
|
|
|
dropUnexpiredRegistrations: function() {
|
|
|
|
let subscriptionChanges = [];
|
|
|
|
return this._db.clearIf(record => {
|
|
|
|
if (record.isExpired()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
subscriptionChanges.push(record);
|
|
|
|
return true;
|
|
|
|
}).then(() => {
|
|
|
|
this.notifySubscriptionChanges(subscriptionChanges);
|
|
|
|
});
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
_notifySubscriptionChangeObservers: function(record) {
|
2015-11-11 01:27:47 +03:00
|
|
|
if (!record) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
// Notify XPCOM observers.
|
|
|
|
Services.obs.notifyObservers(
|
|
|
|
null,
|
|
|
|
"push-subscription-change",
|
|
|
|
record.scope
|
|
|
|
);
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
originAttributes: record.originAttributes,
|
|
|
|
scope: record.scope
|
|
|
|
};
|
|
|
|
|
2015-09-16 20:28:17 +03:00
|
|
|
Services.telemetry.getHistogramById("PUSH_API_NOTIFY_REGISTRATION_LOST").add();
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
this._notifyListeners('pushsubscriptionchange', data);
|
|
|
|
},
|
|
|
|
|
|
|
|
_notifyListeners: function(name, data) {
|
|
|
|
if (this._childListeners.length > 0) {
|
|
|
|
// Try to send messages to all listeners, but remove any that fail since
|
|
|
|
// the receiver is likely gone away.
|
|
|
|
for (var i = this._childListeners.length - 1; i >= 0; --i) {
|
|
|
|
try {
|
|
|
|
this._childListeners[i].sendAsyncMessage(name, data);
|
|
|
|
} catch(e) {
|
|
|
|
this._childListeners.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let ppmm = Cc['@mozilla.org/parentprocessmessagemanager;1']
|
|
|
|
.getService(Ci.nsIMessageListenerManager);
|
|
|
|
ppmm.broadcastAsyncMessage(name, data);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
dropRegistrationAndNotifyApp: function(aKeyID) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
updateRegistrationAndNotifyApp: function(aOldKey, aNewRecord) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
updateRecordAndNotifyApp: function(aKeyID, aUpdateFunc) {
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
notifySubscriptionChanges: function(records) {
|
|
|
|
records.forEach(record => {
|
2015-06-26 00:52:57 +03:00
|
|
|
this._notifySubscriptionChangeObservers(record);
|
|
|
|
});
|
2015-06-03 15:06:00 +03:00
|
|
|
},
|
|
|
|
|
2015-09-17 15:08:50 +03:00
|
|
|
ensureP256dhKey: function(record) {
|
|
|
|
if (record.p256dhPublicKey && record.p256dhPrivateKey) {
|
|
|
|
return Promise.resolve(record);
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
return PushCrypto.generateKeys()
|
|
|
|
.then(exportedKeys => {
|
|
|
|
return this.updateRecordAndNotifyApp(record.keyID, record => {
|
|
|
|
record.p256dhPublicKey = exportedKeys[0];
|
|
|
|
record.p256dhPrivateKey = exportedKeys[1];
|
|
|
|
return record;
|
|
|
|
});
|
|
|
|
}, error => {
|
|
|
|
return this.dropRegistrationAndNotifyApp(record.keyID).then(
|
|
|
|
() => Promise.reject(error));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-09-16 20:28:17 +03:00
|
|
|
_recordDidNotNotify: function(reason) {
|
|
|
|
Services.telemetry.
|
|
|
|
getHistogramById("PUSH_API_NOTIFICATION_RECEIVED_BUT_DID_NOT_NOTIFY").
|
|
|
|
add(reason);
|
|
|
|
},
|
|
|
|
|
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.
|
|
|
|
* @param {String} message The message contents.
|
2015-09-17 15:08:50 +03:00
|
|
|
* @param {Object} cryptoParams The message encryption settings.
|
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.
|
|
|
|
*/
|
2015-09-17 15:08:50 +03:00
|
|
|
receivedPushMessage: function(keyID, message, cryptoParams, updateFunc) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receivedPushMessage()");
|
2015-09-16 20:28:17 +03:00
|
|
|
Services.telemetry.getHistogramById("PUSH_API_NOTIFICATION_RECEIVED").add();
|
2015-06-26 00:52:57 +03:00
|
|
|
|
|
|
|
let shouldNotify = false;
|
2015-09-11 17:51:32 +03:00
|
|
|
return this.getByKeyID(keyID).then(record => {
|
2015-06-26 00:52:57 +03:00
|
|
|
if (!record) {
|
2015-09-16 20:28:17 +03:00
|
|
|
this._recordDidNotNotify(kDROP_NOTIFICATION_REASON_KEY_NOT_FOUND);
|
2015-06-26 00:52:57 +03:00
|
|
|
throw new Error("No record for key ID " + keyID);
|
|
|
|
}
|
|
|
|
return record.getLastVisit();
|
|
|
|
}).then(lastVisit => {
|
|
|
|
// As a special case, don't notify the service worker if the user
|
|
|
|
// cleared their history.
|
|
|
|
shouldNotify = isFinite(lastVisit);
|
2015-09-16 20:28:17 +03:00
|
|
|
if (!shouldNotify) {
|
|
|
|
this._recordDidNotNotify(kDROP_NOTIFICATION_REASON_NO_HISTORY);
|
|
|
|
}
|
2015-06-26 00:52:57 +03:00
|
|
|
return this._db.update(keyID, record => {
|
|
|
|
let newRecord = updateFunc(record);
|
|
|
|
if (!newRecord) {
|
2015-09-16 20:28:17 +03:00
|
|
|
this._recordDidNotNotify(kDROP_NOTIFICATION_REASON_NO_VERSION_INCREMENT);
|
2015-06-26 00:52:57 +03:00
|
|
|
return null;
|
|
|
|
}
|
2015-09-17 15:08:50 +03:00
|
|
|
// 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.
|
2015-06-26 00:52:57 +03:00
|
|
|
if (newRecord.isExpired()) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("receivedPushMessage: Ignoring update for expired key ID",
|
|
|
|
keyID);
|
2015-06-26 00:52:57 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
newRecord.receivedPush(lastVisit);
|
|
|
|
return newRecord;
|
|
|
|
});
|
|
|
|
}).then(record => {
|
2015-09-11 17:51:32 +03:00
|
|
|
var notified = false;
|
2015-06-26 00:52:57 +03:00
|
|
|
if (!record) {
|
2015-09-11 17:51:32 +03:00
|
|
|
return notified;
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
2015-09-17 15:08:50 +03:00
|
|
|
let decodedPromise;
|
|
|
|
if (cryptoParams) {
|
|
|
|
decodedPromise = PushCrypto.decodeMsg(
|
|
|
|
message,
|
|
|
|
record.p256dhPrivateKey,
|
|
|
|
cryptoParams.dh,
|
|
|
|
cryptoParams.salt,
|
|
|
|
cryptoParams.rs
|
2015-09-17 15:10:42 +03:00
|
|
|
);
|
2015-09-17 15:08:50 +03:00
|
|
|
} else {
|
2015-09-17 15:10:42 +03:00
|
|
|
decodedPromise = Promise.resolve(null);
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
2015-09-17 15:08:50 +03:00
|
|
|
return decodedPromise.then(message => {
|
|
|
|
if (shouldNotify) {
|
|
|
|
notified = this._notifyApp(record, message);
|
|
|
|
}
|
2015-11-17 08:33:11 +03:00
|
|
|
// Update quota after the delay, at which point
|
|
|
|
// we check for visible notifications.
|
|
|
|
setTimeout(() => this._updateQuota(keyID),
|
|
|
|
prefs.get("quotaUpdateDelay"));
|
2015-09-17 15:08:50 +03:00
|
|
|
return notified;
|
|
|
|
});
|
2015-06-26 00:52:57 +03:00
|
|
|
}).catch(error => {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("receivedPushMessage: Error notifying app", error);
|
2015-06-26 00:52:57 +03:00
|
|
|
});
|
2015-06-03 15:05:00 +03:00
|
|
|
},
|
|
|
|
|
2015-11-17 08:33:11 +03:00
|
|
|
_updateQuota: function(keyID) {
|
|
|
|
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.
|
|
|
|
if (!this._visibleNotifications.has(record.uri.prePath)) {
|
|
|
|
record.reduceQuota();
|
|
|
|
}
|
|
|
|
return record;
|
|
|
|
}).then(record => {
|
|
|
|
if (record && record.isExpired()) {
|
|
|
|
this._recordDidNotNotify(kDROP_NOTIFICATION_REASON_EXPIRED);
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_notificationForOriginShown(origin) {
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
_notificationForOriginClosed(origin) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
_notifyApp: function(aPushRecord, 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);
|
2015-09-11 17:51:32 +03:00
|
|
|
return false;
|
2013-03-29 07:49:41 +04:00
|
|
|
}
|
|
|
|
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("notifyApp()", aPushRecord.scope);
|
2015-04-21 21:10:50 +03:00
|
|
|
// Notify XPCOM observers.
|
|
|
|
let notification = Cc["@mozilla.org/push/ObserverNotification;1"]
|
|
|
|
.createInstance(Ci.nsIPushObserverNotification);
|
|
|
|
notification.pushEndpoint = aPushRecord.pushEndpoint;
|
|
|
|
notification.version = aPushRecord.version;
|
2015-09-17 15:10:42 +03:00
|
|
|
|
|
|
|
let payload = ArrayBuffer.isView(message) ?
|
|
|
|
new Uint8Array(message.buffer) : message;
|
|
|
|
if (payload) {
|
|
|
|
notification.data = "";
|
|
|
|
for (let i = 0; i < payload.length; i++) {
|
|
|
|
notification.data += String.fromCharCode(payload[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 16:44:00 +03:00
|
|
|
notification.lastPush = aPushRecord.lastPush;
|
|
|
|
notification.pushCount = aPushRecord.pushCount;
|
|
|
|
|
2015-04-21 21:10:50 +03:00
|
|
|
Services.obs.notifyObservers(
|
|
|
|
notification,
|
|
|
|
"push-notification",
|
|
|
|
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);
|
2015-09-11 17:51:32 +03:00
|
|
|
return false;
|
2015-04-11 06:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
2015-09-17 15:10:42 +03:00
|
|
|
payload: payload,
|
2015-06-24 23:34:54 +03:00
|
|
|
originAttributes: aPushRecord.originAttributes,
|
2015-04-11 06:19:28 +03:00
|
|
|
scope: aPushRecord.scope
|
|
|
|
};
|
|
|
|
|
2015-09-16 20:28:17 +03:00
|
|
|
Services.telemetry.getHistogramById("PUSH_API_NOTIFY").add();
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
this._notifyListeners('push', data);
|
2015-09-11 17:51:32 +03:00
|
|
|
return true;
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
getByKeyID: function(aKeyID) {
|
|
|
|
return this._db.getByKeyID(aKeyID);
|
2013-03-29 07:49:41 +04:00
|
|
|
},
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
getAllUnexpired: function() {
|
|
|
|
return this._db.getAllUnexpired();
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
2013-03-29 07:49:41 +04:00
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
_sendRequest: function(action, aRecord) {
|
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"));
|
2015-06-03 15:05:00 +03:00
|
|
|
} else if (this._state == PUSH_SERVICE_ACTIVE_OFFLINE) {
|
2015-10-27 11:13:00 +03:00
|
|
|
if (this._service.serviceType() == "WebSocket" && action == "unregister") {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2015-11-10 00:58:50 +03:00
|
|
|
return Promise.reject(new Error("Push service offline"));
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
return this._service.request(action, aRecord);
|
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
|
|
|
|
* navigator.push, identifying the sending page and other fields.
|
|
|
|
*/
|
2015-06-03 15:05:00 +03:00
|
|
|
_registerWithServer: function(aPageRecord) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("registerWithServer()", aPageRecord);
|
2015-04-19 13:06:11 +03:00
|
|
|
|
2015-08-06 03:03:49 +03:00
|
|
|
Services.telemetry.getHistogramById("PUSH_API_SUBSCRIBE_ATTEMPT").add();
|
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))
|
2015-06-26 00:52:57 +03:00
|
|
|
.then(record => {
|
2015-06-24 23:34:54 +03:00
|
|
|
this._deletePendingRequest(aPageRecord);
|
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
|
|
|
|
2015-08-06 03:03:49 +03:00
|
|
|
_sendUnregister: function(aRecord) {
|
|
|
|
Services.telemetry.getHistogramById("PUSH_API_UNSUBSCRIBE_ATTEMPT").add();
|
|
|
|
return this._sendRequest("unregister", aRecord).then(function(v) {
|
|
|
|
Services.telemetry.getHistogramById("PUSH_API_UNSUBSCRIBE_SUCCEEDED").add();
|
|
|
|
return v;
|
|
|
|
}).catch(function(e) {
|
|
|
|
Services.telemetry.getHistogramById("PUSH_API_UNSUBSCRIBE_FAILED").add();
|
|
|
|
return Promise.reject(e);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
*/
|
2015-06-03 15:05:00 +03:00
|
|
|
_onRegisterSuccess: function(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)
|
2015-08-06 03:03:49 +03:00
|
|
|
.then(record => {
|
|
|
|
Services.telemetry.getHistogramById("PUSH_API_SUBSCRIBE_SUCCEEDED").add();
|
|
|
|
return record;
|
|
|
|
})
|
2015-06-26 00:52:57 +03:00
|
|
|
.catch(error => {
|
2015-08-06 03:03:49 +03:00
|
|
|
Services.telemetry.getHistogramById("PUSH_API_SUBSCRIBE_FAILED").add()
|
2015-06-07 08:17:04 +03:00
|
|
|
// Unable to save. Destroy the subscription in the background.
|
2015-11-10 00:58:32 +03:00
|
|
|
this._backgroundUnregister(aRecord);
|
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
|
|
|
*/
|
2015-04-21 21:10:50 +03:00
|
|
|
_onRegisterError: function(reply) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("_onRegisterError()");
|
2015-08-06 03:03:49 +03:00
|
|
|
Services.telemetry.getHistogramById("PUSH_API_SUBSCRIBE_FAILED").add()
|
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!",
|
|
|
|
reply.error);
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
receiveMessage: function(aMessage) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receiveMessage()", aMessage.name);
|
2015-06-03 15:04:00 +03:00
|
|
|
|
|
|
|
if (kCHILD_PROCESS_MESSAGES.indexOf(aMessage.name) == -1) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receiveMessage: Invalid message from child",
|
|
|
|
aMessage.name);
|
2015-06-03 15:04:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
if (aMessage.name === "Push:RegisterEventNotificationListener") {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receiveMessage: Adding child listener");
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
this._childListeners.push(aMessage.target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMessage.name === "child-process-shutdown") {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receiveMessage: Possibly removing child listener");
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
for (var i = this._childListeners.length - 1; i >= 0; --i) {
|
|
|
|
if (this._childListeners[i] == aMessage.target) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receiveMessage: Removed child listener");
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
this._childListeners.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 08:33:11 +03:00
|
|
|
console.debug("receiveMessage: Clearing notifications from child");
|
|
|
|
this._visibleNotifications.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMessage.name === "Push:NotificationForOriginShown") {
|
|
|
|
console.debug("receiveMessage: Notification shown from child");
|
|
|
|
this._notificationForOriginShown(aMessage.data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMessage.name === "Push:NotificationForOriginClosed") {
|
|
|
|
console.debug("receiveMessage: Notification closed from child");
|
|
|
|
this._notificationForOriginClosed(aMessage.data);
|
Bug 1190661 - Send push only to child processes when in e10s mode. r=smaug
Earlier, the in-process child process message manager and any content process
child process message managers received the push event. This is because
broadcastAsyncMessage is used, but on e10s we want ServiceWorkers to run in the
child process, so the push should only be dispatched to it. This patch
introduces a list of child process listeners in the PushService (running on the
parent). PushServiceChildPreload sends a message to the PushService iff it is
running in a child process. If there are non-zero child listeners, the
PushService will send a message to all of them, otherwise it will fall back to
broadcastAsyncMessage.
We currently do not add support for precise targeting of child processes. This
is because until Bug 1182117 is fixed, all child process ServiceWorkerManagers
maintain all the service worker registrations internally. Yes this is a bug,
but that is the way things are right now. This makes it impossible to
distinguish which child should handle the notification for a given origin.
Considering we don't ship multi-process e10s, I would like to land this right
now.
When Bug 1182117 is fixed, we can remove this code since the
ServiceWorkerManager will manage registrations in the parent, and it will know
which child process is running which ServiceWorker.
--HG--
extra : commitid : Dgfkg4LqLPK
extra : rebase_source : e64a4f1c757b05fe2939a338c1ad8d14f4015a90
2015-07-31 06:00:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
if (!aMessage.target.assertPermission("push")) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receiveMessage: Got message from a child process that",
|
|
|
|
"does not have 'push' permission");
|
2015-06-24 23:34:54 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-06-03 15:04:00 +03:00
|
|
|
let mm = aMessage.target.QueryInterface(Ci.nsIMessageSender);
|
2015-06-24 23:34:54 +03:00
|
|
|
|
2015-11-10 00:58:50 +03:00
|
|
|
let name = aMessage.name.slice("Push:".length);
|
|
|
|
Promise.resolve().then(_ => {
|
|
|
|
let pageRecord = this._validatePageRecord(aMessage);
|
|
|
|
return this[name.toLowerCase()](pageRecord);
|
|
|
|
}).then(result => {
|
|
|
|
mm.sendAsyncMessage("PushService:" + name + ":OK", {
|
|
|
|
requestID: aMessage.data.requestID,
|
|
|
|
result: result,
|
|
|
|
});
|
|
|
|
}, error => {
|
|
|
|
console.error("receiveMessage: Error handling message", aMessage, error);
|
|
|
|
mm.sendAsyncMessage("PushService:" + name + ":KO", {
|
|
|
|
requestID: aMessage.data.requestID,
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
console.error("receiveMessage: Error sending reply", error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_validatePageRecord: function(aMessage) {
|
2015-06-24 23:34:54 +03:00
|
|
|
let principal = aMessage.principal;
|
|
|
|
if (!principal) {
|
2015-11-10 00:58:50 +03:00
|
|
|
throw new Error("Missing message principal");
|
|
|
|
}
|
|
|
|
|
|
|
|
let pageRecord = aMessage.data;
|
|
|
|
if (!pageRecord.scope) {
|
|
|
|
throw new Error("Missing page record scope");
|
2015-06-24 23:34:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pageRecord.originAttributes =
|
|
|
|
ChromeUtils.originAttributesToSuffix(principal.originAttributes);
|
|
|
|
|
2015-11-10 00:58:50 +03:00
|
|
|
return pageRecord;
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
|
|
|
|
2015-11-10 00:58:50 +03:00
|
|
|
register: function(aPageRecord) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("register()", aPageRecord);
|
2015-06-03 15:04:00 +03:00
|
|
|
|
2015-11-10 00:58:50 +03:00
|
|
|
if (!aPageRecord.scope || aPageRecord.originAttributes === undefined) {
|
|
|
|
return Promise.reject(new Error("Invalid page record"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._checkActivated()
|
|
|
|
.then(_ => this._db.getByIdentifiers(aPageRecord))
|
2015-06-26 00:52:57 +03:00
|
|
|
.then(record => {
|
2015-11-10 00:58:50 +03:00
|
|
|
if (!record) {
|
|
|
|
return this._lookupOrPutPendingRequest(aPageRecord);
|
|
|
|
}
|
|
|
|
if (record.isExpired()) {
|
|
|
|
return record.quotaChanged().then(isChanged => {
|
|
|
|
if (isChanged) {
|
|
|
|
// If the user revisited the site, drop the expired push
|
|
|
|
// registration and re-register.
|
2015-11-11 01:27:47 +03:00
|
|
|
return this.dropRegistrationAndNotifyApp(record.keyID);
|
2015-11-10 00:58:50 +03:00
|
|
|
}
|
|
|
|
throw new Error("Push subscription expired");
|
|
|
|
}).then(_ => this._lookupOrPutPendingRequest(aPageRecord));
|
|
|
|
}
|
2015-11-11 01:27:47 +03:00
|
|
|
return record.toSubscription();
|
2015-06-03 15:05:00 +03:00
|
|
|
});
|
2015-06-03 15:04:00 +03:00
|
|
|
},
|
|
|
|
|
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
|
|
|
*/
|
2015-11-10 00:58:50 +03:00
|
|
|
unregister: function(aPageRecord) {
|
|
|
|
console.debug("unregister()", aPageRecord);
|
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
if (!aPageRecord.scope || aPageRecord.originAttributes === undefined) {
|
2015-11-10 00:58:50 +03:00
|
|
|
return Promise.reject(new Error("Invalid page record"));
|
2013-03-29 07:49:41 +04:00
|
|
|
}
|
|
|
|
|
2015-06-03 15:05:00 +03:00
|
|
|
return this._checkActivated()
|
2015-06-24 23:34:54 +03:00
|
|
|
.then(_ => this._db.getByIdentifiers(aPageRecord))
|
2015-05-12 19:08:00 +03:00
|
|
|
.then(record => {
|
|
|
|
if (record === undefined) {
|
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
|
|
|
|
2015-06-03 15:06:00 +03:00
|
|
|
return Promise.all([
|
2015-08-06 03:03:49 +03:00
|
|
|
this._sendUnregister(record),
|
2015-06-26 00:52:57 +03:00
|
|
|
this._db.delete(record.keyID),
|
2015-07-15 01:01:41 +03:00
|
|
|
]).then(() => true);
|
2015-05-12 19:08:00 +03:00
|
|
|
});
|
2015-04-21 21:10:50 +03:00
|
|
|
},
|
|
|
|
|
2015-04-21 21:10:51 +03:00
|
|
|
_clearAll: function _clearAll() {
|
2015-06-03 15:05:00 +03:00
|
|
|
return this._checkActivated()
|
2015-11-10 00:58:32 +03:00
|
|
|
.then(_ => this._db.drop())
|
2015-07-29 21:33:48 +03:00
|
|
|
.catch(_ => Promise.resolve());
|
|
|
|
},
|
|
|
|
|
|
|
|
_clearForDomain: function(domain) {
|
|
|
|
/**
|
|
|
|
* Copied from ForgetAboutSite.jsm.
|
|
|
|
*
|
|
|
|
* Returns true if the string passed in is part of the root domain of the
|
|
|
|
* current string. For example, if this is "www.mozilla.org", and we pass in
|
|
|
|
* "mozilla.org", this will return true. It would return false the other way
|
|
|
|
* around.
|
|
|
|
*/
|
|
|
|
function hasRootDomain(str, aDomain)
|
|
|
|
{
|
|
|
|
let index = str.indexOf(aDomain);
|
|
|
|
// If aDomain is not found, we know we do not have it as a root domain.
|
|
|
|
if (index == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the strings are the same, we obviously have a match.
|
|
|
|
if (str == aDomain)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, we have aDomain as our root domain iff the index of aDomain is
|
|
|
|
// aDomain.length subtracted from our length and (since we do not have an
|
|
|
|
// exact match) the character before the index is a dot or slash.
|
|
|
|
let prevChar = str[index - 1];
|
|
|
|
return (index == (str.length - aDomain.length)) &&
|
|
|
|
(prevChar == "." || prevChar == "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
let clear = (db, domain) => {
|
|
|
|
db.clearIf(record => {
|
2015-08-20 02:03:15 +03:00
|
|
|
return hasRootDomain(record.uri.prePath, domain);
|
2015-07-29 21:33:48 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._checkActivated()
|
|
|
|
.then(_ => clear(this._db, domain))
|
|
|
|
.catch(e => {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.warn("clearForDomain: Error forgetting about 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
|
|
|
|
2015-11-10 00:58:50 +03:00
|
|
|
registration: function(aPageRecord) {
|
|
|
|
console.debug("registration()");
|
2015-06-24 23:34:54 +03:00
|
|
|
if (!aPageRecord.scope || aPageRecord.originAttributes === undefined) {
|
2015-11-10 00:58:50 +03:00
|
|
|
return Promise.reject(new Error("Invalid page record"));
|
2015-06-03 15:05:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._checkActivated()
|
2015-06-24 23:34:54 +03:00
|
|
|
.then(_ => this._db.getByIdentifiers(aPageRecord))
|
2015-06-26 00:52:57 +03:00
|
|
|
.then(record => {
|
|
|
|
if (!record) {
|
2015-06-03 15:05:00 +03:00
|
|
|
return null;
|
|
|
|
}
|
2015-06-26 00:52:57 +03:00
|
|
|
if (record.isExpired()) {
|
2015-10-06 18:14:25 +03:00
|
|
|
return record.quotaChanged().then(isChanged => {
|
|
|
|
if (isChanged) {
|
2015-11-11 01:27:47 +03:00
|
|
|
return this.dropRegistrationAndNotifyApp(record.keyID).then(_ => null);
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
2015-10-30 19:37:22 +03:00
|
|
|
return null;
|
|
|
|
});
|
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
|
|
|
},
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
_dropExpiredRegistrations: function() {
|
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.
|
2015-11-11 01:27:47 +03:00
|
|
|
return 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
|
|
|
})
|
|
|
|
));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_onPermissionChange: function(subject, data) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("onPermissionChange()");
|
2015-10-06 18:14:25 +03:00
|
|
|
|
|
|
|
if (data == "cleared") {
|
|
|
|
// If the permission list was cleared, drop all registrations
|
|
|
|
// that are subject to quota.
|
|
|
|
return this._db.clearIf(record => {
|
|
|
|
if (record.quotaApplies()) {
|
|
|
|
if (!record.isExpired()) {
|
|
|
|
// Drop the registration in the background.
|
2015-11-10 00:58:32 +03:00
|
|
|
this._backgroundUnregister(record);
|
2015-10-06 18:14:25 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
_updatePermission: function(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.
|
2015-11-11 01:27:47 +03:00
|
|
|
return this._reduceByPrincipal(
|
2015-10-31 04:15:48 +03:00
|
|
|
permission.principal,
|
2015-11-11 01:27:47 +03:00
|
|
|
(subscriptionChanges, record, cursor) => {
|
|
|
|
this._permissionAllowed(subscriptionChanges, record, cursor);
|
|
|
|
return subscriptionChanges;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
).then(subscriptionChanges => {
|
|
|
|
this.notifySubscriptionChanges(subscriptionChanges);
|
|
|
|
});
|
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.
|
2015-11-11 01:27:47 +03:00
|
|
|
return this._reduceByPrincipal(
|
2015-10-31 04:15:48 +03:00
|
|
|
permission.principal,
|
2015-11-11 01:27:47 +03:00
|
|
|
(memo, 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();
|
|
|
|
},
|
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
_reduceByPrincipal: function(principal, callback, initialValue) {
|
|
|
|
return this._db.reduceByOrigin(
|
2015-10-06 18:14:25 +03:00
|
|
|
principal.URI.prePath,
|
2015-10-31 04:15:48 +03:00
|
|
|
ChromeUtils.originAttributesToSuffix(principal.originAttributes),
|
2015-11-11 01:27:47 +03:00
|
|
|
callback,
|
|
|
|
initialValue
|
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
|
|
|
*/
|
2015-11-11 01:27:47 +03:00
|
|
|
_permissionDenied: function(record, cursor) {
|
|
|
|
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.
|
2015-11-10 00:58:32 +03:00
|
|
|
this._backgroundUnregister(record);
|
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 {Array} subscriptionChanges A list of records whose associated
|
|
|
|
* service workers should be notified once the transaction has committed.
|
|
|
|
* @param {PushRecord} record The record to update.
|
|
|
|
* @param {IDBCursor} cursor The IndexedDB cursor.
|
2015-10-06 18:14:25 +03:00
|
|
|
*/
|
2015-11-11 01:27:47 +03:00
|
|
|
_permissionAllowed: function(subscriptionChanges, record, cursor) {
|
|
|
|
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.
|
2015-11-11 01:27:47 +03:00
|
|
|
subscriptionChanges.push(record);
|
|
|
|
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
|
|
|
},
|
2015-04-21 21:10:50 +03:00
|
|
|
};
|