|
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 "Console.h" |
|
31 #include "KURL.h" |
|
32 #include <wtf/HashMap.h> |
|
33 #include <wtf/HashSet.h> |
|
34 #include <wtf/PassOwnPtr.h> |
|
35 #include <wtf/PassRefPtr.h> |
|
36 #include <wtf/RefPtr.h> |
|
37 #include <wtf/Threading.h> |
|
38 |
|
39 namespace WebCore { |
|
40 |
|
41 class ActiveDOMObject; |
|
42 #if ENABLE(DATABASE) |
|
43 class Database; |
|
44 class DatabaseTaskSynchronizer; |
|
45 class DatabaseThread; |
|
46 #endif |
|
47 class DOMTimer; |
|
48 #if ENABLE(FILE_READER) || ENABLE(FILE_WRITER) |
|
49 class FileThread; |
|
50 #endif |
|
51 class MessagePort; |
|
52 class SecurityOrigin; |
|
53 class ScriptString; |
|
54 class String; |
|
55 #if ENABLE(INSPECTOR) |
|
56 class InspectorController; |
|
57 #endif |
|
58 |
|
59 class ScriptExecutionContext { |
|
60 public: |
|
61 ScriptExecutionContext(); |
|
62 virtual ~ScriptExecutionContext(); |
|
63 |
|
64 virtual bool isDocument() const { return false; } |
|
65 virtual bool isWorkerContext() const { return false; } |
|
66 |
|
67 #if ENABLE(DATABASE) |
|
68 virtual bool isDatabaseReadOnly() const = 0; |
|
69 virtual void databaseExceededQuota(const String& name) = 0; |
|
70 DatabaseThread* databaseThread(); |
|
71 void setHasOpenDatabases() { m_hasOpenDatabases = true; } |
|
72 bool hasOpenDatabases() const { return m_hasOpenDatabases; } |
|
73 // When the database cleanup is done, cleanupSync will be signalled. |
|
74 void stopDatabases(DatabaseTaskSynchronizer*); |
|
75 #endif |
|
76 virtual bool isContextThread() const = 0; |
|
77 virtual bool isJSExecutionTerminated() const = 0; |
|
78 |
|
79 const KURL& url() const { return virtualURL(); } |
|
80 KURL completeURL(const String& url) const { return virtualCompleteURL(url); } |
|
81 |
|
82 virtual String userAgent(const KURL&) const = 0; |
|
83 |
|
84 SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); } |
|
85 #if ENABLE(INSPECTOR) |
|
86 virtual InspectorController* inspectorController() const { return 0; } |
|
87 #endif |
|
88 |
|
89 virtual void reportException(const String& errorMessage, int lineNumber, const String& sourceURL) = 0; |
|
90 virtual void addMessage(MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceURL) = 0; |
|
91 |
|
92 // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked. |
|
93 bool canSuspendActiveDOMObjects(); |
|
94 // Active objects can be asked to suspend even if canSuspendActiveDOMObjects() returns 'false' - |
|
95 // step-by-step JS debugging is one example. |
|
96 void suspendActiveDOMObjects(); |
|
97 void resumeActiveDOMObjects(); |
|
98 void stopActiveDOMObjects(); |
|
99 void createdActiveDOMObject(ActiveDOMObject*, void* upcastPointer); |
|
100 void destroyedActiveDOMObject(ActiveDOMObject*); |
|
101 typedef const HashMap<ActiveDOMObject*, void*> ActiveDOMObjectsMap; |
|
102 ActiveDOMObjectsMap& activeDOMObjects() const { return m_activeDOMObjects; } |
|
103 |
|
104 // MessagePort is conceptually a kind of ActiveDOMObject, but it needs to be tracked separately for message dispatch. |
|
105 void processMessagePortMessagesSoon(); |
|
106 void dispatchMessagePortEvents(); |
|
107 void createdMessagePort(MessagePort*); |
|
108 void destroyedMessagePort(MessagePort*); |
|
109 const HashSet<MessagePort*>& messagePorts() const { return m_messagePorts; } |
|
110 |
|
111 void ref() { refScriptExecutionContext(); } |
|
112 void deref() { derefScriptExecutionContext(); } |
|
113 |
|
114 class Task : public Noncopyable { |
|
115 public: |
|
116 virtual ~Task(); |
|
117 virtual void performTask(ScriptExecutionContext*) = 0; |
|
118 // Certain tasks get marked specially so that they aren't discarded, and are executed, when the context is shutting down its message queue. |
|
119 virtual bool isCleanupTask() const { return false; } |
|
120 }; |
|
121 |
|
122 virtual void postTask(PassOwnPtr<Task>) = 0; // Executes the task on context's thread asynchronously. |
|
123 |
|
124 void addTimeout(int timeoutId, DOMTimer*); |
|
125 void removeTimeout(int timeoutId); |
|
126 DOMTimer* findTimeout(int timeoutId); |
|
127 |
|
128 #if USE(JSC) |
|
129 JSC::JSGlobalData* globalData(); |
|
130 #endif |
|
131 |
|
132 #if ENABLE(FILE_READER) || ENABLE(FILE_WRITER) |
|
133 FileThread* fileThread(); |
|
134 void stopFileThread(); |
|
135 #endif |
|
136 |
|
137 protected: |
|
138 // Explicitly override the security origin for this script context. |
|
139 // Note: It is dangerous to change the security origin of a script context |
|
140 // that already contains content. |
|
141 void setSecurityOrigin(PassRefPtr<SecurityOrigin>); |
|
142 |
|
143 private: |
|
144 virtual const KURL& virtualURL() const = 0; |
|
145 virtual KURL virtualCompleteURL(const String&) const = 0; |
|
146 |
|
147 RefPtr<SecurityOrigin> m_securityOrigin; |
|
148 |
|
149 HashSet<MessagePort*> m_messagePorts; |
|
150 |
|
151 HashMap<ActiveDOMObject*, void*> m_activeDOMObjects; |
|
152 |
|
153 HashMap<int, DOMTimer*> m_timeouts; |
|
154 |
|
155 virtual void refScriptExecutionContext() = 0; |
|
156 virtual void derefScriptExecutionContext() = 0; |
|
157 |
|
158 #if ENABLE(DATABASE) |
|
159 RefPtr<DatabaseThread> m_databaseThread; |
|
160 bool m_hasOpenDatabases; // This never changes back to false, even after the database thread is closed. |
|
161 #endif |
|
162 |
|
163 #if ENABLE(FILE_READER) || ENABLE(FILE_WRITER) |
|
164 RefPtr<FileThread> m_fileThread; |
|
165 #endif |
|
166 }; |
|
167 |
|
168 } // namespace WebCore |
|
169 |
|
170 |
|
171 #endif // ScriptExecutionContext_h |