Flutter Engine
flutter_engine.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 "include/flutter/flutter_engine.h"
6 
7 #include <algorithm>
8 #include <iostream>
9 
10 #include "binary_messenger_impl.h"
11 
12 namespace flutter {
13 
15  FlutterDesktopEngineProperties c_engine_properties = {};
16  c_engine_properties.assets_path = project.assets_path().c_str();
17  c_engine_properties.icu_data_path = project.icu_data_path().c_str();
18  c_engine_properties.aot_library_path = project.aot_library_path().c_str();
19 
20  const std::vector<std::string>& entrypoint_args =
21  project.dart_entrypoint_arguments();
22  std::vector<const char*> entrypoint_argv;
24  entrypoint_args.begin(), entrypoint_args.end(),
25  std::back_inserter(entrypoint_argv),
26  [](const std::string& arg) -> const char* { return arg.c_str(); });
27 
28  c_engine_properties.dart_entrypoint_argc =
29  static_cast<int>(entrypoint_argv.size());
30  c_engine_properties.dart_entrypoint_argv =
31  entrypoint_argv.size() > 0 ? entrypoint_argv.data() : nullptr;
32 
33  engine_ = FlutterDesktopEngineCreate(&c_engine_properties);
34 
35  auto core_messenger = FlutterDesktopEngineGetMessenger(engine_);
36  messenger_ = std::make_unique<BinaryMessengerImpl>(core_messenger);
37 }
38 
40  ShutDown();
41 }
42 
43 bool FlutterEngine::Run(const char* entry_point) {
44  if (!engine_) {
45  std::cerr << "Cannot run an engine that failed creation." << std::endl;
46  return false;
47  }
48  if (has_been_run_) {
49  std::cerr << "Cannot run an engine more than once." << std::endl;
50  return false;
51  }
52  bool run_succeeded = FlutterDesktopEngineRun(engine_, entry_point);
53  if (!run_succeeded) {
54  std::cerr << "Failed to start engine." << std::endl;
55  }
56  has_been_run_ = true;
57  return run_succeeded;
58 }
59 
61  if (engine_ && owns_engine_) {
63  }
64  engine_ = nullptr;
65 }
66 
67 #ifndef WINUWP
68 std::chrono::nanoseconds FlutterEngine::ProcessMessages() {
69  return std::chrono::nanoseconds(FlutterDesktopEngineProcessMessages(engine_));
70 }
71 #endif
72 
75 }
76 
79 }
80 
82  const std::string& plugin_name) {
83  if (!engine_) {
84  std::cerr << "Cannot get plugin registrar on an engine that isn't running; "
85  "call Run first."
86  << std::endl;
87  return nullptr;
88  }
89  return FlutterDesktopEngineGetPluginRegistrar(engine_, plugin_name.c_str());
90 }
91 
92 FlutterDesktopEngineRef FlutterEngine::RelinquishEngine() {
93  owns_engine_ = false;
94  return engine_;
95 }
96 
97 } // namespace flutter
FlutterDesktopEngineRef FlutterDesktopEngineCreate(const FlutterDesktopEngineProperties *engine_properties)
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(const std::string &plugin_name) override
void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine)
const std::vector< std::string > & dart_entrypoint_arguments() const
Definition: dart_project.h:58
bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref)
void FlutterDesktopEngineReloadPlatformBrightness(FlutterDesktopEngineRef engine)
bool Run(const char *entry_point=nullptr)
uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine)
FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine)
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
std::chrono::nanoseconds ProcessMessages()
bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine, const char *entry_point)