summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/workers/experimental/worker_task_queue.cc
blob: f88f0acab61c0d0e3de650ff684c5cc3b472fb8c (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
// Copyright 2018 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 "third_party/blink/renderer/core/workers/experimental/worker_task_queue.h"

#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/core/dom/abort_signal.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/workers/experimental/task.h"
#include "third_party/blink/renderer/core/workers/experimental/thread_pool.h"

namespace blink {

WorkerTaskQueue* WorkerTaskQueue::Create(ExecutionContext* context,
                                         const String& type,
                                         ExceptionState& exception_state) {
  if (context->IsContextDestroyed()) {
    exception_state.ThrowDOMException(DOMExceptionCode::kInvalidAccessError,
                                      "The context provided is invalid.");
    return nullptr;
  }

  auto* document = DynamicTo<Document>(context);
  if (!document) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kInvalidAccessError,
        "WorkerTaskQueue can only be constructed from a document.");
    return nullptr;
  }
  DCHECK(type == "user-interaction" || type == "background");
  TaskType task_type = type == "user-interaction" ? TaskType::kUserInteraction
                                                  : TaskType::kIdleTask;
  return MakeGarbageCollected<WorkerTaskQueue>(document, task_type);
}

WorkerTaskQueue::WorkerTaskQueue(Document* document, TaskType task_type)
    : document_(document), task_type_(task_type) {}

ScriptPromise WorkerTaskQueue::postFunction(
    ScriptState* script_state,
    const ScriptValue& function,
    AbortSignal* signal,
    const Vector<ScriptValue>& arguments) {
  DCHECK(document_->IsContextThread());
  DCHECK(function.IsFunction());

  Task* task =
      MakeGarbageCollected<Task>(ThreadPool::From(*document_), script_state,
                                 function, arguments, task_type_);
  if (signal)
    signal->AddAlgorithm(WTF::Bind(&Task::cancel, WrapWeakPersistent(task)));
  return task->result(script_state);
}

Task* WorkerTaskQueue::postTask(ScriptState* script_state,
                                const ScriptValue& function,
                                const Vector<ScriptValue>& arguments) {
  DCHECK(document_->IsContextThread());
  DCHECK(function.IsFunction());
  return MakeGarbageCollected<Task>(ThreadPool::From(*document_), script_state,
                                    function, arguments, task_type_);
}

void WorkerTaskQueue::Trace(blink::Visitor* visitor) {
  ScriptWrappable::Trace(visitor);
  visitor->Trace(document_);
}

}  // namespace blink