2 * Copyright (C) 2008, 2009 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 WorkerGlobalScope_h
28 #define WorkerGlobalScope_h
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"
53 class ScheduledAction;
56 class WorkerInspectorController;
58 class WorkerNavigator;
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>);
65 virtual ~WorkerGlobalScope();
67 virtual bool isWorkerGlobalScope() const OVERRIDE FINAL { return true; }
69 virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
71 virtual bool isSharedWorkerGlobalScope() const { return false; }
72 virtual bool isDedicatedWorkerGlobalScope() const { return false; }
73 virtual bool isServiceWorkerGlobalScope() const { return false; }
75 const KURL& url() const { return m_url; }
76 KURL completeURL(const String&) const;
78 virtual String userAgent(const KURL&) const OVERRIDE FINAL;
79 virtual void disableEval(const String& errorMessage) OVERRIDE FINAL;
81 WorkerScriptController* script() { return m_script.get(); }
82 void clearScript() { m_script.clear(); }
83 void clearInspector();
85 // FIXME: We can remove this interface when we remove openDatabaseSync.
86 class TerminationObserver {
88 virtual ~TerminationObserver() { }
89 // The function is probably called in the main thread.
90 virtual void wasRequestedToTerminate() = 0;
92 void registerTerminationObserver(TerminationObserver*);
93 void unregisterTerminationObserver(TerminationObserver*);
94 void wasRequestedToTerminate();
98 WorkerThread* thread() const { return m_thread; }
100 virtual void postTask(PassOwnPtr<ExecutionContextTask>) OVERRIDE FINAL; // Executes the task on context's thread asynchronously.
103 WorkerGlobalScope* self() { return this; }
104 WorkerConsole* console();
105 WorkerLocation* location() const;
108 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
111 virtual void importScripts(const Vector<String>& urls, ExceptionState&);
112 WorkerNavigator* navigator() const;
114 // ExecutionContextClient
115 virtual WorkerEventQueue* eventQueue() const OVERRIDE FINAL;
116 virtual SecurityContext& securityContext() OVERRIDE FINAL { return *this; }
118 virtual bool isContextThread() const OVERRIDE FINAL;
119 virtual bool isJSExecutionForbidden() const OVERRIDE FINAL;
121 virtual double timerAlignmentInterval() const OVERRIDE FINAL;
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(); }
130 bool isClosing() { return m_closing; }
132 bool idleNotification();
134 double timeOrigin() const { return m_timeOrigin; }
136 WorkerClients* clients() { return m_workerClients.get(); }
138 using SecurityContext::securityOrigin;
139 using SecurityContext::contentSecurityPolicy;
141 virtual void trace(Visitor*);
144 WorkerGlobalScope(const KURL&, const String& userAgent, WorkerThread*, double timeOrigin, PassOwnPtrWillBeRawPtr<WorkerClients>);
145 void applyContentSecurityPolicyFromString(const String& contentSecurityPolicy, ContentSecurityPolicyHeaderType);
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*);
151 virtual void refExecutionContext() OVERRIDE FINAL { ref(); }
152 virtual void derefExecutionContext() OVERRIDE FINAL { deref(); }
154 virtual const KURL& virtualURL() const OVERRIDE FINAL;
155 virtual KURL virtualCompleteURL(const String&) const OVERRIDE FINAL;
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;
160 virtual EventTarget* errorEventTarget() OVERRIDE FINAL;
161 virtual void didUpdateSecurityOrigin() OVERRIDE FINAL { }
166 mutable RefPtrWillBeMember<WorkerConsole> m_console;
167 mutable RefPtrWillBeMember<WorkerLocation> m_location;
168 mutable RefPtrWillBeMember<WorkerNavigator> m_navigator;
170 OwnPtr<WorkerScriptController> m_script;
171 WorkerThread* m_thread;
173 OwnPtr<WorkerInspectorController> m_workerInspectorController;
176 OwnPtr<WorkerEventQueue> m_eventQueue;
178 OwnPtrWillBeMember<WorkerClients> m_workerClients;
181 TerminationObserver* m_terminationObserver;
184 DEFINE_TYPE_CASTS(WorkerGlobalScope, ExecutionContext, context, context->isWorkerGlobalScope(), context.isWorkerGlobalScope());
186 } // namespace WebCore
188 #endif // WorkerGlobalScope_h