Flutter Engine
zircon::dart::Handle Class Reference

#include <handle.h>

Inheritance diagram for zircon::dart::Handle:
fml::RefCountedThreadSafe< Handle > tonic::DartWrappable fml::internal::RefCountedThreadSafeBase

Public Member Functions

 ~Handle ()
 
zx_handle_t ReleaseHandle ()
 
bool is_valid () const
 
zx_handle_t handle () const
 
zx_koid_t koid () const
 
zx_status_t Close ()
 
fml::RefPtr< HandleWaiterAsyncWait (zx_signals_t signals, Dart_Handle callback)
 
void ReleaseWaiter (HandleWaiter *waiter)
 
Dart_Handle Duplicate (uint32_t rights)
 
void ScheduleCallback (tonic::DartPersistentValue callback, zx_status_t status, const zx_packet_signal_t *signal)
 
- Public Member Functions inherited from fml::RefCountedThreadSafe< Handle >
void Release () const
 
- Public Member Functions inherited from fml::internal::RefCountedThreadSafeBase
void AddRef () const
 
bool HasOneRef () const
 
void AssertHasOneRef () const
 
- Public Member Functions inherited from tonic::DartWrappable
 DartWrappable ()
 
virtual const DartWrapperInfoGetDartWrapperInfo () const =0
 
virtual size_t GetAllocationSize () const
 
Dart_Handle CreateDartWrapper (DartState *dart_state)
 
void AssociateWithDartWrapper (Dart_Handle wrappable)
 
void ClearDartWrapper ()
 
Dart_WeakPersistentHandle dart_wrapper () const
 

Static Public Member Functions

static void RegisterNatives (tonic::DartLibraryNatives *natives)
 
static fml::RefPtr< HandleCreate (zx_handle_t handle)
 
static fml::RefPtr< HandleCreate (zx::handle handle)
 
static fml::RefPtr< HandleUnwrap (Dart_Handle handle)
 
static Dart_Handle CreateInvalid ()
 

Additional Inherited Members

- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields {
  kPeerIndex,
  kWrapperInfoIndex,
  kNumberOfNativeFields
}
 
- Protected Member Functions inherited from fml::RefCountedThreadSafe< Handle >
 RefCountedThreadSafe ()
 
 ~RefCountedThreadSafe ()
 
- Protected Member Functions inherited from fml::internal::RefCountedThreadSafeBase
 RefCountedThreadSafeBase ()
 
 ~RefCountedThreadSafeBase ()
 
bool Release () const
 
void Adopt ()
 
- Protected Member Functions inherited from tonic::DartWrappable
virtual ~DartWrappable ()
 
- Static Protected Member Functions inherited from tonic::DartWrappable
static Dart_PersistentHandle GetTypeForWrapper (tonic::DartState *dart_state, const tonic::DartWrapperInfo &wrapper_info)
 

Detailed Description

Handle is the native peer of a Dart object (Handle in dart:zircon) that holds an zx_handle_t. It tracks active waiters on handle too.

Definition at line 25 of file handle.h.

Constructor & Destructor Documentation

◆ ~Handle()

zircon::dart::Handle::~Handle ( )

Definition at line 43 of file handle.cc.

References Close(), FML_DCHECK, and is_valid().

43  {
44  if (is_valid()) {
45  zx_status_t status = Close();
46  FML_DCHECK(status == ZX_OK);
47  }
48 }
#define FML_DCHECK(condition)
Definition: logging.h:86
bool is_valid() const
Definition: handle.h:50
zx_status_t Close()
Definition: handle.cc:75

Member Function Documentation

◆ AsyncWait()

fml::RefPtr< HandleWaiter > zircon::dart::Handle::AsyncWait ( zx_signals_t  signals,
Dart_Handle  callback 
)

Definition at line 83 of file handle.cc.

References zircon::dart::HandleWaiter::Create(), FML_LOG, fml::RefPtr< T >::get(), and is_valid().

Referenced by koid().

84  {
85  if (!is_valid()) {
86  FML_LOG(WARNING) << "Attempt to wait on an invalid handle.";
87  return nullptr;
88  }
89 
91  HandleWaiter::Create(this, signals, callback);
92  waiters_.push_back(waiter.get());
93 
94  return waiter;
95 }
bool is_valid() const
Definition: handle.h:50
#define FML_LOG(severity)
Definition: logging.h:65
static fml::RefPtr< HandleWaiter > Create(Handle *handle, zx_signals_t signals, Dart_Handle callback)
T * get() const
Definition: ref_ptr.h:112

