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 WorkerContext_h
     28 #define WorkerContext_h
     29 
     30 #if ENABLE(WORKERS)
     31 
     32 #include "EventListener.h"
     33 #include "EventNames.h"
     34 #include "EventTarget.h"
     35 #include "ScriptExecutionContext.h"
     36 #include "WorkerScriptController.h"
     37 #include <wtf/Assertions.h>
     38 #include <wtf/HashMap.h>
     39 #include <wtf/OwnPtr.h>
     40 #include <wtf/PassRefPtr.h>
     41 #include <wtf/RefCounted.h>
     42 #include <wtf/RefPtr.h>
     43 #include <wtf/text/AtomicStringHash.h>
     44 
     45 namespace WebCore {
     46 
     47     class Blob;
     48     class DOMFileSystemSync;
     49     class DOMURL;
     50     class Database;
     51     class DatabaseCallback;
     52     class DatabaseSync;
     53     class EntryCallback;
     54     class EntrySync;
     55     class ErrorCallback;
     56     class FileSystemCallback;
     57     class NotificationCenter;
     58     class ScheduledAction;
     59     class WorkerInspectorController;
     60     class WorkerLocation;
     61     class WorkerNavigator;
     62     class WorkerThread;
     63 
     64     class WorkerContext : public RefCounted<WorkerContext>, public ScriptExecutionContext, public EventTarget {
     65     public:
     66         virtual ~WorkerContext();
     67 
     68         virtual bool isWorkerContext() const { return true; }
     69 
     70         virtual ScriptExecutionContext* scriptExecutionContext() const;
     71 
     72         virtual bool isSharedWorkerContext() const { return false; }
     73         virtual bool isDedicatedWorkerContext() const { return false; }
     74 
     75         const KURL& url() const { return m_url; }
     76         KURL completeURL(const String&) const;
     77 
     78         virtual String userAgent(const KURL&) const;
     79 
     80         WorkerScriptController* script() { return m_script.get(); }
     81         void clearScript() { m_script.clear(); }
     82 
     83         WorkerThread* thread() const { return m_thread; }
     84 
     85         bool hasPendingActivity() const;
     86 
     87         virtual void postTask(PassOwnPtr<Task>); // 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 void addMessage(MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceURL, PassRefPtr<ScriptCallStack>);
    108 
    109 #if ENABLE(NOTIFICATIONS)
    110         NotificationCenter* webkitNotifications() const;
    111 #endif
    112 
    113 #if ENABLE(DATABASE)
    114         // HTML 5 client-side database
    115         PassRefPtr<Database> openDatabase(const String& name, const String& version, const String& displayName, unsigned long estimatedSize, PassRefPtr<DatabaseCallback> creationCallback, ExceptionCode&);
    116         PassRefPtr<DatabaseSync> openDatabaseSync(const String& name, const String& version, const String& displayName, unsigned long estimatedSize, PassRefPtr<DatabaseCallback> creationCallback, ExceptionCode&);
    117 
    118         // Not implemented yet.
    119         virtual bool allowDatabaseAccess() const { return true; }
    120         // Not implemented for real yet.
    121         virtual void databaseExceededQuota(const String&);
    122 #endif
    123         virtual bool isContextThread() const;
    124         virtual bool isJSExecutionForbidden() const;
    125 
    126 #if ENABLE(BLOB)
    127         DOMURL* webkitURL() const;
    128 #endif
    129 
    130 #if ENABLE(FILE_SYSTEM)
    131         enum FileSystemType {
    132             TEMPORARY,
    133             PERSISTENT,
    134             EXTERNAL,
    135         };
    136         void webkitRequestFileSystem(int type, long long size, PassRefPtr<FileSystemCallback> successCallback, PassRefPtr<ErrorCallback>);
    137         PassRefPtr<DOMFileSystemSync> webkitRequestFileSystemSync(int type, long long size, ExceptionCode&);
    138         void webkitResolveLocalFileSystemURL(const String& url, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback>);
    139         PassRefPtr<EntrySync> webkitResolveLocalFileSystemSyncURL(const String& url, ExceptionCode&);
    140 #endif
    141 #if ENABLE(INSPECTOR)
    142         WorkerInspectorController* workerInspectorController() { return m_workerInspectorController.get(); }
    143 #endif
    144         // These methods are used for GC marking. See JSWorkerContext::markChildren(MarkStack&) in
    145         // JSWorkerContextCustom.cpp.
    146         WorkerNavigator* optionalNavigator() const { return m_navigator.get(); }
    147         WorkerLocation* optionalLocation() const { return m_location.get(); }
    148 
    149         using RefCounted<WorkerContext>::ref;
    150         using RefCounted<WorkerContext>::deref;
    151 
    152         bool isClosing() { return m_closing; }
    153 
    154         // An observer interface to be notified when the worker thread is getting stopped.
    155         class Observer {
    156             WTF_MAKE_NONCOPYABLE(Observer);
    157         public:
    158             Observer(WorkerContext*);
    159             virtual ~Observer();
    160             virtual void notifyStop() = 0;
    161             void stopObserving();
    162         private:
    163             WorkerContext* m_context;
    164         };
    165         friend class Observer;
    166         void registerObserver(Observer*);
    167         void unregisterObserver(Observer*);
    168         void notifyObserversOfStop();
    169 
    170     protected:
    171         WorkerContext(const KURL&, const String&, WorkerThread*);
    172 
    173     private:
    174         virtual void refScriptExecutionContext() { ref(); }
    175         virtual void derefScriptExecutionContext() { deref(); }
    176 
    177         virtual void refEventTarget() { ref(); }
    178         virtual void derefEventTarget() { deref(); }
    179         virtual EventTargetData* eventTargetData();
    180         virtual EventTargetData* ensureEventTargetData();
    181 
    182         virtual const KURL& virtualURL() const;
    183         virtual KURL virtualCompleteURL(const String&) const;
    184 
    185         virtual EventTarget* errorEventTarget();
    186         virtual void logExceptionToConsole(const String& errorMessage, int lineNumber, const String& sourceURL, PassRefPtr<ScriptCallStack>);
    187 
    188         KURL m_url;
    189         String m_userAgent;
    190 
    191         mutable RefPtr<WorkerLocation> m_location;
    192         mutable RefPtr<WorkerNavigator> m_navigator;
    193 
    194         OwnPtr<WorkerScriptController> m_script;
    195         WorkerThread* m_thread;
    196 
    197 #if ENABLE_NOTIFICATIONS
    198         mutable RefPtr<NotificationCenter> m_notifications;
    199 #endif
    200 #if ENABLE(BLOB)
    201         mutable RefPtr<DOMURL> m_domURL;
    202 #endif
    203 #if ENABLE(INSPECTOR)
    204         OwnPtr<WorkerInspectorController> m_workerInspectorController;
    205 #endif
    206         bool m_closing;
    207         EventTargetData m_eventTargetData;
    208 
    209         HashSet<Observer*> m_workerObservers;
    210     };
    211 
    212 } // namespace WebCore
    213 
    214 #endif // ENABLE(WORKERS)
    215 
    216 #endif // WorkerContext_h
    217