Flutter Engine
 
Loading...
Searching...
No Matches
tonic::DartMessageHandler Class Reference

#include <dart_message_handler.h>

Public Types

using TaskDispatcher = std::function< void(std::function< void(void)>)>
 

Public Member Functions

 DartMessageHandler ()
 
 ~DartMessageHandler ()
 
void Initialize (TaskDispatcher dispatcher)
 
void UnhandledError (Dart_Handle error)
 
bool isolate_exited () const
 
bool isolate_had_uncaught_exception_error () const
 
DartErrorHandleType isolate_last_error () const
 

Protected Member Functions

void OnMessage (DartState *dart_state)
 
void OnHandleMessage (DartState *dart_state)
 
bool handled_first_message () const
 
void set_handled_first_message (bool handled_first_message)
 

Protected Attributes

bool handled_first_message_
 
bool isolate_exited_
 
bool isolate_had_uncaught_exception_error_
 
bool isolate_had_fatal_error_
 
DartErrorHandleType isolate_last_error_
 
TaskDispatcher task_dispatcher_
 

Detailed Description

Definition at line 16 of file dart_message_handler.h.

Member Typedef Documentation

◆ TaskDispatcher

using tonic::DartMessageHandler::TaskDispatcher = std::function<void(std::function<void(void)>)>

Definition at line 18 of file dart_message_handler.h.

Constructor & Destructor Documentation

◆ DartMessageHandler()

tonic::DartMessageHandler::DartMessageHandler ( )

◆ ~DartMessageHandler()

tonic::DartMessageHandler::~DartMessageHandler ( )

Definition at line 24 of file dart_message_handler.cc.

24 {
25 task_dispatcher_ = nullptr;
26}

References task_dispatcher_.

Member Function Documentation

◆ handled_first_message()

bool tonic::DartMessageHandler::handled_first_message ( ) const
inlineprotected

Definition at line 47 of file dart_message_handler.h.

47{ return handled_first_message_; }

References handled_first_message_.

Referenced by set_handled_first_message().

◆ Initialize()

void tonic::DartMessageHandler::Initialize ( TaskDispatcher  dispatcher)

Definition at line 28 of file dart_message_handler.cc.

28 {
29 // Only can be called once.
30 TONIC_CHECK(!task_dispatcher_ && dispatcher);
31 task_dispatcher_ = dispatcher;
32 Dart_SetMessageNotifyCallback(MessageNotifyCallback);
33}
#define TONIC_CHECK(condition)
Definition macros.h:23

References task_dispatcher_, and TONIC_CHECK.

◆ isolate_exited()

bool tonic::DartMessageHandler::isolate_exited ( ) const
inline

Definition at line 32 of file dart_message_handler.h.

32{ return isolate_exited_; }

References isolate_exited_.

◆ isolate_had_uncaught_exception_error()

bool tonic::DartMessageHandler::isolate_had_uncaught_exception_error ( ) const
inline

Definition at line 35 of file dart_message_handler.h.

References isolate_had_uncaught_exception_error_.

◆ isolate_last_error()

DartErrorHandleType tonic::DartMessageHandler::isolate_last_error ( ) const
inline

Definition at line 39 of file dart_message_handler.h.

39{ return isolate_last_error_; }

References isolate_last_error_.

Referenced by flutter::UIDartState::GetLastError().

◆ OnHandleMessage()

void tonic::DartMessageHandler::OnHandleMessage ( DartState dart_state)
protected

Definition at line 65 of file dart_message_handler.cc.