◆ Close()

zx_status_t zircon::dart::Handle::Close ( )

Definition at line 75 of file handle.cc.

References is_valid(), and ReleaseHandle().

Referenced by koid(), and ~Handle().

75  {
76  if (is_valid()) {
77  zx_handle_t handle = ReleaseHandle();
78  return zx_handle_close(handle);
79  }
80  return ZX_ERR_BAD_HANDLE;
81 }
zx_handle_t ReleaseHandle()
Definition: handle.cc:58
zx_handle_t handle() const
Definition: handle.h:52
bool is_valid() const
Definition: handle.h:50

◆ Create() [1/2]

fml::RefPtr< Handle > zircon::dart::Handle::Create ( zx_handle_t  handle)
static

◆ Create() [2/2]

static fml::RefPtr<Handle> zircon::dart::Handle::Create ( zx::handle  handle)
inlinestatic

Definition at line 37 of file handle.h.

References Create().

37  {
38  return Create(handle.release());
39  }
zx_handle_t handle() const
Definition: handle.h:52
static fml::RefPtr< Handle > Create(zx_handle_t handle)
Definition: handle.cc:50

◆ CreateInvalid()

Dart_Handle zircon::dart::Handle::CreateInvalid ( )
static

Definition at line 54 of file handle.cc.

References Create(), and tonic::ToDart().

Referenced by Unwrap().

54  {
55  return ToDart(Create(ZX_HANDLE_INVALID));
56 }
static fml::RefPtr< Handle > Create(zx_handle_t handle)
Definition: handle.cc:50
Dart_Handle ToDart(const T &object)

◆ Duplicate()

Dart_Handle zircon::dart::Handle::Duplicate ( uint32_t  rights)

Definition at line 105 of file handle.cc.

References Create(), is_valid(), and tonic::ToDart().

Referenced by koid().

105  {
106  if (!is_valid()) {
107  return ToDart(Create(ZX_HANDLE_INVALID));
108  }
109 
110  zx_handle_t out_handle;
111  zx_status_t status = zx_handle_duplicate(handle_, rights, &out_handle);
112  if (status != ZX_OK) {
113  return ToDart(Create(ZX_HANDLE_INVALID));
114  }
115  return ToDart(Create(out_handle));
116 }
bool is_valid() const
Definition: handle.h:50
static fml::RefPtr< Handle > Create(zx_handle_t handle)
Definition: handle.cc:50
Dart_Handle ToDart(const T &object)

◆ handle()

zx_handle_t zircon::dart::Handle::handle ( ) const
inline

Definition at line 52 of file handle.h.

Referenced by zircon::dart::HandleWaiter::Create(), Create(), flutter::SceneHost::OnViewStateChanged(), and ReleaseHandle().

52 { return handle_; }

◆ is_valid()

bool zircon::dart::Handle::is_valid ( ) const
inline

Definition at line 50 of file handle.h.

Referenced by AsyncWait(), Close(), zircon::dart::HandleWaiter::Create(), Duplicate(), ReleaseHandle(), and ~Handle().

50 { return handle_ != ZX_HANDLE_INVALID; }

◆ koid()

zx_koid_t zircon::dart::Handle::koid ( ) const
inline

Definition at line 54 of file handle.h.

References fml::internal::RefCountedThreadSafeBase::AddRef(), AsyncWait(), Close(), Duplicate(), fml::RefCountedThreadSafe< Handle >::Release(), ReleaseWaiter(), and ScheduleCallback().

54  {
55  zx_info_handle_basic_t info;
56  zx_status_t status = zx_object_get_info(
57  handle_, ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr, nullptr);
58  return status == ZX_OK ? info.koid : ZX_KOID_INVALID;
59  }

◆ RegisterNatives()

void zircon::dart::Handle::RegisterNatives ( tonic::DartLibraryNatives natives)
static

Definition at line 169 of file handle.cc.

References DART_REGISTER_NATIVE, DART_REGISTER_NATIVE_STATIC_, FOR_EACH_BINDING, and FOR_EACH_STATIC_BINDING.

