2  * Copyright (C) 2013 Apple Inc. All rights reserved. 
   3  * Copyright (C) 2012 Google Inc. All rights reserved. 
   5  * Redistribution and use in source and binary forms, with or without 
   6  * modification, are permitted provided that the following conditions are 
   9  *     * Redistributions of source code must retain the above copyright 
  10  * notice, this list of conditions and the following disclaimer. 
  11  *     * Redistributions in binary form must reproduce the above 
  12  * copyright notice, this list of conditions and the following disclaimer 
  13  * in the documentation and/or other materials provided with the 
  15  *     * Neither the name of Google Inc. nor the names of its 
  16  * contributors may be used to endorse or promote products derived from 
  17  * this software without specific prior written permission. 
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
  22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
  23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
  26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  33 #include "InjectedScript.h" 
  35 #include "InspectorValues.h" 
  36 #include "JSCInlines.h" 
  37 #include "ScriptFunctionCall.h" 
  38 #include "ScriptObject.h" 
  39 #include <wtf/text/WTFString.h> 
  41 using Inspector::Protocol::Array
; 
  45 InjectedScript::InjectedScript() 
  46     : InjectedScriptBase(ASCIILiteral("InjectedScript")) 
  50 InjectedScript::InjectedScript(Deprecated::ScriptObject injectedScriptObject
, InspectorEnvironment
* environment
) 
  51     : InjectedScriptBase(ASCIILiteral("InjectedScript"), injectedScriptObject
, environment
) 
  55 InjectedScript::~InjectedScript() 
  59 void InjectedScript::evaluate(ErrorString
& errorString
, const String
& expression
, const String
& objectGroup
, bool includeCommandLineAPI
, bool returnByValue
, bool generatePreview
, bool saveResult
, RefPtr
<Inspector::Protocol::Runtime::RemoteObject
>* result
, Inspector::Protocol::OptOutput
<bool>* wasThrown
, Inspector::Protocol::OptOutput
<int>* savedResultIndex
) 
  61     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("evaluate"), inspectorEnvironment()->functionCallHandler()); 
  62     function
.appendArgument(expression
); 
  63     function
.appendArgument(objectGroup
); 
  64     function
.appendArgument(includeCommandLineAPI
); 
  65     function
.appendArgument(returnByValue
); 
  66     function
.appendArgument(generatePreview
); 
  67     function
.appendArgument(saveResult
); 
  68     makeEvalCall(errorString
, function
, result
, wasThrown
, savedResultIndex
); 
  71 void InjectedScript::callFunctionOn(ErrorString
& errorString
, const String
& objectId
, const String
& expression
, const String
& arguments
, bool returnByValue
, bool generatePreview
, RefPtr
<Inspector::Protocol::Runtime::RemoteObject
>* result
, Inspector::Protocol::OptOutput
<bool>* wasThrown
) 
  73     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("callFunctionOn"), inspectorEnvironment()->functionCallHandler()); 
  74     function
.appendArgument(objectId
); 
  75     function
.appendArgument(expression
); 
  76     function
.appendArgument(arguments
); 
  77     function
.appendArgument(returnByValue
); 
  78     function
.appendArgument(generatePreview
); 
  79     makeEvalCall(errorString
, function
, result
, wasThrown
); 
  82 void InjectedScript::evaluateOnCallFrame(ErrorString
& errorString
, const Deprecated::ScriptValue
& callFrames
, const String
& callFrameId
, const String
& expression
, const String
& objectGroup
, bool includeCommandLineAPI
, bool returnByValue
, bool generatePreview
, bool saveResult
, RefPtr
<Inspector::Protocol::Runtime::RemoteObject
>* result
, Inspector::Protocol::OptOutput
<bool>* wasThrown
, Inspector::Protocol::OptOutput
<int>* savedResultIndex
) 
  84     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("evaluateOnCallFrame"), inspectorEnvironment()->functionCallHandler()); 
  85     function
.appendArgument(callFrames
); 
  86     function