65 {
67 // Don't handle any more messages.
68 return;
69 }
70
71 DartIsolateScope scope(dart_state->isolate());
72 DartApiScope dart_api_scope;
73 Dart_Handle result = Dart_Null();
74 bool error = false;
75
76 // On the first message, check if we should pause on isolate start.
77 if (!handled_first_message()) {
79 if (Dart_ShouldPauseOnStart()) {
80 // Mark that we are paused on isolate start.
81 Dart_SetPausedOnStart(true);
82 }
83 }
84
85 if (Dart_IsPausedOnStart()) {
86 // We are paused on isolate start. Only handle service messages until we are
87 // requested to resume.
88 if (Dart_HasServiceMessages()) {
89 bool resume = Dart_HandleServiceMessages();
90 if (!resume) {
91 return;
92 }
93 Dart_SetPausedOnStart(false);
94 // We've resumed, handle normal messages that are in the queue.
95 result = Dart_HandleMessage();
96 error = CheckAndHandleError(result);
97 dart_state->MessageEpilogue(result);
98 if (!Dart_CurrentIsolate()) {
99 isolate_exited_ = true;
100 return;
101 }
102 }
103 } else if (Dart_IsPausedOnExit()) {
104 // We are paused on isolate exit. Only handle service messages until we are
105 // requested to resume.
106 if (Dart_HasServiceMessages()) {
107 bool resume = Dart_HandleServiceMessages();
108 if (!resume) {
109 return;
110 }
111 Dart_SetPausedOnExit(false);
112 }
113 } else {
114 // We are processing messages normally.
115 result = Dart_HandleMessage();
116 // If the Dart program has set a return code, then it is intending to shut
117 // down by way of a fatal error, and so there is no need to emit a log
118 // message.
119 if (dart_state->has_set_return_code() && Dart_IsError(result) &&
120 Dart_IsFatalError(result)) {
121 error = true;
122 } else {
123 error = CheckAndHandleError(result);
124 }
125 dart_state->MessageEpilogue(result);
126 if (!Dart_CurrentIsolate()) {
127 isolate_exited_ = true;
128 return;
129 }
130 }
131
132 if (error) {
133 UnhandledError(result);
134 } else if (!Dart_HasLivePorts()) {
135 // The isolate has no live ports and would like to exit.
136 if (!Dart_IsPausedOnExit() && Dart_ShouldPauseOnExit()) {
137 // Mark that we are paused on exit.
138 Dart_SetPausedOnExit(true);
139 } else {
140 isolate_exited_ = true;
141 }
142 }
143}
void UnhandledError(Dart_Handle error)
void set_handled_first_message(bool handled_first_message)
const uint8_t uint32_t uint32_t GError ** error
bool CheckAndHandleError(Dart_Handle handle)
Definition dart_error.cc:33

References tonic::CheckAndHandleError(), error, tonic::DartState::has_set_return_code(), tonic::DartState::isolate(), and tonic::DartState::MessageEpilogue().

◆ OnMessage()

void tonic::DartMessageHandler::OnMessage ( DartState dart_state)
protected

Definition at line 35 of file dart_message_handler.cc.

35 {
36 auto task_dispatcher_ = dart_state->message_handler().task_dispatcher_;
37
38 // Schedule a task to run on the message loop thread.
39 auto weak_dart_state = dart_state->GetWeakPtr();
40 task_dispatcher_([weak_dart_state]() {
41 if (auto dart_state = weak_dart_state.lock()) {
42 if (!dart_state->IsShuttingDown()) {
43 dart_state->message_handler().OnHandleMessage(dart_state.get());
44 }
45 }
46 });
47}

References tonic::DartState::GetWeakPtr(), tonic::DartState::message_handler(), and task_dispatcher_.

◆ set_handled_first_message()

void tonic::DartMessageHandler::set_handled_first_message ( bool  handled_first_message)
inlineprotected

◆ UnhandledError()

void tonic::DartMessageHandler::UnhandledError ( Dart_Handle  error)

Definition at line 49 of file dart_message_handler.cc.

49 {
50 TONIC_DCHECK(Dart_CurrentIsolate());
51 TONIC_DCHECK(Dart_IsError(error));
52
54 // Remember that we had an uncaught exception error.
56 if (Dart_IsFatalError(error)) {
58 // Stop handling messages.
59 Dart_SetMessageNotifyCallback(nullptr);
60 // Shut down the isolate.
61 Dart_ShutdownIsolate();
62 }
63}
DartErrorHandleType GetErrorHandleType(Dart_Handle handle)
Definition dart_error.cc:56
#define TONIC_DCHECK
Definition macros.h:32

References error, tonic::GetErrorHandleType(), and TONIC_DCHECK.

Member Data Documentation

◆ handled_first_message_

bool tonic::DartMessageHandler::handled_first_message_
protected

Definition at line 53 of file dart_message_handler.h.

Referenced by handled_first_message(), and set_handled_first_message().

◆ isolate_exited_

bool tonic::DartMessageHandler::isolate_exited_
protected

Definition at line 54 of file dart_message_handler.h.

Referenced by isolate_exited().

◆ isolate_had_fatal_error_

bool tonic::DartMessageHandler::isolate_had_fatal_error_
protected

Definition at line 56 of file dart_message_handler.h.

◆ isolate_had_uncaught_exception_error_

bool tonic::DartMessageHandler::isolate_had_uncaught_exception_error_
protected

Definition at line 55 of file dart_message_handler.h.

Referenced by isolate_had_uncaught_exception_error().

◆ isolate_last_error_

DartErrorHandleType tonic::DartMessageHandler::isolate_last_error_
protected

Definition at line 57 of file dart_message_handler.h.

Referenced by isolate_last_error().

◆ task_dispatcher_

TaskDispatcher tonic::DartMessageHandler::task_dispatcher_
protected

Definition at line 58 of file dart_message_handler.h.

Referenced by Initialize(), OnMessage(), and ~DartMessageHandler().


The documentation for this class was generated from the following files: