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/core/v8/WorkerScriptController.h"
31 #include "core/dom/ExecutionContext.h"
32 #include "core/events/EventListener.h"
33 #include "core/events/EventTarget.h"
34 #include "core/frame/DOMWindowBase64.h"
35 #include "core/frame/UseCounter.h"
36 #include "core/frame/csp/ContentSecurityPolicy.h"
37 #include "core/inspector/ConsoleMessage.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 SecurityContext, public ExecutionContext, public ExecutionContextClient, public WillBeHeapSupplementable<WorkerGlobalScope>, public EventTargetWithInlineData, public DOMWindowBase64 {
62 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(WorkerGlobalScope);
63 REFCOUNTED_EVENT_TARGET(WorkerGlobalScope);
65 virtual ~WorkerGlobalScope();
67 virtual bool isWorkerGlobalScope() const OVERRIDE FINAL { return true; }
68 virtual bool isSharedWorkerGlobalScope() const OVERRIDE { return false; }
69 virtual bool isDedicatedWorkerGlobalScope() const OVERRIDE { return false; }
70 virtual bool isServiceWorkerGlobalScope() const OVERRIDE { return false; }
72 virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
74 virtual void countFeature(UseCounter::Feature) const;
75 virtual void countDeprecation(UseCounter::Feature) const;
77 const KURL& url() const { return m_url; }
78 KURL completeURL(const String&) const;
80 virtual String userAgent(const KURL&) const OVERRIDE FINAL;
81 virtual void disableEval(const String& errorMessage) OVERRIDE FINAL;
83 WorkerScriptController* script() { return m_script.get(); }
84 void clearScript() { m_script.clear(); }
85 void clearInspector();
87 // FIXME: We can remove this interface when we remove openDatabaseSync.
88 class TerminationObserver {
90 virtual ~TerminationObserver() { }
91 // The function is probably called in the main thread.
92 virtual void wasRequestedToTerminate() = 0;
94 void registerTerminationObserver(TerminationObserver*);
95 void unregisterTerminationObserver(TerminationObserver*);
96 void wasRequestedToTerminate();
100 WorkerThread* thread() const { return m_thread; }
102 virtual void postTask(PassOwnPtr<ExecutionContextTask>) OVERRIDE FINAL; // Executes the task on context's thread asynchronously.
105 WorkerGlobalScope* self() { return this; }
106 WorkerConsole* console();
107 WorkerLocation* location() const;
110 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
113 virtual void importScripts(const Vector<String>& urls, ExceptionState&);
114 WorkerNavigator* navigator() const;
116 // ExecutionContextClient
117 virtual WorkerEventQueue* eventQueue() const OVERRIDE FINAL;
118 virtual SecurityContext& securityContext() OVERRIDE FINAL { return *this; }
120 virtual bool isContextThread() const OVERRIDE FINAL;
121 virtual bool isJSExecutionForbidden() const OVERRIDE FINAL;
123 virtual double timerAlignmentInterval() const OVERRIDE FINAL;
125 WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); }
127 bool isClosing() { return m_closing; }
129 virtual void stopFetch() { }
131 bool idleNotification();
133 double timeOrigin() const { return m_timeOrigin; }
135 WorkerClients* clients() { return m_workerClients.get(); }
137 using SecurityContext::securityOrigin;
138 using SecurityContext::contentSecurityPolicy;
140 virtual void trace(Visitor*) OVERRIDE;
143 WorkerGlobalScope(const KURL&, const String& userAgent, WorkerThread*, double timeOrigin, PassOwnPtrWillBeRawPtr<WorkerClients>);
144 void applyContentSecurityPolicyFromString(const String& contentSecurityPolicy, ContentSecurityPolicyHeaderType);
146 virtual void logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtrWillBeRawPtr<ScriptCallStack>) OVERRIDE;
147 void addMessageToWorkerConsole(PassRefPtrWillBeRawPtr<ConsoleMessage>);
151 virtual void refExecutionContext() OVERRIDE FINAL { ref(); }
152 virtual void derefExecutionContext() OVERRIDE FINAL { deref(); }
155 virtual const KURL& virtualURL() const OVERRIDE FINAL;
156 virtual KURL virtualCompleteURL(const String&) const OVERRIDE FINAL;
158 virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) OVERRIDE FINAL;
159 virtual void addMessage(PassRefPtrWillBeRawPtr<ConsoleMessage>) OVERRIDE FINAL;
161 virtual EventTarget* errorEventTarget() OVERRIDE FINAL;
162 virtual void didUpdateSecurityOrigin() OVERRIDE FINAL { }
167 mutable RefPtrWillBeMember<WorkerConsole> m_console;
168 mutable RefPtrWillBeMember<WorkerLocation> m_location;
169 mutable RefPtrWillBeMember<WorkerNavigator> m_navigator;
171 OwnPtr<WorkerScriptController> m_script;
172 WorkerThread* m_thread;
174 RefPtrWillBeMember<WorkerInspectorController> m_workerInspectorController;
177 OwnPtrWillBeMember<WorkerEventQueue> m_eventQueue;
179 OwnPtrWillBeMember<WorkerClients> m_workerClients;
182 TerminationObserver* m_terminationObserver;
185 DEFINE_TYPE_CASTS(WorkerGlobalScope, ExecutionContext, context, context->isWorkerGlobalScope(), context.isWorkerGlobalScope());
189 #endif // WorkerGlobalScope_h