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/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/workers/WorkerEventQueue.h"
38#include "platform/heap/Handle.h"
39#include "platform/network/ContentSecurityPolicyParsers.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 blink {
49
50class Blob;
51class ConsoleMessage;
52class ConsoleMessageStorage;
53class ExceptionState;
54class ScheduledAction;
55class WorkerClients;
56class WorkerConsole;
57class WorkerInspectorController;
58class WorkerLocation;
59class WorkerNavigator;
60class WorkerThread;
61
62class WorkerGlobalScope : public RefCountedWillBeGarbageCollectedFinalized<WorkerGlobalScope>, public SecurityContext, public ExecutionContext, public WillBeHeapSupplementable<WorkerGlobalScope>, public EventTargetWithInlineData, public DOMWindowBase64 {
63    DEFINE_WRAPPERTYPEINFO();
64    REFCOUNTED_EVENT_TARGET(WorkerGlobalScope);
65    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(WorkerGlobalScope);
66public:
67    virtual ~WorkerGlobalScope();
68
69    virtual bool isWorkerGlobalScope() const OVERRIDE FINAL { return true; }
70
71    virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
72
73    virtual void countFeature(UseCounter::Feature) const;
74    virtual void countDeprecation(UseCounter::Feature) const;
75
76    const KURL& url() const { return m_url; }
77    KURL completeURL(const String&) const;
78
79    virtual String userAgent(const KURL&) const OVERRIDE FINAL;
80    virtual void disableEval(const String& errorMessage) OVERRIDE FINAL;
81
82    WorkerScriptController* script() { return m_script.get(); }
83    void clearScript() { m_script.clear(); }
84    void clearInspector();
85
86    void dispose();
87
88    WorkerThread* thread() const { return m_thread; }
89
90    virtual void postTask(PassOwnPtr<ExecutionContextTask>) OVERRIDE FINAL; // Executes the task on context's thread asynchronously.
91
92    // WorkerGlobalScope
93    WorkerGlobalScope* self() { return this; }
94    WorkerConsole* console();
95    WorkerLocation* location() const;
96    void close();
97
98    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
99
100    // WorkerUtils
101    virtual void importScripts(const Vector<String>& urls, ExceptionState&);
102    WorkerNavigator* navigator() const;
103
104    // ExecutionContextClient
105    virtual WorkerEventQueue* eventQueue() const OVERRIDE FINAL;
106    virtual SecurityContext& securityContext() OVERRIDE FINAL { return *this; }
107
108    virtual bool isContextThread() const OVERRIDE FINAL;
109    virtual bool isJSExecutionForbidden() const OVERRIDE FINAL;
110
111    virtual double timerAlignmentInterval() const OVERRIDE FINAL;
112
113    WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); }
114
115    bool isClosing() { return m_closing; }
116
117    virtual void stopFetch() { }
118
119    bool idleNotification();
120
121    double timeOrigin() const { return m_timeOrigin; }
122
123    WorkerClients* clients() { return m_workerClients.get(); }
124
125    using SecurityContext::securityOrigin;
126    using SecurityContext::contentSecurityPolicy;
127
128    virtual void addConsoleMessage(PassRefPtrWillBeRawPtr<ConsoleMessage>) OVERRIDE FINAL;
129    ConsoleMessageStorage* messageStorage();
130
131    virtual void trace(Visitor*) OVERRIDE;
132
133protected:
134    WorkerGlobalScope(const KURL&, const String& userAgent, WorkerThread*, double timeOrigin, PassOwnPtrWillBeRawPtr<WorkerClients>);
135    void applyContentSecurityPolicyFromString(const String& contentSecurityPolicy, ContentSecurityPolicyHeaderType);
136
137    virtual void logExceptionToConsole(const String& errorMessage, int scriptId, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtrWillBeRawPtr<ScriptCallStack>) OVERRIDE;
138    void addMessageToWorkerConsole(PassRefPtrWillBeRawPtr<ConsoleMessage>);
139
140private:
141#if !ENABLE(OILPAN)
142    virtual void refExecutionContext() OVERRIDE FINAL { ref(); }
143    virtual void derefExecutionContext() OVERRIDE FINAL { deref(); }
144#endif
145
146    virtual const KURL& virtualURL() const OVERRIDE FINAL;
147    virtual KURL virtualCompleteURL(const String&) const OVERRIDE FINAL;
148
149    virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) OVERRIDE FINAL;
150
151    virtual EventTarget* errorEventTarget() OVERRIDE FINAL;
152    virtual void didUpdateSecurityOrigin() OVERRIDE FINAL { }
153
154    KURL m_url;
155    String m_userAgent;
156
157    mutable RefPtrWillBeMember<WorkerConsole> m_console;
158    mutable RefPtrWillBeMember<WorkerLocation> m_location;
159    mutable RefPtrWillBeMember<WorkerNavigator> m_navigator;
160
161    OwnPtr<WorkerScriptController> m_script;
162    WorkerThread* m_thread;
163
164    RefPtrWillBeMember<WorkerInspectorController> m_workerInspectorController;
165    bool m_closing;
166
167    OwnPtrWillBeMember<WorkerEventQueue> m_eventQueue;
168
169    OwnPtrWillBeMember<WorkerClients> m_workerClients;
170
171    double m_timeOrigin;
172
173    OwnPtrWillBeMember<ConsoleMessageStorage> m_messageStorage;
174};
175
176DEFINE_TYPE_CASTS(WorkerGlobalScope, ExecutionContext, context, context->isWorkerGlobalScope(), context.isWorkerGlobalScope());
177
178} // namespace blink
179
180#endif // WorkerGlobalScope_h
181