summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/streams/stream_algorithms.h
blob: 5362af4ed9d867b37dc64da0eeeb7fb7b50d1e0e (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
// Copyright 2019 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_STREAM_ALGORITHMS_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_STREAM_ALGORITHMS_H_

#include "base/optional.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/heap/visitor.h"
#include "v8/include/v8.h"

namespace blink {

class ExceptionState;
class ScriptState;

// Base class for algorithms that calculate the size of a given chunk as part of
// the stream's queuing strategy. This is the type for the
// [[strategySizeAlgorithm]] internal slots in the standard; see for example
// https://streams.spec.whatwg.org/#rs-default-controller-internal-slots.
// Subclasses may refer to JavaScript functions and so objects of this type must
// always be reachable by V8's garbage collector.
class StrategySizeAlgorithm : public GarbageCollected<StrategySizeAlgorithm> {
 public:
  virtual ~StrategySizeAlgorithm() = default;

  virtual base::Optional<double> Run(ScriptState*,
                                     v8::Local<v8::Value> chunk,
                                     ExceptionState&) = 0;

  virtual void Trace(Visitor*) const {}
};

// Base class for start algorithms, ie. those that are derived from the start()
// method of the underlying object. These differ from other underlying
// algorithms in that they can throw synchronously. Objects of this
// type must always be reachable by V8's garbage collector.
class StreamStartAlgorithm : public GarbageCollected<StreamStartAlgorithm> {
 public:
  virtual ~StreamStartAlgorithm() = default;

  virtual v8::MaybeLocal<v8::Promise> Run(ScriptState*, ExceptionState&) = 0;

  virtual void Trace(Visitor*) const {}
};

// Base class for algorithms which take one or more arguments and return a
// Promise. This is used as the type for all the algorithms in the standard that
// do not use StrategySizeAlgorithm or StreamStartAlgorithm. Objects of this
// type must always be reachable by V8's garbage collector.
class StreamAlgorithm : public GarbageCollected<StreamAlgorithm> {
 public:
  virtual ~StreamAlgorithm() = default;

  virtual v8::Local<v8::Promise> Run(ScriptState*,
                                     int argc,
                                     v8::Local<v8::Value> argv[]) = 0;

  virtual void Trace(Visitor*) const {}
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_STREAM_ALGORITHMS_H_