Commit c167a965 authored by Ilya Sherman's avatar Ilya Sherman Committed by Commit Bot

Add code for formatting the X-Client-Data header in devtools

This is essentially pulling
https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2351775
into the Chromium repo, so that it can be maintained alongside the proto
definition that it is parsing and formatting.

R=mathias

Bug: 1103854
Change-Id: I25707cf94678682d856f085b3aa259b02361dfa0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2360517Reviewed-by: default avatarMathias Bynens <mathias@chromium.org>
Reviewed-by: default avatarDirk Pranke <dpranke@google.com>
Commit-Queue: Ilya Sherman <isherman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799228}
parent 69a842ef
......@@ -12,7 +12,7 @@ PRESUBMIT_VERSION = '2.0.0'
_EXCLUDED_PATHS = (
# Generated file.
(r"^components[\\/]variations[\\/]proto[\\/]devtools[\\/]"
r"client_variations_parser.js"),
r"client_variations.js"),
r"^native_client_sdk[\\/]src[\\/]build_tools[\\/]make_rules.py",
r"^native_client_sdk[\\/]src[\\/]build_tools[\\/]make_simple.py",
r"^native_client_sdk[\\/]src[\\/]tools[\\/].*.mk",
......
......@@ -15,13 +15,13 @@ if (is_linux || is_mac) {
# Run components/variations/proto/devtools/update_client_variations_parser.py
# to rebuild the library that is synced into the Dev Tools repository.
group("devtools") {
deps = [ ":client_variations_parser_uncompiled" ]
deps = [ ":client_variations_uncompiled" ]
}
# Note that the name passed to the `js_binary` rule implicitly defines a
# dependency on the file that matches the build rule's name.
js_binary("client_variations_parser_uncompiled") {
outputs = [ "$target_gen_dir/client_variations_parser_gen.js" ]
js_binary("client_variations_uncompiled") {
outputs = [ "$target_gen_dir/client_variations_gen.js" ]
deps = [ "..:proto_js" ]
closure_flags = strict_error_checking_closure_args + [
"compilation_level=ADVANCED_OPTIMIZATIONS",
......
/* eslint-disable */
// Copyright 2020 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.
// Note: This is a generated file. Do not edit by hand. Instead, run
// components/variations/proto/devtools/update_client_variations.py to update.
const gen = {};
// clang-format off
(function(){var f=this||self;function l(a,b){a=a.split(".");var c=f;a[0]in c||"undefined"==typeof c.execScript||c.execScript("var "+a[0]);for(var d;a.length&&(d=a.shift());)a.length||void 0===b?c[d]&&c[d]!==Object.prototype[d]?c=c[d]:c=c[d]={}:c[d]=b}
function m(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var c=Object.prototype.toString.call(a);if("[object Window]"==c)return"object";if("[object Array]"==c||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==c||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null";
else if("function"==b&&"undefined"==typeof a.call)return"object";return b}function n(a,b){function c(){}c.prototype=b.prototype;a.prototype=new c;a.prototype.constructor=a;a.base=function(d,e,g){for(var k=Array(arguments.length-2),h=2;h<arguments.length;h++)k[h-2]=arguments[h];return b.prototype[e].apply(d,k)}};function p(a){if(Error.captureStackTrace)Error.captureStackTrace(this,p);else{const b=Error().stack;b&&(this.stack=b)}a&&(this.message=String(a))}n(p,Error);p.prototype.name="CustomError";function q(a,b){a=a.split("%s");for(var c="",d=a.length-1,e=0;e<d;e++)c+=a[e]+(e<b.length?b[e]:"%s");p.call(this,c+a[d])}n(q,p);q.prototype.name="AssertionError";function r(a,b){throw new q("Failure"+(a?": "+a:""),Array.prototype.slice.call(arguments,1));};var t=null;function u(a){var b=a.length,c=3*b/4;c%3?c=Math.floor(c):-1!="=.".indexOf(a[b-1])&&(c=-1!="=.".indexOf(a[b-2])?c-2:c-1);var d=new Uint8Array(c),e=0;w(a,function(g){d[e++]=g});return d.subarray(0,e)}
function w(a,b){function c(I){for(;d<a.length;){var v=a.charAt(d++),x=t[v];if(null!=x)return x;if(!/^[\s\xa0]*$/.test(v))throw Error("Unknown base64 encoding at char: "+v);}return I}y();for(var d=0;;){var e=c(-1),g=c(0),k=c(64),h=c(64);if(64===h&&-1===e)break;b(e<<2|g>>4);64!=k&&(b(g<<4&240|k>>2),64!=h&&b(k<<6&192|h))}}
function y(){if(!t){t={};for(var a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split(""),b=["+/=","+/","-_=","-_.","-_"],c=0;5>c;c++)for(var d=a.concat(b[c].split("")),e=0;e<d.length;e++){var g=d[e];void 0===t[g]&&(t[g]=e)}}};function z(a){if(a.constructor===Uint8Array)return a;if(a.constructor===ArrayBuffer)return new Uint8Array(a);if("undefined"!=typeof Buffer&&a.constructor===Buffer)return new Uint8Array(a);if(a.constructor===Array)return new Uint8Array(a);if(a.constructor===String)return u(a);r("Type not convertible to Uint8Array.");return new Uint8Array(0)};function A(a){this.b=null;this.a=this.c=this.g=0;a&&B(this,a)}var C=[];function B(a,b){a.b=z(b);a.g=0;a.c=a.b.length;a.a=a.g}A.prototype.reset=function(){this.a=this.g};
A.prototype.f=function(){var a=this.b;var b=a[this.a];var c=b&127;if(128>b)return this.a+=1,c;b=a[this.a+1];c|=(b&127)<<7;if(128>b)return this.a+=2,c;b=a[this.a+2];c|=(b&127)<<14;if(128>b)return this.a+=3,c;b=a[this.a+3];c|=(b&127)<<21;if(128>b)return this.a+=4,c;b=a[this.a+4];c|=(b&15)<<28;if(128>b)return this.a+=5,c>>>0;this.a+=5;128<=a[this.a++]&&128<=a[this.a++]&&128<=a[this.a++]&&128<=a[this.a++]&&this.a++;return c};A.prototype.h=A.prototype.f;function D(a){if(C.length){var b=C.pop();a&&B(b,a);a=b}else a=new A(a);this.a=a;this.g=this.a.a;this.b=this.c=-1;this.f=!1}D.prototype.reset=function(){this.a.reset();this.b=this.c=-1};function E(a){var b=a.a;if(b.a==b.c)return!1;(b=a.f)||(b=a.a,b=0>b.a||b.a>b.c);if(b)return r("Decoder hit an error"),!1;a.g=a.a.a;var c=a.a.f();b=c>>>3;c&=7;if(0!=c&&5!=c&&1!=c&&2!=c&&3!=c&&4!=c)return r("Invalid wire type: %s (at position %s)",c,a.g),a.f=!0,!1;a.c=b;a.b=c;return!0}
function F(a){switch(a.b){case 0:if(0!=a.b)r("Invalid wire type for skipVarintField"),F(a);else{for(a=a.a;a.b[a.a]&128;)a.a++;a.a++}break;case 1:1!=a.b?(r("Invalid wire type for skipFixed64Field"),F(a)):(a=a.a,a.a+=8);break;case 2:if(2!=a.b)r("Invalid wire type for skipDelimitedField"),F(a);else{var b=a.a.f();a=a.a;a.a+=b}break;case 5:5!=a.b?(r("Invalid wire type for skipFixed32Field"),F(a)):(a=a.a,a.a+=4);break;case 3:b=a.c;do{if(!E(a)){r("Unmatched start-group tag: stream EOF");a.f=!0;break}if(4==
a.b){a.c!=b&&(r("Unmatched end-group tag"),a.f=!0);break}F(a)}while(1);break;default:r("Invalid wire encoding for field.")}};function G(){}var H="function"==typeof Uint8Array,J=Object.freeze?Object.freeze([]):[];function K(a,b){if(b<a.c){b+=a.f;var c=a.a[b];return c===J?a.a[b]=[]:c}if(a.b)return c=a.b[b],c===J?a.b[b]=[]:c}G.prototype.toString=function(){return this.a.toString()};function L(a){var b=a;a=M;b||(b=[]);this.f=-1;this.a=b;a:{if(b=this.a.length){--b;var c=this.a[b];if(!(null===c||"object"!=typeof c||"array"==m(c)||H&&c instanceof Uint8Array)){this.c=b- -1;this.b=c;break a}}this.c=Number.MAX_VALUE}if(a)for(b=0;b<a.length;b++)if(c=a[b],c<this.c)c+=-1,this.a[c]=this.a[c]||J;else{var d=this.c+-1;this.a[d]||(this.b=this.a[d]={});this.b[c]=this.b[c]||J}}n(L,G);var M=[1,3];l("parseClientVariations",function(a){var b="";try{b=atob(a)}catch(d){}a=[];for(var c=0;c<b.length;c++)a.push(b.charCodeAt(c));b=new D(a);for(a=new L;E(b)&&4!=b.b;)switch(b.c){case 1:c=b.a.h();K(a,1).push(c);break;case 3:c=b.a.h();K(a,3).push(c);break;default:F(b)}return{variationIds:K(a,1),triggerVariationIds:K(a,3)}});
l("formatClientVariations",function(a,b="Active client experiment variation IDs.",c="Active client experiment variation IDs that trigger server-side behavior."){const d=a.variationIds;a=a.triggerVariationIds;const e=["message ClientVariations {"];d.length&&e.push(` // ${b}`,` repeated int32 variation_id = [${d.join(", ")}];`);a.length&&e.push(` // ${c}`,` repeated int32 trigger_variation_id = [${a.join(", ")}];`);e.push("}");return e.join("\n")});}).call(gen);
// clang-format on
export function parseClientVariations(data) {
return gen.parseClientVariations(data);
}
export function formatClientVariations(data) {
return gen.formatClientVariations(data);
}
/* eslint-disable */
// Copyright 2020 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.
// Note: This is a generated file. Do not edit by hand. Instead, run
// components/variations/proto/devtools/update_client_variations_parser.py to
// update.
const gen = {};
// clang-format off
(function(){var f=this||self;
function g(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var c=Object.prototype.toString.call(a);if("[object Window]"==c)return"object";if("[object Array]"==c||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==c||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null";else if("function"==
b&&"undefined"==typeof a.call)return"object";return b}function l(a,b){function c(){}c.prototype=b.prototype;a.prototype=new c;a.prototype.constructor=a;a.base=function(e,d,h){for(var m=Array(arguments.length-2),k=2;k<arguments.length;k++)m[k-2]=arguments[k];return b.prototype[d].apply(e,m)}};function n(a){if(Error.captureStackTrace)Error.captureStackTrace(this,n);else{const b=Error().stack;b&&(this.stack=b)}a&&(this.message=String(a))}l(n,Error);n.prototype.name="CustomError";function p(a,b){a=a.split("%s");for(var c="",e=a.length-1,d=0;d<e;d++)c+=a[d]+(d<b.length?b[d]:"%s");n.call(this,c+a[e])}l(p,n);p.prototype.name="AssertionError";function q(a,b){throw new p("Failure"+(a?": "+a:""),Array.prototype.slice.call(arguments,1));};var r=null;function t(a){var b=a.length,c=3*b/4;c%3?c=Math.floor(c):-1!="=.".indexOf(a[b-1])&&(c=-1!="=.".indexOf(a[b-2])?c-2:c-1);var e=new Uint8Array(c),d=0;u(a,function(h){e[d++]=h});return e.subarray(0,d)}
function u(a,b){function c(L){for(;e<a.length;){var y=a.charAt(e++),z=r[y];if(null!=z)return z;if(!/^[\s\xa0]*$/.test(y))throw Error("Unknown base64 encoding at char: "+y);}return L}v();for(var e=0;;){var d=c(-1),h=c(0),m=c(64),k=c(64);if(64===k&&-1===d)break;b(d<<2|h>>4);64!=m&&(b(h<<4&240|m>>2),64!=k&&b(m<<6&192|k))}}
function v(){if(!r){r={};for(var a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split(""),b=["+/=","+/","-_=","-_.","-_"],c=0;5>c;c++)for(var e=a.concat(b[c].split("")),d=0;d<e.length;d++){var h=e[d];void 0===r[h]&&(r[h]=d)}}};function w(a){if(a.constructor===Uint8Array)return a;if(a.constructor===ArrayBuffer)return new Uint8Array(a);if("undefined"!=typeof Buffer&&a.constructor===Buffer)return new Uint8Array(a);if(a.constructor===Array)return new Uint8Array(a);if(a.constructor===String)return t(a);q("Type not convertible to Uint8Array.");return new Uint8Array(0)};function x(a){this.b=null;this.a=this.c=this.g=0;a&&A(this,a)}var B=[];function A(a,b){a.b=w(b);a.g=0;a.c=a.b.length;a.a=a.g}x.prototype.reset=function(){this.a=this.g};
x.prototype.f=function(){var a=this.b;var b=a[this.a];var c=b&127;if(128>b)return this.a+=1,c;b=a[this.a+1];c|=(b&127)<<7;if(128>b)return this.a+=2,c;b=a[this.a+2];c|=(b&127)<<14;if(128>b)return this.a+=3,c;b=a[this.a+3];c|=(b&127)<<21;if(128>b)return this.a+=4,c;b=a[this.a+4];c|=(b&15)<<28;if(128>b)return this.a+=5,c>>>0;this.a+=5;128<=a[this.a++]&&128<=a[this.a++]&&128<=a[this.a++]&&128<=a[this.a++]&&this.a++;return c};x.prototype.h=x.prototype.f;function C(a){if(B.length){var b=B.pop();a&&A(b,a);a=b}else a=new x(a);this.a=a;this.g=this.a.a;this.b=this.c=-1;this.f=!1}C.prototype.reset=function(){this.a.reset();this.b=this.c=-1};function D(a){var b=a.a;if(b.a==b.c)return!1;(b=a.f)||(b=a.a,b=0>b.a||b.a>b.c);if(b)return q("Decoder hit an error"),!1;a.g=a.a.a;var c=a.a.f();b=c>>>3;c&=7;if(0!=c&&5!=c&&1!=c&&2!=c&&3!=c&&4!=c)return q("Invalid wire type: %s (at position %s)",c,a.g),a.f=!0,!1;a.c=b;a.b=c;return!0}
function E(a){switch(a.b){case 0:if(0!=a.b)q("Invalid wire type for skipVarintField"),E(a);else{for(a=a.a;a.b[a.a]&128;)a.a++;a.a++}break;case 1:1!=a.b?(q("Invalid wire type for skipFixed64Field"),E(a)):(a=a.a,a.a+=8);break;case 2:if(2!=a.b)q("Invalid wire type for skipDelimitedField"),E(a);else{var b=a.a.f();a=a.a;a.a+=b}break;case 5:5!=a.b?(q("Invalid wire type for skipFixed32Field"),E(a)):(a=a.a,a.a+=4);break;case 3:b=a.c;do{if(!D(a)){q("Unmatched start-group tag: stream EOF");a.f=!0;break}if(4==
a.b){a.c!=b&&(q("Unmatched end-group tag"),a.f=!0);break}E(a)}while(1);break;default:q("Invalid wire encoding for field.")}};function F(){}var G="function"==typeof Uint8Array,H=Object.freeze?Object.freeze([]):[];function I(a,b){if(b<a.c){b+=a.f;var c=a.a[b];return c===H?a.a[b]=[]:c}if(a.b)return c=a.b[b],c===H?a.b[b]=[]:c}F.prototype.toString=function(){return this.a.toString()};function J(a){var b=a;a=K;b||(b=[]);this.f=-1;this.a=b;a:{if(b=this.a.length){--b;var c=this.a[b];if(!(null===c||"object"!=typeof c||"array"==g(c)||G&&c instanceof Uint8Array)){this.c=b- -1;this.b=c;break a}}this.c=Number.MAX_VALUE}if(a)for(b=0;b<a.length;b++)if(c=a[b],c<this.c)c+=-1,this.a[c]=this.a[c]||H;else{var e=this.c+-1;this.a[e]||(this.b=this.a[e]={});this.b[c]=this.b[c]||H}}l(J,F);var K=[1,3];function M(a){var b="";try{b=atob(a)}catch(e){}a=[];for(var c=0;c<b.length;c++)a.push(b.charCodeAt(c));b=new C(a);for(a=new J;D(b)&&4!=b.b;)switch(b.c){case 1:c=b.a.h();I(a,1).push(c);break;case 3:c=b.a.h();I(a,3).push(c);break;default:E(b)}return{variationIds:I(a,1),triggerVariationIds:I(a,3)}}var N=["parseClientVariations"],O=f;N[0]in O||"undefined"==typeof O.execScript||O.execScript("var "+N[0]);
for(var P;N.length&&(P=N.shift());)N.length||void 0===M?O[P]&&O[P]!==Object.prototype[P]?O=O[P]:O=O[P]={}:O[P]=M;}).call(gen);
// clang-format on
export function parseClientVariations(data) {
return gen.parseClientVariations(data);
}
......@@ -4,6 +4,10 @@
const ClientVariations = goog.require('proto.variations.ClientVariations');
const VARIATION_IDS_COMMENT = 'Active client experiment variation IDs.';
const TRIGGER_VARIATION_IDS_COMMENT =
'Active client experiment variation IDs that trigger server-side behavior.';
/**
* Parses a serialized ClientVariations proto into a human-readable format.
* @param {string} data The serialized ClientVariations proto contents, e.g.
......@@ -35,4 +39,40 @@ export function parse(data) {
};
}
/**
* Formats a parsed ClientVariations proto into a human-readable representation
* of the proto, which echoes the proto definition in
* https://source.chromium.org/chromium/chromium/src/+/master:components/variations/proto/client_variations.proto;l=14-22
*
* @param {{
* variationIds: !Array<number>,
* triggerVariationIds: !Array<number>,
* }} data
* @param {string=} variationComment
* @param {string=} triggerVariationComment
* @return {string}
*/
export function format(
data, variationIdsComment = VARIATION_IDS_COMMENT,
triggerVariationIdsComment = TRIGGER_VARIATION_IDS_COMMENT) {
const variationIds = data['variationIds'];
const triggerVariationIds = data['triggerVariationIds'];
const buffer = ['message ClientVariations {'];
if (variationIds.length) {
const ids = variationIds.join(', ');
buffer.push(
` // ${variationIdsComment}`,
` repeated int32 variation_id = [${ids}];`);
}
if (triggerVariationIds.length) {
const ids = triggerVariationIds.join(', ');
buffer.push(
` // ${triggerVariationIdsComment}`,
` repeated int32 trigger_variation_id = [${ids}];`);
}
buffer.push('}');
return buffer.join('\n');
}
goog.exportSymbol('parseClientVariations', parse);
goog.exportSymbol('formatClientVariations', format);
......@@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Updates the generated ClientVariations proto parser.
"""Updates the generated ClientVariations proto parser and formatter.
If this script happens not to run correctly in your environment, it should be
easy to perform the steps manually. This script simply builds a generated file,
......@@ -19,8 +19,7 @@ OUTPUT_TEMPLATE = """\
// found in the LICENSE file.
// Note: This is a generated file. Do not edit by hand. Instead, run
// components/variations/proto/devtools/update_client_variations_parser.py to
// update.
// components/variations/proto/devtools/update_client_variations.py to update.
const gen = {};
......@@ -31,6 +30,9 @@ const gen = {};
export function parseClientVariations(data) {
return gen.parseClientVariations(data);
}
export function formatClientVariations(data) {
return gen.formatClientVariations(data);
}
"""
......@@ -49,13 +51,13 @@ def main():
script_file = os.path.join(
build_dir, 'gen', 'components', 'variations', 'proto', 'devtools',
'client_variations_parser_gen.js')
'client_variations_gen.js')
with open(script_file, 'r') as f:
script = f.read().strip()
script = script.replace('call(this)', 'call(gen)')
output_file = os.path.abspath(
os.path.join(cwd, 'client_variations_parser.js'))
os.path.join(cwd, 'client_variations.js'))
with open(output_file, 'w') as f:
f.write(OUTPUT_TEMPLATE % script)
......
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