browser/components/sessionstore/src/Utils.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["Utils"];
michael@0 8
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Services.jsm", this);
michael@0 12
michael@0 13 this.Utils = Object.freeze({
michael@0 14 makeURI: function (url) {
michael@0 15 return Services.io.newURI(url, null, null);
michael@0 16 },
michael@0 17
michael@0 18 /**
michael@0 19 * Returns true if the |url| passed in is part of the given root |domain|.
michael@0 20 * For example, if |url| is "www.mozilla.org", and we pass in |domain| as
michael@0 21 * "mozilla.org", this will return true. It would return false the other way
michael@0 22 * around.
michael@0 23 */
michael@0 24 hasRootDomain: function (url, domain) {
michael@0 25 let host;
michael@0 26
michael@0 27 try {
michael@0 28 host = this.makeURI(url).host;
michael@0 29 } catch (e) {
michael@0 30 // The given URL probably doesn't have a host.
michael@0 31 return false;
michael@0 32 }
michael@0 33
michael@0 34 let index = host.indexOf(domain);
michael@0 35 if (index == -1)
michael@0 36 return false;
michael@0 37
michael@0 38 if (host == domain)
michael@0 39 return true;
michael@0 40
michael@0 41 let prevChar = host[index - 1];
michael@0 42 return (index == (host.length - domain.length)) &&
michael@0 43 (prevChar == "." || prevChar == "/");
michael@0 44 },
michael@0 45
michael@0 46 shallowCopy: function (obj) {
michael@0 47 let retval = {};
michael@0 48
michael@0 49 for (let key of Object.keys(obj)) {
michael@0 50 retval[key] = obj[key];
michael@0 51 }
michael@0 52
michael@0 53 return retval;
michael@0 54 }
michael@0 55 });

mercurial