InspectorRuntimeAgent.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2011 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 "InspectorRuntimeAgent.h"
  33. #include "InjectedScript.h"
  34. #include "InjectedScriptManager.h"
  35. #include "InspectorValues.h"
  36. #include "JSDOMWindowBase.h"
  37. #include <parser/ParserError.h>
  38. #include <parser/SourceCode.h>
  39. #include <runtime/Completion.h>
  40. #include <runtime/JSLock.h>
  41. #include <wtf/PassRefPtr.h>
  42. #if ENABLE(JAVASCRIPT_DEBUGGER)
  43. #include "ScriptDebugServer.h"
  44. #endif
  45. using namespace JSC;
  46. namespace WebCore {
  47. static bool asBool(const bool* const b)
  48. {
  49. return b ? *b : false;
  50. }
  51. InspectorRuntimeAgent::InspectorRuntimeAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InjectedScriptManager* injectedScriptManager)
  52. : InspectorBaseAgent<InspectorRuntimeAgent>("Runtime", instrumentingAgents, state)
  53. , m_enabled(false)
  54. , m_injectedScriptManager(injectedScriptManager)
  55. #if ENABLE(JAVASCRIPT_DEBUGGER)
  56. , m_scriptDebugServer(0)
  57. #endif
  58. {
  59. }
  60. InspectorRuntimeAgent::~InspectorRuntimeAgent()
  61. {
  62. }
  63. #if ENABLE(JAVASCRIPT_DEBUGGER)
  64. static ScriptDebugServer::PauseOnExceptionsState setPauseOnExceptionsState(ScriptDebugServer* scriptDebugServer, ScriptDebugServer::PauseOnExceptionsState newState)
  65. {
  66. ASSERT(scriptDebugServer);
  67. ScriptDebugServer::PauseOnExceptionsState presentState = scriptDebugServer->pauseOnExceptionsState();
  68. if (presentState != newState)
  69. scriptDebugServer->setPauseOnExceptionsState(newState);
  70. return presentState;
  71. }
  72. #endif
  73. static PassRefPtr<TypeBuilder::Runtime::ErrorRange> buildErrorRangeObject(const JSTokenLocation& tokenLocation)
  74. {
  75. RefPtr<TypeBuilder::Runtime::ErrorRange> result = TypeBuilder::Runtime::ErrorRange::create()
  76. .setStartOffset(tokenLocation.startOffset)
  77. .setEndOffset(tokenLocation.endOffset);
  78. return result.release();
  79. }
  80. void InspectorRuntimeAgent::parse(ErrorString*, const String& expression, TypeBuilder::Runtime::SyntaxErrorType::Enum* result, TypeBuilder::OptOutput<String>* message, RefPtr<TypeBuilder::Runtime::ErrorRange>& range)
  81. {
  82. VM* vm = JSDOMWindowBase::commonVM();
  83. JSLockHolder lock(vm);
  84. ParserError error;
  85. checkSyntax(*vm, JSC::makeSource(expression), error);
  86. switch (error.m_syntaxErrorType) {
  87. case ParserError::SyntaxErrorNone:
  88. *result = TypeBuilder::Runtime::SyntaxErrorType::None;
  89. break;
  90. case ParserError::SyntaxErrorIrrecoverable:
  91. *result = TypeBuilder::Runtime::SyntaxErrorType::Irrecoverable;
  92. break;
  93. case ParserError::SyntaxErrorUnterminatedLiteral:
  94. *result = TypeBuilder::Runtime::SyntaxErrorType::Unterminated_literal;
  95. break;
  96. case ParserError::SyntaxErrorRecoverable:
  97. *result = TypeBuilder::Runtime::SyntaxErrorType::Recoverable;
  98. break;
  99. }
  100. if (error.m_syntaxErrorType != ParserError::SyntaxErrorNone) {
  101. *message = error.m_message;
  102. range = buildErrorRangeObject(error.m_token.m_location);
  103. }
  104. }
  105. void InspectorRuntimeAgent::evaluate(ErrorString* errorString, const String& expression, const String* const objectGroup, const bool* const includeCommandLineAPI, const bool* const doNotPauseOnExceptionsAndMuteConsole, const int* executionContextId, const bool* const returnByValue, const bool* generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>& result, TypeBuilder::OptOutput<bool>* wasThrown)
  106. {
  107. InjectedScript injectedScript = injectedScriptForEval(errorString, executionContextId);
  108. if (injectedScript.hasNoValue())
  109. return;
  110. #if ENABLE(JAVASCRIPT_DEBUGGER)
  111. ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = ScriptDebugServer::DontPauseOnExceptions;
  112. if (asBool(doNotPauseOnExceptionsAndMuteConsole))
  113. previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
  114. #endif
  115. if (asBool(doNotPauseOnExceptionsAndMuteConsole))
  116. muteConsole();
  117. injectedScript.evaluate(errorString, expression, objectGroup ? *objectGroup : "", asBool(includeCommandLineAPI), asBool(returnByValue), asBool(generatePreview), &result, wasThrown);
  118. if (asBool(doNotPauseOnExceptionsAndMuteConsole)) {
  119. unmuteConsole();
  120. #if ENABLE(JAVASCRIPT_DEBUGGER)
  121. setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
  122. #endif
  123. }
  124. }
  125. void InspectorRuntimeAgent::callFunctionOn(ErrorString* errorString, const String& objectId, const String& expression, const RefPtr<InspectorArray>* const optionalArguments, const bool* const doNotPauseOnExceptionsAndMuteConsole, const bool* const returnByValue, const bool* generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>& result, TypeBuilder::OptOutput<bool>* wasThrown)
  126. {
  127. InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
  128. if (injectedScript.hasNoValue()) {
  129. *errorString = "Inspected frame has gone";
  130. return;
  131. }
  132. String arguments;
  133. if (optionalArguments)
  134. arguments = (*optionalArguments)->toJSONString();
  135. #if ENABLE(JAVASCRIPT_DEBUGGER)
  136. ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = ScriptDebugServer::DontPauseOnExceptions;
  137. if (asBool(doNotPauseOnExceptionsAndMuteConsole))
  138. previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
  139. #endif
  140. if (asBool(doNotPauseOnExceptionsAndMuteConsole))
  141. muteConsole();
  142. injectedScript.callFunctionOn(errorString, objectId, expression, arguments, asBool(returnByValue), asBool(generatePreview), &result, wasThrown);
  143. if (asBool(doNotPauseOnExceptionsAndMuteConsole)) {
  144. unmuteConsole();
  145. #if ENABLE(JAVASCRIPT_DEBUGGER)
  146. setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
  147. #endif
  148. }
  149. }
  150. void InspectorRuntimeAgent::getProperties(ErrorString* errorString, const String& objectId, const bool* const ownProperties, RefPtr<TypeBuilder::Array<TypeBuilder::Runtime::PropertyDescriptor> >& result, RefPtr<TypeBuilder::Array<TypeBuilder::Runtime::InternalPropertyDescriptor> >& internalProperties)
  151. {
  152. InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
  153. if (injectedScript.hasNoValue()) {
  154. *errorString = "Inspected frame has gone";
  155. return;
  156. }
  157. #if ENABLE(JAVASCRIPT_DEBUGGER)
  158. ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = setPauseOnExceptionsState(m_scriptDebugServer, ScriptDebugServer::DontPauseOnExceptions);
  159. #endif
  160. muteConsole();
  161. injectedScript.getProperties(errorString, objectId, ownProperties ? *ownProperties : false, &result);
  162. injectedScript.getInternalProperties(errorString, objectId, &internalProperties);
  163. unmuteConsole();
  164. #if ENABLE(JAVASCRIPT_DEBUGGER)
  165. setPauseOnExceptionsState(m_scriptDebugServer, previousPauseOnExceptionsState);
  166. #endif
  167. }
  168. void InspectorRuntimeAgent::releaseObject(ErrorString*, const String& objectId)
  169. {
  170. InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
  171. if (!injectedScript.hasNoValue())
  172. injectedScript.releaseObject(objectId);
  173. }
  174. void InspectorRuntimeAgent::releaseObjectGroup(ErrorString*, const String& objectGroup)
  175. {
  176. m_injectedScriptManager->releaseObjectGroup(objectGroup);
  177. }
  178. void InspectorRuntimeAgent::run(ErrorString*)
  179. {
  180. }
  181. #if ENABLE(JAVASCRIPT_DEBUGGER)
  182. void InspectorRuntimeAgent::setScriptDebugServer(ScriptDebugServer* scriptDebugServer)
  183. {
  184. m_scriptDebugServer = scriptDebugServer;
  185. }
  186. #endif // ENABLE(JAVASCRIPT_DEBUGGER)
  187. } // namespace WebCore
  188. #endif // ENABLE(INSPECTOR)