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 /** | |
pfeldman
2017/04/27 22:20:49
blank line above.
allada
2017/04/28 01:20:07
Done.
| |
5 * @implements {ProductRegistry.Registry} | |
6 */ | |
7 ProductRegistryImpl.Registry = class { | |
8 constructor() { | |
9 } | |
10 | |
11 /** | |
12 * @override | |
13 * @param {!Common.ParsedURL} parsedUrl | |
14 * @return {?string} | |
15 */ | |
16 nameForUrl(parsedUrl) { | |
17 var entry = this.entryForUrl(parsedUrl); | |
18 if (entry) | |
19 return entry.name; | |
20 return null; | |
21 } | |
22 | |
23 /** | |
24 * @override | |
25 * @param {!Common.ParsedURL} parsedUrl | |
26 * @return {?ProductRegistry.Registry.ProductEntry} | |
27 */ | |
28 entryForUrl(parsedUrl) { | |
29 if (parsedUrl.isDataURL()) | |
30 return null; | |
31 // TODO(allada) This should be expanded to allow paths as as well as domain to find a product. | |
32 var productsByDomainHash = ProductRegistryImpl._productsByDomainHash; | |
33 // Remove leading www. if it is the only subdomain. | |
34 var domain = parsedUrl.domain().replace(/^www\.(?=[^.]+\.[^.]+$)/, ''); | |
35 | |
36 var previousIndex = -1; | |
37 var index = -1; | |
38 // Ensure we loop with full domain first, but do not loop over last part (ie : ".com"). | |
39 for (var nextIndex = domain.indexOf('.'); nextIndex !== -1; nextIndex = doma in.indexOf('.', nextIndex + 1)) { | |
40 var previousSubdomain = domain.substring(previousIndex + 1, index); | |
41 var subDomain = domain.substring(index + 1); | |
42 var prefixes = productsByDomainHash.get(ProductRegistryImpl._hashForDomain (subDomain)); | |
43 previousIndex = index; | |
44 index = nextIndex; | |
45 if (!prefixes) | |
46 continue; | |
47 // Exact match domains are always highest priority. | |
48 if ('' in prefixes && domain === subDomain) | |
49 return prefixes['']; | |
50 if (previousSubdomain) { | |
51 for (var prefix in prefixes) { | |
52 var domainPrefix = previousSubdomain.substr(0, prefix.length); | |
53 if (domainPrefix === prefix && prefix !== '') | |
54 return prefixes[prefix]; | |
55 } | |
56 } | |
57 // Process wildcard subdomain if no better match found. | |
58 if (prefixes && '*' in prefixes) | |
59 return prefixes['*']; | |
60 } | |
61 return null; | |
62 } | |
63 | |
64 /** | |
65 * @override | |
66 * @param {!Common.ParsedURL} parsedUrl | |
67 * @return {?number} | |
68 */ | |
69 typeForUrl(parsedUrl) { | |
70 var entry = this.entryForUrl(parsedUrl); | |
71 if (entry) | |
72 return entry.type; | |
73 return null; | |
74 } | |
75 | |
76 /** | |
77 * @override | |
78 * @param {!SDK.ResourceTreeFrame} frame | |
79 * @return {?ProductRegistry.Registry.ProductEntry} | |
80 */ | |
81 entryForFrame(frame) { | |
82 var entry; | |
83 if (frame.url) | |
84 entry = this.entryForUrl(new Common.ParsedURL(frame.url)); | |
85 if (entry) | |
86 return entry; | |
87 // We are not caching the frame url result because it may change. | |
88 var symbol = ProductRegistryImpl.Registry._productEntryForFrameSymbol; | |
89 if (!(symbol in frame)) | |
90 frame[symbol] = this._lookupStackTraceEntryForFrame(frame); | |
91 return frame[symbol]; | |
92 } | |
93 | |
94 /** | |
95 * @param {!SDK.ResourceTreeFrame} frame | |
96 * @return {?ProductRegistry.Registry.ProductEntry} | |
97 */ | |
98 _lookupStackTraceEntryForFrame(frame) { | |
99 var stackTrace = frame.creationStackTrace(); | |
100 var entry; | |
101 while (stackTrace) { | |
102 for (var stack of stackTrace.callFrames) { | |
103 if (stack.url) | |
104 entry = this.entryForUrl(new Common.ParsedURL(stack.url)); | |
105 if (entry) | |
106 return entry; | |
107 } | |
108 stackTrace = frame.parent; | |
109 } | |
110 return null; | |
111 } | |
112 }; | |
113 | |
114 ProductRegistryImpl.Registry._productEntryForFrameSymbol = Symbol('ProductEntryF orFrame'); | |
115 | |
116 /** | |
117 * @param {string} domain | |
118 * @return {string} | |
119 */ | |
120 ProductRegistryImpl._hashForDomain = function(domain) { | |
121 return ProductRegistryImpl.sha1(domain).substr(0, 16); | |
122 }; | |
123 | |
124 /** | |
125 * @param {!Array<string>} productNames | |
126 * @param {!Array<!{hash: string, prefixes: !Object<string, !{product: number, t ype: (number|undefined)}>}>} data | |
127 */ | |
128 ProductRegistryImpl.register = function(productNames, data) { | |
129 for (var i = 0; i < data.length; i++) { | |
130 var entry = data[i]; | |
131 var prefixes = {}; | |
132 for (var prefix in entry.prefixes) { | |
133 var prefixEntry = entry.prefixes[prefix]; | |
134 var type = prefixEntry.type !== undefined ? prefixEntry.type : null; | |
135 prefixes[prefix] = {name: productNames[prefixEntry.product], type: type}; | |
136 } | |
137 ProductRegistryImpl._productsByDomainHash.set(entry.hash, prefixes); | |
138 } | |
139 }; | |
140 | |
141 /** @type {!Map<string, !Object<string, !ProductRegistry.Registry.ProductEntry>> }} */ | |
142 ProductRegistryImpl._productsByDomainHash = new Map(); | |
OLD | NEW |