.appendArgument(callFrameId
); 
  87     function
.appendArgument(expression
); 
  88     function
.appendArgument(objectGroup
); 
  89     function
.appendArgument(includeCommandLineAPI
); 
  90     function
.appendArgument(returnByValue
); 
  91     function
.appendArgument(generatePreview
); 
  92     function
.appendArgument(saveResult
); 
  93     makeEvalCall(errorString
, function
, result
, wasThrown
, savedResultIndex
); 
  96 void InjectedScript::getFunctionDetails(ErrorString
& errorString
, const String
& functionId
, RefPtr
<Inspector::Protocol::Debugger::FunctionDetails
>* result
) 
  98     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("getFunctionDetails"), inspectorEnvironment()->functionCallHandler()); 
  99     function
.appendArgument(functionId
); 
 101     RefPtr
<InspectorValue
> resultValue
; 
 102     makeCall(function
, &resultValue
); 
 103     if (!resultValue 
|| resultValue
->type() != InspectorValue::Type::Object
) { 
 104         if (!resultValue
->asString(errorString
)) 
 105             errorString 
= ASCIILiteral("Internal error"); 
 109     *result 
= BindingTraits
<Inspector::Protocol::Debugger::FunctionDetails
>::runtimeCast(WTF::move(resultValue
)); 
 112 void InjectedScript::getProperties(ErrorString
& errorString
, const String
& objectId
, bool ownProperties
, bool generatePreview
, RefPtr
<Array
<Inspector::Protocol::Runtime::PropertyDescriptor
>>* properties
) 
 114     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("getProperties"), inspectorEnvironment()->functionCallHandler()); 
 115     function
.appendArgument(objectId
); 
 116     function
.appendArgument(ownProperties
); 
 117     function
.appendArgument(generatePreview
); 
 119     RefPtr
<InspectorValue
> result
; 
 120     makeCall(function
, &result
); 
 121     if (!result 
|| result
->type() != InspectorValue::Type::Array
) { 
 122         errorString 
= ASCIILiteral("Internal error"); 
 126     *properties 
= BindingTraits
<Array
<Inspector::Protocol::Runtime::PropertyDescriptor
>>::runtimeCast(WTF::move(result
)); 
 129 void InjectedScript::getDisplayableProperties(ErrorString
& errorString
, const String
& objectId
, bool generatePreview
, RefPtr
<Array
<Inspector::Protocol::Runtime::PropertyDescriptor
>>* properties
) 
 131     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("getDisplayableProperties"), inspectorEnvironment()->functionCallHandler()); 
 132     function
.appendArgument(objectId
); 
 133     function
.appendArgument(generatePreview
); 
 135     RefPtr
<InspectorValue
> result
; 
 136     makeCall(function
, &result
); 
 137     if (!result 
|| result
->type() != InspectorValue::Type::Array
) { 
 138         errorString 
= ASCIILiteral("Internal error"); 
 142     *properties 
= BindingTraits
<Array
<Inspector::Protocol::Runtime::PropertyDescriptor
>>::runtimeCast(WTF::move(result
)); 
 145 void InjectedScript::getInternalProperties(ErrorString
& errorString
, const String
& objectId
, bool generatePreview
, RefPtr
<Array
<Inspector::Protocol::Runtime::InternalPropertyDescriptor
>>* properties
) 
 147     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("getInternalProperties"), inspectorEnvironment()->functionCallHandler()); 
 148     function
.appendArgument(objectId
); 
 149     function
.appendArgument(generatePreview
); 
 151     RefPtr
<InspectorValue
> result
; 
 152     makeCall(function
, &result
); 
 153     if (!result 
|| result
->type() != InspectorValue::Type::Array
) { 
 154         errorString 
= ASCIILiteral("Internal error"); 
 158     auto array 
= BindingTraits
<Array
<Inspector::Protocol::Runtime::InternalPropertyDescriptor
>>::runtimeCast(WTF::move(result
)); 
 159     *properties 
= array
->length() > 0 ? array 
: nullptr; 
 162 void InjectedScript::getCollectionEntries(ErrorString
& errorString
, const String
& objectId
, const String
& objectGroup
, int startIndex
, int numberToFetch
, RefPtr
<Protocol::Array
<Protocol::Runtime::CollectionEntry
>>* entries
) 
 164     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("getCollectionEntries"), inspectorEnvironment()->functionCallHandler()); 
 165     function
