Bug 1277026 - Handle FxA push payloads. r?kitcambridge
MozReview-Commit-ID: Cd6nKHP2VQi
--- a/services/fxaccounts/FxAccountsPush.js
+++ b/services/fxaccounts/FxAccountsPush.js
@@ -110,47 +110,56 @@ FxAccountsPushService.prototype = {
/**
* Standard observer interface to listen to push messages, changes and logout.
*
* @param subject
* @param topic
* @param data
*/
observe(subject, topic, data) {
- this.log.trace(`observed topic=${topic}, data=${data}, subject=${subject}`);
- switch (topic) {
- case this.pushService.pushTopic:
- if (data === FXA_PUSH_SCOPE_ACCOUNT_UPDATE) {
- this._onPushMessage();
- }
- break;
- case this.pushService.subscriptionChangeTopic:
- if (data === FXA_PUSH_SCOPE_ACCOUNT_UPDATE) {
- this._onPushSubscriptionChange();
- }
- break;
- case ONLOGOUT_NOTIFICATION:
- // user signed out, we need to stop polling the Push Server
- this.unsubscribe().catch(err => {
- this.log.error("Error during unsubscribe", err);
- });
- break;
- default:
- break;
- }
+ this.log.trace(`observed topic=${topic}, data=${data}, subject=${subject}`);
+ switch (topic) {
+ case this.pushService.pushTopic:
+ if (data === FXA_PUSH_SCOPE_ACCOUNT_UPDATE) {
+ let message = subject.QueryInterface(Ci.nsIPushMessage);
+ this._onPushMessage(message);
+ }
+ break;
+ case this.pushService.subscriptionChangeTopic:
+ if (data === FXA_PUSH_SCOPE_ACCOUNT_UPDATE) {
+ this._onPushSubscriptionChange();
+ }
+ break;
+ case ONLOGOUT_NOTIFICATION:
+ // user signed out, we need to stop polling the Push Server
+ this.unsubscribe().catch(err => {
+ this.log.error("Error during unsubscribe", err);
+ });
+ break;
+ default:
+ break;
+ }
},
/**
* Fired when the Push server sends a notification.
*
* @private
*/
- _onPushMessage() {
+ _onPushMessage(message) {
this.log.trace("FxAccountsPushService _onPushMessage");
- // Use this signal to check the verification state of the account right away
- this.fxAccounts.checkVerificationStatus();
+ if (!message.data) {
+ // Use the empty signal to check the verification state of the account right away
+ this.fxAccounts.checkVerificationStatus();
+ return;
+ }
+ let payload = message.data.json();
+ switch (payload.command) {
+ default:
+ this.log.warn("FxA Push command unrecognized: " + payload.command);
+ }
},
/**
* Fired when the Push server drops a subscription, or the subscription identifier changes.
*
* https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIPushService#Receiving_Push_Messages
*
* @private
*/
--- a/services/fxaccounts/tests/xpcshell/test_push_service.js
+++ b/services/fxaccounts/tests/xpcshell/test_push_service.js
@@ -44,69 +44,69 @@ let mockLog = {
debug() {},
warn() {},
error() {}
};
add_task(function* initialize() {
let pushService = new FxAccountsPushService();
- do_check_eq(pushService.initialize(), false);
+ equal(pushService.initialize(), false);
});
add_task(function* registerPushEndpointSuccess() {
let pushService = new FxAccountsPushService({
pushService: mockPushService,
fxAccounts: mockFxAccounts,
});
let subscription = yield pushService.registerPushEndpoint();
- do_check_eq(subscription.endpoint, MOCK_ENDPOINT);
+ equal(subscription.endpoint, MOCK_ENDPOINT);
});
add_task(function* registerPushEndpointFailure() {
let failPushService = Object.assign(mockPushService, {
subscribe(scope, principal, cb) {
cb(Components.results.NS_ERROR_ABORT);
}
});
let pushService = new FxAccountsPushService({
pushService: failPushService,
fxAccounts: mockFxAccounts,
});
let subscription = yield pushService.registerPushEndpoint();
- do_check_eq(subscription, null);
+ equal(subscription, null);
});
add_task(function* unsubscribeSuccess() {
let pushService = new FxAccountsPushService({
pushService: mockPushService,
fxAccounts: mockFxAccounts,
});
let result = yield pushService.unsubscribe();
- do_check_eq(result, true);
+ equal(result, true);
});
add_task(function* unsubscribeFailure() {
let failPushService = Object.assign(mockPushService, {
unsubscribe(scope, principal, cb) {
cb(Components.results.NS_ERROR_ABORT);
}
});
let pushService = new FxAccountsPushService({
pushService: failPushService,
fxAccounts: mockFxAccounts,
});
let result = yield pushService.unsubscribe();
- do_check_eq(result, null);
+ equal(result, null);
});
add_test(function observeLogout() {
let customLog = Object.assign(mockLog, {
trace: function (msg) {
if (msg === "FxAccountsPushService unsubscribe") {
// logout means we unsubscribe
run_next_test();
@@ -117,30 +117,35 @@ add_test(function observeLogout() {
let pushService = new FxAccountsPushService({
pushService: mockPushService,
log: customLog
});
pushService.observe(null, ONLOGOUT_NOTIFICATION);
});
-add_test(function observePushTopic() {
+add_test(function observePushTopicVerify() {
+ let emptyMsg = {
+ QueryInterface: function() {
+ return this;
+ }
+ };
let customAccounts = Object.assign(mockFxAccounts, {
checkVerificationStatus: function () {
- // checking verification status on push messages
+ // checking verification status on push messages without data
run_next_test();
}
});
let pushService = new FxAccountsPushService({
pushService: mockPushService,
fxAccounts: customAccounts,
});
- pushService.observe(null, mockPushService.pushTopic, FXA_PUSH_SCOPE_ACCOUNT_UPDATE);
+ pushService.observe(emptyMsg, mockPushService.pushTopic, FXA_PUSH_SCOPE_ACCOUNT_UPDATE);
});
add_test(function observeSubscriptionChangeTopic() {
let customAccounts = Object.assign(mockFxAccounts, {
updateDeviceRegistration: function () {
// subscription change means updating the device registration
run_next_test();
}
@@ -148,13 +153,8 @@ add_test(function observeSubscriptionCha
let pushService = new FxAccountsPushService({
pushService: mockPushService,
fxAccounts: customAccounts,
});
pushService.observe(null, mockPushService.subscriptionChangeTopic, FXA_PUSH_SCOPE_ACCOUNT_UPDATE);
});
-
-
-function run_test() {
- run_next_test();
-}