1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
* Copyright (C) 2009, 2011 Google Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "WorkerGlobalScope.h"
#include "ActiveDOMObject.h"
#include "ContentSecurityPolicy.h"
#include "DOMTimer.h"
#include "DOMURL.h"
#include "DOMWindow.h"
#include "ErrorEvent.h"
#include "Event.h"
#include "EventException.h"
#include "ExceptionCode.h"
#include "InspectorConsoleInstrumentation.h"
#include "MessagePort.h"
#include "ScheduledAction.h"
#include "ScriptSourceCode.h"
#include "SecurityOrigin.h"
#include "URL.h"
#include "WorkerInspectorController.h"
#include "WorkerLocation.h"
#include "WorkerNavigator.h"
#include "WorkerObjectProxy.h"
#include "WorkerScriptLoader.h"
#include "WorkerThread.h"
#include "WorkerThreadableLoader.h"
#include "XMLHttpRequestException.h"
#include <bindings/ScriptValue.h>
#include <inspector/ScriptCallStack.h>
#include <wtf/RefPtr.h>
#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
#include "NotificationCenter.h"
#endif
using namespace Inspector;
namespace WebCore {
WorkerGlobalScope::WorkerGlobalScope(const URL& url, const String& userAgent, std::unique_ptr<GroupSettings> settings, WorkerThread& thread, PassRefPtr<SecurityOrigin> topOrigin)
: m_url(url)
, m_userAgent(userAgent)
, m_groupSettings(WTF::move(settings))
, m_script(std::make_unique<WorkerScriptController>(this))
, m_thread(thread)
#if ENABLE(INSPECTOR)
, m_workerInspectorController(std::make_unique<WorkerInspectorController>(*this))
#endif
, m_closing(false)
, m_eventQueue(*this)
, m_topOrigin(topOrigin)
{
setSecurityOrigin(SecurityOrigin::create(url));
}
WorkerGlobalScope::~WorkerGlobalScope()
{
ASSERT(currentThread() == thread().threadID());
// Make sure we have no observers.
notifyObserversOfStop();
// Notify proxy that we are going away. This can free the WorkerThread object, so do not access it after this.
thread().workerReportingProxy().workerGlobalScopeDestroyed();
}
void WorkerGlobalScope::applyContentSecurityPolicyFromString(const String& policy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType)
{
setContentSecurityPolicy(std::make_unique<ContentSecurityPolicy>(this));
contentSecurityPolicy()->didReceiveHeader(policy, contentSecurityPolicyType);
}
URL WorkerGlobalScope::completeURL(const String& url) const
{
// Always return a null URL when passed a null string.
// FIXME: Should we change the URL constructor to have this behavior?
if (url.isNull())
return URL();
// Always use UTF-8 in Workers.
return URL(m_url, url);
}
String WorkerGlobalScope::userAgent(const URL&) const
{
return m_userAgent;
}
void WorkerGlobalScope::disableEval(const String& errorMessage)
{
m_script->disableEval(errorMessage);
}
WorkerLocation* WorkerGlobalScope::location() const
{
if (!m_location)
m_location = WorkerLocation::create(m_url);
return m_location.get();
}
void WorkerGlobalScope::close()
{
if (m_closing)
return;
// Let current script run to completion but prevent future script evaluations.
// After m_closing is set, all the tasks in the queue continue to be fetched but only
// tasks with isCleanupTask()==true will be executed.
m_closing = true;
postTask({ ScriptExecutionContext::Task::CleanupTask, [] (ScriptExecutionContext& context) {
ASSERT_WITH_SECURITY_IMPLICATION(context.isWorkerGlobalScope());
WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(&context);
// Notify parent that this context is closed. Parent is responsible for calling WorkerThread::stop().
workerGlobalScope->thread().workerReportingProxy().workerGlobalScopeClosed();
} });
}
WorkerNavigator* WorkerGlobalScope::navigator() const
{
if (!m_navigator)
m_navigator = WorkerNavigator::create(m_userAgent);
return m_navigator.get();
}
void WorkerGlobalScope::postTask(Task task)
{
thread().runLoop().postTask(WTF::move(task));
}
int WorkerGlobalScope::setTimeout(std::unique_ptr<ScheduledAction> action, int timeout)
{
return DOMTimer::install(scriptExecutionContext(), WTF::move(action), timeout, true);
}
void WorkerGlobalScope::clearTimeout(int timeoutId)
{
DOMTimer::removeById(scriptExecutionContext(), timeoutId);
}
int WorkerGlobalScope::setInterval(std::unique_ptr<ScheduledAction> action, int timeout)
{
return DOMTimer::install(scriptExecutionContext(), WTF::move(action), timeout, false);
}
void WorkerGlobalScope::clearInterval(int timeoutId)
{
DOMTimer::removeById(scriptExecutionContext(), timeoutId);
}
void WorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionCode& ec)
{
ASSERT(contentSecurityPolicy());
ec = 0;
Vector<String>::const_iterator urlsEnd = urls.end();
Vector<URL> completedURLs;
for (Vector<String>::const_iterator it = urls.begin(); it != urlsEnd; ++it) {
const URL& url = scriptExecutionContext()->completeURL(*it);
if (!url.isValid()) {
ec = SYNTAX_ERR;
return;
}
completedURLs.append(url);
}
Vector<URL>::const_iterator end = completedURLs.end();
for (Vector<URL>::const_iterator it = completedURLs.begin(); it != end; ++it) {
RefPtr<WorkerScriptLoader> scriptLoader(WorkerScriptLoader::create());
scriptLoader->loadSynchronously(scriptExecutionContext(), *it, AllowCrossOriginRequests);
// If the fetching attempt failed, throw a NETWORK_ERR exception and abort all these steps.
if (scriptLoader->failed()) {
ec = XMLHttpRequestException::NETWORK_ERR;
return;
}
InspectorInstrumentation::scriptImported(scriptExecutionContext(), scriptLoader->identifier(), scriptLoader->script());
Deprecated::ScriptValue exception;
m_script->evaluate(ScriptSourceCode(scriptLoader->script(), scriptLoader->responseURL()), &exception);
if (!exception.hasNoValue()) {
m_script->setException(exception);
return;
}
}
}
EventTarget* WorkerGlobalScope::errorEventTarget()
{
return this;
}
void WorkerGlobalScope::logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtr<ScriptCallStack>)
{
thread().workerReportingProxy().postExceptionToWorkerObject(errorMessage, lineNumber, columnNumber, sourceURL);
}
void WorkerGlobalScope::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, unsigned long requestIdentifier)
{
if (!isContextThread()) {
postTask(AddConsoleMessageTask(source, level, message));
return;
}
thread().workerReportingProxy().postConsoleMessageToWorkerObject(source, level, message, 0, 0, String());
addMessageToWorkerConsole(source, level, message, String(), 0, 0, 0, 0, requestIdentifier);
}
void WorkerGlobalScope::addMessage(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack> callStack, JSC::ExecState* state, unsigned long requestIdentifier)
{
if (!isContextThread()) {
postTask(AddConsoleMessageTask(source, level, message));
return;
}
thread().workerReportingProxy().postConsoleMessageToWorkerObject(source, level, message, lineNumber, columnNumber, sourceURL);
addMessageToWorkerConsole(source, level, message, sourceURL, lineNumber, columnNumber, callStack, state, requestIdentifier);
}
void WorkerGlobalScope::addMessageToWorkerConsole(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack> callStack, JSC::ExecState* state, unsigned long requestIdentifier)
{
ASSERT(isContextThread());
if (callStack)
InspectorInstrumentation::addMessageToConsole(this, source, MessageType::Log, level, message, callStack, requestIdentifier);
else
InspectorInstrumentation::addMessageToConsole(this, source, MessageType::Log, level, message, sourceURL, lineNumber, columnNumber, state, requestIdentifier);
}
bool WorkerGlobalScope::isContextThread() const
{
return currentThread() == thread().threadID();
}
bool WorkerGlobalScope::isJSExecutionForbidden() const
{
return m_script->isExecutionForbidden();
}
WorkerGlobalScope::Observer::Observer(WorkerGlobalScope* context)
: m_context(context)
{
ASSERT(m_context && m_context->isContextThread());
m_context->registerObserver(this);
}
WorkerGlobalScope::Observer::~Observer()
{
if (!m_context)
return;
ASSERT(m_context->isContextThread());
m_context->unregisterObserver(this);
}
void WorkerGlobalScope::Observer::stopObserving()
{
if (!m_context)
return;
ASSERT(m_context->isContextThread());
m_context->unregisterObserver(this);
m_context = 0;
}
void WorkerGlobalScope::registerObserver(Observer* observer)
{
ASSERT(observer);
m_workerObservers.add(observer);
}
void WorkerGlobalScope::unregisterObserver(Observer* observer)
{
ASSERT(observer);
m_workerObservers.remove(observer);
}
void WorkerGlobalScope::notifyObserversOfStop()
{
HashSet<Observer*>::iterator iter = m_workerObservers.begin();
while (iter != m_workerObservers.end()) {
WorkerGlobalScope::Observer* observer = *iter;
observer->stopObserving();
observer->notifyStop();
iter = m_workerObservers.begin();
}
}
WorkerEventQueue& WorkerGlobalScope::eventQueue() const
{
return m_eventQueue;
}
#if ENABLE(SUBTLE_CRYPTO)
bool WorkerGlobalScope::wrapCryptoKey(const Vector<uint8_t>&, Vector<uint8_t>&)
{
return false;
}
bool WorkerGlobalScope::unwrapCryptoKey(const Vector<uint8_t>&, Vector<uint8_t>&)
{
return false;
}
#endif // ENABLE(SUBTLE_CRYPTO)
} // namespace WebCore
|