summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/modules/worklet/Worklet.cpp
blob: 1b9d032c59a8c260ce6c3000647ce9c5cf81ae3a (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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"
#include "core/inspector/InspectorInstrumentation.h"
#include "modules/worklet/WorkletGlobalScope.h"

namespace blink {

// static
Worklet* Worklet::create(LocalFrame* frame, ExecutionContext* executionContext)
{
    Worklet* worklet = new Worklet(frame, executionContext);
    worklet->suspendIfNeeded();
    return worklet;
}

Worklet::Worklet(LocalFrame* frame, ExecutionContext* executionContext)
    : ActiveDOMObject(executionContext)
    , m_workletGlobalScope(WorkletGlobalScope::create(frame, executionContext->url(), executionContext->userAgent(), executionContext->getSecurityOrigin(), toIsolate(executionContext)))
{
}

ScriptPromise Worklet::import(ScriptState* scriptState, const String& url)
{
    KURL scriptURL = getExecutionContext()->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.

    // NOTE: WorkerScriptLoader may synchronously invoke its callbacks
    // (resolving the promise) before we return it.
    m_scriptLoaders.append(WorkerScriptLoader::create());
    m_scriptLoaders.last()->loadAsynchronously(*getExecutionContext(), scriptURL, DenyCrossOriginRequests,
        getExecutionContext()->securityContext().addressSpace(),
        bind(&Worklet::onResponse, this),
        bind(&Worklet::onFinished, this, m_scriptLoaders.last().get(), resolver));

    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 {
        // TODO(ikilpatrick): Worklets don't have the same error behaviour
        // as workers, etc. For a SyntaxError we should reject, however if
        // the script throws a normal error, resolve. For now just resolve.
        m_workletGlobalScope->scriptController()->evaluate(ScriptSourceCode(scriptLoader->script(), scriptLoader->url()));
        InspectorInstrumentation::scriptImported(m_workletGlobalScope.get(), scriptLoader->identifier(), scriptLoader->script());
        resolver->resolve();
    }

    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->scriptController()->willScheduleExecutionTermination();

    for (auto scriptLoader : m_scriptLoaders) {
        scriptLoader->cancel();
    }
}

DEFINE_TRACE(Worklet)
{
    visitor->trace(m_resolvers);
    visitor->trace(m_workletGlobalScope);
    ActiveDOMObject::trace(visitor);
}

} // namespace blink