InjectedScriptBase.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "InjectedScriptBase.h"
  33. #include "InspectorInstrumentation.h"
  34. #include "InspectorValues.h"
  35. #include "ScriptFunctionCall.h"
  36. #include <runtime/JSGlobalObject.h>
  37. #include <wtf/text/WTFString.h>
  38. using WebCore::TypeBuilder::Runtime::RemoteObject;
  39. namespace WebCore {
  40. InjectedScriptBase::InjectedScriptBase(const String& name)
  41. : m_name(name)
  42. , m_inspectedStateAccessCheck(0)
  43. {
  44. }
  45. InjectedScriptBase::InjectedScriptBase(const String& name, ScriptObject injectedScriptObject, InspectedStateAccessCheck accessCheck)
  46. : m_name(name)
  47. , m_injectedScriptObject(injectedScriptObject)
  48. , m_inspectedStateAccessCheck(accessCheck)
  49. {
  50. }
  51. void InjectedScriptBase::initialize(ScriptObject injectedScriptObject, InspectedStateAccessCheck accessCheck)
  52. {
  53. m_injectedScriptObject = injectedScriptObject;
  54. m_inspectedStateAccessCheck = accessCheck;
  55. }
  56. bool InjectedScriptBase::canAccessInspectedWindow() const
  57. {
  58. return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState());
  59. }
  60. const ScriptObject& InjectedScriptBase::injectedScriptObject() const
  61. {
  62. return m_injectedScriptObject;
  63. }
  64. ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(ScriptFunctionCall& function, bool& hadException) const
  65. {
  66. ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromScriptState(m_injectedScriptObject.scriptState());
  67. InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(scriptExecutionContext, name(), 1);
  68. ScriptState* scriptState = m_injectedScriptObject.scriptState();
  69. bool evalIsDisabled = false;
  70. if (scriptState) {
  71. evalIsDisabled = !scriptState->lexicalGlobalObject()->evalEnabled();
  72. // Temporarily enable allow evals for inspector.
  73. if (evalIsDisabled)
  74. scriptState->lexicalGlobalObject()->setEvalEnabled(true);
  75. }
  76. ScriptValue resultValue = function.call(hadException);
  77. if (evalIsDisabled)
  78. scriptState->lexicalGlobalObject()->setEvalEnabled(false);
  79. InspectorInstrumentation::didCallFunction(cookie);
  80. return resultValue;
  81. }
  82. void InjectedScriptBase::makeCall(ScriptFunctionCall& function, RefPtr<InspectorValue>* result)
  83. {
  84. if (hasNoValue() || !canAccessInspectedWindow()) {
  85. *result = InspectorValue::null();
  86. return;
  87. }
  88. bool hadException = false;
  89. ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
  90. ASSERT(!hadException);
  91. if (!hadException) {
  92. *result = resultValue.toInspectorValue(m_injectedScriptObject.scriptState());
  93. if (!*result)
  94. *result = InspectorString::create(String::format("Object has too long reference chain(must not be longer than %d)", InspectorValue::maxDepth));
  95. } else
  96. *result = InspectorString::create("Exception while making a call.");
  97. }
  98. void InjectedScriptBase::makeEvalCall(ErrorString* errorString, ScriptFunctionCall& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuilder::OptOutput<bool>* wasThrown)
  99. {
  100. RefPtr<InspectorValue> result;
  101. makeCall(function, &result);
  102. if (!result) {
  103. *errorString = "Internal error: result value is empty";
  104. return;
  105. }
  106. if (result->type() == InspectorValue::TypeString) {
  107. result->asString(errorString);
  108. ASSERT(errorString->length());
  109. return;
  110. }
  111. RefPtr<InspectorObject> resultPair = result->asObject();
  112. if (!resultPair) {
  113. *errorString = "Internal error: result is not an Object";
  114. return;
  115. }
  116. RefPtr<InspectorObject> resultObj = resultPair->getObject("result");
  117. bool wasThrownVal = false;
  118. if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) {
  119. *errorString = "Internal error: result is not a pair of value and wasThrown flag";
  120. return;
  121. }
  122. *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj);
  123. *wasThrown = wasThrownVal;
  124. }
  125. } // namespace WebCore
  126. #endif // ENABLE(INSPECTOR)