tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / dom / ScriptExecutionContext.h
1 /*
2  * Copyright (C) 2008 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26
27 #ifndef ScriptExecutionContext_h
28 #define ScriptExecutionContext_h
29
30 #include "ActiveDOMObject.h"
31 #include "ConsoleTypes.h"
32 #include "KURL.h"
33 #include "SecurityContext.h"
34 #include <wtf/Forward.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/HashSet.h>
37 #include <wtf/Noncopyable.h>
38 #include <wtf/OwnPtr.h>
39 #include <wtf/PassOwnPtr.h>
40 #include <wtf/Threading.h>
41 #include <wtf/text/StringHash.h>
42
43 #if USE(JSC)
44 #include <runtime/JSGlobalData.h>
45 #endif
46
47 namespace WebCore {
48
49 class DOMTimer;
50 class EventListener;
51 class EventQueue;
52 class EventTarget;
53 class MessagePort;
54 class ScriptCallStack;
55
56 #if ENABLE(SQL_DATABASE)
57 class Database;
58 class DatabaseTaskSynchronizer;
59 class DatabaseThread;
60 #endif
61
62 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
63 class FileThread;
64 #endif
65
66 class ScriptExecutionContext : public SecurityContext {
67 public:
68     ScriptExecutionContext();
69     virtual ~ScriptExecutionContext();
70
71     virtual bool isDocument() const { return false; }
72     virtual bool isWorkerContext() const { return false; }
73
74 #if ENABLE(SQL_DATABASE)
75     virtual bool allowDatabaseAccess() const = 0;
76     virtual void databaseExceededQuota(const String& name) = 0;
77     DatabaseThread* databaseThread();
78     void setHasOpenDatabases() { m_hasOpenDatabases = true; }
79     bool hasOpenDatabases() const { return m_hasOpenDatabases; }
80     // When the database cleanup is done, cleanupSync will be signalled.
81     void stopDatabases(DatabaseTaskSynchronizer*);
82 #endif
83     virtual bool isContextThread() const = 0;
84     virtual bool isJSExecutionForbidden() const = 0;
85
86     const KURL& url() const { return virtualURL(); }
87     KURL completeURL(const String& url) const { return virtualCompleteURL(url); }
88
89     virtual String userAgent(const KURL&) const = 0;
90
91     virtual void disableEval() = 0;
92
93     bool sanitizeScriptError(String& errorMessage, int& lineNumber, String& sourceURL);
94     void reportException(const String& errorMessage, int lineNumber, const String& sourceURL, PassRefPtr<ScriptCallStack>);
95     virtual void addMessage(MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceURL, PassRefPtr<ScriptCallStack>) = 0;
96     void addConsoleMessage(MessageSource, MessageType, MessageLevel, const String& message);
97
98     // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked.
99     bool canSuspendActiveDOMObjects();
100     // Active objects can be asked to suspend even if canSuspendActiveDOMObjects() returns 'false' -
101     // step-by-step JS debugging is one example.
102     virtual void suspendActiveDOMObjects(ActiveDOMObject::ReasonForSuspension);
103     virtual void resumeActiveDOMObjects();
104     virtual void stopActiveDOMObjects();
105
106     void didCreateActiveDOMObject(ActiveDOMObject*, void* upcastPointer);
107     void willDestroyActiveDOMObject(ActiveDOMObject*);
108
109     typedef const HashMap<ActiveDOMObject*, void*> ActiveDOMObjectsMap;
110     ActiveDOMObjectsMap& activeDOMObjects() const { return m_activeDOMObjects; }
111
112     void didCreateDestructionObserver(ContextDestructionObserver*);
113     void willDestroyDestructionObserver(ContextDestructionObserver*);
114
115     virtual void suspendScriptedAnimationControllerCallbacks() { }
116     virtual void resumeScriptedAnimationControllerCallbacks() { }
117
118     // MessagePort is conceptually a kind of ActiveDOMObject, but it needs to be tracked separately for message dispatch.
119     void processMessagePortMessagesSoon();
120     void dispatchMessagePortEvents();
121     void createdMessagePort(MessagePort*);
122     void destroyedMessagePort(MessagePort*);
123     const HashSet<MessagePort*>& messagePorts() const { return m_messagePorts; }
124
125     void ref() { refScriptExecutionContext(); }
126     void deref() { derefScriptExecutionContext(); }
127
128     class Task {
129         WTF_MAKE_NONCOPYABLE(Task);
130         WTF_MAKE_FAST_ALLOCATED;
131     public:
132         Task() { }
133         virtual ~Task();
134         virtual void performTask(ScriptExecutionContext*) = 0;
135         // Certain tasks get marked specially so that they aren't discarded, and are executed, when the context is shutting down its message queue.
136         virtual bool isCleanupTask() const { return false; }
137     };
138
139     virtual void postTask(PassOwnPtr<Task>) = 0; // Executes the task on context's thread asynchronously.
140
141     void addTimeout(int timeoutId, DOMTimer*);
142     void removeTimeout(int timeoutId);
143     DOMTimer* findTimeout(int timeoutId);
144
145 #if USE(JSC)
146     JSC::JSGlobalData* globalData();
147 #endif
148
149 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
150     FileThread* fileThread();
151     void stopFileThread();
152 #endif
153
154     // Interval is in seconds.
155     void adjustMinimumTimerInterval(double oldMinimumTimerInterval);
156     virtual double minimumTimerInterval() const;
157
158     virtual EventQueue* eventQueue() const = 0;
159
160 protected:
161     class AddConsoleMessageTask : public Task {
162     public:
163         static PassOwnPtr<AddConsoleMessageTask> create(MessageSource source, MessageType type, MessageLevel level, const String& message)
164         {
165             return adoptPtr(new AddConsoleMessageTask(source, type, level, message));
166         }
167         virtual void performTask(ScriptExecutionContext*);
168     private:
169         AddConsoleMessageTask(MessageSource source, MessageType type, MessageLevel level, const String& message)
170             : m_source(source)
171             , m_type(type)
172             , m_level(level)
173             , m_message(message.isolatedCopy())
174         {
175         }
176         MessageSource m_source;
177         MessageType m_type;
178         MessageLevel m_level;
179         String m_message;
180     };
181
182 private:
183     virtual const KURL& virtualURL() const = 0;
184     virtual KURL virtualCompleteURL(const String&) const = 0;
185
186     virtual EventTarget* errorEventTarget() = 0;
187     virtual void logExceptionToConsole(const String& errorMessage, int lineNumber, const String& sourceURL, PassRefPtr<ScriptCallStack>) = 0;
188     bool dispatchErrorEvent(const String& errorMessage, int lineNumber, const String& sourceURL);
189
190     void closeMessagePorts();
191
192     HashSet<MessagePort*> m_messagePorts;
193     HashSet<ContextDestructionObserver*> m_destructionObservers;
194     HashMap<ActiveDOMObject*, void*> m_activeDOMObjects;
195     bool m_iteratingActiveDOMObjects;
196     bool m_inDestructor;
197
198     typedef HashMap<int, DOMTimer*> TimeoutMap;
199     TimeoutMap m_timeouts;
200
201     virtual void refScriptExecutionContext() = 0;
202     virtual void derefScriptExecutionContext() = 0;
203
204     bool m_inDispatchErrorEvent;
205     class PendingException;
206     OwnPtr<Vector<OwnPtr<PendingException> > > m_pendingExceptions;
207
208 #if ENABLE(SQL_DATABASE)
209     RefPtr<DatabaseThread> m_databaseThread;
210     bool m_hasOpenDatabases; // This never changes back to false, even after the database thread is closed.
211 #endif
212
213 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
214     RefPtr<FileThread> m_fileThread;
215 #endif
216 };
217
218 } // namespace WebCore
219
220 #endif // ScriptExecutionContext_h