Bug 1294866 - Part 1: Move principal serialization to a utils file. r=mikedeboer

--HG--
extra : rebase_source : da212f0a03fff8cfaf5d76256e1558a9f6d095c7
This commit is contained in:
Steven Englehardt 2016-09-13 21:18:36 +08:00
Родитель b25bbcece4
Коммит 36e8d1bf37
2 изменённых файлов: 37 добавлений и 44 удалений

Просмотреть файл

@ -172,7 +172,7 @@ var SessionHistoryInternal = {
// Collect triggeringPrincipal data for the current history entry.
try {
let triggeringPrincipal = this.serializeTriggeringPrincipal(shEntry);
let triggeringPrincipal = Utils.serializePrincipal(shEntry.triggeringPrincipal);
if (triggeringPrincipal) {
entry.triggeringPrincipal_b64 = triggeringPrincipal;
}
@ -218,39 +218,6 @@ var SessionHistoryInternal = {
return entry;
},
/**
* Serialize triggeringPrincipal data contained in the given session history entry.
*
* @param shEntry
* The session history entry.
* @return The base64 encoded triggeringPrincipal data.
*/
serializeTriggeringPrincipal: function (shEntry) {
if (!shEntry.triggeringPrincipal) {
return null;
}
let binaryStream = Cc["@mozilla.org/binaryoutputstream;1"].
createInstance(Ci.nsIObjectOutputStream);
let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
pipe.init(false, false, 0, 0xffffffff, null);
binaryStream.setOutputStream(pipe.outputStream);
binaryStream.writeCompoundObject(shEntry.triggeringPrincipal, Ci.nsIPrincipal, true);
binaryStream.close();
// Now we want to read the data from the pipe's input end and encode it.
let scriptableStream = Cc["@mozilla.org/binaryinputstream;1"].
createInstance(Ci.nsIBinaryInputStream);
scriptableStream.setInputStream(pipe.inputStream);
let triggeringPrincipalBytes =
scriptableStream.readByteArray(scriptableStream.available());
// We can stop doing base64 encoding once our serialization into JSON
// is guaranteed to handle all chars in strings, including embedded
// nulls.
return btoa(String.fromCharCode.apply(null, triggeringPrincipalBytes));
},
/**
* Restores session history data for a given docShell.
*
@ -388,16 +355,7 @@ var SessionHistoryInternal = {
}
if (entry.triggeringPrincipal_b64) {
var triggeringPrincipalInput = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
var binaryData = atob(entry.triggeringPrincipal_b64);
triggeringPrincipalInput.setData(binaryData, binaryData.length);
var binaryStream = Cc["@mozilla.org/binaryinputstream;1"].
createInstance(Ci.nsIObjectInputStream);
binaryStream.setInputStream(triggeringPrincipalInput);
try { // Catch possible deserialization exceptions
shEntry.triggeringPrincipal = binaryStream.readObject(true);
} catch (ex) { debug(ex); }
shEntry.triggeringPrincipal = Utils.deserializePrincipal(entry.triggeringPrincipal_b64);
}
if (entry.children && shEntry instanceof Ci.nsISHContainer) {

Просмотреть файл

@ -11,6 +11,11 @@ const Cc = Components.classes;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/Services.jsm", this);
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
XPCOMUtils.defineLazyServiceGetter(this, "serializationHelper",
"@mozilla.org/network/serialization-helper;1",
"nsISerializationHelper");
this.Utils = Object.freeze({
makeURI: function (url) {
@ -60,5 +65,35 @@ this.Utils = Object.freeze({
}
return retval;
},
/**
* Serialize principal data.
*
* @param {nsIPrincipal} principal The principal to serialize.
* @return {String} The base64 encoded principal data.
*/
serializePrincipal(principal) {
if (!principal)
return null;
return serializationHelper.serializeToString(principal);
},
/**
* Deserialize a base64 encoded principal (serialized with
* Utils::serializePrincipal).
*
* @param {String} principal_b64 A base64 encoded serialized principal.
* @return {nsIPrincipal} A deserialized principal.
*/
deserializePrincipal(principal_b64) {
if (!principal_b64)
return null;
let principal = serializationHelper.deserializeObject(principal_b64);
principal.QueryInterface(Ci.nsIPrincipal);
return principal;
}
});