Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / ExecutionContext.cpp
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 #include "config.h"
29 #include "core/dom/ExecutionContext.h"
30
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"
41
42 namespace WebCore {
43
44 class ExecutionContext::PendingException {
45     WTF_MAKE_NONCOPYABLE(PendingException);
46 public:
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)
53     {
54     }
55     String m_errorMessage;
56     int m_lineNumber;
57     int m_columnNumber;
58     String m_sourceURL;
59     RefPtr<ScriptCallStack> m_callStack;
60 };
61
62 ExecutionContext::ExecutionContext()
63     : m_client(0)
64     , m_sandboxFlags(SandboxNone)
65     , m_circularSequentialID(0)
66     , m_inDispatchErrorEvent(false)
67     , m_activeDOMObjectsAreSuspended(false)
68     , m_activeDOMObjectsAreStopped(false)
69 {
70 }
71
72 ExecutionContext::~ExecutionContext()
73 {
74 }
75
76 bool ExecutionContext::hasPendingActivity()
77 {
78     return lifecycleNotifier().hasPendingActivity();
79 }
80
81 void ExecutionContext::suspendActiveDOMObjects()
82 {
83     lifecycleNotifier().notifySuspendingActiveDOMObjects();
84     m_activeDOMObjectsAreSuspended = true;
85 }
86
87 void ExecutionContext::resumeActiveDOMObjects()
88 {
89     m_activeDOMObjectsAreSuspended = false;
90     lifecycleNotifier().notifyResumingActiveDOMObjects();
91 }
92
93 void ExecutionContext::stopActiveDOMObjects()
94 {
95     m_activeDOMObjectsAreStopped = true;
96     lifecycleNotifier().notifyStoppingActiveDOMObjects();
97 }
98
99 unsigned ExecutionContext::activeDOMObjectCount()
100 {
101     return lifecycleNotifier().activeDOMObjects().size();
102 }
103
104 void ExecutionContext::suspendScheduledTasks()
105 {
106     suspendActiveDOMObjects();
107     if (m_client)
108         m_client->tasksWereSuspended();
109 }
110
111 void ExecutionContext::resumeScheduledTasks()
112 {
113     resumeActiveDOMObjects();
114     if (m_client)
115         m_client->tasksWereResumed();
116 }
117
118 void ExecutionContext::suspendActiveDOMObjectIfNeeded(ActiveDOMObject* object)
119 {
120     ASSERT(lifecycleNotifier().contains(object));
121     // Ensure all ActiveDOMObjects are suspended also newly created ones.
122     if (m_activeDOMObjectsAreSuspended)
123         object->suspend();
124 }
125
126 bool ExecutionContext::shouldSanitizeScriptError(const String& sourceURL, AccessControlStatus corsStatus)
127 {
128     return !(securityOrigin()->canRequest(completeURL(sourceURL)) || corsStatus == SharableCrossOrigin);
129 }
130
131 void ExecutionContext::reportException(PassRefPtrWillBeRawPtr<ErrorEvent> event, PassRefPtr<ScriptCallStack> callStack, AccessControlStatus corsStatus)
132 {
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)));
138         return;
139     }
140
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);
144
145     if (!m_pendingExceptions)
146         return;
147
148     for (size_t i = 0; i < m_pendingExceptions->size(); i++) {
149         PendingException* e = m_pendingExceptions->at(i).get();
150         if (m_client)
151             m_client->logExceptionToConsole(e->m_errorMessage, e->m_sourceURL, e->m_lineNumber, e->m_columnNumber, e->m_callStack);
152     }
153     m_pendingExceptions.clear();
154 }
155
156 void ExecutionContext::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber)
157 {
158     if (!m_client)
159         return;
160     m_client->addMessage(source, level, message, sourceURL, lineNumber, 0);
161 }
162
163 void ExecutionContext::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, ScriptState* scriptState)
164 {
165     if (!m_client)
166         return;
167     m_client->addMessage(source, level, message, String(), 0, scriptState);
168 }
169
170 bool ExecutionContext::dispatchErrorEvent(PassRefPtrWillBeRawPtr<ErrorEvent> event, AccessControlStatus corsStatus)
171 {
172     if (!m_client)
173         return false;
174     EventTarget* target = m_client->errorEventTarget();
175     if (!target)
176         return false;
177
178     RefPtrWillBeRawPtr<ErrorEvent> errorEvent = event;
179     if (shouldSanitizeScriptError(errorEvent->filename(), corsStatus))
180         errorEvent = ErrorEvent::createSanitizedError(errorEvent->world());
181
182     ASSERT(!m_inDispatchErrorEvent);
183     m_inDispatchErrorEvent = true;
184     target->dispatchEvent(errorEvent);
185     m_inDispatchErrorEvent = false;
186     return errorEvent->defaultPrevented();
187 }
188
189 int ExecutionContext::circularSequentialID()
190 {
191     ++m_circularSequentialID;
192     if (m_circularSequentialID <= 0)
193         m_circularSequentialID = 1;
194     return m_circularSequentialID;
195 }
196
197 int ExecutionContext::installNewTimeout(PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
198 {
199     int timeoutID;
200     while (true) {
201         timeoutID = circularSequentialID();
202         if (!m_timeouts.contains(timeoutID))
203             break;
204     }
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();
208
209     timer->suspendIfNeeded();
210
211     return timer->timeoutID();
212 }
213
214 void ExecutionContext::removeTimeoutByID(int timeoutID)
215 {
216     if (timeoutID <= 0)
217         return;
218     m_timeouts.remove(timeoutID);
219 }
220
221 PublicURLManager& ExecutionContext::publicURLManager()
222 {
223     if (!m_publicURLManager)
224         m_publicURLManager = PublicURLManager::create(this);
225     return *m_publicURLManager;
226 }
227
228 void ExecutionContext::didChangeTimerAlignmentInterval()
229 {
230     for (TimeoutMap::iterator iter = m_timeouts.begin(); iter != m_timeouts.end(); ++iter)
231         iter->value->didChangeAlignmentInterval();
232 }
233
234 SecurityOrigin* ExecutionContext::securityOrigin() const
235 {
236     RELEASE_ASSERT(m_client);
237     return m_client->securityContext().securityOrigin();
238 }
239
240 ContentSecurityPolicy* ExecutionContext::contentSecurityPolicy() const
241 {
242     RELEASE_ASSERT(m_client);
243     return m_client->securityContext().contentSecurityPolicy();
244 }
245
246 const KURL& ExecutionContext::url() const
247 {
248     if (!m_client) {
249         DEFINE_STATIC_LOCAL(KURL, emptyURL, ());
250         return emptyURL;
251     }
252
253     return m_client->virtualURL();
254 }
255
256 KURL ExecutionContext::completeURL(const String& url) const
257 {
258
259     if (!m_client) {
260         DEFINE_STATIC_LOCAL(KURL, emptyURL, ());
261         return emptyURL;
262     }
263
264     return m_client->virtualCompleteURL(url);
265 }
266
267 void ExecutionContext::disableEval(const String& errorMessage)
268 {
269     if (!m_client)
270         return;
271     return m_client->disableEval(errorMessage);
272 }
273
274 DOMWindow* ExecutionContext::executingWindow() const
275 {
276     RELEASE_ASSERT(m_client);
277     return m_client->executingWindow();
278 }
279
280 String ExecutionContext::userAgent(const KURL& url) const
281 {
282     if (!m_client)
283         return String();
284     return m_client->userAgent(url);
285 }
286
287 double ExecutionContext::timerAlignmentInterval() const
288 {
289     if (!m_client)
290         return DOMTimer::visiblePageAlignmentInterval();
291     return m_client->timerAlignmentInterval();
292 }
293
294 void ExecutionContext::postTask(PassOwnPtr<ExecutionContextTask> task)
295 {
296     if (!m_client)
297         return;
298     m_client->postTask(task);
299 }
300
301 void ExecutionContext::postTask(const Closure& closure)
302 {
303     if (!m_client)
304         return;
305     m_client->postTask(CallClosureTask::create(closure));
306 }
307
308 PassOwnPtr<LifecycleNotifier<ExecutionContext> > ExecutionContext::createLifecycleNotifier()
309 {
310     return ContextLifecycleNotifier::create(this);
311 }
312
313 ContextLifecycleNotifier& ExecutionContext::lifecycleNotifier()
314 {
315     return static_cast<ContextLifecycleNotifier&>(LifecycleContext<ExecutionContext>::lifecycleNotifier());
316 }
317
318 bool ExecutionContext::isIteratingOverObservers() const
319 {
320     return m_lifecycleNotifier && m_lifecycleNotifier->isIteratingOverObservers();
321 }
322
323 void ExecutionContext::enforceSandboxFlags(SandboxFlags mask)
324 {
325     m_sandboxFlags |= mask;
326
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();
332     }
333 }
334
335 } // namespace WebCore