Index: third_party/WebKit/Source/modules/worklet/Worklet.cpp |
diff --git a/third_party/WebKit/Source/modules/worklet/Worklet.cpp b/third_party/WebKit/Source/modules/worklet/Worklet.cpp |
index 3fd3be792e071234c9363190bce002d9d28b866e..8dc693c8a12a324f5504bf1019417fec797a571a 100644 |
--- a/third_party/WebKit/Source/modules/worklet/Worklet.cpp |
+++ b/third_party/WebKit/Source/modules/worklet/Worklet.cpp |
@@ -4,24 +4,97 @@ |
#include "modules/worklet/Worklet.h" |
+#include "bindings/core/v8/ScriptPromiseResolver.h" |
+#include "bindings/core/v8/ScriptSourceCode.h" |
#include "bindings/core/v8/V8Binding.h" |
+#include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
+#include "core/dom/DOMException.h" |
+#include "core/dom/ExceptionCode.h" |
namespace blink { |
// static |
Worklet* Worklet::create(ExecutionContext* executionContext) |
{ |
- return new Worklet(executionContext); |
+ Worklet* worklet = new Worklet(executionContext); |
+ worklet->suspendIfNeeded(); |
+ return worklet; |
} |
Worklet::Worklet(ExecutionContext* executionContext) |
- : m_workletGlobalScope(WorkletGlobalScope::create(executionContext->url(), executionContext->userAgent(), toIsolate(executionContext))) |
+ : ActiveDOMObject(executionContext) |
+ , m_workletGlobalScope(WorkletGlobalScope::create(executionContext->url(), executionContext->userAgent(), executionContext->securityOrigin(), toIsolate(executionContext))) |
{ |
} |
+ScriptPromise Worklet::import(ScriptState* scriptState, const String& url) |
+{ |
+ KURL scriptURL = executionContext()->completeURL(url); |
+ if (!scriptURL.isValid()) { |
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(SyntaxError, "'" + url + "' is not a valid URL.")); |
+ } |
+ |
+ // TODO(ikilpatrick): Perform upfront CSP checks once we decide on a |
+ // CSP-policy for worklets. |
+ |
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
+ m_resolvers.append(resolver); |
+ |
+ ScriptPromise promise = resolver->promise(); |
+ |
+ // TODO(ikilpatrick): WorkerScriptLoader will need to be extended to allow |
+ // module loading support. For now just fetch a 'classic' script. |
+ m_scriptLoaders.append(WorkerScriptLoader::create()); |
+ m_scriptLoaders.last()->loadAsynchronously(*executionContext(), scriptURL, DenyCrossOriginRequests, |
+ bind(&Worklet::onResponse, this), |
+ bind(&Worklet::onFinished, this, m_scriptLoaders.last().get(), resolver)); |
kinuko
2016/02/12 07:25:44
In some error cases onFinished could run before re
ikilpatrick
2016/02/12 22:59:57
Done.
|
+ |
+ return promise; |
+} |
+ |
+void Worklet::onResponse() |
+{ |
+ // TODO(ikilpatrick): Add devtools instrumentation on worklet script |
+ // resource loading. |
+} |
+ |
+void Worklet::onFinished(WorkerScriptLoader* scriptLoader, ScriptPromiseResolver* resolver) |
+{ |
+ if (scriptLoader->failed()) { |
+ resolver->reject(DOMException::create(NetworkError)); |
+ } else { |
+ m_workletGlobalScope->script()->evaluate(ScriptSourceCode(scriptLoader->script(), scriptLoader->url())); |
kinuko
2016/02/12 07:25:45
Should we catch error event here?
ikilpatrick
2016/02/12 22:59:57
Added note for the current thinking at the moment.
|
+ resolver->resolve(); |
kinuko
2016/02/12 07:25:45
Maybe check resolver->executionContext() || resolv
ikilpatrick
2016/02/12 22:59:57
Added guard for the moment.
|
+ } |
+ |
+ size_t index = m_scriptLoaders.find(scriptLoader); |
+ |
+ ASSERT(index != kNotFound); |
+ ASSERT(m_resolvers[index] == resolver); |
+ |
+ m_scriptLoaders.remove(index); |
+ m_resolvers.remove(index); |
+} |
+ |
+void Worklet::stop() |
+{ |
+ m_workletGlobalScope->script()->willScheduleExecutionTermination(); |
+ |
+ for (auto scriptLoader : m_scriptLoaders) { |
+ scriptLoader->cancel(); |
+ } |
+} |
+ |
+bool Worklet::hasPendingActivity() const |
+{ |
+ return !m_scriptLoaders.isEmpty(); |
+} |
+ |
DEFINE_TRACE(Worklet) |
{ |
+ visitor->trace(m_resolvers); |
visitor->trace(m_workletGlobalScope); |
+ ActiveDOMObject::trace(visitor); |
} |
} // namespace blink |