Home | History | Annotate | Download | only in js
      1 /*
      2  *  Copyright (C) 1999 Harri Porten (porten (at) kde.org)
      3  *  Copyright (C) 2001 Peter Kelly (pmk (at) post.com)
      4  *  Copyright (C) 2008 Apple Inc. All rights reserved.
      5  *  Copyright (C) 2008 Eric Seidel <eric (at) webkit.org>
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     20  */
     21 
     22 #ifndef ScriptController_h
     23 #define ScriptController_h
     24 
     25 #include "JSDOMWindowShell.h"
     26 #include "ScriptInstance.h"
     27 #include <runtime/Protect.h>
     28 #include <wtf/RefPtr.h>
     29 
     30 #if PLATFORM(MAC)
     31 #include <wtf/RetainPtr.h>
     32 
     33 #ifdef __OBJC__
     34 @class WebScriptObject;
     35 #else
     36 class WebScriptObject;
     37 #endif
     38 #endif
     39 
     40 struct NPObject;
     41 
     42 namespace JSC {
     43     class JSGlobalObject;
     44 
     45     namespace Bindings {
     46         class RootObject;
     47     }
     48 }
     49 
     50 namespace WebCore {
     51 
     52 class Event;
     53 class EventListener;
     54 class HTMLPlugInElement;
     55 class Frame;
     56 class Node;
     57 class ScriptSourceCode;
     58 class ScriptValue;
     59 class String;
     60 class Widget;
     61 class XSSAuditor;
     62 
     63 typedef HashMap<void*, RefPtr<JSC::Bindings::RootObject> > RootObjectMap;
     64 
     65 class ScriptController {
     66     friend class ScriptCachedFrameData;
     67     typedef WTF::HashMap< RefPtr<DOMWrapperWorld>, JSC::ProtectedPtr<JSDOMWindowShell> > ShellMap;
     68 
     69 public:
     70     ScriptController(Frame*);
     71     ~ScriptController();
     72 
     73     static PassRefPtr<DOMWrapperWorld> createWorld();
     74 
     75     JSDOMWindowShell* windowShell(DOMWrapperWorld* world)
     76     {
     77         ShellMap::iterator iter = m_windowShells.find(world);
     78         return (iter != m_windowShells.end()) ? iter->second.get() : initScript(world);
     79     }
     80     JSDOMWindowShell* existingWindowShell(DOMWrapperWorld* world) const
     81     {
     82         ShellMap::const_iterator iter = m_windowShells.find(world);
     83         return (iter != m_windowShells.end()) ? iter->second.get() : 0;
     84     }
     85     JSDOMWindow* globalObject(DOMWrapperWorld* world)
     86     {
     87         return windowShell(world)->window();
     88     }
     89 
     90     static void getAllWorlds(Vector<DOMWrapperWorld*>&);
     91 
     92     ScriptValue executeScript(const ScriptSourceCode&);
     93     ScriptValue executeScript(const String& script, bool forceUserGesture = false);
     94     ScriptValue executeScriptInWorld(DOMWrapperWorld* world, const String& script, bool forceUserGesture = false);
     95 
     96     // Returns true if argument is a JavaScript URL.
     97     bool executeIfJavaScriptURL(const KURL&, bool userGesture = false, bool replaceDocument = true);
     98 
     99     // This function must be called from the main thread. It is safe to call it repeatedly.
    100     // Darwin is an exception to this rule: it is OK to call this function from any thread, even reentrantly.
    101     static void initializeThreading();
    102 
    103     ScriptValue evaluate(const ScriptSourceCode&);
    104     ScriptValue evaluateInWorld(const ScriptSourceCode&, DOMWrapperWorld*);
    105 
    106     void setEventHandlerLineNumber(int lineno) { m_handlerLineNumber = lineno; }
    107     int eventHandlerLineNumber() { return m_handlerLineNumber; }
    108 
    109     void setProcessingTimerCallback(bool b) { m_processingTimerCallback = b; }
    110     bool processingUserGesture(DOMWrapperWorld*) const;
    111     bool anyPageIsProcessingUserGesture() const;
    112 
    113     bool canExecuteScripts();
    114 
    115     // Debugger can be 0 to detach any existing Debugger.
    116     void attachDebugger(JSC::Debugger*); // Attaches/detaches in all worlds/window shells.
    117     void attachDebugger(JSDOMWindowShell*, JSC::Debugger*);
    118 
    119     void setPaused(bool b) { m_paused = b; }
    120     bool isPaused() const { return m_paused; }
    121 
    122     void setAllowPopupsFromPlugin(bool allowPopupsFromPlugin) { m_allowPopupsFromPlugin = allowPopupsFromPlugin; }
    123     bool allowPopupsFromPlugin() const { return m_allowPopupsFromPlugin; }
    124 
    125     const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script
    126 
    127     void clearWindowShell();
    128     void updateDocument();
    129 
    130     // Notifies the ScriptController that the securityOrigin of the current
    131     // document was modified.  For example, this method is called when
    132     // document.domain is set.  This method is *not* called when a new document
    133     // is attached to a frame because updateDocument() is called instead.
    134     void updateSecurityOrigin();
    135 
    136     void clearScriptObjects();
    137     void cleanupScriptObjectsForPlugin(void*);
    138 
    139     void updatePlatformScriptObjects();
    140 
    141     PassScriptInstance createScriptInstanceForWidget(Widget*);
    142     JSC::Bindings::RootObject* bindingRootObject();
    143 
    144     PassRefPtr<JSC::Bindings::RootObject> createRootObject(void* nativeHandle);
    145 
    146 #if PLATFORM(MAC)
    147 #if ENABLE(MAC_JAVA_BRIDGE)
    148     static void initJavaJSBindings();
    149 #endif
    150     WebScriptObject* windowScriptObject();
    151 #endif
    152 
    153     JSC::JSObject* jsObjectForPluginElement(HTMLPlugInElement*);
    154 
    155 #if ENABLE(NETSCAPE_PLUGIN_API)
    156     NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
    157     NPObject* windowScriptNPObject();
    158 #endif
    159 
    160     XSSAuditor* xssAuditor() { return m_XSSAuditor.get(); }
    161 
    162 private:
    163     JSDOMWindowShell* initScript(DOMWrapperWorld* world);
    164 
    165     void disconnectPlatformScriptObjects();
    166 
    167     bool processingUserGestureEvent(DOMWrapperWorld*) const;
    168     bool isJavaScriptAnchorNavigation() const;
    169 
    170     ShellMap m_windowShells;
    171     Frame* m_frame;
    172     int m_handlerLineNumber;
    173     const String* m_sourceURL;
    174 
    175     bool m_inExecuteScript;
    176 
    177     bool m_processingTimerCallback;
    178     bool m_paused;
    179     bool m_allowPopupsFromPlugin;
    180 
    181     // The root object used for objects bound outside the context of a plugin.
    182     RefPtr<JSC::Bindings::RootObject> m_bindingRootObject;
    183     RootObjectMap m_rootObjects;
    184 #if ENABLE(NETSCAPE_PLUGIN_API)
    185     NPObject* m_windowScriptNPObject;
    186 #endif
    187 #if PLATFORM(MAC)
    188     RetainPtr<WebScriptObject> m_windowScriptObject;
    189 #endif
    190 
    191     // The XSSAuditor associated with this ScriptController.
    192     OwnPtr<XSSAuditor> m_XSSAuditor;
    193 };
    194 
    195 } // namespace WebCore
    196 
    197 #endif // ScriptController_h
    198