ResourceWebSocketFrameView.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * @constructor
  20. * @extends {WebInspector.View}
  21. */
  22. WebInspector.ResourceWebSocketFrameView = function(resource)
  23. {
  24. WebInspector.View.call(this);
  25. this.element.addStyleClass("resource-websocket");
  26. this.resource = resource;
  27. this.element.removeChildren();
  28. this._dataGrid = new WebInspector.DataGrid([
  29. {id: "data", title: WebInspector.UIString("Data"), sortable: false, weight: 88},
  30. {id: "length", title: WebInspector.UIString("Length"), sortable: false, alig: WebInspector.DataGrid.Align.Right, weight: 5},
  31. {id: "time", title: WebInspector.UIString("Time"), weight: 7}
  32. ]);
  33. this.refresh();
  34. this._dataGrid.setName("ResourceWebSocketFrameView");
  35. this._dataGrid.show(this.element);
  36. }
  37. WebInspector.ResourceWebSocketFrameView.OpCodes = {
  38. ContinuationFrame: 0,
  39. TextFrame: 1,
  40. BinaryFrame: 2,
  41. ConnectionCloseFrame: 8,
  42. PingFrame: 9,
  43. PongFrame: 10
  44. };
  45. WebInspector.ResourceWebSocketFrameView.prototype = {
  46. appendFrame: function (frame)
  47. {
  48. var payload = frame;
  49. var date = new Date(payload.time * 1000);
  50. var row = {
  51. data: "",
  52. length: typeof payload.payloadData === "undefined" ? payload.errorMessage.length.toString() : payload.payloadData.length.toString(),
  53. time: date.toLocaleTimeString()
  54. };
  55. var rowClass = "";
  56. if (payload.errorMessage) {
  57. rowClass = "error";
  58. row.data = payload.errorMessage;
  59. } else if (payload.opcode == WebInspector.ResourceWebSocketFrameView.OpCodes.TextFrame) {
  60. if (payload.sent)
  61. rowClass = "outcoming";
  62. row.data = payload.payloadData;
  63. } else {
  64. rowClass = "opcode";
  65. var opcodeMeaning = "";
  66. switch (payload.opcode) {
  67. case WebInspector.ResourceWebSocketFrameView.OpCodes.ContinuationFrame:
  68. opcodeMeaning = WebInspector.UIString("Continuation Frame");
  69. break;
  70. case WebInspector.ResourceWebSocketFrameView.OpCodes.BinaryFrame:
  71. opcodeMeaning = WebInspector.UIString("Binary Frame");
  72. break;
  73. case WebInspector.ResourceWebSocketFrameView.OpCodes.ConnectionCloseFrame:
  74. opcodeMeaning = WebInspector.UIString("Connection Close Frame");
  75. break;
  76. case WebInspector.ResourceWebSocketFrameView.OpCodes.PingFrame:
  77. opcodeMeaning = WebInspector.UIString("Ping Frame");
  78. break;
  79. case WebInspector.ResourceWebSocketFrameView.OpCodes.PongFrame:
  80. opcodeMeaning = WebInspector.UIString("Pong Frame");
  81. break;
  82. }
  83. row.data = WebInspector.UIString("%s (Opcode %d%s)", opcodeMeaning, payload.opcode, (payload.mask ? ", mask" : ""));
  84. }
  85. var node = new WebInspector.DataGridNode(row, false);
  86. this._dataGrid.rootNode().appendChild(node);
  87. if (rowClass)
  88. node.element.classList.add("resource-websocket-row-" + rowClass);
  89. },
  90. refresh: function ()
  91. {
  92. this._dataGrid.rootNode().removeChildren();
  93. var frames = this.resource.frames();
  94. for (var i = frames.length - 1; i >= 0; i--) {
  95. this.appendFrame(frames[i]);
  96. }
  97. },
  98. show: function (parentElement, insertBefore)
  99. {
  100. this.refresh();
  101. WebInspector.View.prototype.show.call(this, parentElement, insertBefore);
  102. },
  103. __proto__: WebInspector.View.prototype
  104. }