Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / promises / PromisePane.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * @constructor
7  * @extends {WebInspector.VBox}
8  */
9 WebInspector.PromisePane = function()
10 {
11     WebInspector.VBox.call(this);
12     this.registerRequiredCSS("promises/promisePane.css");
13     this.element.classList.add("promises");
14
15     var statusBar = this.element.createChild("div", "panel-status-bar");
16     this._recordButton = new WebInspector.StatusBarButton(WebInspector.UIString("Record Promises"), "record-profile-status-bar-item");
17     this._recordButton.addEventListener("click", this._recordButtonClicked.bind(this));
18     statusBar.appendChild(this._recordButton.element);
19     var clearButton = new WebInspector.StatusBarButton(WebInspector.UIString("Clear"), "clear-status-bar-item");
20     clearButton.addEventListener("click", this._clearButtonClicked.bind(this));
21     statusBar.appendChild(clearButton.element);
22     this._refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
23     this._refreshButton.addEventListener("click", this._refreshButtonClicked.bind(this));
24     this._refreshButton.setEnabled(false);
25     statusBar.appendChild(this._refreshButton.element);
26     this._liveCheckbox = new WebInspector.StatusBarCheckbox(WebInspector.UIString("Live"));
27     this._liveCheckbox.element.title = WebInspector.UIString("Live Recording");
28     this._liveCheckbox.inputElement.disabled = true;
29     statusBar.appendChild(this._liveCheckbox.element);
30
31     this._dataGridContainer = new WebInspector.VBox();
32     this._dataGridContainer.show(this.element);
33     var columns = [
34         { id: "location", title: WebInspector.UIString("Location"), disclosure: true },
35         { id: "status", title: WebInspector.UIString("Status") },
36         { id: "tts", title: WebInspector.UIString("Time to settle") }
37     ];
38     this._dataGrid = new WebInspector.DataGrid(columns, undefined, undefined, undefined, this._onContextMenu.bind(this));
39     this._dataGrid.show(this._dataGridContainer.element);
40
41     this._linkifier = new WebInspector.Linkifier();
42 }
43
44 WebInspector.PromisePane.prototype = {
45     _recordButtonClicked: function(event)
46     {
47         var recording = !this._recordButton.toggled;
48         this._recordButton.toggled = recording;
49         this._refreshButton.setEnabled(recording);
50         if (recording)
51             this._enablePromiseTracker();
52         else
53             this._disablePromiseTracker();
54     },
55
56     _refreshButtonClicked: function(event)
57     {
58         this._updateData();
59     },
60
61     _clearButtonClicked: function(event)
62     {
63         this._clear();
64     },
65
66     _enablePromiseTracker: function()
67     {
68         var mainTarget = WebInspector.targetManager.mainTarget();
69         if (mainTarget) {
70             mainTarget.debuggerAgent().enablePromiseTracker();
71             this._target = mainTarget;
72         }
73     },
74
75     _disablePromiseTracker: function()
76     {
77         if (this._target) {
78             this._target.debuggerAgent().disablePromiseTracker();
79             delete this._target;
80         }
81         this._clear();
82     },
83
84     /**
85      * @param {!DebuggerAgent.PromiseDetails} p1
86      * @param {!DebuggerAgent.PromiseDetails} p2
87      * @return {number}
88      */
89     _comparePromises: function(p1, p2) {
90         var t1 = p1.creationTime || 0;
91         var t2 = p2.creationTime || 0;
92         return t1 - t2;
93     },
94
95     _updateData: function()
96     {
97         /**
98          * @param {?Protocol.Error} error
99          * @param {?Array.<!DebuggerAgent.PromiseDetails>} promiseData
100          * @this {WebInspector.PromisePane}
101          */
102         function callback(error, promiseData)
103         {
104             if (error || !promiseData)
105                 return;
106
107             promiseData.sort(this._comparePromises);
108             var nodesToInsert = { __proto__: null };
109             for (var i = 0; i < promiseData.length; i++) {
110                 var promise = promiseData[i];
111                 var status = createElementWithClass("div", "status");
112                 status.classList.add(promise.status);
113                 status.createTextChild(promise.status);
114                 var data = {
115                     promiseId: promise.id,
116                     status: status
117                 };
118                 if (promise.callFrame)
119                     data.location = this._linkifier.linkifyConsoleCallFrame(this._target, promise.callFrame);
120                 if (promise.creationTime && promise.settlementTime && promise.settlementTime >= promise.creationTime)
121                     data.tts = Number.millisToString(promise.settlementTime - promise.creationTime, true);
122                 var node = new WebInspector.DataGridNode(data, false);
123                 nodesToInsert[promise.id] = { node: node, parentId: promise.parentId };
124             }
125
126             var rootNode = this._dataGrid.rootNode();
127
128             for (var id in nodesToInsert) {
129                 var node = nodesToInsert[id].node;
130                 var parentId = nodesToInsert[id].parentId;
131                 var parentNode = (parentId && nodesToInsert[parentId]) ? nodesToInsert[parentId].node : rootNode;
132                 parentNode.appendChild(node);
133                 parentNode.expanded = true;
134             }
135         }
136
137         this._clear();
138         if (this._target)
139             this._target.debuggerAgent().getPromises(callback.bind(this));
140     },
141
142     _clear: function()
143     {
144         this._dataGrid.rootNode().removeChildren();
145         this._linkifier.reset();
146     },
147
148     /**
149      * @param {!WebInspector.ContextMenu} contextMenu
150      * @param {!WebInspector.DataGridNode} node
151      */
152     _onContextMenu: function(contextMenu, node)
153     {
154         if (!this._target)
155             return;
156         var promiseId = node.data.promiseId;
157
158         contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Show in console" : "Show In Console"), showPromiseInConsole.bind(this));
159         contextMenu.show();
160
161         /**
162          * @this {WebInspector.PromisePane}
163          */
164         function showPromiseInConsole()
165         {
166             if (this._target)
167                 this._target.debuggerAgent().getPromiseById(promiseId, "console", didGetPromiseById.bind(this));
168         }
169
170         /**
171          * @param {?Protocol.Error} error
172          * @param {?RuntimeAgent.RemoteObject} promise
173          * @this {WebInspector.PromisePane}
174          */
175         function didGetPromiseById(error, promise)
176         {
177             if (error || !promise)
178                 return;
179
180             if (!this._target)
181                 return;
182
183             this._target.consoleAgent().setLastEvaluationResult(promise.objectId);
184             var message = new WebInspector.ConsoleMessage(this._target,
185                                                           WebInspector.ConsoleMessage.MessageSource.Other,
186                                                           WebInspector.ConsoleMessage.MessageLevel.Log,
187                                                           "",
188                                                           WebInspector.ConsoleMessage.MessageType.Log,
189                                                           undefined,
190                                                           undefined,
191                                                           undefined,
192                                                           undefined,
193                                                           [promise]);
194             this._target.consoleModel.addMessage(message);
195             WebInspector.console.show();
196         }
197     },
198
199     __proto__: WebInspector.VBox.prototype
200 }