1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2012 Google Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#ifndef ExecutionContext_h
29#define ExecutionContext_h
30
31#include "core/dom/ActiveDOMObject.h"
32#include "core/dom/ExecutionContextClient.h"
33#include "core/dom/SandboxFlags.h"
34#include "core/dom/SecurityContext.h"
35#include "core/events/ErrorEvent.h"
36#include "core/fetch/CrossOriginAccessControl.h"
37#include "core/frame/ConsoleTypes.h"
38#include "core/frame/DOMTimer.h"
39#include "platform/LifecycleContext.h"
40#include "platform/Supplementable.h"
41#include "platform/heap/Handle.h"
42#include "platform/weborigin/KURL.h"
43#include "wtf/Functional.h"
44#include "wtf/OwnPtr.h"
45#include "wtf/PassOwnPtr.h"
46
47namespace WTF {
48class OrdinalNumber;
49}
50
51namespace WebCore {
52
53class ContextLifecycleNotifier;
54class LocalDOMWindow;
55class EventListener;
56class EventQueue;
57class EventTarget;
58class ExecutionContextTask;
59class ScriptState;
60class PublicURLManager;
61class SecurityOrigin;
62class ScriptCallStack;
63
64class ExecutionContext
65    : public WillBeGarbageCollectedMixin
66    , public LifecycleContext<ExecutionContext>
67    , public Supplementable<ExecutionContext> {
68public:
69    virtual void trace(Visitor*);
70
71    // Delegating to ExecutionContextClient
72    bool isDocument() const { return m_client && m_client->isDocument(); }
73    bool isWorkerGlobalScope() const { return m_client && m_client->isWorkerGlobalScope(); }
74    bool isJSExecutionForbidden() { return m_client && m_client->isJSExecutionForbidden(); }
75    SecurityOrigin* securityOrigin() const;
76    ContentSecurityPolicy* contentSecurityPolicy() const;
77    const KURL& url() const;
78    KURL completeURL(const String& url) const;
79    void disableEval(const String& errorMessage);
80    LocalDOMWindow* executingWindow() const;
81    String userAgent(const KURL&) const;
82    void postTask(PassOwnPtr<ExecutionContextTask>);
83    void postTask(const Closure&);
84    double timerAlignmentInterval() const;
85
86    virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) = 0;
87
88    virtual SecurityContext& securityContext() = 0;
89    KURL contextURL() const { return virtualURL(); }
90    KURL contextCompleteURL(const String& url) const { return virtualCompleteURL(url); }
91
92    bool shouldSanitizeScriptError(const String& sourceURL, AccessControlStatus);
93    void reportException(PassRefPtrWillBeRawPtr<ErrorEvent>, PassRefPtrWillBeRawPtr<ScriptCallStack>, AccessControlStatus);
94
95    void addConsoleMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber);
96    void addConsoleMessage(MessageSource, MessageLevel, const String& message, ScriptState* = 0);
97
98    PublicURLManager& publicURLManager();
99
100    // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked.
101    bool hasPendingActivity();
102
103    void suspendActiveDOMObjects();
104    void resumeActiveDOMObjects();
105    void stopActiveDOMObjects();
106    unsigned activeDOMObjectCount();
107
108    virtual void suspendScheduledTasks();
109    virtual void resumeScheduledTasks();
110    virtual bool tasksNeedSuspension() { return false; }
111
112    bool activeDOMObjectsAreSuspended() const { return m_activeDOMObjectsAreSuspended; }
113    bool activeDOMObjectsAreStopped() const { return m_activeDOMObjectsAreStopped; }
114    bool isIteratingOverObservers() const;
115
116    // Called after the construction of an ActiveDOMObject to synchronize suspend state.
117    void suspendActiveDOMObjectIfNeeded(ActiveDOMObject*);
118#if !ENABLE(OILPAN)
119    void ref() { refExecutionContext(); }
120    void deref() { derefExecutionContext(); }
121#endif
122
123    // Gets the next id in a circular sequence from 1 to 2^31-1.
124    int circularSequentialID();
125
126    void didChangeTimerAlignmentInterval();
127
128    SandboxFlags sandboxFlags() const { return m_sandboxFlags; }
129    bool isSandboxed(SandboxFlags mask) const { return m_sandboxFlags & mask; }
130    void enforceSandboxFlags(SandboxFlags mask);
131
132    PassOwnPtr<LifecycleNotifier<ExecutionContext> > createLifecycleNotifier();
133
134    virtual EventQueue* eventQueue() const = 0;
135
136protected:
137    ExecutionContext();
138    virtual ~ExecutionContext();
139
140    void setClient(ExecutionContextClient* client) { m_client = client; }
141
142    virtual const KURL& virtualURL() const = 0;
143    virtual KURL virtualCompleteURL(const String&) const = 0;
144
145    ContextLifecycleNotifier& lifecycleNotifier();
146
147private:
148    friend class DOMTimer; // For installNewTimeout() and removeTimeoutByID() below.
149
150    bool dispatchErrorEvent(PassRefPtrWillBeRawPtr<ErrorEvent>, AccessControlStatus);
151
152#if !ENABLE(OILPAN)
153    virtual void refExecutionContext() = 0;
154    virtual void derefExecutionContext() = 0;
155#endif
156    // LifecycleContext implementation.
157
158    // Implementation details for DOMTimer. No other classes should call these functions.
159    int installNewTimeout(PassOwnPtr<ScheduledAction>, int timeout, bool singleShot);
160    void removeTimeoutByID(int timeoutID); // This makes underlying DOMTimer instance destructed.
161
162    ExecutionContextClient* m_client;
163    SandboxFlags m_sandboxFlags;
164
165    int m_circularSequentialID;
166    typedef HashMap<int, OwnPtr<DOMTimer> > TimeoutMap;
167    TimeoutMap m_timeouts;
168
169    bool m_inDispatchErrorEvent;
170    class PendingException;
171    OwnPtrWillBeMember<WillBeHeapVector<OwnPtrWillBeMember<PendingException> > > m_pendingExceptions;
172
173    bool m_activeDOMObjectsAreSuspended;
174    bool m_activeDOMObjectsAreStopped;
175
176    OwnPtr<PublicURLManager> m_publicURLManager;
177
178    // The location of this member is important; to make sure contextDestroyed() notification on
179    // ExecutionContext's members (notably m_timeouts) is called before they are destructed,
180    // m_lifecycleNotifer should be placed *after* such members.
181    OwnPtr<ContextLifecycleNotifier> m_lifecycleNotifier;
182};
183
184} // namespace WebCore
185
186#endif // ExecutionContext_h
187