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.
29 #include "core/dom/ExecutionContext.h"
31 #include "core/dom/AddConsoleMessageTask.h"
32 #include "core/dom/ContextLifecycleNotifier.h"
33 #include "core/dom/ExecutionContextTask.h"
34 #include "core/events/ErrorEvent.h"
35 #include "core/events/EventTarget.h"
36 #include "core/html/PublicURLManager.h"
37 #include "core/inspector/InspectorInstrumentation.h"
38 #include "core/inspector/ScriptCallStack.h"
39 #include "core/workers/WorkerGlobalScope.h"
40 #include "core/workers/WorkerThread.h"
41 #include "wtf/MainThread.h"
45 class ExecutionContext::PendingException : public NoBaseWillBeGarbageCollectedFinalized<ExecutionContext::PendingException> {
46 WTF_MAKE_NONCOPYABLE(PendingException);
48 PendingException(const String& errorMessage, int lineNumber, int columnNumber, int scriptId, const String& sourceURL, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack)
49 : m_errorMessage(errorMessage)
50 , m_lineNumber(lineNumber)
51 , m_columnNumber(columnNumber)
52 , m_scriptId(scriptId)
53 , m_sourceURL(sourceURL)
54 , m_callStack(callStack)
57 void trace(Visitor* visitor)
59 visitor->trace(m_callStack);
61 String m_errorMessage;
66 RefPtrWillBeMember<ScriptCallStack> m_callStack;
69 ExecutionContext::ExecutionContext()
70 : m_sandboxFlags(SandboxNone)
71 , m_circularSequentialID(0)
72 , m_inDispatchErrorEvent(false)
73 , m_activeDOMObjectsAreSuspended(false)
74 , m_activeDOMObjectsAreStopped(false)
78 ExecutionContext::~ExecutionContext()
82 bool ExecutionContext::hasPendingActivity()
84 return lifecycleNotifier().hasPendingActivity();
87 void ExecutionContext::suspendActiveDOMObjects()
89 lifecycleNotifier().notifySuspendingActiveDOMObjects();
90 m_activeDOMObjectsAreSuspended = true;
93 void ExecutionContext::resumeActiveDOMObjects()
95 m_activeDOMObjectsAreSuspended = false;
96 lifecycleNotifier().notifyResumingActiveDOMObjects();
99 void ExecutionContext::stopActiveDOMObjects()
101 m_activeDOMObjectsAreStopped = true;
102 lifecycleNotifier().notifyStoppingActiveDOMObjects();
105 unsigned ExecutionContext::activeDOMObjectCount()
107 return lifecycleNotifier().activeDOMObjects().size();
110 void ExecutionContext::suspendScheduledTasks()
112 suspendActiveDOMObjects();
113 tasksWereSuspended();
116 void ExecutionContext::resumeScheduledTasks()
118 resumeActiveDOMObjects();
122 void ExecutionContext::suspendActiveDOMObjectIfNeeded(ActiveDOMObject* object)
124 ASSERT(lifecycleNotifier().contains(object));
125 // Ensure all ActiveDOMObjects are suspended also newly created ones.
126 if (m_activeDOMObjectsAreSuspended)
130 bool ExecutionContext::shouldSanitizeScriptError(const String& sourceURL, AccessControlStatus corsStatus)
132 return !(securityOrigin()->canRequest(completeURL(sourceURL)) || corsStatus == SharableCrossOrigin);
135 void ExecutionContext::reportException(PassRefPtrWillBeRawPtr<ErrorEvent> event, int scriptId, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack, AccessControlStatus corsStatus)
137 RefPtrWillBeRawPtr<ErrorEvent> errorEvent = event;
138 if (m_inDispatchErrorEvent) {
139 if (!m_pendingExceptions)
140 m_pendingExceptions = adoptPtrWillBeNoop(new WillBeHeapVector<OwnPtrWillBeMember<PendingException> >());
141 m_pendingExceptions->append(adoptPtrWillBeNoop(new PendingException(errorEvent->messageForConsole(), errorEvent->lineno(), errorEvent->colno(), scriptId, errorEvent->filename(), callStack)));
145 // First report the original exception and only then all the nested ones.
146 if (!dispatchErrorEvent(errorEvent, corsStatus))
147 logExceptionToConsole(errorEvent->messageForConsole(), scriptId, errorEvent->filename(), errorEvent->lineno(), errorEvent->colno(), callStack);
149 if (!m_pendingExceptions)
152 for (size_t i = 0; i < m_pendingExceptions->size(); i++) {
153 PendingException* e = m_pendingExceptions->at(i).get();
154 logExceptionToConsole(e->m_errorMessage, e->m_scriptId, e->m_sourceURL, e->m_lineNumber, e->m_columnNumber, e->m_callStack);
156 m_pendingExceptions.clear();
159 bool ExecutionContext::dispatchErrorEvent(PassRefPtrWillBeRawPtr<ErrorEvent> event, AccessControlStatus corsStatus)
161 EventTarget* target = errorEventTarget();
165 RefPtrWillBeRawPtr<ErrorEvent> errorEvent = event;
166 if (shouldSanitizeScriptError(errorEvent->filename(), corsStatus))
167 errorEvent = ErrorEvent::createSanitizedError(errorEvent->world());
169 ASSERT(!m_inDispatchErrorEvent);
170 m_inDispatchErrorEvent = true;
171 target->dispatchEvent(errorEvent);
172 m_inDispatchErrorEvent = false;
173 return errorEvent->defaultPrevented();
176 int ExecutionContext::circularSequentialID()
178 ++m_circularSequentialID;
179 if (m_circularSequentialID <= 0)
180 m_circularSequentialID = 1;
181 return m_circularSequentialID;
184 int ExecutionContext::installNewTimeout(PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
188 timeoutID = circularSequentialID();
189 if (!m_timeouts.contains(timeoutID))
192 TimeoutMap::AddResult result = m_timeouts.add(timeoutID, DOMTimer::create(this, action, timeout, singleShot, timeoutID));
193 ASSERT(result.isNewEntry);
194 DOMTimer* timer = result.storedValue->value.get();
196 timer->suspendIfNeeded();
198 return timer->timeoutID();
201 void ExecutionContext::removeTimeoutByID(int timeoutID)
205 m_timeouts.remove(timeoutID);
208 PublicURLManager& ExecutionContext::publicURLManager()
210 if (!m_publicURLManager)
211 m_publicURLManager = PublicURLManager::create(this);
212 return *m_publicURLManager;
215 void ExecutionContext::didChangeTimerAlignmentInterval()
217 for (TimeoutMap::iterator iter = m_timeouts.begin(); iter != m_timeouts.end(); ++iter)
218 iter->value->didChangeAlignmentInterval();
221 SecurityOrigin* ExecutionContext::securityOrigin()
223 return securityContext().securityOrigin();
226 ContentSecurityPolicy* ExecutionContext::contentSecurityPolicy()
228 return securityContext().contentSecurityPolicy();
231 const KURL& ExecutionContext::url() const
236 KURL ExecutionContext::completeURL(const String& url) const
238 return virtualCompleteURL(url);
241 PassOwnPtr<LifecycleNotifier<ExecutionContext> > ExecutionContext::createLifecycleNotifier()
243 return ContextLifecycleNotifier::create(this);
246 ContextLifecycleNotifier& ExecutionContext::lifecycleNotifier()
248 return static_cast<ContextLifecycleNotifier&>(LifecycleContext<ExecutionContext>::lifecycleNotifier());
251 bool ExecutionContext::isIteratingOverObservers() const
253 return m_lifecycleNotifier && m_lifecycleNotifier->isIteratingOverObservers();
256 void ExecutionContext::enforceSandboxFlags(SandboxFlags mask)
258 m_sandboxFlags |= mask;
260 // The SandboxOrigin is stored redundantly in the security origin.
261 if (isSandboxed(SandboxOrigin) && securityContext().securityOrigin() && !securityContext().securityOrigin()->isUnique()) {
262 securityContext().setSecurityOrigin(SecurityOrigin::createUnique());
263 didUpdateSecurityOrigin();
267 void ExecutionContext::trace(Visitor* visitor)
270 visitor->trace(m_pendingExceptions);
271 HeapSupplementable<ExecutionContext>::trace(visitor);
273 LifecycleContext<ExecutionContext>::trace(visitor);