• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FLUTTER_RUNTIME_DART_ISOLATE_H_
6 #define FLUTTER_RUNTIME_DART_ISOLATE_H_
7 
8 #include <memory>
9 #include <set>
10 #include <string>
11 
12 #include "flutter/common/task_runners.h"
13 #include "flutter/fml/compiler_specific.h"
14 #include "flutter/fml/macros.h"
15 #include "flutter/fml/mapping.h"
16 #include "flutter/lib/ui/io_manager.h"
17 #include "flutter/lib/ui/ui_dart_state.h"
18 #include "flutter/lib/ui/window/window.h"
19 #include "flutter/runtime/dart_snapshot.h"
20 #include "third_party/dart/runtime/include/dart_api.h"
21 
22 namespace flutter {
23 class DartVM;
24 
25 class DartIsolate : public UIDartState {
26  public:
27   enum class Phase {
28     Unknown,
29     Uninitialized,
30     Initialized,
31     LibrariesSetup,
32     Ready,
33     Running,
34     Shutdown,
35   };
36 
37   using ChildIsolatePreparer = std::function<bool(DartIsolate*)>;
38 
39   // The root isolate of a Flutter application is special because it gets Window
40   // bindings. From the VM's perspective, this isolate is not special in any
41   // way.
42   static std::weak_ptr<DartIsolate> CreateRootIsolate(
43       const Settings& settings,
44       fml::RefPtr<const DartSnapshot> isolate_snapshot,
45       fml::RefPtr<const DartSnapshot> shared_snapshot,
46       TaskRunners task_runners,
47       std::unique_ptr<Window> window,
48       fml::WeakPtr<IOManager> io_manager,
49       fml::WeakPtr<ImageDecoder> image_decoder,
50       std::string advisory_script_uri,
51       std::string advisory_script_entrypoint,
52       Dart_IsolateFlags* flags,
53       fml::closure isolate_create_callback,
54       fml::closure isolate_shutdown_callback);
55 
56   DartIsolate(const Settings& settings,
57               fml::RefPtr<const DartSnapshot> isolate_snapshot,
58               fml::RefPtr<const DartSnapshot> shared_snapshot,
59               TaskRunners task_runners,
60               fml::WeakPtr<IOManager> io_manager,
61               fml::WeakPtr<ImageDecoder> image_decoder,
62               std::string advisory_script_uri,
63               std::string advisory_script_entrypoint,
64               ChildIsolatePreparer child_isolate_preparer,
65               fml::closure isolate_create_callback,
66               fml::closure isolate_shutdown_callback);
67 
68   ~DartIsolate() override;
69 
70   const Settings& GetSettings() const;
71 
72   Phase GetPhase() const;
73 
74   std::string GetServiceId();
75 
76   FML_WARN_UNUSED_RESULT
77   bool PrepareForRunningFromPrecompiledCode();
78 
79   FML_WARN_UNUSED_RESULT
80   bool PrepareForRunningFromKernel(std::shared_ptr<const fml::Mapping> kernel,
81                                    bool last_piece = true);
82 
83   FML_WARN_UNUSED_RESULT
84   bool PrepareForRunningFromKernels(
85       std::vector<std::shared_ptr<const fml::Mapping>> kernels);
86 
87   FML_WARN_UNUSED_RESULT
88   bool PrepareForRunningFromKernels(
89       std::vector<std::unique_ptr<const fml::Mapping>> kernels);
90 
91   FML_WARN_UNUSED_RESULT
92   bool Run(const std::string& entrypoint,
93            const std::vector<std::string>& args,
94            fml::closure on_run = nullptr);
95 
96   FML_WARN_UNUSED_RESULT
97   bool RunFromLibrary(const std::string& library_name,
98                       const std::string& entrypoint,
99                       const std::vector<std::string>& args,
100                       fml::closure on_run = nullptr);
101 
102   FML_WARN_UNUSED_RESULT
103   bool Shutdown();
104 
105   void AddIsolateShutdownCallback(fml::closure closure);
106 
107   fml::RefPtr<const DartSnapshot> GetIsolateSnapshot() const;
108 
109   fml::RefPtr<const DartSnapshot> GetSharedSnapshot() const;
110 
111   std::weak_ptr<DartIsolate> GetWeakIsolatePtr();
112 
113   fml::RefPtr<fml::TaskRunner> GetMessageHandlingTaskRunner() const;
114 
115  private:
116   bool LoadKernel(std::shared_ptr<const fml::Mapping> mapping, bool last_piece);
117 
118   class AutoFireClosure {
119    public:
120     AutoFireClosure(fml::closure closure);
121 
122     ~AutoFireClosure();
123 
124    private:
125     fml::closure closure_;
126     FML_DISALLOW_COPY_AND_ASSIGN(AutoFireClosure);
127   };
128   friend class DartVM;
129 
130   Phase phase_ = Phase::Unknown;
131   const Settings settings_;
132   const fml::RefPtr<const DartSnapshot> isolate_snapshot_;
133   const fml::RefPtr<const DartSnapshot> shared_snapshot_;
134   std::vector<std::shared_ptr<const fml::Mapping>> kernel_buffers_;
135   std::vector<std::unique_ptr<AutoFireClosure>> shutdown_callbacks_;
136   ChildIsolatePreparer child_isolate_preparer_ = nullptr;
137   fml::RefPtr<fml::TaskRunner> message_handling_task_runner_;
138   const fml::closure isolate_create_callback_;
139   const fml::closure isolate_shutdown_callback_;
140 
141   FML_WARN_UNUSED_RESULT bool Initialize(Dart_Isolate isolate,
142                                          bool is_root_isolate);
143 
144   void SetMessageHandlingTaskRunner(fml::RefPtr<fml::TaskRunner> runner,
145                                     bool is_root_isolate);
146 
147   FML_WARN_UNUSED_RESULT
148   bool LoadLibraries(bool is_root_isolate);
149 
150   bool UpdateThreadPoolNames() const;
151 
152   FML_WARN_UNUSED_RESULT
153   bool MarkIsolateRunnable();
154 
155   void OnShutdownCallback();
156 
157   // |Dart_IsolateGroupCreateCallback|
158   static Dart_Isolate DartIsolateGroupCreateCallback(
159       const char* advisory_script_uri,
160       const char* advisory_script_entrypoint,
161       const char* package_root,
162       const char* package_config,
163       Dart_IsolateFlags* flags,
164       std::shared_ptr<DartIsolate>* embedder_isolate,
165       char** error);
166 
167   static Dart_Isolate DartCreateAndStartServiceIsolate(
168       const char* package_root,
169       const char* package_config,
170       Dart_IsolateFlags* flags,
171       char** error);
172 
173   static std::pair<Dart_Isolate /* vm */,
174                    std::weak_ptr<DartIsolate> /* embedder */>
175   CreateDartVMAndEmbedderObjectPair(
176       const char* advisory_script_uri,
177       const char* advisory_script_entrypoint,
178       const char* package_root,
179       const char* package_config,
180       Dart_IsolateFlags* flags,
181       std::shared_ptr<DartIsolate>* parent_embedder_isolate,
182       bool is_root_isolate,
183       char** error);
184 
185   // |Dart_IsolateShutdownCallback|
186   static void DartIsolateShutdownCallback(
187       std::shared_ptr<DartIsolate>* isolate_group_data,
188       std::shared_ptr<DartIsolate>* isolate_data);
189 
190   // |Dart_IsolateGroupCleanupCallback|
191   static void DartIsolateGroupCleanupCallback(
192       std::shared_ptr<DartIsolate>* isolate_group_data);
193 
194   FML_DISALLOW_COPY_AND_ASSIGN(DartIsolate);
195 };
196 
197 }  // namespace flutter
198 
199 #endif  // FLUTTER_RUNTIME_DART_ISOLATE_H_
200