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