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 31 #ifndef ScriptController_h 32 #define ScriptController_h 33 34 #include "ScriptInstance.h" 35 #include "ScriptValue.h" 36 37 #include "V8Proxy.h" 38 39 #include <v8.h> 40 41 #include <wtf/HashMap.h> 42 #include <wtf/Vector.h> 43 44 namespace WebCore { 45 class Event; 46 class Frame; 47 class HTMLPlugInElement; 48 class ScriptSourceCode; 49 class ScriptState; 50 class String; 51 class Widget; 52 class XSSAuditor; 53 54 class ScriptController { 55 public: 56 ScriptController(Frame*); 57 ~ScriptController(); 58 59 // FIXME: V8Proxy should either be folded into ScriptController 60 // or this accessor should be made JSProxy* proxy()61 V8Proxy* proxy() { return m_proxy.get(); } 62 63 // Evaluate a script file in the environment of this proxy. 64 // If succeeded, 'succ' is set to true and result is returned 65 // as a string. 66 ScriptValue evaluate(const ScriptSourceCode&); 67 68 // Executes JavaScript in a new world associated with the web frame. The 69 // script gets its own global scope, its own prototypes for intrinsic 70 // JavaScript objects (String, Array, and so-on), and its own wrappers for 71 // all DOM nodes and DOM constructors. 72 void evaluateInNewWorld(const Vector<ScriptSourceCode>&, int extensionGroup); 73 74 // Executes JavaScript in a new context associated with the web frame. The 75 // script gets its own global scope and its own prototypes for intrinsic 76 // JavaScript objects (String, Array, and so-on). It shares the wrappers for 77 // all DOM nodes and DOM constructors. 78 void evaluateInNewContext(const Vector<ScriptSourceCode>&, int extensionGroup); 79 80 // JSC has a WindowShell object, but for V8, the ScriptController 81 // is the WindowShell. haveWindowShell()82 bool haveWindowShell() const { return true; } 83 84 // Masquerade 'this' as the windowShell. 85 // This is a bit of a hack, but provides reasonable compatibility 86 // with what JSC does as well. windowShell()87 ScriptController* windowShell() { return this; } 88 state()89 ScriptState* state() const { return m_scriptState.get(); } 90 xssAuditor()91 XSSAuditor* xssAuditor() { return m_XSSAuditor.get(); } 92 93 void collectGarbage(); 94 95 // Notify V8 that the system is running low on memory. 96 void lowMemoryNotification(); 97 98 // Creates a property of the global object of a frame. 99 void bindToWindowObject(Frame*, const String& key, NPObject*); 100 101 PassScriptInstance createScriptInstanceForWidget(Widget*); 102 103 // Check if the javascript engine has been initialized. 104 bool haveInterpreter() const; 105 106 bool isEnabled() const; 107 108 // FIXME: void* is a compile hack. 109 void attachDebugger(void*); 110 111 // --- Static methods assume we are running VM in single thread, --- 112 // --- and there is only one VM instance. --- 113 114 // Returns the frame for the entered context. See comments in 115 // V8Proxy::retrieveFrameForEnteredContext() for more information. 116 static Frame* retrieveFrameForEnteredContext(); 117 118 // Returns the frame for the current context. See comments in 119 // V8Proxy::retrieveFrameForEnteredContext() for more information. 120 static Frame* retrieveFrameForCurrentContext(); 121 122 // Check whether it is safe to access a frame in another domain. 123 static bool isSafeScript(Frame*); 124 125 // Pass command-line flags to the JS engine. 126 static void setFlags(const char* string, int length); 127 128 // Protect and unprotect the JS wrapper from garbage collected. 129 static void gcProtectJSWrapper(void*); 130 static void gcUnprotectJSWrapper(void*); 131 132 void finishedWithEvent(Event*); 133 void setEventHandlerLineNumber(int lineNumber); 134 setProcessingTimerCallback(bool processingTimerCallback)135 void setProcessingTimerCallback(bool processingTimerCallback) { m_processingTimerCallback = processingTimerCallback; } 136 bool processingUserGesture() const; 137 setPaused(bool paused)138 void setPaused(bool paused) { m_paused = paused; } isPaused()139 bool isPaused() const { return m_paused; } 140 sourceURL()141 const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script. 142 143 void clearWindowShell(); 144 void updateDocument(); 145 146 void updateSecurityOrigin(); 147 void clearScriptObjects(); 148 void updatePlatformScriptObjects(); 149 void cleanupScriptObjectsForPlugin(void*); 150 151 #if ENABLE(NETSCAPE_PLUGIN_API) 152 NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*); 153 NPObject* windowScriptNPObject(); 154 #endif 155 156 private: 157 Frame* m_frame; 158 const String* m_sourceURL; 159 160 bool m_processingTimerCallback; 161 bool m_paused; 162 163 OwnPtr<ScriptState> m_scriptState; 164 OwnPtr<V8Proxy> m_proxy; 165 typedef HashMap<void*, NPObject*> PluginObjectMap; 166 167 // A mapping between Widgets and their corresponding script object. 168 // This list is used so that when the plugin dies, we can immediately 169 // invalidate all sub-objects which are associated with that plugin. 170 // The frame keeps a NPObject reference for each item on the list. 171 PluginObjectMap m_pluginObjects; 172 #if ENABLE(NETSCAPE_PLUGIN_API) 173 NPObject* m_windowScriptNPObject; 174 #endif 175 // The XSSAuditor associated with this ScriptController. 176 OwnPtr<XSSAuditor> m_XSSAuditor; 177 }; 178 179 } // namespace WebCore 180 181 #endif // ScriptController_h 182