Flutter Engine
flutter::FlutterWindowsEngine Class Reference

#include <flutter_windows_engine.h>

Public Member Functions

 FlutterWindowsEngine (const FlutterProjectBundle &project)
 
virtual ~FlutterWindowsEngine ()
 
 FlutterWindowsEngine (FlutterWindowsEngine const &)=delete
 
FlutterWindowsEngineoperator= (FlutterWindowsEngine const &)=delete
 
bool RunWithEntrypoint (const char *entrypoint)
 
bool running ()
 
bool Stop ()
 
void SetView (FlutterWindowsView *view)
 
FlutterWindowsViewview ()
 
FlutterDesktopPluginRegistrarRef GetRegistrar ()
 
void SetPluginRegistrarDestructionCallback (FlutterDesktopOnPluginRegistrarDestroyed callback)
 
 FLUTTER_API_SYMBOL (FlutterEngine) engine()
 
FlutterDesktopMessengerRef messenger ()
 
IncomingMessageDispatchermessage_dispatcher ()
 
Win32TaskRunnertask_runner ()
 
Win32WindowProcDelegateManagerwindow_proc_delegate_manager ()
 
void HandlePlatformMessage (const FlutterPlatformMessage *)
 

Detailed Description

Definition at line 29 of file flutter_windows_engine.h.

Constructor & Destructor Documentation

◆ FlutterWindowsEngine() [1/2]

flutter::FlutterWindowsEngine::FlutterWindowsEngine ( const FlutterProjectBundle project)
explicit

Definition at line 92 of file flutter_windows_engine.cc.

References FlutterEngineRunTask(), and flutter::kSuccess.

93  : project_(std::make_unique<FlutterProjectBundle>(project)) {
94  task_runner_ = std::make_unique<Win32TaskRunner>(
95  GetCurrentThreadId(), [this](const auto* task) {
96  if (!engine_) {
97  std::cerr << "Cannot post an engine task when engine is not running."
98  << std::endl;
99  return;
100  }
101  if (FlutterEngineRunTask(engine_, task) != kSuccess) {
102  std::cerr << "Failed to post an engine task." << std::endl;
103  }
104  });
105 
106  // Set up the legacy structs backing the API handles.
107  messenger_ = std::make_unique<FlutterDesktopMessenger>();
108  messenger_->engine = this;
109  plugin_registrar_ = std::make_unique<FlutterDesktopPluginRegistrar>();
110  plugin_registrar_->engine = this;
111 
112  message_dispatcher_ =
113  std::make_unique<IncomingMessageDispatcher>(messenger_.get());
114  window_proc_delegate_manager_ =
115  std::make_unique<Win32WindowProcDelegateManager>();
116 }
FlutterEngineResult FlutterEngineRunTask(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterTask *task)
Inform the engine to run the specified task. This task has been given to the engine via the FlutterTa...
Definition: embedder.cc:1717

◆ ~FlutterWindowsEngine()

flutter::FlutterWindowsEngine::~FlutterWindowsEngine ( )
virtual

Definition at line 118 of file flutter_windows_engine.cc.

References Stop().

118  {
119  Stop();
120 }

◆ FlutterWindowsEngine() [2/2]

flutter::FlutterWindowsEngine::FlutterWindowsEngine ( FlutterWindowsEngine const &  )
delete

Member Function Documentation

◆ FLUTTER_API_SYMBOL()

flutter::FlutterWindowsEngine::FLUTTER_API_SYMBOL ( FlutterEngine  )
inline

Definition at line 68 of file flutter_windows_engine.h.

Referenced by window_proc_delegate_manager().

68 { return engine_; }

◆ GetRegistrar()

FlutterDesktopPluginRegistrarRef flutter::FlutterWindowsEngine::GetRegistrar ( )

Definition at line 215 of file flutter_windows_engine.cc.

Referenced by FlutterDesktopEngineGetPluginRegistrar(), and view().

215  {
216  return plugin_registrar_.get();
217 }

◆ HandlePlatformMessage()

void flutter::FlutterWindowsEngine::HandlePlatformMessage ( const FlutterPlatformMessage engine_message)

Definition at line 224 of file flutter_windows_engine.cc.

References ConvertToDesktopMessage(), FlutterEngineUpdateLocales(), flutter::GetPreferredLanguageInfo(), FlutterPlatformMessage::struct_size, and flutter::transform.

Referenced by RunWithEntrypoint(), and window_proc_delegate_manager().

225  {
226  if (engine_message->struct_size != sizeof(FlutterPlatformMessage)) {
227  std::cerr << "Invalid message size received. Expected: "
228  << sizeof(FlutterPlatformMessage) << " but received "
229  << engine_message->struct_size << std::endl;
230  return;
231  }
232 
233  auto message = ConvertToDesktopMessage(*engine_message);
234 
235  message_dispatcher_->HandleMessage(
236  message, [this] {}, [this] {});
237 }
static FlutterDesktopMessage ConvertToDesktopMessage(const FlutterPlatformMessage &engine_message)
size_t struct_size
The size of this struct. Must be sizeof(FlutterPlatformMessage).
Definition: embedder.h:566

