|
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 |
|
| 37 |
// COMPATIBILITY (iOS 10.3): `walltime` did not exist in 10.3 and earlier. |
| 38 |
this._showTimeColumn = NetworkAgent.hasEventParameter("webSocketWillSendHandshakeRequest", "walltime"); |
| 39 |
|
| 40 |
this.element.classList.add("web-socket", "resource"); |
| 41 |
|
| 42 |
let columns = {data: {}}; |
| 43 |
columns.data.title = WebInspector.UIString("Data"); |
| 44 |
columns.data.sortable = false; |
| 45 |
columns.data.icon = true; |
| 46 |
columns.data.width = "85%"; |
| 47 |
|
| 48 |
if (this._showTimeColumn) |
| 49 |
columns.time = {title: WebInspector.UIString("Time"), sortable: true}; |
| 50 |
|
| 51 |
this._dataGrid = new WebInspector.DataGrid(columns); |
| 52 |
this._dataGrid.variableHeightRows = true; |
| 53 |
this.addSubview(this._dataGrid); |
| 54 |
|
| 55 |
this._addRow(WebInspector.UIString("WebSocket Connection Established"), this._resource.walltime, ["non-text-frame"]); |
| 56 |
|
| 57 |
this._dataGrid.updateLayout(); |
| 58 |
} |
| 59 |
|
| 60 |
// Static |
| 61 |
|
| 62 |
static textForOpcode(opcode) |
| 63 |
{ |
| 64 |
switch (opcode) { |
| 65 |
case WebInspector.WebSocketResource.OpCodes.ContinuationFrame: |
| 66 |
return WebInspector.UIString("Continuation Frame"); |
| 67 |
case WebInspector.WebSocketResource.OpCodes.TextFrame: |
| 68 |
return WebInspector.UIString("Text Frame"); |
| 69 |
case WebInspector.WebSocketResource.OpCodes.BinaryFrame: |
| 70 |
return WebInspector.UIString("Binary Frame"); |
| 71 |
case WebInspector.WebSocketResource.OpCodes.ConnectionCloseFrame: |
| 72 |
return WebInspector.UIString("Connection Close Frame"); |
| 73 |
case WebInspector.WebSocketResource.OpCodes.PingFrame: |
| 74 |
return WebInspector.UIString("Ping Frame"); |
| 75 |
case WebInspector.WebSocketResource.OpCodes.PongFrame: |
| 76 |
return WebInspector.UIString("Pong Frame"); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Public |
| 81 |
|
| 82 |
shown() |
| 83 |
{ |
| 84 |
this._updateFrames(); |
| 85 |
this._resource.addEventListener(WebInspector.WebSocketResource.Event.FrameAdded, this._updateFrames, this); |
| 86 |
} |
| 87 |
|
| 88 |
hidden() |
| 89 |
{ |
| 90 |
this._resource.removeEventListener(WebInspector.WebSocketResource.Event.FrameAdded, this._updateFrames, this); |
| 91 |
} |
| 92 |
|
| 93 |
addFrame(data, isOutgoing, opcode, time) |
| 94 |
{ |
| 95 |
let nodeText; |
| 96 |
if (opcode === WebInspector.WebSocketResource.OpCodes.TextFrame) |
| 97 |
nodeText = data; |
| 98 |
else |
| 99 |
nodeText = WebInspector.WebSocketContentView.textForOpcode(opcode); |
| 100 |
|
| 101 |
let classNames = [ |
| 102 |
isOutgoing ? "outgoing" : "incoming", |
| 103 |
opcode === WebInspector.WebSocketResource.OpCodes.TextFrame ? "text-frame" : "non-text-frame" |
| 104 |
]; |
| 105 |
|
| 106 |
this._addRow(nodeText, time, classNames); |
| 107 |
} |
| 108 |
|
| 109 |
// Private |
| 110 |
|
| 111 |
_updateFrames() |
| 112 |
{ |
| 113 |
let framesLength = this._resource.frames.length; |
| 114 |
for (let index = this._framesRendered; index < framesLength; index++) { |
| 115 |
let frame = this._resource.frames[index]; |
| 116 |
let {data, isOutgoing, opcode, walltime} = frame; |
| 117 |
this.addFrame(data, isOutgoing, opcode, walltime); |
| 118 |
} |
| 119 |
this._framesRendered = framesLength; |
| 120 |
} |
| 121 |
|
| 122 |
_addRow(data, time, classNames) |
| 123 |
{ |
| 124 |
let node; |
| 125 |
if (this._showTimeColumn) |
| 126 |
node = new WebInspector.DataGridNode({data, time: this._timeStringFromTimestamp(time)}); |
| 127 |
else |
| 128 |
node = new WebInspector.DataGridNode({data}); |
| 129 |
|
| 130 |
this._dataGrid.appendChild(node); |
| 131 |
|
| 132 |
node.element.classList.add(...classNames); |
| 133 |
} |
| 134 |
|
| 135 |
_timeStringFromTimestamp(timestamp) |
| 136 |
{ |
| 137 |
return new Date(timestamp * 1000).toLocaleTimeString(); |
| 138 |
} |
| 139 |
}; |