169  {
172 }
#define DART_REGISTER_NATIVE_STATIC_(CLASS, METHOD)
Definition: handle.cc:163
void Register(std::initializer_list< Entry > entries)
#define FOR_EACH_STATIC_BINDING(V)
Definition: handle.cc:150
#define FOR_EACH_BINDING(V)
Definition: handle.cc:152
#define DART_REGISTER_NATIVE(CLASS, METHOD)

◆ ReleaseHandle()

zx_handle_t zircon::dart::Handle::ReleaseHandle ( )

Definition at line 58 of file handle.cc.

References FML_DCHECK, handle(), and is_valid().

Referenced by Close(), flutter::SceneHost::OnViewStateChanged(), and Unwrap().

58  {
60 
61  zx_handle_t handle = handle_;
62  handle_ = ZX_HANDLE_INVALID;
63  while (waiters_.size()) {
64  // HandleWaiter::Cancel calls Handle::ReleaseWaiter which removes the
65  // HandleWaiter from waiters_.
66  FML_DCHECK(waiters_.back()->is_pending());
67  waiters_.back()->Cancel();
68  }
69 
70  FML_DCHECK(!is_valid());
71 
72  return handle;
73 }
zx_handle_t handle() const
Definition: handle.h:52
#define FML_DCHECK(condition)
Definition: logging.h:86
bool is_valid() const
Definition: handle.h:50

◆ ReleaseWaiter()

void zircon::dart::Handle::ReleaseWaiter ( HandleWaiter waiter)

Definition at line 97 of file handle.cc.

References FML_DCHECK.

Referenced by zircon::dart::HandleWaiter::Cancel(), and koid().

97  {
98  FML_DCHECK(waiter);
99  auto iter = std::find(waiters_.cbegin(), waiters_.cend(), waiter);
100  FML_DCHECK(iter != waiters_.cend());
101  FML_DCHECK(*iter == waiter);
102  waiters_.erase(iter);
103 }
#define FML_DCHECK(condition)
Definition: logging.h:86

◆ ScheduleCallback()

void zircon::dart::Handle::ScheduleCallback ( tonic::DartPersistentValue  callback,
zx_status_t  status,
const zx_packet_signal_t *  signal 
)

Definition at line 118 of file handle.cc.

References tonic::DartPersistentValue::dart_state(), FML_DCHECK, tonic::DartPersistentValue::Get(), tonic::DartPersistentValue::is_empty(), tonic::LogIfError(), tonic::DartPersistentValue::Release(), and tonic::ToDart().

Referenced by koid().

120  {
121  auto state = callback.dart_state().lock();
122  FML_DCHECK(state);
123  tonic::DartState::Scope scope(state);
124 
125  // Make a new _OnWaitCompleteClosure(callback, status, signal->observed).
126  FML_DCHECK(!callback.is_empty());
127  std::vector<Dart_Handle> constructor_args{callback.Release(), ToDart(status),
128  ToDart(signal->observed)};
129  Dart_Handle on_wait_complete_closure =
130  Dart_New(on_wait_completer_type_.Get(), Dart_Null(),
131  constructor_args.size(), constructor_args.data());
132  FML_DCHECK(!tonic::LogIfError(on_wait_complete_closure));
133 
134  // The _callback field contains the thunk:
135  // () => callback(status, signal->observed)
136  Dart_Handle closure =
137  Dart_GetField(on_wait_complete_closure, closure_string_.Get());
138  FML_DCHECK(!tonic::LogIfError(closure));
139 
140  // Put the thunk on the microtask queue by calling scheduleMicrotask().
141  std::vector<Dart_Handle> sm_args{closure};
142  Dart_Handle sm_result =
143  Dart_Invoke(async_lib_.Get(), schedule_microtask_string_.Get(),
144  sm_args.size(), sm_args.data());
145  FML_DCHECK(!tonic::LogIfError(sm_result));
146 }
#define FML_DCHECK(condition)
Definition: logging.h:86
std::function< void()> closure
Definition: closure.h:14
const std::weak_ptr< DartState > & dart_state() const
Dart_Handle ToDart(const T &object)
bool LogIfError(Dart_Handle handle)
Definition: dart_error.cc:15

◆ Unwrap()

static fml::RefPtr<Handle> zircon::dart::Handle::Unwrap ( Dart_Handle  handle)
inlinestatic

Definition at line 41 of file handle.h.

References CreateInvalid(), and ReleaseHandle().


The documentation for this class was generated from the following files: