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" 15 #include "third_party/tonic/dart_library_natives.h" 16 #include "third_party/tonic/dart_wrappable.h" 17 #include "third_party/tonic/typed_data/dart_byte_data.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); Create(zx::handle handle)37 static fml::RefPtr<Handle> Create(zx::handle handle) { 38 return Create(handle.release()); 39 } 40 Unwrap(Dart_Handle handle)41 static fml::RefPtr<Handle> Unwrap(Dart_Handle handle) { 42 return fml::RefPtr<Handle>( 43 tonic::DartConverter<zircon::dart::Handle*>::FromDart(handle)); 44 } 45 46 static Dart_Handle CreateInvalid(); 47 48 zx_handle_t ReleaseHandle(); 49 is_valid()50 bool is_valid() const { return handle_ != ZX_HANDLE_INVALID; } 51 handle()52 zx_handle_t handle() const { return handle_; } 53 54 zx_status_t Close(); 55 56 fml::RefPtr<HandleWaiter> AsyncWait(zx_signals_t signals, 57 Dart_Handle callback); 58 59 void ReleaseWaiter(HandleWaiter* waiter); 60 61 Dart_Handle Duplicate(uint32_t rights); 62 63 void ScheduleCallback(tonic::DartPersistentValue callback, 64 zx_status_t status, 65 const zx_packet_signal_t* signal); 66 67 private: 68 explicit Handle(zx_handle_t handle); 69 RetainDartWrappableReference()70 void RetainDartWrappableReference() const override { AddRef(); } 71 ReleaseDartWrappableReference()72 void ReleaseDartWrappableReference() const override { Release(); } 73 74 zx_handle_t handle_; 75 76 std::vector<HandleWaiter*> waiters_; 77 78 // Some cached persistent handles to make running handle wait completers 79 // faster. 80 tonic::DartPersistentValue async_lib_; 81 tonic::DartPersistentValue closure_string_; 82 tonic::DartPersistentValue on_wait_completer_type_; 83 tonic::DartPersistentValue schedule_microtask_string_; 84 }; 85 86 } // namespace dart 87 } // namespace zircon 88 89 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_SDK_EXT_HANDLE_H_ 90