2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef ScriptExecutionContext_h
28 #define ScriptExecutionContext_h
30 #include "ActiveDOMObject.h"
31 #include "ConsoleTypes.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>
44 #include <runtime/JSGlobalData.h>
54 class ScriptCallStack;
56 #if ENABLE(SQL_DATABASE)
58 class DatabaseTaskSynchronizer;
62 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
66 class ScriptExecutionContext : public SecurityContext {
68 ScriptExecutionContext();
69 virtual ~ScriptExecutionContext();
71 virtual bool isDocument() const { return false; }
72 virtual bool isWorkerContext() const { return false; }
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*);
83 virtual bool isContextThread() const = 0;
84 virtual bool isJSExecutionForbidden() const = 0;
86 const KURL& url() const { return virtualURL(); }
87 KURL completeURL(const String& url) const { return virtualCompleteURL(url); }
89 virtual String userAgent(const KURL&) const = 0;
91 virtual void disableEval() = 0;
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);
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();
106 void didCreateActiveDOMObject(ActiveDOMObject*, void* upcastPointer);
107 void willDestroyActiveDOMObject(ActiveDOMObject*);
109 typedef const HashMap<ActiveDOMObject*, void*> ActiveDOMObjectsMap;
110 ActiveDOMObjectsMap& activeDOMObjects() const { return m_activeDOMObjects; }
112 void didCreateDestructionObserver(ContextDestructionObserver*);
113 void willDestroyDestructionObserver(ContextDestructionObserver*);
115 virtual void suspendScriptedAnimationControllerCallbacks() { }
116 virtual void resumeScriptedAnimationControllerCallbacks() { }
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; }
125 void ref() { refScriptExecutionContext(); }
126 void deref() { derefScriptExecutionContext(); }
129 WTF_MAKE_NONCOPYABLE(Task);
130 WTF_MAKE_FAST_ALLOCATED;
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; }
139 virtual void postTask(PassOwnPtr<Task>) = 0; // Executes the task on context's thread asynchronously.
141 void addTimeout(int timeoutId, DOMTimer*);
142 void removeTimeout(int timeoutId);
143 DOMTimer* findTimeout(int timeoutId);
146 JSC::JSGlobalData* globalData();
149 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
150 FileThread* fileThread();
151 void stopFileThread();
154 // Interval is in seconds.
155 void adjustMinimumTimerInterval(double oldMinimumTimerInterval);
156 virtual double minimumTimerInterval() const;
158 virtual EventQueue* eventQueue() const = 0;
161 class AddConsoleMessageTask : public Task {
163 static PassOwnPtr<AddConsoleMessageTask> create(MessageSource source, MessageType type, MessageLevel level, const String& message)
165 return adoptPtr(new AddConsoleMessageTask(source, type, level, message));
167 virtual void performTask(ScriptExecutionContext*);
169 AddConsoleMessageTask(MessageSource source, MessageType type, MessageLevel level, const String& message)
173 , m_message(message.isolatedCopy())
176 MessageSource m_source;
178 MessageLevel m_level;
183 virtual const KURL& virtualURL() const = 0;
184 virtual KURL virtualCompleteURL(const String&) const = 0;
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);
190 void closeMessagePorts();
192 HashSet<MessagePort*> m_messagePorts;
193 HashSet<ContextDestructionObserver*> m_destructionObservers;
194 HashMap<ActiveDOMObject*, void*> m_activeDOMObjects;
195 bool m_iteratingActiveDOMObjects;
198 typedef HashMap<int, DOMTimer*> TimeoutMap;
199 TimeoutMap m_timeouts;
201 virtual void refScriptExecutionContext() = 0;
202 virtual void derefScriptExecutionContext() = 0;
204 bool m_inDispatchErrorEvent;
205 class PendingException;
206 OwnPtr<Vector<OwnPtr<PendingException> > > m_pendingExceptions;
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.
213 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
214 RefPtr<FileThread> m_fileThread;
218 } // namespace WebCore
220 #endif // ScriptExecutionContext_h