2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2012 Google Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #ifndef ExecutionContext_h
29 #define ExecutionContext_h
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"
48 class ContextLifecycleNotifier;
52 class ExecutionContextTask;
54 class PublicURLManager;
56 class ScriptCallStack;
58 class ExecutionContext
59 : public LifecycleContext<ExecutionContext>
60 , public WillBeHeapSupplementable<ExecutionContext> {
62 virtual void trace(Visitor*) OVERRIDE;
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;
81 virtual void reportBlockedScriptExecutionToInspector(const String& directiveText) = 0;
83 virtual SecurityContext& securityContext() = 0;
84 KURL contextURL() const { return virtualURL(); }
85 KURL contextCompleteURL(const String& url) const { return virtualCompleteURL(url); }
87 bool shouldSanitizeScriptError(const String& sourceURL, AccessControlStatus);
88 void reportException(PassRefPtrWillBeRawPtr<ErrorEvent>, int scriptId, PassRefPtrWillBeRawPtr<ScriptCallStack>, AccessControlStatus);
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;
93 PublicURLManager& publicURLManager();
95 // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked.
96 bool hasPendingActivity();
98 void suspendActiveDOMObjects();
99 void resumeActiveDOMObjects();
100 void stopActiveDOMObjects();
101 unsigned activeDOMObjectCount();
103 virtual void suspendScheduledTasks();
104 virtual void resumeScheduledTasks();
105 virtual bool tasksNeedSuspension() { return false; }
106 virtual void tasksWereSuspended() { }
107 virtual void tasksWereResumed() { }
109 bool activeDOMObjectsAreSuspended() const { return m_activeDOMObjectsAreSuspended; }
110 bool activeDOMObjectsAreStopped() const { return m_activeDOMObjectsAreStopped; }
111 bool isIteratingOverObservers() const;
113 // Called after the construction of an ActiveDOMObject to synchronize suspend state.
114 void suspendActiveDOMObjectIfNeeded(ActiveDOMObject*);
116 void ref() { refExecutionContext(); }
117 void deref() { derefExecutionContext(); }
120 // Gets the next id in a circular sequence from 1 to 2^31-1.
121 int circularSequentialID();
123 void didChangeTimerAlignmentInterval();
125 SandboxFlags sandboxFlags() const { return m_sandboxFlags; }
126 bool isSandboxed(SandboxFlags mask) const { return m_sandboxFlags & mask; }
127 void enforceSandboxFlags(SandboxFlags mask);
129 PassOwnPtr<LifecycleNotifier<ExecutionContext> > createLifecycleNotifier();
131 virtual EventTarget* errorEventTarget() = 0;
132 virtual EventQueue* eventQueue() const = 0;
136 virtual ~ExecutionContext();
138 virtual const KURL& virtualURL() const = 0;
139 virtual KURL virtualCompleteURL(const String&) const = 0;
141 ContextLifecycleNotifier& lifecycleNotifier();
144 friend class DOMTimer; // For installNewTimeout() and removeTimeoutByID() below.
146 bool dispatchErrorEvent(PassRefPtrWillBeRawPtr<ErrorEvent>, AccessControlStatus);
149 virtual void refExecutionContext() = 0;
150 virtual void derefExecutionContext() = 0;
152 // LifecycleContext implementation.
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.
158 SandboxFlags m_sandboxFlags;
160 int m_circularSequentialID;
161 typedef HashMap<int, OwnPtr<DOMTimer> > TimeoutMap;
162 TimeoutMap m_timeouts;
164 bool m_inDispatchErrorEvent;
165 class PendingException;
166 OwnPtrWillBeMember<WillBeHeapVector<OwnPtrWillBeMember<PendingException> > > m_pendingExceptions;
168 bool m_activeDOMObjectsAreSuspended;
169 bool m_activeDOMObjectsAreStopped;
171 OwnPtr<PublicURLManager> m_publicURLManager;
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;
181 #endif // ExecutionContext_h