Bug 1368567 - Remove support for importing localstore.rdf at startup; r=florian

This removes some useless main-thread IO from the startup path.
This commit is contained in:
Ehsan Akhgari 2017-06-02 23:18:50 -04:00
parent 5694a686ee
commit 76dd200b85
2 changed files with 30 additions and 83 deletions

View File

@ -21,7 +21,6 @@ const STOREDB_FILENAME = "xulstore.json";
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
function XULStore() {
@ -65,11 +64,7 @@ XULStore.prototype = {
this._storeFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
this._storeFile.append(STOREDB_FILENAME);
if (!this._storeFile.exists()) {
this.import();
} else {
this.readFile();
}
this.readFile();
},
observe(subject, topic, data) {
@ -89,58 +84,6 @@ XULStore.prototype = {
Services.console.logStringMessage("XULStore: " + message);
},
import() {
let localStoreFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
localStoreFile.append("localstore.rdf");
if (!localStoreFile.exists()) {
return;
}
const RDF = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
const persistKey = RDF.GetResource("http://home.netscape.com/NC-rdf#persist");
this.log("Import localstore from " + localStoreFile.path);
let localStoreURI = Services.io.newFileURI(localStoreFile).spec;
let localStore = RDF.GetDataSourceBlocking(localStoreURI);
let resources = localStore.GetAllResources();
while (resources.hasMoreElements()) {
let resource = resources.getNext().QueryInterface(Ci.nsIRDFResource);
let uri;
try {
uri = NetUtil.newURI(resource.ValueUTF8);
} catch (ex) {
continue; // skip invalid uris
}
// If this has a ref, then this is an attribute reference. Otherwise,
// this is a document reference.
if (!uri.hasRef)
continue;
// Verify that there the persist key is connected up.
let docURI = uri.specIgnoringRef;
if (!localStore.HasAssertion(RDF.GetResource(docURI), persistKey, resource, true))
continue;
let id = uri.ref;
let attrs = localStore.ArcLabelsOut(resource);
while (attrs.hasMoreElements()) {
let attr = attrs.getNext().QueryInterface(Ci.nsIRDFResource);
let value = localStore.GetTarget(resource, attr, true);
if (value instanceof Ci.nsIRDFLiteral) {
this.setValue(docURI, id, attr.ValueUTF8, value.Value);
}
}
}
},
readFile() {
const MODE_RDONLY = 0x01;
const FILE_PERMS = 0o600;
@ -153,7 +96,8 @@ XULStore.prototype = {
this._data = json.decodeFromStream(stream, stream.available());
} catch (e) {
this.log("Error reading JSON: " + e);
// Ignore problem, we'll just continue on with an empty dataset.
// This exception could mean that the file didn't exist.
// We'll just ignore the error and start with a blank slate.
} finally {
stream.close();
}

View File

@ -22,7 +22,7 @@ function run_test() {
function checkValue(uri, id, attr, reference) {
let value = XULStore.getValue(uri, id, attr);
do_check_true(value === reference);
do_check_eq(value, reference);
}
function checkValueExists(uri, id, attr, exists) {
@ -59,31 +59,31 @@ function getAttributes(uri, id) {
function checkArrays(a, b) {
a.sort();
b.sort();
do_check_true(a.toString() == b.toString());
do_check_eq(a.toString(), b.toString());
}
function checkOldStore() {
checkArrays(["addon-bar", "main-window", "sidebar-title"], getIDs(browserURI));
checkArrays(["collapsed"], getAttributes(browserURI, "addon-bar"));
checkArrays(["height", "screenX", "screenY", "sizemode", "width"],
checkArrays([], getIDs(browserURI));
checkArrays([], getAttributes(browserURI, "addon-bar"));
checkArrays([],
getAttributes(browserURI, "main-window"));
checkArrays(["value"], getAttributes(browserURI, "sidebar-title"));
checkArrays([], getAttributes(browserURI, "sidebar-title"));
checkValue(browserURI, "addon-bar", "collapsed", "true");
checkValue(browserURI, "main-window", "width", "994");
checkValue(browserURI, "main-window", "height", "768");
checkValue(browserURI, "main-window", "screenX", "4");
checkValue(browserURI, "main-window", "screenY", "22");
checkValue(browserURI, "main-window", "sizemode", "normal");
checkValue(browserURI, "addon-bar", "collapsed", "");
checkValue(browserURI, "main-window", "width", "");
checkValue(browserURI, "main-window", "height", "");
checkValue(browserURI, "main-window", "screenX", "");
checkValue(browserURI, "main-window", "screenY", "");
checkValue(browserURI, "main-window", "sizemode", "");
checkValue(browserURI, "sidebar-title", "value", "");
checkArrays(["lockCol", "prefCol"], getIDs(aboutURI));
checkArrays(["ordinal"], getAttributes(aboutURI, "lockCol"));
checkArrays(["ordinal", "sortDirection"], getAttributes(aboutURI, "prefCol"));
checkArrays([], getIDs(aboutURI));
checkArrays([], getAttributes(aboutURI, "lockCol"));
checkArrays([], getAttributes(aboutURI, "prefCol"));
checkValue(aboutURI, "prefCol", "ordinal", "1");
checkValue(aboutURI, "prefCol", "sortDirection", "ascending");
checkValue(aboutURI, "lockCol", "ordinal", "3");
checkValue(aboutURI, "prefCol", "ordinal", "");
checkValue(aboutURI, "prefCol", "sortDirection", "");
checkValue(aboutURI, "lockCol", "ordinal", "");
}
add_task(async function testImport() {
@ -92,9 +92,12 @@ add_task(async function testImport() {
await OS.File.copy(src, dst);
// Importing relies on XULStore not yet being loaded before this point.
// Test to make sure that localstore.rdf isn't imported any more.
XULStore = Cc["@mozilla.org/xul/xulstore;1"].getService(Ci.nsIXULStore);
checkOldStore();
// Set a value that a future test depends on manually
XULStore.setValue(browserURI, "main-window", "width", "994");
});
add_task(async function testTruncation() {
@ -133,21 +136,21 @@ add_task(async function testSetValue() {
checkValue(browserURI, "side-bar", "width", "");
XULStore.setValue(browserURI, "side-bar", "width", "1000");
checkValue(browserURI, "side-bar", "width", "1000");
checkArrays(["addon-bar", "main-window", "side-bar", "sidebar-title"], getIDs(browserURI));
checkArrays(["main-window", "side-bar"], getIDs(browserURI));
checkArrays(["width"], getAttributes(browserURI, "side-bar"));
// Modify existing property
checkValue(browserURI, "side-bar", "width", "1000");
XULStore.setValue(browserURI, "side-bar", "width", "1024");
checkValue(browserURI, "side-bar", "width", "1024");
checkArrays(["addon-bar", "main-window", "side-bar", "sidebar-title"], getIDs(browserURI));
checkArrays(["main-window", "side-bar"], getIDs(browserURI));
checkArrays(["width"], getAttributes(browserURI, "side-bar"));
// Add another attribute
checkValue(browserURI, "side-bar", "height", "");
XULStore.setValue(browserURI, "side-bar", "height", "1000");
checkValue(browserURI, "side-bar", "height", "1000");
checkArrays(["addon-bar", "main-window", "side-bar", "sidebar-title"], getIDs(browserURI));
checkArrays(["main-window", "side-bar"], getIDs(browserURI));
checkArrays(["width", "height"], getAttributes(browserURI, "side-bar"));
});
@ -157,14 +160,14 @@ add_task(async function testRemoveValue() {
XULStore.removeValue(browserURI, "side-bar", "width");
checkValue(browserURI, "side-bar", "width", "");
checkValueExists(browserURI, "side-bar", "width", false);
checkArrays(["addon-bar", "main-window", "side-bar", "sidebar-title"], getIDs(browserURI));
checkArrays(["main-window", "side-bar"], getIDs(browserURI));
checkArrays(["height"], getAttributes(browserURI, "side-bar"));
// Remove second attribute
checkValue(browserURI, "side-bar", "height", "1000");
XULStore.removeValue(browserURI, "side-bar", "height");
checkValue(browserURI, "side-bar", "height", "");
checkArrays(["addon-bar", "main-window", "sidebar-title"], getIDs(browserURI));
checkArrays(["main-window"], getIDs(browserURI));
// Removing an attribute that doesn't exists shouldn't fail
XULStore.removeValue(browserURI, "main-window", "bar");