This source file includes following definitions.
- create
- performTask
- isCleanupTask
- m_timeOrigin
- applyContentSecurityPolicyFromString
- executionContext
- virtualURL
- virtualCompleteURL
- completeURL
- userAgent
- disableEval
- timerAlignmentInterval
- location
- close
- console
- navigator
- postTask
- clearInspector
- willStopActiveDOMObjects
- dispose
- importScripts
- errorEventTarget
- logExceptionToConsole
- reportBlockedScriptExecutionToInspector
- addMessage
- addMessageToWorkerConsole
- isContextThread
- isJSExecutionForbidden
- idleNotification
- eventQueue
- trace
#include "config.h"
#include "core/workers/WorkerGlobalScope.h"
#include "bindings/v8/ExceptionState.h"
#include "bindings/v8/ScheduledAction.h"
#include "bindings/v8/ScriptSourceCode.h"
#include "bindings/v8/ScriptValue.h"
#include "core/dom/ActiveDOMObject.h"
#include "core/dom/AddConsoleMessageTask.h"
#include "core/dom/ContextLifecycleNotifier.h"
#include "core/dom/DOMURL.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/MessagePort.h"
#include "core/events/ErrorEvent.h"
#include "core/events/Event.h"
#include "core/inspector/InspectorConsoleInstrumentation.h"
#include "core/inspector/ScriptCallStack.h"
#include "core/inspector/WorkerInspectorController.h"
#include "core/loader/WorkerThreadableLoader.h"
#include "core/frame/DOMWindow.h"
#include "core/workers/WorkerNavigator.h"
#include "core/workers/WorkerClients.h"
#include "core/workers/WorkerLocation.h"
#include "core/workers/WorkerReportingProxy.h"
#include "core/workers/WorkerScriptLoader.h"
#include "core/workers/WorkerThread.h"
#include "platform/network/ContentSecurityPolicyParsers.h"
#include "platform/weborigin/KURL.h"
#include "platform/weborigin/SecurityOrigin.h"
namespace WebCore {
class CloseWorkerGlobalScopeTask : public ExecutionContextTask {
public:
static PassOwnPtr<CloseWorkerGlobalScopeTask> create()
{
return adoptPtr(new CloseWorkerGlobalScopeTask);
}
virtual void performTask(ExecutionContext *context)
{
WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context);
workerGlobalScope->thread()->workerReportingProxy().workerGlobalScopeClosed();
}
virtual bool isCleanupTask() const { return true; }
};
WorkerGlobalScope::WorkerGlobalScope(const KURL& url, const String& userAgent, WorkerThread* thread, double timeOrigin, PassOwnPtr<WorkerClients> workerClients)
: m_url(url)
, m_userAgent(userAgent)
, m_script(adoptPtr(new WorkerScriptController(*this)))
, m_thread(thread)
, m_workerInspectorController(adoptPtr(new WorkerInspectorController(this)))
, m_closing(false)
, m_eventQueue(WorkerEventQueue::create(this))
, m_workerClients(workerClients)
, m_timeOrigin(timeOrigin)
{
ScriptWrappable::init(this);
setClient(this);
setSecurityOrigin(SecurityOrigin::create(url));
m_workerClients->reattachThread();
}
WorkerGlobalScope::~WorkerGlobalScope()
{
#if !ENABLE(OILPAN)
dispose();
#endif
}
void WorkerGlobalScope::applyContentSecurityPolicyFromString(const String& policy, ContentSecurityPolicyHeaderType contentSecurityPolicyType)
{
setContentSecurityPolicy(ContentSecurityPolicy::create(this));
contentSecurityPolicy()->didReceiveHeader(policy, contentSecurityPolicyType, ContentSecurityPolicyHeaderSourceHTTP);
}
ExecutionContext* WorkerGlobalScope::executionContext() const
{
return const_cast<WorkerGlobalScope*>(this);
}
const KURL& WorkerGlobalScope::virtualURL() const
{
return m_url;
}
KURL WorkerGlobalScope::virtualCompleteURL(const String& url) const
{
return completeURL(url);
}
KURL WorkerGlobalScope::completeURL(const String& url) const
{
if (url.isNull())
return KURL();
return KURL(m_url, url);
}
String WorkerGlobalScope::userAgent(const KURL&) const
{
return m_userAgent;
}
void WorkerGlobalScope::disableEval(const String& errorMessage)
{
m_script->disableEval(errorMessage);
}
double WorkerGlobalScope::timerAlignmentInterval() const
{
return DOMTimer::visiblePageAlignmentInterval();
}
WorkerLocation* WorkerGlobalScope::location() const
{
if (!m_location)
m_location = WorkerLocation::create(m_url);
return m_location.get();
}
void WorkerGlobalScope::close()
{
if (m_closing)
return;
m_closing = true;
postTask(CloseWorkerGlobalScopeTask::create());
}
WorkerConsole* WorkerGlobalScope::console()
{
if (!m_console)
m_console = WorkerConsole::create(this);
return m_console.get();
}
WorkerNavigator* WorkerGlobalScope::navigator() const
{
if (!m_navigator)
m_navigator = WorkerNavigator::create(m_userAgent);
return m_navigator.get();
}
void WorkerGlobalScope::postTask(PassOwnPtr<ExecutionContextTask> task)
{
thread()->runLoop().postTask(task);
}
void WorkerGlobalScope::clearInspector()
{
m_workerInspectorController.clear();
}
void WorkerGlobalScope::willStopActiveDOMObjects()
{
lifecycleNotifier().notifyWillStopActiveDOMObjects();
}
void WorkerGlobalScope::dispose()
{
ASSERT(thread()->isCurrentThread());
clearScript();
clearInspector();
setClient(0);
}
void WorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionState& exceptionState)
{
ASSERT(contentSecurityPolicy());
Vector<String>::const_iterator urlsEnd = urls.end();
Vector<KURL> completedURLs;
for (Vector<String>::const_iterator it = urls.begin(); it != urlsEnd; ++it) {
const KURL& url = executionContext()->completeURL(*it);
if (!url.isValid()) {
exceptionState.throwDOMException(SyntaxError, "The URL '" + *it + "' is invalid.");
return;
}
completedURLs.append(url);
}
Vector<KURL>::const_iterator end = completedURLs.end();
for (Vector<KURL>::const_iterator it = completedURLs.begin(); it != end; ++it) {
RefPtr<WorkerScriptLoader> scriptLoader(WorkerScriptLoader::create());
scriptLoader->setTargetType(ResourceRequest::TargetIsScript);
scriptLoader->loadSynchronously(executionContext(), *it, AllowCrossOriginRequests);
if (scriptLoader->failed()) {
exceptionState.throwDOMException(NetworkError, "The script at '" + it->elidedString() + "' failed to load.");
return;
}
InspectorInstrumentation::scriptImported(executionContext(), scriptLoader->identifier(), scriptLoader->script());
RefPtrWillBeRawPtr<ErrorEvent> errorEvent = nullptr;
m_script->evaluate(ScriptSourceCode(scriptLoader->script(), scriptLoader->responseURL()), &errorEvent);
if (errorEvent) {
m_script->rethrowExceptionFromImportedScript(errorEvent.release());
return;
}
}
}
EventTarget* WorkerGlobalScope::errorEventTarget()
{
return this;
}
void WorkerGlobalScope::logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtr<ScriptCallStack>)
{
thread()->workerReportingProxy().reportException(errorMessage, lineNumber, columnNumber, sourceURL);
}
void WorkerGlobalScope::reportBlockedScriptExecutionToInspector(const String& directiveText)
{
InspectorInstrumentation::scriptExecutionBlockedByCSP(this, directiveText);
}
void WorkerGlobalScope::addMessage(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, ScriptState* state)
{
if (!isContextThread()) {
postTask(AddConsoleMessageTask::create(source, level, message));
return;
}
thread()->workerReportingProxy().reportConsoleMessage(source, level, message, lineNumber, sourceURL);
addMessageToWorkerConsole(source, level, message, sourceURL, lineNumber, nullptr, state);
}
void WorkerGlobalScope::addMessageToWorkerConsole(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, PassRefPtr<ScriptCallStack> callStack, ScriptState* state)
{
ASSERT(isContextThread());
if (callStack)
InspectorInstrumentation::addMessageToConsole(this, source, LogMessageType, level, message, callStack);
else
InspectorInstrumentation::addMessageToConsole(this, source, LogMessageType, level, message, sourceURL, lineNumber, 0, state);
}
bool WorkerGlobalScope::isContextThread() const
{
return thread()->isCurrentThread();
}
bool WorkerGlobalScope::isJSExecutionForbidden() const
{
return m_script->isExecutionForbidden();
}
bool WorkerGlobalScope::idleNotification()
{
return script()->idleNotification();
}
WorkerEventQueue* WorkerGlobalScope::eventQueue() const
{
return m_eventQueue.get();
}
void WorkerGlobalScope::trace(Visitor* visitor)
{
visitor->trace(m_console);
visitor->trace(m_location);
visitor->trace(m_navigator);
#if ENABLE(OILPAN)
HeapSupplementable<WorkerGlobalScope>::trace(visitor);
#endif
}
}