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 "flutter/runtime/isolate_configuration.h"
8 
9 namespace flutter {
10 namespace testing {
11 AutoIsolateShutdown::AutoIsolateShutdown(std::shared_ptr<DartIsolate> isolate,
13  : isolate_(std::move(isolate)), runner_(std::move(runner)) {}
14 
16  if (!IsValid()) {
17  return;
18  }
21  runner_, [isolate = std::move(isolate_), &latch]() {
22  if (!isolate->Shutdown()) {
23  FML_LOG(ERROR) << "Could not shutdown isolate.";
24  FML_CHECK(false);
25  }
26  latch.Signal();
27  });
28  latch.Wait();
29 }
30 
32  std::function<bool(void)> closure) {
33  if (!IsValid()) {
34  return false;
35  }
36 
37  bool result = false;
40  runner_, [this, &result, &latch, closure]() {
41  tonic::DartIsolateScope scope(isolate_->isolate());
42  tonic::DartApiScope api_scope;
43  if (closure) {
44  result = closure();
45  }
46  latch.Signal();
47  });
48  latch.Wait();
49  return true;
50 }
51 
52 std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolateOnUITaskRunner(
53  DartVMRef& vm_ref,
54  const Settings& p_settings,
55  const TaskRunners& task_runners,
56  std::string entrypoint,
57  const std::vector<std::string>& args,
58  const std::string& fixtures_path,
59  fml::WeakPtr<IOManager> io_manager) {
61 
62  if (!vm_ref) {
63  return nullptr;
64  }
65 
66  auto vm_data = vm_ref.GetVMData();
67 
68  if (!vm_data) {
69  return nullptr;
70  }
71 
72  auto settings = p_settings;
73 
74  settings.dart_entrypoint_args = args;
75 
77  auto kernel_file_path =
78  fml::paths::JoinPaths({fixtures_path, "kernel_blob.bin"});
79 
80  if (!fml::IsFile(kernel_file_path)) {
81  FML_LOG(ERROR) << "Could not locate kernel file.";
82  return nullptr;
83  }
84 
85  auto kernel_file = fml::OpenFile(kernel_file_path.c_str(), false,
87 
88  if (!kernel_file.is_valid()) {
89  FML_LOG(ERROR) << "Kernel file descriptor was invalid.";
90  return nullptr;
91  }
92 
93  auto kernel_mapping = std::make_unique<fml::FileMapping>(kernel_file);
94 
95  if (kernel_mapping->GetMapping() == nullptr) {
96  FML_LOG(ERROR) << "Could not setup kernel mapping.";
97  return nullptr;
98  }
99 
100  settings.application_kernels = fml::MakeCopyable(
101  [kernel_mapping = std::move(kernel_mapping)]() mutable -> Mappings {
102  Mappings mappings;
103  mappings.emplace_back(std::move(kernel_mapping));
104  return mappings;
105  });
106  }
107 
108  auto isolate_configuration =
110 
111  auto isolate =
113  settings, // settings
114  vm_data->GetIsolateSnapshot(), // isolate snapshot
115  std::move(task_runners), // task runners
116  nullptr, // window
117  {}, // snapshot delegate
118  {}, // hint freed delegate
119  io_manager, // io manager
120  {}, // unref queue
121  {}, // image decoder
122  "main.dart", // advisory uri
123  entrypoint.c_str(), // advisory entrypoint
124  DartIsolate::Flags{}, // flags
125  settings.isolate_create_callback, // isolate create callback
126  settings.isolate_shutdown_callback, // isolate shutdown callback
127  entrypoint, // entrypoint
128  std::nullopt, // library
129  std::move(isolate_configuration) // isolate configuration
130  )
131  .lock();
132 
133  if (!isolate) {
134  FML_LOG(ERROR) << "Could not create running isolate.";
135  return nullptr;
136  }
137 
138  return std::make_unique<AutoIsolateShutdown>(isolate,
139  task_runners.GetUITaskRunner());
140 }
141 
142 std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate(
143  DartVMRef& vm_ref,
144  const Settings& settings,
145  const TaskRunners& task_runners,
146  std::string entrypoint,
147  const std::vector<std::string>& args,
148  const std::string& fixtures_path,
149  fml::WeakPtr<IOManager> io_manager) {
150  std::unique_ptr<AutoIsolateShutdown> result;
153  task_runners.GetUITaskRunner(), fml::MakeCopyable([&]() mutable {
155  vm_ref, settings, task_runners, entrypoint, args, fixtures_path,
156  io_manager);
157  latch.Signal();
158  }));
159  latch.Wait();
160  return result;
161 }
162 
163 } // namespace testing
164 } // namespace flutter
G_BEGIN_DECLS FlValue * args
std::vector< std::string > dart_entrypoint_args
Definition: settings.h:98
static void RunNowOrPostTask(fml::RefPtr< fml::TaskRunner > runner, const fml::closure &task)
Definition: task_runner.cc:55
Definition: ref_ptr.h:252
Dart_NativeFunction function
Definition: fuchsia.cc:51
static std::weak_ptr< DartIsolate > CreateRunningRootIsolate(const Settings &settings, fml::RefPtr< const DartSnapshot > isolate_snapshot, TaskRunners task_runners, std::unique_ptr< PlatformConfiguration > platform_configuration, fml::WeakPtr< SnapshotDelegate > snapshot_delegate, fml::WeakPtr< HintFreedDelegate > hint_freed_delegate, fml::WeakPtr< IOManager > io_manager, fml::RefPtr< SkiaUnrefQueue > skia_unref_queue, fml::WeakPtr< ImageDecoder > image_decoder, std::string advisory_script_uri, std::string advisory_script_entrypoint, Flags flags, 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, std::unique_ptr< IsolateConfiguration > isolate_configration)
Creates an instance of a root isolate and returns a weak pointer to the same. The isolate instance ma...
Definition: dart_isolate.cc:76
static std::shared_ptr< const DartVMData > GetVMData()
#define FML_LOG(severity)
Definition: logging.h:65
std::vector< std::unique_ptr< const fml::Mapping > > Mappings
Definition: settings.h:57
std::function< void()> closure
Definition: closure.h:14
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
std::string JoinPaths(std::initializer_list< std::string > components)
Definition: paths.cc:14
static std::unique_ptr< IsolateConfiguration > InferFromSettings(const Settings &settings, std::shared_ptr< AssetManager > asset_manager=nullptr, fml::RefPtr< fml::TaskRunner > io_worker=nullptr)
Attempts to infer the isolate configuration from the Settings object. If the VM is configured for AOT...
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 &fixtures_path, fml::WeakPtr< IOManager > io_manager)
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:199
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
#define FML_CHECK(condition)
Definition: logging.h:68
bool IsFile(const std::string &path)
Definition: file_posix.cc:143
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:64
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43
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 &fixtures_path, fml::WeakPtr< IOManager > io_manager)
bool RunInIsolateScope(std::function< bool(void)> closure)