• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
10 #include "third_party/tonic/converter/dart_converter.h"
11 #include "third_party/tonic/dart_args.h"
12 #include "third_party/tonic/dart_binding_macros.h"
13 #include "third_party/tonic/dart_library_natives.h"
14 #include "third_party/tonic/dart_message_handler.h"
15 #include "third_party/tonic/logging/dart_invoke.h"
16 
17 using tonic::DartInvokeField;
18 using tonic::DartState;
19 using tonic::ToDart;
20 
21 namespace zircon {
22 namespace dart {
23 
24 IMPLEMENT_WRAPPERTYPEINFO(zircon, HandleWaiter);
25 
26 #define FOR_EACH_BINDING(V) V(HandleWaiter, Cancel)
27 
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)28 FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
29 
30 void HandleWaiter::RegisterNatives(tonic::DartLibraryNatives* natives) {
31   natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
32 }
33 
Create(Handle * handle,zx_signals_t signals,Dart_Handle callback)34 fml::RefPtr<HandleWaiter> HandleWaiter::Create(Handle* handle,
35                                                zx_signals_t signals,
36                                                Dart_Handle callback) {
37   return fml::MakeRefCounted<HandleWaiter>(handle, signals, callback);
38 }
39 
HandleWaiter(Handle * handle,zx_signals_t signals,Dart_Handle callback)40 HandleWaiter::HandleWaiter(Handle* handle,
41                            zx_signals_t signals,
42                            Dart_Handle callback)
43     : wait_(this, handle->handle(), signals),
44       handle_(handle),
45       callback_(DartState::Current(), callback) {
46   FML_CHECK(handle_ != nullptr);
47   FML_CHECK(handle_->is_valid());
48 
49   zx_status_t status = wait_.Begin(async_get_default_dispatcher());
50   FML_DCHECK(status == ZX_OK);
51 }
52 
~HandleWaiter()53 HandleWaiter::~HandleWaiter() {
54   Cancel();
55 }
56 
Cancel()57 void HandleWaiter::Cancel() {
58   FML_DCHECK(wait_.is_pending() == !!handle_);
59   if (handle_) {
60     // Cancel the wait.
61     wait_.Cancel();
62 
63     // Release this object from the handle and clear handle_.
64     handle_->ReleaseWaiter(this);
65     handle_ = nullptr;
66   }
67   FML_DCHECK(!wait_.is_pending());
68 }
69 
OnWaitComplete(async_dispatcher_t * dispatcher,async::WaitBase * wait,zx_status_t status,const zx_packet_signal_t * signal)70 void HandleWaiter::OnWaitComplete(async_dispatcher_t* dispatcher,
71                                   async::WaitBase* wait,
72                                   zx_status_t status,
73                                   const zx_packet_signal_t* signal) {
74   FML_DCHECK(handle_);
75 
76   FML_DCHECK(!callback_.is_empty());
77 
78   // Hold a reference to this object.
79   fml::RefPtr<HandleWaiter> ref(this);
80 
81   // Remove this waiter from the handle.
82   handle_->ReleaseWaiter(this);
83 
84   // Schedule the callback on the microtask queue.
85   handle_->ScheduleCallback(std::move(callback_), status, signal);
86 
87   // Clear handle_.
88   handle_ = nullptr;
89 }
90 
91 }  // namespace dart
92 }  // namespace zircon
93