CanvasReplayStateView.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (C) 2013 Google 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 are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.View}
  33. * @param {WebInspector.CanvasTraceLogPlayerProxy} traceLogPlayer
  34. */
  35. WebInspector.CanvasReplayStateView = function(traceLogPlayer)
  36. {
  37. WebInspector.View.call(this);
  38. this.registerRequiredCSS("canvasProfiler.css");
  39. this.element.addStyleClass("canvas-replay-state-view");
  40. this._traceLogPlayer = traceLogPlayer;
  41. var controlsContainer = this.element.createChild("div", "status-bar");
  42. this._prevButton = this._createControlButton(controlsContainer, "canvas-replay-state-prev", WebInspector.UIString("Previous resource."), this._onResourceNavigationClick.bind(this, false));
  43. this._nextButton = this._createControlButton(controlsContainer, "canvas-replay-state-next", WebInspector.UIString("Next resource."), this._onResourceNavigationClick.bind(this, true));
  44. this._createControlButton(controlsContainer, "canvas-replay-state-refresh", WebInspector.UIString("Refresh."), this._onStateRefreshClick.bind(this));
  45. this._resourceSelector = new WebInspector.StatusBarComboBox(this._onReplayResourceChanged.bind(this));
  46. this._currentOption = this._resourceSelector.createOption(WebInspector.UIString("<auto>"), WebInspector.UIString("Show state of the last replayed resource."), "");
  47. controlsContainer.appendChild(this._resourceSelector.element);
  48. /** @type {!Object.<string, string>} */
  49. this._resourceIdToDescription = {};
  50. /** @type {!Object.<string, !Object.<string, boolean>>} */
  51. this._gridNodesExpandedState = {};
  52. /** @type {!Object.<string, {scrollTop:number, scrollLeft:number}>} */
  53. this._gridScrollPositions = {};
  54. /** @type {?CanvasAgent.ResourceId} */
  55. this._currentResourceId = null;
  56. /** @type {!Array.<!Element>} */
  57. this._prevOptionsStack = [];
  58. /** @type {!Array.<!Element>} */
  59. this._nextOptionsStack = [];
  60. /** @type {!Array.<!WebInspector.DataGridNode>} */
  61. this._highlightedGridNodes = [];
  62. var columns = [
  63. {title: WebInspector.UIString("Name"), sortable: false, width: "50%", disclosure: true},
  64. {title: WebInspector.UIString("Value"), sortable: false, width: "50%"}
  65. ];
  66. this._stateGrid = new WebInspector.DataGrid(columns);
  67. this._stateGrid.element.addStyleClass("fill");
  68. this._stateGrid.show(this.element);
  69. this._traceLogPlayer.addEventListener(WebInspector.CanvasTraceLogPlayerProxy.Events.CanvasReplayStateChanged, this._onReplayResourceChanged, this);
  70. this._traceLogPlayer.addEventListener(WebInspector.CanvasTraceLogPlayerProxy.Events.CanvasTraceLogReceived, this._onCanvasTraceLogReceived, this);
  71. this._traceLogPlayer.addEventListener(WebInspector.CanvasTraceLogPlayerProxy.Events.CanvasResourceStateReceived, this._onCanvasResourceStateReceived, this);
  72. this._updateButtonsEnabledState();
  73. }
  74. WebInspector.CanvasReplayStateView.prototype = {
  75. /**
  76. * @param {string} resourceId
  77. */
  78. selectResource: function(resourceId)
  79. {
  80. if (resourceId === this._resourceSelector.selectedOption().value)
  81. return;
  82. var option = this._resourceSelector.selectElement().firstChild;
  83. for (var index = 0; option; ++index, option = option.nextSibling) {
  84. if (resourceId === option.value) {
  85. this._resourceSelector.setSelectedIndex(index);
  86. this._onReplayResourceChanged();
  87. break;
  88. }
  89. }
  90. },
  91. /**
  92. * @param {Element} parent
  93. * @param {string} className
  94. * @param {string} title
  95. * @param {function(this:WebInspector.CanvasProfileView)} clickCallback
  96. * @return {!WebInspector.StatusBarButton}
  97. */
  98. _createControlButton: function(parent, className, title, clickCallback)
  99. {
  100. var button = new WebInspector.StatusBarButton(title, className + " canvas-replay-button");
  101. parent.appendChild(button.element);
  102. button.makeLongClickEnabled();
  103. button.addEventListener("click", clickCallback, this);
  104. button.addEventListener("longClickDown", clickCallback, this);
  105. button.addEventListener("longClickPress", clickCallback, this);
  106. return button;
  107. },
  108. /**
  109. * @param {boolean} forward
  110. */
  111. _onResourceNavigationClick: function(forward)
  112. {
  113. var newOption = forward ? this._nextOptionsStack.pop() : this._prevOptionsStack.pop();
  114. if (!newOption)
  115. return;
  116. (forward ? this._prevOptionsStack : this._nextOptionsStack).push(this._currentOption);
  117. this._isNavigationButton = true;
  118. this.selectResource(newOption.value);
  119. delete this._isNavigationButton;
  120. this._updateButtonsEnabledState();
  121. },
  122. _onStateRefreshClick: function()
  123. {
  124. this._traceLogPlayer.clearResourceStates();
  125. },
  126. _updateButtonsEnabledState: function()
  127. {
  128. this._prevButton.setEnabled(this._prevOptionsStack.length > 0);
  129. this._nextButton.setEnabled(this._nextOptionsStack.length > 0);
  130. },
  131. _updateCurrentOption: function()
  132. {
  133. const maxStackSize = 256;
  134. var selectedOption = this._resourceSelector.selectedOption();
  135. if (this._currentOption === selectedOption)
  136. return;
  137. if (!this._isNavigationButton) {
  138. this._prevOptionsStack.push(this._currentOption);
  139. this._nextOptionsStack = [];
  140. if (this._prevOptionsStack.length > maxStackSize)
  141. this._prevOptionsStack.shift();
  142. this._updateButtonsEnabledState();
  143. }
  144. this._currentOption = selectedOption;
  145. },
  146. /**
  147. * @param {!CanvasAgent.TraceLog} traceLog
  148. */
  149. _collectResourcesFromTraceLog: function(traceLog)
  150. {
  151. /** @type {!Array.<!CanvasAgent.CallArgument>} */
  152. var collectedResources = [];
  153. var calls = traceLog.calls;
  154. for (var i = 0, n = calls.length; i < n; ++i) {
  155. var call = calls[i];
  156. var args = call.arguments || [];
  157. for (var j = 0; j < args.length; ++j)
  158. this._collectResourceFromCallArgument(args[j], collectedResources);
  159. this._collectResourceFromCallArgument(call.result, collectedResources);
  160. this._collectResourceFromCallArgument(call.value, collectedResources);
  161. }
  162. var contexts = traceLog.contexts;
  163. for (var i = 0, n = contexts.length; i < n; ++i)
  164. this._collectResourceFromCallArgument(contexts[i], collectedResources);
  165. this._addCollectedResourcesToSelector(collectedResources);
  166. },
  167. /**
  168. * @param {!CanvasAgent.ResourceState} resourceState
  169. */
  170. _collectResourcesFromResourceState: function(resourceState)
  171. {
  172. /** @type {!Array.<!CanvasAgent.CallArgument>} */
  173. var collectedResources = [];
  174. this._collectResourceFromResourceStateDescriptors(resourceState.descriptors, collectedResources);
  175. this._addCollectedResourcesToSelector(collectedResources);
  176. },
  177. /**
  178. * @param {Array.<!CanvasAgent.ResourceStateDescriptor>|undefined} descriptors
  179. * @param {!Array.<!CanvasAgent.CallArgument>} output
  180. */
  181. _collectResourceFromResourceStateDescriptors: function(descriptors, output)
  182. {
  183. if (!descriptors)
  184. return;
  185. for (var i = 0, n = descriptors.length; i < n; ++i) {
  186. var descriptor = descriptors[i];
  187. this._collectResourceFromCallArgument(descriptor.value, output);
  188. this._collectResourceFromResourceStateDescriptors(descriptor.values, output);
  189. }
  190. },
  191. /**
  192. * @param {CanvasAgent.CallArgument|undefined} argument
  193. * @param {!Array.<!CanvasAgent.CallArgument>} output
  194. */
  195. _collectResourceFromCallArgument: function(argument, output)
  196. {
  197. if (!argument)
  198. return;
  199. var resourceId = argument.resourceId;
  200. if (!resourceId || this._resourceIdToDescription[resourceId])
  201. return;
  202. this._resourceIdToDescription[resourceId] = argument.description;
  203. output.push(argument);
  204. },
  205. /**
  206. * @param {!Array.<!CanvasAgent.CallArgument>} collectedResources
  207. */
  208. _addCollectedResourcesToSelector: function(collectedResources)
  209. {
  210. if (!collectedResources.length)
  211. return;
  212. /**
  213. * @param {!CanvasAgent.CallArgument} arg1
  214. * @param {!CanvasAgent.CallArgument} arg2
  215. * @return {number}
  216. */
  217. function comparator(arg1, arg2)
  218. {
  219. var a = arg1.description;
  220. var b = arg2.description;
  221. return String.naturalOrderComparator(a, b);
  222. }
  223. collectedResources.sort(comparator);
  224. var selectElement = this._resourceSelector.selectElement();
  225. var currentOption = selectElement.firstChild;
  226. currentOption = currentOption.nextSibling; // Skip the "<auto>" option.
  227. for (var i = 0, n = collectedResources.length; i < n; ++i) {
  228. var argument = collectedResources[i];
  229. while (currentOption && String.naturalOrderComparator(currentOption.text, argument.description) < 0)
  230. currentOption = currentOption.nextSibling;
  231. var option = this._resourceSelector.createOption(argument.description, WebInspector.UIString("Show state of this resource."), argument.resourceId);
  232. if (currentOption)
  233. selectElement.insertBefore(option, currentOption);
  234. }
  235. },
  236. _onReplayResourceChanged: function()
  237. {
  238. this._updateCurrentOption();
  239. var selectedResourceId = this._resourceSelector.selectedOption().value;
  240. /**
  241. * @param {?CanvasAgent.ResourceState} resourceState
  242. */
  243. function didReceiveResourceState(resourceState)
  244. {
  245. if (selectedResourceId !== this._resourceSelector.selectedOption().value)
  246. return;
  247. this._showResourceState(resourceState);
  248. }
  249. this._traceLogPlayer.getResourceState(selectedResourceId, didReceiveResourceState.bind(this));
  250. },
  251. /**
  252. * @param {WebInspector.Event} event
  253. */
  254. _onCanvasTraceLogReceived: function(event)
  255. {
  256. var traceLog = /** @type {CanvasAgent.TraceLog} */ (event.data);
  257. if (traceLog)
  258. this._collectResourcesFromTraceLog(traceLog);
  259. },
  260. /**
  261. * @param {WebInspector.Event} event
  262. */
  263. _onCanvasResourceStateReceived: function(event)
  264. {
  265. var resourceState = /** @type {CanvasAgent.ResourceState} */ (event.data);
  266. if (resourceState)
  267. this._collectResourcesFromResourceState(resourceState);
  268. },
  269. /**
  270. * @param {?CanvasAgent.ResourceState} resourceState
  271. */
  272. _showResourceState: function(resourceState)
  273. {
  274. this._saveExpandedState();
  275. this._saveScrollState();
  276. var rootNode = this._stateGrid.rootNode();
  277. if (!resourceState) {
  278. this._currentResourceId = null;
  279. this._updateDataGridHighlights([]);
  280. rootNode.removeChildren();
  281. return;
  282. }
  283. var nodesToHighlight = [];
  284. var nameToOldGridNodes = {};
  285. /**
  286. * @param {!Object} map
  287. * @param {WebInspector.DataGridNode=} node
  288. */
  289. function populateNameToNodesMap(map, node)
  290. {
  291. if (!node)
  292. return;
  293. for (var i = 0, child; child = node.children[i]; ++i) {
  294. var item = {
  295. node: child,
  296. children: {}
  297. };
  298. map[child.name] = item;
  299. populateNameToNodesMap(item.children, child);
  300. }
  301. }
  302. populateNameToNodesMap(nameToOldGridNodes, rootNode);
  303. rootNode.removeChildren();
  304. /**
  305. * @param {!CanvasAgent.ResourceStateDescriptor} d1
  306. * @param {!CanvasAgent.ResourceStateDescriptor} d2
  307. * @return {number}
  308. */
  309. function comparator(d1, d2)
  310. {
  311. var hasChildren1 = !!d1.values;
  312. var hasChildren2 = !!d2.values;
  313. if (hasChildren1 !== hasChildren2)
  314. return hasChildren1 ? 1 : -1;
  315. return String.naturalOrderComparator(d1.name, d2.name);
  316. }
  317. /**
  318. * @param {Array.<!CanvasAgent.ResourceStateDescriptor>|undefined} descriptors
  319. * @param {!WebInspector.DataGridNode} parent
  320. * @param {Object=} nameToOldChildren
  321. */
  322. function appendResourceStateDescriptors(descriptors, parent, nameToOldChildren)
  323. {
  324. descriptors = descriptors || [];
  325. descriptors.sort(comparator);
  326. var oldChildren = nameToOldChildren || {};
  327. for (var i = 0, n = descriptors.length; i < n; ++i) {
  328. var descriptor = descriptors[i];
  329. var childNode = this._createDataGridNode(descriptor);
  330. parent.appendChild(childNode);
  331. var oldChildrenItem = oldChildren[childNode.name] || {};
  332. var oldChildNode = oldChildrenItem.node;
  333. if (!oldChildNode || oldChildNode.element.textContent !== childNode.element.textContent)
  334. nodesToHighlight.push(childNode);
  335. appendResourceStateDescriptors.call(this, descriptor.values, childNode, oldChildrenItem.children);
  336. }
  337. }
  338. appendResourceStateDescriptors.call(this, resourceState.descriptors, rootNode, nameToOldGridNodes);
  339. var shouldHighlightChanges = (this._resourceKindId(this._currentResourceId) === this._resourceKindId(resourceState.id));
  340. this._currentResourceId = resourceState.id;
  341. this._restoreExpandedState();
  342. this._updateDataGridHighlights(shouldHighlightChanges ? nodesToHighlight : []);
  343. this._restoreScrollState();
  344. },
  345. /**
  346. * @param {!Array.<!WebInspector.DataGridNode>} nodes
  347. */
  348. _updateDataGridHighlights: function(nodes)
  349. {
  350. for (var i = 0, n = this._highlightedGridNodes.length; i < n; ++i) {
  351. var node = this._highlightedGridNodes[i];
  352. node.element.removeStyleClass("canvas-grid-node-highlighted");
  353. }
  354. this._highlightedGridNodes = nodes;
  355. for (var i = 0, n = this._highlightedGridNodes.length; i < n; ++i) {
  356. var node = this._highlightedGridNodes[i];
  357. node.element.addStyleClass("canvas-grid-node-highlighted");
  358. node.reveal();
  359. }
  360. },
  361. /**
  362. * @param {?CanvasAgent.ResourceId} resourceId
  363. * @return {string}
  364. */
  365. _resourceKindId: function(resourceId)
  366. {
  367. var description = (resourceId && this._resourceIdToDescription[resourceId]) || "";
  368. return description.replace(/\d+/g, "");
  369. },
  370. /**
  371. * @param {function(!WebInspector.DataGridNode, string):void} callback
  372. */
  373. _forEachGridNode: function(callback)
  374. {
  375. /**
  376. * @param {!WebInspector.DataGridNode} node
  377. * @param {string} key
  378. */
  379. function processRecursively(node, key)
  380. {
  381. for (var i = 0, child; child = node.children[i]; ++i) {
  382. var childKey = key + "#" + child.name;
  383. callback(child, childKey);
  384. processRecursively(child, childKey);
  385. }
  386. }
  387. processRecursively(this._stateGrid.rootNode(), "");
  388. },
  389. _saveExpandedState: function()
  390. {
  391. if (!this._currentResourceId)
  392. return;
  393. var expandedState = {};
  394. var key = this._resourceKindId(this._currentResourceId);
  395. this._gridNodesExpandedState[key] = expandedState;
  396. /**
  397. * @param {!WebInspector.DataGridNode} node
  398. * @param {string} key
  399. */
  400. function callback(node, key)
  401. {
  402. if (node.expanded)
  403. expandedState[key] = true;
  404. }
  405. this._forEachGridNode(callback);
  406. },
  407. _restoreExpandedState: function()
  408. {
  409. if (!this._currentResourceId)
  410. return;
  411. var key = this._resourceKindId(this._currentResourceId);
  412. var expandedState = this._gridNodesExpandedState[key];
  413. if (!expandedState)
  414. return;
  415. /**
  416. * @param {!WebInspector.DataGridNode} node
  417. * @param {string} key
  418. */
  419. function callback(node, key)
  420. {
  421. if (expandedState[key])
  422. node.expand();
  423. }
  424. this._forEachGridNode(callback);
  425. },
  426. _saveScrollState: function()
  427. {
  428. if (!this._currentResourceId)
  429. return;
  430. var key = this._resourceKindId(this._currentResourceId);
  431. this._gridScrollPositions[key] = {
  432. scrollTop: this._stateGrid.scrollContainer.scrollTop,
  433. scrollLeft: this._stateGrid.scrollContainer.scrollLeft
  434. };
  435. },
  436. _restoreScrollState: function()
  437. {
  438. if (!this._currentResourceId)
  439. return;
  440. var key = this._resourceKindId(this._currentResourceId);
  441. var scrollState = this._gridScrollPositions[key];
  442. if (!scrollState)
  443. return;
  444. this._stateGrid.scrollContainer.scrollTop = scrollState.scrollTop;
  445. this._stateGrid.scrollContainer.scrollLeft = scrollState.scrollLeft;
  446. },
  447. /**
  448. * @param {!CanvasAgent.ResourceStateDescriptor} descriptor
  449. * @return {!WebInspector.DataGridNode}
  450. */
  451. _createDataGridNode: function(descriptor)
  452. {
  453. var name = descriptor.name;
  454. var callArgument = descriptor.value;
  455. /** @type {!Element|string} */
  456. var valueElement = callArgument ? WebInspector.CanvasProfileDataGridHelper.createCallArgumentElement(callArgument) : "";
  457. /** @type {!Element|string} */
  458. var nameElement = name;
  459. if (typeof descriptor.enumValueForName !== "undefined")
  460. nameElement = WebInspector.CanvasProfileDataGridHelper.createEnumValueElement(name, +descriptor.enumValueForName);
  461. if (descriptor.isArray && descriptor.values) {
  462. if (typeof nameElement === "string")
  463. nameElement += "[" + descriptor.values.length + "]";
  464. else {
  465. var element = document.createElement("span");
  466. element.appendChild(nameElement);
  467. element.createTextChild("[" + descriptor.values.length + "]");
  468. nameElement = element;
  469. }
  470. }
  471. var data = {};
  472. data[0] = nameElement;
  473. data[1] = valueElement;
  474. var node = new WebInspector.DataGridNode(data);
  475. node.selectable = false;
  476. node.name = name;
  477. return node;
  478. },
  479. __proto__: WebInspector.View.prototype
  480. }