зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1170115 - Use `clear-origin-data` to remove Push records. r=allstars.chh
--HG-- extra : commitid : HPTqiGSIMbc extra : rebase_source : 04ebebe52cd847c2258913ca97ed2dcc09aa9c32
This commit is contained in:
Родитель
9c69abbb6c
Коммит
da70e5e668
|
@ -209,6 +209,11 @@ PushRecord.prototype = {
|
|||
return this.quota === 0;
|
||||
},
|
||||
|
||||
matchesOriginAttributes(pattern) {
|
||||
return ChromeUtils.originAttributesMatchPattern(
|
||||
this.principal.originAttributes, pattern);
|
||||
},
|
||||
|
||||
toSubscription() {
|
||||
return {
|
||||
pushEndpoint: this.pushEndpoint,
|
||||
|
|
|
@ -277,37 +277,31 @@ this.PushService = {
|
|||
})
|
||||
break;
|
||||
|
||||
case "webapps-clear-data":
|
||||
console.debug("webapps-clear-data");
|
||||
|
||||
let data = aSubject
|
||||
.QueryInterface(Ci.mozIApplicationClearPrivateDataParams);
|
||||
if (!data) {
|
||||
console.error("webapps-clear-data: Failed to get information " +
|
||||
"about application");
|
||||
return;
|
||||
}
|
||||
|
||||
var originAttributes =
|
||||
ChromeUtils.originAttributesToSuffix({ appId: data.appId,
|
||||
inBrowser: data.browserOnly });
|
||||
this._db.getAllByOriginAttributes(originAttributes)
|
||||
.then(records => Promise.all(records.map(record =>
|
||||
this._db.delete(record.keyID)
|
||||
.catch(err => {
|
||||
console.error("webapps-clear-data: Error removing record",
|
||||
record, err);
|
||||
// This is the record we were unable to delete.
|
||||
return record;
|
||||
})
|
||||
.then(maybeDeleted => this._backgroundUnregister(maybeDeleted))
|
||||
)
|
||||
));
|
||||
|
||||
case "clear-origin-data":
|
||||
this._clearOriginData(data).catch(error => {
|
||||
console.error("clearOriginData: Error clearing origin data:", error);
|
||||
});
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
_clearOriginData: function(data) {
|
||||
console.log("clearOriginData()");
|
||||
|
||||
if (!data) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
let pattern = JSON.parse(data);
|
||||
return this._db.clearIf(record => {
|
||||
if (!record.matchesOriginAttributes(pattern)) {
|
||||
return false;
|
||||
}
|
||||
this._backgroundUnregister(record);
|
||||
return true;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Sends an unregister request to the server in the background. If the
|
||||
* service is not connected, this function is a no-op.
|
||||
|
@ -487,7 +481,7 @@ this.PushService = {
|
|||
return;
|
||||
}
|
||||
|
||||
Services.obs.addObserver(this, "webapps-clear-data", false);
|
||||
Services.obs.addObserver(this, "clear-origin-data", false);
|
||||
|
||||
// On B2G the NetworkManager interface fires a network-active-changed
|
||||
// event.
|
||||
|
@ -614,7 +608,7 @@ this.PushService = {
|
|||
prefs.ignore("connection.enabled", this);
|
||||
|
||||
Services.obs.removeObserver(this, this._networkStateChangeEventName);
|
||||
Services.obs.removeObserver(this, "webapps-clear-data");
|
||||
Services.obs.removeObserver(this, "clear-origin-data");
|
||||
Services.obs.removeObserver(this, "idle-daily");
|
||||
Services.obs.removeObserver(this, "perm-changed");
|
||||
},
|
||||
|
|
|
@ -12,6 +12,7 @@ Cu.import('resource://gre/modules/Timer.jsm');
|
|||
Cu.import('resource://gre/modules/Promise.jsm');
|
||||
Cu.import('resource://gre/modules/Preferences.jsm');
|
||||
Cu.import('resource://gre/modules/PlacesUtils.jsm');
|
||||
Cu.import('resource://gre/modules/ObjectUtils.jsm');
|
||||
|
||||
const serviceExports = Cu.import('resource://gre/modules/PushService.jsm', {});
|
||||
const servicePrefs = new Preferences('dom.push.');
|
||||
|
|
|
@ -0,0 +1,144 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
'use strict';
|
||||
|
||||
const {PushDB, PushService, PushServiceWebSocket} = serviceExports;
|
||||
|
||||
const userAgentID = 'bd744428-f125-436a-b6d0-dd0c9845837f';
|
||||
|
||||
let clearForPattern = Task.async(function* (testRecords, pattern) {
|
||||
let patternString = JSON.stringify(pattern);
|
||||
yield PushService._clearOriginData(patternString);
|
||||
|
||||
for (let length = testRecords.length; length--;) {
|
||||
let test = testRecords[length];
|
||||
let originSuffix = ChromeUtils.originAttributesToSuffix(
|
||||
test.originAttributes);
|
||||
|
||||
let registration = yield PushNotificationService.registration(
|
||||
test.scope,
|
||||
originSuffix
|
||||
);
|
||||
|
||||
let url = test.scope + originSuffix;
|
||||
|
||||
if (ObjectUtils.deepEqual(test.clearIf, pattern)) {
|
||||
ok(!registration, 'Should clear registration ' + url +
|
||||
' for pattern ' + patternString);
|
||||
testRecords.splice(length, 1);
|
||||
} else {
|
||||
ok(registration, 'Should not clear registration ' + url +
|
||||
' for pattern ' + patternString);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function run_test() {
|
||||
do_get_profile();
|
||||
setPrefs({
|
||||
userAgentID,
|
||||
requestTimeout: 1000,
|
||||
retryBaseInterval: 150
|
||||
});
|
||||
disableServiceWorkerEvents(
|
||||
'https://example.org/1'
|
||||
);
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
add_task(function* test_webapps_cleardata() {
|
||||
let db = PushServiceWebSocket.newPushDB();
|
||||
do_register_cleanup(() => {return db.drop().then(_ => db.close());});
|
||||
|
||||
let testRecords = [{
|
||||
scope: 'https://example.org/1',
|
||||
originAttributes: { appId: 1 },
|
||||
clearIf: { appId: 1, inBrowser: false },
|
||||
}, {
|
||||
scope: 'https://example.org/1',
|
||||
originAttributes: { appId: 1, inBrowser: true },
|
||||
clearIf: { appId: 1 },
|
||||
}, {
|
||||
scope: 'https://example.org/1',
|
||||
originAttributes: { appId: 2, inBrowser: true },
|
||||
clearIf: { appId: 2, inBrowser: true },
|
||||
}, {
|
||||
scope: 'https://example.org/2',
|
||||
originAttributes: { appId: 1 },
|
||||
clearIf: { appId: 1, inBrowser: false },
|
||||
}, {
|
||||
scope: 'https://example.org/2',
|
||||
originAttributes: { appId: 2, inBrowser: true },
|
||||
clearIf: { appId: 2, inBrowser: true },
|
||||
}, {
|
||||
scope: 'https://example.org/3',
|
||||
originAttributes: { appId: 3, inBrowser: true },
|
||||
clearIf: { inBrowser: true },
|
||||
}, {
|
||||
scope: 'https://example.org/3',
|
||||
originAttributes: { appId: 4, inBrowser: true },
|
||||
clearIf: { inBrowser: true },
|
||||
}];
|
||||
|
||||
let unregisterDone;
|
||||
let unregisterPromise = new Promise(resolve =>
|
||||
unregisterDone = after(testRecords.length, resolve));
|
||||
|
||||
PushService.init({
|
||||
serverURI: "wss://push.example.org",
|
||||
networkInfo: new MockDesktopNetworkInfo(),
|
||||
db,
|
||||
makeWebSocket(uri) {
|
||||
return new MockWebSocket(uri, {
|
||||
onHello(data) {
|
||||
equal(data.messageType, 'hello', 'Handshake: wrong message type');
|
||||
equal(data.uaid, userAgentID, 'Handshake: wrong device ID');
|
||||
this.serverSendMsg(JSON.stringify({
|
||||
messageType: 'hello',
|
||||
status: 200,
|
||||
uaid: userAgentID
|
||||
}));
|
||||
},
|
||||
onRegister(data) {
|
||||
equal(data.messageType, 'register', 'Register: wrong message type');
|
||||
this.serverSendMsg(JSON.stringify({
|
||||
messageType: 'register',
|
||||
status: 200,
|
||||
channelID: data.channelID,
|
||||
uaid: userAgentID,
|
||||
pushEndpoint: 'https://example.com/update/' + Math.random(),
|
||||
}));
|
||||
},
|
||||
onUnregister(data) {
|
||||
unregisterDone();
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
yield Promise.all(testRecords.map(test =>
|
||||
PushNotificationService.register(
|
||||
test.scope,
|
||||
ChromeUtils.originAttributesToSuffix(test.originAttributes)
|
||||
)
|
||||
));
|
||||
|
||||
// Removes records for all scopes with the same app ID. Excludes records
|
||||
// where `inBrowser` is true.
|
||||
yield clearForPattern(testRecords, { appId: 1, inBrowser: false });
|
||||
|
||||
// Removes the remaining record for app ID 1, where `inBrowser` is true.
|
||||
yield clearForPattern(testRecords, { appId: 1 });
|
||||
|
||||
// Removes all records for all scopes with the same app ID, where
|
||||
// `inBrowser` is true.
|
||||
yield clearForPattern(testRecords, { appId: 2, inBrowser: true });
|
||||
|
||||
// Removes all records where `inBrowser` is true.
|
||||
yield clearForPattern(testRecords, { inBrowser: true });
|
||||
|
||||
equal(testRecords.length, 0, 'Should remove all test records');
|
||||
yield waitForPromise(unregisterPromise, DEFAULT_TIMEOUT,
|
||||
'Timed out waiting for unregister');
|
||||
});
|
|
@ -1,95 +0,0 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
'use strict';
|
||||
|
||||
const {PushDB, PushService, PushServiceWebSocket} = serviceExports;
|
||||
|
||||
const userAgentID = 'bd744428-f125-436a-b6d0-dd0c9845837f';
|
||||
|
||||
function run_test() {
|
||||
do_get_profile();
|
||||
setPrefs({
|
||||
userAgentID,
|
||||
requestTimeout: 1000,
|
||||
retryBaseInterval: 150
|
||||
});
|
||||
disableServiceWorkerEvents(
|
||||
'https://example.org/1'
|
||||
);
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
add_task(function* test_webapps_cleardata() {
|
||||
let db = PushServiceWebSocket.newPushDB();
|
||||
do_register_cleanup(() => {return db.drop().then(_ => db.close());});
|
||||
|
||||
let unregisterDone;
|
||||
let unregisterPromise = new Promise(resolve => unregisterDone = resolve);
|
||||
|
||||
PushService.init({
|
||||
serverURI: "wss://push.example.org",
|
||||
networkInfo: new MockDesktopNetworkInfo(),
|
||||
db,
|
||||
makeWebSocket(uri) {
|
||||
return new MockWebSocket(uri, {
|
||||
onHello(data) {
|
||||
equal(data.messageType, 'hello', 'Handshake: wrong message type');
|
||||
equal(data.uaid, userAgentID, 'Handshake: wrong device ID');
|
||||
this.serverSendMsg(JSON.stringify({
|
||||
messageType: 'hello',
|
||||
status: 200,
|
||||
uaid: userAgentID
|
||||
}));
|
||||
},
|
||||
onRegister(data) {
|
||||
equal(data.messageType, 'register', 'Register: wrong message type');
|
||||
this.serverSendMsg(JSON.stringify({
|
||||
messageType: 'register',
|
||||
status: 200,
|
||||
channelID: data.channelID,
|
||||
uaid: userAgentID,
|
||||
pushEndpoint: 'https://example.com/update/' + Math.random(),
|
||||
}));
|
||||
},
|
||||
onUnregister(data) {
|
||||
unregisterDone();
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
let registers = yield Promise.all([
|
||||
PushNotificationService.register(
|
||||
'https://example.org/1',
|
||||
ChromeUtils.originAttributesToSuffix({ appId: 1, inBrowser: false })),
|
||||
PushNotificationService.register(
|
||||
'https://example.org/1',
|
||||
ChromeUtils.originAttributesToSuffix({ appId: 1, inBrowser: true })),
|
||||
]);
|
||||
|
||||
Services.obs.notifyObservers(
|
||||
{ appId: 1, browserOnly: false,
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams])},
|
||||
"webapps-clear-data", "");
|
||||
|
||||
let waitAWhile = new Promise(function(res) {
|
||||
setTimeout(res, 2000);
|
||||
});
|
||||
yield waitAWhile;
|
||||
|
||||
let registration;
|
||||
registration = yield PushNotificationService.registration(
|
||||
'https://example.org/1',
|
||||
ChromeUtils.originAttributesToSuffix({ appId: 1, inBrowser: false }));
|
||||
ok(!registration, 'Registration for { 1, false } should not exist.');
|
||||
|
||||
registration = yield PushNotificationService.registration(
|
||||
'https://example.org/1',
|
||||
ChromeUtils.originAttributesToSuffix({ appId: 1, inBrowser: true }));
|
||||
ok(registration, 'Registration for { 1, true } should still exist.');
|
||||
|
||||
yield waitForPromise(unregisterPromise, DEFAULT_TIMEOUT,
|
||||
'Timed out waiting for unregister');
|
||||
});
|
||||
|
|
@ -4,6 +4,7 @@ tail =
|
|||
# Push notifications and alarms are currently disabled on Android.
|
||||
skip-if = toolkit == 'android'
|
||||
|
||||
[test_clear_origin_data.js]
|
||||
[test_drop_expired.js]
|
||||
[test_notification_ack.js]
|
||||
[test_notification_data.js]
|
||||
|
@ -39,7 +40,6 @@ run-sequentially = This will delete all existing push subscriptions.
|
|||
[test_unregister_invalid_json.js]
|
||||
[test_unregister_not_found.js]
|
||||
[test_unregister_success.js]
|
||||
[test_webapps_cleardata.js]
|
||||
[test_updateRecordNoEncryptionKeys_ws.js]
|
||||
[test_reconnect_retry.js]
|
||||
[test_retry_ws.js]
|
||||
|
|
Загрузка…
Ссылка в новой задаче