michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["Utils"]; michael@0: michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm", this); michael@0: michael@0: this.Utils = Object.freeze({ michael@0: makeURI: function (url) { michael@0: return Services.io.newURI(url, null, null); michael@0: }, michael@0: michael@0: /** michael@0: * Returns true if the |url| passed in is part of the given root |domain|. michael@0: * For example, if |url| is "www.mozilla.org", and we pass in |domain| as michael@0: * "mozilla.org", this will return true. It would return false the other way michael@0: * around. michael@0: */ michael@0: hasRootDomain: function (url, domain) { michael@0: let host; michael@0: michael@0: try { michael@0: host = this.makeURI(url).host; michael@0: } catch (e) { michael@0: // The given URL probably doesn't have a host. michael@0: return false; michael@0: } michael@0: michael@0: let index = host.indexOf(domain); michael@0: if (index == -1) michael@0: return false; michael@0: michael@0: if (host == domain) michael@0: return true; michael@0: michael@0: let prevChar = host[index - 1]; michael@0: return (index == (host.length - domain.length)) && michael@0: (prevChar == "." || prevChar == "/"); michael@0: }, michael@0: michael@0: shallowCopy: function (obj) { michael@0: let retval = {}; michael@0: michael@0: for (let key of Object.keys(obj)) { michael@0: retval[key] = obj[key]; michael@0: } michael@0: michael@0: return retval; michael@0: } michael@0: });