◆ message_dispatcher()

IncomingMessageDispatcher* flutter::FlutterWindowsEngine::message_dispatcher ( )
inline

Definition at line 72 of file flutter_windows_engine.h.

72  {
73  return message_dispatcher_.get();
74  }

◆ messenger()

FlutterDesktopMessengerRef flutter::FlutterWindowsEngine::messenger ( )
inline

Definition at line 70 of file flutter_windows_engine.h.

Referenced by FlutterDesktopEngineGetMessenger().

70 { return messenger_.get(); }

◆ operator=()

FlutterWindowsEngine& flutter::FlutterWindowsEngine::operator= ( FlutterWindowsEngine const &  )
delete

◆ running()

bool flutter::FlutterWindowsEngine::running ( )
inline

Definition at line 47 of file flutter_windows_engine.h.

References SetView(), Stop(), and view().

Referenced by FlutterDesktopEngineDestroy().

47 { return engine_ != nullptr; }

◆ RunWithEntrypoint()

bool flutter::FlutterWindowsEngine::RunWithEntrypoint ( const char *  entrypoint)

Definition at line 122 of file flutter_windows_engine.cc.

References FlutterProjectArgs::aot_data, args, FlutterProjectArgs::assets_path, FlutterProjectArgs::command_line_argc, FlutterProjectArgs::command_line_argv, FlutterProjectArgs::custom_dart_entrypoint, FlutterProjectArgs::custom_task_runners, FLUTTER_ENGINE_VERSION, FlutterEngineRun(), FlutterEngineRunsAOTCompiledDartCode(), HandlePlatformMessage(), FlutterProjectArgs::icu_data_path, flutter::kSuccess, FlutterProjectArgs::platform_message_callback, FlutterCustomTaskRunners::platform_task_runner, FlutterTaskRunnerDescription::struct_size, FlutterCustomTaskRunners::struct_size, FlutterProjectArgs::struct_size, flutter::transform, and user_data.

Referenced by FlutterDesktopEngineRun().

122  {
123  if (!project_->HasValidPaths()) {
124  std::cerr << "Missing or unresolvable paths to assets." << std::endl;
125  return false;
126  }
127  std::string assets_path_string = project_->assets_path().u8string();
128  std::string icu_path_string = project_->icu_path().u8string();
130  aot_data_ = project_->LoadAotData();
131  if (!aot_data_) {
132  std::cerr << "Unable to start engine without AOT data." << std::endl;
133  return false;
134  }
135  }
136 
137  // FlutterProjectArgs is expecting a full argv, so when processing it for
138  // flags the first item is treated as the executable and ignored. Add a dummy
139  // value so that all provided arguments are used.
140  std::vector<std::string> switches = project_->GetSwitches();
141  std::vector<const char*> argv = {"placeholder"};
143  switches.begin(), switches.end(), std::back_inserter(argv),
144  [](const std::string& arg) -> const char* { return arg.c_str(); });
145 
146  // Configure task runners.
147  FlutterTaskRunnerDescription platform_task_runner = {};
148  platform_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
149  platform_task_runner.user_data = task_runner_.get();
150  platform_task_runner.runs_task_on_current_thread_callback =
151  [](void* user_data) -> bool {
152  return static_cast<Win32TaskRunner*>(user_data)->RunsTasksOnCurrentThread();
153  };
154  platform_task_runner.post_task_callback = [](FlutterTask task,
155  uint64_t target_time_nanos,
156  void* user_data) -> void {
157  static_cast<Win32TaskRunner*>(user_data)->PostTask(task, target_time_nanos);
158  };
159  FlutterCustomTaskRunners custom_task_runners = {};
160  custom_task_runners.struct_size = sizeof(FlutterCustomTaskRunners);
161  custom_task_runners.platform_task_runner = &platform_task_runner;
162 
164  args.struct_size = sizeof(FlutterProjectArgs);
165  args.assets_path = assets_path_string.c_str();
166  args.icu_data_path = icu_path_string.c_str();
167  args.command_line_argc = static_cast<int>(argv.size());
168  args.command_line_argv = argv.size() > 0 ? argv.data() : nullptr;
170  [](const FlutterPlatformMessage* engine_message,
171  void* user_data) -> void {
172  auto host = static_cast<FlutterWindowsEngine*>(user_data);
173  return host->HandlePlatformMessage(engine_message);
174  };
175  args.custom_task_runners = &custom_task_runners;
176  if (aot_data_) {
177  args.aot_data = aot_data_.get();
178  }
179  if (entrypoint) {
180  args.custom_dart_entrypoint = entrypoint;
181  }
182 
183  FlutterRendererConfig renderer_config = GetRendererConfig();
184 
185  auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &renderer_config,
186  &args, this, &engine_);
187  if (result != kSuccess || engine_ == nullptr) {
188  std::cerr << "Failed to start Flutter engine: error " << result
189  << std::endl;
190  return false;
191  }
192 
193  SendSystemSettings();
194 
195  return true;
196 }
G_BEGIN_DECLS FlValue * args
const char * icu_data_path
Definition: embedder.h:1199
size_t struct_size
The size of this struct. Must be sizeof(FlutterTaskRunnerDescription).
Definition: embedder.h:722
const FlutterCustomTaskRunners * custom_task_runners
Definition: embedder.h:1313
FlutterWindowsEngine(const FlutterProjectBundle &project)
FlutterPlatformMessageCallback platform_message_callback
Definition: embedder.h:1222
const char *const * command_line_argv
Definition: embedder.h:1217
G_BEGIN_DECLS FlValue gpointer user_data
#define FLUTTER_ENGINE_VERSION
Definition: embedder.h:63
FlutterEngineResult FlutterEngineRun(size_t version, const FlutterRendererConfig *config, const FlutterProjectArgs *args, void *user_data, FLUTTER_API_SYMBOL(FlutterEngine) *engine_out)
Initialize and run a Flutter engine instance and return a handle to it. This is a convenience method ...
Definition: embedder.cc:718
const char * custom_dart_entrypoint
Definition: embedder.h:1308
const char * assets_path
Definition: embedder.h:1175
size_t struct_size
The size of this struct. Must be sizeof(FlutterProjectArgs).
Definition: embedder.h:1171
const FlutterTaskRunnerDescription * platform_task_runner
Definition: embedder.h:752
FlutterEngineAOTData aot_data
Definition: embedder.h:1363
size_t struct_size
The size of this struct. Must be sizeof(FlutterCustomTaskRunners).
Definition: embedder.h:747
bool FlutterEngineRunsAOTCompiledDartCode(void)
Returns if the Flutter engine instance will run AOT compiled Dart code. This call has no threading re...
Definition: embedder.cc:1821
int command_line_argc
The command line argument count used to initialize the project.
Definition: embedder.h:1201

