Flutter Engine
The Flutter Engine
dart_state.h
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#ifndef LIB_TONIC_DART_STATE_H_
6#define LIB_TONIC_DART_STATE_H_
7
8#include <atomic>
9#include <functional>
10#include <memory>
11
12#include "third_party/dart/runtime/include/dart_api.h"
13#include "tonic/common/macros.h"
17
18namespace tonic {
19class DartClassLibrary;
20class DartMessageHandler;
21class FileLoader;
22
23// DartState represents the state associated with a given Dart isolate. The
24// lifetime of this object is controlled by the DartVM. If you want to hold a
25// reference to a DartState instance, please hold a std::weak_ptr<DartState>.
26//
27// DartState is analogous to gin::PerIsolateData and JSC::ExecState.
28class DartState : public std::enable_shared_from_this<DartState> {
29 public:
30 class Scope {
31 public:
32 explicit Scope(DartState* dart_state);
33 explicit Scope(std::shared_ptr<DartState> dart_state);
34 ~Scope();
35
36 private:
37 DartIsolateScope scope_;
38 DartApiScope api_scope_;
39 };
40
41 explicit DartState(
42 int dirfd = -1,
43 std::function<void(Dart_Handle)> message_epilogue = nullptr);
44 virtual ~DartState();
45
47 static DartState* Current();
48
49 std::weak_ptr<DartState> GetWeakPtr();
50
51 Dart_Isolate isolate() { return isolate_; }
53
54 // TODO(https://github.com/flutter/flutter/issues/50997): Work around until we
55 // drop the need for Dart_New in tonic.
57 return private_constructor_name_.Get();
58 }
59
60 DartClassLibrary& class_library() { return *class_library_; }
61 DartMessageHandler& message_handler() { return *message_handler_; }
62 FileLoader& file_loader() { return *file_loader_; }
63
64 void MessageEpilogue(Dart_Handle message_result) {
65 if (message_epilogue_) {
66 message_epilogue_(message_result);
67 }
68 }
69 void SetReturnCode(uint32_t return_code);
70 void SetReturnCodeCallback(std::function<void(uint32_t)> callback);
71 bool has_set_return_code() const { return has_set_return_code_; }
72
73 void SetIsShuttingDown() { is_shutting_down_ = true; }
74 bool IsShuttingDown() { return is_shutting_down_; }
75
76 virtual void DidSetIsolate();
77
79 Dart_Handle library,
80 Dart_Handle url);
81
82 protected:
83 Dart_Isolate isolate() const { return isolate_; }
84
85 private:
86 Dart_Isolate isolate_;
87 DartPersistentValue private_constructor_name_;
88 std::unique_ptr<DartClassLibrary> class_library_;
89 std::unique_ptr<DartMessageHandler> message_handler_;
90 std::unique_ptr<FileLoader> file_loader_;
91 std::function<void(Dart_Handle)> message_epilogue_;
92 std::function<void(uint32_t)> set_return_code_callback_;
93 bool has_set_return_code_;
94 std::atomic<bool> is_shutting_down_;
95
96 protected:
98};
99
100} // namespace tonic
101
102#endif // LIB_TONIC_DART_STATE_H_
Scope(DartState *dart_state)
Definition: dart_state.cc:14
TONIC_DISALLOW_COPY_AND_ASSIGN(DartState)
virtual void DidSetIsolate()
Definition: dart_state.cc:77
void SetReturnCode(uint32_t return_code)
Definition: dart_state.cc:66
Dart_Isolate isolate()
Definition: dart_state.h:51
static DartState * From(Dart_Isolate isolate)
Definition: dart_state.cc:50
virtual ~DartState()
Definition: dart_state.cc:33
void SetIsolate(Dart_Isolate isolate)
Definition: dart_state.cc:35
DartMessageHandler & message_handler()
Definition: dart_state.h:61
void MessageEpilogue(Dart_Handle message_result)
Definition: dart_state.h:64
DartState(int dirfd=-1, std::function< void(Dart_Handle)> message_epilogue=nullptr)
Definition: dart_state.cc:22
bool has_set_return_code() const
Definition: dart_state.h:71
void SetIsShuttingDown()
Definition: dart_state.h:73
FileLoader & file_loader()
Definition: dart_state.h:62
void SetReturnCodeCallback(std::function< void(uint32_t)> callback)
Definition: dart_state.cc:73
std::weak_ptr< DartState > GetWeakPtr()
Definition: dart_state.cc:62
static DartState * Current()
Definition: dart_state.cc:56
Dart_PersistentHandle private_constructor_name()
Definition: dart_state.h:56
bool IsShuttingDown()
Definition: dart_state.h:74
DartClassLibrary & class_library()
Definition: dart_state.h:60
static Dart_Handle HandleLibraryTag(Dart_LibraryTag tag, Dart_Handle library, Dart_Handle url)
Definition: dart_state.cc:79
Dart_Isolate isolate() const
Definition: dart_state.h:83
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_Isolate * Dart_Isolate
Definition: dart_api.h:88
Dart_Handle Dart_PersistentHandle
Definition: dart_api.h:259
Dart_LibraryTag
Definition: dart_api.h:3419
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
Dart_NativeFunction function
Definition: fuchsia.cc:51