/platform/external/webkit/WebCore/bindings/v8/ScriptController.h

https://github.com/aharish/totoro-gb-opensource-update2 · C Header · 199 lines · 83 code · 46 blank · 70 comment · 0 complexity · 1f4ae0c3b3118306a64ea636cfb9509d MD5 · raw file

  1. /*
  2. * Copyright (C) 2008, 2009 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. #ifndef ScriptController_h
  31. #define ScriptController_h
  32. #include "ScriptInstance.h"
  33. #include "ScriptValue.h"
  34. #include "V8Proxy.h"
  35. #include <v8.h>
  36. #include <wtf/HashMap.h>
  37. #include <wtf/RefCounted.h>
  38. #include <wtf/Vector.h>
  39. struct NPObject;
  40. namespace WebCore {
  41. class DOMWrapperWorld;
  42. class Event;
  43. class Frame;
  44. class HTMLPlugInElement;
  45. class ScriptSourceCode;
  46. class String;
  47. class Widget;
  48. class XSSAuditor;
  49. class ScriptController {
  50. public:
  51. ScriptController(Frame*);
  52. ~ScriptController();
  53. // FIXME: V8Proxy should either be folded into ScriptController
  54. // or this accessor should be made JSProxy*
  55. V8Proxy* proxy() { return m_proxy.get(); }
  56. ScriptValue executeScript(const ScriptSourceCode&);
  57. ScriptValue executeScript(const String& script, bool forceUserGesture = false);
  58. // Returns true if argument is a JavaScript URL.
  59. bool executeIfJavaScriptURL(const KURL&, bool userGesture = false, bool replaceDocument = true);
  60. // This function must be called from the main thread. It is safe to call it repeatedly.
  61. static void initializeThreading();
  62. // Evaluate a script file in the environment of this proxy.
  63. // If succeeded, 'succ' is set to true and result is returned
  64. // as a string.
  65. ScriptValue evaluate(const ScriptSourceCode&);
  66. void evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>&);
  67. // Executes JavaScript in an isolated world. The script gets its own global scope,
  68. // its own prototypes for intrinsic JavaScript objects (String, Array, and so-on),
  69. // and its own wrappers for all DOM nodes and DOM constructors.
  70. //
  71. // If an isolated world with the specified ID already exists, it is reused.
  72. // Otherwise, a new world is created.
  73. //
  74. // If the worldID is 0, a new world is always created.
  75. //
  76. // FIXME: Get rid of extensionGroup here.
  77. void evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>&, int extensionGroup);
  78. // Masquerade 'this' as the windowShell.
  79. // This is a bit of a hack, but provides reasonable compatibility
  80. // with what JSC does as well.
  81. ScriptController* windowShell(DOMWrapperWorld*) { return this; }
  82. ScriptController* existingWindowShell(DOMWrapperWorld*) { return this; }
  83. XSSAuditor* xssAuditor() { return m_XSSAuditor.get(); }
  84. void collectGarbage();
  85. // Notify V8 that the system is running low on memory.
  86. void lowMemoryNotification();
  87. // Creates a property of the global object of a frame.
  88. void bindToWindowObject(Frame*, const String& key, NPObject*);
  89. PassScriptInstance createScriptInstanceForWidget(Widget*);
  90. // Check if the javascript engine has been initialized.
  91. bool haveInterpreter() const;
  92. bool canExecuteScripts();
  93. // FIXME: void* is a compile hack.
  94. void attachDebugger(void*);
  95. // --- Static methods assume we are running VM in single thread, ---
  96. // --- and there is only one VM instance. ---
  97. // Returns the frame for the entered context. See comments in
  98. // V8Proxy::retrieveFrameForEnteredContext() for more information.
  99. static Frame* retrieveFrameForEnteredContext();
  100. // Returns the frame for the current context. See comments in
  101. // V8Proxy::retrieveFrameForEnteredContext() for more information.
  102. static Frame* retrieveFrameForCurrentContext();
  103. // Check whether it is safe to access a frame in another domain.
  104. static bool isSafeScript(Frame*);
  105. // Pass command-line flags to the JS engine.
  106. static void setFlags(const char* string, int length);
  107. // Protect and unprotect the JS wrapper from garbage collected.
  108. static void gcProtectJSWrapper(void*);
  109. static void gcUnprotectJSWrapper(void*);
  110. void finishedWithEvent(Event*);
  111. void setEventHandlerLineNumber(int lineNumber);
  112. void setProcessingTimerCallback(bool processingTimerCallback) { m_processingTimerCallback = processingTimerCallback; }
  113. // FIXME: Currently we don't use the parameter world at all.
  114. // See http://trac.webkit.org/changeset/54182
  115. bool processingUserGesture(DOMWrapperWorld* world = 0) const;
  116. bool anyPageIsProcessingUserGesture() const;
  117. void setPaused(bool paused) { m_paused = paused; }
  118. bool isPaused() const { return m_paused; }
  119. const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script.
  120. void clearWindowShell();
  121. void updateDocument();
  122. void updateSecurityOrigin();
  123. void clearScriptObjects();
  124. void updatePlatformScriptObjects();
  125. void cleanupScriptObjectsForPlugin(Widget*);
  126. #if ENABLE(NETSCAPE_PLUGIN_API)
  127. NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
  128. NPObject* windowScriptNPObject();
  129. #endif
  130. // Dummy method to avoid a bunch of ifdef's in WebCore.
  131. void evaluateInWorld(const ScriptSourceCode&, DOMWrapperWorld*);
  132. static void getAllWorlds(Vector<DOMWrapperWorld*>& worlds);
  133. private:
  134. Frame* m_frame;
  135. const String* m_sourceURL;
  136. bool m_inExecuteScript;
  137. bool m_processingTimerCallback;
  138. bool m_paused;
  139. OwnPtr<V8Proxy> m_proxy;
  140. typedef HashMap<Widget*, NPObject*> PluginObjectMap;
  141. // A mapping between Widgets and their corresponding script object.
  142. // This list is used so that when the plugin dies, we can immediately
  143. // invalidate all sub-objects which are associated with that plugin.
  144. // The frame keeps a NPObject reference for each item on the list.
  145. PluginObjectMap m_pluginObjects;
  146. #if ENABLE(NETSCAPE_PLUGIN_API)
  147. NPObject* m_windowScriptNPObject;
  148. #endif
  149. // The XSSAuditor associated with this ScriptController.
  150. OwnPtr<XSSAuditor> m_XSSAuditor;
  151. };
  152. } // namespace WebCore
  153. #endif // ScriptController_h