9f5d05b0d3253f20909d4fa91addf859ecb9e51d
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / sdk / PaintProfiler.js
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 /**
32  * @constructor
33  * @param {!WebInspector.Target} target
34  * @param {string} snapshotId
35  */
36 WebInspector.PaintProfilerSnapshot = function(target, snapshotId)
37 {
38     this._target = target;
39     this._id = snapshotId;
40 }
41
42 /**
43  * @param {!WebInspector.Target} target
44  * @param {string} encodedPicture
45  * @param {function(?WebInspector.PaintProfilerSnapshot)} callback
46  */
47 WebInspector.PaintProfilerSnapshot.load = function(target, encodedPicture, callback)
48 {
49     var wrappedCallback = InspectorBackend.wrapClientCallback(callback, "LayerTreeAgent.loadSnapshot(): ", WebInspector.PaintProfilerSnapshot.bind(null, target));
50     target.layerTreeAgent().loadSnapshot(encodedPicture, wrappedCallback);
51 }
52
53 /**
54  * @param {!Array.<!WebInspector.RawPaintProfilerLogItem>} log
55  * @return {!Array.<!WebInspector.PaintProfilerLogItem>}
56  */
57 WebInspector.PaintProfilerSnapshot._processAnnotations = function(log)
58 {
59     var result = [];
60     /** @type {!Array.<!Object.<string, string>>} */
61     var commentGroupStack = [];
62
63     for (var i = 0; i < log.length; ++i) {
64         var method = log[i].method;
65         switch (method) {
66         case "beginCommentGroup":
67             commentGroupStack.push({});
68             break;
69         case "addComment":
70             var group = commentGroupStack.peekLast();
71             if (!group) {
72                 console.assert(false, "Stray comment without a group");
73                 break;
74             }
75             var key = String(log[i].params["key"]);
76             var value = String(log[i].params["value"]);
77             if (!key || typeof value === "undefined") {
78                 console.assert(false, "Missing key or value in addComment() params");
79                 break;
80             }
81             if (key in group) {
82                 console.assert(false, "Duplicate key in comment group");
83                 break;
84             }
85             group[key] = value;
86             break;
87         case "endCommentGroup":
88             if (!commentGroupStack.length)
89                 console.assert(false, "Unbalanced commentGroupEnd call");
90             else
91                 commentGroupStack.pop();
92             break;
93         default:
94             result.push(new WebInspector.PaintProfilerLogItem(log[i], i, commentGroupStack.peekLast()));
95         }
96     }
97     return result;
98 }
99
100 WebInspector.PaintProfilerSnapshot.prototype = {
101     dispose: function()
102     {
103         this._target.layerTreeAgent().releaseSnapshot(this._id);
104     },
105
106     /**
107      * @return {!WebInspector.Target}
108      */
109     target: function()
110     {
111         return this._target;
112     },
113
114     /**
115      * @param {?number} firstStep
116      * @param {?number} lastStep
117      * @param {?number} scale
118      * @param {function(string=)} callback
119      */
120     requestImage: function(firstStep, lastStep, scale, callback)
121     {
122         var wrappedCallback = InspectorBackend.wrapClientCallback(callback, "LayerTreeAgent.replaySnapshot(): ");
123         this._target.layerTreeAgent().replaySnapshot(this._id, firstStep || undefined, lastStep || undefined, scale || 1.0, wrappedCallback);
124     },
125
126     /**
127      * @param {function(!Array.<!LayerTreeAgent.PaintProfile>=)} callback
128      */
129     profile: function(callback)
130     {
131         var wrappedCallback = InspectorBackend.wrapClientCallback(callback, "LayerTreeAgent.profileSnapshot(): ");
132         this._target.layerTreeAgent().profileSnapshot(this._id, 5, 1, wrappedCallback);
133     },
134
135     /**
136      * @param {function(!Array.<!WebInspector.PaintProfilerLogItem>=)} callback
137      */
138     commandLog: function(callback)
139     {
140         /**
141          * @param {?string} error
142          * @param {!Array.<!WebInspector.RawPaintProfilerLogItem>} log
143          */
144         function callbackWrapper(error, log)
145         {
146             if (error) {
147                 console.error("LayerTreeAgent.snapshotCommandLog(): " + error);
148                 callback();
149                 return;
150             }
151             callback(WebInspector.PaintProfilerSnapshot._processAnnotations(log));
152         }
153
154         this._target.layerTreeAgent().snapshotCommandLog(this._id, callbackWrapper);
155     }
156 };
157
158 /**
159  * @typedef {!{method: string, params: Array.<Object.<string, *>>}}
160  */
161 WebInspector.RawPaintProfilerLogItem;
162
163 /**
164  * @constructor
165  * @param {!WebInspector.RawPaintProfilerLogItem} rawEntry
166  * @param {number} commandIndex
167  * @param {!Object.<string, string>=} annotations
168  */
169 WebInspector.PaintProfilerLogItem = function(rawEntry, commandIndex, annotations)
170 {
171     this.method = rawEntry.method;
172     this.params = rawEntry.params;
173     this.annotations = annotations;
174     this.commandIndex = commandIndex;
175 }
176
177 WebInspector.PaintProfilerLogItem.prototype = {
178     /**
179      * @return {number}
180      */
181     nodeId: function()
182     {
183         if (!this.annotations)
184             return 0;
185         var inspectorId = this.annotations["INSPECTOR_ID"];
186         return Number(inspectorId);
187     }
188 }