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