Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/hosted_mode/protocol_files_proxy.js |
| diff --git a/third_party/WebKit/Source/devtools/hosted_mode/protocol_files_proxy.js b/third_party/WebKit/Source/devtools/hosted_mode/protocol_files_proxy.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4d41f5f269aaff07c620688de288c1b1a0aa159d |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/hosted_mode/protocol_files_proxy.js |
| @@ -0,0 +1,83 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +var request = require("./request"); |
| + |
| +var protocolFileToPath = { |
| + "/front_end/sdk/protocol/js_protocol.json": "core/inspector/browser_protocol.json", |
| + "/front_end/sdk/protocol/browser_protocol.json": "platform/v8_inspector/js_protocol.json" |
| +}; |
| +var protocolFiles = Object.keys(protocolFileToPath); |
| + |
| +function proxy(filePath, sendResponse) |
| +{ |
| + if (protocolFiles.indexOf(filePath) === -1) |
|
lushnikov
2016/07/22 21:06:04
if (!(filePath in protocolFileToPath))
return
chenwilliam
2016/07/22 23:20:31
Done.
|
| + return null; |
| + return request("http://localhost:9222/json/version") |
| + .then(onBrowserMetadata) |
| + .then(data => sendResponse(200, data)) |
| + .catch(err => console.log(`Error getting ${protocolFile}:`, err)); |
| + |
| + function onBrowserMetadata(metadata) |
| + { |
| + metadata = JSON.parse(metadata); |
|
lushnikov
2016/07/22 21:06:04
let's make a separate variable for the parsed json
chenwilliam
2016/07/22 23:20:31
Done.
|
| + var match = metadata["WebKit-Version"].match(/\s\(@(\b[0-9a-f]{5,40}\b)/); |
| + var commitHash = match[1]; |
| + protocolFileCache.setVersion(commitHash); |
| + if (protocolFileCache.hasFile(filePath)) |
| + return protocolFileCache.getFile(filePath); |
| + return request(getURL(filePath, commitHash)) |
|
lushnikov
2016/07/22 21:06:04
I would use this url as a cache keys
chenwilliam
2016/07/22 23:20:31
Done.
|
| + .then(text => new Buffer(text, "base64").toString("binary")) |
| + .then(data => { |
|
lushnikov
2016/07/22 21:06:04
style: we don't use multiline arrow functions, let
chenwilliam
2016/07/22 23:20:31
Done.
|
| + protocolFileCache.cacheFile(filePath, data); |
| + return data; |
| + }); |
| + } |
| +} |
| + |
| +function getURL(protocolFile, commitHash) |
| +{ |
| + var path = protocolFileToPath[protocolFile]; |
| + return `https://chromium.googlesource.com/chromium/src/+/${commitHash}/third_party/WebKit/Source/${path}?format=TEXT`; |
| +} |
| + |
| +var Cache = function() |
|
lushnikov
2016/07/22 21:06:04
you don't need this Cache at all - just inline it
chenwilliam
2016/07/22 23:20:31
Done.
|
| +{ |
| + this._data = new Map(); |
| + this._version = null; |
| +}; |
| + |
| +Cache.prototype = { |
| + |
| + setVersion: function(version) |
| + { |
| + this._version = version; |
| + }, |
| + |
| + hasFile: function(file) |
| + { |
| + return this._data.has(this._getKey(file)); |
| + }, |
| + |
| + getFile: function(file) |
| + { |
| + return this._data.get(this._getKey(file)); |
| + }, |
| + |
| + cacheFile: function(file, data) |
| + { |
| + this._data.set(this._getKey(file), data); |
| + }, |
| + |
| + _getKey: function(file) |
| + { |
| + return `${file}__version:${this._version}`; |
| + } |
| +}; |
| + |
| +var protocolFileCache = new Cache(); |
| + |
| +module.exports = { |
| + proxy |
| +}; |