author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 3 | 41300fa6a67c |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 2008 Apple Inc. All Rights Reserved. |
|
3 |
* Copyright (C) 2009 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 "WorkerContext.h" |
|
33 |
||
34 |
#include "ActiveDOMObject.h" |
|
35 |
#include "DOMTimer.h" |
|
36 |
#include "DOMWindow.h" |
|
37 |
#include "Event.h" |
|
38 |
#include "EventException.h" |
|
39 |
#include "MessagePort.h" |
|
40 |
#include "NotImplemented.h" |
|
41 |
#include "ScriptSourceCode.h" |
|
42 |
#include "ScriptValue.h" |
|
43 |
#include "SecurityOrigin.h" |
|
44 |
#include "WorkerLocation.h" |
|
45 |
#include "WorkerNavigator.h" |
|
46 |
#include "WorkerObjectProxy.h" |
|
47 |
#include "WorkerScriptLoader.h" |
|
48 |
#include "WorkerThread.h" |
|
49 |
#include "WorkerThreadableLoader.h" |
|
50 |
#include "XMLHttpRequestException.h" |
|
51 |
#include <wtf/RefPtr.h> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
#include <wtf/UnusedParam.h> |
0 | 53 |
|
54 |
#if ENABLE(NOTIFICATIONS) |
|
55 |
#include "NotificationCenter.h" |
|
56 |
#endif |
|
57 |
||
58 |
namespace WebCore { |
|
59 |
||
60 |
WorkerContext::WorkerContext(const KURL& url, const String& userAgent, WorkerThread* thread) |
|
61 |
: m_url(url) |
|
62 |
, m_userAgent(userAgent) |
|
63 |
, m_script(new WorkerScriptController(this)) |
|
64 |
, m_thread(thread) |
|
65 |
, m_closing(false) |
|
66 |
{ |
|
67 |
setSecurityOrigin(SecurityOrigin::create(url)); |
|
68 |
} |
|
69 |
||
70 |
WorkerContext::~WorkerContext() |
|
71 |
{ |
|
72 |
ASSERT(currentThread() == thread()->threadID()); |
|
73 |
#if ENABLE(NOTIFICATIONS) |
|
74 |
m_notifications.clear(); |
|
75 |
#endif |
|
76 |
// Notify proxy that we are going away. This can free the WorkerThread object, so do not access it after this. |
|
77 |
thread()->workerReportingProxy().workerContextDestroyed(); |
|
78 |
} |
|
79 |
||
80 |
ScriptExecutionContext* WorkerContext::scriptExecutionContext() const |
|
81 |
{ |
|
82 |
return const_cast<WorkerContext*>(this); |
|
83 |
} |
|
84 |
||
85 |
const KURL& WorkerContext::virtualURL() const |
|
86 |
{ |
|
87 |
return m_url; |
|
88 |
} |
|
89 |
||
90 |
KURL WorkerContext::virtualCompleteURL(const String& url) const |
|
91 |
{ |
|
92 |
return completeURL(url); |
|
93 |
} |
|
94 |
||
95 |
KURL WorkerContext::completeURL(const String& url) const |
|
96 |
{ |
|
97 |
// Always return a null URL when passed a null string. |
|
98 |
// FIXME: Should we change the KURL constructor to have this behavior? |
|
99 |
if (url.isNull()) |
|
100 |
return KURL(); |
|
101 |
// Always use UTF-8 in Workers. |
|
102 |
return KURL(m_url, url); |
|
103 |
} |
|
104 |
||
105 |
String WorkerContext::userAgent(const KURL&) const |
|
106 |
{ |
|
107 |
return m_userAgent; |
|
108 |
} |
|
109 |
||
110 |
WorkerLocation* WorkerContext::location() const |
|
111 |
{ |
|
112 |
if (!m_location) |
|
113 |
m_location = WorkerLocation::create(m_url); |
|
114 |
return m_location.get(); |
|
115 |
} |
|
116 |
||
117 |
void WorkerContext::close() |
|
118 |
{ |
|
119 |
if (m_closing) |
|
120 |
return; |
|
121 |
||
122 |
m_closing = true; |
|
123 |
// Notify parent that this context is closed. Parent is responsible for calling WorkerThread::stop(). |
|
124 |
thread()->workerReportingProxy().workerContextClosed(); |
|
125 |
} |
|
126 |
||
127 |
WorkerNavigator* WorkerContext::navigator() const |
|
128 |
{ |
|
129 |
if (!m_navigator) |
|
130 |
m_navigator = WorkerNavigator::create(m_userAgent); |
|
131 |
return m_navigator.get(); |
|
132 |
} |
|
133 |
||
134 |
bool WorkerContext::hasPendingActivity() const |
|
135 |
{ |
|
136 |
ActiveDOMObjectsMap& activeObjects = activeDOMObjects(); |
|
137 |
ActiveDOMObjectsMap::const_iterator activeObjectsEnd = activeObjects.end(); |
|
138 |
for (ActiveDOMObjectsMap::const_iterator iter = activeObjects.begin(); iter != activeObjectsEnd; ++iter) { |
|
139 |
if (iter->first->hasPendingActivity()) |
|
140 |
return true; |
|
141 |
} |
|
142 |
||
143 |
// Keep the worker active as long as there is a MessagePort with pending activity or that is remotely entangled. |
|
144 |
HashSet<MessagePort*>::const_iterator messagePortsEnd = messagePorts().end(); |
|
145 |
for (HashSet<MessagePort*>::const_iterator iter = messagePorts().begin(); iter != messagePortsEnd; ++iter) { |
|
146 |
if ((*iter)->hasPendingActivity() || ((*iter)->isEntangled() && !(*iter)->locallyEntangledPort())) |
|
147 |
return true; |
|
148 |
} |
|
149 |
||
150 |
return false; |
|
151 |
} |
|
152 |
||
153 |
void WorkerContext::resourceRetrievedByXMLHttpRequest(unsigned long, const ScriptString&) |
|
154 |
{ |
|
155 |
// FIXME: The implementation is pending the fixes in https://bugs.webkit.org/show_bug.cgi?id=23175 |
|
156 |
notImplemented(); |
|
157 |
} |
|
158 |
||
159 |
void WorkerContext::scriptImported(unsigned long, const String&) |
|
160 |
{ |
|
161 |
// FIXME: The implementation is pending the fixes in https://bugs.webkit.org/show_bug.cgi?id=23175 |
|
162 |
notImplemented(); |
|
163 |
} |
|
164 |
||
165 |
void WorkerContext::postTask(PassRefPtr<Task> task) |
|
166 |
{ |
|
167 |
thread()->runLoop().postTask(task); |
|
168 |
} |
|
169 |
||
170 |
int WorkerContext::setTimeout(ScheduledAction* action, int timeout) |
|
171 |
{ |
|
172 |
return DOMTimer::install(scriptExecutionContext(), action, timeout, true); |
|
173 |
} |
|
174 |
||
175 |
void WorkerContext::clearTimeout(int timeoutId) |
|
176 |
{ |
|
177 |
DOMTimer::removeById(scriptExecutionContext(), timeoutId); |
|
178 |
} |
|
179 |
||
180 |
int WorkerContext::setInterval(ScheduledAction* action, int timeout) |
|
181 |
{ |
|
182 |
return DOMTimer::install(scriptExecutionContext(), action, timeout, false); |
|
183 |
} |
|
184 |
||
185 |
void WorkerContext::clearInterval(int timeoutId) |
|
186 |
{ |
|
187 |
DOMTimer::removeById(scriptExecutionContext(), timeoutId); |
|
188 |
} |
|
189 |
||
190 |
void WorkerContext::importScripts(const Vector<String>& urls, const String& callerURL, int callerLine, ExceptionCode& ec) |
|
191 |
{ |
|
192 |
#if !ENABLE(INSPECTOR) |
|
193 |
UNUSED_PARAM(callerURL); |
|
194 |
UNUSED_PARAM(callerLine); |
|
195 |
#endif |
|
196 |
ec = 0; |
|
197 |
Vector<String>::const_iterator urlsEnd = urls.end(); |
|
198 |
Vector<KURL> completedURLs; |
|
199 |
for (Vector<String>::const_iterator it = urls.begin(); it != urlsEnd; ++it) { |
|
200 |
const KURL& url = scriptExecutionContext()->completeURL(*it); |
|
201 |
if (!url.isValid()) { |
|
202 |
ec = SYNTAX_ERR; |
|
203 |
return; |
|
204 |
} |
|
205 |
completedURLs.append(url); |
|
206 |
} |
|
207 |
Vector<KURL>::const_iterator end = completedURLs.end(); |
|
208 |
||
209 |
for (Vector<KURL>::const_iterator it = completedURLs.begin(); it != end; ++it) { |
|
210 |
WorkerScriptLoader scriptLoader; |
|
211 |
scriptLoader.loadSynchronously(scriptExecutionContext(), *it, AllowCrossOriginRequests); |
|
212 |
||
213 |
// If the fetching attempt failed, throw a NETWORK_ERR exception and abort all these steps. |
|
214 |
if (scriptLoader.failed()) { |
|
215 |
ec = XMLHttpRequestException::NETWORK_ERR; |
|
216 |
return; |
|
217 |
} |
|
218 |
||
219 |
scriptExecutionContext()->scriptImported(scriptLoader.identifier(), scriptLoader.script()); |
|
220 |
#if ENABLE(INSPECTOR) |
|
221 |
scriptExecutionContext()->addMessage(InspectorControllerDestination, JSMessageSource, LogMessageType, LogMessageLevel, "Worker script imported: \"" + *it + "\".", callerLine, callerURL); |
|
222 |
#endif |
|
223 |
||
224 |
ScriptValue exception; |
|
225 |
m_script->evaluate(ScriptSourceCode(scriptLoader.script(), *it), &exception); |
|
226 |
if (!exception.hasNoValue()) { |
|
227 |
m_script->setException(exception); |
|
228 |
return; |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
void WorkerContext::reportException(const String& errorMessage, int lineNumber, const String& sourceURL) |
|
234 |
{ |
|
235 |
bool errorHandled = false; |
|
236 |
if (onerror()) |
|
237 |
errorHandled = onerror()->reportError(this, errorMessage, sourceURL, lineNumber); |
|
238 |
||
239 |
if (!errorHandled) |
|
240 |
thread()->workerReportingProxy().postExceptionToWorkerObject(errorMessage, lineNumber, sourceURL); |
|
241 |
} |
|
242 |
||
243 |
void WorkerContext::addMessage(MessageDestination destination, MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceURL) |
|
244 |
{ |
|
245 |
thread()->workerReportingProxy().postConsoleMessageToWorkerObject(destination, source, type, level, message, lineNumber, sourceURL); |
|
246 |
} |
|
247 |
||
248 |
#if ENABLE(NOTIFICATIONS) |
|
249 |
NotificationCenter* WorkerContext::webkitNotifications() const |
|
250 |
{ |
|
251 |
if (!m_notifications) |
|
252 |
m_notifications = NotificationCenter::create(scriptExecutionContext(), m_thread->getNotificationPresenter()); |
|
253 |
return m_notifications.get(); |
|
254 |
} |
|
255 |
#endif |
|
256 |
||
257 |
EventTargetData* WorkerContext::eventTargetData() |
|
258 |
{ |
|
259 |
return &m_eventTargetData; |
|
260 |
} |
|
261 |
||
262 |
EventTargetData* WorkerContext::ensureEventTargetData() |
|
263 |
{ |
|
264 |
return &m_eventTargetData; |
|
265 |
} |
|
266 |
||
267 |
} // namespace WebCore |
|
268 |
||
269 |
#endif // ENABLE(WORKERS) |