Bug 1189998, Part 2 - Migrate Push service callers. r=mt

This commit is contained in:
Kit Cambridge 2015-12-08 15:41:48 -05:00
parent 5f9c946b4f
commit 3bfcbcb131
7 changed files with 104 additions and 30 deletions

View File

@ -549,9 +549,13 @@ Sanitizer.prototype = {
// Clear all push notification subscriptions
try {
var push = Cc["@mozilla.org/push/NotificationService;1"]
.getService(Ci.nsIPushNotificationService);
push.clearAll();
var push = Cc["@mozilla.org/push/Service;1"]
.getService(Ci.nsIPushService);
push.clearForDomain("*", status => {
if (!Components.isSuccessCode(status)) {
dump("Error clearing Web Push data: " + status + "\n");
}
});
} catch (e) {
dump("Web Push may not be available.\n");
}

View File

@ -42,10 +42,10 @@ interface nsIPushSubscription : nsISupports
};
/**
* Called by the |unsubscribe| method. A non-success |status| indicates that
* there was a problem unsubscribing, and the |success| argument should be
* ignored. Otherwise, |success| is true if unsubscribing was successful, and
* false if the subscription does not exist.
* Called by |unsubscribe|. A non-success |status| indicates that there was
* a problem unsubscribing, and the |success| argument should be ignored.
* Otherwise, |success| is true if unsubscribing was successful, and false if
* the subscription does not exist.
*/
[scriptable, uuid(d574118f-61a9-4270-b1f6-4461aa85c4f5), function]
interface nsIUnsubscribeResultCallback : nsISupports
@ -53,6 +53,16 @@ interface nsIUnsubscribeResultCallback : nsISupports
void onUnsubscribe(in nsresult status, in bool success);
};
/**
* Called by |clearForDomain|. A non-success |status| indicates that there was
* a problem clearing subscriptions for the given domain.
*/
[scriptable, uuid(bd47b38e-8bfa-4f92-834e-832a4431e05e), function]
interface nsIPushClearResultCallback : nsISupports
{
void onClear(in nsresult status);
};
/**
* A service for components to subscribe and receive push messages from web
* services. This functionality is exposed to content via the Push DOM API,
@ -60,7 +70,7 @@ interface nsIUnsubscribeResultCallback : nsISupports
* and allows privileged code to receive messages without migrating to service
* workers.
*/
[scriptable, uuid(cf46905d-fb55-4f21-8927-31f821c371b6)]
[scriptable, uuid(678ef584-bf25-47aa-ac84-03efc0865b68)]
interface nsIPushService : nsISupports
{
/**
@ -96,6 +106,13 @@ interface nsIPushService : nsISupports
*/
void getSubscription(in DOMString scope, in nsIPrincipal principal,
in nsIPushSubscriptionCallback callback);
/**
* Drops every subscription for the given |domain|, or all domains if
* |domain| is "*".
*/
void clearForDomain(in DOMString domain,
in nsIPushClearResultCallback callback);
};
[scriptable, uuid(a2555e70-46f8-4b52-bf02-d978b979d143)]

View File

