Flutter Engine
The Flutter Engine
handle.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "handle.h"
6
7#include <algorithm>
8
11
12using tonic::ToDart;
13
14namespace zircon {
15namespace dart {
16
18
19Handle::Handle(zx_handle_t handle) : handle_(handle) {}
20
21Handle::~Handle() {
22 if (is_valid()) {
23 zx_status_t status = Close();
24 FML_DCHECK(status == ZX_OK);
25 }
26}
27
29 return fml::MakeRefCounted<Handle>(handle);
30}
31
32Dart_Handle Handle::CreateInvalid() {
33 return ToDart(Create(ZX_HANDLE_INVALID));
34}
35
36zx_handle_t Handle::ReleaseHandle() {
38
39 zx_handle_t handle = handle_;
40 handle_ = ZX_HANDLE_INVALID;
41 cached_koid_ = std::nullopt;
42 while (waiters_.size()) {
43 // HandleWaiter::Cancel calls Handle::ReleaseWaiter which removes the
44 // HandleWaiter from waiters_.
45 FML_DCHECK(waiters_.back()->is_pending());
46 waiters_.back()->Cancel();
47 }
48
50
51 return handle;
52}
53
54zx_status_t Handle::Close() {
55 if (is_valid()) {
56 zx_handle_t handle = ReleaseHandle();
57 return zx_handle_close(handle);
58 }
59 return ZX_ERR_BAD_HANDLE;
60}
61
62fml::RefPtr<HandleWaiter> Handle::AsyncWait(zx_signals_t signals,
64 if (!is_valid()) {
65 FML_LOG(WARNING) << "Attempt to wait on an invalid handle.";
66 return nullptr;
67 }
68
71 waiters_.push_back(waiter.get());
72
73 return waiter;
74}
75
76void Handle::ReleaseWaiter(HandleWaiter* waiter) {
77 FML_DCHECK(waiter);
78 auto iter = std::find(waiters_.cbegin(), waiters_.cend(), waiter);
79 FML_DCHECK(iter != waiters_.cend());
80 FML_DCHECK(*iter == waiter);
81 waiters_.erase(iter);
82}
83
85 if (!is_valid()) {
86 return ToDart(Create(ZX_HANDLE_INVALID));
87 }
88
89 zx_handle_t out_handle;
90 zx_status_t status = zx_handle_duplicate(handle_, rights, &out_handle);
91 if (status != ZX_OK) {
92 return ToDart(Create(ZX_HANDLE_INVALID));
93 }
94 return ToDart(Create(out_handle));
95}
96
97Dart_Handle Handle::Replace(uint32_t rights) {
98 if (!is_valid()) {
99 return ToDart(Create(ZX_HANDLE_INVALID));
100 }
101
102 zx_handle_t out_handle;
103 zx_status_t status = zx_handle_replace(ReleaseHandle(), rights, &out_handle);
104 if (status != ZX_OK) {
105 return ToDart(Create(ZX_HANDLE_INVALID));
106 }
107 return ToDart(Create(out_handle));
108}
109
110// clang-format: off
111
112#define FOR_EACH_STATIC_BINDING(V) V(Handle, CreateInvalid)
113
114#define FOR_EACH_BINDING(V) \
115 V(Handle, handle) \
116 V(Handle, koid) \
117 V(Handle, is_valid) \
118 V(Handle, Close) \
119 V(Handle, AsyncWait) \
120 V(Handle, Duplicate) \
121 V(Handle, Replace)
122
123// clang-format: on
124
125// Tonic is missing a comma.
126#define DART_REGISTER_NATIVE_STATIC_(CLASS, METHOD) \
127 DART_REGISTER_NATIVE_STATIC(CLASS, METHOD),
128
131
132void Handle::RegisterNatives(tonic::DartLibraryNatives* natives) {
135}
136
137} // namespace dart
138} // namespace zircon
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
static bool is_valid(SkISize dim)
int find(T *array, int N, T item)
T * get() const
Definition: ref_ptr.h:116
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
#define DART_NATIVE_CALLBACK_STATIC(CLASS, METHOD)
#define DART_NATIVE_NO_UI_CHECK_CALLBACK(CLASS, METHOD)
#define DART_REGISTER_NATIVE(CLASS, METHOD)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static guint signals[kSignalLastSignal]
Definition: fl_settings.cc:16
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_DCHECK(condition)
Definition: logging.h:103
Definition: dart_vm.cc:33
fml::UniqueFD Duplicate(fml::UniqueFD::element_type descriptor)
Definition: file_posix.cc:123
void Close(PathBuilder *builder)
Definition: tessellator.cc:38
Dart_Handle ToDart(const T &object)
IMPLEMENT_WRAPPERTYPEINFO(zircon, Handle)
Definition: handle.cc:14
fuchsia::ui::composition::ParentViewportWatcherHandle handle_
#define FOR_EACH_STATIC_BINDING(V)
Definition: handle.cc:112
#define FOR_EACH_BINDING(V)
Definition: handle.cc:114
#define DART_REGISTER_NATIVE_STATIC_(CLASS, METHOD)
Definition: handle.cc:126