summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/sessionstore/Utils.jsm
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/modules/sessionstore/Utils.jsm
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/modules/sessionstore/Utils.jsm')
-rw-r--r--toolkit/modules/sessionstore/Utils.jsm107
1 files changed, 107 insertions, 0 deletions
diff --git a/toolkit/modules/sessionstore/Utils.jsm b/toolkit/modules/sessionstore/Utils.jsm
new file mode 100644
index 000000000..863bca6f5
--- /dev/null
+++ b/toolkit/modules/sessionstore/Utils.jsm
@@ -0,0 +1,107 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = ["Utils"];
+
+const Cu = Components.utils;
+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");
+
+function debug(msg) {
+ Services.console.logStringMessage("Utils: " + msg);
+}
+
+this.Utils = Object.freeze({
+ makeURI: function (url) {
+ return Services.io.newURI(url, null, null);
+ },
+
+ makeInputStream: function (aString) {
+ let stream = Cc["@mozilla.org/io/string-input-stream;1"].
+ createInstance(Ci.nsISupportsCString);
+ stream.data = aString;
+ return stream; // XPConnect will QI this to nsIInputStream for us.
+ },
+
+ /**
+ * Returns true if the |url| passed in is part of the given root |domain|.
+ * For example, if |url| is "www.mozilla.org", and we pass in |domain| as
+ * "mozilla.org", this will return true. It would return false the other way
+ * around.
+ */
+ hasRootDomain: function (url, domain) {
+ let host;
+
+ try {
+ host = this.makeURI(url).host;
+ } catch (e) {
+ // The given URL probably doesn't have a host.
+ return false;
+ }
+
+ let index = host.indexOf(domain);
+ if (index == -1)
+ return false;
+
+ if (host == domain)
+ return true;
+
+ let prevChar = host[index - 1];
+ return (index == (host.length - domain.length)) &&
+ (prevChar == "." || prevChar == "/");
+ },
+
+ shallowCopy: function (obj) {
+ let retval = {};
+
+ for (let key of Object.keys(obj)) {
+ retval[key] = obj[key];
+ }
+
+ 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;
+
+ try {
+ let principal = serializationHelper.deserializeObject(principal_b64);
+ principal.QueryInterface(Ci.nsIPrincipal);
+ return principal;
+ } catch (e) {
+ debug(`Failed to deserialize principal_b64 '${principal_b64}' ${e}`);
+ }
+ return null;
+ }
+});