InjectedScript.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  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
  13. * distribution.
  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.
  17. *
  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.
  29. */
  30. #include "config.h"
  31. #if ENABLE(INSPECTOR)
  32. #include "InjectedScript.h"
  33. #include "InjectedScriptHost.h"
  34. #include "InjectedScriptModule.h"
  35. #include "InspectorValues.h"
  36. #include "Node.h"
  37. #include "ScriptFunctionCall.h"
  38. #include "SerializedScriptValue.h"
  39. #include <wtf/text/WTFString.h>
  40. using WebCore::TypeBuilder::Array;
  41. using WebCore::TypeBuilder::Debugger::CallFrame;
  42. using WebCore::TypeBuilder::Runtime::PropertyDescriptor;
  43. using WebCore::TypeBuilder::Runtime::InternalPropertyDescriptor;
  44. using WebCore::TypeBuilder::Debugger::FunctionDetails;
  45. using WebCore::TypeBuilder::Runtime::RemoteObject;
  46. namespace WebCore {
  47. InjectedScript::InjectedScript()
  48. : InjectedScriptBase("InjectedScript")
  49. {
  50. }
  51. InjectedScript::InjectedScript(ScriptObject injectedScriptObject, InspectedStateAccessCheck accessCheck)
  52. : InjectedScriptBase("InjectedScript", injectedScriptObject, accessCheck)
  53. {
  54. }
  55. void InjectedScript::evaluate(ErrorString* errorString, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown)
  56. {
  57. ScriptFunctionCall function(injectedScriptObject(), "evaluate");
  58. function.appendArgument(expression);
  59. function.appendArgument(objectGroup);
  60. function.appendArgument(includeCommandLineAPI);
  61. function.appendArgument(returnByValue);
  62. function.appendArgument(generatePreview);
  63. makeEvalCall(errorString, function, result, wasThrown);
  64. }
  65. void InjectedScript::callFunctionOn(ErrorString* errorString, const String& objectId, const String& expression, const String& arguments, bool returnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown)
  66. {
  67. ScriptFunctionCall function(injectedScriptObject(), "callFunctionOn");
  68. function.appendArgument(objectId);
  69. function.appendArgument(expression);
  70. function.appendArgument(arguments);
  71. function.appendArgument(returnByValue);
  72. function.appendArgument(generatePreview);
  73. makeEvalCall(errorString, function, result, wasThrown);
  74. }
  75. void InjectedScript::evaluateOnCallFrame(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, RefPtr<RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown)
  76. {
  77. ScriptFunctionCall function(injectedScriptObject(), "evaluateOnCallFrame");
  78. function.appendArgument(callFrames);
  79. function.appendArgument(callFrameId);
  80. function.appendArgument(expression);
  81. function.appendArgument(objectGroup);
  82. function.appendArgument(includeCommandLineAPI);
  83. function.appendArgument(returnByValue);
  84. function.appendArgument(generatePreview);
  85. makeEvalCall(errorString, function, result, wasThrown);
  86. }
  87. void InjectedScript::restartFrame(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, RefPtr<InspectorObject>* result)
  88. {
  89. ScriptFunctionCall function(injectedScriptObject(), "restartFrame");
  90. function.appendArgument(callFrames);
  91. function.appendArgument(callFrameId);
  92. RefPtr<InspectorValue> resultValue;
  93. makeCall(function, &resultValue);
  94. if (resultValue) {
  95. if (resultValue->type() == InspectorValue::TypeString) {
  96. resultValue->asString(errorString);
  97. return;
  98. }
  99. if (resultValue->type() == InspectorValue::TypeObject) {
  100. *result = resultValue->asObject();
  101. return;
  102. }
  103. }
  104. *errorString = "Internal error";
  105. }
  106. void InjectedScript::setVariableValue(ErrorString* errorString, const ScriptValue& callFrames, const String* callFrameIdOpt, const String* functionObjectIdOpt, int scopeNumber, const String& variableName, const String& newValueStr)
  107. {
  108. ScriptFunctionCall function(injectedScriptObject(), "setVariableValue");
  109. if (callFrameIdOpt) {
  110. function.appendArgument(callFrames);
  111. function.appendArgument(*callFrameIdOpt);
  112. } else {
  113. function.appendArgument(false);
  114. function.appendArgument(false);
  115. }
  116. if (functionObjectIdOpt)
  117. function.appendArgument(*functionObjectIdOpt);
  118. else
  119. function.appendArgument(false);
  120. function.appendArgument(scopeNumber);
  121. function.appendArgument(variableName);
  122. function.appendArgument(newValueStr);
  123. RefPtr<InspectorValue> resultValue;
  124. makeCall(function, &resultValue);
  125. if (!resultValue) {
  126. *errorString = "Internal error";
  127. return;
  128. }
  129. if (resultValue->type() == InspectorValue::TypeString) {
  130. resultValue->asString(errorString);
  131. return;
  132. }
  133. // Normal return.
  134. }
  135. void InjectedScript::getFunctionDetails(ErrorString* errorString, const String& functionId, RefPtr<FunctionDetails>* result)
  136. {
  137. ScriptFunctionCall function(injectedScriptObject(), "getFunctionDetails");
  138. function.appendArgument(functionId);
  139. RefPtr<InspectorValue> resultValue;
  140. makeCall(function, &resultValue);
  141. if (!resultValue || resultValue->type() != InspectorValue::TypeObject) {
  142. if (!resultValue->asString(errorString))
  143. *errorString = "Internal error";
  144. return;
  145. }
  146. *result = FunctionDetails::runtimeCast(resultValue);
  147. }
  148. void InjectedScript::getProperties(ErrorString* errorString, const String& objectId, bool ownProperties, RefPtr<Array<PropertyDescriptor> >* properties)
  149. {
  150. ScriptFunctionCall function(injectedScriptObject(), "getProperties");
  151. function.appendArgument(objectId);
  152. function.appendArgument(ownProperties);
  153. RefPtr<InspectorValue> result;
  154. makeCall(function, &result);
  155. if (!result || result->type() != InspectorValue::TypeArray) {
  156. *errorString = "Internal error";
  157. return;
  158. }
  159. *properties = Array<PropertyDescriptor>::runtimeCast(result);
  160. }
  161. void InjectedScript::getInternalProperties(ErrorString* errorString, const String& objectId, RefPtr<Array<InternalPropertyDescriptor> >* properties)
  162. {
  163. ScriptFunctionCall function(injectedScriptObject(), "getInternalProperties");
  164. function.appendArgument(objectId);
  165. RefPtr<InspectorValue> result;
  166. makeCall(function, &result);
  167. if (!result || result->type() != InspectorValue::TypeArray) {
  168. *errorString = "Internal error";
  169. return;
  170. }
  171. RefPtr<Array<InternalPropertyDescriptor> > array = Array<InternalPropertyDescriptor>::runtimeCast(result);
  172. if (array->length() > 0)
  173. *properties = array;
  174. }
  175. Node* InjectedScript::nodeForObjectId(const String& objectId)
  176. {
  177. if (hasNoValue() || !canAccessInspectedWindow())
  178. return 0;
  179. ScriptFunctionCall function(injectedScriptObject(), "nodeForObjectId");
  180. function.appendArgument(objectId);
  181. bool hadException = false;
  182. ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
  183. ASSERT(!hadException);
  184. return InjectedScriptHost::scriptValueAsNode(resultValue);
  185. }
  186. void InjectedScript::releaseObject(const String& objectId)
  187. {
  188. ScriptFunctionCall function(injectedScriptObject(), "releaseObject");
  189. function.appendArgument(objectId);
  190. RefPtr<InspectorValue> result;
  191. makeCall(function, &result);
  192. }
  193. #if ENABLE(JAVASCRIPT_DEBUGGER)
  194. PassRefPtr<Array<CallFrame> > InjectedScript::wrapCallFrames(const ScriptValue& callFrames)
  195. {
  196. ASSERT(!hasNoValue());
  197. ScriptFunctionCall function(injectedScriptObject(), "wrapCallFrames");
  198. function.appendArgument(callFrames);
  199. bool hadException = false;
  200. ScriptValue callFramesValue = callFunctionWithEvalEnabled(function, hadException);
  201. ASSERT(!hadException);
  202. RefPtr<InspectorValue> result = callFramesValue.toInspectorValue(scriptState());
  203. if (result->type() == InspectorValue::TypeArray)
  204. return Array<CallFrame>::runtimeCast(result);
  205. return Array<CallFrame>::create();
  206. }
  207. #endif
  208. PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapObject(const ScriptValue& value, const String& groupName, bool generatePreview) const
  209. {
  210. ASSERT(!hasNoValue());
  211. ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapObject");
  212. wrapFunction.appendArgument(value);
  213. wrapFunction.appendArgument(groupName);
  214. wrapFunction.appendArgument(canAccessInspectedWindow());
  215. wrapFunction.appendArgument(generatePreview);
  216. bool hadException = false;
  217. ScriptValue r = callFunctionWithEvalEnabled(wrapFunction, hadException);
  218. if (hadException)
  219. return 0;
  220. RefPtr<InspectorObject> rawResult = r.toInspectorValue(scriptState())->asObject();
  221. return TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult);
  222. }
  223. PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapTable(const ScriptValue& table, const ScriptValue& columns) const
  224. {
  225. ASSERT(!hasNoValue());
  226. ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapTable");
  227. wrapFunction.appendArgument(canAccessInspectedWindow());
  228. wrapFunction.appendArgument(table);
  229. if (columns.hasNoValue())
  230. wrapFunction.appendArgument(false);
  231. else
  232. wrapFunction.appendArgument(columns);
  233. bool hadException = false;
  234. ScriptValue r = callFunctionWithEvalEnabled(wrapFunction, hadException);
  235. if (hadException)
  236. return 0;
  237. RefPtr<InspectorObject> rawResult = r.toInspectorValue(scriptState())->asObject();
  238. return TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult);
  239. }
  240. PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapNode(Node* node, const String& groupName)
  241. {
  242. return wrapObject(nodeAsScriptValue(node), groupName);
  243. }
  244. ScriptValue InjectedScript::findObjectById(const String& objectId) const
  245. {
  246. ASSERT(!hasNoValue());
  247. ScriptFunctionCall function(injectedScriptObject(), "findObjectById");
  248. function.appendArgument(objectId);
  249. bool hadException = false;
  250. ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
  251. ASSERT(!hadException);
  252. return resultValue;
  253. }
  254. void InjectedScript::inspectNode(Node* node)
  255. {
  256. ASSERT(!hasNoValue());
  257. ScriptFunctionCall function(injectedScriptObject(), "inspectNode");
  258. function.appendArgument(nodeAsScriptValue(node));
  259. RefPtr<InspectorValue> result;
  260. makeCall(function, &result);
  261. }
  262. void InjectedScript::releaseObjectGroup(const String& objectGroup)
  263. {
  264. ASSERT(!hasNoValue());
  265. ScriptFunctionCall releaseFunction(injectedScriptObject(), "releaseObjectGroup");
  266. releaseFunction.appendArgument(objectGroup);
  267. bool hadException = false;
  268. callFunctionWithEvalEnabled(releaseFunction, hadException);
  269. ASSERT(!hadException);
  270. }
  271. ScriptValue InjectedScript::nodeAsScriptValue(Node* node)
  272. {
  273. return InjectedScriptHost::nodeAsScriptValue(scriptState(), node);
  274. }
  275. } // namespace WebCore
  276. #endif // ENABLE(INSPECTOR)