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 <functional>
9 #include <memory>
10 
11 #include "third_party/dart/runtime/include/dart_api.h"
12 #include "tonic/common/macros.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:
89 };
90 
91 } // namespace tonic
92 
93 #endif // LIB_TONIC_DART_STATE_H_
void SetIsolate(Dart_Isolate isolate)
Definition: dart_state.cc:34
bool has_set_return_code() const
Definition: dart_state.h:69
Dart_Isolate isolate()
Definition: dart_state.h:49
TONIC_DISALLOW_COPY_AND_ASSIGN(DartState)
DartState(int dirfd=-1, std::function< void(Dart_Handle)> message_epilogue=nullptr)
Definition: dart_state.cc:22
static DartState * From(Dart_Isolate isolate)
Definition: dart_state.cc:49
void SetReturnCodeCallback(std::function< void(uint32_t)> callback)
Definition: dart_state.cc:72
Dart_NativeFunction function
Definition: fuchsia.cc:51
static DartState * Current()
Definition: dart_state.cc:55
Scope(DartState *dart_state)
Definition: dart_state.cc:14
static Dart_Handle HandleLibraryTag(Dart_LibraryTag tag, Dart_Handle library, Dart_Handle url)
Definition: dart_state.cc:78
void SetReturnCode(uint32_t return_code)
Definition: dart_state.cc:65
DartClassLibrary & class_library()
Definition: dart_state.h:58
virtual ~DartState()
Definition: dart_state.cc:32
std::weak_ptr< DartState > GetWeakPtr()
Definition: dart_state.cc:61
void MessageEpilogue(Dart_Handle message_result)
Definition: dart_state.h:62
Dart_PersistentHandle private_constructor_name()
Definition: dart_state.h:54
FileLoader & file_loader()
Definition: dart_state.h:60
DartMessageHandler & message_handler()
Definition: dart_state.h:59
virtual void DidSetIsolate()
Definition: dart_state.cc:76