WebInspector.RemoteObject = function(objectId, type, subtype, value, description, preview, scopeRef)
{
this._type = type;
this._subtype = subtype;
if (objectId) {
this._objectId = objectId;
this._description = description;
this._hasChildren = true;
this._preview = preview;
} else {
console.assert(type !== "object" || value === null);
this._description = description || (value + "");
this._hasChildren = false;
this.value = value;
}
this._scopeRef = scopeRef;
}
WebInspector.RemoteObject.fromPrimitiveValue = function(value)
{
return new WebInspector.RemoteObject(undefined, typeof value, undefined, value);
}
WebInspector.RemoteObject.fromLocalObject = function(value)
{
return new WebInspector.LocalJSONObject(value);
}
WebInspector.RemoteObject.resolveNode = function(node, objectGroup, callback)
{
function mycallback(error, object)
{
if (!callback)
return;
if (error || !object)
callback(null);
else
callback(WebInspector.RemoteObject.fromPayload(object));
}
DOMAgent.resolveNode(node.id, objectGroup, mycallback);
}
WebInspector.RemoteObject.fromPayload = function(payload)
{
console.assert(typeof payload === "object", "Remote object payload should only be an object");
return new WebInspector.RemoteObject(payload.objectId, payload.type, payload.subtype, payload.value, payload.description, payload.preview);
}
WebInspector.RemoteObject.fromScopePayload = function(payload, scopeRef)
{
return new WebInspector.RemoteObject(payload.objectId, payload.type, payload.subtype, payload.value, payload.description, payload.preview, scopeRef);
}
WebInspector.RemoteObject.type = function(remoteObject)
{
if (remoteObject === null)
return "null";
var type = typeof remoteObject;
if (type !== "object" && type !== "function")
return type;
return remoteObject.type;
}
WebInspector.RemoteObject.prototype = {
get objectId()
{
return this._objectId;
},
get type()
{
return this._type;
},
get subtype()
{
return this._subtype;
},
get description()
{
return this._description;
},
get hasChildren()
{
return this._hasChildren;
},
get preview()
{
return this._preview;
},
getOwnProperties: function(callback)
{
this._getProperties(true, callback);
},
getAllProperties: function(callback)
{
this._getProperties(false, callback);
},
_getProperties: function(ownProperties, callback)
{
if (!this._objectId) {
callback([]);
return;
}
function remoteObjectBinder(error, properties, internalProperties)
{
if (error) {
callback(null);
return;
}
var result = [];
for (var i = 0; properties && i < properties.length; ++i) {
var property = properties[i];
if (property.get || property.set) {
if (property.get)
result.push(new WebInspector.RemoteObjectProperty("get " + property.name, WebInspector.RemoteObject.fromPayload(property.get), property));
if (property.set)
result.push(new WebInspector.RemoteObjectProperty("set " + property.name, WebInspector.RemoteObject.fromPayload(property.set), property));
} else
result.push(new WebInspector.RemoteObjectProperty(property.name, WebInspector.RemoteObject.fromPayload(property.value), property));
}
var internalPropertiesResult;
if (internalProperties) {
internalPropertiesResult = [];
for (var i = 0; i < internalProperties.length; i++) {
var property = internalProperties[i];
internalPropertiesResult.push(new WebInspector.RemoteObjectProperty(property.name, WebInspector.RemoteObject.fromPayload(property.value)));
}
}
callback(result, internalPropertiesResult);
}
RuntimeAgent.getProperties(this._objectId, ownProperties, remoteObjectBinder);
},
setPropertyValue: function(name, value, callback)
{
if (!this._objectId) {
callback("Can't set a property of non-object.");
return;
}
RuntimeAgent.evaluate.invoke({expression:value, doNotPauseOnExceptionsAndMuteConsole:true}, evaluatedCallback.bind(this));
function evaluatedCallback(error, result, wasThrown)
{
if (error || wasThrown) {
callback(error || result.description);
return;
}
if (this._scopeRef)
this._setDeclarativeVariableValue(result, name, callback);
else
this._setObjectPropertyValue(result, name, callback);
if (result._objectId)
RuntimeAgent.releaseObject(result._objectId);
}
},
_setObjectPropertyValue: function(result, name, callback)
{
var setPropertyValueFunction = "function(a, b) { this[a] = b; }";
if (result.type === "number" && typeof result.value !== "number")
setPropertyValueFunction = "function(a) { this[a] = " + result.description + "; }";
delete result.description; RuntimeAgent.callFunctionOn(this._objectId, setPropertyValueFunction, [{ value:name }, result], true, undefined, undefined, propertySetCallback.bind(this));
function propertySetCallback(error, result, wasThrown)
{
if (error || wasThrown) {
callback(error || result.description);
return;
}
callback();
}
},
_setDeclarativeVariableValue: function(result, name, callback)
{
var newValue;
switch (result.type) {
case "undefined":
newValue = {};
break;
case "object":
case "function":
newValue = { objectId: result.objectId };
break;
default:
newValue = { value: result.value };
}
DebuggerAgent.setVariableValue(this._scopeRef.number, name, newValue, this._scopeRef.callFrameId, this._scopeRef.functionId, setVariableValueCallback.bind(this));
function setVariableValueCallback(error)
{
if (error) {
callback(error);
return;
}
callback();
}
},
pushNodeToFrontend: function(callback)
{
if (this._objectId)
WebInspector.domAgent.pushNodeToFrontend(this._objectId, callback);
else
callback(0);
},
highlightAsDOMNode: function()
{
WebInspector.domAgent.highlightDOMNode(undefined, undefined, this._objectId);
},
hideDOMNodeHighlight: function()
{
WebInspector.domAgent.hideDOMNodeHighlight();
},
callFunction: function(functionDeclaration, args, callback)
{
function mycallback(error, result, wasThrown)
{
if (!callback)
return;
callback((error || wasThrown) ? null : WebInspector.RemoteObject.fromPayload(result));
}
RuntimeAgent.callFunctionOn(this._objectId, functionDeclaration.toString(), args, true, undefined, undefined, mycallback);
},
callFunctionJSON: function(functionDeclaration, args, callback)
{
function mycallback(error, result, wasThrown)
{
callback((error || wasThrown) ? null : result.value);
}
RuntimeAgent.callFunctionOn(this._objectId, functionDeclaration.toString(), args, true, true, false, mycallback);
},
release: function()
{
if (!this._objectId)
return;
RuntimeAgent.releaseObject(this._objectId);
},
arrayLength: function()
{
if (this.subtype !== "array")
return 0;
var matches = this._description.match(/\[([0-9]+)\]/);
if (!matches)
return 0;
return parseInt(matches[1], 10);
}
}
WebInspector.ScopeRef = function(number, callFrameId, functionId)
{
this.number = number;
this.callFrameId = callFrameId;
this.functionId = functionId;
}
WebInspector.RemoteObjectProperty = function(name, value, descriptor)
{
this.name = name;
this.value = value;
this.enumerable = descriptor ? !!descriptor.enumerable : true;
this.writable = descriptor ? !!descriptor.writable : true;
if (descriptor && descriptor.wasThrown)
this.wasThrown = true;
}
WebInspector.RemoteObjectProperty.fromPrimitiveValue = function(name, value)
{
return new WebInspector.RemoteObjectProperty(name, WebInspector.RemoteObject.fromPrimitiveValue(value));
}
WebInspector.RemoteObjectProperty.fromScopeValue = function(name, value)
{
var result = new WebInspector.RemoteObjectProperty(name, value);
result.writable = false;
return result;
}
WebInspector.LocalJSONObject = function(value)
{
this._value = value;
}
WebInspector.LocalJSONObject.prototype = {
get description()
{
if (this._cachedDescription)
return this._cachedDescription;
if (this.type === "object") {
switch (this.subtype) {
case "array":
function formatArrayItem(property)
{
return property.value.description;
}
this._cachedDescription = this._concatenate("[", "]", formatArrayItem);
break;
case "date":
this._cachedDescription = "" + this._value;
break;
case "null":
this._cachedDescription = "null";
break;
default:
function formatObjectItem(property)
{
return property.name + ":" + property.value.description;
}
this._cachedDescription = this._concatenate("{", "}", formatObjectItem);
}
} else
this._cachedDescription = String(this._value);
return this._cachedDescription;
},
_concatenate: function(prefix, suffix, formatProperty)
{
const previewChars = 100;
var buffer = prefix;
var children = this._children();
for (var i = 0; i < children.length; ++i) {
var itemDescription = formatProperty(children[i]);
if (buffer.length + itemDescription.length > previewChars) {
buffer += ",\u2026";
break;
}
if (i)
buffer += ", ";
buffer += itemDescription;
}
buffer += suffix;
return buffer;
},
get type()
{
return typeof this._value;
},
get subtype()
{
if (this._value === null)
return "null";
if (this._value instanceof Array)
return "array";
if (this._value instanceof Date)
return "date";
return undefined;
},
get hasChildren()
{
return typeof this._value === "object" && this._value !== null && !!Object.keys(this._value).length;
},
getOwnProperties: function(callback)
{
callback(this._children());
},
getAllProperties: function(callback)
{
callback(this._children());
},
_children: function()
{
if (!this.hasChildren)
return [];
function buildProperty(propName)
{
return new WebInspector.RemoteObjectProperty(propName, new WebInspector.LocalJSONObject(this._value[propName]));
}
if (!this._cachedChildren)
this._cachedChildren = Object.keys(this._value || {}).map(buildProperty.bind(this));
return this._cachedChildren;
},
isError: function()
{
return false;
},
arrayLength: function()
{
return this._value instanceof Array ? this._value.length : 0;
}
}