.appendArgument(objectId
); 
 166     function
.appendArgument(objectGroup
); 
 167     function
.appendArgument(startIndex
); 
 168     function
.appendArgument(numberToFetch
); 
 170     RefPtr
<InspectorValue
> result
; 
 171     makeCall(function
, &result
); 
 172     if (!result 
|| result
->type() != InspectorValue::Type::Array
) { 
 173         errorString 
= ASCIILiteral("Internal error"); 
 177     *entries 
= BindingTraits
<Array
<Protocol::Runtime::CollectionEntry
>>::runtimeCast(WTF::move(result
)); 
 180 void InjectedScript::saveResult(ErrorString
& errorString
, const String
& callArgumentJSON
, Inspector::Protocol::OptOutput
<int>* savedResultIndex
) 
 182     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("saveResult"), inspectorEnvironment()->functionCallHandler()); 
 183     function
.appendArgument(callArgumentJSON
); 
 185     RefPtr
<InspectorValue
> result
; 
 186     makeCall(function
, &result
); 
 187     if (!result 
|| result
->type() != InspectorValue::Type::Integer
) { 
 188         errorString 
= ASCIILiteral("Internal error"); 
 192     int savedResultIndexInt 
= 0; 
 193     if (result
->asInteger(savedResultIndexInt
) && savedResultIndexInt 
> 0) 
 194         *savedResultIndex 
= savedResultIndexInt
; 
 197 Ref
<Array
<Inspector::Protocol::Debugger::CallFrame
>> InjectedScript::wrapCallFrames(const Deprecated::ScriptValue
& callFrames
) 
 199     ASSERT(!hasNoValue()); 
 200     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("wrapCallFrames"), inspectorEnvironment()->functionCallHandler()); 
 201     function
.appendArgument(callFrames
); 
 203     bool hadException 
= false; 
 204     Deprecated::ScriptValue callFramesValue 
= callFunctionWithEvalEnabled(function
, hadException
); 
 205     ASSERT(!hadException
); 
 206     RefPtr
<InspectorValue
> result 
= callFramesValue
.toInspectorValue(scriptState()); 
 207     if (result
->type() == InspectorValue::Type::Array
) 
 208         return BindingTraits
<Array
<Inspector::Protocol::Debugger::CallFrame
>>::runtimeCast(WTF::move(result
)).releaseNonNull(); 
 210     return Array
<Inspector::Protocol::Debugger::CallFrame
>::create(); 
 213 RefPtr
<Inspector::Protocol::Runtime::RemoteObject
> InjectedScript::wrapObject(const Deprecated::ScriptValue
& value
, const String
& groupName
, bool generatePreview
) const 
 215     ASSERT(!hasNoValue()); 
 216     Deprecated::ScriptFunctionCall 
wrapFunction(injectedScriptObject(), ASCIILiteral("wrapObject"), inspectorEnvironment()->functionCallHandler()); 
 217     wrapFunction
.appendArgument(value
); 
 218     wrapFunction
.appendArgument(groupName
); 
 219     wrapFunction
.appendArgument(hasAccessToInspectedScriptState()); 
 220     wrapFunction
.appendArgument(generatePreview
); 
 222     bool hadException 
= false; 
 223     Deprecated::ScriptValue r 
= callFunctionWithEvalEnabled(wrapFunction
, hadException
); 
 227     RefPtr
<InspectorObject
> resultObject
; 
 228     bool castSucceeded 
= r
.toInspectorValue(scriptState())->asObject(resultObject
); 
 229     ASSERT_UNUSED(castSucceeded
, castSucceeded
); 
 231     return BindingTraits
