|
Line 0
a/Source/WebInspectorUI/UserInterface/Views/WebSocketContentView.js_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
WebInspector.WebSocketContentView = class WebSocketContentView extends WebInspector.ContentView |
| 27 |
{ |
| 28 |
constructor(resource) |
| 29 |
{ |
| 30 |
console.assert(resource instanceof WebInspector.WebSocketResource, resource); |
| 31 |
|
| 32 |
super(resource); |
| 33 |
|
| 34 |
this._resource = resource; |
| 35 |
this._framesRendered = 0; |
| 36 |
this._showTimeColumn = !isNaN(resource.walltime); |
| 37 |
|
| 38 |
this.element.classList.add("web-socket", "resource"); |
| 39 |
|
| 40 |
let columns = {data: {}}; |
| 41 |
columns.data.title = WebInspector.UIString("Data"); |
| 42 |
columns.data.sortable = false; |
| 43 |
columns.data.icon = true; |
| 44 |
columns.data.width = "85%"; |
| 45 |
|
| 46 |
if (this._showTimeColumn) |
| 47 |
columns.time = {title: WebInspector.UIString("Time"), sortable: true}; |
| 48 |
|
| 49 |
this._dataGrid = new WebInspector.DataGrid(columns); |
| 50 |
this._dataGrid.variableHeightRows = true; |
| 51 |
this.addSubview(this._dataGrid); |
| 52 |
|
| 53 |
this._addRow(WebInspector.UIString("WebSocket connection established"), this._resource.walltime, ["non-text-frame"]); |
| 54 |
|
| 55 |
this._dataGrid.updateLayout(); |
| 56 |
} |
| 57 |
|
| 58 |
// Static |
| 59 |
|
| 60 |
static textForOpcode(opcode) |
| 61 |
{ |
| 62 |
const opCodesDescription = { |
| 63 |
[WebInspector.WebSocketResource.OpCodes.ContinuationFrame]: WebInspector.UIString("Continuation Frame"), |
| 64 |
[WebInspector.WebSocketResource.OpCodes.TextFrame]: WebInspector.UIString("Text Frame"), |
| 65 |
[WebInspector.WebSocketResource.OpCodes.BinaryFrame]: WebInspector.UIString("Binary Frame"), |
| 66 |
[WebInspector.WebSocketResource.OpCodes.ConnectionCloseFrame]: WebInspector.UIString("Connection Close Frame"), |
| 67 |
[WebInspector.WebSocketResource.OpCodes.PingFrame]: WebInspector.UIString("Ping Frame"), |
| 68 |
[WebInspector.WebSocketResource.OpCodes.PongFrame]: WebInspector.UIString("Pong Frame"), |
| 69 |
}; |
| 70 |
|
| 71 |
return opCodesDescription[opcode]; |
| 72 |
} |
| 73 |
|
| 74 |
// Public |
| 75 |
|
| 76 |
shown() |
| 77 |
{ |
| 78 |
this._updateFrames(); |
| 79 |
this._resource.addEventListener(WebInspector.WebSocketResource.Event.FrameAdded, this._updateFrames, this); |
| 80 |
} |
| 81 |
|
| 82 |
hidden() |
| 83 |
{ |
| 84 |
this._resource.removeEventListener(WebInspector.WebSocketResource.Event.FrameAdded, this._updateFrames, this); |
| 85 |
} |
| 86 |
|
| 87 |
addFrame(data, isOutgoing, opcode, time) |
| 88 |
{ |
| 89 |
let nodeText; |
| 90 |
if (opcode === WebInspector.WebSocketResource.OpCodes.TextFrame) |
| 91 |
nodeText = data; |
| 92 |
else |
| 93 |
nodeText = WebInspector.WebSocketContentView.textForOpcode(opcode); |
| 94 |
|
| 95 |
let classNames = [ |
| 96 |
isOutgoing ? "outgoing" : "incoming", |
| 97 |
opcode === WebInspector.WebSocketResource.OpCodes.TextFrame ? "text-frame" : "non-text-frame" |
| 98 |
]; |
| 99 |
|
| 100 |
this._addRow(nodeText, time, classNames); |
| 101 |
} |
| 102 |
|
| 103 |
// Private |
| 104 |
|
| 105 |
_updateFrames() |
| 106 |
{ |
| 107 |
let framesLength = this._resource.frames.length; |
| 108 |
for (let index = this._framesRendered; index < framesLength; index++) { |
| 109 |
let frame = this._resource.frames[index]; |
| 110 |
let {data, isOutgoing, opcode, walltime} = frame; |
| 111 |
this.addFrame(data, isOutgoing, opcode, walltime); |
| 112 |
} |
| 113 |
this._framesRendered = framesLength; |
| 114 |
} |
| 115 |
|
| 116 |
_addRow(data, time, classNames) |
| 117 |
{ |
| 118 |
let node; |
| 119 |
if (this._showTimeColumn) |
| 120 |
node = new WebInspector.DataGridNode({data, time: this._timeStringFromTimestamp(time)}); |
| 121 |
else |
| 122 |
node = new WebInspector.DataGridNode({data}); |
| 123 |
|
| 124 |
this._dataGrid.appendChild(node); |
| 125 |
|
| 126 |
if (classNames && classNames.length > 0) |
| 127 |
node.element.classList.add(...classNames); |
| 128 |
} |
| 129 |
|
| 130 |
_timeStringFromTimestamp(timestamp) |
| 131 |
{ |
| 132 |
return new Date(timestamp * 1000).toLocaleTimeString(); |
| 133 |
} |
| 134 |
}; |