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 "ContentSecurityPolicy.h"
33#include "EventListener.h"
34#include "EventNames.h"
35#include "EventTarget.h"
36#include "GroupSettings.h"
37#include "ScriptExecutionContext.h"
38#include "WorkerEventQueue.h"
39#include "WorkerScriptController.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
48namespace WebCore {
49
50    class Blob;
51    class DOMURL;
52    class ScheduledAction;
53    class WorkerInspectorController;
54    class WorkerLocation;
55    class WorkerNavigator;
56    class WorkerThread;
57
58    class WorkerContext : public RefCounted<WorkerContext>, public ScriptExecutionContext, public EventTarget {
59    public:
60        virtual ~WorkerContext();
61
62        virtual bool isWorkerContext() const OVERRIDE { return true; }
63
64        virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE;
65
66        virtual bool isSharedWorkerContext() const { return false; }
67        virtual bool isDedicatedWorkerContext() const { return false; }
68
69        const KURL& url() const { return m_url; }
70        KURL completeURL(const String&) const;
71
72        const GroupSettings* groupSettings() { return m_groupSettings.get(); }
73        virtual String userAgent(const KURL&) const;
74
75        virtual void disableEval(const String& errorMessage) OVERRIDE;
76
77        WorkerScriptController* script() { return m_script.get(); }
78        void clearScript() { m_script.clear(); }
79#if ENABLE(INSPECTOR)
80        void clearInspector();
81#endif
82
83        WorkerThread* thread() const { return m_thread; }
84
85        bool hasPendingActivity() const;
86
87        virtual void postTask(PassOwnPtr<Task>) OVERRIDE; // Executes the task on context's thread asynchronously.
88
89        // WorkerGlobalScope
90        WorkerContext* self() { return this; }
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, ExceptionCode&);
98        WorkerNavigator* navigator() const;
99
100        // Timers
101        int setTimeout(PassOwnPtr<ScheduledAction>, int timeout);
102        void clearTimeout(int timeoutId);
103        int setInterval(PassOwnPtr<ScheduledAction>, int timeout);
104        void clearInterval(int timeoutId);
105
106        // ScriptExecutionContext
107        virtual WorkerEventQueue* eventQueue() const OVERRIDE;
108
109        virtual bool isContextThread() const OVERRIDE;
110        virtual bool isJSExecutionForbidden() const OVERRIDE;
111
112#if ENABLE(INSPECTOR)
113        WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); }
114#endif
115        // These methods are used for GC marking. See JSWorkerContext::visitChildrenVirtual(SlotVisitor&) in
116        // JSWorkerContextCustom.cpp.
117        WorkerNavigator* optionalNavigator() const { return m_navigator.get(); }
118        WorkerLocation* optionalLocation() const { return m_location.get(); }
119
120        using RefCounted<WorkerContext>::ref;
121        using RefCounted<WorkerContext>::deref;
122
123        bool isClosing() { return m_closing; }
124
125        // An observer interface to be notified when the worker thread is getting stopped.
126        class Observer {
127            WTF_MAKE_NONCOPYABLE(Observer);
128        public:
129            Observer(WorkerContext*);
130            virtual ~Observer();
131            virtual void notifyStop() = 0;
132            void stopObserving();
133        private:
134            WorkerContext* m_context;
135        };
136        friend class Observer;
137        void registerObserver(Observer*);
138        void unregisterObserver(Observer*);
139        void notifyObserversOfStop();
140
141        virtual SecurityOrigin* topOrigin() const OVERRIDE { return m_topOrigin.get(); }
142
143    protected:
144        WorkerContext(const KURL&, const String& userAgent, PassOwnPtr<GroupSettings>, WorkerThread*, PassRefPtr<SecurityOrigin> topOrigin);
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, unsigned columnNumber, PassRefPtr<ScriptCallStack>, ScriptState* = 0, unsigned long requestIdentifier = 0);
149
150    private:
151        virtual void refScriptExecutionContext() OVERRIDE { ref(); }
152        virtual void derefScriptExecutionContext() OVERRIDE { deref(); }
153
154        virtual void refEventTarget() OVERRIDE { ref(); }
155        virtual void derefEventTarget() OVERRIDE { deref(); }
156        virtual EventTargetData* eventTargetData() OVERRIDE;
157        virtual EventTargetData* ensureEventTargetData() OVERRIDE;
158
159        virtual const KURL& virtualURL() const OVERRIDE;
160        virtual KURL virtualCompleteURL(const String&) const;
161
162        virtual void addMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack>, ScriptState* = 0, unsigned long requestIdentifier = 0) OVERRIDE;
163        virtual void addConsoleMessage(MessageSource, MessageLevel, const String& message, unsigned long requestIdentifier = 0) OVERRIDE;
164
165        virtual EventTarget* errorEventTarget() OVERRIDE;
166
167        KURL m_url;
168        String m_userAgent;
169        OwnPtr<GroupSettings> m_groupSettings;
170
171        mutable RefPtr<WorkerLocation> m_location;
172        mutable RefPtr<WorkerNavigator> m_navigator;
173
174        OwnPtr<WorkerScriptController> m_script;
175        WorkerThread* m_thread;
176
177#if ENABLE(BLOB)
178        mutable RefPtr<DOMURL> m_domURL;
179#endif
180#if ENABLE(INSPECTOR)
181        OwnPtr<WorkerInspectorController> m_workerInspectorController;
182#endif
183        bool m_closing;
184        EventTargetData m_eventTargetData;
185
186        HashSet<Observer*> m_workerObservers;
187
188        OwnPtr<WorkerEventQueue> m_eventQueue;
189
190        RefPtr<SecurityOrigin> m_topOrigin;
191    };
192
193} // namespace WebCore
194
195#endif // ENABLE(WORKERS)
196
197#endif // WorkerContext_h
198