ScriptController.h 5.96 KB
Newer Older
kocienda's avatar
kocienda committed
1 2
/*
 *  Copyright (C) 1999 Harri Porten (porten@kde.org)
3
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
weinig@apple.com's avatar
weinig@apple.com committed
4
 *  Copyright (C) 2008 Apple Inc. All rights reserved.
5
 *  Copyright (C) 2008 Eric Seidel <eric@webkit.org>
kocienda's avatar
kocienda committed
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
ddkilzer's avatar
ddkilzer committed
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
kocienda's avatar
kocienda committed
20 21
 */

darin@apple.com's avatar
darin@apple.com committed
22 23
#ifndef ScriptController_h
#define ScriptController_h
kocienda's avatar
kocienda committed
24

25
#include "JSDOMWindowShell.h"
26
#include "ScriptInstance.h"
27
#include <runtime/Protect.h>
ggaren's avatar
ggaren committed
28
#include <wtf/RefPtr.h>
29 30

#if PLATFORM(MAC)
eric@webkit.org's avatar
eric@webkit.org committed
31 32
#include <wtf/RetainPtr.h>

33 34 35 36 37 38 39 40
#ifdef __OBJC__
@class WebScriptObject;
#else
class WebScriptObject;
#endif
#endif

struct NPObject;
ggaren's avatar
ggaren committed
41

42
namespace JSC {
ggaren@apple.com's avatar
ggaren@apple.com committed
43
    class JSGlobalObject;
44 45 46 47

    namespace Bindings {
        class RootObject;
    }
48 49 50 51
}

namespace WebCore {

darin's avatar
darin committed
52
class Event;
darin's avatar
darin committed
53
class EventListener;
54
class HTMLPlugInElement;
darin's avatar
darin committed
55
class Frame;
darin's avatar
darin committed
56
class Node;
57
class ScriptSourceCode;
58
class ScriptValue;
darin's avatar
darin committed
59
class String;
60
class Widget;
abarth@webkit.org's avatar
abarth@webkit.org committed
61
class XSSAuditor;
62

63
typedef HashMap<void*, RefPtr<JSC::Bindings::RootObject> > RootObjectMap;
kocienda's avatar
kocienda committed
64

darin@apple.com's avatar
darin@apple.com committed
65
class ScriptController {
66
    friend class ScriptCachedFrameData;
67
    typedef WTF::HashMap< RefPtr<DOMWrapperWorld>, JSC::ProtectedPtr<JSDOMWindowShell> > ShellMap;
68

kocienda's avatar
kocienda committed
69
public:
darin@apple.com's avatar
darin@apple.com committed
70 71
    ScriptController(Frame*);
    ~ScriptController();
ggaren's avatar
ggaren committed
72

73 74
    static PassRefPtr<DOMWrapperWorld> createWorld();

75
    JSDOMWindowShell* windowShell(DOMWrapperWorld* world)
weinig@apple.com's avatar
weinig@apple.com committed
76
    {
77 78
        ShellMap::iterator iter = m_windowShells.find(world);
        return (iter != m_windowShells.end()) ? iter->second.get() : initScript(world);
weinig@apple.com's avatar
weinig@apple.com committed
79
    }
80 81 82 83 84 85
    JSDOMWindowShell* existingWindowShell(DOMWrapperWorld* world) const
    {
        ShellMap::const_iterator iter = m_windowShells.find(world);
        return (iter != m_windowShells.end()) ? iter->second.get() : 0;
    }
    JSDOMWindow* globalObject(DOMWrapperWorld* world)
ggaren@apple.com's avatar
ggaren@apple.com committed
86
    {
87 88
        return windowShell(world)->window();
    }
89

90 91
    ScriptValue executeScript(const ScriptSourceCode&);
    ScriptValue executeScript(const String& script, bool forceUserGesture = false);
92
    ScriptValue executeScriptInWorld(DOMWrapperWorld* world, const String& script, bool forceUserGesture = false);
93 94 95 96

