зеркало из https://github.com/mozilla/gecko-dev.git
Bug 608757 - Make Utils.jsonSave/Load use NetUtil.asyncCopy and asyncFetch, respectively. r=mconnor
This commit is contained in:
Родитель
f857f6374c
Коммит
6ea5f6c474
|
@ -49,6 +49,15 @@ Cu.import("resource://services-sync/ext/StringBundle.js");
|
|||
Cu.import("resource://services-sync/ext/Sync.js");
|
||||
Cu.import("resource://services-sync/log4moz.js");
|
||||
|
||||
let NetUtil;
|
||||
try {
|
||||
let ns = {};
|
||||
Cu.import("resource://gre/modules/NetUtil.jsm", ns);
|
||||
NetUtil = ns.NetUtil;
|
||||
} catch (ex) {
|
||||
// Firefox 3.5 :(
|
||||
}
|
||||
|
||||
/*
|
||||
* Utility functions
|
||||
*/
|
||||
|
@ -1067,17 +1076,37 @@ let Utils = {
|
|||
return;
|
||||
}
|
||||
|
||||
let json;
|
||||
try {
|
||||
let [is] = Utils.open(file, "<");
|
||||
json = JSON.parse(Utils.readStream(is));
|
||||
// Gecko < 2.0
|
||||
if (!NetUtil || !NetUtil.newChannel) {
|
||||
let json;
|
||||
try {
|
||||
let [is] = Utils.open(file, "<");
|
||||
json = JSON.parse(Utils.readStream(is));
|
||||
is.close();
|
||||
} catch (ex) {
|
||||
if (that._log)
|
||||
that._log.debug("Failed to load json: " + Utils.exceptionStr(ex));
|
||||
}
|
||||
callback.call(that, json);
|
||||
return;
|
||||
}
|
||||
|
||||
NetUtil.asyncFetch(file, function (is, result) {
|
||||
if (!Components.isSuccessCode(result)) {
|
||||
callback.call(that);
|
||||
return;
|
||||
}
|
||||
let string = NetUtil.readInputStreamToString(is, is.available());
|
||||
is.close();
|
||||
}
|
||||
catch (ex) {
|
||||
if (that._log)
|
||||
that._log.debug("Failed to load json: " + Utils.exceptionStr(ex));
|
||||
}
|
||||
callback.call(that, json);
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(string);
|
||||
} catch (ex) {
|
||||
if (that._log)
|
||||
that._log.debug("Failed to load json: " + Utils.exceptionStr(ex));
|
||||
}
|
||||
callback.call(that, json);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -1101,12 +1130,30 @@ let Utils = {
|
|||
let file = Utils.getProfileFile({ autoCreate: true, path: filePath });
|
||||
let json = typeof obj == "function" ? obj.call(that) : obj;
|
||||
let out = JSON.stringify(json);
|
||||
let [fos] = Utils.open(file, ">");
|
||||
fos.writeString(out);
|
||||
fos.close();
|
||||
if (typeof callback == "function") {
|
||||
callback.call(that);
|
||||
|
||||
// Firefox 3.5
|
||||
if (!NetUtil) {
|
||||
let [fos] = Utils.open(file, ">");
|
||||
fos.writeString(out);
|
||||
fos.close();
|
||||
if (typeof callback == "function") {
|
||||
callback.call(that);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let fos = Cc["@mozilla.org/network/safe-file-output-stream;1"]
|
||||
.createInstance(Ci.nsIFileOutputStream);
|
||||
fos.init(file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
|
||||
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
||||
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
||||
converter.charset = "UTF-8";
|
||||
let is = converter.convertToInputStream(out);
|
||||
NetUtil.asyncCopy(is, fos, function (result) {
|
||||
if (typeof callback == "function") {
|
||||
callback.call(that);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -1149,6 +1196,7 @@ let Utils = {
|
|||
return thisObj[name] = timer;
|
||||
},
|
||||
|
||||
// Gecko <2.0
|
||||
open: function open(pathOrFile, mode, perms) {
|
||||
let stream, file;
|
||||
|
||||
|
@ -1221,6 +1269,7 @@ let Utils = {
|
|||
return Str.errors.get("error.reason.unknown");
|
||||
},
|
||||
|
||||
// Gecko <2.0
|
||||
// assumes an nsIConverterInputStream
|
||||
readStream: function Weave_readStream(is) {
|
||||
let ret = "", str = {};
|
||||
|
|
|
@ -150,53 +150,21 @@ function FakePasswordService(contents) {
|
|||
|
||||
function FakeFilesystemService(contents) {
|
||||
this.fakeContents = contents;
|
||||
|
||||
let self = this;
|
||||
|
||||
Utils.getProfileFile = function fake_getProfileFile(arg) {
|
||||
let fakeNsILocalFile = {
|
||||
exists: function() {
|
||||
return this._fakeFilename in self.fakeContents;
|
||||
},
|
||||
_fakeFilename: (typeof(arg) == "object") ? arg.path : arg
|
||||
};
|
||||
return fakeNsILocalFile;
|
||||
Utils.jsonSave = function jsonSave(filePath, that, obj, callback) {
|
||||
let json = typeof obj == "function" ? obj.call(that) : obj;
|
||||
self.fakeContents["weave/" + filePath + ".json"] = JSON.stringify(json);
|
||||
callback.call(that);
|
||||
};
|
||||
|
||||
Utils.readStream = function fake_readStream(stream) {
|
||||
getTestLogger().info("Reading from stream.");
|
||||
return stream._fakeData;
|
||||
};
|
||||
|
||||
Utils.open = function fake_open(file, mode) {
|
||||
switch (mode) {
|
||||
case "<":
|
||||
mode = "reading";
|
||||
break;
|
||||
case ">":
|
||||
mode = "writing";
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unexpected mode: " + mode);
|
||||
Utils.jsonLoad = function jsonLoad(filePath, that, callback) {
|
||||
let obj;
|
||||
let json = self.fakeContents["weave/" + filePath + ".json"];
|
||||
if (json) {
|
||||
obj = JSON.parse(json);
|
||||
}
|
||||
|
||||
getTestLogger().info("Opening '" + file._fakeFilename + "' for " +
|
||||
mode + ".");
|
||||
var contents = "";
|
||||
if (file._fakeFilename in self.fakeContents && mode == "reading")
|
||||
contents = self.fakeContents[file._fakeFilename];
|
||||
let fakeStream = {
|
||||
writeString: function(data) {
|
||||
contents += data;
|
||||
getTestLogger().info("Writing data to local file '" +
|
||||
file._fakeFilename +"': " + data);
|
||||
},
|
||||
close: function() {
|
||||
self.fakeContents[file._fakeFilename] = contents;
|
||||
},
|
||||
get _fakeData() { return contents; }
|
||||
};
|
||||
return [fakeStream];
|
||||
callback.call(that, obj);
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче