/platform/external/webkit/WebCore/bindings/js/JSInjectedScriptHostCustom.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 237 lines · 171 code · 35 blank · 31 comment · 27 complexity · 553a34eba860bce6e2af6e03c0f89810 MD5 · raw file

  1. /*
  2. * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
  4. * Copyright (C) 2009 Google Inc. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "config.h"
  33. #include "JSInjectedScriptHost.h"
  34. #if ENABLE(INSPECTOR)
  35. #include "Console.h"
  36. #if ENABLE(DATABASE)
  37. #include "Database.h"
  38. #include "JSDatabase.h"
  39. #endif
  40. #include "ExceptionCode.h"
  41. #include "Frame.h"
  42. #include "FrameLoader.h"
  43. #include "InjectedScript.h"
  44. #include "InjectedScriptHost.h"
  45. #include "InspectorController.h"
  46. #include "InspectorResource.h"
  47. #include "JSDOMWindow.h"
  48. #include "JSNode.h"
  49. #include "JSRange.h"
  50. #include "Node.h"
  51. #include "Page.h"
  52. #if ENABLE(DOM_STORAGE)
  53. #include "SerializedScriptValue.h"
  54. #include "Storage.h"
  55. #include "JSStorage.h"
  56. #endif
  57. #include "TextIterator.h"
  58. #include "VisiblePosition.h"
  59. #include <parser/SourceCode.h>
  60. #include <runtime/JSArray.h>
  61. #include <runtime/JSLock.h>
  62. #include <wtf/RefPtr.h>
  63. #include <wtf/Vector.h>
  64. #if ENABLE(JAVASCRIPT_DEBUGGER)
  65. #include "JavaScriptCallFrame.h"
  66. #include "JavaScriptDebugServer.h"
  67. #include "JSJavaScriptCallFrame.h"
  68. #endif
  69. using namespace JSC;
  70. namespace WebCore {
  71. static ScriptObject createInjectedScript(const String& source, InjectedScriptHost* injectedScriptHost, ScriptState* scriptState, long id)
  72. {
  73. SourceCode sourceCode = makeSource(source);
  74. JSLock lock(SilenceAssertionsOnly);
  75. JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
  76. JSValue globalThisValue = scriptState->globalThisValue();
  77. Completion comp = JSC::evaluate(scriptState, globalObject->globalScopeChain(), sourceCode, globalThisValue);
  78. if (comp.complType() != JSC::Normal && comp.complType() != JSC::ReturnValue)
  79. return ScriptObject();
  80. JSValue functionValue = comp.value();
  81. CallData callData;
  82. CallType callType = functionValue.getCallData(callData);
  83. if (callType == CallTypeNone)
  84. return ScriptObject();
  85. MarkedArgumentBuffer args;
  86. args.append(toJS(scriptState, globalObject, injectedScriptHost));
  87. args.append(globalThisValue);
  88. args.append(jsNumber(scriptState, id));
  89. JSValue result = JSC::call(scriptState, functionValue, callType, callData, globalThisValue, args);
  90. if (result.isObject())
  91. return ScriptObject(scriptState, result.getObject());
  92. return ScriptObject();
  93. }
  94. #if ENABLE(DATABASE)
  95. JSValue JSInjectedScriptHost::databaseForId(ExecState* exec, const ArgList& args)
  96. {
  97. if (args.size() < 1)
  98. return jsUndefined();
  99. InspectorController* ic = impl()->inspectorController();
  100. if (!ic)
  101. return jsUndefined();
  102. Database* database = impl()->databaseForId(args.at(0).toInt32(exec));
  103. if (!database)
  104. return jsUndefined();
  105. return toJS(exec, database);
  106. }
  107. #endif
  108. #if ENABLE(JAVASCRIPT_DEBUGGER)
  109. JSValue JSInjectedScriptHost::currentCallFrame(ExecState* exec, const ArgList&)
  110. {
  111. JavaScriptCallFrame* callFrame = impl()->currentCallFrame();
  112. if (!callFrame || !callFrame->isValid())
  113. return jsUndefined();
  114. JSLock lock(SilenceAssertionsOnly);
  115. return toJS(exec, callFrame);
  116. }
  117. JSValue JSInjectedScriptHost::isActivation(ExecState*, const ArgList& args)
  118. {
  119. JSObject* object = args.at(0).getObject();
  120. return jsBoolean(object && object->isActivationObject());
  121. }
  122. #endif
  123. JSValue JSInjectedScriptHost::nodeForId(ExecState* exec, const ArgList& args)
  124. {
  125. if (args.size() < 1)
  126. return jsUndefined();
  127. Node* node = impl()->nodeForId(args.at(0).toInt32(exec));
  128. if (!node)
  129. return jsUndefined();
  130. InspectorController* ic = impl()->inspectorController();
  131. if (!ic)
  132. return jsUndefined();
  133. JSLock lock(SilenceAssertionsOnly);
  134. return toJS(exec, node);
  135. }
  136. JSValue JSInjectedScriptHost::pushNodePathToFrontend(ExecState* exec, const ArgList& args)
  137. {
  138. if (args.size() < 3)
  139. return jsUndefined();
  140. Node* node = toNode(args.at(0));
  141. if (!node)
  142. return jsUndefined();
  143. bool withChildren = args.at(1).toBoolean(exec);
  144. bool selectInUI = args.at(2).toBoolean(exec);
  145. return jsNumber(exec, impl()->pushNodePathToFrontend(node, withChildren, selectInUI));
  146. }
  147. #if ENABLE(DATABASE)
  148. JSValue JSInjectedScriptHost::selectDatabase(ExecState*, const ArgList& args)
  149. {
  150. if (args.size() < 1)
  151. return jsUndefined();
  152. Database* database = toDatabase(args.at(0));
  153. if (database)
  154. impl()->selectDatabase(database);
  155. return jsUndefined();
  156. }
  157. #endif
  158. #if ENABLE(DOM_STORAGE)
  159. JSValue JSInjectedScriptHost::selectDOMStorage(ExecState*, const ArgList& args)
  160. {
  161. if (args.size() < 1)
  162. return jsUndefined();
  163. InspectorController* ic = impl()->inspectorController();
  164. if (!ic)
  165. return jsUndefined();
  166. Storage* storage = toStorage(args.at(0));
  167. if (storage)
  168. impl()->selectDOMStorage(storage);
  169. return jsUndefined();
  170. }
  171. #endif
  172. JSValue JSInjectedScriptHost::reportDidDispatchOnInjectedScript(ExecState* exec, const ArgList& args)
  173. {
  174. if (args.size() < 3)
  175. return jsUndefined();
  176. if (!args.at(0).isInt32())
  177. return jsUndefined();
  178. int callId = args.at(0).asInt32();
  179. RefPtr<SerializedScriptValue> result(SerializedScriptValue::create(exec, args.at(1)));
  180. bool isException;
  181. if (!args.at(2).getBoolean(isException))
  182. return jsUndefined();
  183. impl()->reportDidDispatchOnInjectedScript(callId, result.get(), isException);
  184. return jsUndefined();
  185. }
  186. InjectedScript InjectedScriptHost::injectedScriptFor(ScriptState* scriptState)
  187. {
  188. JSLock lock(SilenceAssertionsOnly);
  189. JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
  190. JSObject* injectedScript = globalObject->injectedScript();
  191. if (injectedScript)
  192. return InjectedScript(ScriptObject(scriptState, injectedScript));
  193. ASSERT(!m_injectedScriptSource.isEmpty());
  194. ScriptObject injectedScriptObject = createInjectedScript(m_injectedScriptSource, this, scriptState, m_nextInjectedScriptId);
  195. globalObject->setInjectedScript(injectedScriptObject.jsObject());
  196. InjectedScript result(injectedScriptObject);
  197. m_idToInjectedScript.set(m_nextInjectedScriptId, result);
  198. m_nextInjectedScriptId++;
  199. return result;
  200. }
  201. } // namespace WebCore
  202. #endif // ENABLE(INSPECTOR)