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});
80 callGetter: function(callback, objectId, getterName)
82 var callId = this._nextCallId++;
83 this._callbacks[callId] = callback;
84 this._postMessage({callId: callId, disposition: "getter", objectId: objectId, methodName: getterName});
88 * @param {?function(...[?])} callback
89 * @param {string} objectId
90 * @param {string} methodName
91 * @param {function(new:T, ...[?])} proxyConstructor
95 callFactoryMethod: function(callback, objectId, methodName, proxyConstructor)
97 var callId = this._nextCallId++;
98 var methodArguments = Array.prototype.slice.call(arguments, 4);
99 var newObjectId = this._nextObjectId++;
102 * @this {WebInspector.HeapSnapshotWorkerProxy}
104 function wrapCallback(remoteResult)
106 callback(remoteResult ? new proxyConstructor(this, newObjectId) : null);
110 this._callbacks[callId] = wrapCallback.bind(this);
111 this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
114 this._postMessage({callId: callId, disposition: "factory", objectId: objectId, methodName: methodName, methodArguments: methodArguments, newObjectId: newObjectId});
115 return new proxyConstructor(this, newObjectId);
120 * @param {function(*)} callback
121 * @param {string} objectId
122 * @param {string} methodName
124 callMethod: function(callback, objectId, methodName)
126 var callId = this._nextCallId++;
127 var methodArguments = Array.prototype.slice.call(arguments, 3);
129 this._callbacks[callId] = callback;
130 this._postMessage({callId: callId, disposition: "method", objectId: objectId, methodName: methodName, methodArguments: methodArguments});
133 startCheckingForLongRunningCalls: function()
137 this._checkLongRunningCalls();
138 this._interval = setInterval(this._checkLongRunningCalls.bind(this), 300);
141 _checkLongRunningCalls: function()
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;
151 this.dispatchEventToListeners("wait", hasLongRunningCalls);
152 for (callId in this._callbacks)
153 this._previousCallbacks[callId] = true;
156 _findFunction: function(name)
158 var path = name.split(".");
160 for (var i = 0; i < path.length; ++i)
161 result = result[path[i]];
166 * @param {!MessageEvent} event
168 _messageReceived: function(event)
170 var data = event.data;
171 if (data.eventName) {
172 if (this._eventHandler)
173 this._eventHandler(data.eventName, data.data);
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];
183 if (!this._callbacks[data.callId])
185 var callback = this._callbacks[data.callId];
186 delete this._callbacks[data.callId];
187 callback(data.result);
190 _postMessage: function(message)
192 this._worker.postMessage(message);
195 __proto__: WebInspector.Object.prototype
201 * @param {number} objectId
203 WebInspector.HeapSnapshotProxyObject = function(worker, objectId)
205 this._worker = worker;
206 this._objectId = objectId;
209 WebInspector.HeapSnapshotProxyObject.prototype = {
211 * @param {string} workerMethodName
212 * @param {!Array.<*>} args
214 _callWorker: function(workerMethodName, args)
216 args.splice(1, 0, this._objectId);
217 return this._worker[workerMethodName].apply(this._worker, args);
222 this._worker.disposeObject(this._objectId);
225 disposeWorker: function()
227 this._worker.dispose();
231 * @param {?function(...[?])} callback
232 * @param {string} methodName
233 * @param {function (new:T, ...[?])} proxyConstructor
234 * @param {...*} var_args
235 * @return {?WebInspector.HeapSnapshotProviderProxy}
238 callFactoryMethod: function(callback, methodName, proxyConstructor, var_args)
240 return this._callWorker("callFactoryMethod", Array.prototype.slice.call(arguments, 0));
244 * @param {function(T)|undefined} callback
245 * @param {string} getterName
249 callGetter: function(callback, getterName)
251 return this._callWorker("callGetter", Array.prototype.slice.call(arguments, 0));
255 * @param {function(T)|undefined} callback
256 * @param {string} methodName
257 * @param {...*} var_args
261 callMethod: function(callback, methodName, var_args)
263 return this._callWorker("callMethod", Array.prototype.slice.call(arguments, 0));
273 * @extends {WebInspector.HeapSnapshotProxyObject}
274 * @implements {WebInspector.OutputStream}
275 * @param {number} objectId
276 * @param {number} profileUid
277 * @param {function(!WebInspector.HeapSnapshotProxy)} snapshotReceivedCallback
279 WebInspector.HeapSnapshotLoaderProxy = function(worker, objectId, profileUid, snapshotReceivedCallback)
281 WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
282 this._profileUid = profileUid;
283 this._snapshotReceivedCallback = snapshotReceivedCallback;
286 WebInspector.HeapSnapshotLoaderProxy.prototype = {
288 * @param {string} chunk
289 * @param {function(!WebInspector.OutputStream)=} callback
291 write: function(chunk, callback)
293 this.callMethod(callback, "write", chunk);
297 * @param {function()=} callback
299 close: function(callback)
302 * @this {WebInspector.HeapSnapshotLoaderProxy}
304 function buildSnapshot()
308 this.callFactoryMethod(updateStaticData.bind(this), "buildSnapshot", WebInspector.HeapSnapshotProxy);
312 * @param {!WebInspector.HeapSnapshotProxy} snapshotProxy
313 * @this {WebInspector.HeapSnapshotLoaderProxy}
315 function updateStaticData(snapshotProxy)
318 snapshotProxy.setProfileUid(this._profileUid);
319 snapshotProxy.updateStaticData(this._snapshotReceivedCallback.bind(this));
322 this.callMethod(buildSnapshot.bind(this), "close");
325 __proto__: WebInspector.HeapSnapshotProxyObject.prototype
331 * @extends {WebInspector.HeapSnapshotProxyObject}
332 * @param {!WebInspector.HeapSnapshotWorkerProxy} worker
333 * @param {number} objectId
335 WebInspector.HeapSnapshotProxy = function(worker, objectId)
337 WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
338 /** @type {?WebInspector.HeapSnapshotCommon.StaticData} */
339 this._staticData = null;
342 WebInspector.HeapSnapshotProxy.prototype = {
343 aggregates: function(sortedIndexes, key, filter, callback)
345 this.callMethod(callback, "aggregates", sortedIndexes, key, filter);
348 aggregatesForDiff: function(callback)
350 this.callMethod(callback, "aggregatesForDiff");
353 calculateSnapshotDiff: function(baseSnapshotId, baseSnapshotAggregates, callback)
355 this.callMethod(callback, "calculateSnapshotDiff", baseSnapshotId, baseSnapshotAggregates);
358 nodeClassName: function(snapshotObjectId, callback)
360 this.callMethod(callback, "nodeClassName", snapshotObjectId);
363 dominatorIdsForNode: function(nodeIndex, callback)
365 this.callMethod(callback, "dominatorIdsForNode", nodeIndex);
369 * @param {number} nodeIndex
370 * @param {boolean} showHiddenData
371 * @return {?WebInspector.HeapSnapshotProviderProxy}
373 createEdgesProvider: function(nodeIndex, showHiddenData)
375 return this.callFactoryMethod(null, "createEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex, showHiddenData);
379 * @param {number} nodeIndex
380 * @param {boolean} showHiddenData
381 * @return {?WebInspector.HeapSnapshotProviderProxy}
383 createRetainingEdgesProvider: function(nodeIndex, showHiddenData)
385 return this.callFactoryMethod(null, "createRetainingEdgesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndex, showHiddenData);
389 * @param {string} baseSnapshotId
390 * @param {string} className
391 * @return {?WebInspector.HeapSnapshotProviderProxy}
393 createAddedNodesProvider: function(baseSnapshotId, className)
395 return this.callFactoryMethod(null, "createAddedNodesProvider", WebInspector.HeapSnapshotProviderProxy, baseSnapshotId, className);
399 * @param {!Array.<number>} nodeIndexes
400 * @return {?WebInspector.HeapSnapshotProviderProxy}
402 createDeletedNodesProvider: function(nodeIndexes)
404 return this.callFactoryMethod(null, "createDeletedNodesProvider", WebInspector.HeapSnapshotProviderProxy, nodeIndexes);
408 * @param {function(*):boolean} filter
409 * @return {?WebInspector.HeapSnapshotProviderProxy}
411 createNodesProvider: function(filter)
413 return this.callFactoryMethod(null, "createNodesProvider", WebInspector.HeapSnapshotProviderProxy, filter);
417 * @param {string} className
418 * @param {string} aggregatesKey
419 * @return {?WebInspector.HeapSnapshotProviderProxy}
421 createNodesProviderForClass: function(className, aggregatesKey)
423 return this.callFactoryMethod(null, "createNodesProviderForClass", WebInspector.HeapSnapshotProviderProxy, className, aggregatesKey);
427 * @param {number} nodeIndex
428 * @return {?WebInspector.HeapSnapshotProviderProxy}
430 createNodesProviderForDominator: function(nodeIndex)
432 return this.callFactoryMethod(null, "createNodesProviderForDominator", WebInspector.HeapSnapshotProviderProxy, nodeIndex);
435 allocationTracesTops: function(callback)
437 this.callMethod(callback, "allocationTracesTops");
440 allocationNodeCallers: function(nodeId, callback)
442 this.callMethod(callback, "allocationNodeCallers", nodeId);
447 throw new Error("Should never be called");
452 return this._staticData.nodeCount;
457 return this._staticData.rootNodeIndex;
460 updateStaticData: function(callback)
463 * @param {!WebInspector.HeapSnapshotCommon.StaticData} staticData
464 * @this {WebInspector.HeapSnapshotProxy}
466 function dataReceived(staticData)
468 this._staticData = staticData;
471 this.callMethod(dataReceived.bind(this), "updateStaticData");
476 return this._staticData.totalSize;
481 return this._profileUid;
484 setProfileUid: function(profileUid)
486 this._profileUid = profileUid;
492 maxJSObjectId: function()
494 return this._staticData.maxJSObjectId;
497 __proto__: WebInspector.HeapSnapshotProxyObject.prototype
503 * @extends {WebInspector.HeapSnapshotProxyObject}
504 * @param {!WebInspector.HeapSnapshotWorkerProxy} worker
505 * @param {number} objectId
507 WebInspector.HeapSnapshotProviderProxy = function(worker, objectId)
509 WebInspector.HeapSnapshotProxyObject.call(this, worker, objectId);
512 WebInspector.HeapSnapshotProviderProxy.prototype = {
513 nodePosition: function(snapshotObjectId, callback)
515 this.callMethod(callback, "nodePosition", snapshotObjectId);
518 isEmpty: function(callback)
520 this.callMethod(callback, "isEmpty");
524 * @param {number} startPosition
525 * @param {number} endPosition
526 * @param {function(!WebInspector.HeapSnapshotCommon.ItemsRange)} callback
528 serializeItemsRange: function(startPosition, endPosition, callback)
530 this.callMethod(callback, "serializeItemsRange", startPosition, endPosition);
533 sortAndRewind: function(comparator, callback)
535 this.callMethod(callback, "sortAndRewind", comparator);
538 __proto__: WebInspector.HeapSnapshotProxyObject.prototype