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/SandboxFlags.h"
33#include "core/dom/SecurityContext.h"
34#include "core/fetch/CrossOriginAccessControl.h"
35#include "core/frame/ConsoleTypes.h"
36#include "core/frame/DOMTimer.h"
37#include "core/inspector/ConsoleMessage.h"
38#include "platform/LifecycleContext.h"
39#include "platform/Supplementable.h"
40#include "platform/heap/Handle.h"
41#include "platform/weborigin/KURL.h"
42#include "wtf/Functional.h"
43#include "wtf/OwnPtr.h"
44#include "wtf/PassOwnPtr.h"
45
46namespace blink {
47
48class ContextLifecycleNotifier;
49class LocalDOMWindow;
50class ErrorEvent;
51class EventQueue;
52class ExecutionContextTask;
53class ScriptState;
54class PublicURLManager;
55class SecurityOrigin;
56class ScriptCallStack;
57
58class ExecutionContext
59    : public LifecycleContext<ExecutionContext>
60    , public WillBeHeapSupplementable<ExecutionContext> {
61public:
62    virtual void trace(Visitor*) OVERRIDE;
63
64    virtual bool isDocument() const { return false; }
65    virtual bool isWorkerGlobalScope() const { return false; }
66    virtual bool isDedicatedWorkerGlobalScope() const { return false; }
67    virtual bool isSharedWorkerGlobalScope() const { return false; }
68    virtual bool isServiceWorkerGlobalScope() const { return false; }
69    virtual bool isJSExecutionForbidden() const { return false; }
70    SecurityOrigin* securityOrigin();
71    ContentSecurityPolicy* contentSecurityPolicy();
72    virtual void didUpdateSecurityOrigin() = 0;
73    const KURL& url() const;
74    KURL completeURL(const String& url) const;
75    virtual void disableEval(const String& errorMessage) = 0;
76    virtual LocalDOMWindow* executingWindow() { return 0; }
77    virtual String userAgent(const KURL&) const = 0;
78    virtual void postTask(PassOwnPtr<ExecutionContextTask>) = 0; // Executes the task on context's thread asynchronously.
79    virtual double timerAlignmentInterval() const = 0;
80
81    virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) = 0;
82
83    virtual SecurityContext& securityContext() = 0;
84    KURL contextURL() const { return virtualURL(); }
85    KURL contextCompleteURL(const String& url) const { return virtualCompleteURL(url); }
86
87    bool shouldSanitizeScriptError(const String& sourceURL, AccessControlStatus);
88    void reportException(PassRefPtrWillBeRawPtr<ErrorEvent>, int scriptId, PassRefPtrWillBeRawPtr<ScriptCallStack>, AccessControlStatus);
89
90    virtual void addConsoleMessage(PassRefPtrWillBeRawPtr<ConsoleMessage>) = 0;
91    virtual void logExceptionToConsole(const String& errorMessage, int scriptId, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtrWillBeRawPtr<ScriptCallStack>) = 0;
92
93    PublicURLManager& publicURLManager();
94
95    // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked.
96    bool hasPendingActivity();
97
98    void suspendActiveDOMObjects();
99    void resumeActiveDOMObjects();
100    void stopActiveDOMObjects();
101    unsigned activeDOMObjectCount();
102
103    virtual void suspendScheduledTasks();
104    virtual void resumeScheduledTasks();
105    virtual bool tasksNeedSuspension() { return false; }
106    virtual void tasksWereSuspended() { }
107    virtual void tasksWereResumed() { }
108
109    bool activeDOMObjectsAreSuspended() const { return m_activeDOMObjectsAreSuspended; }
110    bool activeDOMObjectsAreStopped() const { return m_activeDOMObjectsAreStopped; }
111    bool isIteratingOverObservers() const;
112
113    // Called after the construction of an ActiveDOMObject to synchronize suspend state.
114    void suspendActiveDOMObjectIfNeeded(ActiveDOMObject*);
115#if !ENABLE(OILPAN)
116    void ref() { refExecutionContext(); }
117    void deref() { derefExecutionContext(); }
118#endif
119
120    // Gets the next id in a circular sequence from 1 to 2^31-1.
121    int circularSequentialID();
122
123    void didChangeTimerAlignmentInterval();
124
125    SandboxFlags sandboxFlags() const { return m_sandboxFlags; }
126    bool isSandboxed(SandboxFlags mask) const { return m_sandboxFlags & mask; }
127    void enforceSandboxFlags(SandboxFlags mask);
128
129    PassOwnPtr<LifecycleNotifier<ExecutionContext> > createLifecycleNotifier();
130
131    virtual EventTarget* errorEventTarget() = 0;
132    virtual EventQueue* eventQueue() const = 0;
133
134protected:
135    ExecutionContext();
136    virtual ~ExecutionContext();
137
138    virtual const KURL& virtualURL() const = 0;
139    virtual KURL virtualCompleteURL(const String&) const = 0;
140
141    ContextLifecycleNotifier& lifecycleNotifier();
142
143private:
144    friend class DOMTimer; // For installNewTimeout() and removeTimeoutByID() below.
145
146    bool dispatchErrorEvent(PassRefPtrWillBeRawPtr<ErrorEvent>, AccessControlStatus);
147
148#if !ENABLE(OILPAN)
149    virtual void refExecutionContext() = 0;
150    virtual void derefExecutionContext() = 0;
151#endif
152    // LifecycleContext implementation.
153
154    // Implementation details for DOMTimer. No other classes should call these functions.
155    int installNewTimeout(PassOwnPtr<ScheduledAction>, int timeout, bool singleShot);
156    void removeTimeoutByID(int timeoutID); // This makes underlying DOMTimer instance destructed.
157
158    SandboxFlags m_sandboxFlags;
159
160    int m_circularSequentialID;
161    typedef HashMap<int, OwnPtr<DOMTimer> > TimeoutMap;
162    TimeoutMap m_timeouts;
163
164    bool m_inDispatchErrorEvent;
165    class PendingException;
166    OwnPtrWillBeMember<WillBeHeapVector<OwnPtrWillBeMember<PendingException> > > m_pendingExceptions;
167
168    bool m_activeDOMObjectsAreSuspended;
169    bool m_activeDOMObjectsAreStopped;
170
171    OwnPtr<PublicURLManager> m_publicURLManager;
172
173    // The location of this member is important; to make sure contextDestroyed() notification on
174    // ExecutionContext's members (notably m_timeouts) is called before they are destructed,
175    // m_lifecycleNotifer should be placed *after* such members.
176    OwnPtr<ContextLifecycleNotifier> m_lifecycleNotifier;
177};
178
179} // namespace blink
180
181#endif // ExecutionContext_h
182