Flutter Engine
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

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.

References task_dispatcher_.

24  {
25  task_dispatcher_ = nullptr;
26 }

Member Function Documentation

◆ handled_first_message()

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

◆ Initialize()

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

Definition at line 28 of file dart_message_handler.cc.

References task_dispatcher_, and TONIC_CHECK.

Referenced by flutter::DartIsolate::GetMessageHandlingTaskRunner().

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

◆ isolate_exited()

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

Definition at line 32 of file dart_message_handler.h.

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.

References isolate_last_error_, OnHandleMessage(), and OnMessage().

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

39 { return isolate_last_error_; }
DartErrorHandleType isolate_last_error_

◆ OnHandleMessage()

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

Definition at line 63 of file dart_message_handler.cc.

References error, tonic::DartState::From(), handled_first_message(), tonic::DartState::has_set_return_code(), tonic::DartState::isolate(), isolate_exited_, isolate_had_fatal_error_, tonic::LogIfError(), tonic::DartState::MessageEpilogue(), set_handled_first_message(), TONIC_CHECK, and UnhandledError().

Referenced by isolate_last_error(), and OnMessage().

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

◆ OnMessage()

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

Definition at line 35 of file dart_message_handler.cc.

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

Referenced by isolate_last_error().

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  dart_state->message_handler().OnHandleMessage(dart_state.get());
43  }
44  });
45 }

◆ 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 47 of file dart_message_handler.cc.

References tonic::GetErrorHandleType(), isolate_had_fatal_error_, isolate_had_uncaught_exception_error_, isolate_last_error_, and TONIC_DCHECK.

Referenced by OnHandleMessage().

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

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(), and OnHandleMessage().

◆ isolate_had_fatal_error_

bool tonic::DartMessageHandler::isolate_had_fatal_error_
protected

Definition at line 56 of file dart_message_handler.h.

Referenced by OnHandleMessage(), and UnhandledError().

◆ 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(), and UnhandledError().

◆ 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(), and UnhandledError().

◆ 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: