Index: third_party/WebKit/Source/devtools/front_end/sdk/ServerTiming.js |
diff --git a/third_party/WebKit/Source/devtools/front_end/sdk/ServerTiming.js b/third_party/WebKit/Source/devtools/front_end/sdk/ServerTiming.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a744503e3a1917cb202e479b3d0de8484758a672 |
--- /dev/null |
+++ b/third_party/WebKit/Source/devtools/front_end/sdk/ServerTiming.js |
@@ -0,0 +1,68 @@ |
+// Copyright 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. |
+ |
+/** |
+ * @constructor |
+ * @param {!WebInspector.Target} target |
+ * @param {string} metric |
+ * @param {number} value |
+ * @param {string} description |
+ */ |
+WebInspector.ServerTiming = function(target, metric, value, description) |
+{ |
+ this._target = target; |
+ this.metric = metric; |
+ this.value = value; |
+ this.description = description; |
+} |
+ |
+/** |
+ * @param {!WebInspector.Target} target |
+ * @param {!Array.<!WebInspector.NetworkRequest.NameValue>} headers |
caseq
2016/04/05 17:18:42
nit: here and below, please use Array<> instead of
|
+ * @return {?Array.<!WebInspector.ServerTiming>} |
+ */ |
+WebInspector.ServerTiming.parseHeaders = function(target, headers) |
+{ |
+ var rawServerTimingHeaders = headers.filter(item => item.name.toLowerCase() === 'server-timing'); |
caseq
2016/04/05 17:18:42
use "double quotes", please.
|
+ if (!rawServerTimingHeaders.length) |
+ return null; |
+ |
+ /** |
+ * @param {?string} valueString |
+ * @return {?Array.<!WebInspector.ServerTiming>} |
+ */ |
+ function createFromHeaderValue(valueString) |
+ { |
+ // Server-Timing = "Server-Timing" ":" #server-timing-metric |
caseq
2016/04/05 17:18:42
Should we have a link to the w3c doc here instead?
|
+ // server-timing-metric = metric [ ";" description ] |
+ // metric = metric-name [ "=" metric-value ] |
+ // metric-name = token |
+ // metric-value = 1*digit [ "." 1*digit ] |
+ // description = token | quoted-string |
+ // token = 1*tchar |
+ // tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" |
+ // / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" |
+ // / DIGIT / ALPHA |
+ var serverTimingMetricRegExp = /[ \t]*([\!\#\$\%\&\'\*\+\-\.\^\_\`\|\~0-9A-Za-z]+)[ \t]*(?:=[ \t]*(\d+(?:\.\d+)?))?[ \t]*(?:;[ \t]*(?:"([^"]+)"|([\!\#\$\%\&\'\*\+\-\.\^\_\`\|\~0-9A-Za-z]+)))?[ \t]*(?:,(.*))?/; |
+ var metricMatch, result = []; |
caseq
2016/04/05 17:18:42
style: please put result on a separate line.
|
+ while (valueString && (metricMatch = serverTimingMetricRegExp.exec(valueString))) { |
+ var metric = metricMatch[1], |
+ value = metricMatch[2], |
caseq
2016/04/05 17:18:42
style: please split into separate var statements.
|
+ description = metricMatch[3] || metricMatch[4]; |
+ if (value !== null) |
+ value = Math.abs(parseFloat(metricMatch[2])); |
+ valueString = metricMatch[5]; // comma delimited headers |
+ result.push(new WebInspector.ServerTiming(target, metric, value, description)); |
+ } |
+ return result; |
+ } |
+ |
+ var serverTimings = rawServerTimingHeaders.reduce((memo, header) => { |
+ var timing = createFromHeaderValue(header.value); |
+ Array.prototype.push.apply(memo, timing); |
+ return memo; |
+ }, []); |
+ serverTimings.sort((a, b) => a.metric.toLowerCase().compareTo(b.metric.toLowerCase())); |
+ return serverTimings; |
+} |