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 WorkerContext_h
28 #define WorkerContext_h
32 #include "AtomicStringHash.h"
33 #include "EventListener.h"
34 #include "EventTarget.h"
35 #include "ScriptExecutionContext.h"
36 #include "WorkerScriptController.h"
37 #include <wtf/OwnPtr.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RefCounted.h>
40 #include <wtf/RefPtr.h>
44 class ScheduledAction;
46 class WorkerNavigator;
49 class WorkerContext : public RefCounted<WorkerContext>, public ScriptExecutionContext, public EventTarget {
52 virtual ~WorkerContext();
54 virtual bool isWorkerContext() const { return true; }
56 virtual ScriptExecutionContext* scriptExecutionContext() const;
58 const KURL& url() const { return m_url; }
59 KURL completeURL(const String&) const;
61 virtual String userAgent(const KURL&) const;
63 WorkerScriptController* script() { return m_script.get(); }
64 void clearScript() { return m_script.clear(); }
66 WorkerThread* thread() { return m_thread; }
68 bool hasPendingActivity() const;
70 virtual void reportException(const String& errorMessage, int lineNumber, const String& sourceURL) = 0;
71 virtual void addMessage(MessageDestination, MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceURL);
72 virtual void resourceRetrievedByXMLHttpRequest(unsigned long identifier, const ScriptString& sourceString);
73 virtual void scriptImported(unsigned long identifier, const String& sourceString);
75 virtual void postTask(PassRefPtr<Task>); // Executes the task on context's thread asynchronously.
78 WorkerContext* self() { return this; }
79 WorkerLocation* location() const;
81 void setOnerror(PassRefPtr<EventListener> eventListener) { m_onerrorListener = eventListener; }
82 EventListener* onerror() const { return m_onerrorListener.get(); }
85 void importScripts(const Vector<String>& urls, const String& callerURL, int callerLine, ExceptionCode&);
86 WorkerNavigator* navigator() const;
89 int setTimeout(ScheduledAction*, int timeout);
90 void clearTimeout(int timeoutId);
91 int setInterval(ScheduledAction*, int timeout);
92 void clearInterval(int timeoutId);
95 virtual void addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture);
96 virtual void removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture);
97 virtual bool dispatchEvent(PassRefPtr<Event>, ExceptionCode&);
99 typedef Vector<RefPtr<EventListener> > ListenerVector;
100 typedef HashMap<AtomicString, ListenerVector> EventListenersMap;
101 EventListenersMap& eventListeners() { return m_eventListeners; }
104 // These methods are used for GC marking. See JSWorkerContext::mark() in
105 // JSWorkerContextCustom.cpp.
106 WorkerNavigator* optionalNavigator() const { return m_navigator.get(); }
107 WorkerLocation* optionalLocation() const { return m_location.get(); }
109 using RefCounted<WorkerContext>::ref;
110 using RefCounted<WorkerContext>::deref;
113 WorkerContext(const KURL&, const String&, WorkerThread*);
114 bool isClosing() { return m_closing; }
117 virtual void refScriptExecutionContext() { ref(); }
118 virtual void derefScriptExecutionContext() { deref(); }
119 virtual void refEventTarget() { ref(); }
120 virtual void derefEventTarget() { deref(); }
122 virtual const KURL& virtualURL() const;
123 virtual KURL virtualCompleteURL(const String&) const;
128 mutable RefPtr<WorkerLocation> m_location;
129 mutable RefPtr<WorkerNavigator> m_navigator;
131 OwnPtr<WorkerScriptController> m_script;
132 WorkerThread* m_thread;
134 RefPtr<EventListener> m_onerrorListener;
135 EventListenersMap m_eventListeners;
140 } // namespace WebCore
142 #endif // ENABLE(WORKERS)
144 #endif // WorkerContext_h