Flutter Engine
 
Loading...
Searching...
No Matches
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,
37 Dart_Handle callback) {
38 return fml::MakeRefCounted<HandleWaiter>(handle, signals, callback);
39}
40
41HandleWaiter::HandleWaiter(Handle* handle,
42 zx_signals_t signals,
43 Dart_Handle callback)
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();
89 FML_DCHECK(state);
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
bool is_valid() const
Definition handle.h:51
void ReleaseWaiter(HandleWaiter *waiter)
Definition handle.cc:76
static fml::RefPtr< HandleWaiter > Create(Handle *handle, zx_signals_t signals, Dart_Handle callback)
#define DART_NATIVE_NO_UI_CHECK_CALLBACK(CLASS, METHOD)
#define DART_REGISTER_NATIVE(CLASS, METHOD)
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
static guint signals[LAST_SIGNAL]
FlutterDesktopBinaryReply callback
#define FML_CHECK(condition)
Definition logging.h:104
#define FML_DCHECK(condition)
Definition logging.h:122
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via dart
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)
fuchsia::ui::composition::ParentViewportWatcherHandle handle_
#define FOR_EACH_BINDING(V)
Definition handle.cc:114