diff --git a/browser/components/sessionstore/SessionHistory.jsm b/browser/components/sessionstore/SessionHistory.jsm index c31b1b8e3e58..68c7b7ebdf09 100644 --- a/browser/components/sessionstore/SessionHistory.jsm +++ b/browser/components/sessionstore/SessionHistory.jsm @@ -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) { diff --git a/browser/components/sessionstore/Utils.jsm b/browser/components/sessionstore/Utils.jsm index d42d6234c342..ea374137652d 100644 --- a/browser/components/sessionstore/Utils.jsm +++ b/browser/components/sessionstore/Utils.jsm @@ -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; } });