summaryrefslogtreecommitdiff
path: root/deps/v8/build/fuchsia/fidlgen_js/runtime/zircon.h
blob: b54d35495c1d6b0fd4e829f2a0dbfe2295b8fea3 (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
// 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.

#ifndef BUILD_FUCHSIA_FIDLGEN_JS_RUNTIME_ZIRCON_H_
#define BUILD_FUCHSIA_FIDLGEN_JS_RUNTIME_ZIRCON_H_

#include <memory>

#include "base/containers/flat_set.h"
#include "base/containers/unique_ptr_adapters.h"
#include "base/macros.h"
#include "v8/include/v8.h"

namespace fidljs {

class WaitPromiseImpl;

// A WaitSet is associated with each Isolate and represents all outstanding
// waits that are queued on the dispatcher.
//
// If the wait completes normally, the contained promise is resolved, the
// WaitPromiseImpl is marked as completed, and then deleted (by removing it from
// the pending set).
//
// If the caller shuts down with outstanding waits pending, the asynchronous
// waits are canceled by clearing the set (which deletes all the
// WaitPromiseImpls). If a WaitPromiseImpl has not completed when it is
// destroyed, it cancels the outstanding wait in its destructor.
//
// WaitPromiseImpl is responsible for resolving or rejecting promises. If the
// object was created, but a wait never started it will not have been added to
// the wait set, and so will reject the promise immediately. Otherwise, the
// promise will be resolved or rejected when the asynchronous wait is signaled
// or canceled.
using WaitSet =
    base::flat_set<std::unique_ptr<WaitPromiseImpl>, base::UniquePtrComparator>;

class ZxBindings {
 public:
  // Adds Zircon APIs bindings to |global|, for use by JavaScript callers.
  ZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global);

  // Cleans up attached storage in the isolate added by the bindings, and
  // cancels any pending asynchronous requests. It is important this this be
  // done before the v8 context is torn down.
  ~ZxBindings();

 private:
  v8::Isolate* const isolate_;
  std::unique_ptr<WaitSet> wait_set_;

  DISALLOW_COPY_AND_ASSIGN(ZxBindings);
};

}  // namespace fidljs

#endif  // BUILD_FUCHSIA_FIDLGEN_JS_RUNTIME_ZIRCON_H_