browser/components/sessionstore/src/Utils.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/src/Utils.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,55 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["Utils"];
    1.11 +
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm", this);
    1.15 +
    1.16 +this.Utils = Object.freeze({
    1.17 +  makeURI: function (url) {
    1.18 +    return Services.io.newURI(url, null, null);
    1.19 +  },
    1.20 +
    1.21 +  /**
    1.22 +   * Returns true if the |url| passed in is part of the given root |domain|.
    1.23 +   * For example, if |url| is "www.mozilla.org", and we pass in |domain| as
    1.24 +   * "mozilla.org", this will return true. It would return false the other way
    1.25 +   * around.
    1.26 +   */
    1.27 +  hasRootDomain: function (url, domain) {
    1.28 +    let host;
    1.29 +
    1.30 +    try {
    1.31 +      host = this.makeURI(url).host;
    1.32 +    } catch (e) {
    1.33 +      // The given URL probably doesn't have a host.
    1.34 +      return false;
    1.35 +    }
    1.36 +
    1.37 +    let index = host.indexOf(domain);
    1.38 +    if (index == -1)
    1.39 +      return false;
    1.40 +
    1.41 +    if (host == domain)
    1.42 +      return true;
    1.43 +
    1.44 +    let prevChar = host[index - 1];
    1.45 +    return (index == (host.length - domain.length)) &&
    1.46 +           (prevChar == "." || prevChar == "/");
    1.47 +  },
    1.48 +
    1.49 +  shallowCopy: function (obj) {
    1.50 +    let retval = {};
    1.51 +
    1.52 +    for (let key of Object.keys(obj)) {
    1.53 +      retval[key] = obj[key];
    1.54 +    }
    1.55 +
    1.56 +    return retval;
    1.57 +  }
    1.58 +});

mercurial