Commit 3f273419 authored by Mandy Chen's avatar Mandy Chen Committed by Commit Bot

DevTools: Add runtime functions that allow specific file requests

This CL adds the following functions:

* Runtime.getResourceURL: returns the request URL given resource name and
request base URL (e.g. devtools://devtools/bundle/ or
devtools://devtools/remote/serve_file/@010ddcfda246975d194964ccf20038ebbdec6084/)
* (Runtime) module: return Runtime.Module object given module name
* (Runtime.Module) fetchResource: make a request to load a script of the given
name in the current module. This is in the Runtime.Module class so that the
request can be adjusted based on if the module is remote or not.

We can leverage these functions to request specific files. I'm planning to
use them to request the appropriate locale file for lighthouse in the
audits worker, e.g.
`await self.runtime.module('audits_worker').fetchResource('lighthouse/locales/zh.json')`.

Bug: 941561
Change-Id: Ib5c7bfdc46bef7d98533c89b12c7d365d4a26dfd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1784981Reviewed-by: default avatarYang Guo <yangguo@chromium.org>
Reviewed-by: default avatarPaul Irish <paulirish@chromium.org>
Commit-Queue: Mandy Chen <mandy.chen@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#697719}
parent 72a98a7e
...@@ -149,6 +149,20 @@ var Runtime = class { // eslint-disable-line ...@@ -149,6 +149,20 @@ var Runtime = class { // eslint-disable-line
return normalizedPath; return normalizedPath;
} }
/**
* @param {string} scriptName
* @param {string=} base
* @return {string}
*/
static getResourceURL(scriptName, base) {
const sourceURL = (base || self._importScriptPathPrefix) + scriptName;
const schemaIndex = sourceURL.indexOf('://') + 3;
let pathIndex = sourceURL.indexOf('/', schemaIndex);
if (pathIndex === -1)
pathIndex = sourceURL.length;
return sourceURL.substring(0, pathIndex) + Runtime.normalizePath(sourceURL.substring(pathIndex));
}
/** /**
* @param {!Array.<string>} scriptNames * @param {!Array.<string>} scriptNames
* @param {string=} base * @param {string=} base
...@@ -163,13 +177,7 @@ var Runtime = class { // eslint-disable-line ...@@ -163,13 +177,7 @@ var Runtime = class { // eslint-disable-line
let scriptToEval = 0; let scriptToEval = 0;
for (let i = 0; i < scriptNames.length; ++i) { for (let i = 0; i < scriptNames.length; ++i) {
const scriptName = scriptNames[i]; const scriptName = scriptNames[i];
let sourceURL = (base || self._importScriptPathPrefix) + scriptName; const sourceURL = Runtime.getResourceURL(scriptName, base);
const schemaIndex = sourceURL.indexOf('://') + 3;
let pathIndex = sourceURL.indexOf('/', schemaIndex);
if (pathIndex === -1)
pathIndex = sourceURL.length;
sourceURL = sourceURL.substring(0, pathIndex) + Runtime.normalizePath(sourceURL.substring(pathIndex));
if (_loadedScripts[sourceURL]) if (_loadedScripts[sourceURL])
continue; continue;
...@@ -391,6 +399,14 @@ var Runtime = class { // eslint-disable-line ...@@ -391,6 +399,14 @@ var Runtime = class { // eslint-disable-line
Runtime._remoteBase += 'debug/'; Runtime._remoteBase += 'debug/';
} }
/**
* @param {string} moduleName
* @return {!Runtime.Module}
*/
module(moduleName) {
return this._modulesMap[moduleName];
}
/** /**
* @param {!Runtime.ModuleDescriptor} descriptor * @param {!Runtime.ModuleDescriptor} descriptor
*/ */
...@@ -790,6 +806,16 @@ Runtime.Module = class { ...@@ -790,6 +806,16 @@ Runtime.Module = class {
return !Runtime.queryParam('debugFrontend') && this._descriptor.remote && Runtime._remoteBase || undefined; return !Runtime.queryParam('debugFrontend') && this._descriptor.remote && Runtime._remoteBase || undefined;
} }
/**
* @param {string} resourceName
* @return {!Promise.<string>}
*/
fetchResource(resourceName) {
const base = this._remoteBase();
const sourceURL = Runtime.getResourceURL(this._modularizeURL(resourceName), base);
return base ? Runtime.loadResourcePromiseWithFallback(sourceURL) : Runtime.loadResourcePromise(sourceURL);
}
/** /**
* @param {string} value * @param {string} value
* @return {string} * @return {string}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment