Make some things that return never-null pointers return references instead.
[WebKit-https.git] / Source / WebCore / workers / WorkerGlobalScope.cpp
1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2009, 2011 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
30 #if ENABLE(WORKERS)
31
32 #include "WorkerGlobalScope.h"
33
34 #include "ActiveDOMObject.h"
35 #include "ContentSecurityPolicy.h"
36 #include "DOMTimer.h"
37 #include "DOMURL.h"
38 #include "DOMWindow.h"
39 #include "ErrorEvent.h"
40 #include "Event.h"
41 #include "EventException.h"
42 #include "InspectorConsoleInstrumentation.h"
43 #include "KURL.h"
44 #include "MessagePort.h"
45 #include "NotImplemented.h"
46 #include "ScheduledAction.h"
47 #include "ScriptCallStack.h"
48 #include "ScriptSourceCode.h"
49 #include "ScriptValue.h"
50 #include "SecurityOrigin.h"
51 #include "WorkerInspectorController.h"
52 #include "WorkerLocation.h"
53 #include "WorkerNavigator.h"
54 #include "WorkerObjectProxy.h"
55 #include "WorkerScriptLoader.h"
56 #include "WorkerThread.h"
57 #include "WorkerThreadableLoader.h"
58 #include "XMLHttpRequestException.h"
59 #include <wtf/RefPtr.h>
60
61 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
62 #include "NotificationCenter.h"
63 #endif
64
65 #include "ExceptionCode.h"
66
67 namespace WebCore {
68
69 class CloseWorkerGlobalScopeTask : public ScriptExecutionContext::Task {
70 public:
71     static PassOwnPtr<CloseWorkerGlobalScopeTask> create()
72     {
73         return adoptPtr(new CloseWorkerGlobalScopeTask);
74     }
75
76     virtual void performTask(ScriptExecutionContext *context)
77     {
78         ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
79         WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
80         // Notify parent that this context is closed. Parent is responsible for calling WorkerThread::stop().
81         workerGlobalScope->thread()->workerReportingProxy().workerGlobalScopeClosed();
82     }
83
84     virtual bool isCleanupTask() const { return true; }
85 };
86
87 WorkerGlobalScope::WorkerGlobalScope(const KURL& url, const String& userAgent, PassOwnPtr<GroupSettings> settings, WorkerThread* thread, PassRefPtr<SecurityOrigin> topOrigin)
88     : m_url(url)
89     , m_userAgent(userAgent)
90     , m_groupSettings(settings)
91     , m_script(adoptPtr(new WorkerScriptController(this)))
92     , m_thread(thread)
93 #if ENABLE(INSPECTOR)
94     , m_workerInspectorController(adoptPtr(new WorkerInspectorController(this)))
95 #endif
96     , m_closing(false)
97     , m_eventQueue(WorkerEventQueue::create(this))
98     , m_topOrigin(topOrigin)
99 {
100     setSecurityOrigin(SecurityOrigin::create(url));
101 }
102
103 WorkerGlobalScope::~WorkerGlobalScope()
104 {
105     ASSERT(currentThread() == thread()->threadID());
106
107     // Make sure we have no observers.
108     notifyObserversOfStop();
109
110     // Notify proxy that we are going away. This can free the WorkerThread object, so do not access it after this.
111     thread()->workerReportingProxy().workerGlobalScopeDestroyed();
112 }
113
114 void WorkerGlobalScope::applyContentSecurityPolicyFromString(const String& policy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType)
115 {
116     setContentSecurityPolicy(ContentSecurityPolicy::create(this));
117     contentSecurityPolicy()->didReceiveHeader(policy, contentSecurityPolicyType);
118 }
119
120 ScriptExecutionContext* WorkerGlobalScope::scriptExecutionContext() const
121 {
122     return const_cast<WorkerGlobalScope*>(this);
123 }
124
125 const KURL& WorkerGlobalScope::virtualURL() const
126 {
127     return m_url;
128 }
129
130 KURL WorkerGlobalScope::virtualCompleteURL(const String& url) const
131 {
132     return completeURL(url);
133 }
134
135 KURL WorkerGlobalScope::completeURL(const String& url) const
136 {
137     // Always return a null URL when passed a null string.
138     // FIXME: Should we change the KURL constructor to have this behavior?
139     if (url.isNull())
140         return KURL();
141     // Always use UTF-8 in Workers.
142     return KURL(m_url, url);
143 }
144
145 String WorkerGlobalScope::userAgent(const KURL&) const
146 {
147     return m_userAgent;
148 }
149
150 void WorkerGlobalScope::disableEval(const String& errorMessage)
151 {
152     m_script->disableEval(errorMessage);
153 }
154
155 WorkerLocation* WorkerGlobalScope::location() const
156 {
157     if (!m_location)
158         m_location = WorkerLocation::create(m_url);
159     return m_location.get();
160 }
161
162 void WorkerGlobalScope::close()
163 {
164     if (m_closing)
165         return;
166
167     // Let current script run to completion but prevent future script evaluations.
168     // After m_closing is set, all the tasks in the queue continue to be fetched but only
169     // tasks with isCleanupTask()==true will be executed.
170     m_closing = true;
171     postTask(CloseWorkerGlobalScopeTask::create());
172 }
173
174 WorkerNavigator* WorkerGlobalScope::navigator() const
175 {
176     if (!m_navigator)
177         m_navigator = WorkerNavigator::create(m_userAgent);
178     return m_navigator.get();
179 }
180
181 bool WorkerGlobalScope::hasPendingActivity() const
182 {
183     ActiveDOMObjectsSet::const_iterator activeObjectsEnd = activeDOMObjects().end();
184     for (ActiveDOMObjectsSet::const_iterator iter = activeDOMObjects().begin(); iter != activeObjectsEnd; ++iter) {
185         if ((*iter)->hasPendingActivity())
186             return true;
187     }
188
189     HashSet<MessagePort*>::const_iterator messagePortsEnd = messagePorts().end();
190     for (HashSet<MessagePort*>::const_iterator iter = messagePorts().begin(); iter != messagePortsEnd; ++iter) {
191         if ((*iter)->hasPendingActivity())
192             return true;
193     }
194
195     return false;
196 }
197
198 void WorkerGlobalScope::postTask(PassOwnPtr<Task> task)
199 {
200     thread()->runLoop().postTask(task);
201 }
202
203 int WorkerGlobalScope::setTimeout(PassOwnPtr<ScheduledAction> action, int timeout)
204 {
205     return DOMTimer::install(scriptExecutionContext(), action, timeout, true);
206 }
207
208 void WorkerGlobalScope::clearTimeout(int timeoutId)
209 {
210     DOMTimer::removeById(scriptExecutionContext(), timeoutId);
211 }
212
213 #if ENABLE(INSPECTOR)
214 void WorkerGlobalScope::clearInspector()
215 {
216     m_workerInspectorController.clear();
217 }
218 #endif
219
220 int WorkerGlobalScope::setInterval(PassOwnPtr<ScheduledAction> action, int timeout)
221 {
222     return DOMTimer::install(scriptExecutionContext(), action, timeout, false);
223 }
224
225 void WorkerGlobalScope::clearInterval(int timeoutId)
226 {
227     DOMTimer::removeById(scriptExecutionContext(), timeoutId);
228 }
229
230 void WorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionCode& ec)
231 {
232     ASSERT(contentSecurityPolicy());
233     ec = 0;
234     Vector<String>::const_iterator urlsEnd = urls.end();
235     Vector<KURL> completedURLs;
236     for (Vector<String>::const_iterator it = urls.begin(); it != urlsEnd; ++it) {
237         const KURL& url = scriptExecutionContext()->completeURL(*it);
238         if (!url.isValid()) {
239             ec = SYNTAX_ERR;
240             return;
241         }
242         completedURLs.append(url);
243     }
244     Vector<KURL>::const_iterator end = completedURLs.end();
245
246     for (Vector<KURL>::const_iterator it = completedURLs.begin(); it != end; ++it) {
247         RefPtr<WorkerScriptLoader> scriptLoader(WorkerScriptLoader::create());
248 #if PLATFORM(BLACKBERRY)
249         scriptLoader->setTargetType(ResourceRequest::TargetIsScript);
250 #endif
251         scriptLoader->loadSynchronously(scriptExecutionContext(), *it, AllowCrossOriginRequests);
252
253         // If the fetching attempt failed, throw a NETWORK_ERR exception and abort all these steps.
254         if (scriptLoader->failed()) {
255             ec = XMLHttpRequestException::NETWORK_ERR;
256             return;
257         }
258
259         InspectorInstrumentation::scriptImported(scriptExecutionContext(), scriptLoader->identifier(), scriptLoader->script());
260
261         ScriptValue exception;
262         m_script->evaluate(ScriptSourceCode(scriptLoader->script(), scriptLoader->responseURL()), &exception);
263         if (!exception.hasNoValue()) {
264             m_script->setException(exception);
265             return;
266         }
267     }
268 }
269
270 EventTarget* WorkerGlobalScope::errorEventTarget()
271 {
272     return this;
273 }
274
275 void WorkerGlobalScope::logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtr<ScriptCallStack>)
276 {
277     thread()->workerReportingProxy().postExceptionToWorkerObject(errorMessage, lineNumber, columnNumber, sourceURL);
278 }
279
280 void WorkerGlobalScope::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, unsigned long requestIdentifier)
281 {
282     if (!isContextThread()) {
283         postTask(AddConsoleMessageTask::create(source, level, message));
284         return;
285     }
286
287     thread()->workerReportingProxy().postConsoleMessageToWorkerObject(source, level, message, 0, 0, String());
288     addMessageToWorkerConsole(source, level, message, String(), 0, 0, 0, 0, requestIdentifier);
289 }
290
291 void WorkerGlobalScope::addMessage(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack> callStack, ScriptState* state, unsigned long requestIdentifier)
292 {
293     if (!isContextThread()) {
294         postTask(AddConsoleMessageTask::create(source, level, message));
295         return;
296     }
297
298     thread()->workerReportingProxy().postConsoleMessageToWorkerObject(source, level, message, lineNumber, columnNumber, sourceURL);
299     addMessageToWorkerConsole(source, level, message, sourceURL, lineNumber, columnNumber, callStack, state, requestIdentifier);
300 }
301
302 void WorkerGlobalScope::addMessageToWorkerConsole(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack> callStack, ScriptState* state, unsigned long requestIdentifier)
303 {
304     ASSERT(isContextThread());
305     if (callStack)
306         InspectorInstrumentation::addMessageToConsole(this, source, LogMessageType, level, message, callStack, requestIdentifier);
307     else
308         InspectorInstrumentation::addMessageToConsole(this, source, LogMessageType, level, message, sourceURL, lineNumber, columnNumber, state, requestIdentifier);
309 }
310
311 bool WorkerGlobalScope::isContextThread() const
312 {
313     return currentThread() == thread()->threadID();
314 }
315
316 bool WorkerGlobalScope::isJSExecutionForbidden() const
317 {
318     return m_script->isExecutionForbidden();
319 }
320
321 EventTargetData* WorkerGlobalScope::eventTargetData()
322 {
323     return &m_eventTargetData;
324 }
325
326 EventTargetData& WorkerGlobalScope::ensureEventTargetData()
327 {
328     return m_eventTargetData;
329 }
330
331 WorkerGlobalScope::Observer::Observer(WorkerGlobalScope* context)
332     : m_context(context)
333 {
334     ASSERT(m_context && m_context->isContextThread());
335     m_context->registerObserver(this);
336 }
337
338 WorkerGlobalScope::Observer::~Observer()
339 {
340     if (!m_context)
341         return;
342     ASSERT(m_context->isContextThread());
343     m_context->unregisterObserver(this);
344 }
345
346 void WorkerGlobalScope::Observer::stopObserving()
347 {
348     if (!m_context)
349         return;
350     ASSERT(m_context->isContextThread());
351     m_context->unregisterObserver(this);
352     m_context = 0;
353 }
354
355 void WorkerGlobalScope::registerObserver(Observer* observer)
356 {
357     ASSERT(observer);
358     m_workerObservers.add(observer);
359 }
360
361 void WorkerGlobalScope::unregisterObserver(Observer* observer)
362 {
363     ASSERT(observer);
364     m_workerObservers.remove(observer);
365 }
366
367 void WorkerGlobalScope::notifyObserversOfStop()
368 {
369     HashSet<Observer*>::iterator iter = m_workerObservers.begin();
370     while (iter != m_workerObservers.end()) {
371         WorkerGlobalScope::Observer* observer = *iter;
372         observer->stopObserving();
373         observer->notifyStop();
374         iter = m_workerObservers.begin();
375     }
376 }
377
378 WorkerEventQueue* WorkerGlobalScope::eventQueue() const
379 {
380     return m_eventQueue.get();
381 }
382
383 } // namespace WebCore
384
385 #endif // ENABLE(WORKERS)