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/EventTarget.h"
35 #include "core/html/PublicURLManager.h"
36 #include "core/inspector/InspectorInstrumentation.h"
37 #include "core/inspector/ScriptCallStack.h"
38 #include "core/workers/WorkerGlobalScope.h"
39 #include "core/workers/WorkerThread.h"
40 #include "wtf/MainThread.h"
44 class ExecutionContext::PendingException {
45 WTF_MAKE_NONCOPYABLE(PendingException);
47 PendingException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, PassRefPtr<ScriptCallStack> callStack)
48 : m_errorMessage(errorMessage)
49 , m_lineNumber(lineNumber)
50 , m_columnNumber(columnNumber)
51 , m_sourceURL(sourceURL)
52 , m_callStack(callStack)
55 String m_errorMessage;
59 RefPtr<ScriptCallStack> m_callStack;
62 ExecutionContext::ExecutionContext()
64 , m_sandboxFlags(SandboxNone)
65 , m_circularSequentialID(0)
66 , m_inDispatchErrorEvent(false)
67 , m_activeDOMObjectsAreSuspended(false)
68 , m_activeDOMObjectsAreStopped(false)
72 ExecutionContext::~ExecutionContext()
76 bool ExecutionContext::hasPendingActivity()
78 return lifecycleNotifier().hasPendingActivity();
81 void ExecutionContext::suspendActiveDOMObjects()
83 lifecycleNotifier().notifySuspendingActiveDOMObjects();
84 m_activeDOMObjectsAreSuspended = true;
87 void ExecutionContext::resumeActiveDOMObjects()
89 m_activeDOMObjectsAreSuspended = false;
90 lifecycleNotifier().notifyResumingActiveDOMObjects();
93 void ExecutionContext::stopActiveDOMObjects()
95 m_activeDOMObjectsAreStopped = true;
96 lifecycleNotifier().notifyStoppingActiveDOMObjects();
99 unsigned ExecutionContext::activeDOMObjectCount()
101 return lifecycleNotifier().activeDOMObjects().size();
104 void ExecutionContext::suspendScheduledTasks()
106 suspendActiveDOMObjects();
108 m_client->tasksWereSuspended();
111 void ExecutionContext::resumeScheduledTasks()
113 resumeActiveDOMObjects();
115 m_client->tasksWereResumed();
118 void ExecutionContext::suspendActiveDOMObjectIfNeeded(ActiveDOMObject* object)
120 ASSERT(lifecycleNotifier().contains(object));
121 // Ensure all ActiveDOMObjects are suspended also newly created ones.
122 if (m_activeDOMObjectsAreSuspended)
126 bool ExecutionContext::shouldSanitizeScriptError(const String& sourceURL, AccessControlStatus corsStatus)
128 return !(securityOrigin()->canRequest(completeURL(sourceURL)) || corsStatus == SharableCrossOrigin);
131 void ExecutionContext::reportException(PassRefPtrWillBeRawPtr<ErrorEvent> event, PassRefPtr<ScriptCallStack> callStack, AccessControlStatus corsStatus)
133 RefPtrWillBeRawPtr<ErrorEvent> errorEvent = event;
134 if (m_inDispatchErrorEvent) {
135 if (!m_pendingExceptions)
136 m_pendingExceptions = adoptPtr(new Vector<OwnPtr<PendingException> >());
137 m_pendingExceptions->append(adoptPtr(new PendingException(errorEvent->messageForConsole(), errorEvent->lineno(), errorEvent->colno(), errorEvent->filename(), callStack)));
141 // First report the original exception and only then all the nested ones.
142 if (!dispatchErrorEvent(errorEvent, corsStatus) && m_client)
143 m_client->logExceptionToConsole(errorEvent->messageForConsole(), errorEvent->filename(), errorEvent->lineno(), errorEvent->colno(), callStack);
145 if (!m_pendingExceptions)
148 for (size_t i = 0; i < m_pendingExceptions->size(); i++) {
149 PendingException* e = m_pendingExceptions->at(i).get();
151 m_client->logExceptionToConsole(e->m_errorMessage, e->m_sourceURL, e->m_lineNumber, e->m_columnNumber, e->m_callStack);
153 m_pendingExceptions.clear();
156 void ExecutionContext::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber)
160 m_client->addMessage(source, level, message, sourceURL, lineNumber, 0);
163 void ExecutionContext::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, ScriptState* scriptState)
167 m_client->addMessage(source, level, message, String(), 0, scriptState);
170 bool ExecutionContext::dispatchErrorEvent(PassRefPtrWillBeRawPtr<ErrorEvent> event, AccessControlStatus corsStatus)
174 EventTarget* target = m_client->errorEventTarget();
178 RefPtrWillBeRawPtr<ErrorEvent> errorEvent = event;
179 if (shouldSanitizeScriptError(errorEvent->filename(), corsStatus))
180 errorEvent = ErrorEvent::createSanitizedError(errorEvent->world());
182 ASSERT(!m_inDispatchErrorEvent);
183 m_inDispatchErrorEvent = true;
184 target->dispatchEvent(errorEvent);
185 m_inDispatchErrorEvent = false;
186 return errorEvent->defaultPrevented();
189 int ExecutionContext::circularSequentialID()
191 ++m_circularSequentialID;
192 if (m_circularSequentialID <= 0)
193 m_circularSequentialID = 1;
194 return m_circularSequentialID;
197 int ExecutionContext::installNewTimeout(PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
201 timeoutID = circularSequentialID();
202 if (!m_timeouts.contains(timeoutID))
205 TimeoutMap::AddResult result = m_timeouts.add(timeoutID, DOMTimer::create(this, action, timeout, singleShot, timeoutID));
206 ASSERT(result.isNewEntry);
207 DOMTimer* timer = result.storedValue->value.get();
209 timer->suspendIfNeeded();
211 return timer->timeoutID();
214 void ExecutionContext::removeTimeoutByID(int timeoutID)
218 m_timeouts.remove(timeoutID);
221 PublicURLManager& ExecutionContext::publicURLManager()
223 if (!m_publicURLManager)
224 m_publicURLManager = PublicURLManager::create(this);
225 return *m_publicURLManager;
228 void ExecutionContext::didChangeTimerAlignmentInterval()
230 for (TimeoutMap::iterator iter = m_timeouts.begin(); iter != m_timeouts.end(); ++iter)
231 iter->value->didChangeAlignmentInterval();
234 SecurityOrigin* ExecutionContext::securityOrigin() const
236 RELEASE_ASSERT(m_client);
237 return m_client->securityContext().securityOrigin();
240 ContentSecurityPolicy* ExecutionContext::contentSecurityPolicy() const
242 RELEASE_ASSERT(m_client);
243 return m_client->securityContext().contentSecurityPolicy();
246 const KURL& ExecutionContext::url() const
249 DEFINE_STATIC_LOCAL(KURL, emptyURL, ());
253 return m_client->virtualURL();
256 KURL ExecutionContext::completeURL(const String& url) const
260 DEFINE_STATIC_LOCAL(KURL, emptyURL, ());
264 return m_client->virtualCompleteURL(url);
267 void ExecutionContext::disableEval(const String& errorMessage)
271 return m_client->disableEval(errorMessage);
274 DOMWindow* ExecutionContext::executingWindow() const
276 RELEASE_ASSERT(m_client);
277 return m_client->executingWindow();
280 String ExecutionContext::userAgent(const KURL& url) const
284 return m_client->userAgent(url);
287 double ExecutionContext::timerAlignmentInterval() const
290 return DOMTimer::visiblePageAlignmentInterval();
291 return m_client->timerAlignmentInterval();
294 void ExecutionContext::postTask(PassOwnPtr<ExecutionContextTask> task)
298 m_client->postTask(task);
301 void ExecutionContext::postTask(const Closure& closure)
305 m_client->postTask(CallClosureTask::create(closure));
308 PassOwnPtr<LifecycleNotifier<ExecutionContext> > ExecutionContext::createLifecycleNotifier()
310 return ContextLifecycleNotifier::create(this);
313 ContextLifecycleNotifier& ExecutionContext::lifecycleNotifier()
315 return static_cast<ContextLifecycleNotifier&>(LifecycleContext<ExecutionContext>::lifecycleNotifier());
318 bool ExecutionContext::isIteratingOverObservers() const
320 return m_lifecycleNotifier && m_lifecycleNotifier->isIteratingOverObservers();
323 void ExecutionContext::enforceSandboxFlags(SandboxFlags mask)
325 m_sandboxFlags |= mask;
327 RELEASE_ASSERT(m_client);
328 // The SandboxOrigin is stored redundantly in the security origin.
329 if (isSandboxed(SandboxOrigin) && m_client->securityContext().securityOrigin() && !m_client->securityContext().securityOrigin()->isUnique()) {
330 m_client->securityContext().setSecurityOrigin(SecurityOrigin::createUnique());
331 m_client->didUpdateSecurityOrigin();
335 } // namespace WebCore