    // Returns true if argument is a JavaScript URL.
    bool executeIfJavaScriptURL(const KURL&, bool userGesture = false, bool replaceDocument = true);

97 98 99 100
    // This function must be called from the main thread. It is safe to call it repeatedly.
    // Darwin is an exception to this rule: it is OK to call this function from any thread, even reentrantly.
    static void initializeThreading();

101
    ScriptValue evaluate(const ScriptSourceCode&);
102
    ScriptValue evaluateInWorld(const ScriptSourceCode&, DOMWrapperWorld*);
103

104 105
    void setEventHandlerLineNumber(int lineno) { m_handlerLineNumber = lineno; }
    int eventHandlerLineNumber() { return m_handlerLineNumber; }
kocienda's avatar
kocienda committed
106

ggaren@apple.com's avatar
ggaren@apple.com committed
107 108
    void setProcessingTimerCallback(bool b) { m_processingTimerCallback = b; }
    bool processingUserGesture() const;
ggaren@apple.com's avatar
ggaren@apple.com committed
109
    bool anyPageIsProcessingUserGesture() const;
ggaren@apple.com's avatar
ggaren@apple.com committed
110

alice.liu@apple.com's avatar
alice.liu@apple.com committed
111 112
    bool isEnabled();

113
    void attachDebugger(JSC::Debugger*);
114

115 116 117
    void setPaused(bool b) { m_paused = b; }
    bool isPaused() const { return m_paused; }

andersca@apple.com's avatar
andersca@apple.com committed
118 119 120
    void setAllowPopupsFromPlugin(bool allowPopupsFromPlugin) { m_allowPopupsFromPlugin = allowPopupsFromPlugin; }
    bool allowPopupsFromPlugin() const { return m_allowPopupsFromPlugin; }
    
121 122 123
    const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script

    void clearWindowShell();
124 125
    void updateDocument();

126 127 128 129 130 131
    // Notifies the ScriptController that the securityOrigin of the current
    // document was modified.  For example, this method is called when
    // document.domain is set.  This method is *not* called when a new document
    // is attached to a frame because updateDocument() is called instead.
    void updateSecurityOrigin();

132 133 134
    void clearScriptObjects();
    void cleanupScriptObjectsForPlugin(void*);

135 136
    void updatePlatformScriptObjects();

137
    PassScriptInstance createScriptInstanceForWidget(Widget*);
138
    JSC::Bindings::RootObject* bindingRootObject();
139

140
    PassRefPtr<JSC::Bindings::RootObject> createRootObject(void* nativeHandle);
141 142 143 144 145 146 147 148

#if PLATFORM(MAC)
#if ENABLE(MAC_JAVA_BRIDGE)
    static void initJavaJSBindings();
#endif
    WebScriptObject* windowScriptObject();
#endif

andersca@apple.com's avatar
andersca@apple.com committed
149 150
    JSC::JSObject* jsObjectForPluginElement(HTMLPlugInElement*);
    
151 152 153 154
#if ENABLE(NETSCAPE_PLUGIN_API)
    NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
    NPObject* windowScriptNPObject();
#endif
abarth@webkit.org's avatar
abarth@webkit.org committed
155 156
    
    XSSAuditor* xssAuditor() { return m_XSSAuditor.get(); }
157

eseidel's avatar
eseidel committed
158
private:
159
    JSDOMWindowShell* initScript(DOMWrapperWorld* world);
ggaren@apple.com's avatar
ggaren@apple.com committed
160

161 162
    void disconnectPlatformScriptObjects();

ggaren@apple.com's avatar
ggaren@apple.com committed
163 164 165
    bool processingUserGestureEvent() const;
    bool isJavaScriptAnchorNavigation() const;

166
    ShellMap m_windowShells;
darin's avatar
darin committed
167
    Frame* m_frame;
168
    int m_handlerLineNumber;
169 170
    const String* m_sourceURL;

171 172
    bool m_inExecuteScript;

ggaren@apple.com's avatar
ggaren@apple.com committed
173
    bool m_processingTimerCallback;
174
    bool m_paused;
andersca@apple.com's avatar
andersca@apple.com committed
175
    bool m_allowPopupsFromPlugin;
176 177

    // The root object used for objects bound outside the context of a plugin.
178
    RefPtr<JSC::Bindings::RootObject> m_bindingRootObject;
179 180 181 182 183 184 185
    RootObjectMap m_rootObjects;
#if ENABLE(NETSCAPE_PLUGIN_API)
    NPObject* m_windowScriptNPObject;
#endif
#if PLATFORM(MAC)
    RetainPtr<WebScriptObject> m_windowScriptObject;
#endif
abarth@webkit.org's avatar
abarth@webkit.org committed
186 187 188
    
    // The XSSAuditor associated with this ScriptController.
    OwnPtr<XSSAuditor> m_XSSAuditor;
189
};
kocienda's avatar
kocienda committed
190

weinig@apple.com's avatar
weinig@apple.com committed
191
} // namespace WebCore
192

darin@apple.com's avatar
darin@apple.com committed
193
#endif // ScriptController_h