◆ SetPluginRegistrarDestructionCallback()

void flutter::FlutterWindowsEngine::SetPluginRegistrarDestructionCallback ( FlutterDesktopOnPluginRegistrarDestroyed  callback)

Definition at line 219 of file flutter_windows_engine.cc.

Referenced by view().

220  {
221  plugin_registrar_destruction_callback_ = callback;
222 }

◆ SetView()

void flutter::FlutterWindowsEngine::SetView ( FlutterWindowsView view)

Definition at line 210 of file flutter_windows_engine.cc.

References view().

Referenced by running().

210  {
211  view_ = view;
212 }

◆ Stop()

bool flutter::FlutterWindowsEngine::Stop ( )

Definition at line 198 of file flutter_windows_engine.cc.

References FlutterEngineShutdown(), and flutter::kSuccess.

Referenced by FlutterDesktopEngineDestroy(), running(), and ~FlutterWindowsEngine().

198  {
199  if (engine_) {
200  if (plugin_registrar_destruction_callback_) {
201  plugin_registrar_destruction_callback_(plugin_registrar_.get());
202  }
203  FlutterEngineResult result = FlutterEngineShutdown(engine_);
204  engine_ = nullptr;
205  return (result == kSuccess);
206  }
207  return false;
208 }
FlutterEngineResult FlutterEngineShutdown(FLUTTER_API_SYMBOL(FlutterEngine) engine)
Shuts down a Flutter engine instance. The engine handle is no longer valid for any calls in the embed...
Definition: embedder.cc:1218
FlutterEngineResult
Definition: embedder.h:65

◆ task_runner()

Win32TaskRunner* flutter::FlutterWindowsEngine::task_runner ( )
inline

Definition at line 76 of file flutter_windows_engine.h.

Referenced by FlutterDesktopEngineProcessMessages().

76 { return task_runner_.get(); }

◆ view()

FlutterWindowsView* flutter::FlutterWindowsEngine::view ( )
inline

Definition at line 59 of file flutter_windows_engine.h.

References GetRegistrar(), and SetPluginRegistrarDestructionCallback().

Referenced by running(), and SetView().

59 { return view_; }

◆ window_proc_delegate_manager()

Win32WindowProcDelegateManager* flutter::FlutterWindowsEngine::window_proc_delegate_manager ( )
inline

Definition at line 78 of file flutter_windows_engine.h.

References FLUTTER_API_SYMBOL(), and HandlePlatformMessage().

78  {
79  return window_proc_delegate_manager_.get();
80  }

The documentation for this class was generated from the following files: