Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / workers / WorkerGlobalScope.h
1 /*
2  * Copyright (C) 2008, 2009 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 WorkerGlobalScope_h
28 #define WorkerGlobalScope_h
29
30 #include "bindings/v8/ScriptWrappable.h"
31 #include "bindings/v8/WorkerScriptController.h"
32 #include "core/dom/ExecutionContext.h"
33 #include "core/events/EventListener.h"
34 #include "core/events/EventTarget.h"
35 #include "core/frame/DOMWindowBase64.h"
36 #include "core/frame/csp/ContentSecurityPolicy.h"
37 #include "core/workers/WorkerConsole.h"
38 #include "core/workers/WorkerEventQueue.h"
39 #include "platform/heap/Handle.h"
40 #include "platform/network/ContentSecurityPolicyParsers.h"
41 #include "wtf/Assertions.h"
42 #include "wtf/HashMap.h"
43 #include "wtf/OwnPtr.h"
44 #include "wtf/PassRefPtr.h"
45 #include "wtf/RefCounted.h"
46 #include "wtf/RefPtr.h"
47 #include "wtf/text/AtomicStringHash.h"
48
49 namespace WebCore {
50
51     class Blob;
52     class ExceptionState;
53     class ScheduledAction;
54     class WorkerClients;
55     class WorkerConsole;
56     class WorkerInspectorController;
57     class WorkerLocation;
58     class WorkerNavigator;
59     class WorkerThread;
60
61     class WorkerGlobalScope : public RefCountedWillBeRefCountedGarbageCollected<WorkerGlobalScope>, public ScriptWrappable, public SecurityContext, public ExecutionContext, public ExecutionContextClient, public WillBeHeapSupplementable<WorkerGlobalScope>, public EventTargetWithInlineData, public DOMWindowBase64 {
62         WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(WorkerGlobalScope);
63         DEFINE_EVENT_TARGET_REFCOUNTING(RefCountedWillBeRefCountedGarbageCollected<WorkerGlobalScope>);
64     public:
65         virtual ~WorkerGlobalScope();
66
67         virtual bool isWorkerGlobalScope() const OVERRIDE FINAL { return true; }
68
69         virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
70
71         virtual bool isSharedWorkerGlobalScope() const { return false; }
72         virtual bool isDedicatedWorkerGlobalScope() const { return false; }
73         virtual bool isServiceWorkerGlobalScope() const { return false; }
74
75         const KURL& url() const { return m_url; }
76         KURL completeURL(const String&) const;
77
78         virtual String userAgent(const KURL&) const OVERRIDE FINAL;
79         virtual void disableEval(const String& errorMessage) OVERRIDE FINAL;
80
81         WorkerScriptController* script() { return m_script.get(); }
82         void clearScript() { m_script.clear(); }
83         void clearInspector();
84
85         // FIXME: We can remove this interface when we remove openDatabaseSync.
86         class TerminationObserver {
87         public:
88             virtual ~TerminationObserver() { }
89             // The function is probably called in the main thread.
90             virtual void wasRequestedToTerminate() = 0;
91         };
92         void registerTerminationObserver(TerminationObserver*);
93         void unregisterTerminationObserver(TerminationObserver*);
94         void wasRequestedToTerminate();
95
96         void dispose();
97
98         WorkerThread* thread() const { return m_thread; }
99
100         virtual void postTask(PassOwnPtr<ExecutionContextTask>) OVERRIDE FINAL; // Executes the task on context's thread asynchronously.
101
102         // WorkerGlobalScope
103         WorkerGlobalScope* self() { return this; }
104         WorkerConsole* console();
105         WorkerLocation* location() const;
106         void close();
107
108         DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
109
110         // WorkerUtils
111         virtual void importScripts(const Vector<String>& urls, ExceptionState&);
112         WorkerNavigator* navigator() const;
113
114         // ExecutionContextClient
115         virtual WorkerEventQueue* eventQueue() const OVERRIDE FINAL;
116         virtual SecurityContext& securityContext() OVERRIDE FINAL { return *this; }
117
118         virtual bool isContextThread() const OVERRIDE FINAL;
119         virtual bool isJSExecutionForbidden() const OVERRIDE FINAL;
120
121         virtual double timerAlignmentInterval() const OVERRIDE FINAL;
122
123         WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); }
124         // These methods are used for GC marking. See JSWorkerGlobalScope::visitChildrenVirtual(SlotVisitor&) in
125         // JSWorkerGlobalScopeCustom.cpp.
126         WorkerConsole* optionalConsole() const { return m_console.get(); }
127         WorkerNavigator* optionalNavigator() const { return m_navigator.get(); }
128         WorkerLocation* optionalLocation() const { return m_location.get(); }
129
130         bool isClosing() { return m_closing; }
131
132         bool idleNotification();
133
134         double timeOrigin() const { return m_timeOrigin; }
135
136         WorkerClients* clients() { return m_workerClients.get(); }
137
138         using SecurityContext::securityOrigin;
139         using SecurityContext::contentSecurityPolicy;
140
141         virtual void trace(Visitor*);
142
143     protected:
144         WorkerGlobalScope(const KURL&, const String& userAgent, WorkerThread*, double timeOrigin, PassOwnPtrWillBeRawPtr<WorkerClients>);
145         void applyContentSecurityPolicyFromString(const String& contentSecurityPolicy, ContentSecurityPolicyHeaderType);
146
147         virtual void logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtr<ScriptCallStack>) OVERRIDE;
148         void addMessageToWorkerConsole(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, PassRefPtr<ScriptCallStack>, ScriptState*);
149
150     private:
151         virtual void refExecutionContext() OVERRIDE FINAL { ref(); }
152         virtual void derefExecutionContext() OVERRIDE FINAL { deref(); }
153
154         virtual const KURL& virtualURL() const OVERRIDE FINAL;
155         virtual KURL virtualCompleteURL(const String&) const OVERRIDE FINAL;
156
157         virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) OVERRIDE FINAL;
158         virtual void addMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, ScriptState* = 0) OVERRIDE FINAL;
159
160         virtual EventTarget* errorEventTarget() OVERRIDE FINAL;
161         virtual void didUpdateSecurityOrigin() OVERRIDE FINAL { }
162
163         KURL m_url;
164         String m_userAgent;
165
166         mutable RefPtrWillBeMember<WorkerConsole> m_console;
167         mutable RefPtrWillBeMember<WorkerLocation> m_location;
168         mutable RefPtrWillBeMember<WorkerNavigator> m_navigator;
169
170         OwnPtr<WorkerScriptController> m_script;
171         WorkerThread* m_thread;
172
173         OwnPtr<WorkerInspectorController> m_workerInspectorController;
174         bool m_closing;
175
176         OwnPtr<WorkerEventQueue> m_eventQueue;
177
178         OwnPtrWillBeMember<WorkerClients> m_workerClients;
179
180         double m_timeOrigin;
181         TerminationObserver* m_terminationObserver;
182     };
183
184 DEFINE_TYPE_CASTS(WorkerGlobalScope, ExecutionContext, context, context->isWorkerGlobalScope(), context.isWorkerGlobalScope());
185
186 } // namespace WebCore
187
188 #endif // WorkerGlobalScope_h