Index: Source/devtools/front_end/sdk/RemoteObjectUtils.js |
diff --git a/Source/devtools/front_end/sdk/RemoteObjectUtils.js b/Source/devtools/front_end/sdk/RemoteObjectUtils.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5865ab0ecd8c4155cc5411ab53d76c03344852df |
--- /dev/null |
+++ b/Source/devtools/front_end/sdk/RemoteObjectUtils.js |
@@ -0,0 +1,248 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+/** |
+ * @param {*} data |
+ * @param {*} key |
+ * @param {T} value |
+ * @return {T} |
+ * @template T |
+ */ |
+WebInspector.storeTo = function(data, key, value) |
pfeldman
2015/08/27 01:21:32
This should go to utilities.js as a separate patch
|
+{ |
+ data[key] = value; |
+ return value; |
+} |
+ |
+/** |
+ * @param {!WebInspector.CallFunctionResult} result |
+ * @return {!Promise<!WebInspector.RemoteObject>} |
+ */ |
+WebInspector.assertCallFunctionResult = function(result) |
pfeldman
2015/08/27 01:21:32
This seems too specific and needs to be next to th
|
+{ |
+ if (result.wasThrown || !result.object) |
+ return Promise.reject(new Error("Function was thrown")); |
+ return Promise.resolve(result.object); |
+} |
+ |
+/** |
+ * @suppressReceiverCheck |
+ * @return {T} |
+ * @this {T} |
+ * @template T |
+ */ |
+WebInspector.identity = function() |
pfeldman
2015/08/27 01:21:32
This is weird!
|
+{ |
+ return this; |
+} |
+ |
+/** |
+ * @constructor |
+ * @param {!WebInspector.RemoteObject} object |
+ */ |
+WebInspector.RemoteArray = function(object) |
pfeldman
2015/08/27 01:21:32
These could go into RemoteObject.js, lets land it
|
+{ |
+ this._object = object; |
+} |
+ |
+/** |
+ * @param {?WebInspector.RemoteObject} object |
+ * @return {!Promise<!WebInspector.RemoteArray>} |
+ */ |
+WebInspector.RemoteArray.createPromise = function(object) |
pfeldman
2015/08/27 01:21:32
This does not need to be a promise, there is no as
|
+{ |
+ if (!object || object.subtype !== "array") |
+ return Promise.reject(new Error("Object is empty or not an array")); |
+ return Promise.resolve(new WebInspector.RemoteArray(object)); |
+} |
+ |
+WebInspector.RemoteArray.prototype = { |
+ /** |
+ * @param {number} idx |
+ * @return {!Promise<!WebInspector.CallFunctionResult>} |
+ */ |
+ at: function(idx) |
pfeldman
2015/08/27 01:21:32
no abbreviations in Blink.
|
+ { |
+ return this._object.callFunctionPromise(at, [WebInspector.RemoteObject.toCallArgument(idx)]); |
+ |
+ /** |
+ * @suppressReceiverCheck |
+ * @param {number} idx |
+ * @return {*} |
+ * @this {!Object} |
+ */ |
+ function at(idx) |
+ { |
+ return this[idx]; |
+ } |
+ }, |
+ |
+ /** |
+ * @return {number} |
+ */ |
+ length: function() |
+ { |
+ return this._object.arrayLength(); |
+ } |
+} |
+ |
+/** |
+ * @constructor |
+ * @param {!WebInspector.RemoteObject} object |
+ */ |
+WebInspector.RemoteFunction = function(object) |
+{ |
+ this._object = object; |
+} |
+ |
+/** |
+ * @param {?WebInspector.RemoteObject} object |
+ * @return {!Promise<!WebInspector.RemoteFunction>} |
+ */ |
+WebInspector.RemoteFunction.createPromise = function(object) |
+{ |
+ if (!object || object.type !== "function") |
+ return Promise.reject(new Error("Object is empty or not a function")); |
+ return Promise.resolve(new WebInspector.RemoteFunction(object)); |
pfeldman
2015/08/27 01:21:32
ditto
|
+} |
+ |
+WebInspector.RemoteFunction.prototype = { |
+ /** |
+ * @return {!Promise<!WebInspector.RemoteObject>} |
+ */ |
+ targetFunction: function() |
+ { |
+ return this._object.getOwnPropertiesPromise().then(findTargetFunction.bind(this)); |
+ |
+ /** |
+ * @param {!{properties: ?Array<!WebInspector.RemoteObjectProperty>, internalProperties: ?Array<!WebInspector.RemoteObjectProperty>}} ownProperties |
+ * @return {!WebInspector.RemoteObject} |
+ * @this {WebInspector.RemoteFunction} |
+ */ |
+ function findTargetFunction(ownProperties) |
+ { |
+ if (!ownProperties.internalProperties) |
+ return this._object; |
+ var internalProperties = ownProperties.internalProperties; |
+ for (var i = 0; i < internalProperties.length; ++i) { |
+ if (internalProperties[i].name === "[[TargetFunction]]") |
+ return internalProperties[i].value; |
+ } |
+ return this._object; |
+ } |
+ } |
+} |
+ |
+/** |
+ * @constructor |
+ * @param {!WebInspector.RemoteObject} object |
+ */ |
+WebInspector.RemoteSet = function(object) |
pfeldman
2015/08/27 01:21:32
Why do you need a writable wrapper like this?
|
+{ |
+ this._object = object; |
+} |
+ |
+/** |
+ * @param {!Array<!WebInspector.RemoteObject>} array |
+ * @return {!Promise<!WebInspector.RemoteSet>} |
+ * @this {WebInspector.RemoteObject} |
+ */ |
+WebInspector.RemoteSet.createFromArrayPromise = function(array) |
+{ |
+ return WebInspector.RemoteSet.createEmptyPromise.call(this).then(addArray); |
+ /** |
+ * @param {!WebInspector.RemoteSet} remoteSet |
+ * @return {!Promise<!WebInspector.RemoteSet>} |
+ */ |
+ function addArray(remoteSet) |
+ { |
+ var promise = Promise.resolve(remoteSet); |
+ for (var i = 0; i < array.length; ++i) |
+ promise = promise.then(WebInspector.RemoteSet.prototype.add.bind(remoteSet, array[i])); |
+ return promise; |
+ } |
+} |
+ |
+/** |
+ * @return {!Promise<!WebInspector.RemoteSet>} |
+ * @this {WebInspector.RemoteObject} |
+ */ |
+WebInspector.RemoteSet.createEmptyPromise = function() |
+{ |
+ return this.callFunctionPromise(emptySet, undefined) |
+ .then(WebInspector.assertCallFunctionResult) |
+ .then(createRemoteSet); |
+ |
+ /** |
+ * @return {!Set} |
+ */ |
+ function emptySet() |
+ { |
+ return new Set(); |
+ } |
+ |
+ /** |
+ * @param {!WebInspector.RemoteObject} object |
+ * @return {!WebInspector.RemoteSet} |
+ */ |
+ function createRemoteSet(object) |
+ { |
+ return new WebInspector.RemoteSet(object); |
+ } |
+} |
+ |
+WebInspector.RemoteSet.prototype = |
+{ |
+ /** |
+ * @param {!WebInspector.RemoteObject} object |
+ * @return {!Promise<!WebInspector.RemoteSet>} |
+ */ |
+ add: function(object) |
+ { |
+ return this._object.callFunctionPromise(add, [WebInspector.RemoteObject.toCallArgument(object)]) |
+ .then(returnThis.bind(this)); |
+ |
+ /** |
+ * @suppressReceiverCheck |
+ * @param {!Object} object |
+ * @return {!Set} |
+ * @this {Set} |
+ */ |
+ function add(object) |
+ { |
+ this.add(object); |
+ return this; |
+ } |
+ |
+ /** |
+ * @return {!T} |
+ * @this {T} |
+ * @template T |
+ */ |
+ function returnThis() |
+ { |
+ return this; |
+ } |
+ }, |
+ |
+ /** |
+ * @param {!WebInspector.RemoteObject} object |
+ * @return {!Promise<boolean>} |
+ */ |
+ has: function(object) |
+ { |
+ return /** @type {!Promise<boolean>} */(this._object.callFunctionJSONPromise(has, [WebInspector.RemoteObject.toCallArgument(object)])); |
+ |
+ /** |
+ * @suppressReceiverCheck |
+ * @param {!Object} object |
+ * @return {boolean} |
+ * @this {Set} |
+ */ |
+ function has(object) |
+ { |
+ return this.has(object); |
+ } |
+ } |
+} |