OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Fetches the extended TLD + 1 of an origin. |
| 7 */ |
| 8 'use strict'; |
| 9 |
| 10 /** |
| 11 * A class to fetch the extended TLD + 1 of an origin. |
| 12 * @param {!TextFetcher} fetcher A URL fetcher. |
| 13 * @param {boolean=} opt_cache Whether to cache fetched results for the |
| 14 * lifetime of this object. |
| 15 * @constructor |
| 16 */ |
| 17 function EffectiveTldFetcher(fetcher, opt_cache) { |
| 18 /** @private {(!Array.<string>)|undefined} */ |
| 19 this.eTlds_ = ETLD_NAMES_LIST; |
| 20 } |
| 21 |
| 22 /** @private {!Array.<string>} */ |
| 23 EffectiveTldFetcher.effectiveTldListUrls_ = []; |
| 24 |
| 25 /** |
| 26 * A fixed list of known TLDs. |
| 27 * @private {!Array.<string>} |
| 28 */ |
| 29 EffectiveTldFetcher.fixed_tld_list_ = []; |
| 30 |
| 31 /** |
| 32 * Sets a fixed list of known TLDs. This list is not canonical: if an origin |
| 33 * is not found to be in this list, a canonical list is fetched from |
| 34 * EffectiveTldFetcher.effectiveTldListUrls_. |
| 35 * @param {!Array.<string>} tlds The list of known TLDs. |
| 36 */ |
| 37 EffectiveTldFetcher.setFixedTldList = function(tlds) { |
| 38 EffectiveTldFetcher.fixed_tld_list_ = tlds; |
| 39 }; |
| 40 |
| 41 /** |
| 42 * @param {string} origin The origin. |
| 43 * @return {Promise.<?string>} A promise for the eTLD+1 of origin, or null if it |
| 44 * doesn't have one (e.g. the origin is invalid.) |
| 45 */ |
| 46 EffectiveTldFetcher.prototype.getEffectiveTldPlusOne = function(origin) { |
| 47 var etld = this.getEffectiveTldPlusOne_(origin, |
| 48 EffectiveTldFetcher.fixed_tld_list_); |
| 49 if (etld) { |
| 50 return Promise.resolve(/** @type {?string} */(etld)); |
| 51 } |
| 52 return Promise.resolve(this.getEffectiveTldPlusOne_(origin, this.eTlds_)); |
| 53 }; |
| 54 |
| 55 /** |
| 56 * @param {string} origin The origin. |
| 57 * @param {!Array.<string>} eTlds The list of extended TLDs. |
| 58 * @return {?string} The eTLD + 1 of the origin, or null if it doesn't have one |
| 59 * (e.g. is invalid.) |
| 60 * @private |
| 61 */ |
| 62 EffectiveTldFetcher.prototype.getEffectiveTldPlusOne_ = |
| 63 function(origin, eTlds) { |
| 64 var prev = ''; |
| 65 var host; |
| 66 if (origin.indexOf('http://') == 0) { |
| 67 host = origin.substring(7); |
| 68 } else if (origin.indexOf('https://') == 0) { |
| 69 host = origin.substring(8); |
| 70 } else { |
| 71 host = origin; |
| 72 } |
| 73 if (host.indexOf(':') != -1) { |
| 74 host = host.substring(0, host.indexOf(':')); |
| 75 } |
| 76 if (host == 'localhost') { |
| 77 return host; |
| 78 } |
| 79 // Loop over each possible subdomain, from longest to shortest, in order to |
| 80 // find the longest matching eTLD first. |
| 81 var next = host; |
| 82 while (true) { |
| 83 var dot = next.indexOf('.'); |
| 84 if (dot == -1) return null; |
| 85 prev = next; |
| 86 next = next.substring(dot + 1); |
| 87 if (eTlds.indexOf(next) >= 0) { |
| 88 return prev; |
| 89 } |
| 90 } |
| 91 }; |
| 92 |
OLD | NEW |