Bug 1416468 - Share container data parsing instead of duplicating it. r?
load() and ensureDataReady() both parsethe containers data, so put the parsing
into a function they can share, and use NetUtil.readInputStream() instead of
NetUtil.readInputStreamToString() to get bytes.
MozReview-Commit-ID: I4RYaFEhID6
--- a/toolkit/components/contextualidentity/ContextualIdentityService.jsm
+++ b/toolkit/components/contextualidentity/ContextualIdentityService.jsm
@@ -146,30 +146,17 @@ function _ContextualIdentityService(path
load() {
OS.File.read(this._path).then(bytes => {
// If synchronous loading happened in the meantime, exit now.
if (this._dataReady) {
return;
}
try {
- let data = JSON.parse(gTextDecoder.decode(bytes));
- if (data.version == 1) {
- this.resetDefault();
- }
- if (data.version != 2) {
- dump("ERROR - ContextualIdentityService - Unknown version found in " + this._path + "\n");
- this.loadError(null);
- return;
- }
-
- this._identities = data.identities;
- this._lastUserContextId = data.lastUserContextId;
-
- this._dataReady = true;
+ this.parseData(bytes);
} catch (error) {
this.loadError(error);
}
}, (error) => {
this.loadError(error);
});
},
@@ -300,34 +287,47 @@ function _ContextualIdentityService(path
icon: identity.icon,
color: identity.color,
userContextId: identity.userContextId,
};
return {wrappedJSObject};
},
+ parseData(bytes) {
+ let data = JSON.parse(gTextDecoder.decode(bytes));
+ if (data.version == 1) {
+ this.resetDefault();
+ }
+ if (data.version != 2) {
+ dump("ERROR - ContextualIdentityService - Unknown version found in " + this._path + "\n");
+ this.loadError(null);
+ return;
+ }
+
+ this._identities = data.identities;
+ this._lastUserContextId = data.lastUserContextId;
+
+ this._dataReady = true;
+ },
+
ensureDataReady() {
if (this._dataReady) {
return;
}
try {
// This reads the file and automatically detects the UTF-8 encoding.
let inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
inputStream.init(new FileUtils.File(this._path),
FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0);
try {
- let bytes = NetUtil.readInputStreamToString(inputStream, inputStream.available());
- let data = JSON.parse(gTextDecoder.decode(bytes));
- this._identities = data.identities;
- this._lastUserContextId = data.lastUserContextId;
-
- this._dataReady = true;
+ let bytes = NetUtil.readInputStream(inputStream, inputStream.available());
+ this.parseData(bytes);
} finally {
inputStream.close();
}
} catch (error) {
this.loadError(error);
}
},