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 WorkerContext_h 28 #define WorkerContext_h 29 30 #if ENABLE(WORKERS) 31 32 #include "AtomicStringHash.h" 33 #include "Database.h" 34 #include "EventListener.h" 35 #include "EventNames.h" 36 #include "EventTarget.h" 37 #include "ScriptExecutionContext.h" 38 #include "WorkerScriptController.h" 39 #include <wtf/Assertions.h> 40 #include <wtf/OwnPtr.h> 41 #include <wtf/PassRefPtr.h> 42 #include <wtf/RefCounted.h> 43 #include <wtf/RefPtr.h> 44 45 namespace WebCore { 46 47 class Database; 48 class NotificationCenter; 49 class ScheduledAction; 50 class WorkerLocation; 51 class WorkerNavigator; 52 class WorkerThread; 53 54 class WorkerContext : public RefCounted<WorkerContext>, public ScriptExecutionContext, public EventTarget { 55 public: 56 virtual ~WorkerContext(); 57 58 virtual bool isWorkerContext() const { return true; } 59 60 virtual ScriptExecutionContext* scriptExecutionContext() const; 61 62 virtual bool isSharedWorkerContext() const { return false; } 63 virtual bool isDedicatedWorkerContext() const { return false; } 64 65 const KURL& url() const { return m_url; } 66 KURL completeURL(const String&) const; 67 68 virtual String userAgent(const KURL&) const; 69 70 WorkerScriptController* script() { return m_script.get(); } 71 void clearScript() { m_script.clear(); } 72 73 WorkerThread* thread() const { return m_thread; } 74 75 bool hasPendingActivity() const; 76 77 virtual void resourceRetrievedByXMLHttpRequest(unsigned long identifier, const ScriptString& sourceString); 78 virtual void scriptImported(unsigned long identifier, const String& sourceString); 79 80 virtual void postTask(PassOwnPtr<Task>); // Executes the task on context's thread asynchronously. 81 82 // WorkerGlobalScope 83 WorkerContext* self() { return this; } 84 WorkerLocation* location() const; 85 void close(); 86 87 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 88 89 // WorkerUtils 90 virtual void importScripts(const Vector<String>& urls, const String& callerURL, int callerLine, ExceptionCode&); 91 WorkerNavigator* navigator() const; 92 93 // Timers 94 int setTimeout(ScheduledAction*, int timeout); 95 void clearTimeout(int timeoutId); 96 int setInterval(ScheduledAction*, int timeout); 97 void clearInterval(int timeoutId); 98 99 // ScriptExecutionContext 100 virtual void reportException(const String& errorMessage, int lineNumber, const String& sourceURL); 101 virtual void addMessage(MessageDestination, MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceURL); 102 103 #if ENABLE(NOTIFICATIONS) 104 NotificationCenter* webkitNotifications() const; 105 #endif 106 107 #if ENABLE(DATABASE) 108 // HTML 5 client-side database 109 PassRefPtr<Database> openDatabase(const String& name, const String& version, const String& displayName, unsigned long estimatedSize, ExceptionCode&); 110 // Not implemented yet. 111 virtual bool isDatabaseReadOnly() const { return false; } 112 // Not implemented yet. 113 virtual void databaseExceededQuota(const String&) { } 114 #endif 115 virtual bool isContextThread() const; 116 117 118 // These methods are used for GC marking. See JSWorkerContext::markChildren(MarkStack&) in 119 // JSWorkerContextCustom.cpp. 120 WorkerNavigator* optionalNavigator() const { return m_navigator.get(); } 121 WorkerLocation* optionalLocation() const { return m_location.get(); } 122 123 using RefCounted<WorkerContext>::ref; 124 using RefCounted<WorkerContext>::deref; 125 126 bool isClosing() { return m_closing; } 127 128 protected: 129 WorkerContext(const KURL&, const String&, WorkerThread*); 130 131 private: 132 virtual void refScriptExecutionContext() { ref(); } 133 virtual void derefScriptExecutionContext() { deref(); } 134 135 virtual void refEventTarget() { ref(); } 136 virtual void derefEventTarget() { deref(); } 137 virtual EventTargetData* eventTargetData(); 138 virtual EventTargetData* ensureEventTargetData(); 139 140 virtual const KURL& virtualURL() const; 141 virtual KURL virtualCompleteURL(const String&) const; 142 143 KURL m_url; 144 String m_userAgent; 145 146 mutable RefPtr<WorkerLocation> m_location; 147 mutable RefPtr<WorkerNavigator> m_navigator; 148 149 OwnPtr<WorkerScriptController> m_script; 150 WorkerThread* m_thread; 151 152 #if ENABLE_NOTIFICATIONS 153 mutable RefPtr<NotificationCenter> m_notifications; 154 #endif 155 bool m_closing; 156 EventTargetData m_eventTargetData; 157 }; 158 159 } // namespace WebCore 160 161 #endif // ENABLE(WORKERS) 162 163 #endif // WorkerContext_h 164