2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @param {function(string, *)} eventHandler
34 * @extends {WebInspector.Object}
36 WebInspector.HeapSnapshotWorkerProxy = function(eventHandler)
38 this._eventHandler = eventHandler;
39 this._nextObjectId = 1;
42 this._previousCallbacks = [];
43 this._worker = new Worker("HeapSnapshotWorker.js");
44 this._worker.onmessage = this._messageReceived.bind(this);
47 WebInspector.HeapSnapshotWorkerProxy.prototype = {
49 * @param {number} profileUid
50 * @param {function(!WebInspector.HeapSnapshotProxy)} snapshotReceivedCallback
51 * @return {!WebInspector.HeapSnapshotLoaderProxy}
53 createLoader: function(profileUid, snapshotReceivedCallback)
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"});
63 this._worker.terminate();
65 clearInterval(this._interval);
68 disposeObject: function(objectId)
70 this._postMessage({callId: this._nextCallId++, disposition: "dispose", objectId: objectId});
73 evaluateForTest: function(script, callback)
75 var callId = this._nextCallId++;
76 this._callbacks[callId] = callback;
77 this._postMessage({callId: callId, disposition: "evaluateForTest", source: script});
81 * @param {?function(...[?])} callback
82 * @param {string} objectId
83 * @param {string} methodName
84 * @param {function(new:T, ...[?])} proxyConstructor
88 callFactoryMethod: function(callback, objectId, methodName, proxyConstructor)
90 var callId = this._nextCallId++;
91 var methodArguments = Array.prototype.slice.call(arguments, 4);
92 var newObjectId = this._nextObjectId++;
95 * @this {WebInspector.HeapSnapshotWorkerProxy}
97 function wrapCallback(remoteResult)
99 callback(remoteResult ? new proxyConstructor(this, newObjectId) : null);
103 this._callbacks[callId] = wrapCallback.bind(this);
104 this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
107 this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
108 return new proxyConstructor(this, newObjectId);
113 * @param {function(*)} callback
114 * @param {string} objectId
115 * @param {string} methodName
117 callMethod: function(callback, objectId, methodName)
119 var callId = this._nextCallId++;
120 var methodArguments = Array.prototype.slice.call(arguments, 3);
122 this._callbacks[callId] = callback;
123 this._postMessage({callId: callId, disposition: "method", objectId: objectId, methodName: methodName, methodArguments: methodArguments});
126 startCheckingForLongRunningCalls: function()
130 this._checkLongRunningCalls();
131 this._interval = setInterval(this._checkLongRunningCalls.bind(this), 300);
134 _checkLongRunningCalls: function()
136 for (var callId in this._previousCallbacks)
137 if (!(callId in this._callbacks))
138 delete this._previousCallbacks[callId];
139 var hasLongRunningCalls = false;
140 for (callId in this._previousCallbacks) {
141 hasLongRunningCalls = true;
144 this.dispatchEventToListeners("wait", hasLongRunningCalls);
145 for (callId in this._callbacks)
146 this._previousCallbacks[callId] = true;
150 * @param {!MessageEvent} event
152 _messageReceived: function(event)
154 var data = event.data;
155 if (data.eventName) {
156 if (this._eventHandler)
157 this._eventHandler(data.eventName, data.data);
161 if (data.errorMethodName)
162 WebInspector.console.log(WebInspector.UIString("An error happened when a call for method '%s' was requested", data.errorMethodName));
163 WebInspector.console.log(data["errorCallStack"]);
164 delete this._callbacks[data.callId];
167 if (!this._callbacks[data.callId])
169 var callback = this._callbacks[data.callId];
170 delete this._callbacks[data.callId];
171 callback(data.result);
174 _postMessage: function(message)
176 this._worker.postMessage(message);
179 __proto__: WebInspector.Object.prototype
185 * @param {number} objectId
187 WebInspector.HeapSnapshotProxyObject = function(worker, objectId)
189 this._worker = worker;
190 this._objectId = objectId;
193 WebInspector.HeapSnapshotProxyObject.prototype = {
195 * @param {string} workerMethodName
196 * @param {!Array.<*>} args
198 _callWorker: function(workerMethodName, args)
200 args.splice(1, 0, this._objectId);
201 return this._worker[workerMethodName].apply(this._worker, args);
206 this._worker.disposeObject(this._objectId);
209 disposeWorker: function()
211 this._worker.dispose();
215 * @param {?function(...[?])} callback
216 * @param {string} methodName
217 * @param {function (new:T, ...[?])} proxyConstructor
218 * @param {...*} var_args
222 callFactoryMethod: function(callback, methodName, proxyConstructor, var_args)
224 return this._callWorker("callFactoryMethod", Array.prototype.slice.call(arguments, 0));
228 * @param {function(T)|undefined} callback
229 * @param {string} methodName
230 * @param {...*} var_args
234 callMethod: function(callback, methodName, var_args)
236 return this._callWorker("callMethod", Array.prototype.slice.call(arguments, 0));
242 * @extends {WebInspector.HeapSnapshotProxyObject}
243 * @implements {WebInspector.OutputStream}
244 * @param {number} objectId
245 * @param {number} profileUid
246 * @param {function(!WebInspector.HeapSnapshotProxy)} snapshotReceivedCallback
248 WebInspector.HeapSnapshotLoaderProxy = function(worker, objectId, profileUid, snapshotReceivedCallback)
250 WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
251 this._profileUid = profileUid;
252 this._snapshotReceivedCallback = snapshotReceivedCallback;
255 WebInspector.HeapSnapshotLoaderProxy.prototype = {
257 * @param {string} chunk
258 * @param {function(!WebInspector.OutputStream)=} callback
260 write: function(chunk, callback)
262 this.callMethod(callback, "write", chunk);
266 * @param {function()=} callback
268 close: function(callback)
271 * @this {WebInspector.HeapSnapshotLoaderProxy}
273 function buildSnapshot()
277 var showHiddenData = WebInspector.settings.showAdvancedHeapSnapshotProperties.get();
278 this.callFactoryMethod(updateStaticData.bind(this), "buildSnapshot", WebInspector.HeapSnapshotProxy, showHiddenData);
282 * @param {!WebInspector.HeapSnapshotProxy} snapshotProxy
283 * @this {WebInspector.HeapSnapshotLoaderProxy}
285 function updateStaticData(snapshotProxy)
288 snapshotProxy.setProfileUid(this._profileUid);
289 snapshotProxy.updateStaticData(this._snapshotReceivedCallback.bind(this));
292 this.callMethod(buildSnapshot.bind(this), "close");
295 __proto__: WebInspector.HeapSnapshotProxyObject.prototype
301 * @extends {WebInspector.HeapSnapshotProxyObject}
302 * @param {!WebInspector.HeapSnapshotWorkerProxy} worker
303 * @param {number} objectId
305 WebInspector.HeapSnapshotProxy = function(worker, objectId)
307 WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
308 /** @type {?WebInspector.HeapSnapshotCommon.StaticData} */
309 this._staticData = null;
312 WebInspector.HeapSnapshotProxy.prototype = {
314 * @param {!WebInspector.HeapSnapshotCommon.NodeFilter} filter
315 * @param {function(!Object.<string, !WebInspector.HeapSnapshotCommon.Aggregate>)} callback
317 aggregatesWithFilter: function(filter, callback)
319 this.callMethod(callback, "aggregatesWithFilter", filter);
322 aggregatesForDiff: function(callback)
324 this.callMethod(callback, "aggregatesForDiff");
327 calculateSnapshotDiff: function(baseSnapshotId, baseSnapshotAggregates, callback)
329 this.callMethod(callback, "calculateSnapshotDiff", baseSnapshotId, baseSnapshotAggregates);
332 nodeClassName: function(snapshotObjectId, callback)
334 this.callMethod(callback, "nodeClassName", snapshotObjectId);
337 dominatorIdsForNode: function(nodeIndex, callback)
339 this.callMethod(callback, "dominatorIdsForNode", nodeIndex);
343 * @param {number} nodeIndex
344 * @return {!WebInspector.HeapSnapshotProviderProxy}
346 createEdgesProvider: function(nodeIndex)
348 return this.callFactoryMethod(null, "createEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex);
352 * @param {number} nodeIndex
353 * @return {!WebInspector.HeapSnapshotProviderProxy}
355 createRetainingEdgesProvider: function(nodeIndex)
357 return this.callFactoryMethod(null, "createRetainingEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex);
361 * @param {string} baseSnapshotId
362 * @param {string} className
363 * @return {?WebInspector.HeapSnapshotProviderProxy}
365 createAddedNodesProvider: function(baseSnapshotId, className)
367 return this.callFactoryMethod(null, "createAddedNodesProvider", WebInspector.HeapSnapshotProviderProxy, baseSnapshotId, className);
371 * @param {!Array.<number>} nodeIndexes
372 * @return {?WebInspector.HeapSnapshotProviderProxy}
374 createDeletedNodesProvider: function(nodeIndexes)
376 return this.callFactoryMethod(null, "createDeletedNodesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndexes);
380 * @param {function(*):boolean} filter
381 * @return {?WebInspector.HeapSnapshotProviderProxy}
383 createNodesProvider: function(filter)
385 return this.callFactoryMethod(null, "createNodesProvider", WebInspector.HeapSnapshotProviderProxy, filter);
389 * @param {string} className
390 * @param {!WebInspector.HeapSnapshotCommon.NodeFilter} nodeFilter
391 * @return {?WebInspector.HeapSnapshotProviderProxy}
393 createNodesProviderForClass: function(className, nodeFilter)
395 return this.callFactoryMethod(null, "createNodesProviderForClass", WebInspector.HeapSnapshotProviderProxy, className, nodeFilter);
399 * @param {number} nodeIndex
400 * @return {?WebInspector.HeapSnapshotProviderProxy}
402 createNodesProviderForDominator: function(nodeIndex)
404 return this.callFactoryMethod(null, "createNodesProviderForDominator", WebInspector.HeapSnapshotProviderProxy, nodeIndex);
407 allocationTracesTops: function(callback)
409 this.callMethod(callback, "allocationTracesTops");
413 * @param {number} nodeId
414 * @param {function(!WebInspector.HeapSnapshotCommon.AllocationNodeCallers)} callback
416 allocationNodeCallers: function(nodeId, callback)
418 this.callMethod(callback, "allocationNodeCallers", nodeId);
423 throw new Error("Should never be called");
428 return this._staticData.nodeCount;
433 return this._staticData.rootNodeIndex;
436 updateStaticData: function(callback)
439 * @param {!WebInspector.HeapSnapshotCommon.StaticData} staticData
440 * @this {WebInspector.HeapSnapshotProxy}
442 function dataReceived(staticData)
444 this._staticData = staticData;
447 this.callMethod(dataReceived.bind(this), "updateStaticData");
451 * @param {!function(!WebInspector.HeapSnapshotCommon.Statistics):void} callback
453 getStatistics: function(callback)
455 this.callMethod(callback, "getStatistics");
460 return this._staticData.totalSize;
465 return this._profileUid;
468 setProfileUid: function(profileUid)
470 this._profileUid = profileUid;
476 maxJSObjectId: function()
478 return this._staticData.maxJSObjectId;
481 __proto__: WebInspector.HeapSnapshotProxyObject.prototype
487 * @extends {WebInspector.HeapSnapshotProxyObject}
488 * @implements {WebInspector.HeapSnapshotGridNode.ChildrenProvider}
489 * @param {!WebInspector.HeapSnapshotWorkerProxy} worker
490 * @param {number} objectId
492 WebInspector.HeapSnapshotProviderProxy = function(worker, objectId)
494 WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
497 WebInspector.HeapSnapshotProviderProxy.prototype = {
500 * @param {number} snapshotObjectId
501 * @param {function(number)} callback
503 nodePosition: function(snapshotObjectId, callback)
505 this.callMethod(callback, "nodePosition", snapshotObjectId);
510 * @param {function(boolean)} callback
512 isEmpty: function(callback)
514 this.callMethod(callback, "isEmpty");
519 * @param {number} startPosition
520 * @param {number} endPosition
521 * @param {function(!WebInspector.HeapSnapshotCommon.ItemsRange)} callback
523 serializeItemsRange: function(startPosition, endPosition, callback)
525 this.callMethod(callback, "serializeItemsRange", startPosition, endPosition);
530 * @param {!WebInspector.HeapSnapshotCommon.ComparatorConfig} comparator
531 * @param {function()} callback
533 sortAndRewind: function(comparator, callback)
535 this.callMethod(callback, "sortAndRewind", comparator);
538 __proto__: WebInspector.HeapSnapshotProxyObject.prototype