OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 /** | |
5 * @param {!Common.ParsedURL} parsedUrl | |
6 * @return {?string} | |
7 */ | |
8 ProductRegistry.nameForUrl = function(parsedUrl) { | |
9 var entry = ProductRegistry.entryForUrl(parsedUrl); | |
10 if (entry) | |
11 return entry.name; | |
12 return null; | |
13 }; | |
14 | |
15 /** | |
16 * @param {!Common.ParsedURL} parsedUrl | |
17 * @return {?ProductRegistry.ProductEntry} | |
18 */ | |
19 ProductRegistry.entryForUrl = function(parsedUrl) { | |
20 if (parsedUrl.isDataURL()) | |
21 return null; | |
22 // TODO(allada) This should be expanded to allow paths as as well as domain to
find a product. | |
23 var productsByDomainHash = ProductRegistry._productsByDomainHash; | |
24 // Remove leading www. if it is the only subdomain. | |
25 var domain = parsedUrl.domain().replace(/^www\.(?=[^.]+\.[^.]+$)/, ''); | |
26 | |
27 var previousIndex = -1; | |
28 var index = -1; | |
29 // Ensure we loop with full domain first, but do not loop over last part (ie:
".com"). | |
30 for (var nextIndex = domain.indexOf('.'); nextIndex !== -1; nextIndex = domain
.indexOf('.', nextIndex + 1)) { | |
31 var previousSubdomain = domain.substring(previousIndex + 1, index); | |
32 var subDomain = domain.substring(index + 1); | |
33 var prefixes = productsByDomainHash.get(ProductRegistry._hashForDomain(subDo
main)); | |
34 previousIndex = index; | |
35 index = nextIndex; | |
36 if (!prefixes) | |
37 continue; | |
38 // Exact match domains are always highest priority. | |
39 if ('' in prefixes && domain === subDomain) | |
40 return prefixes['']; | |
41 if (previousSubdomain) { | |
42 for (var prefix in prefixes) { | |
43 var domainPrefix = previousSubdomain.substr(0, prefix.length); | |
44 if (domainPrefix === prefix && prefix !== '') | |
45 return prefixes[prefix]; | |
46 } | |
47 } | |
48 // Process wildcard subdomain if no better match found. | |
49 if (prefixes && '*' in prefixes) | |
50 return prefixes['*']; | |
51 } | |
52 return null; | |
53 }; | |
54 | |
55 /** | |
56 * @param {!Common.ParsedURL} parsedUrl | |
57 * @return {?number} | |
58 */ | |
59 ProductRegistry.typeForUrl = function(parsedUrl) { | |
60 var entry = ProductRegistry.entryForUrl(parsedUrl); | |
61 if (entry) | |
62 return entry.type; | |
63 return null; | |
64 }; | |
65 | |
66 /** | |
67 * @param {string} domain | |
68 * @return {string} | |
69 */ | |
70 ProductRegistry._hashForDomain = function(domain) { | |
71 return ProductRegistry.sha1(domain).substr(0, 16); | |
72 }; | |
73 | |
74 /** | |
75 * @param {!Array<string>} productNames | |
76 * @param {!Array<!{hash: string, prefixes: !Object<string, !{product: number, t
ype: (number|undefined)}>}>} data | |
77 */ | |
78 ProductRegistry.register = function(productNames, data) { | |
79 for (var i = 0; i < data.length; i++) { | |
80 var entry = data[i]; | |
81 var prefixes = {}; | |
82 for (var prefix in entry.prefixes) { | |
83 var prefixEntry = entry.prefixes[prefix]; | |
84 var type = prefixEntry.type !== undefined ? prefixEntry.type : null; | |
85 prefixes[prefix] = {name: productNames[prefixEntry.product], type: type}; | |
86 } | |
87 ProductRegistry._productsByDomainHash.set(entry.hash, prefixes); | |
88 } | |
89 }; | |
90 | |
91 /** @typedef {!{name: string, type: ?number}} */ | |
92 ProductRegistry.ProductEntry; | |
93 | |
94 /** @type {!Map<string, !Object<string, !ProductRegistry.ProductEntry>>}} */ | |
95 ProductRegistry._productsByDomainHash = new Map(); | |
OLD | NEW |