Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 30 | const _loadedScripts = {}; |
| 31 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 32 | (function() { |
| 33 | const baseUrl = self.location ? self.location.origin + self.location.pathname : ''; |
| 34 | self._importScriptPathPrefix = baseUrl.substring(0, baseUrl.lastIndexOf('/') + 1); |
| 35 | })(); |
| 36 | |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 37 | const REMOTE_MODULE_FALLBACK_REVISION = '@010ddcfda246975d194964ccf20038ebbdec6084'; |
| 38 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 39 | /** |
| 40 | * @unrestricted |
| 41 | */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 42 | class Runtime { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 43 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 44 | * @param {!Array.<!ModuleDescriptor>} descriptors |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 45 | */ |
| 46 | constructor(descriptors) { |
| 47 | /** @type {!Array<!Runtime.Module>} */ |
| 48 | this._modules = []; |
| 49 | /** @type {!Object<string, !Runtime.Module>} */ |
| 50 | this._modulesMap = {}; |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 51 | /** @type {!Array<!Extension>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 52 | this._extensions = []; |
| 53 | /** @type {!Object<string, !function(new:Object)>} */ |
| 54 | this._cachedTypeClasses = {}; |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 55 | /** @type {!Object<string, !ModuleDescriptor>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 56 | this._descriptorsMap = {}; |
| 57 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 58 | for (let i = 0; i < descriptors.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 59 | this._registerModule(descriptors[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 60 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 64 | * @private |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 65 | * @param {string} url |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 66 | * @param {boolean} asBinary |
| 67 | * @template T |
| 68 | * @return {!Promise.<T>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 69 | */ |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 70 | static _loadResourcePromise(url, asBinary) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 71 | return new Promise(load); |
| 72 | |
| 73 | /** |
| 74 | * @param {function(?)} fulfill |
| 75 | * @param {function(*)} reject |
| 76 | */ |
| 77 | function load(fulfill, reject) { |
| 78 | const xhr = new XMLHttpRequest(); |
| 79 | xhr.open('GET', url, true); |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 80 | if (asBinary) { |
| 81 | xhr.responseType = 'arraybuffer'; |
| 82 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 83 | xhr.onreadystatechange = onreadystatechange; |
| 84 | |
| 85 | /** |
| 86 | * @param {Event} e |
| 87 | */ |
| 88 | function onreadystatechange(e) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 89 | if (xhr.readyState !== XMLHttpRequest.DONE) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 90 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 91 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 92 | |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 93 | const {response} = e.target; |
| 94 | |
| 95 | const text = asBinary ? new TextDecoder().decode(response) : response; |
| 96 | |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 97 | // DevTools Proxy server can mask 404s as 200s, check the body to be sure |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 98 | const status = /^HTTP\/1.1 404/.test(text) ? 404 : xhr.status; |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 99 | |
| 100 | if ([0, 200, 304].indexOf(status) === -1) // Testing harness file:/// results in 0. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 101 | { |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 102 | reject(new Error('While loading from url ' + url + ' server responded with a status of ' + status)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 103 | } else { |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 104 | fulfill(response); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 105 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 106 | } |
| 107 | xhr.send(null); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 112 | * @param {string} url |
| 113 | * @return {!Promise.<string>} |
| 114 | */ |
Ingvar Stepanyan | 1a8762b | 2019-10-29 18:24:39 +0000 | [diff] [blame] | 115 | static loadResourcePromise(url) { |
| 116 | return Runtime._loadResourcePromise(url, false); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param {string} url |
| 121 | * @return {!Promise.<!ArrayBuffer>} |
| 122 | */ |
| 123 | static loadBinaryResourcePromise(url) { |
| 124 | return Runtime._loadResourcePromise(url, true); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param {string} url |
| 129 | * @return {!Promise.<string>} |
| 130 | */ |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 131 | static loadResourcePromiseWithFallback(url) { |
| 132 | return Runtime.loadResourcePromise(url).catch(err => { |
| 133 | const urlWithFallbackVersion = url.replace(/@[0-9a-f]{40}/, REMOTE_MODULE_FALLBACK_REVISION); |
| 134 | // TODO(phulce): mark fallbacks in module.json and modify build script instead |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 135 | if (urlWithFallbackVersion === url || !url.includes('audits_worker_module')) { |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 136 | throw err; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 137 | } |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 138 | return Runtime.loadResourcePromise(urlWithFallbackVersion); |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 143 | * http://tools.ietf.org/html/rfc3986#section-5.2.4 |
| 144 | * @param {string} path |
| 145 | * @return {string} |
| 146 | */ |
| 147 | static normalizePath(path) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 148 | if (path.indexOf('..') === -1 && path.indexOf('.') === -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 149 | return path; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 150 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 151 | |
| 152 | const normalizedSegments = []; |
| 153 | const segments = path.split('/'); |
| 154 | for (let i = 0; i < segments.length; i++) { |
| 155 | const segment = segments[i]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 156 | if (segment === '.') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 157 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 158 | } else if (segment === '..') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 159 | normalizedSegments.pop(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 160 | } else if (segment) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 161 | normalizedSegments.push(segment); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 162 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 163 | } |
| 164 | let normalizedPath = normalizedSegments.join('/'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 165 | if (normalizedPath[normalizedPath.length - 1] === '/') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 166 | return normalizedPath; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 167 | } |
| 168 | if (path[0] === '/' && normalizedPath) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 169 | normalizedPath = '/' + normalizedPath; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 170 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 171 | if ((path[path.length - 1] === '/') || (segments[segments.length - 1] === '.') || |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 172 | (segments[segments.length - 1] === '..')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 173 | normalizedPath = normalizedPath + '/'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 174 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 175 | |
| 176 | return normalizedPath; |
| 177 | } |
| 178 | |
| 179 | /** |
Mandy Chen | 20b48f6 | 2019-09-18 19:28:22 +0000 | [diff] [blame] | 180 | * @param {string} scriptName |
| 181 | * @param {string=} base |
| 182 | * @return {string} |
| 183 | */ |
| 184 | static getResourceURL(scriptName, base) { |
| 185 | const sourceURL = (base || self._importScriptPathPrefix) + scriptName; |
| 186 | const schemaIndex = sourceURL.indexOf('://') + 3; |
| 187 | let pathIndex = sourceURL.indexOf('/', schemaIndex); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 188 | if (pathIndex === -1) { |
Mandy Chen | 20b48f6 | 2019-09-18 19:28:22 +0000 | [diff] [blame] | 189 | pathIndex = sourceURL.length; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 190 | } |
Mandy Chen | 20b48f6 | 2019-09-18 19:28:22 +0000 | [diff] [blame] | 191 | return sourceURL.substring(0, pathIndex) + Runtime.normalizePath(sourceURL.substring(pathIndex)); |
| 192 | } |
| 193 | |
| 194 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 195 | * @param {!Array.<string>} scriptNames |
| 196 | * @param {string=} base |
| 197 | * @return {!Promise.<undefined>} |
| 198 | */ |
| 199 | static _loadScriptsPromise(scriptNames, base) { |
| 200 | /** @type {!Array<!Promise<undefined>>} */ |
| 201 | const promises = []; |
| 202 | /** @type {!Array<string>} */ |
| 203 | const urls = []; |
| 204 | const sources = new Array(scriptNames.length); |
| 205 | let scriptToEval = 0; |
| 206 | for (let i = 0; i < scriptNames.length; ++i) { |
| 207 | const scriptName = scriptNames[i]; |
Mandy Chen | 20b48f6 | 2019-09-18 19:28:22 +0000 | [diff] [blame] | 208 | const sourceURL = Runtime.getResourceURL(scriptName, base); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 209 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 210 | if (_loadedScripts[sourceURL]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 211 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 212 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 213 | urls.push(sourceURL); |
Patrick Hulce | b8c09f3 | 2018-06-11 21:28:22 +0000 | [diff] [blame] | 214 | const loadResourcePromise = |
| 215 | base ? Runtime.loadResourcePromiseWithFallback(sourceURL) : Runtime.loadResourcePromise(sourceURL); |
| 216 | promises.push( |
| 217 | loadResourcePromise.then(scriptSourceLoaded.bind(null, i), scriptSourceLoaded.bind(null, i, undefined))); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 218 | } |
| 219 | return Promise.all(promises).then(undefined); |
| 220 | |
| 221 | /** |
| 222 | * @param {number} scriptNumber |
| 223 | * @param {string=} scriptSource |
| 224 | */ |
| 225 | function scriptSourceLoaded(scriptNumber, scriptSource) { |
| 226 | sources[scriptNumber] = scriptSource || ''; |
| 227 | // Eval scripts as fast as possible. |
| 228 | while (typeof sources[scriptToEval] !== 'undefined') { |
| 229 | evaluateScript(urls[scriptToEval], sources[scriptToEval]); |
| 230 | ++scriptToEval; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @param {string} sourceURL |
| 236 | * @param {string=} scriptSource |
| 237 | */ |
| 238 | function evaluateScript(sourceURL, scriptSource) { |
| 239 | _loadedScripts[sourceURL] = true; |
| 240 | if (!scriptSource) { |
| 241 | // Do not reject, as this is normal in the hosted mode. |
| 242 | console.error('Empty response arrived for script \'' + sourceURL + '\''); |
| 243 | return; |
| 244 | } |
| 245 | self.eval(scriptSource + '\n//# sourceURL=' + sourceURL); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @param {string} url |
| 251 | * @param {boolean} appendSourceURL |
| 252 | * @return {!Promise<undefined>} |
| 253 | */ |
| 254 | static _loadResourceIntoCache(url, appendSourceURL) { |
| 255 | return Runtime.loadResourcePromise(url).then( |
| 256 | cacheResource.bind(this, url), cacheResource.bind(this, url, undefined)); |
| 257 | |
| 258 | /** |
| 259 | * @param {string} path |
| 260 | * @param {string=} content |
| 261 | */ |
| 262 | function cacheResource(path, content) { |
| 263 | if (!content) { |
| 264 | console.error('Failed to load resource: ' + path); |
| 265 | return; |
| 266 | } |
| 267 | const sourceURL = appendSourceURL ? Runtime.resolveSourceURL(path) : ''; |
| 268 | Runtime.cachedResources[path] = content + sourceURL; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @return {!Promise} |
| 274 | */ |
Pavel Feldman | 63e89eb | 2018-11-25 05:47:09 +0000 | [diff] [blame] | 275 | static async appStarted() { |
| 276 | return Runtime._appStartedPromise; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @param {string} appName |
| 281 | * @return {!Promise.<undefined>} |
| 282 | */ |
| 283 | static async startApplication(appName) { |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 284 | console.timeStamp('Root.Runtime.startApplication'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 285 | |
| 286 | const allDescriptorsByName = {}; |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 287 | for (let i = 0; i < Root.allDescriptors.length; ++i) { |
| 288 | const d = Root.allDescriptors[i]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 289 | allDescriptorsByName[d['name']] = d; |
| 290 | } |
| 291 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 292 | if (!Root.applicationDescriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 293 | let data = await Runtime.loadResourcePromise(appName + '.json'); |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 294 | Root.applicationDescriptor = JSON.parse(data); |
| 295 | let descriptor = Root.applicationDescriptor; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 296 | while (descriptor.extends) { |
| 297 | data = await Runtime.loadResourcePromise(descriptor.extends + '.json'); |
| 298 | descriptor = JSON.parse(data); |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 299 | Root.applicationDescriptor.modules = descriptor.modules.concat(Root.applicationDescriptor.modules); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 303 | const configuration = Root.applicationDescriptor.modules; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 304 | const moduleJSONPromises = []; |
| 305 | const coreModuleNames = []; |
| 306 | for (let i = 0; i < configuration.length; ++i) { |
| 307 | const descriptor = configuration[i]; |
| 308 | const name = descriptor['name']; |
| 309 | const moduleJSON = allDescriptorsByName[name]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 310 | if (moduleJSON) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 311 | moduleJSONPromises.push(Promise.resolve(moduleJSON)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 312 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 313 | moduleJSONPromises.push(Runtime.loadResourcePromise(name + '/module.json').then(JSON.parse.bind(JSON))); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 314 | } |
| 315 | if (descriptor['type'] === 'autostart') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 316 | coreModuleNames.push(name); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 317 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | const moduleDescriptors = await Promise.all(moduleJSONPromises); |
| 321 | |
| 322 | for (let i = 0; i < moduleDescriptors.length; ++i) { |
| 323 | moduleDescriptors[i].name = configuration[i]['name']; |
| 324 | moduleDescriptors[i].condition = configuration[i]['condition']; |
| 325 | moduleDescriptors[i].remote = configuration[i]['type'] === 'remote'; |
| 326 | } |
| 327 | self.runtime = new Runtime(moduleDescriptors); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 328 | if (coreModuleNames) { |
Pavel Feldman | 63e89eb | 2018-11-25 05:47:09 +0000 | [diff] [blame] | 329 | await self.runtime._loadAutoStartModules(coreModuleNames); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 330 | } |
Pavel Feldman | 63e89eb | 2018-11-25 05:47:09 +0000 | [diff] [blame] | 331 | Runtime._appStartedPromiseCallback(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
| 335 | * @param {string} appName |
| 336 | * @return {!Promise.<undefined>} |
| 337 | */ |
| 338 | static startWorker(appName) { |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 339 | return Root.Runtime.startApplication(appName).then(sendWorkerReady); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 340 | |
| 341 | function sendWorkerReady() { |
| 342 | self.postMessage('workerReady'); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * @param {string} name |
| 348 | * @return {?string} |
| 349 | */ |
| 350 | static queryParam(name) { |
Ingvar Stepanyan | d48ae08 | 2018-11-07 04:38:32 +0000 | [diff] [blame] | 351 | return Runtime._queryParamsObject.get(name); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * @return {string} |
| 356 | */ |
| 357 | static queryParamsString() { |
| 358 | return location.search; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * @return {!Object} |
| 363 | */ |
| 364 | static _experimentsSetting() { |
| 365 | try { |
| 366 | return /** @type {!Object} */ ( |
| 367 | JSON.parse(self.localStorage && self.localStorage['experiments'] ? self.localStorage['experiments'] : '{}')); |
| 368 | } catch (e) { |
| 369 | console.error('Failed to parse localStorage[\'experiments\']'); |
| 370 | return {}; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static _assert(value, message) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 375 | if (value) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 376 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 377 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 378 | Runtime._originalAssert.call(Runtime._console, value, message + ' ' + new Error().stack); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * @param {string} platform |
| 383 | */ |
| 384 | static setPlatform(platform) { |
| 385 | Runtime._platform = platform; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * @param {!Object} descriptor |
| 390 | * @return {boolean} |
| 391 | */ |
| 392 | static _isDescriptorEnabled(descriptor) { |
| 393 | const activatorExperiment = descriptor['experiment']; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 394 | if (activatorExperiment === '*') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 395 | return Runtime.experiments.supportEnabled(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 396 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 397 | if (activatorExperiment && activatorExperiment.startsWith('!') && |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 398 | Runtime.experiments.isEnabled(activatorExperiment.substring(1))) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 399 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 400 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 401 | if (activatorExperiment && !activatorExperiment.startsWith('!') && |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 402 | !Runtime.experiments.isEnabled(activatorExperiment)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 403 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 404 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 405 | const condition = descriptor['condition']; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 406 | if (condition && !condition.startsWith('!') && !Runtime.queryParam(condition)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 407 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 408 | } |
| 409 | if (condition && condition.startsWith('!') && Runtime.queryParam(condition.substring(1))) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 410 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 411 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 412 | return true; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * @param {string} path |
| 417 | * @return {string} |
| 418 | */ |
| 419 | static resolveSourceURL(path) { |
| 420 | let sourceURL = self.location.href; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 421 | if (self.location.search) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 422 | sourceURL = sourceURL.replace(self.location.search, ''); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 423 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 424 | sourceURL = sourceURL.substring(0, sourceURL.lastIndexOf('/') + 1) + path; |
| 425 | return '\n/*# sourceURL=' + sourceURL + ' */'; |
| 426 | } |
| 427 | |
Vidal Diazleal | 0b6aff4 | 2019-04-20 01:15:43 +0000 | [diff] [blame] | 428 | /** |
| 429 | * @param {function(string):string} localizationFunction |
| 430 | */ |
| 431 | static setL10nCallback(localizationFunction) { |
| 432 | Runtime._l10nCallback = localizationFunction; |
| 433 | } |
| 434 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 435 | useTestBase() { |
| 436 | Runtime._remoteBase = 'http://localhost:8000/inspector-sources/'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 437 | if (Runtime.queryParam('debugFrontend')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 438 | Runtime._remoteBase += 'debug/'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 439 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /** |
Mandy Chen | 20b48f6 | 2019-09-18 19:28:22 +0000 | [diff] [blame] | 443 | * @param {string} moduleName |
| 444 | * @return {!Runtime.Module} |
| 445 | */ |
| 446 | module(moduleName) { |
| 447 | return this._modulesMap[moduleName]; |
| 448 | } |
| 449 | |
| 450 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 451 | * @param {!ModuleDescriptor} descriptor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 452 | */ |
| 453 | _registerModule(descriptor) { |
| 454 | const module = new Runtime.Module(this, descriptor); |
| 455 | this._modules.push(module); |
| 456 | this._modulesMap[descriptor['name']] = module; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * @param {string} moduleName |
| 461 | * @return {!Promise.<undefined>} |
| 462 | */ |
| 463 | loadModulePromise(moduleName) { |
| 464 | return this._modulesMap[moduleName]._loadPromise(); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * @param {!Array.<string>} moduleNames |
| 469 | * @return {!Promise.<!Array.<*>>} |
| 470 | */ |
| 471 | _loadAutoStartModules(moduleNames) { |
| 472 | const promises = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 473 | for (let i = 0; i < moduleNames.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 474 | promises.push(this.loadModulePromise(moduleNames[i])); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 475 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 476 | return Promise.all(promises); |
| 477 | } |
| 478 | |
| 479 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 480 | * @param {!Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 481 | * @param {?function(function(new:Object)):boolean} predicate |
| 482 | * @return {boolean} |
| 483 | */ |
| 484 | _checkExtensionApplicability(extension, predicate) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 485 | if (!predicate) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 486 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 487 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 488 | const contextTypes = extension.descriptor().contextTypes; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 489 | if (!contextTypes) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 490 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 491 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 492 | for (let i = 0; i < contextTypes.length; ++i) { |
| 493 | const contextType = this._resolve(contextTypes[i]); |
| 494 | const isMatching = !!contextType && predicate(contextType); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 495 | if (isMatching) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 496 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 497 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 498 | } |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 503 | * @param {!Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 504 | * @param {?Object} context |
| 505 | * @return {boolean} |
| 506 | */ |
| 507 | isExtensionApplicableToContext(extension, context) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 508 | if (!context) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 509 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 510 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 511 | return this._checkExtensionApplicability(extension, isInstanceOf); |
| 512 | |
| 513 | /** |
| 514 | * @param {!Function} targetType |
| 515 | * @return {boolean} |
| 516 | */ |
| 517 | function isInstanceOf(targetType) { |
| 518 | return context instanceof targetType; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 523 | * @param {!Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 524 | * @param {!Set.<!Function>=} currentContextTypes |
| 525 | * @return {boolean} |
| 526 | */ |
| 527 | isExtensionApplicableToContextTypes(extension, currentContextTypes) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 528 | if (!extension.descriptor().contextTypes) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 529 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 530 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 531 | |
| 532 | return this._checkExtensionApplicability(extension, currentContextTypes ? isContextTypeKnown : null); |
| 533 | |
| 534 | /** |
| 535 | * @param {!Function} targetType |
| 536 | * @return {boolean} |
| 537 | */ |
| 538 | function isContextTypeKnown(targetType) { |
| 539 | return currentContextTypes.has(targetType); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * @param {*} type |
| 545 | * @param {?Object=} context |
| 546 | * @param {boolean=} sortByTitle |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 547 | * @return {!Array.<!Extension>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 548 | */ |
| 549 | extensions(type, context, sortByTitle) { |
| 550 | return this._extensions.filter(filter).sort(sortByTitle ? titleComparator : orderComparator); |
| 551 | |
| 552 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 553 | * @param {!Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 554 | * @return {boolean} |
| 555 | */ |
| 556 | function filter(extension) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 557 | if (extension._type !== type && extension._typeClass() !== type) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 558 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 559 | } |
| 560 | if (!extension.enabled()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 561 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 562 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 563 | return !context || extension.isApplicable(context); |
| 564 | } |
| 565 | |
| 566 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 567 | * @param {!Extension} extension1 |
| 568 | * @param {!Extension} extension2 |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 569 | * @return {number} |
| 570 | */ |
| 571 | function orderComparator(extension1, extension2) { |
| 572 | const order1 = extension1.descriptor()['order'] || 0; |
| 573 | const order2 = extension2.descriptor()['order'] || 0; |
| 574 | return order1 - order2; |
| 575 | } |
| 576 | |
| 577 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 578 | * @param {!Extension} extension1 |
| 579 | * @param {!Extension} extension2 |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 580 | * @return {number} |
| 581 | */ |
| 582 | function titleComparator(extension1, extension2) { |
| 583 | const title1 = extension1.title() || ''; |
| 584 | const title2 = extension2.title() || ''; |
| 585 | return title1.localeCompare(title2); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * @param {*} type |
| 591 | * @param {?Object=} context |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 592 | * @return {?Extension} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 593 | */ |
| 594 | extension(type, context) { |
| 595 | return this.extensions(type, context)[0] || null; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * @param {*} type |
| 600 | * @param {?Object=} context |
| 601 | * @return {!Promise.<!Array.<!Object>>} |
| 602 | */ |
| 603 | allInstances(type, context) { |
| 604 | return Promise.all(this.extensions(type, context).map(extension => extension.instance())); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * @return {?function(new:Object)} |
| 609 | */ |
| 610 | _resolve(typeName) { |
| 611 | if (!this._cachedTypeClasses[typeName]) { |
| 612 | const path = typeName.split('.'); |
| 613 | let object = self; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 614 | for (let i = 0; object && (i < path.length); ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 615 | object = object[path[i]]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 616 | } |
| 617 | if (object) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 618 | this._cachedTypeClasses[typeName] = /** @type function(new:Object) */ (object); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 619 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 620 | } |
| 621 | return this._cachedTypeClasses[typeName] || null; |
| 622 | } |
| 623 | |
| 624 | /** |
Alexei Filippov | f2e42e4 | 2019-04-04 21:40:45 +0000 | [diff] [blame] | 625 | * @param {function(new:T)} constructorFunction |
| 626 | * @return {!T} |
| 627 | * @template T |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 628 | */ |
| 629 | sharedInstance(constructorFunction) { |
| 630 | if (Runtime._instanceSymbol in constructorFunction && |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 631 | Object.getOwnPropertySymbols(constructorFunction).includes(Runtime._instanceSymbol)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 632 | return constructorFunction[Runtime._instanceSymbol]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 633 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 634 | |
| 635 | const instance = new constructorFunction(); |
| 636 | constructorFunction[Runtime._instanceSymbol] = instance; |
| 637 | return instance; |
| 638 | } |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 639 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 640 | |
Ingvar Stepanyan | d48ae08 | 2018-11-07 04:38:32 +0000 | [diff] [blame] | 641 | /** @type {!URLSearchParams} */ |
| 642 | Runtime._queryParamsObject = new URLSearchParams(Runtime.queryParamsString()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 643 | |
| 644 | Runtime._instanceSymbol = Symbol('instance'); |
| 645 | |
| 646 | /** |
| 647 | * @type {!Object.<string, string>} |
| 648 | */ |
| 649 | Runtime.cachedResources = { |
| 650 | __proto__: null |
| 651 | }; |
| 652 | |
| 653 | |
| 654 | Runtime._console = console; |
| 655 | Runtime._originalAssert = console.assert; |
| 656 | |
| 657 | |
| 658 | Runtime._platform = ''; |
| 659 | |
| 660 | |
| 661 | /** |
| 662 | * @unrestricted |
| 663 | */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 664 | class ModuleDescriptor { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 665 | constructor() { |
| 666 | /** |
| 667 | * @type {string} |
| 668 | */ |
| 669 | this.name; |
| 670 | |
| 671 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 672 | * @type {!Array.<!RuntimeExtensionDescriptor>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 673 | */ |
| 674 | this.extensions; |
| 675 | |
| 676 | /** |
| 677 | * @type {!Array.<string>|undefined} |
| 678 | */ |
| 679 | this.dependencies; |
| 680 | |
| 681 | /** |
| 682 | * @type {!Array.<string>} |
| 683 | */ |
| 684 | this.scripts; |
| 685 | |
| 686 | /** |
Tim van der Lippe | 647e33b | 2019-11-04 19:02:31 +0000 | [diff] [blame] | 687 | * @type {!Array.<string>} |
| 688 | */ |
| 689 | this.modules; |
| 690 | |
| 691 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 692 | * @type {string|undefined} |
| 693 | */ |
| 694 | this.condition; |
| 695 | |
| 696 | /** |
| 697 | * @type {boolean|undefined} |
| 698 | */ |
| 699 | this.remote; |
| 700 | } |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 701 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 702 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 703 | // This class is named like this, because we already have an "ExtensionDescriptor" in the externs |
| 704 | // These two do not share the same structure |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 705 | /** |
| 706 | * @unrestricted |
| 707 | */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 708 | class RuntimeExtensionDescriptor { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 709 | constructor() { |
| 710 | /** |
| 711 | * @type {string} |
| 712 | */ |
| 713 | this.type; |
| 714 | |
| 715 | /** |
| 716 | * @type {string|undefined} |
| 717 | */ |
| 718 | this.className; |
| 719 | |
| 720 | /** |
| 721 | * @type {string|undefined} |
| 722 | */ |
| 723 | this.factoryName; |
| 724 | |
| 725 | /** |
| 726 | * @type {!Array.<string>|undefined} |
| 727 | */ |
| 728 | this.contextTypes; |
| 729 | } |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 730 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 731 | |
Tim van der Lippe | 647e33b | 2019-11-04 19:02:31 +0000 | [diff] [blame] | 732 | // Module namespaces. |
| 733 | // NOTE: Update scripts/build/special_case_namespaces.json if you add a special cased namespace. |
| 734 | const specialCases = { |
| 735 | 'sdk': 'SDK', |
| 736 | 'js_sdk': 'JSSDK', |
| 737 | 'browser_sdk': 'BrowserSDK', |
| 738 | 'ui': 'UI', |
| 739 | 'object_ui': 'ObjectUI', |
| 740 | 'javascript_metadata': 'JavaScriptMetadata', |
| 741 | 'perf_ui': 'PerfUI', |
| 742 | 'har_importer': 'HARImporter', |
| 743 | 'sdk_test_runner': 'SDKTestRunner', |
| 744 | 'cpu_profiler_test_runner': 'CPUProfilerTestRunner' |
| 745 | }; |
| 746 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 747 | /** |
| 748 | * @unrestricted |
| 749 | */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 750 | class Module { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 751 | /** |
| 752 | * @param {!Runtime} manager |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 753 | * @param {!ModuleDescriptor} descriptor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 754 | */ |
| 755 | constructor(manager, descriptor) { |
| 756 | this._manager = manager; |
| 757 | this._descriptor = descriptor; |
| 758 | this._name = descriptor.name; |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 759 | /** @type {!Array<!Extension>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 760 | this._extensions = []; |
| 761 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 762 | /** @type {!Map<string, !Array<!Extension>>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 763 | this._extensionsByClassName = new Map(); |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 764 | const extensions = /** @type {?Array.<!RuntimeExtensionDescriptor>} */ (descriptor.extensions); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 765 | for (let i = 0; extensions && i < extensions.length; ++i) { |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 766 | const extension = new Extension(this, extensions[i]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 767 | this._manager._extensions.push(extension); |
| 768 | this._extensions.push(extension); |
| 769 | } |
| 770 | this._loadedForTest = false; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * @return {string} |
| 775 | */ |
| 776 | name() { |
| 777 | return this._name; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * @return {boolean} |
| 782 | */ |
| 783 | enabled() { |
| 784 | return Runtime._isDescriptorEnabled(this._descriptor); |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * @param {string} name |
| 789 | * @return {string} |
| 790 | */ |
| 791 | resource(name) { |
| 792 | const fullName = this._name + '/' + name; |
| 793 | const content = Runtime.cachedResources[fullName]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 794 | if (!content) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 795 | throw new Error(fullName + ' not preloaded. Check module.json'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 796 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 797 | return content; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * @return {!Promise.<undefined>} |
| 802 | */ |
| 803 | _loadPromise() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 804 | if (!this.enabled()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 805 | return Promise.reject(new Error('Module ' + this._name + ' is not enabled')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 806 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 807 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 808 | if (this._pendingLoadPromise) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 809 | return this._pendingLoadPromise; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 810 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 811 | |
| 812 | const dependencies = this._descriptor.dependencies; |
| 813 | const dependencyPromises = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 814 | for (let i = 0; dependencies && i < dependencies.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 815 | dependencyPromises.push(this._manager._modulesMap[dependencies[i]]._loadPromise()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 816 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 817 | |
| 818 | this._pendingLoadPromise = Promise.all(dependencyPromises) |
| 819 | .then(this._loadResources.bind(this)) |
Tim van der Lippe | 647e33b | 2019-11-04 19:02:31 +0000 | [diff] [blame] | 820 | .then(this._loadModules.bind(this)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 821 | .then(this._loadScripts.bind(this)) |
| 822 | .then(() => this._loadedForTest = true); |
| 823 | |
| 824 | return this._pendingLoadPromise; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * @return {!Promise.<undefined>} |
| 829 | * @this {Runtime.Module} |
| 830 | */ |
| 831 | _loadResources() { |
| 832 | const resources = this._descriptor['resources']; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 833 | if (!resources || !resources.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 834 | return Promise.resolve(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 835 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 836 | const promises = []; |
| 837 | for (let i = 0; i < resources.length; ++i) { |
| 838 | const url = this._modularizeURL(resources[i]); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 839 | const isHtml = url.endsWith('.html'); |
| 840 | promises.push(Runtime._loadResourceIntoCache(url, !isHtml /* appendSourceURL */)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 841 | } |
| 842 | return Promise.all(promises).then(undefined); |
| 843 | } |
| 844 | |
Tim van der Lippe | 647e33b | 2019-11-04 19:02:31 +0000 | [diff] [blame] | 845 | _loadModules() { |
| 846 | if (!this._descriptor.modules || !this._descriptor.modules.length) { |
| 847 | return Promise.resolve(); |
| 848 | } |
| 849 | |
| 850 | const namespace = this._computeNamespace(); |
| 851 | self[namespace] = self[namespace] || {}; |
| 852 | |
| 853 | // TODO(crbug.com/680046): We are in a worker and we dont support modules yet |
| 854 | if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) { |
| 855 | return Promise.resolve(); |
| 856 | } |
| 857 | |
| 858 | // TODO(crbug.com/1011811): Remove eval when we use TypeScript which does support dynamic imports |
| 859 | return eval(`import('./${this._name}/${this._name}.js')`); |
| 860 | } |
| 861 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 862 | /** |
| 863 | * @return {!Promise.<undefined>} |
| 864 | */ |
| 865 | _loadScripts() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 866 | if (!this._descriptor.scripts || !this._descriptor.scripts.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 867 | return Promise.resolve(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 868 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 869 | |
Tim van der Lippe | 647e33b | 2019-11-04 19:02:31 +0000 | [diff] [blame] | 870 | const namespace = this._computeNamespace(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 871 | self[namespace] = self[namespace] || {}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 872 | return Runtime._loadScriptsPromise(this._descriptor.scripts.map(this._modularizeURL, this), this._remoteBase()); |
| 873 | } |
| 874 | |
| 875 | /** |
Tim van der Lippe | 647e33b | 2019-11-04 19:02:31 +0000 | [diff] [blame] | 876 | * @return {string} |
| 877 | */ |
| 878 | _computeNamespace() { |
| 879 | return specialCases[this._name] || |
| 880 | this._name.split('_').map(a => a.substring(0, 1).toUpperCase() + a.substring(1)).join(''); |
| 881 | } |
| 882 | |
| 883 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 884 | * @param {string} resourceName |
| 885 | */ |
| 886 | _modularizeURL(resourceName) { |
| 887 | return Runtime.normalizePath(this._name + '/' + resourceName); |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * @return {string|undefined} |
| 892 | */ |
| 893 | _remoteBase() { |
| 894 | return !Runtime.queryParam('debugFrontend') && this._descriptor.remote && Runtime._remoteBase || undefined; |
| 895 | } |
| 896 | |
| 897 | /** |
Mandy Chen | 20b48f6 | 2019-09-18 19:28:22 +0000 | [diff] [blame] | 898 | * @param {string} resourceName |
| 899 | * @return {!Promise.<string>} |
| 900 | */ |
| 901 | fetchResource(resourceName) { |
| 902 | const base = this._remoteBase(); |
| 903 | const sourceURL = Runtime.getResourceURL(this._modularizeURL(resourceName), base); |
| 904 | return base ? Runtime.loadResourcePromiseWithFallback(sourceURL) : Runtime.loadResourcePromise(sourceURL); |
| 905 | } |
| 906 | |
| 907 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 908 | * @param {string} value |
| 909 | * @return {string} |
| 910 | */ |
| 911 | substituteURL(value) { |
| 912 | const base = this._remoteBase() || ''; |
| 913 | return value.replace(/@url\(([^\)]*?)\)/g, convertURL.bind(this)); |
| 914 | |
| 915 | function convertURL(match, url) { |
| 916 | return base + this._modularizeURL(url); |
| 917 | } |
| 918 | } |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 919 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 920 | |
| 921 | |
| 922 | /** |
| 923 | * @unrestricted |
| 924 | */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 925 | class Extension { /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 926 | * @param {!Runtime.Module} module |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 927 | * @param {!RuntimeExtensionDescriptor} descriptor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 928 | */ |
| 929 | constructor(module, descriptor) { |
| 930 | this._module = module; |
| 931 | this._descriptor = descriptor; |
| 932 | |
| 933 | this._type = descriptor.type; |
| 934 | this._hasTypeClass = this._type.charAt(0) === '@'; |
| 935 | |
| 936 | /** |
| 937 | * @type {?string} |
| 938 | */ |
| 939 | this._className = descriptor.className || null; |
| 940 | this._factoryName = descriptor.factoryName || null; |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * @return {!Object} |
| 945 | */ |
| 946 | descriptor() { |
| 947 | return this._descriptor; |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * @return {!Runtime.Module} |
| 952 | */ |
| 953 | module() { |
| 954 | return this._module; |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * @return {boolean} |
| 959 | */ |
| 960 | enabled() { |
| 961 | return this._module.enabled() && Runtime._isDescriptorEnabled(this.descriptor()); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * @return {?function(new:Object)} |
| 966 | */ |
| 967 | _typeClass() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 968 | if (!this._hasTypeClass) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 969 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 970 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 971 | return this._module._manager._resolve(this._type.substring(1)); |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * @param {?Object} context |
| 976 | * @return {boolean} |
| 977 | */ |
| 978 | isApplicable(context) { |
| 979 | return this._module._manager.isExtensionApplicableToContext(this, context); |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * @return {!Promise.<!Object>} |
| 984 | */ |
| 985 | instance() { |
| 986 | return this._module._loadPromise().then(this._createInstance.bind(this)); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * @return {boolean} |
| 991 | */ |
| 992 | canInstantiate() { |
| 993 | return !!(this._className || this._factoryName); |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * @return {!Object} |
| 998 | */ |
| 999 | _createInstance() { |
| 1000 | const className = this._className || this._factoryName; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1001 | if (!className) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1002 | throw new Error('Could not instantiate extension with no class'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1003 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1004 | const constructorFunction = self.eval(/** @type {string} */ (className)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1005 | if (!(constructorFunction instanceof Function)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1006 | throw new Error('Could not instantiate: ' + className); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1007 | } |
| 1008 | if (this._className) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1009 | return this._module._manager.sharedInstance(constructorFunction); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1010 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1011 | return new constructorFunction(this); |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * @return {string} |
| 1016 | */ |
| 1017 | title() { |
Vidal Diazleal | 0b6aff4 | 2019-04-20 01:15:43 +0000 | [diff] [blame] | 1018 | const title = this._descriptor['title-' + Runtime._platform] || this._descriptor['title']; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1019 | if (title && Runtime._l10nCallback) { |
Vidal Diazleal | 0b6aff4 | 2019-04-20 01:15:43 +0000 | [diff] [blame] | 1020 | return Runtime._l10nCallback(title); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1021 | } |
Vidal Diazleal | 0b6aff4 | 2019-04-20 01:15:43 +0000 | [diff] [blame] | 1022 | return title; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * @param {function(new:Object)} contextType |
| 1027 | * @return {boolean} |
| 1028 | */ |
| 1029 | hasContextType(contextType) { |
| 1030 | const contextTypes = this.descriptor().contextTypes; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1031 | if (!contextTypes) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1032 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1033 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1034 | for (let i = 0; i < contextTypes.length; ++i) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1035 | if (contextType === this._module._manager._resolve(contextTypes[i])) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1036 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1037 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1038 | } |
| 1039 | return false; |
| 1040 | } |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1041 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1042 | |
| 1043 | /** |
| 1044 | * @unrestricted |
| 1045 | */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1046 | class ExperimentsSupport { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1047 | constructor() { |
| 1048 | this._supportEnabled = Runtime.queryParam('experiments') !== null; |
| 1049 | this._experiments = []; |
| 1050 | this._experimentNames = {}; |
| 1051 | this._enabledTransiently = {}; |
Jeff Fisher | 85b08e1 | 2019-06-13 22:24:21 +0000 | [diff] [blame] | 1052 | /** @type {!Set<string>} */ |
| 1053 | this._serverEnabled = new Set(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * @return {!Array.<!Runtime.Experiment>} |
| 1058 | */ |
| 1059 | allConfigurableExperiments() { |
| 1060 | const result = []; |
| 1061 | for (let i = 0; i < this._experiments.length; i++) { |
| 1062 | const experiment = this._experiments[i]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1063 | if (!this._enabledTransiently[experiment.name]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1064 | result.push(experiment); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1065 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1066 | } |
| 1067 | return result; |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * @return {boolean} |
| 1072 | */ |
| 1073 | supportEnabled() { |
| 1074 | return this._supportEnabled; |
| 1075 | } |
| 1076 | |
| 1077 | /** |
| 1078 | * @param {!Object} value |
| 1079 | */ |
| 1080 | _setExperimentsSetting(value) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1081 | if (!self.localStorage) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1082 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1083 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1084 | self.localStorage['experiments'] = JSON.stringify(value); |
| 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * @param {string} experimentName |
| 1089 | * @param {string} experimentTitle |
| 1090 | * @param {boolean=} hidden |
| 1091 | */ |
| 1092 | register(experimentName, experimentTitle, hidden) { |
| 1093 | Runtime._assert(!this._experimentNames[experimentName], 'Duplicate registration of experiment ' + experimentName); |
| 1094 | this._experimentNames[experimentName] = true; |
| 1095 | this._experiments.push(new Runtime.Experiment(this, experimentName, experimentTitle, !!hidden)); |
| 1096 | } |
| 1097 | |
| 1098 | /** |
| 1099 | * @param {string} experimentName |
| 1100 | * @return {boolean} |
| 1101 | */ |
| 1102 | isEnabled(experimentName) { |
| 1103 | this._checkExperiment(experimentName); |
Pavel Feldman | c039591 | 2018-08-03 02:05:00 +0000 | [diff] [blame] | 1104 | // Check for explicitly disabled experiments first - the code could call setEnable(false) on the experiment enabled |
| 1105 | // by default and we should respect that. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1106 | if (Runtime._experimentsSetting()[experimentName] === false) { |
Pavel Feldman | c039591 | 2018-08-03 02:05:00 +0000 | [diff] [blame] | 1107 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1108 | } |
| 1109 | if (this._enabledTransiently[experimentName]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1110 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1111 | } |
| 1112 | if (this._serverEnabled.has(experimentName)) { |
Jeff Fisher | 85b08e1 | 2019-06-13 22:24:21 +0000 | [diff] [blame] | 1113 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1114 | } |
| 1115 | if (!this.supportEnabled()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1116 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1117 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1118 | |
| 1119 | return !!Runtime._experimentsSetting()[experimentName]; |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * @param {string} experimentName |
| 1124 | * @param {boolean} enabled |
| 1125 | */ |
| 1126 | setEnabled(experimentName, enabled) { |
| 1127 | this._checkExperiment(experimentName); |
| 1128 | const experimentsSetting = Runtime._experimentsSetting(); |
| 1129 | experimentsSetting[experimentName] = enabled; |
| 1130 | this._setExperimentsSetting(experimentsSetting); |
| 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * @param {!Array.<string>} experimentNames |
| 1135 | */ |
| 1136 | setDefaultExperiments(experimentNames) { |
| 1137 | for (let i = 0; i < experimentNames.length; ++i) { |
| 1138 | this._checkExperiment(experimentNames[i]); |
| 1139 | this._enabledTransiently[experimentNames[i]] = true; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /** |
Jeff Fisher | 85b08e1 | 2019-06-13 22:24:21 +0000 | [diff] [blame] | 1144 | * @param {!Array.<string>} experimentNames |
| 1145 | */ |
| 1146 | setServerEnabledExperiments(experimentNames) { |
| 1147 | for (const experiment of experimentNames) { |
| 1148 | this._checkExperiment(experiment); |
| 1149 | this._serverEnabled.add(experiment); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1154 | * @param {string} experimentName |
| 1155 | */ |
| 1156 | enableForTest(experimentName) { |
| 1157 | this._checkExperiment(experimentName); |
| 1158 | this._enabledTransiently[experimentName] = true; |
| 1159 | } |
| 1160 | |
| 1161 | clearForTest() { |
| 1162 | this._experiments = []; |
| 1163 | this._experimentNames = {}; |
| 1164 | this._enabledTransiently = {}; |
Jeff Fisher | 85b08e1 | 2019-06-13 22:24:21 +0000 | [diff] [blame] | 1165 | this._serverEnabled.clear(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | cleanUpStaleExperiments() { |
| 1169 | const experimentsSetting = Runtime._experimentsSetting(); |
| 1170 | const cleanedUpExperimentSetting = {}; |
| 1171 | for (let i = 0; i < this._experiments.length; ++i) { |
| 1172 | const experimentName = this._experiments[i].name; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1173 | if (experimentsSetting[experimentName]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1174 | cleanedUpExperimentSetting[experimentName] = true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1175 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1176 | } |
| 1177 | this._setExperimentsSetting(cleanedUpExperimentSetting); |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * @param {string} experimentName |
| 1182 | */ |
| 1183 | _checkExperiment(experimentName) { |
| 1184 | Runtime._assert(this._experimentNames[experimentName], 'Unknown experiment ' + experimentName); |
| 1185 | } |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1186 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1187 | |
| 1188 | /** |
| 1189 | * @unrestricted |
| 1190 | */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1191 | class Experiment { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1192 | /** |
| 1193 | * @param {!Runtime.ExperimentsSupport} experiments |
| 1194 | * @param {string} name |
| 1195 | * @param {string} title |
| 1196 | * @param {boolean} hidden |
| 1197 | */ |
| 1198 | constructor(experiments, name, title, hidden) { |
| 1199 | this.name = name; |
| 1200 | this.title = title; |
| 1201 | this.hidden = hidden; |
| 1202 | this._experiments = experiments; |
| 1203 | } |
| 1204 | |
| 1205 | /** |
| 1206 | * @return {boolean} |
| 1207 | */ |
| 1208 | isEnabled() { |
| 1209 | return this._experiments.isEnabled(this.name); |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * @param {boolean} enabled |
| 1214 | */ |
| 1215 | setEnabled(enabled) { |
| 1216 | this._experiments.setEnabled(this.name, enabled); |
| 1217 | } |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1218 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1219 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1220 | // This must be constructed after the query parameters have been parsed. |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1221 | Runtime.experiments = new ExperimentsSupport(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1222 | |
| 1223 | /** @type {Function} */ |
Pavel Feldman | 63e89eb | 2018-11-25 05:47:09 +0000 | [diff] [blame] | 1224 | Runtime._appStartedPromiseCallback; |
| 1225 | Runtime._appStartedPromise = new Promise(fulfil => Runtime._appStartedPromiseCallback = fulfil); |
Vidal Diazleal | 0b6aff4 | 2019-04-20 01:15:43 +0000 | [diff] [blame] | 1226 | |
| 1227 | /** @type {function(string):string} */ |
| 1228 | Runtime._l10nCallback; |
| 1229 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1230 | /** |
| 1231 | * @type {?string} |
| 1232 | */ |
| 1233 | Runtime._remoteBase; |
| 1234 | (function validateRemoteBase() { |
James Lissiak | 821d228 | 2019-05-15 15:32:04 +0000 | [diff] [blame] | 1235 | if (location.href.startsWith('devtools://devtools/bundled/') && Runtime.queryParam('remoteBase')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1236 | const versionMatch = /\/serve_file\/(@[0-9a-zA-Z]+)\/?$/.exec(Runtime.queryParam('remoteBase')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1237 | if (versionMatch) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1238 | Runtime._remoteBase = `${location.origin}/remote/serve_file/${versionMatch[1]}/`; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 1239 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1240 | } |
| 1241 | })(); |
| 1242 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1243 | self.Root = self.Root || {}; |
| 1244 | Root = Root || {}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1245 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1246 | // This gets all concatenated module descriptors in the release mode. |
| 1247 | Root.allDescriptors = []; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1248 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1249 | Root.applicationDescriptor = undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1250 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1251 | /** @constructor */ |
| 1252 | Root.Runtime = Runtime; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1253 | |
| 1254 | /** @type {!Runtime} */ |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 1255 | Root.runtime; |
| 1256 | |
| 1257 | /** @constructor */ |
| 1258 | Root.Runtime.ModuleDescriptor = ModuleDescriptor; |
| 1259 | |
| 1260 | /** @constructor */ |
| 1261 | Root.Runtime.ExtensionDescriptor = RuntimeExtensionDescriptor; |
| 1262 | |
| 1263 | /** @constructor */ |
| 1264 | Root.Runtime.Extension = Extension; |
| 1265 | |
| 1266 | /** @constructor */ |
| 1267 | Root.Runtime.Module = Module; |
| 1268 | |
| 1269 | /** @constructor */ |
| 1270 | Root.Runtime.ExperimentsSupport = ExperimentsSupport; |
| 1271 | |
| 1272 | /** @constructor */ |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 1273 | Root.Runtime.Experiment = Experiment; |