<Inspector::Protocol::Runtime::RemoteObject
>::runtimeCast(resultObject
); 
 234 RefPtr
<Inspector::Protocol::Runtime::RemoteObject
> InjectedScript::wrapTable(const Deprecated::ScriptValue
& table
, const Deprecated::ScriptValue
& columns
) const 
 236     ASSERT(!hasNoValue()); 
 237     Deprecated::ScriptFunctionCall 
wrapFunction(injectedScriptObject(), ASCIILiteral("wrapTable"), inspectorEnvironment()->functionCallHandler()); 
 238     wrapFunction
.appendArgument(hasAccessToInspectedScriptState()); 
 239     wrapFunction
.appendArgument(table
); 
 240     if (columns
.hasNoValue()) 
 241         wrapFunction
.appendArgument(false); 
 243         wrapFunction
.appendArgument(columns
); 
 245     bool hadException 
= false; 
 246     Deprecated::ScriptValue r 
= callFunctionWithEvalEnabled(wrapFunction
, hadException
); 
 250     RefPtr
<InspectorObject
> resultObject
; 
 251     bool castSucceeded 
= r
.toInspectorValue(scriptState())->asObject(resultObject
); 
 252     ASSERT_UNUSED(castSucceeded
, castSucceeded
); 
 254     return BindingTraits
<Inspector::Protocol::Runtime::RemoteObject
>::runtimeCast(resultObject
); 
 257 void InjectedScript::setExceptionValue(const Deprecated::ScriptValue
& value
) 
 259     ASSERT(!hasNoValue()); 
 260     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("setExceptionValue"), inspectorEnvironment()->functionCallHandler()); 
 261     function
.appendArgument(value
); 
 262     RefPtr
<InspectorValue
> result
; 
 263     makeCall(function
, &result
); 
 266 void InjectedScript::clearExceptionValue() 
 268     ASSERT(!hasNoValue()); 
 269     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("clearExceptionValue"), inspectorEnvironment()->functionCallHandler()); 
 270     RefPtr
<InspectorValue
> result
; 
 271     makeCall(function
, &result
); 
 274 Deprecated::ScriptValue 
InjectedScript::findObjectById(const String
& objectId
) const 
 276     ASSERT(!hasNoValue()); 
 277     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("findObjectById"), inspectorEnvironment()->functionCallHandler()); 
 278     function
.appendArgument(objectId
); 
 280     bool hadException 
= false; 
 281     Deprecated::ScriptValue resultValue 
= callFunctionWithEvalEnabled(function
, hadException
); 
 282     ASSERT(!hadException
); 
 287 void InjectedScript::inspectObject(Deprecated::ScriptValue value
) 
 289     ASSERT(!hasNoValue()); 
 290     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("inspectObject"), inspectorEnvironment()->functionCallHandler()); 
 291     function
.appendArgument(value
); 
 292     RefPtr
<InspectorValue
> result
; 
 293     makeCall(function
, &result
); 
 296 void InjectedScript::releaseObject(const String
& objectId
) 
 298     Deprecated::ScriptFunctionCall 
function(injectedScriptObject(), ASCIILiteral("releaseObject"), inspectorEnvironment()->functionCallHandler()); 
 299     function
.appendArgument(objectId
); 
 300     RefPtr
<InspectorValue
> result
; 
 301     makeCall(function
, &result
); 
 304 void InjectedScript::releaseObjectGroup(const String
& objectGroup
) 
 306     ASSERT(!hasNoValue()); 
 307     Deprecated::ScriptFunctionCall 
releaseFunction(injectedScriptObject(), ASCIILiteral("releaseObjectGroup"), inspectorEnvironment()->functionCallHandler()); 
 308     releaseFunction
.appendArgument(objectGroup
); 
 310     bool hadException 
= false; 
 311     callFunctionWithEvalEnabled(releaseFunction
, hadException
); 
 312     ASSERT(!hadException
); 
 315 } // namespace Inspector