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