Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2008 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 ScriptExecutionContext_h
     28 #define ScriptExecutionContext_h
     29 
     30 #include "Console.h"
     31 #include "KURL.h"
     32 #include <wtf/HashMap.h>
     33 #include <wtf/HashSet.h>
     34 #include <wtf/PassOwnPtr.h>
     35 #include <wtf/PassRefPtr.h>
     36 #include <wtf/RefPtr.h>
     37 #include <wtf/Threading.h>
     38 
     39 namespace WebCore {
     40 
     41     class ActiveDOMObject;
     42 #if ENABLE(DATABASE)
     43     class Database;
     44     class DatabaseTaskSynchronizer;
     45     class DatabaseThread;
     46 #endif
     47     class DOMTimer;
     48     class MessagePort;
     49     class SecurityOrigin;
     50     class ScriptString;
     51     class String;
     52 
     53     enum MessageDestination {
     54 #if ENABLE(INSPECTOR)
     55         InspectorControllerDestination,
     56 #endif
     57         ConsoleDestination,
     58     };
     59 
     60     class ScriptExecutionContext {
     61     public:
     62         ScriptExecutionContext();
     63         virtual ~ScriptExecutionContext();
     64 
     65         virtual bool isDocument() const { return false; }
     66         virtual bool isWorkerContext() const { return false; }
     67 
     68 #if ENABLE(DATABASE)
     69         virtual bool isDatabaseReadOnly() const = 0;
     70         virtual void databaseExceededQuota(const String& name) = 0;
     71         DatabaseThread* databaseThread();
     72         void setHasOpenDatabases() { m_hasOpenDatabases = true; }
     73         bool hasOpenDatabases() const { return m_hasOpenDatabases; }
     74         void addOpenDatabase(Database*);
     75         void removeOpenDatabase(Database*);
     76         // When the database cleanup is done, cleanupSync will be signalled.
     77         void stopDatabases(DatabaseTaskSynchronizer*);
     78 #endif
     79         virtual bool isContextThread() const = 0;
     80 
     81         const KURL& url() const { return virtualURL(); }
     82         KURL completeURL(const String& url) const { return virtualCompleteURL(url); }
     83 
     84         virtual String userAgent(const KURL&) const = 0;
     85 
     86         SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); }
     87 
     88         virtual void reportException(const String& errorMessage, int lineNumber, const String& sourceURL) = 0;
     89         virtual void addMessage(MessageDestination, MessageSource, MessageType, MessageLevel, const String& message, unsigned lineNumber, const String& sourceURL) = 0;
     90         virtual void resourceRetrievedByXMLHttpRequest(unsigned long identifier, const ScriptString& sourceString) = 0;
     91         virtual void scriptImported(unsigned long, const String&) = 0;
     92 
     93         // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked.
     94         bool canSuspendActiveDOMObjects();
     95         // Active objects can be asked to suspend even if canSuspendActiveDOMObjects() returns 'false' -
     96         // step-by-step JS debugging is one example.
     97         void suspendActiveDOMObjects();
     98         void resumeActiveDOMObjects();
     99         void stopActiveDOMObjects();
    100         void createdActiveDOMObject(ActiveDOMObject*, void* upcastPointer);
    101         void destroyedActiveDOMObject(ActiveDOMObject*);
    102         typedef const HashMap<ActiveDOMObject*, void*> ActiveDOMObjectsMap;
    103         ActiveDOMObjectsMap& activeDOMObjects() const { return m_activeDOMObjects; }
    104 
    105         // MessagePort is conceptually a kind of ActiveDOMObject, but it needs to be tracked separately for message dispatch.
    106         void processMessagePortMessagesSoon();
    107         void dispatchMessagePortEvents();
    108         void createdMessagePort(MessagePort*);
    109         void destroyedMessagePort(MessagePort*);
    110         const HashSet<MessagePort*>& messagePorts() const { return m_messagePorts; }
    111 
    112         void ref() { refScriptExecutionContext(); }
    113         void deref() { derefScriptExecutionContext(); }
    114 
    115         class Task : public Noncopyable {
    116         public:
    117             virtual ~Task();
    118             virtual void performTask(ScriptExecutionContext*) = 0;
    119             // Certain tasks get marked specially so that they aren't discarded, and are executed, when the context is shutting down its message queue.
    120             virtual bool isCleanupTask() const { return false; }
    121         };
    122 
    123         virtual void postTask(PassOwnPtr<Task>) = 0; // Executes the task on context's thread asynchronously.
    124 
    125         void addTimeout(int timeoutId, DOMTimer*);
    126         void removeTimeout(int timeoutId);
    127         DOMTimer* findTimeout(int timeoutId);
    128 
    129 #if USE(JSC)
    130         JSC::JSGlobalData* globalData();
    131 #endif
    132 
    133     protected:
    134         // Explicitly override the security origin for this script context.
    135         // Note: It is dangerous to change the security origin of a script context
    136         //       that already contains content.
    137         void setSecurityOrigin(PassRefPtr<SecurityOrigin>);
    138 
    139     private:
    140         virtual const KURL& virtualURL() const = 0;
    141         virtual KURL virtualCompleteURL(const String&) const = 0;
    142 
    143         RefPtr<SecurityOrigin> m_securityOrigin;
    144 
    145         HashSet<MessagePort*> m_messagePorts;
    146 
    147         HashMap<ActiveDOMObject*, void*> m_activeDOMObjects;
    148 
    149         HashMap<int, DOMTimer*> m_timeouts;
    150 
    151         virtual void refScriptExecutionContext() = 0;
    152         virtual void derefScriptExecutionContext() = 0;
    153 
    154 #if ENABLE(DATABASE)
    155         RefPtr<DatabaseThread> m_databaseThread;
    156         bool m_hasOpenDatabases; // This never changes back to false, even after the database thread is closed.
    157         typedef HashSet<Database* > DatabaseSet;
    158         OwnPtr<DatabaseSet> m_openDatabaseSet;
    159 #endif
    160     };
    161 
    162 } // namespace WebCore
    163 
    164 
    165 #endif // ScriptExecutionContext_h
    166