Bug 1274105 - Refactor classifierHelper.js. r=gcp

MozReview-Commit-ID: ljq4KPzUqu

--HG--
extra : rebase_source : 71de8b56d523568a940cb1d12a4f773c991c3294
This commit is contained in:
Dimi Lee 2016-05-25 20:20:42 +08:00
Родитель cb7ad80606
Коммит d77739fce2
5 изменённых файлов: 82 добавлений и 53 удалений

Просмотреть файл

@ -16,7 +16,7 @@ function doUpdate(update) {
updateUrlRequested: function(url) { }, updateUrlRequested: function(url) { },
streamFinished: function(status) { }, streamFinished: function(status) { },
updateError: function(errorCode) { updateError: function(errorCode) {
sendAsyncMessage("updateError", { errorCode }); sendAsyncMessage("updateError", errorCode);
}, },
updateSuccess: function(requestedTimeout) { updateSuccess: function(requestedTimeout) {
sendAsyncMessage("updateSuccess"); sendAsyncMessage("updateSuccess");

Просмотреть файл

@ -3,7 +3,7 @@ if (typeof(classifierHelper) == "undefined") {
} }
const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js"); const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js");
var classifierCommonScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL); var gScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
const ADD_CHUNKNUM = 524; const ADD_CHUNKNUM = 524;
const SUB_CHUNKNUM = 523; const SUB_CHUNKNUM = 523;
@ -19,47 +19,71 @@ classifierHelper._updatesToCleanup = [];
// Pass { url: ..., db: ... } to add url to database, // Pass { url: ..., db: ... } to add url to database,
// onsuccess/onerror will be called when update complete. // onsuccess/onerror will be called when update complete.
classifierHelper.addUrlToDB = function(updateData, onsuccess, onerror) { classifierHelper.addUrlToDB = function(updateData) {
var testUpdate = ""; return new Promise(function(resolve, reject) {
for (var update of updateData) { var testUpdate = "";
var LISTNAME = update.db; for (var update of updateData) {
var CHUNKDATA = update.url; var LISTNAME = update.db;
var CHUNKLEN = CHUNKDATA.length; var CHUNKDATA = update.url;
var CHUNKLEN = CHUNKDATA.length;
classifierHelper._updatesToCleanup.push(update); classifierHelper._updatesToCleanup.push(update);
testUpdate += testUpdate +=
"n:1000\n" + "n:1000\n" +
"i:" + LISTNAME + "\n" + "i:" + LISTNAME + "\n" +
"ad:1\n" + "ad:1\n" +
"a:" + ADD_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" + "a:" + ADD_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
CHUNKDATA; CHUNKDATA;
} }
classifierHelper._update(testUpdate, onsuccess, onerror); classifierHelper._update(testUpdate, resolve, reject);
});
} }
// Pass { url: ..., db: ... } to remove url from database, // Pass { url: ..., db: ... } to remove url from database,
// onsuccess/onerror will be called when update complete. // onsuccess/onerror will be called when update complete.
classifierHelper.removeUrlFromDB = function(updateData, onsuccess, onerror) { classifierHelper.removeUrlFromDB = function(updateData) {
var testUpdate = ""; return new Promise(function(resolve, reject) {
for (var update of updateData) { var testUpdate = "";
var LISTNAME = update.db; for (var update of updateData) {
var CHUNKDATA = ADD_CHUNKNUM + ":" + update.url; var LISTNAME = update.db;
var CHUNKLEN = CHUNKDATA.length; var CHUNKDATA = ADD_CHUNKNUM + ":" + update.url;
var CHUNKLEN = CHUNKDATA.length;
testUpdate += testUpdate +=
"n:1000\n" + "n:1000\n" +
"i:" + LISTNAME + "\n" + "i:" + LISTNAME + "\n" +
"s:" + SUB_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" + "s:" + SUB_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
CHUNKDATA; CHUNKDATA;
} }
classifierHelper._updatesToCleanup = classifierHelper._updatesToCleanup =
classifierHelper._updatesToCleanup.filter((v) => { classifierHelper._updatesToCleanup.filter((v) => {
return updateData.indexOf(v) == -1; return updateData.indexOf(v) == -1;
}); });
classifierHelper._update(testUpdate, onsuccess, onerror); classifierHelper._update(testUpdate, resolve, reject);
});
};
// This API is used to expire all add/sub chunks we have updated
// by using addUrlToDB and removeUrlFromDB.
classifierHelper.resetDB = function() {
return new Promise(function(resolve, reject) {
var testUpdate = "";
for (var update of classifierHelper._updatesToCleanup) {
if (testUpdate.includes(update.db))
continue;
testUpdate +=
"n:1000\n" +
"i:" + update.db + "\n" +
"ad:" + ADD_CHUNKNUM + "\n" +
"sd:" + SUB_CHUNKNUM + "\n"
}
classifierHelper._update(testUpdate, resolve, reject);
});
}; };
classifierHelper._update = function(testUpdate, onsuccess, onerror) { classifierHelper._update = function(testUpdate, onsuccess, onerror) {
@ -71,7 +95,7 @@ classifierHelper._update = function(testUpdate, onsuccess, onerror) {
return; return;
} }
classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate }); gScript.sendAsyncMessage("doUpdate", { testUpdate });
}; };
classifierHelper._updateSuccess = function() { classifierHelper._updateSuccess = function() {
@ -80,7 +104,7 @@ classifierHelper._updateSuccess = function() {
if (classifierHelper._updates.length) { if (classifierHelper._updates.length) {
var testUpdate = classifierHelper._updates[0].data; var testUpdate = classifierHelper._updates[0].data;
classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate }); gScript.sendAsyncMessage("doUpdate", { testUpdate });
} }
}; };
@ -90,13 +114,13 @@ classifierHelper._updateError = function(errorCode) {
if (classifierHelper._updates.length) { if (classifierHelper._updates.length) {
var testUpdate = classifierHelper._updates[0].data; var testUpdate = classifierHelper._updates[0].data;
classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate }); gScript.sendAsyncMessage("doUpdate", { testUpdate });
} }
}; };
classifierHelper._setup = function() { classifierHelper._setup = function() {
classifierCommonScript.addMessageListener("updateSuccess", classifierHelper._updateSuccess); gScript.addMessageListener("updateSuccess", classifierHelper._updateSuccess);
classifierCommonScript.addMessageListener("updateError", classifierHelper._updateError); gScript.addMessageListener("updateError", classifierHelper._updateError);
// cleanup will be called at end of each testcase to remove all the urls added to database. // cleanup will be called at end of each testcase to remove all the urls added to database.
SimpleTest.registerCleanupFunction(classifierHelper._cleanup); SimpleTest.registerCleanupFunction(classifierHelper._cleanup);
@ -107,9 +131,7 @@ classifierHelper._cleanup = function() {
return Promise.resolve(); return Promise.resolve();
} }
return new Promise(function(resolve, reject) { return classifierHelper.resetDB();
classifierHelper.removeUrlFromDB(classifierHelper._updatesToCleanup, resolve, reject);
});
}; };
classifierHelper._setup(); classifierHelper._setup();

Просмотреть файл

@ -49,7 +49,11 @@ SpecialPowers.pushPrefEnv(
{"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"], {"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
["urlclassifier.phishTable", "test-phish-simple"]]}, ["urlclassifier.phishTable", "test-phish-simple"]]},
function() { function() {
classifierHelper.addUrlToDB(testData, updateSuccess, updateError); classifierHelper.addUrlToDB(testData)
.then(updateSuccess)
.catch(err => {
updateError(err);
});
}); });
</script> </script>

Просмотреть файл

@ -56,7 +56,11 @@ SpecialPowers.pushPrefEnv(
{"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"], {"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
["urlclassifier.phishTable", "test-phish-simple"]]}, ["urlclassifier.phishTable", "test-phish-simple"]]},
function() { function() {
classifierHelper.addUrlToDB(testData, updateSuccess, updateError); classifierHelper.addUrlToDB(testData)
.then(updateSuccess)
.catch(err => {
updateError(err);
});
}); });
window.addEventListener("message", onmessage, false); window.addEventListener("message", onmessage, false);

Просмотреть файл

@ -117,19 +117,18 @@
} }
function addMalewareUrlToDB() { function addMalewareUrlToDB() {
return new Promise(function(resolve, reject) { var testData = [
var testData = [ { url: "malware.example.com/",
{ url: "malware.example.com/", db: "test-malware-simple"
db: "test-malware-simple" }
} ];
];
classifierHelper.addUrlToDB(testData, resolve, function() { return classifierHelper.addUrlToDB(testData)
ok(false, "Couldn't update classifier. Error code: " + errorCode); .catch(function(err) {
ok(false, "Couldn't update classifier. Error code: " + err);
// Abort test. // Abort test.
SimpleTest.finish(); SimpleTest.finish();
}); });
});
} }
function runTest() { function runTest() {