1 /* 2 * Copyright (C) 2012 Research In Motion Limited. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 19 /** 20 * @constructor 21 * @extends {WebInspector.VBox} 22 */ 23 WebInspector.ResourceWebSocketFrameView = function(resource) 24 { 25 WebInspector.VBox.call(this); 26 this.element.classList.add("resource-websocket"); 27 this.resource = resource; 28 this.element.removeChildren(); 29 30 this._dataGrid = new WebInspector.DataGrid([ 31 {id: "data", title: WebInspector.UIString("Data"), sortable: false, weight: 88, longText: true}, 32 {id: "length", title: WebInspector.UIString("Length"), sortable: false, alig: WebInspector.DataGrid.Align.Right, weight: 5}, 33 {id: "time", title: WebInspector.UIString("Time"), weight: 7} 34 ], undefined, undefined, undefined, this._onContextMenu.bind(this)); 35 36 this.refresh(); 37 this._dataGrid.setName("ResourceWebSocketFrameView"); 38 this._dataGrid.show(this.element); 39 } 40 41 WebInspector.ResourceWebSocketFrameView.OpCodes = { 42 ContinuationFrame: 0, 43 TextFrame: 1, 44 BinaryFrame: 2, 45 ConnectionCloseFrame: 8, 46 PingFrame: 9, 47 PongFrame: 10 48 }; 49 50 WebInspector.ResourceWebSocketFrameView.prototype = { 51 appendFrame: function(frame) 52 { 53 var payload = frame; 54 55 var date = new Date(payload.time * 1000); 56 var row = { 57 data: "", 58 length: typeof payload.payloadData === "undefined" ? payload.errorMessage.length.toString() : payload.payloadData.length.toString(), 59 time: date.toLocaleTimeString() 60 }; 61 62 var rowClass = ""; 63 if (payload.errorMessage) { 64 rowClass = "error"; 65 row.data = payload.errorMessage; 66 } else if (payload.opcode == WebInspector.ResourceWebSocketFrameView.OpCodes.TextFrame) { 67 if (payload.sent) 68 rowClass = "outcoming"; 69 70 row.data = payload.payloadData; 71 } else { 72 rowClass = "opcode"; 73 var opcodeMeaning = ""; 74 switch (payload.opcode) { 75 case WebInspector.ResourceWebSocketFrameView.OpCodes.ContinuationFrame: 76 opcodeMeaning = WebInspector.UIString("Continuation Frame"); 77 break; 78 case WebInspector.ResourceWebSocketFrameView.OpCodes.BinaryFrame: 79 opcodeMeaning = WebInspector.UIString("Binary Frame"); 80 break; 81 case WebInspector.ResourceWebSocketFrameView.OpCodes.ConnectionCloseFrame: 82 opcodeMeaning = WebInspector.UIString("Connection Close Frame"); 83 break; 84 case WebInspector.ResourceWebSocketFrameView.OpCodes.PingFrame: 85 opcodeMeaning = WebInspector.UIString("Ping Frame"); 86 break; 87 case WebInspector.ResourceWebSocketFrameView.OpCodes.PongFrame: 88 opcodeMeaning = WebInspector.UIString("Pong Frame"); 89 break; 90 } 91 row.data = WebInspector.UIString("%s (Opcode %d%s)", opcodeMeaning, payload.opcode, (payload.mask ? ", mask" : "")); 92 } 93 94 var node = new WebInspector.DataGridNode(row, false); 95 this._dataGrid.rootNode().appendChild(node); 96 97 if (rowClass) 98 node.element.classList.add("resource-websocket-row-" + rowClass); 99 }, 100 101 refresh: function() 102 { 103 this._dataGrid.rootNode().removeChildren(); 104 var frames = this.resource.frames(); 105 for (var i = frames.length - 1; i >= 0; i--) { 106 this.appendFrame(frames[i]); 107 } 108 }, 109 110 show: function(parentElement, insertBefore) 111 { 112 this.refresh(); 113 WebInspector.View.prototype.show.call(this, parentElement, insertBefore); 114 }, 115 116 /** 117 * @param {!WebInspector.ContextMenu} contextMenu 118 * @param {!WebInspector.DataGridNode} node 119 */ 120 _onContextMenu: function(contextMenu, node) 121 { 122 contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Copy message" : "Copy Message"), this._copyMessage.bind(this, node.data)); 123 }, 124 125 /** 126 * @param {!Object} row 127 */ 128 _copyMessage: function(row) 129 { 130 InspectorFrontendHost.copyText(row.data); 131 }, 132 133 __proto__: WebInspector.VBox.prototype 134 } 135