2013-10-26 01:56:51 +04:00
|
|
|
var Cc = Components.classes;
|
|
|
|
var Ci = Components.interfaces;
|
|
|
|
var Cu = Components.utils;
|
|
|
|
var Cr = Components.results;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
|
|
|
|
2014-06-04 00:37:46 +04:00
|
|
|
var predictor = null;
|
2013-10-26 01:56:51 +04:00
|
|
|
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
|
|
|
var profile = null;
|
|
|
|
|
|
|
|
function extract_origin(uri) {
|
|
|
|
var o = uri.scheme + "://" + uri.asciiHost;
|
|
|
|
if (uri.port !== -1) {
|
|
|
|
o = o + ":" + uri.port;
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
var LoadContext = function _loadContext() {
|
|
|
|
};
|
|
|
|
|
|
|
|
LoadContext.prototype = {
|
|
|
|
usePrivateBrowsing: false,
|
|
|
|
|
|
|
|
getInterface: function loadContext_getInterface(iid) {
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: function loadContext_QueryInterface(iid) {
|
2014-06-04 00:37:46 +04:00
|
|
|
if (iid.equals(Ci.nsINetworkPredictorVerifier) ||
|
2013-10-26 01:56:51 +04:00
|
|
|
iid.equals(Ci.nsILoadContext)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var load_context = new LoadContext();
|
|
|
|
|
|
|
|
var Verifier = function _verifier(testing, expected_preconnects, expected_preresolves) {
|
|
|
|
this.verifying = testing;
|
|
|
|
this.expected_preconnects = expected_preconnects;
|
|
|
|
this.expected_preresolves = expected_preresolves;
|
|
|
|
};
|
|
|
|
|
|
|
|
Verifier.prototype = {
|
2015-01-15 00:59:04 +03:00
|
|
|
complete: false,
|
2013-10-26 01:56:51 +04:00
|
|
|
verifying: null,
|
|
|
|
expected_preconnects: null,
|
|
|
|
expected_preresolves: null,
|
|
|
|
|
|
|
|
getInterface: function verifier_getInterface(iid) {
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: function verifier_QueryInterface(iid) {
|
2014-06-04 00:37:46 +04:00
|
|
|
if (iid.equals(Ci.nsINetworkPredictorVerifier) ||
|
2013-10-26 01:56:51 +04:00
|
|
|
iid.equals(Ci.nsISupports)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
maybe_run_next_test: function verifier_maybe_run_next_test() {
|
|
|
|
if (this.expected_preconnects.length === 0 &&
|
2015-01-15 00:59:04 +03:00
|
|
|
this.expected_preresolves.length === 0 &&
|
|
|
|
!this.complete) {
|
|
|
|
this.complete = true;
|
2013-10-26 01:56:51 +04:00
|
|
|
do_check_true(true, "Well this is unexpected...");
|
2015-01-15 00:59:04 +03:00
|
|
|
// This kicks off the ability to run the next test
|
|
|
|
reset_predictor();
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onPredictPreconnect: function verifier_onPredictPreconnect(uri) {
|
|
|
|
var origin = extract_origin(uri);
|
|
|
|
var index = this.expected_preconnects.indexOf(origin);
|
2015-01-15 00:59:04 +03:00
|
|
|
if (index == -1 && !this.complete) {
|
2013-10-26 01:56:51 +04:00
|
|
|
do_check_true(false, "Got preconnect for unexpected uri " + origin);
|
|
|
|
} else {
|
|
|
|
this.expected_preconnects.splice(index, 1);
|
|
|
|
}
|
|
|
|
this.maybe_run_next_test();
|
|
|
|
},
|
|
|
|
|
|
|
|
onPredictDNS: function verifier_onPredictDNS(uri) {
|
|
|
|
var origin = extract_origin(uri);
|
|
|
|
var index = this.expected_preresolves.indexOf(origin);
|
2015-01-15 00:59:04 +03:00
|
|
|
if (index == -1 && !this.complete) {
|
2013-10-26 01:56:51 +04:00
|
|
|
do_check_true(false, "Got preresolve for unexpected uri " + origin);
|
|
|
|
} else {
|
|
|
|
this.expected_preresolves.splice(index, 1);
|
|
|
|
}
|
|
|
|
this.maybe_run_next_test();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-04 00:37:46 +04:00
|
|
|
function reset_predictor() {
|
|
|
|
predictor.reset();
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function newURI(s) {
|
|
|
|
return ios.newURI(s, null, null);
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:59:04 +03:00
|
|
|
var prepListener = {
|
|
|
|
numEntriesToOpen: 0,
|
|
|
|
numEntriesOpened: 0,
|
|
|
|
continueCallback: null,
|
|
|
|
|
|
|
|
QueryInterface: function (iid) {
|
|
|
|
if (iid.equals(Ci.nsICacheEntryOpenCallback)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function (entriesToOpen, cb) {
|
|
|
|
this.numEntriesOpened = 0;
|
|
|
|
this.numEntriesToOpen = entriesToOpen;
|
|
|
|
this.continueCallback = cb;
|
|
|
|
},
|
|
|
|
|
|
|
|
onCacheEntryCheck: function (entry, appCache) {
|
|
|
|
return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
|
|
|
|
},
|
|
|
|
|
|
|
|
onCacheEntryAvailable: function (entry, isNew, appCache, result) {
|
|
|
|
do_check_eq(result, Cr.NS_OK);
|
|
|
|
entry.setMetaDataElement("predictor_test", "1");
|
|
|
|
entry.metaDataReady();
|
|
|
|
this.numEntriesOpened++;
|
|
|
|
if (this.numEntriesToOpen == this.numEntriesOpened) {
|
|
|
|
this.continueCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function open_and_continue(uris, continueCallback) {
|
|
|
|
var lci = {
|
|
|
|
QueryInterface: function (iid) {
|
|
|
|
if (iid.equals(Ci.nsILoadContextInfo)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
isPrivate: false,
|
|
|
|
appId: Ci.nsILoadContextInfo.NO_APP_ID,
|
|
|
|
isInBrowserElement: false,
|
|
|
|
isAnonymous: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var css = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
|
|
|
|
.getService(Ci.nsICacheStorageService);
|
|
|
|
var ds = css.diskCacheStorage(lci, false);
|
|
|
|
|
|
|
|
prepListener.init(uris.length, continueCallback);
|
|
|
|
for (var i = 0; i < uris.length; ++i) {
|
|
|
|
ds.asyncOpenURI(uris[i], "", Ci.nsICacheStorage.OPEN_NORMALLY,
|
|
|
|
prepListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 01:56:51 +04:00
|
|
|
function test_link_hover() {
|
|
|
|
var uri = newURI("http://localhost:4444/foo/bar");
|
|
|
|
var referrer = newURI("http://localhost:4444/foo");
|
|
|
|
var preconns = ["http://localhost:4444"];
|
|
|
|
|
|
|
|
var verifier = new Verifier("hover", preconns, []);
|
2014-06-04 00:37:46 +04:00
|
|
|
predictor.predict(uri, referrer, predictor.PREDICT_LINK, load_context, verifier);
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_pageload() {
|
|
|
|
var toplevel = "http://localhost:4444/index.html";
|
|
|
|
var subresources = [
|
|
|
|
"http://localhost:4444/style.css",
|
|
|
|
"http://localhost:4443/jquery.js",
|
|
|
|
"http://localhost:4444/image.png"
|
|
|
|
];
|
|
|
|
|
|
|
|
var tluri = newURI(toplevel);
|
2015-01-15 00:59:04 +03:00
|
|
|
open_and_continue([tluri], function () {
|
|
|
|
// This is necessary to learn the origin stuff
|
|
|
|
predictor.learn(tluri, null, predictor.LEARN_LOAD_TOPLEVEL, load_context);
|
|
|
|
var preconns = [];
|
|
|
|
for (var i = 0; i < subresources.length; i++) {
|
|
|
|
var sruri = newURI(subresources[i]);
|
|
|
|
predictor.learn(sruri, tluri, predictor.LEARN_LOAD_SUBRESOURCE, load_context);
|
|
|
|
preconns.push(extract_origin(sruri));
|
|
|
|
}
|
2013-10-26 01:56:51 +04:00
|
|
|
|
2015-01-15 00:59:04 +03:00
|
|
|
var verifier = new Verifier("pageload", preconns, []);
|
|
|
|
predictor.predict(tluri, null, predictor.PREDICT_LOAD, load_context, verifier);
|
|
|
|
});
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_redirect() {
|
|
|
|
var initial = "http://localhost:4443/redirect";
|
|
|
|
var target = "http://localhost:4444/index.html";
|
|
|
|
var subresources = [
|
|
|
|
"http://localhost:4444/style.css",
|
|
|
|
"http://localhost:4443/jquery.js",
|
|
|
|
"http://localhost:4444/image.png"
|
|
|
|
];
|
|
|
|
|
|
|
|
var inituri = newURI(initial);
|
|
|
|
var targeturi = newURI(target);
|
2015-01-15 00:59:04 +03:00
|
|
|
open_and_continue([inituri, targeturi], function () {
|
|
|
|
predictor.learn(inituri, null, predictor.LEARN_LOAD_TOPLEVEL, load_context);
|
|
|
|
predictor.learn(targeturi, null, predictor.LEARN_LOAD_TOPLEVEL, load_context);
|
|
|
|
predictor.learn(targeturi, inituri, predictor.LEARN_LOAD_REDIRECT, load_context);
|
|
|
|
|
|
|
|
var preconns = [];
|
|
|
|
preconns.push(extract_origin(targeturi));
|
|
|
|
for (var i = 0; i < subresources.length; i++) {
|
|
|
|
var sruri = newURI(subresources[i]);
|
|
|
|
predictor.learn(sruri, targeturi, predictor.LEARN_LOAD_SUBRESOURCE, load_context);
|
|
|
|
preconns.push(extract_origin(sruri));
|
|
|
|
}
|
2013-10-26 01:56:51 +04:00
|
|
|
|
2015-01-15 00:59:04 +03:00
|
|
|
var verifier = new Verifier("redirect", preconns, []);
|
|
|
|
predictor.predict(inituri, null, predictor.PREDICT_LOAD, load_context, verifier);
|
|
|
|
});
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_startup() {
|
|
|
|
var uris = [
|
|
|
|
"http://localhost:4444/startup",
|
|
|
|
"http://localhost:4443/startup"
|
|
|
|
];
|
|
|
|
var preconns = [];
|
|
|
|
for (var i = 0; i < uris.length; i++) {
|
|
|
|
var uri = newURI(uris[i]);
|
2014-06-04 00:37:46 +04:00
|
|
|
predictor.learn(uri, null, predictor.LEARN_STARTUP, load_context);
|
2013-10-26 01:56:51 +04:00
|
|
|
preconns.push(extract_origin(uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
var verifier = new Verifier("startup", preconns, []);
|
2014-06-04 00:37:46 +04:00
|
|
|
predictor.predict(null, null, predictor.PREDICT_STARTUP, load_context, verifier);
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_dns() {
|
|
|
|
var toplevel = "http://localhost:4444/index.html";
|
|
|
|
var subresource = "http://localhost:4443/jquery.js";
|
|
|
|
|
|
|
|
var tluri = newURI(toplevel);
|
2015-01-15 00:59:04 +03:00
|
|
|
open_and_continue([tluri], function () {
|
|
|
|
predictor.learn(tluri, null, predictor.LEARN_LOAD_TOPLEVEL, load_context);
|
|
|
|
var sruri = newURI(subresource);
|
|
|
|
predictor.learn(sruri, tluri, predictor.LEARN_LOAD_SUBRESOURCE, load_context);
|
|
|
|
|
|
|
|
// Ensure that this will do preresolves
|
|
|
|
prefs.setIntPref("network.predictor.preconnect-min-confidence", 101);
|
|
|
|
|
|
|
|
var preresolves = [extract_origin(sruri)];
|
|
|
|
var verifier = new Verifier("dns", [], preresolves);
|
|
|
|
predictor.predict(tluri, null, predictor.PREDICT_LOAD, load_context, verifier);
|
|
|
|
});
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_origin() {
|
|
|
|
var toplevel = "http://localhost:4444/index.html";
|
|
|
|
var subresources = [
|
|
|
|
"http://localhost:4444/style.css",
|
|
|
|
"http://localhost:4443/jquery.js",
|
|
|
|
"http://localhost:4444/image.png"
|
|
|
|
];
|
|
|
|
|
|
|
|
var tluri = newURI(toplevel);
|
2015-01-15 00:59:04 +03:00
|
|
|
open_and_continue([tluri], function () {
|
|
|
|
predictor.learn(tluri, null, predictor.LEARN_LOAD_TOPLEVEL, load_context);
|
|
|
|
var preconns = [];
|
|
|
|
for (var i = 0; i < subresources.length; i++) {
|
|
|
|
var sruri = newURI(subresources[i]);
|
|
|
|
predictor.learn(sruri, tluri, predictor.LEARN_LOAD_SUBRESOURCE, load_context);
|
|
|
|
var origin = extract_origin(sruri);
|
|
|
|
if (preconns.indexOf(origin) === -1) {
|
|
|
|
preconns.push(origin);
|
|
|
|
}
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
2015-01-15 00:59:04 +03:00
|
|
|
var loaduri = newURI("http://localhost:4444/anotherpage.html");
|
|
|
|
var verifier = new Verifier("origin", preconns, []);
|
|
|
|
predictor.predict(loaduri, null, predictor.PREDICT_LOAD, load_context, verifier);
|
|
|
|
});
|
2013-10-26 01:56:51 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 00:03:00 +04:00
|
|
|
var prefs;
|
|
|
|
|
|
|
|
function cleanup() {
|
2015-01-15 00:59:04 +03:00
|
|
|
observer.cleaningUp = true;
|
|
|
|
predictor.reset();
|
2014-05-08 00:03:00 +04:00
|
|
|
}
|
|
|
|
|
2013-10-26 01:56:51 +04:00
|
|
|
var tests = [
|
2015-01-15 00:59:04 +03:00
|
|
|
// This must ALWAYS come first, to ensure a clean slate
|
|
|
|
reset_predictor,
|
2013-10-26 01:56:51 +04:00
|
|
|
test_link_hover,
|
|
|
|
test_pageload,
|
2015-01-15 00:59:04 +03:00
|
|
|
// TODO: These are disabled until the features are re-written
|
|
|
|
//test_redirect,
|
|
|
|
//test_startup,
|
|
|
|
// END DISABLED TESTS
|
|
|
|
test_origin,
|
2013-10-26 01:56:51 +04:00
|
|
|
test_dns,
|
2015-01-15 00:59:04 +03:00
|
|
|
// This must ALWAYS come last, to ensure we clean up after ourselves
|
|
|
|
cleanup
|
2013-10-26 01:56:51 +04:00
|
|
|
];
|
|
|
|
|
2015-01-15 00:59:04 +03:00
|
|
|
var observer = {
|
|
|
|
cleaningUp: false,
|
|
|
|
|
|
|
|
QueryInterface: function (iid) {
|
|
|
|
if (iid.equals(Ci.nsIObserver) ||
|
|
|
|
iid.equals(Ci.nsISupports)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function (subject, topic, data) {
|
|
|
|
if (topic != "predictor-reset-complete") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.cleaningUp) {
|
|
|
|
unregisterObserver();
|
|
|
|
}
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function registerObserver() {
|
|
|
|
var svc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
|
|
|
|
svc.addObserver(observer, "predictor-reset-complete", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregisterObserver() {
|
|
|
|
var svc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
|
|
|
|
svc.removeObserver(observer, "predictor-reset-complete");
|
|
|
|
}
|
|
|
|
|
2013-10-26 01:56:51 +04:00
|
|
|
function run_test() {
|
|
|
|
tests.forEach(add_test);
|
|
|
|
profile = do_get_profile();
|
2014-05-08 00:03:00 +04:00
|
|
|
prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
|
2014-06-04 00:37:46 +04:00
|
|
|
prefs.setBoolPref("network.predictor.enabled", true);
|
2015-01-15 00:59:04 +03:00
|
|
|
prefs.setBoolPref("network.predictor.cleaned-up", true);
|
2015-01-19 02:03:00 +03:00
|
|
|
prefs.setBoolPref("browser.cache.use_new_backend_temp", true);
|
|
|
|
prefs.setIntPref("browser.cache.use_new_backend", 1);
|
|
|
|
do_register_cleanup(() => {
|
|
|
|
prefs.clearUserPref("network.predictor.preconnect-min-confidence");
|
|
|
|
prefs.clearUserPref("network.predictor.enabled");
|
|
|
|
prefs.clearUserPref("network.predictor.cleaned-up");
|
|
|
|
prefs.clearUserPref("browser.cache.use_new_backend_temp");
|
|
|
|
prefs.clearUserPref("browser.cache.use_new_backend");
|
|
|
|
});
|
2014-06-04 00:37:46 +04:00
|
|
|
predictor = Cc["@mozilla.org/network/predictor;1"].getService(Ci.nsINetworkPredictor);
|
2015-01-15 00:59:04 +03:00
|
|
|
registerObserver();
|
2013-10-26 01:56:51 +04:00
|
|
|
run_next_test();
|
|
|
|
}
|