Flutter Engine
handle.h
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 #ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_SDK_EXT_HANDLE_H_
6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_SDK_EXT_HANDLE_H_
7 
8 #include <zircon/syscalls.h>
9 
10 #include <vector>
11 
12 #include "flutter/fml/memory/ref_counted.h"
13 #include "handle_waiter.h"
14 #include "third_party/dart/runtime/include/dart_api.h"
18 
19 namespace zircon {
20 namespace dart {
21 /**
22  * Handle is the native peer of a Dart object (Handle in dart:zircon)
23  * that holds an zx_handle_t. It tracks active waiters on handle too.
24  */
25 class Handle : public fml::RefCountedThreadSafe<Handle>,
26  public tonic::DartWrappable {
27  DEFINE_WRAPPERTYPEINFO();
28  FML_FRIEND_REF_COUNTED_THREAD_SAFE(Handle);
29  FML_FRIEND_MAKE_REF_COUNTED(Handle);
30 
31  public:
32  ~Handle();
33 
34  static void RegisterNatives(tonic::DartLibraryNatives* natives);
35 
36  static fml::RefPtr<Handle> Create(zx_handle_t handle);
37  static fml::RefPtr<Handle> Create(zx::handle handle) {
38  return Create(handle.release());
39  }
40 
41  static fml::RefPtr<Handle> Unwrap(Dart_Handle handle) {
42  return fml::RefPtr<Handle>(
44  }
45 
46  static Dart_Handle CreateInvalid();
47 
48  zx_handle_t ReleaseHandle();
49 
50  bool is_valid() const { return handle_ != ZX_HANDLE_INVALID; }
51 
52  zx_handle_t handle() const { return handle_; }
53 
54  zx_koid_t koid() const {
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  }
60 
61  zx_status_t Close();
62 
63  fml::RefPtr<HandleWaiter> AsyncWait(zx_signals_t signals,
64  Dart_Handle callback);
65 
66  void ReleaseWaiter(HandleWaiter* waiter);
67 
68  Dart_Handle Duplicate(uint32_t rights);
69 
71  zx_status_t status,
72  const zx_packet_signal_t* signal);
73 
74  private:
75  explicit Handle(zx_handle_t handle);
76 
77  void RetainDartWrappableReference() const override { AddRef(); }
78 
79  void ReleaseDartWrappableReference() const override { Release(); }
80 
81  zx_handle_t handle_;
82 
83  std::vector<HandleWaiter*> waiters_;
84 
85  // Some cached persistent handles to make running handle wait completers
86  // faster.
87  tonic::DartPersistentValue async_lib_;
88  tonic::DartPersistentValue closure_string_;
89  tonic::DartPersistentValue on_wait_completer_type_;
90  tonic::DartPersistentValue schedule_microtask_string_;
91 };
92 
93 } // namespace dart
94 } // namespace zircon
95 
96 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_SDK_EXT_HANDLE_H_
zx_handle_t ReleaseHandle()
Definition: handle.cc:58
static Dart_Handle CreateInvalid()
Definition: handle.cc:54
Definition: handle.cc:14
void ReleaseWaiter(HandleWaiter *waiter)
Definition: handle.cc:97
zx_handle_t handle() const
Definition: handle.h:52
fml::RefPtr< HandleWaiter > AsyncWait(zx_signals_t signals, Dart_Handle callback)
Definition: handle.cc:83
bool is_valid() const
Definition: handle.h:50
zx_status_t Close()
Definition: handle.cc:75
static fml::RefPtr< Handle > Create(zx::handle handle)
Definition: handle.h:37
void ScheduleCallback(tonic::DartPersistentValue callback, zx_status_t status, const zx_packet_signal_t *signal)
Definition: handle.cc:118
static fml::RefPtr< Handle > Create(zx_handle_t handle)
Definition: handle.cc:50
Definition: dart_vm.cc:38
static fml::RefPtr< Handle > Unwrap(Dart_Handle handle)
Definition: handle.h:41
zx_koid_t koid() const
Definition: handle.h:54
Dart_Handle Duplicate(uint32_t rights)
Definition: handle.cc:105
static void RegisterNatives(tonic::DartLibraryNatives *natives)
Definition: handle.cc:169