Bug 1239584, Part 2 - Remove message manager usage from PushService.jsm. r=dragana

This commit is contained in:
Kit Cambridge 2016-01-13 22:24:37 -07:00
parent 1a7e1adb65
commit 9667d537f8
9 changed files with 23 additions and 189 deletions

View File

@ -6,7 +6,6 @@
XPIDL_SOURCES += [
'nsIPushNotifier.idl',
'nsIPushObserverNotification.idl',
'nsIPushService.idl',
]

View File

@ -1,40 +0,0 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "nsISupports.idl"
/**
* A push message received by an `nsIPushService`, used as the subject of a
* `push-notification` observer notification.
*/
[scriptable, uuid(56f57607-28b6-44b0-aa56-3d4d3c88be15)]
interface nsIPushObserverNotification : nsISupports
{
/* The URL that receives push messages from an application server. */
attribute string pushEndpoint;
/**
* The notification version sent by the application server. This is a
* monotonically increasing number.
*/
attribute long long version;
/**
* The notification payload. Delivery is not guaranteed; if the browser is
* offline when the application server sends the push message, the payload
* may be discarded.
*/
attribute string data;
/**
* How many times has a push event occured against this pushEndpoint
*/
attribute long long pushCount;
/**
* The last time a push occured against this this pushEndpoint
*/
attribute long long lastPush;
};

View File

@ -81,9 +81,8 @@ interface nsIPushService : nsISupports
* The |endpoint| property of the subscription record is a URL string
* that can be used to send push messages to subscribers.
*
* Each incoming message fires a `push-notification` observer
* notification, with an `nsIPushObserverNotification` as the subject and
* the |scope| as the data.
* Each incoming message fires a `push-message` observer notification, with
* an `nsIPushMessage` as the subject and the |scope| as the data.
*
* If the server drops a subscription, a `push-subscription-change` observer
* will be fired, with the subject set to `null` and the data set to |scope|.

View File

@ -6,6 +6,3 @@ contract @mozilla.org/push/PushManager;1 {cde1d019-fad8-4044-b141-65fb4fb7a245}
component {daaa8d73-677e-4233-8acd-2c404bd01658} PushComponents.js
contract @mozilla.org/push/Service;1 {daaa8d73-677e-4233-8acd-2c404bd01658}
category app-startup PushServiceParent @mozilla.org/push/Service;1
component {e68997fd-8b92-49ee-af12-800830b023e8} PushComponents.js
contract @mozilla.org/push/ObserverNotification;1 {e68997fd-8b92-49ee-af12-800830b023e8}

View File

