Flutter Engine
The Flutter Engine
dart_isolate_runner.cc
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#include "flutter/testing/dart_isolate_runner.h"
6
7#include <utility>
8
9#include "flutter/runtime/isolate_configuration.h"
10
11namespace flutter {
12namespace testing {
13AutoIsolateShutdown::AutoIsolateShutdown(std::shared_ptr<DartIsolate> isolate,
15 : isolate_(std::move(isolate)), runner_(std::move(runner)) {}
16
18 if (!isolate_->IsShuttingDown()) {
19 Shutdown();
20 }
22 fml::TaskRunner::RunNowOrPostTask(runner_, [this, &latch]() {
23 // Delete isolate on thread.
24 isolate_.reset();
25 latch.Signal();
26 });
27 latch.Wait();
28}
29
31 if (!IsValid()) {
32 return;
33 }
36 runner_, [isolate = isolate_.get(), &latch]() {
37 if (!isolate->Shutdown()) {
38 FML_LOG(ERROR) << "Could not shutdown isolate.";
39 FML_CHECK(false);
40 }
41 latch.Signal();
42 });
43 latch.Wait();
44}
45
46[[nodiscard]] bool AutoIsolateShutdown::RunInIsolateScope(
47 const std::function<bool(void)>& closure) {
48 if (!IsValid()) {
49 return false;
50 }
51
52 bool result = false;
55 runner_, [this, &result, &latch, closure]() {
56 tonic::DartIsolateScope scope(isolate_->isolate());
57 tonic::DartApiScope api_scope;
58 if (closure) {
59 result = closure();
60 }
61 latch.Signal();
62 });
63 latch.Wait();
64 return result;
65}
66
67std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolateOnUITaskRunner(
68 DartVMRef& vm_ref,
69 const Settings& p_settings,
70 const TaskRunners& task_runners,
71 std::string entrypoint,
72 const std::vector<std::string>& args,
73 const std::string& kernel_file_path,
74 fml::WeakPtr<IOManager> io_manager,
75 const std::shared_ptr<VolatilePathTracker>& volatile_path_tracker,
76 std::unique_ptr<PlatformConfiguration> platform_configuration) {
78
79 if (!vm_ref) {
80 return nullptr;
81 }
82
83 auto vm_data = vm_ref.GetVMData();
84
85 if (!vm_data) {
86 return nullptr;
87 }
88
89 auto settings = p_settings;
90
92 if (!fml::IsFile(kernel_file_path)) {
93 FML_LOG(ERROR) << "Could not locate kernel file.";
94 return nullptr;
95 }
96
97 auto kernel_file = fml::OpenFile(kernel_file_path.c_str(), false,
99
100 if (!kernel_file.is_valid()) {
101 FML_LOG(ERROR) << "Kernel file descriptor was invalid.";
102 return nullptr;
103 }
104
105 auto kernel_mapping = std::make_unique<fml::FileMapping>(kernel_file);
106
107 if (kernel_mapping->GetMapping() == nullptr) {
108 FML_LOG(ERROR) << "Could not set up kernel mapping.";
109 return nullptr;
110 }
111
112 settings.application_kernels = fml::MakeCopyable(
113 [kernel_mapping = std::move(kernel_mapping)]() mutable -> Mappings {
114 Mappings mappings;
115 mappings.emplace_back(std::move(kernel_mapping));
116 return mappings;
117 });
118 }
119
120 auto isolate_configuration =
122
123 UIDartState::Context context(task_runners);
124 context.io_manager = std::move(io_manager);
125 context.advisory_script_uri = "main.dart";
126 context.advisory_script_entrypoint = entrypoint.c_str();
127 context.enable_impeller = p_settings.enable_impeller;
128
129 auto isolate =
131 settings, // settings
132 vm_data->GetIsolateSnapshot(), // isolate snapshot
133 std::move(platform_configuration), // platform configuration
134 DartIsolate::Flags{}, // flags
135 nullptr, // root isolate create callback
136 settings.isolate_create_callback, // isolate create callback
137 settings.isolate_shutdown_callback, // isolate shutdown callback
138 entrypoint, // entrypoint
139 std::nullopt, // library
140 args, // args
141 std::move(isolate_configuration), // isolate configuration
142 context // engine context
143 )
144 .lock();
145
146 if (!isolate) {
147 FML_LOG(ERROR) << "Could not create running isolate.";
148 return nullptr;
149 }
150
151 return std::make_unique<AutoIsolateShutdown>(
152 isolate, context.task_runners.GetUITaskRunner());
153}
154
155std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate(
156 DartVMRef& vm_ref,
157 const Settings& settings,
158 const TaskRunners& task_runners,
159 std::string entrypoint,
160 const std::vector<std::string>& args,
161 const std::string& kernel_file_path,
162 fml::WeakPtr<IOManager> io_manager,
163 std::shared_ptr<VolatilePathTracker> volatile_path_tracker,
164 std::unique_ptr<PlatformConfiguration> platform_configuration) {
165 std::unique_ptr<AutoIsolateShutdown> result;
168 task_runners.GetUITaskRunner(), fml::MakeCopyable([&]() mutable {
169 result = RunDartCodeInIsolateOnUITaskRunner(
170 vm_ref, settings, task_runners, entrypoint, args, kernel_file_path,
171 io_manager, volatile_path_tracker,
172 std::move(platform_configuration));
173 latch.Signal();
174 }));
175 latch.Wait();
176 return result;
177}
178
179} // namespace testing
180} // namespace flutter
static std::weak_ptr< DartIsolate > CreateRunningRootIsolate(const Settings &settings, const fml::RefPtr< const DartSnapshot > &isolate_snapshot, std::unique_ptr< PlatformConfiguration > platform_configuration, Flags flags, const fml::closure &root_isolate_create_callback, const fml::closure &isolate_create_callback, const fml::closure &isolate_shutdown_callback, std::optional< std::string > dart_entrypoint, std::optional< std::string > dart_entrypoint_library, const std::vector< std::string > &dart_entrypoint_args, std::unique_ptr< IsolateConfiguration > isolate_configuration, const UIDartState::Context &context, const DartIsolate *spawning_isolate=nullptr)
Creates an instance of a root isolate and returns a weak pointer to the same. The isolate instance ma...
Definition: dart_isolate.cc:90
static std::shared_ptr< const DartVMData > GetVMData()
static bool IsRunningPrecompiledCode()
Checks if VM instances in the process can run precompiled code. This call can be made at any time and...
Definition: dart_vm.cc:205
static std::unique_ptr< IsolateConfiguration > InferFromSettings(const Settings &settings, const std::shared_ptr< AssetManager > &asset_manager=nullptr, const fml::RefPtr< fml::TaskRunner > &io_worker=nullptr, IsolateLaunchType launch_type=IsolateLaunchType::kNewGroup)
Attempts to infer the isolate configuration from the Settings object. If the VM is configured for AOT...
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
Definition: task_runner.cc:55
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_CHECK(condition)
Definition: logging.h:85
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::unique_ptr< AutoIsolateShutdown > RunDartCodeInIsolate(DartVMRef &vm_ref, const Settings &settings, const TaskRunners &task_runners, std::string entrypoint, const std::vector< std::string > &args, const std::string &kernel_file_path, fml::WeakPtr< IOManager > io_manager, std::shared_ptr< VolatilePathTracker > volatile_path_tracker, std::unique_ptr< PlatformConfiguration > platform_configuration)
std::unique_ptr< AutoIsolateShutdown > RunDartCodeInIsolateOnUITaskRunner(DartVMRef &vm_ref, const Settings &p_settings, const TaskRunners &task_runners, std::string entrypoint, const std::vector< std::string > &args, const std::string &kernel_file_path, fml::WeakPtr< IOManager > io_manager, const std::shared_ptr< VolatilePathTracker > &volatile_path_tracker, std::unique_ptr< PlatformConfiguration > platform_configuration)
std::vector< std::unique_ptr< const fml::Mapping > > Mappings
Definition: settings.h:94
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
bool IsFile(const std::string &path)
Definition: file_posix.cc:146
std::function< void()> closure
Definition: closure.h:14
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:66
Definition: ref_ptr.h:256
bool enable_impeller
Definition: settings.h:229
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...
Definition: ui_dart_state.h:45
fml::WeakPtr< IOManager > io_manager
The IO manager used by the isolate for asynchronous texture uploads.
Definition: ui_dart_state.h:72
const TaskRunners task_runners
Definition: ui_dart_state.h:64
bool enable_impeller
Whether Impeller is enabled or not.
std::string advisory_script_entrypoint
Definition: ui_dart_state.h:93
#define ERROR(message)
Definition: elf_loader.cc:260