@ -105,6 +105,7 @@ Object.assign(PushServiceParent.prototype, {
"Push:Register",
"Push:Registration",
"Push:Unregister",
"Push:Clear",
"Push:RegisterEventNotificationListener",
"Push:NotificationForOriginShown",
"Push:NotificationForOriginClosed",
@ -143,6 +144,17 @@ Object.assign(PushServiceParent.prototype, {
}).catch(Cu.reportError);
},
clearForDomain(domain, callback) {
let principal = Services.scriptSecurityManager.getSystemPrincipal();
return this._handleRequest("Push:Clear", principal, {
domain: domain,
}).then(result => {
callback.onClear(Cr.NS_OK);
}, error => {
callback.onClear(Cr.NS_ERROR_FAILURE);
}).catch(Cu.reportError);
},
// nsIPushQuotaManager methods
notificationForOriginShown(origin) {
@ -210,6 +222,10 @@ Object.assign(PushServiceParent.prototype, {
return Promise.reject(new Error("Invalid request: missing principal"));
}
if (name == "Push:Clear") {
return this._service.clear(data);
}
let pageRecord;
try {
pageRecord = this._toPageRecord(principal, data);
@ -264,6 +280,8 @@ Object.assign(PushServiceContent.prototype, {
"PushService:Registration:KO",
"PushService:Unregister:OK",
"PushService:Unregister:KO",
"PushService:Clear:OK",
"PushService:Clear:KO",
],
// nsIPushService methods
@ -292,6 +310,14 @@ Object.assign(PushServiceContent.prototype, {
}, null, principal);
},
clearForDomain(domain, callback) {
let requestId = this._addRequest(callback);
this._mm.sendAsyncMessage("Push:Clear", {
domain: domain,
requestID: requestId,
});
},
// nsIPushQuotaManager methods
notificationForOriginShown(origin) {
@ -348,6 +374,14 @@ Object.assign(PushServiceContent.prototype, {
request.onUnsubscribe(Cr.NS_ERROR_FAILURE, false);
break;
case "PushService:Clear:OK":
request.onClear(Cr.NS_OK);
break;
case "PushService:Clear:KO":
request.onClear(Cr.NS_ERROR_FAILURE);
break;
default:
break;
}

View File

@ -1160,6 +1160,13 @@ this.PushService = {
});
},
clear: function(info) {
if (info.domain == "*") {
return this._clearAll();
}
return this._clearForDomain(info.domain);
},
_clearAll: function _clearAll() {
return this._checkActivated()
.then(_ => this._db.drop())

View File

@ -46,12 +46,12 @@ function init() {
return;
}
let pns = undefined;
let ps = undefined;
try {
pns = Cc["@mozilla.org/push/NotificationService;1"]
.getService(Ci.nsIPushNotificationService);
ps = Cc["@mozilla.org/push/Service;1"]
.getService(Ci.nsIPushService);
} catch(e) {
dump("Could not acquire PushNotificationService\n");
dump("Could not acquire PushService\n");
}
for (let i = 0; i < length; ++i) {
@ -61,11 +61,11 @@ function init() {
continue;
}
display(info, pns);
display(info, ps);
}
}
function display(info, pushNotificationService) {
function display(info, pushService) {
let parent = document.getElementById("serviceworkers");
let div = document.createElement('div');
@ -124,13 +124,14 @@ function display(info, pushNotificationService) {
createItem(bundle.GetStringFromName('waitingCacheName'), waitingCacheName);
let pushItem = createItem(bundle.GetStringFromName('pushEndpoint'), bundle.GetStringFromName('waiting'));
if (pushNotificationService) {
pushNotificationService.registration(info.scope, info.principal.originAttributes)
.then(pushRecord => {
if (pushService) {
pushService.getRegistration(info.scope, info.principal, (status, pushRecord) => {
if (Components.isSuccessCode(status)) {
pushItem.data = JSON.stringify(pushRecord);
}).catch(error => {
} else {
dump("about:serviceworkers - retrieving push registration failed\n");
});
}
});
}
let updateButton = document.createElement("button");

View File

@ -189,13 +189,15 @@ this.ForgetAboutSite = {
np.reset();
// Push notifications.
try {
var push = Cc["@mozilla.org/push/NotificationService;1"]
.getService(Ci.nsIPushNotificationService);
push.clearForDomain(aDomain);
} catch (e) {
promises.push(new Promise(resolve => {
var push = Cc["@mozilla.org/push/Service;1"]
.getService(Ci.nsIPushService);
push.clearForDomain(aDomain, status => {
(Components.isSuccessCode(status) ? resolve : reject)(status);
});
}).catch(e => {
dump("Web Push may not be available.\n");
}
}));
return Promise.all(promises);
}

View File

@ -467,8 +467,8 @@ function* test_push_cleared()
{
let ps;
try {
ps = Cc["@mozilla.org/push/NotificationService;1"].
getService(Ci.nsIPushNotificationService);
ps = Cc["@mozilla.org/push/Service;1"].
getService(Ci.nsIPushService);
} catch(e) {
// No push service, skip test.
return;
@ -502,9 +502,18 @@ function* test_push_cleared()
function push_registration_exists(aURL, ps)
{
return ps.registration(aURL, ChromeUtils.originAttributesToSuffix({ appId: Ci.nsIScriptSecurityManager.NO_APP_ID, inBrowser: false }))
.then(record => !!record)
.catch(_ => false);
return new Promise(resolve => {
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager);
let principal = ssm.createCodebasePrincipalFromOrigin(aURL);
return ps.getSubscription(aURL, principal, (status, record) => {
if (!Components.isSuccessCode(status)) {
resolve(false);
} else {
resolve(!!record);
}
});
});
}
const TEST_URL = "https://www.mozilla.org/scope/";