@ -106,10 +106,8 @@ Object.assign(PushServiceParent.prototype, {
"Push:Registration",
"Push:Unregister",
"Push:Clear",
"Push:RegisterEventNotificationListener",
"Push:NotificationForOriginShown",
"Push:NotificationForOriginClosed",
"child-process-shutdown",
],
// nsIPushService methods
@ -169,14 +167,6 @@ Object.assign(PushServiceParent.prototype, {
return;
}
let {name, principal, target, data} = message;
if (name === "Push:RegisterEventNotificationListener") {
this._service.registerListener(target);
return;
}
if (name === "child-process-shutdown") {
this._service.unregisterListener(target);
return;
}
if (name === "Push:NotificationForOriginShown") {
this.notificationForOriginShown(data);
return;
@ -468,21 +458,7 @@ PushSubscription.prototype = {
},
};
/**
* `PushObserverNotification` instances are passed to all
* `push-notification` observers.
*/
function PushObserverNotification() {}
PushObserverNotification.prototype = {
classID: Components.ID("{e68997fd-8b92-49ee-af12-800830b023e8}"),
contractID: "@mozilla.org/push/ObserverNotification;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPushObserverNotification]),
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([
PushObserverNotification,
// Export the correct implementation depending on whether we're running in
// the parent or content process.
isParent ? PushServiceParent : PushServiceContent,

View File

@ -31,6 +31,10 @@ XPCOMUtils.defineLazyServiceGetter(this, "gContentSecurityManager",
"@mozilla.org/contentsecuritymanager;1",
"nsIContentSecurityManager");
XPCOMUtils.defineLazyServiceGetter(this, "gPushNotifier",
"@mozilla.org/push/Notifier;1",
"nsIPushNotifier");
this.EXPORTED_SYMBOLS = ["PushService"];
XPCOMUtils.defineLazyGetter(this, "console", () => {
@ -99,8 +103,6 @@ this.PushService = {
// reduce the quota for a record. Used for testing purposes.
_updateQuotaTestCallback: null,
_childListeners: new Set(),
// When serverURI changes (this is used for testing), db is cleaned up and a
// a new db is started. This events must be sequential.
_stateChangeProcessQueue: null,
@ -592,8 +594,6 @@ this.PushService = {
uninit: function() {
console.debug("uninit()");
this._childListeners.clear();
if (this._state == PUSH_SERVICE_UNINIT) {
return;
}
@ -688,38 +688,8 @@ this.PushService = {
return;
}
// Notify XPCOM observers.
Services.obs.notifyObservers(
null,
"push-subscription-change",
record.scope
);
let data = {
originAttributes: record.originAttributes,
scope: record.scope
};
Services.telemetry.getHistogramById("PUSH_API_NOTIFY_REGISTRATION_LOST").add();
this._notifyListeners('pushsubscriptionchange', data);
},
_notifyListeners: function(name, data) {
if (this._childListeners.size > 0) {
// Try to send messages to all listeners, but remove any that fail since
// the receiver is likely gone away.
for (let listener of this._childListeners) {
try {
listener.sendAsyncMessage(name, data);
} catch(e) {
this._childListeners.delete(listener);
}
}
} else {
let ppmm = Cc['@mozilla.org/parentprocessmessagemanager;1']
.getService(Ci.nsIMessageListenerManager);
ppmm.broadcastAsyncMessage(name, data);
}
gPushNotifier.notifySubscriptionChange(record.scope, record.principal);
},
/**
@ -959,29 +929,6 @@ this.PushService = {
}
console.debug("notifyApp()", aPushRecord.scope);
// Notify XPCOM observers.
let notification = Cc["@mozilla.org/push/ObserverNotification;1"]
.createInstance(Ci.nsIPushObserverNotification);
notification.pushEndpoint = aPushRecord.pushEndpoint;
notification.version = aPushRecord.version;
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]);
}
}
notification.lastPush = aPushRecord.lastPush;
notification.pushCount = aPushRecord.pushCount;
Services.obs.notifyObservers(
notification,
"push-notification",
aPushRecord.scope
);
// If permission has been revoked, trash the message.
if (!aPushRecord.hasPermission()) {
@ -989,14 +936,22 @@ this.PushService = {
return false;
}
let data = {
payload: payload,
originAttributes: aPushRecord.originAttributes,
scope: aPushRecord.scope
};
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();
this._notifyListeners('push', data);
}
if (payload) {
gPushNotifier.notifyPushWithData(aPushRecord.scope,
aPushRecord.principal,
payload.length, payload);
} else {
gPushNotifier.notifyPush(aPushRecord.scope, aPushRecord.principal);
}
return true;
},
@ -1086,14 +1041,7 @@ this.PushService = {
throw reply.error;
},
registerListener(listener) {
console.debug("registerListener: Adding child listener");
this._childListeners.add(listener);
},
unregisterListener(listener) {
console.debug("unregisterListener: Possibly removing child listener");
this._childListeners.delete(listener);
notificationsCleared() {
this._visibleNotifications.clear();
},

View File

@ -1,41 +0,0 @@
/* 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";
this.EXPORTED_SYMBOLS = [];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyServiceGetter(this,
"swm",
"@mozilla.org/serviceworkers/manager;1",
"nsIServiceWorkerManager");
var processType = Cc["@mozilla.org/xre/app-info;1"]
.getService(Ci.nsIXULRuntime).processType;
var isParent = processType === Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
Services.cpmm.addMessageListener("push", function (aMessage) {
let {originAttributes, scope, payload} = aMessage.data;
if (payload) {
swm.sendPushEvent(originAttributes, scope, payload.length, payload);
} else {
swm.sendPushEvent(originAttributes, scope);
}
});
Services.cpmm.addMessageListener("pushsubscriptionchange", function (aMessage) {
swm.sendPushSubscriptionChangeEvent(aMessage.data.originAttributes,
aMessage.data.scope);
});
if (!isParent) {
Services.cpmm.sendAsyncMessage("Push:RegisterEventNotificationListener", null, null, null);
}

View File

@ -14,7 +14,6 @@ EXTRA_JS_MODULES += [
'PushDB.jsm',
'PushRecord.jsm',
'PushService.jsm',
'PushServiceChildPreload.jsm',
'PushServiceHttp2.jsm',
'PushServiceWebSocket.jsm',
]

View File

@ -10,6 +10,3 @@ var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
// and for those that match URLs provided by the parent process will set up
// a dedicated message port and notify the parent process.
Cu.import("resource://gre/modules/RemotePageManager.jsm");
// Hooks to listen for push messages
Cu.import("resource://gre/modules/PushServiceChildPreload.jsm");