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/ExecutionContext.h"
     33 #include "core/events/EventListener.h"
     34 #include "core/events/EventTarget.h"
     35 #include "core/frame/DOMWindowBase64.h"
     36 #include "core/frame/UseCounter.h"
     37 #include "core/frame/csp/ContentSecurityPolicy.h"
     38 #include "core/workers/WorkerEventQueue.h"
     39 #include "platform/heap/Handle.h"
     40 #include "platform/network/ContentSecurityPolicyParsers.h"
     41 #include "wtf/Assertions.h"
     42 #include "wtf/HashMap.h"
     43 #include "wtf/OwnPtr.h"
     44 #include "wtf/PassRefPtr.h"
     45 #include "wtf/RefCounted.h"
     46 #include "wtf/RefPtr.h"
     47 #include "wtf/text/AtomicStringHash.h"
     48 
     49 namespace WebCore {
     50 
     51     class Blob;
     52     class ExceptionState;
     53     class ScheduledAction;
     54     class WorkerClients;
     55     class WorkerConsole;
     56     class WorkerInspectorController;
     57     class WorkerLocation;
     58     class WorkerNavigator;
     59     class WorkerThread;
     60 
     61     class WorkerGlobalScope : public RefCountedWillBeRefCountedGarbageCollected<WorkerGlobalScope>, public ScriptWrappable, public SecurityContext, public ExecutionContext, public ExecutionContextClient, public WillBeHeapSupplementable<WorkerGlobalScope>, public EventTargetWithInlineData, public DOMWindowBase64 {
     62         WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(WorkerGlobalScope);
     63         REFCOUNTED_EVENT_TARGET(WorkerGlobalScope);
     64     public:
     65         virtual ~WorkerGlobalScope();
     66 
     67         virtual bool isWorkerGlobalScope() const OVERRIDE FINAL { return true; }
     68 
     69         virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
     70 
     71         virtual bool isSharedWorkerGlobalScope() const { return false; }
     72         virtual bool isDedicatedWorkerGlobalScope() const { return false; }
     73         virtual bool isServiceWorkerGlobalScope() const { return false; }
     74         virtual void countFeature(UseCounter::Feature) const;
     75         virtual void countDeprecation(UseCounter::Feature) const;
     76 
     77         const KURL& url() const { return m_url; }
     78         KURL completeURL(const String&) const;
     79 
     80         virtual String userAgent(const KURL&) const OVERRIDE FINAL;
     81         virtual void disableEval(const String& errorMessage) OVERRIDE FINAL;
     82 
     83         WorkerScriptController* script() { return m_script.get(); }
     84         void clearScript() { m_script.clear(); }
     85         void clearInspector();
     86 
     87         // FIXME: We can remove this interface when we remove openDatabaseSync.
     88         class TerminationObserver {
     89         public:
     90             virtual ~TerminationObserver() { }
     91             // The function is probably called in the main thread.
     92             virtual void wasRequestedToTerminate() = 0;
     93         };
     94         void registerTerminationObserver(TerminationObserver*);
     95         void unregisterTerminationObserver(TerminationObserver*);
     96         void wasRequestedToTerminate();
     97 
     98         void dispose();
     99 
    100         WorkerThread* thread() const { return m_thread; }
    101 
    102         virtual void postTask(PassOwnPtr<ExecutionContextTask>) OVERRIDE FINAL; // Executes the task on context's thread asynchronously.
    103 
    104         // WorkerGlobalScope
    105         WorkerGlobalScope* self() { return this; }
    106         WorkerConsole* console();
    107         WorkerLocation* location() const;
    108         void close();
    109 
    110         DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
    111 
    112         // WorkerUtils
    113         virtual void importScripts(const Vector<String>& urls, ExceptionState&);
    114         WorkerNavigator* navigator() const;
    115 
    116         // ExecutionContextClient
    117         virtual WorkerEventQueue* eventQueue() const OVERRIDE FINAL;
    118         virtual SecurityContext& securityContext() OVERRIDE FINAL { return *this; }
    119 
    120         virtual bool isContextThread() const OVERRIDE FINAL;
    121         virtual bool isJSExecutionForbidden() const OVERRIDE FINAL;
    122 
    123         virtual double timerAlignmentInterval() const OVERRIDE FINAL;
    124 
    125         WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); }
    126 
    127         bool isClosing() { return m_closing; }
    128 
    129         virtual void stopFetch() { }
    130 
    131         bool idleNotification();
    132 
    133         double timeOrigin() const { return m_timeOrigin; }
    134 
    135         WorkerClients* clients() { return m_workerClients.get(); }
    136 
    137         using SecurityContext::securityOrigin;
    138         using SecurityContext::contentSecurityPolicy;
    139 
    140         virtual void trace(Visitor*) OVERRIDE;
    141 
    142     protected:
    143         WorkerGlobalScope(const KURL&, const String& userAgent, WorkerThread*, double timeOrigin, PassOwnPtrWillBeRawPtr<WorkerClients>);
    144         void applyContentSecurityPolicyFromString(const String& contentSecurityPolicy, ContentSecurityPolicyHeaderType);
    145 
    146         virtual void logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtrWillBeRawPtr<ScriptCallStack>) OVERRIDE;
    147         void addMessageToWorkerConsole(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, PassRefPtrWillBeRawPtr<ScriptCallStack>, ScriptState*);
    148 
    149     private:
    150 #if !ENABLE(OILPAN)
    151         virtual void refExecutionContext() OVERRIDE FINAL { ref(); }
    152         virtual void derefExecutionContext() OVERRIDE FINAL { deref(); }
    153 #endif
    154 
    155         virtual const KURL& virtualURL() const OVERRIDE FINAL;
    156         virtual KURL virtualCompleteURL(const String&) const OVERRIDE FINAL;
    157 
    158         virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) OVERRIDE FINAL;
    159         virtual void addMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, ScriptState* = 0) OVERRIDE FINAL;
    160 
    161         virtual EventTarget* errorEventTarget() OVERRIDE FINAL;
    162         virtual void didUpdateSecurityOrigin() OVERRIDE FINAL { }
    163 
    164         KURL m_url;
    165         String m_userAgent;
    166 
    167         mutable RefPtrWillBeMember<WorkerConsole> m_console;
    168         mutable RefPtrWillBeMember<WorkerLocation> m_location;
    169         mutable RefPtrWillBeMember<WorkerNavigator> m_navigator;
    170 
    171         OwnPtr<WorkerScriptController> m_script;
    172         WorkerThread* m_thread;
    173 
    174         OwnPtr<WorkerInspectorController> m_workerInspectorController;
    175         bool m_closing;
    176 
    177         OwnPtrWillBeMember<WorkerEventQueue> m_eventQueue;
    178 
    179         OwnPtrWillBeMember<WorkerClients> m_workerClients;
    180 
    181         double m_timeOrigin;
    182         TerminationObserver* m_terminationObserver;
    183     };
    184 
    185 DEFINE_TYPE_CASTS(WorkerGlobalScope, ExecutionContext, context, context->isWorkerGlobalScope(), context.isWorkerGlobalScope());
    186 
    187 } // namespace WebCore
    188 
    189 #endif // WorkerGlobalScope_h
    190