Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / HeapSnapshotProxy.js
1 /*
2  * Copyright (C) 2011 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 copyrightdd
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 {function(string, *)} eventHandler
34  * @extends {WebInspector.Object}
35  */
36 WebInspector.HeapSnapshotWorkerProxy = function(eventHandler)
37 {
38     this._eventHandler = eventHandler;
39     this._nextObjectId = 1;
40     this._nextCallId = 1;
41     this._callbacks = [];
42     this._previousCallbacks = [];
43     this._worker = new Worker("HeapSnapshotWorker.js");
44     this._worker.onmessage = this._messageReceived.bind(this);
45 }
46
47 WebInspector.HeapSnapshotWorkerProxy.prototype = {
48     /**
49      * @param {number} profileUid
50      * @param {function(!WebInspector.HeapSnapshotProxy)} snapshotReceivedCallback
51      * @return {!WebInspector.HeapSnapshotLoaderProxy}
52      */
53     createLoader: function(profileUid, snapshotReceivedCallback)
54     {
55         var objectId = this._nextObjectId++;
56         var proxy = new WebInspector.HeapSnapshotLoaderProxy(this, objectId, profileUid, snapshotReceivedCallback);
57         this._postMessage({callId: this._nextCallId++, disposition: "create", objectId: objectId, methodName: "WebInspector.HeapSnapshotLoader"});
58         return proxy;
59     },
60
61     dispose: function()
62     {
63         this._worker.terminate();
64         if (this._interval)
65             clearInterval(this._interval);
66     },
67
68     disposeObject: function(objectId)
69     {
70         this._postMessage({callId: this._nextCallId++, disposition: "dispose", objectId: objectId});
71     },
72
73     evaluateForTest: function(script, callback)
74     {
75         var callId = this._nextCallId++;
76         this._callbacks[callId] = callback;
77         this._postMessage({callId: callId, disposition: "evaluateForTest", source: script});
78     },
79
80     callGetter: function(callback, objectId, getterName)
81     {
82         var callId = this._nextCallId++;
83         this._callbacks[callId] = callback;
84         this._postMessage({callId: callId, disposition: "getter", objectId: objectId, methodName: getterName});
85     },
86
87     /**
88      * @param {?function(...[?])} callback
89      * @param {string} objectId
90      * @param {string} methodName
91      * @param {function(new:T, ...[?])} proxyConstructor
92      * @return {?Object}
93      * @template T
94      */
95     callFactoryMethod: function(callback, objectId, methodName, proxyConstructor)
96     {
97         var callId = this._nextCallId++;
98         var methodArguments = Array.prototype.slice.call(arguments, 4);
99         var newObjectId = this._nextObjectId++;
100
101         /**
102          * @this {WebInspector.HeapSnapshotWorkerProxy}
103          */
104         function wrapCallback(remoteResult)
105         {
106             callback(remoteResult ? new proxyConstructor(this, newObjectId) : null);
107         }
108
109         if (callback) {
110             this._callbacks[callId] = wrapCallback.bind(this);
111             this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
112             return null;
113         } else {
114             this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
115             return new proxyConstructor(this, newObjectId);
116         }
117     },
118
119     /**
120      * @param {function(*)} callback
121      * @param {string} objectId
122      * @param {string} methodName
123      */
124     callMethod: function(callback, objectId, methodName)
125     {
126         var callId = this._nextCallId++;
127         var methodArguments = Array.prototype.slice.call(arguments, 3);
128         if (callback)
129             this._callbacks[callId] = callback;
130         this._postMessage({callId: callId, disposition: "method", objectId: objectId, methodName: methodName, methodArguments: methodArguments});
131     },
132
133     startCheckingForLongRunningCalls: function()
134     {
135         if (this._interval)
136             return;
137         this._checkLongRunningCalls();
138         this._interval = setInterval(this._checkLongRunningCalls.bind(this), 300);
139     },
140
141     _checkLongRunningCalls: function()
142     {
143         for (var callId in this._previousCallbacks)
144             if (!(callId in this._callbacks))
145                 delete this._previousCallbacks[callId];
146         var hasLongRunningCalls = false;
147         for (callId in this._previousCallbacks) {
148             hasLongRunningCalls = true;
149             break;
150         }
151         this.dispatchEventToListeners("wait", hasLongRunningCalls);
152         for (callId in this._callbacks)
153             this._previousCallbacks[callId] = true;
154     },
155
156     _findFunction: function(name)
157     {
158         var path = name.split(".");
159         var result = window;
160         for (var i = 0; i < path.length; ++i)
161             result = result[path[i]];
162         return result;
163     },
164
165     /**
166      * @param {!MessageEvent} event
167      */
168     _messageReceived: function(event)
169     {
170         var data = event.data;
171         if (data.eventName) {
172             if (this._eventHandler)
173                 this._eventHandler(data.eventName, data.data);
174             return;
175         }
176         if (data.error) {
177             if (data.errorMethodName)
178                 WebInspector.log(WebInspector.UIString("An error happened when a call for method '%s' was requested", data.errorMethodName));
179             WebInspector.log(data["errorCallStack"]);
180             delete this._callbacks[data.callId];
181             return;
182         }
183         if (!this._callbacks[data.callId])
184             return;
185         var callback = this._callbacks[data.callId];
186         delete this._callbacks[data.callId];
187         callback(data.result);
188     },
189
190     _postMessage: function(message)
191     {
192         this._worker.postMessage(message);
193     },
194
195     __proto__: WebInspector.Object.prototype
196 }
197
198
199 /**
200  * @constructor
201  * @param {number} objectId
202  */
203 WebInspector.HeapSnapshotProxyObject = function(worker, objectId)
204 {
205     this._worker = worker;
206     this._objectId = objectId;
207 }
208
209 WebInspector.HeapSnapshotProxyObject.prototype = {
210     /**
211      * @param {string} workerMethodName
212      * @param {!Array.<*>} args
213      */
214     _callWorker: function(workerMethodName, args)
215     {
216         args.splice(1, 0, this._objectId);
217         return this._worker[workerMethodName].apply(this._worker, args);
218     },
219
220     dispose: function()
221     {
222         this._worker.disposeObject(this._objectId);
223     },
224
225     disposeWorker: function()
226     {
227         this._worker.dispose();
228     },
229
230     /**
231      * @param {?function(...[?])} callback
232      * @param {string} methodName
233      * @param {function (new:T, ...[?])} proxyConstructor
234      * @param {...*} var_args
235      * @return {?WebInspector.HeapSnapshotProviderProxy}
236      * @template T
237      */
238     callFactoryMethod: function(callback, methodName, proxyConstructor, var_args)
239     {
240         return this._callWorker("callFactoryMethod", Array.prototype.slice.call(arguments, 0));
241     },
242
243     /**
244      * @param {function(T)|undefined} callback
245      * @param {string} getterName
246      * @return {*}
247      * @template T
248      */
249     callGetter: function(callback, getterName)
250     {
251         return this._callWorker("callGetter", Array.prototype.slice.call(arguments, 0));
252     },
253
254     /**
255      * @param {function(T)|undefined} callback
256      * @param {string} methodName
257      * @param {...*} var_args
258      * @return {*}
259      * @template T
260      */
261     callMethod: function(callback, methodName, var_args)
262     {
263         return this._callWorker("callMethod", Array.prototype.slice.call(arguments, 0));
264     },
265
266     get worker() {
267         return this._worker;
268     }
269 };
270
271 /**
272  * @constructor
273  * @extends {WebInspector.HeapSnapshotProxyObject}
274  * @implements {WebInspector.OutputStream}
275  * @param {number} objectId
276  * @param {number} profileUid
277  * @param {function(!WebInspector.HeapSnapshotProxy)} snapshotReceivedCallback
278  */
279 WebInspector.HeapSnapshotLoaderProxy = function(worker, objectId, profileUid, snapshotReceivedCallback)
280 {
281     WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
282     this._profileUid = profileUid;
283     this._snapshotReceivedCallback = snapshotReceivedCallback;
284 }
285
286 WebInspector.HeapSnapshotLoaderProxy.prototype = {
287     /**
288      * @param {string} chunk
289      * @param {function(!WebInspector.OutputStream)=} callback
290      */
291     write: function(chunk, callback)
292     {
293         this.callMethod(callback, "write", chunk);
294     },
295
296     /**
297      * @param {function()=} callback
298      */
299     close: function(callback)
300     {
301         /**
302          * @this {WebInspector.HeapSnapshotLoaderProxy}
303          */
304         function buildSnapshot()
305         {
306             if (callback)
307                 callback();
308             this.callFactoryMethod(updateStaticData.bind(this), "buildSnapshot", WebInspector.HeapSnapshotProxy);
309         }
310
311         /**
312          * @param {!WebInspector.HeapSnapshotProxy} snapshotProxy
313          * @this {WebInspector.HeapSnapshotLoaderProxy}
314          */
315         function updateStaticData(snapshotProxy)
316         {
317             this.dispose();
318             snapshotProxy.setProfileUid(this._profileUid);
319             snapshotProxy.updateStaticData(this._snapshotReceivedCallback.bind(this));
320         }
321
322         this.callMethod(buildSnapshot.bind(this), "close");
323     },
324
325     __proto__: WebInspector.HeapSnapshotProxyObject.prototype
326 }
327
328
329 /**
330  * @constructor
331  * @extends {WebInspector.HeapSnapshotProxyObject}
332  * @param {!WebInspector.HeapSnapshotWorkerProxy} worker
333  * @param {number} objectId
334  */
335 WebInspector.HeapSnapshotProxy = function(worker, objectId)
336 {
337     WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
338     /** @type {?WebInspector.HeapSnapshotCommon.StaticData} */
339     this._staticData = null;
340 }
341
342 WebInspector.HeapSnapshotProxy.prototype = {
343     aggregates: function(sortedIndexes, key, filter, callback)
344     {
345         this.callMethod(callback, "aggregates", sortedIndexes, key, filter);
346     },
347
348     aggregatesForDiff: function(callback)
349     {
350         this.callMethod(callback, "aggregatesForDiff");
351     },
352
353     calculateSnapshotDiff: function(baseSnapshotId, baseSnapshotAggregates, callback)
354     {
355         this.callMethod(callback, "calculateSnapshotDiff", baseSnapshotId, baseSnapshotAggregates);
356     },
357
358     nodeClassName: function(snapshotObjectId, callback)
359     {
360         this.callMethod(callback, "nodeClassName", snapshotObjectId);
361     },
362
363     dominatorIdsForNode: function(nodeIndex, callback)
364     {
365         this.callMethod(callback, "dominatorIdsForNode", nodeIndex);
366     },
367
368     /**
369      * @param {number} nodeIndex
370      * @param {boolean} showHiddenData
371      * @return {?WebInspector.HeapSnapshotProviderProxy}
372      */
373     createEdgesProvider: function(nodeIndex, showHiddenData)
374     {
375         return this.callFactoryMethod(null, "createEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex, showHiddenData);
376     },
377
378     /**
379      * @param {number} nodeIndex
380      * @param {boolean} showHiddenData
381      * @return {?WebInspector.HeapSnapshotProviderProxy}
382      */
383     createRetainingEdgesProvider: function(nodeIndex, showHiddenData)
384     {
385         return this.callFactoryMethod(null, "createRetainingEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex, showHiddenData);
386     },
387
388     /**
389      * @param {string} baseSnapshotId
390      * @param {string} className
391      * @return {?WebInspector.HeapSnapshotProviderProxy}
392      */
393     createAddedNodesProvider: function(baseSnapshotId, className)
394     {
395         return this.callFactoryMethod(null, "createAddedNodesProvider", WebInspector.HeapSnapshotProviderProxy, baseSnapshotId, className);
396     },
397
398     /**
399      * @param {!Array.<number>} nodeIndexes
400      * @return {?WebInspector.HeapSnapshotProviderProxy}
401      */
402     createDeletedNodesProvider: function(nodeIndexes)
403     {
404         return this.callFactoryMethod(null, "createDeletedNodesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndexes);
405     },
406
407     /**
408      * @param {function(*):boolean} filter
409      * @return {?WebInspector.HeapSnapshotProviderProxy}
410      */
411     createNodesProvider: function(filter)
412     {
413         return this.callFactoryMethod(null, "createNodesProvider", WebInspector.HeapSnapshotProviderProxy, filter);
414     },
415
416     /**
417      * @param {string} className
418      * @param {string} aggregatesKey
419      * @return {?WebInspector.HeapSnapshotProviderProxy}
420      */
421     createNodesProviderForClass: function(className, aggregatesKey)
422     {
423         return this.callFactoryMethod(null, "createNodesProviderForClass", WebInspector.HeapSnapshotProviderProxy, className, aggregatesKey);
424     },
425
426     /**
427      * @param {number} nodeIndex
428      * @return {?WebInspector.HeapSnapshotProviderProxy}
429      */
430     createNodesProviderForDominator: function(nodeIndex)
431     {
432         return this.callFactoryMethod(null, "createNodesProviderForDominator", WebInspector.HeapSnapshotProviderProxy, nodeIndex);
433     },
434
435     allocationTracesTops: function(callback)
436     {
437         this.callMethod(callback, "allocationTracesTops");
438     },
439
440     allocationNodeCallers: function(nodeId, callback)
441     {
442         this.callMethod(callback, "allocationNodeCallers", nodeId);
443     },
444
445     dispose: function()
446     {
447         throw new Error("Should never be called");
448     },
449
450     get nodeCount()
451     {
452         return this._staticData.nodeCount;
453     },
454
455     get rootNodeIndex()
456     {
457         return this._staticData.rootNodeIndex;
458     },
459
460     updateStaticData: function(callback)
461     {
462         /**
463          * @param {!WebInspector.HeapSnapshotCommon.StaticData} staticData
464          * @this {WebInspector.HeapSnapshotProxy}
465          */
466         function dataReceived(staticData)
467         {
468             this._staticData = staticData;
469             callback(this);
470         }
471         this.callMethod(dataReceived.bind(this), "updateStaticData");
472     },
473
474     get totalSize()
475     {
476         return this._staticData.totalSize;
477     },
478
479     get uid()
480     {
481         return this._profileUid;
482     },
483
484     setProfileUid: function(profileUid)
485     {
486         this._profileUid = profileUid;
487     },
488
489     /**
490      * @return {number}
491      */
492     maxJSObjectId: function()
493     {
494         return this._staticData.maxJSObjectId;
495     },
496
497     __proto__: WebInspector.HeapSnapshotProxyObject.prototype
498 }
499
500
501 /**
502  * @constructor
503  * @extends {WebInspector.HeapSnapshotProxyObject}
504  * @param {!WebInspector.HeapSnapshotWorkerProxy} worker
505  * @param {number} objectId
506  */
507 WebInspector.HeapSnapshotProviderProxy = function(worker, objectId)
508 {
509     WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
510 }
511
512 WebInspector.HeapSnapshotProviderProxy.prototype = {
513     nodePosition: function(snapshotObjectId, callback)
514     {
515         this.callMethod(callback, "nodePosition", snapshotObjectId);
516     },
517
518     isEmpty: function(callback)
519     {
520         this.callMethod(callback, "isEmpty");
521     },
522
523     /**
524      * @param {number} startPosition
525      * @param {number} endPosition
526      * @param {function(!WebInspector.HeapSnapshotCommon.ItemsRange)} callback
527      */
528     serializeItemsRange: function(startPosition, endPosition, callback)
529     {
530         this.callMethod(callback, "serializeItemsRange", startPosition, endPosition);
531     },
532
533     sortAndRewind: function(comparator, callback)
534     {
535         this.callMethod(callback, "sortAndRewind", comparator);
536     },
537
538     __proto__: WebInspector.HeapSnapshotProxyObject.prototype
539 }
540