diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebInspectorUI/UserInterface/Proxies | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebInspectorUI/UserInterface/Proxies')
6 files changed, 639 insertions, 0 deletions
diff --git a/Source/WebInspectorUI/UserInterface/Proxies/FormatterWorkerProxy.js b/Source/WebInspectorUI/UserInterface/Proxies/FormatterWorkerProxy.js new file mode 100644 index 000000000..96159fb87 --- /dev/null +++ b/Source/WebInspectorUI/UserInterface/Proxies/FormatterWorkerProxy.js @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +WebInspector.FormatterWorkerProxy = class FormatterWorkerProxy extends WebInspector.Object +{ + constructor() + { + super(); + + this._formatterWorker = new Worker("Workers/Formatter/FormatterWorker.js"); + this._formatterWorker.addEventListener("message", this._handleMessage.bind(this)); + + this._nextCallId = 1; + this._callbacks = new Map; + } + + // Static + + static singleton() + { + if (!FormatterWorkerProxy.instance) + FormatterWorkerProxy.instance = new FormatterWorkerProxy; + return FormatterWorkerProxy.instance; + } + + // Actions + + formatJavaScript(sourceText, isModule, indentString, includeSourceMapData) + { + this.performAction("formatJavaScript", ...arguments); + } + + // Public + + performAction(actionName) + { + let callId = this._nextCallId++; + let callback = arguments[arguments.length - 1]; + let actionArguments = Array.prototype.slice.call(arguments, 1, arguments.length - 1); + + console.assert(typeof actionName === "string", "performAction should always have an actionName"); + console.assert(typeof callback === "function", "performAction should always have a callback"); + + this._callbacks.set(callId, callback); + this._postMessage({callId, actionName, actionArguments}); + } + + // Private + + _postMessage() + { + this._formatterWorker.postMessage(...arguments); + } + + _handleMessage(event) + { + let data = event.data; + + // Action response. + if (data.callId) { + let callback = this._callbacks.get(data.callId); + this._callbacks.delete(data.callId); + callback(data.result); + return; + } + + console.error("Unexpected FormatterWorker message", data); + } +}; diff --git a/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotDiffProxy.js b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotDiffProxy.js new file mode 100644 index 000000000..4dc62ed61 --- /dev/null +++ b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotDiffProxy.js @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +WebInspector.HeapSnapshotDiffProxy = class HeapSnapshotDiffProxy extends WebInspector.Object +{ + constructor(snapshotDiffObjectId, snapshot1, snapshot2, totalSize, totalObjectCount, categories) + { + super(); + + this._proxyObjectId = snapshotDiffObjectId; + + console.assert(snapshot1 instanceof WebInspector.HeapSnapshotProxy); + console.assert(snapshot2 instanceof WebInspector.HeapSnapshotProxy); + + this._snapshot1 = snapshot1; + this._snapshot2 = snapshot2; + this._totalSize = totalSize; + this._totalObjectCount = totalObjectCount; + this._categories = Map.fromObject(categories); + } + + // Static + + static deserialize(objectId, serializedSnapshotDiff) + { + let {snapshot1: serializedSnapshot1, snapshot2: serializedSnapshot2, totalSize, totalObjectCount, categories} = serializedSnapshotDiff; + // FIXME: The objectId for these snapshots is the snapshotDiff's objectId. Currently these + // snapshots are only used for static data so the proxing doesn't matter. However, + // should we serialize the objectId with the snapshot so we have the right objectId? + let snapshot1 = WebInspector.HeapSnapshotProxy.deserialize(objectId, serializedSnapshot1); + let snapshot2 = WebInspector.HeapSnapshotProxy.deserialize(objectId, serializedSnapshot2); + return new WebInspector.HeapSnapshotDiffProxy(objectId, snapshot1, snapshot2, totalSize, totalObjectCount, categories); + } + + // Public + + get snapshot1() { return this._snapshot1; } + get snapshot2() { return this._snapshot2; } + get totalSize() { return this._totalSize; } + get totalObjectCount() { return this._totalObjectCount; } + get categories() { return this._categories; } + get invalid() { return this._snapshot1.invalid || this._snapshot2.invalid; } + + updateForCollectionEvent(event) + { + console.assert(!this.invalid); + if (!event.data.affectedSnapshots.includes(this._snapshot2._identifier)) + return; + + this.update(() => { + this.dispatchEventToListeners(WebInspector.HeapSnapshotProxy.Event.CollectedNodes, event.data); + }); + } + + allocationBucketCounts(bucketSizes, callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "allocationBucketCounts", bucketSizes, callback); + } + + instancesWithClassName(className, callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "instancesWithClassName", className, (serializedNodes) => { + callback(serializedNodes.map(WebInspector.HeapSnapshotNodeProxy.deserialize.bind(null, this._proxyObjectId))); + }); + } + + update(callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "update", ({liveSize, categories}) => { + this._categories = Map.fromObject(categories); + callback(); + }); + } + + nodeWithIdentifier(nodeIdentifier, callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "nodeWithIdentifier", nodeIdentifier, (serializedNode) => { + callback(WebInspector.HeapSnapshotNodeProxy.deserialize(this._proxyObjectId, serializedNode)); + }); + } +}; diff --git a/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotEdgeProxy.js b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotEdgeProxy.js new file mode 100644 index 000000000..e4d44b176 --- /dev/null +++ b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotEdgeProxy.js @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +// Directed edge between two HeapSnapshotNodes 'from' and 'to'. + +WebInspector.HeapSnapshotEdgeProxy = class HeapSnapshotEdgeProxy +{ + constructor(objectId, fromIdentifier, toIdentifier, type, data) + { + this._proxyObjectId = objectId; + + console.assert(type in WebInspector.HeapSnapshotEdgeProxy.EdgeType); + + this.fromIdentifier = fromIdentifier; + this.toIdentifier = toIdentifier; + this.type = type; + this.data = data; + + this.from = null; + this.to = null; + } + + isPrivateSymbol() + { + if (WebInspector.isDebugUIEnabled()) + return false; + + return typeof this.data === "string" && this.data.startsWith("PrivateSymbol"); + } + + // Static + + static deserialize(objectId, serializedEdge) + { + let {from, to, type, data} = serializedEdge; + return new WebInspector.HeapSnapshotEdgeProxy(objectId, from, to, type, data); + } +}; + +WebInspector.HeapSnapshotEdgeProxy.EdgeType = { + Internal: "Internal", // No data. + Property: "Property", // data is string property name. + Index: "Index", // data is numeric index. + Variable: "Variable", // data is string variable name. +}; diff --git a/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotNodeProxy.js b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotNodeProxy.js new file mode 100644 index 000000000..908400d0e --- /dev/null +++ b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotNodeProxy.js @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +WebInspector.HeapSnapshotNodeProxy = class HeapSnapshotNodeProxy +{ + constructor(snapshotObjectId, identifier, className, size, retainedSize, internal, gcRoot, dead, dominatorNodeIdentifier, hasChildren) + { + this._proxyObjectId = snapshotObjectId; + + this.id = identifier; + this.className = className; + this.size = size; + this.retainedSize = retainedSize; + this.internal = internal; + this.gcRoot = gcRoot; + this.dead = dead; + this.dominatorNodeIdentifier = dominatorNodeIdentifier; + this.hasChildren = hasChildren; + } + + // Static + + static deserialize(objectId, serializedNode) + { + let {id, className, size, retainedSize, internal, gcRoot, dead, dominatorNodeIdentifier, hasChildren} = serializedNode; + return new WebInspector.HeapSnapshotNodeProxy(objectId, id, className, size, retainedSize, internal, gcRoot, dead, dominatorNodeIdentifier, hasChildren); + } + + // Proxied + + shortestGCRootPath(callback) + { + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "shortestGCRootPath", this.id, (serializedPath) => { + let isNode = false; + let path = serializedPath.map((component) => { + isNode = !isNode; + if (isNode) + return WebInspector.HeapSnapshotNodeProxy.deserialize(this._proxyObjectId, component); + return WebInspector.HeapSnapshotEdgeProxy.deserialize(this._proxyObjectId, component); + }); + + for (let i = 1; i < path.length; i += 2) { + console.assert(path[i] instanceof WebInspector.HeapSnapshotEdgeProxy); + let edge = path[i]; + edge.from = path[i - 1]; + edge.to = path[i + 1]; + } + + callback(path); + }); + } + + dominatedNodes(callback) + { + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "dominatedNodes", this.id, (serializedNodes) => { + callback(serializedNodes.map(WebInspector.HeapSnapshotNodeProxy.deserialize.bind(null, this._proxyObjectId))); + }); + } + + retainedNodes(callback) + { + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "retainedNodes", this.id, ({retainedNodes: serializedNodes, edges: serializedEdges}) => { + let deserializedNodes = serializedNodes.map(WebInspector.HeapSnapshotNodeProxy.deserialize.bind(null, this._proxyObjectId)); + let deserializedEdges = serializedEdges.map(WebInspector.HeapSnapshotEdgeProxy.deserialize.bind(null, this._proxyObjectId)); + callback(deserializedNodes, deserializedEdges); + }); + } + + retainers(callback) + { + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "retainers", this.id, ({retainers: serializedNodes, edges: serializedEdges}) => { + let deserializedNodes = serializedNodes.map(WebInspector.HeapSnapshotNodeProxy.deserialize.bind(null, this._proxyObjectId)); + let deserializedEdges = serializedEdges.map(WebInspector.HeapSnapshotEdgeProxy.deserialize.bind(null, this._proxyObjectId)); + callback(deserializedNodes, deserializedEdges); + }); + } +}; diff --git a/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotProxy.js b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotProxy.js new file mode 100644 index 000000000..1c8ed63a7 --- /dev/null +++ b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotProxy.js @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +WebInspector.HeapSnapshotProxy = class HeapSnapshotProxy extends WebInspector.Object +{ + constructor(snapshotObjectId, identifier, title, totalSize, totalObjectCount, liveSize, categories) + { + super(); + + this._proxyObjectId = snapshotObjectId; + + this._identifier = identifier; + this._title = title; + this._totalSize = totalSize; + this._totalObjectCount = totalObjectCount; + this._liveSize = liveSize; + this._categories = Map.fromObject(categories); + + console.assert(!this.invalid); + + if (!WebInspector.HeapSnapshotProxy.ValidSnapshotProxies) + WebInspector.HeapSnapshotProxy.ValidSnapshotProxies = []; + WebInspector.HeapSnapshotProxy.ValidSnapshotProxies.push(this); + } + + // Static + + static deserialize(objectId, serializedSnapshot) + { + let {identifier, title, totalSize, totalObjectCount, liveSize, categories} = serializedSnapshot; + return new WebInspector.HeapSnapshotProxy(objectId, identifier, title, totalSize, totalObjectCount, liveSize, categories); + } + + static invalidateSnapshotProxies() + { + if (!WebInspector.HeapSnapshotProxy.ValidSnapshotProxies) + return; + + for (let snapshotProxy of WebInspector.HeapSnapshotProxy.ValidSnapshotProxies) + snapshotProxy._invalidate(); + + WebInspector.HeapSnapshotProxy.ValidSnapshotProxies = null; + } + + // Public + + get proxyObjectId() { return this._proxyObjectId; } + get identifier() { return this._identifier; } + get title() { return this._title; } + get totalSize() { return this._totalSize; } + get totalObjectCount() { return this._totalObjectCount; } + get liveSize() { return this._liveSize; } + get categories() { return this._categories; } + get invalid() { return this._proxyObjectId === 0; } + + updateForCollectionEvent(event) + { + console.assert(!this.invalid); + if (!event.data.affectedSnapshots.includes(this._identifier)) + return; + + this.update(() => { + this.dispatchEventToListeners(WebInspector.HeapSnapshotProxy.Event.CollectedNodes, event.data); + }); + } + + allocationBucketCounts(bucketSizes, callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "allocationBucketCounts", bucketSizes, callback); + } + + instancesWithClassName(className, callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "instancesWithClassName", className, (serializedNodes) => { + callback(serializedNodes.map(WebInspector.HeapSnapshotNodeProxy.deserialize.bind(null, this._proxyObjectId))); + }); + } + + update(callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "update", ({liveSize, categories}) => { + this._liveSize = liveSize; + this._categories = Map.fromObject(categories); + callback(); + }); + } + + nodeWithIdentifier(nodeIdentifier, callback) + { + console.assert(!this.invalid); + WebInspector.HeapSnapshotWorkerProxy.singleton().callMethod(this._proxyObjectId, "nodeWithIdentifier", nodeIdentifier, (serializedNode) => { + callback(WebInspector.HeapSnapshotNodeProxy.deserialize(this._proxyObjectId, serializedNode)); + }); + } + + // Private + + _invalidate() + { + this._proxyObjectId = 0; + this._liveSize = 0; + + this.dispatchEventToListeners(WebInspector.HeapSnapshotProxy.Event.Invalidated); + } +}; + +WebInspector.HeapSnapshotProxy.Event = { + CollectedNodes: "heap-snapshot-proxy-collected-nodes", + Invalidated: "heap-snapshot-proxy-invalidated", +}; diff --git a/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotWorkerProxy.js b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotWorkerProxy.js new file mode 100644 index 000000000..c47993c5e --- /dev/null +++ b/Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotWorkerProxy.js @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +WebInspector.HeapSnapshotWorkerProxy = class HeapSnapshotWorkerProxy extends WebInspector.Object +{ + constructor() + { + super(); + + this._heapSnapshotWorker = new Worker("Workers/HeapSnapshot/HeapSnapshotWorker.js"); + this._heapSnapshotWorker.addEventListener("message", this._handleMessage.bind(this)); + + this._nextCallId = 1; + this._callbacks = new Map; + + WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this); + } + + // Static + + static singleton() + { + if (!HeapSnapshotWorkerProxy.instance) + HeapSnapshotWorkerProxy.instance = new HeapSnapshotWorkerProxy; + return HeapSnapshotWorkerProxy.instance; + } + + // Actions + + clearSnapshots(callback) + { + this.performAction("clearSnapshots", callback); + } + + createSnapshot(snapshotStringData, callback) + { + this.performAction("createSnapshot", ...arguments); + } + + createSnapshotDiff(objectId1, objectId2, callback) + { + this.performAction("createSnapshotDiff", ...arguments); + } + + // Public + + performAction(actionName) + { + let callId = this._nextCallId++; + let callback = arguments[arguments.length - 1]; + let actionArguments = Array.prototype.slice.call(arguments, 1, arguments.length - 1); + + console.assert(typeof actionName === "string", "performAction should always have an actionName"); + console.assert(typeof callback === "function", "performAction should always have a callback"); + + this._callbacks.set(callId, callback); + this._postMessage({callId, actionName, actionArguments}); + } + + callMethod(objectId, methodName) + { + let callId = this._nextCallId++; + let callback = arguments[arguments.length - 1]; + let methodArguments = Array.prototype.slice.call(arguments, 2, arguments.length - 1); + + console.assert(typeof objectId === "number", "callMethod should always have an objectId"); + console.assert(typeof methodName === "string", "callMethod should always have a methodName"); + console.assert(typeof callback === "function", "callMethod should always have a callback"); + + this._callbacks.set(callId, callback); + this._postMessage({callId, objectId, methodName, methodArguments}); + } + + // Private + + _mainResourceDidChange(event) + { + if (!event.target.isMainFrame()) + return; + + this.clearSnapshots(() => { + WebInspector.HeapSnapshotProxy.invalidateSnapshotProxies(); + }); + } + + _postMessage() + { + this._heapSnapshotWorker.postMessage(...arguments); + } + + _handleMessage(event) + { + let data = event.data; + + // Error. + if (data.error) { + console.assert(data.callId); + this._callbacks.delete(data.callId); + return; + } + + // Event. + if (data.eventName) { + this.dispatchEventToListeners(data.eventName, data.eventData); + return; + } + + // Action or Method Response. + if (data.callId) { + let callback = this._callbacks.get(data.callId); + this._callbacks.delete(data.callId); + callback(data.result); + return; + } + + console.error("Unexpected HeapSnapshotWorker message", data); + } +}; |