Skip to content

Commit

Permalink
Fetch the protocol from Chrome itself when possible
Browse files Browse the repository at this point in the history
Since 60.0.3097.0 Chrome provides a `/json/protocol` endpoint by which it
exposes the protocol.

https://bugs.chromium.org/p/chromium/issues/detail?id=538300#c10

This finally allows to properly close #10.
  • Loading branch information
cyrus-and committed Jun 6, 2017
1 parent c0a6b4d commit b84ba58
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ This behavior can be changed by setting the `remote` option to `true`
upon [connection](#cdpoptions-callback), in which case the remote instance is
*asked* to provide its own protocol descriptor.

Currently Chrome is not able to do that (see [#10]), so the protocol descriptor
is fetched from the proper [source repository].
Chrome < 60.0.3097.0 is not able to do that, so in that case the protocol
descriptor is fetched from the source repository.

To override the above behavior there are basically three options:

Expand All @@ -367,8 +367,6 @@ To override the above behavior there are basically three options:
fetched with `npm install`).

[local version]: lib/protocol.json
[#10]: https://github.com/cyrus-and/chrome-remote-interface/issues/10
[source repository]: https://chromium.googlesource.com/chromium/src/+/master/third_party/WebKit/Source/

Browser usage
-------------
Expand Down
29 changes: 19 additions & 10 deletions lib/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ module.exports.Protocol = promisesWrapper(function (options, callback) {
// use the proper protocol fetcher
let fetcher;
if (browser.match(/^(Headless)?Chrome\//)) {
fetcher = fetchFromChromeRepo;
// https://bugs.chromium.org/p/chromium/issues/detail?id=538300#c10
const firstJsonProtocolVersion = '60.0.3097.0';
const firstJsonProtocolBuild = explodeChromeVersion(firstJsonProtocolVersion)[2];
const chromeBuild = explodeChromeVersion(info.Browser.split('/')[1])[2];
if (chromeBuild < firstJsonProtocolBuild) {
fetcher = fetchFromChromeRepo;
} else {
fetcher = fetchFromHttpEndpoint;
}
} else if (browser.match(/^Microsoft Edge /)) {
fetcher = fetchFromHttpEndpoint;
} else if (browser.match(/^node.js\//)) {
Expand Down Expand Up @@ -144,15 +152,16 @@ function promisesWrapper(func) {
};
}

function explodeChromeVersion(v) {
return v.split('.').map(function (x) {
return parseInt(x);
});
}

// callback(err, descriptor)
// XXX this function needs a proper refactor but the inconsistency of the
// fetching process makes it useless for now
function fetchFromChromeRepo(options, info, callback) {
function explodeVersion(v) {
return v.split('.').map(function (x) {
return parseInt(x);
});
}
// attempt to fetch the protocol directly from the Chromium repository
// according to the current version
//
Expand All @@ -169,10 +178,10 @@ function fetchFromChromeRepo(options, info, callback) {
} else {
const lastBeforeSplitChromeVersion = '53.0.2758.1'; // before the split (https://crbug.com/580337)
const lastBeforeV8ChromeVersion = '55.0.2854.3'; // before using the JSON from the V8 repo
const chromeVersion = explodeVersion(info.Browser.split('/')[1]);
// according to https://www.chromium.org/developers/version-numbers
const beforeSplit = (chromeVersion[2] <= explodeVersion(lastBeforeSplitChromeVersion)[2]); // patch not meaningful
const beforeFromV8 = (chromeVersion[2] <= explodeVersion(lastBeforeV8ChromeVersion)[2]); // patch not meaningful
const chromeVersion = explodeChromeVersion(info.Browser.split('/')[1]);
// according to https://www.chromium.org/developers/version-numbers (patch not meaningful)
const beforeSplit = (chromeVersion[2] <= explodeChromeVersion(lastBeforeSplitChromeVersion)[2]);
const beforeFromV8 = (chromeVersion[2] <= explodeChromeVersion(lastBeforeV8ChromeVersion)[2]);
if (beforeSplit) {
urls = [`https://chromium.googlesource.com/chromium/src/+/${hash}/third_party/WebKit/Source/devtools/protocol.json?format=TEXT`];
} else if (beforeFromV8) {
Expand Down

0 comments on commit b84ba58

Please sign in to comment.