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