OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 /** | 4 /** |
5 * @param {!Common.ParsedURL} parsedUrl | 5 * @param {!Common.ParsedURL} parsedUrl |
6 * @return {?string} | 6 * @return {?string} |
7 */ | 7 */ |
8 ProductRegistry.nameForUrl = function(parsedUrl) { | 8 ProductRegistry.nameForUrl = function(parsedUrl) { |
9 if (parsedUrl.isDataURL()) | 9 if (parsedUrl.isDataURL()) |
10 return null; | 10 return null; |
11 // TODO(allada) This should be expanded to allow paths as as well as domain to
find a product. | 11 // TODO(allada) This should be expanded to allow paths as as well as domain to
find a product. |
12 var productsByDomain = ProductRegistry._productsByDomain; | 12 var productsByDomainHash = ProductRegistry._productsByDomainHash; |
13 var domain = parsedUrl.domain(); | 13 // Remove leading www. if it is the only subdomain. |
14 var domainParts = domain.split('.'); | 14 var domain = parsedUrl.domain().replace(/^www\.(?=[^.]+\.[^.]+$)/, ''); |
15 while (domainParts.length > 1) { | 15 |
16 var subDomain = domainParts.join('.'); | 16 var previousIndex = -1; |
17 var entry = productsByDomain.get(subDomain); | 17 var index = -1; |
18 if (entry && (!entry.exact || subDomain === domain)) | 18 // Ensure we loop with full domain first, but do not loop over last part (ie:
".com"). |
19 return entry.name; | 19 for (var nextIndex = domain.indexOf('.'); nextIndex !== -1; nextIndex = domain
.indexOf('.', nextIndex + 1)) { |
20 domainParts.shift(); | 20 var previousSubdomain = domain.substring(previousIndex + 1, index); |
| 21 var subDomain = domain.substring(index + 1); |
| 22 var prefixes = productsByDomainHash.get(ProductRegistry._hashForDomain(subDo
main)); |
| 23 previousIndex = index; |
| 24 index = nextIndex; |
| 25 if (!prefixes) |
| 26 continue; |
| 27 // Exact match domains are always highest priority. |
| 28 if ('' in prefixes && domain === subDomain) |
| 29 return prefixes['']; |
| 30 if (previousSubdomain) { |
| 31 for (var prefix in prefixes) { |
| 32 var domainPrefix = previousSubdomain.substr(0, prefix.length); |
| 33 if (domainPrefix === prefix && prefix !== '') |
| 34 return prefixes[prefix]; |
| 35 } |
| 36 } |
| 37 // Process wildcard subdomain if no better match found. |
| 38 if (prefixes && '*' in prefixes) |
| 39 return prefixes['*']; |
21 } | 40 } |
22 return null; | 41 return null; |
23 }; | 42 }; |
24 | 43 |
25 /** | 44 /** |
26 * @param {!Array<!{url: string, name: string, exact: boolean}>} data | 45 * @param {string} domain |
| 46 * @return {string} |
27 */ | 47 */ |
28 ProductRegistry.register = function(data) { | 48 ProductRegistry._hashForDomain = function(domain) { |
| 49 return ProductRegistry.sha1(domain).substr(0, 16); |
| 50 }; |
| 51 |
| 52 /** |
| 53 * @param {!Array<string>} productNames |
| 54 * @param {!Array<!{hash: string, prefixes: !Object<string, number>}>} data |
| 55 */ |
| 56 ProductRegistry.register = function(productNames, data) { |
29 for (var i = 0; i < data.length; i++) { | 57 for (var i = 0; i < data.length; i++) { |
30 var entry = data[i]; | 58 var entry = data[i]; |
31 ProductRegistry._productsByDomain.set(entry.url, entry); | 59 var prefixes = {}; |
| 60 for (var prefix in entry.prefixes) { |
| 61 var productNameIndex = entry.prefixes[prefix]; |
| 62 prefixes[prefix] = productNames[productNameIndex]; |
| 63 } |
| 64 ProductRegistry._productsByDomainHash.set(entry.hash, prefixes); |
32 } | 65 } |
33 }; | 66 }; |
34 | 67 |
35 /** @type {!Map<string, !{url: string, name: string, exact: boolean}>} */ | 68 /** @type {!Map<string, !Object<string, string>>}} */ |
36 ProductRegistry._productsByDomain = new Map(); | 69 ProductRegistry._productsByDomainHash = new Map(); |
OLD | NEW |