Flutter Engine
The Flutter Engine
handle_waiter.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_waiter.h"
6
7#include <lib/async/default.h>
8
9#include "handle.h"
17
20using tonic::ToDart;
21
22namespace zircon {
23namespace dart {
24
26
27#define FOR_EACH_BINDING(V) V(HandleWaiter, Cancel)
28
30
31void HandleWaiter::RegisterNatives(tonic::DartLibraryNatives* natives) {
32 natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
33}
34
36 zx_signals_t signals,
38 return fml::MakeRefCounted<HandleWaiter>(handle, signals, callback);
39}
40
41HandleWaiter::HandleWaiter(Handle* handle,
42 zx_signals_t signals,
44 : wait_(this, handle->handle(), signals),
45 handle_(handle),
46 callback_(DartState::Current(), callback) {
47 FML_CHECK(handle_ != nullptr);
48 FML_CHECK(handle_->is_valid());
49
50 zx_status_t status = wait_.Begin(async_get_default_dispatcher());
51 FML_DCHECK(status == ZX_OK);
52}
53
54HandleWaiter::~HandleWaiter() {
55 Cancel();
56}
57
59 FML_DCHECK(wait_.is_pending() == !!handle_);
60 if (handle_) {
61 // Cancel the wait.
62 wait_.Cancel();
63
64 // Release this object from the handle and clear handle_.
65 handle_->ReleaseWaiter(this);
66 handle_ = nullptr;
67 }
68 FML_DCHECK(!wait_.is_pending());
69}
70
71void HandleWaiter::OnWaitComplete(async_dispatcher_t* dispatcher,
72 async::WaitBase* wait,
73 zx_status_t status,
74 const zx_packet_signal_t* signal) {
75 FML_DCHECK(handle_);
76
77 FML_DCHECK(!callback_.is_empty());
78
79 // Hold a reference to this object.
81
82 // Remove this waiter from the handle.
83 handle_->ReleaseWaiter(this);
84
85 // Clear handle_.
86 handle_ = nullptr;
87
88 auto state = callback_.dart_state().lock();
90 DartState::Scope scope(state);
91
92 // Put the closure invocation on the microtask queue.
93 Dart_Handle zircon_lib = Dart_LookupLibrary(ToDart("dart:zircon"));
95
96 Dart_Handle owc_type =
97 Dart_GetClass(zircon_lib, ToDart("_OnWaitCompleteClosure"));
99
100 FML_DCHECK(!callback_.is_empty());
101 std::vector<Dart_Handle> owc_args{callback_.Release(), ToDart(status),
102 ToDart(signal->observed)};
103 Dart_Handle owc =
104 Dart_New(owc_type, Dart_Null(), owc_args.size(), owc_args.data());
106
107 Dart_Handle closure = Dart_GetField(owc, ToDart("_closure"));
109
110 // TODO(issue#tbd): Use tonic::DartMicrotaskQueue::ScheduleMicrotask()
111 // instead when tonic::DartState gets a microtask queue field.
112 Dart_Handle async_lib = Dart_LookupLibrary(ToDart("dart:async"));
114 std::vector<Dart_Handle> sm_args{closure};
115 Dart_Handle sm_result = Dart_Invoke(async_lib, ToDart("scheduleMicrotask"),
116 sm_args.size(), sm_args.data());
118}
119
120} // namespace dart
121} // namespace zircon
const std::weak_ptr< DartState > & dart_state() const
static fml::RefPtr< HandleWaiter > Create(Handle *handle, zx_signals_t signals, Dart_Handle callback)
bool is_valid() const
Definition: handle.h:51
void ReleaseWaiter(HandleWaiter *waiter)
Definition: handle.cc:76
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_Invoke(Dart_Handle target, Dart_Handle name, int number_of_arguments, Dart_Handle *arguments)
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_New(Dart_Handle type, Dart_Handle constructor_name, int number_of_arguments, Dart_Handle *arguments)
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle class_name)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name)
#define DART_NATIVE_NO_UI_CHECK_CALLBACK(CLASS, METHOD)
#define DART_REGISTER_NATIVE(CLASS, METHOD)
AtkStateType state
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static guint signals[kSignalLastSignal]
Definition: fl_settings.cc:16
#define FML_CHECK(condition)
Definition: logging.h:85
#define FML_DCHECK(condition)
Definition: logging.h:103
#define FOR_EACH_BINDING(V)
Definition: dart_vm.cc:33
std::function< void()> closure
Definition: closure.h:14
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
Definition: dart_error.cc:33
Dart_Handle DartInvokeField(Dart_Handle target, const char *name, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:12
IMPLEMENT_WRAPPERTYPEINFO(zircon, Handle)
Definition: handle.cc:14
fuchsia::ui::composition::ParentViewportWatcherHandle handle_