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#ifndef LIB_TONIC_DART_MESSAGE_HANDLER_H_
6#define LIB_TONIC_DART_MESSAGE_HANDLER_H_
7
8#include <functional>
9
10#include "third_party/dart/runtime/include/dart_api.h"
11#include "tonic/logging/dart_error.h"
12
13namespace tonic {
14class DartState;
15
16class DartMessageHandler {
17 public:
18 using TaskDispatcher = std::function<void(std::function<void(void)>)>;
19
20 DartMessageHandler();
21
22 ~DartMessageHandler();
23
24 // Messages for the current isolate will be scheduled on |runner|.
25 void Initialize(TaskDispatcher dispatcher);
26
27 // Handle an unhandled error. If the error is fatal then shut down the
28 // isolate. The message handler's isolate must be the current isolate.
29 void UnhandledError(Dart_Handle error);
30
31 // Did the isolate exit?
32 bool isolate_exited() const { return isolate_exited_; }
33
34 // Did the isolate have an uncaught exception error?
35 bool isolate_had_uncaught_exception_error() const {
36 return isolate_had_uncaught_exception_error_;
37 }
38
39 DartErrorHandleType isolate_last_error() const { return isolate_last_error_; }
40
41 protected:
42 // Called from an unknown thread for each message.
43 void OnMessage(DartState* dart_state);
44 // By default, called on the task runner's thread for each message.
45 void OnHandleMessage(DartState* dart_state);
46
47 bool handled_first_message() const { return handled_first_message_; }
48
49 void set_handled_first_message(bool handled_first_message) {
50 handled_first_message_ = handled_first_message;
51 }
52
53 bool handled_first_message_;
54 bool isolate_exited_;
55 bool isolate_had_uncaught_exception_error_;
56 bool isolate_had_fatal_error_;
57 DartErrorHandleType isolate_last_error_;
58 TaskDispatcher task_dispatcher_;
59
60 private:
61 static void MessageNotifyCallback(Dart_Isolate dest_isolate);
62};
63
64} // namespace tonic
65
66#endif // LIB_TONIC_DART_MESSAGE_HANDLER_H_
67