summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/streams/stream_promise_resolver.h
blob: 02a7014e9b7b866e0859a40d25ddec53b63e51b2 (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
// 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_PROMISE_RESOLVER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_STREAM_PROMISE_RESOLVER_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "v8/include/v8.h"

namespace blink {

class ScriptPromise;
class ScriptState;
class Visitor;

// A thin wrapper around v8::Promise::Resolver that matches the semantics used
// for promises in the standard. StreamPromiseResolver is used for promises that
// need to be stored somewhere: promises on the stack should generally use
// v8::Local<v8::Promise>, or ScriptPromise if they are to be returned to the
// bindings code.
class CORE_EXPORT StreamPromiseResolver final
    : public GarbageCollected<StreamPromiseResolver> {
 public:
  // Implements "a promise rejected with" from the INFRA standard.
  // https://www.w3.org/2001/tag/doc/promises-guide/#a-promise-rejected-with
  // Prefer PromiseResolve() in miscellaneous_operations.h if the value is to be
  // returned to JavaScript without storing it. This function should not be used
  // when |value| might be a Promise already, as it will not pass it through
  // unchanged at the standard requires.
  static StreamPromiseResolver* CreateResolved(ScriptState*,
                                               v8::Local<v8::Value> value);

  // Implements "a promise resolved with *undefined*". Prefer
  // PromiseResolveWithUndefined() from miscellaneous_operations.h if the value
  // is to be returned to JavaScript without storing it.
  static StreamPromiseResolver* CreateResolvedWithUndefined(ScriptState*);

  // Implements "a promise rejected with" from the INFRA standard.
  // https://www.w3.org/2001/tag/doc/promises-guide/#a-promise-rejected-with
  // Prefer PromiseResolveWithUndefined() from miscellaneous_operations.h if the
  // value is to be returned directly to JavaScript.
  static StreamPromiseResolver* CreateRejected(ScriptState*,
                                               v8::Local<v8::Value> reason);

  // Creates an initialised promise.
  explicit StreamPromiseResolver(ScriptState*);
  ~StreamPromiseResolver();

  // Resolves the promise with |value|. Does nothing if the promise is already
  // settled.
  void Resolve(ScriptState*, v8::Local<v8::Value> value);

  // Resolves the promise with undefined.
  void ResolveWithUndefined(ScriptState*);

  // Rejects the promise with |reason|. Does nothing if the promise is already
  // resolved.
  void Reject(ScriptState*, v8::Local<v8::Value> reason);

  // Returns the promise wrapped in a ScriptPromise.
  ScriptPromise GetScriptPromise(ScriptState* script_state) const;

  // Returns the promise as a v8::Promise.
  v8::Local<v8::Promise> V8Promise(v8::Isolate* isolate) const;

  // Marks the promise is handled, so if it is rejected it won't be considered
  // an unhandled rejection.
  void MarkAsHandled(v8::Isolate*);

  // Returns the state of the promise, one of pending, fulfilled or rejected.
  v8::Promise::PromiseState State(v8::Isolate*) const;

  // Returns true if the the promise is not pending.
  bool IsSettled() const { return is_settled_; }

  void Trace(Visitor*);

 private:
  TraceWrapperV8Reference<v8::Promise::Resolver> resolver_;
  bool is_settled_ = false;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_STREAM_PROMISE_RESOLVER_H_