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/events/ThreadLocalEventNames.h" 36 #include "core/frame/ContentSecurityPolicy.h" 37 #include "core/workers/WorkerConsole.h" 38 #include "core/workers/WorkerEventQueue.h" 39 #include "core/workers/WorkerSupplementable.h" 40 #include "wtf/Assertions.h" 41 #include "wtf/HashMap.h" 42 #include "wtf/OwnPtr.h" 43 #include "wtf/PassRefPtr.h" 44 #include "wtf/RefCounted.h" 45 #include "wtf/RefPtr.h" 46 #include "wtf/text/AtomicStringHash.h" 47 48 namespace WebCore { 49 50 class Blob; 51 class DOMURL; 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 RefCounted<WorkerGlobalScope>, public ScriptWrappable, public SecurityContext, public ExecutionContext, public ExecutionContextClient, public WorkerSupplementable, public EventTargetWithInlineData { 62 REFCOUNTED_EVENT_TARGET(WorkerGlobalScope); 63 public: 64 virtual ~WorkerGlobalScope(); 65 66 virtual bool isWorkerGlobalScope() const OVERRIDE { return true; } 67 68 virtual ExecutionContext* executionContext() const OVERRIDE; 69 70 virtual bool isSharedWorkerGlobalScope() const { return false; } 71 virtual bool isDedicatedWorkerGlobalScope() const { return false; } 72 virtual bool isServiceWorkerGlobalScope() const { return false; } 73 74 const KURL& url() const { return m_url; } 75 KURL completeURL(const String&) const; 76 77 virtual String userAgent(const KURL&) const; 78 virtual void disableEval(const String& errorMessage) OVERRIDE; 79 80 WorkerScriptController* script() { return m_script.get(); } 81 void clearScript() { m_script.clear(); } 82 void clearInspector(); 83 84 WorkerThread* thread() const { return m_thread; } 85 86 virtual void postTask(PassOwnPtr<ExecutionContextTask>) OVERRIDE; // Executes the task on context's thread asynchronously. 87 88 // WorkerGlobalScope 89 WorkerGlobalScope* self() { return this; } 90 WorkerConsole* console(); 91 WorkerLocation* location() const; 92 void close(); 93 94 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 95 96 // WorkerUtils 97 virtual void importScripts(const Vector<String>& urls, ExceptionState&); 98 WorkerNavigator* navigator() const; 99 100 // ExecutionContextClient 101 virtual WorkerEventQueue* eventQueue() const OVERRIDE; 102 virtual SecurityContext& securityContext() OVERRIDE { return *this; } 103 104 virtual bool isContextThread() const OVERRIDE; 105 virtual bool isJSExecutionForbidden() const OVERRIDE; 106 107 virtual double timerAlignmentInterval() const OVERRIDE; 108 109 WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); } 110 // These methods are used for GC marking. See JSWorkerGlobalScope::visitChildrenVirtual(SlotVisitor&) in 111 // JSWorkerGlobalScopeCustom.cpp. 112 WorkerConsole* optionalConsole() const { return m_console.get(); } 113 WorkerNavigator* optionalNavigator() const { return m_navigator.get(); } 114 WorkerLocation* optionalLocation() const { return m_location.get(); } 115 116 bool isClosing() { return m_closing; } 117 118 // An observer interface to be notified when the worker thread is getting stopped. 119 class Observer { 120 WTF_MAKE_NONCOPYABLE(Observer); 121 public: 122 Observer(WorkerGlobalScope*); 123 virtual ~Observer(); 124 virtual void notifyStop() = 0; 125 void stopObserving(); 126 private: 127 WorkerGlobalScope* m_context; 128 }; 129 friend class Observer; 130 void registerObserver(Observer*); 131 void unregisterObserver(Observer*); 132 void notifyObserversOfStop(); 133 134 bool idleNotification(); 135 136 double timeOrigin() const { return m_timeOrigin; } 137 138 WorkerClients* clients() { return m_workerClients.get(); } 139 140 using SecurityContext::securityOrigin; 141 using SecurityContext::contentSecurityPolicy; 142 143 protected: 144 WorkerGlobalScope(const KURL&, const String& userAgent, WorkerThread*, double timeOrigin, PassOwnPtr<WorkerClients>); 145 void applyContentSecurityPolicyFromString(const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType); 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 { ref(); } 152 virtual void derefExecutionContext() OVERRIDE { deref(); } 153 154 virtual const KURL& virtualURL() const OVERRIDE; 155 virtual KURL virtualCompleteURL(const String&) const; 156 157 virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) OVERRIDE; 158 virtual void addMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, ScriptState* = 0) OVERRIDE; 159 160 virtual EventTarget* errorEventTarget() OVERRIDE; 161 virtual void didUpdateSecurityOrigin() OVERRIDE { } 162 163 KURL m_url; 164 String m_userAgent; 165 166 mutable RefPtr<WorkerConsole> m_console; 167 mutable RefPtr<WorkerLocation> m_location; 168 mutable RefPtr<WorkerNavigator> m_navigator; 169 170 OwnPtr<WorkerScriptController> m_script; 171 WorkerThread* m_thread; 172 173 mutable RefPtr<DOMURL> m_domURL; 174 OwnPtr<WorkerInspectorController> m_workerInspectorController; 175 bool m_closing; 176 177 HashSet<Observer*> m_workerObservers; 178 179 OwnPtr<WorkerEventQueue> m_eventQueue; 180 181 OwnPtr<WorkerClients> m_workerClients; 182 183 double m_timeOrigin; 184 }; 185 186 DEFINE_TYPE_CASTS(WorkerGlobalScope, ExecutionContext, context, context->isWorkerGlobalScope(), context.isWorkerGlobalScope()); 187 188 } // namespace WebCore 189 190 #endif // WorkerGlobalScope_h 191