Bug 701967 - Misc code cleanup in Sync's util.js; r=gps

This commit is contained in:
Pallani Kumaran 2012-03-13 02:12:47 +08:00
parent f9b602ca30
commit d00a421293
10 changed files with 73 additions and 126 deletions

View File

@ -88,8 +88,11 @@ WBORecord.prototype = {
// Take a base URI string, with trailing slash, and return the URI of this
// WBO based on collection and ID.
uri: function(base) {
if (this.collection && this.id)
return Utils.makeURL(base + this.collection + "/" + this.id);
if (this.collection && this.id) {
let url = Utils.makeURI(base + this.collection + "/" + this.id);
url.QueryInterface(Ci.nsIURL);
return url;
}
return null;
},

View File

@ -207,28 +207,6 @@ let Utils = {
return !!guid && this._base64url_regex.test(guid);
},
ensureOneOpen: let (windows = {}) function ensureOneOpen(window) {
// Close the other window if it exists
let url = window.location.href;
let other = windows[url];
if (other != null)
other.close();
// Save the new window for future closure
windows[url] = window;
// Actively clean up when the window is closed
window.addEventListener("unload", function() windows[url] = null, false);
},
// Returns a nsILocalFile representing a file relative to the current
// user's profile directory. The argument should be a string with
// unix-style slashes for directory names (these slashes are automatically
// converted to platform-specific path separators).
getProfileFile: function getProfileFile(path) {
return FileUtils.getFile("ProfD", path.split("/"), true);
},
/**
* Add a simple getter/setter to an object that defers access of a property
* to an inner property.
@ -292,70 +270,46 @@ let Utils = {
return true;
},
deepCopy: function Weave_deepCopy(thing, noSort) {
if (typeof(thing) != "object" || thing == null)
return thing;
let ret;
if (Array.isArray(thing)) {
ret = [];
for (let i = 0; i < thing.length; i++)
ret.push(Utils.deepCopy(thing[i], noSort));
} else {
ret = {};
let props = [p for (p in thing)];
if (!noSort)
props = props.sort();
props.forEach(function(k) ret[k] = Utils.deepCopy(thing[k], noSort));
}
return ret;
},
// Works on frames or exceptions, munges file:// URIs to shorten the paths
// FIXME: filename munging is sort of hackish, might be confusing if
// there are multiple extensions with similar filenames
formatFrame: function Utils_formatFrame(frame) {
let tmp = "<file:unknown>";
let file = frame.filename || frame.fileName;
if (file)
tmp = file.replace(/^(?:chrome|file):.*?([^\/\.]+\.\w+)$/, "$1");
if (frame.lineNumber)
tmp += ":" + frame.lineNumber;
if (frame.name)
tmp = frame.name + "()@" + tmp;
return tmp;
},
exceptionStr: function Weave_exceptionStr(e) {
let message = e.message ? e.message : e;
return message + " " + Utils.stackTrace(e);
},
stackTraceFromFrame: function Weave_stackTraceFromFrame(frame) {
let output = [];
while (frame) {
let str = Utils.formatFrame(frame);
if (str)
output.push(str);
frame = frame.caller;
}
return output.join(" < ");
},
stackTrace: function Weave_stackTrace(e) {
// Wrapped nsIException
if (e.location)
return "Stack trace: " + Utils.stackTraceFromFrame(e.location);
if (e.location){
let frame = e.location;
let output = [];
while (frame) {
// Works on frames or exceptions, munges file:// URIs to shorten the paths
// FIXME: filename munging is sort of hackish, might be confusing if
// there are multiple extensions with similar filenames
let str = "<file:unknown>";
let file = frame.filename || frame.fileName;
if (file){
str = file.replace(/^(?:chrome|file):.*?([^\/\.]+\.\w+)$/, "$1");
}
if (frame.lineNumber){
str += ":" + frame.lineNumber;
}
if (frame.name){
str = frame.name + "()@" + str;
}
if (str){
output.push(str);
}
frame = frame.caller;
}
return "Stack trace: " + output.join(" < ");
}
// Standard JS exception
if (e.stack)
if (e.stack){
return "JS Stack trace: " + e.stack.trim().replace(/\n/g, " < ").
replace(/@[^@]*?([^\/\.]+\.\w+:)/g, "@$1");
}
return "No traceback available";
},
@ -891,12 +845,6 @@ let Utils = {
}
},
makeURL: function Weave_makeURL(URIString) {
let url = Utils.makeURI(URIString);
url.QueryInterface(Ci.nsIURL);
return url;
},
/**
* Load a json object from disk
*
@ -912,7 +860,7 @@ let Utils = {
if (that._log)
that._log.trace("Loading json from disk: " + filePath);
let file = Utils.getProfileFile(filePath);
let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
if (!file.exists()) {
callback.call(that);
return;
@ -957,7 +905,7 @@ let Utils = {
if (that._log)
that._log.trace("Saving json to disk: " + filePath);
let file = Utils.getProfileFile(filePath);
let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
let json = typeof obj == "function" ? obj.call(that) : obj;
let out = JSON.stringify(json);

View File

@ -456,3 +456,26 @@ RotaryEngine.prototype = {
}
}
};
deepCopy: function deepCopy(thing, noSort) {
if (typeof(thing) != "object" || thing == null){
return thing;
}
let ret;
if (Array.isArray(thing)) {
ret = [];
for (let i = 0; i < thing.length; i++){
ret.push(deepCopy(thing[i], noSort));
}
} else {
ret = {};
let props = [p for (p in thing)];
if (!noSort){
props = props.sort();
}
props.forEach(function(k) ret[k] = deepCopy(thing[k], noSort));
}
return ret;
};

View File

@ -56,7 +56,7 @@ store.wipe();
function makeLivemark(p, mintGUID) {
let b = new Livemark("bookmarks", p.id);
// Copy here, because tests mutate the contents.
b.cleartext = Utils.deepCopy(p);
b.cleartext = deepCopy(p);
if (mintGUID)
b.id = Utils.makeGUID();

View File

@ -495,8 +495,10 @@ function run_test() {
let query = "?" + args.join("&");
let uri1 = Utils.makeURL("http://foo/" + query);
let uri2 = Utils.makeURL("http://foo/");
let uri1 = Utils.makeURI("http://foo/" + query)
.QueryInterface(Ci.nsIURL);
let uri2 = Utils.makeURI("http://foo/")
.QueryInterface(Ci.nsIURL);
uri2.query = query;
do_check_eq(uri1.query, uri2.query);
server.stop(do_test_finished);

View File

@ -661,8 +661,10 @@ add_test(function test_uri_construction() {
let query = "?" + args.join("&");
let uri1 = Utils.makeURL("http://foo/" + query);
let uri2 = Utils.makeURL("http://foo/");
let uri1 = Utils.makeURI("http://foo/" + query)
.QueryInterface(Ci.nsIURL);
let uri2 = Utils.makeURI("http://foo/")
.QueryInterface(Ci.nsIURL);
uri2.query = query;
do_check_eq(uri1.query, uri2.query);

View File

@ -80,7 +80,7 @@ function fakeSessionSvc(url, numtabs) {
if (numtabs) {
let tabs = obj.windows[0].tabs;
for (let i = 0; i < numtabs-1; i++)
tabs.push(Utils.deepCopy(tabs[0]));
tabs.push(deepCopy(tabs[0]));
}
return JSON.stringify(obj);
}

View File

@ -2,7 +2,7 @@ Cu.import("resource://services-sync/util.js");
function run_test() {
let thing = {o: {foo: "foo", bar: ["bar"]}, a: ["foo", {bar: "bar"}]};
let ret = Utils.deepCopy(thing);
let ret = deepCopy(thing);
do_check_neq(ret, thing)
do_check_neq(ret.o, thing.o);
do_check_neq(ret.o.bar, thing.o.bar);

View File

@ -69,7 +69,8 @@ add_test(function test_load_logging() {
_("Verify that reads and read errors are logged.");
// Write a file with some invalid JSON
let file = Utils.getProfileFile("weave/log.json");
let filePath = "weave/log.json";
let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
let fos = Cc["@mozilla.org/network/file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
let flags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE

View File

@ -65,35 +65,3 @@ function _test_makeURI() {
do_check_eq(Utils.makeURI("chrome://badstuff"), undefined);
do_check_eq(Utils.makeURI("this is a test"), undefined);
}
function _test_makeURL() {
_("Check http uri");
let uri = "http://mozillalabs.com/";
do_check_true(Utils.makeURL(uri) instanceof Ci.nsIURL);
_("Check https uri");
let uris = "https://mozillalabs.com/";
do_check_true(Utils.makeURL(uris) instanceof Ci.nsIURL);
let uric = "chrome://browser/content/browser.xul";
do_check_true(Utils.makeURL(uric) instanceof Ci.nsIURL);
_("Check about uri");
let uria = "about:weave";
let except1;
try {
Utils.makeURL(uria);
} catch(e) {
except1 = e;
}
do_check_true(!!except1);
_("Check invalid uri");
let except2;
try {
Utils.makeURL("mozillalabs.com");
} catch(e) {
except2 = e;
}
do_check_true(!!except2);
}