Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4256)

Unified Diff: chrome/browser/resources/cryptotoken/etld.js

Issue 799923007: Enable 3rd party support for Security Keys. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove duplicate line from merge Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/cryptotoken/etld.js
diff --git a/chrome/browser/resources/cryptotoken/etld.js b/chrome/browser/resources/cryptotoken/etld.js
new file mode 100644
index 0000000000000000000000000000000000000000..f9c0a74002b7e99aa1346e23cffab671c7dce032
--- /dev/null
+++ b/chrome/browser/resources/cryptotoken/etld.js
@@ -0,0 +1,92 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @fileoverview Fetches the extended TLD + 1 of an origin.
+ */
+'use strict';
+
+/**
+ * A class to fetch the extended TLD + 1 of an origin.
+ * @param {!TextFetcher} fetcher A URL fetcher.
+ * @param {boolean=} opt_cache Whether to cache fetched results for the
+ * lifetime of this object.
+ * @constructor
+ */
+function EffectiveTldFetcher(fetcher, opt_cache) {
+ /** @private {(!Array.<string>)|undefined} */
+ this.eTlds_ = ETLD_NAMES_LIST;
+}
+
+/** @private {!Array.<string>} */
+EffectiveTldFetcher.effectiveTldListUrls_ = [];
+
+/**
+ * A fixed list of known TLDs.
+ * @private {!Array.<string>}
+ */
+EffectiveTldFetcher.fixed_tld_list_ = [];
+
+/**
+ * Sets a fixed list of known TLDs. This list is not canonical: if an origin
+ * is not found to be in this list, a canonical list is fetched from
+ * EffectiveTldFetcher.effectiveTldListUrls_.
+ * @param {!Array.<string>} tlds The list of known TLDs.
+ */
+EffectiveTldFetcher.setFixedTldList = function(tlds) {
+ EffectiveTldFetcher.fixed_tld_list_ = tlds;
+};
+
+/**
+ * @param {string} origin The origin.
+ * @return {Promise.<?string>} A promise for the eTLD+1 of origin, or null if it
+ * doesn't have one (e.g. the origin is invalid.)
+ */
+EffectiveTldFetcher.prototype.getEffectiveTldPlusOne = function(origin) {
+ var etld = this.getEffectiveTldPlusOne_(origin,
+ EffectiveTldFetcher.fixed_tld_list_);
+ if (etld) {
+ return Promise.resolve(/** @type {?string} */(etld));
+ }
+ return Promise.resolve(this.getEffectiveTldPlusOne_(origin, this.eTlds_));
+};
+
+/**
+ * @param {string} origin The origin.
+ * @param {!Array.<string>} eTlds The list of extended TLDs.
+ * @return {?string} The eTLD + 1 of the origin, or null if it doesn't have one
+ * (e.g. is invalid.)
+ * @private
+ */
+EffectiveTldFetcher.prototype.getEffectiveTldPlusOne_ =
+ function(origin, eTlds) {
+ var prev = '';
+ var host;
+ if (origin.indexOf('http://') == 0) {
+ host = origin.substring(7);
+ } else if (origin.indexOf('https://') == 0) {
+ host = origin.substring(8);
+ } else {
+ host = origin;
+ }
+ if (host.indexOf(':') != -1) {
+ host = host.substring(0, host.indexOf(':'));
+ }
+ if (host == 'localhost') {
+ return host;
+ }
+ // Loop over each possible subdomain, from longest to shortest, in order to
+ // find the longest matching eTLD first.
+ var next = host;
+ while (true) {
+ var dot = next.indexOf('.');
+ if (dot == -1) return null;
+ prev = next;
+ next = next.substring(dot + 1);
+ if (eTlds.indexOf(next) >= 0) {
+ return prev;
+ }
+ }
+};
+
« no previous file with comments | « chrome/browser/resources/cryptotoken/enroller.js ('k') | chrome/browser/resources/cryptotoken/etld_names_list.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698