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 namespace flutter {
11 
13 
15 
16 bool FlutterEngine::Start(const std::string& icu_data_path,
17  const std::string& assets_path,
18  const std::vector<std::string>& arguments,
19  const std::string& aot_library_path) {
20  if (engine_) {
21  std::cerr << "Cannot run an already running engine. Create a new instance "
22  "or call ShutDown first."
23  << std::endl;
24  return false;
25  }
26 
27  FlutterDesktopEngineProperties c_engine_properties = {};
28  c_engine_properties.assets_path = assets_path.c_str();
29  c_engine_properties.icu_data_path = icu_data_path.c_str();
30  c_engine_properties.aot_library_path = aot_library_path.c_str();
31  std::vector<const char*> engine_switches;
33  arguments.begin(), arguments.end(), std::back_inserter(engine_switches),
34  [](const std::string& arg) -> const char* { return arg.c_str(); });
35  if (engine_switches.size() > 0) {
36  c_engine_properties.switches = &engine_switches[0];
37  c_engine_properties.switches_count = engine_switches.size();
38  }
39 
40  engine_ = UniqueEnginePtr(FlutterDesktopRunEngine(c_engine_properties),
42  if (!engine_) {
43  std::cerr << "Failed to start engine." << std::endl;
44  return false;
45  }
46  return true;
47 }
48 
50  engine_ = nullptr;
51 }
52 
54  const std::string& plugin_name) {
55  if (!engine_) {
56  std::cerr << "Cannot get plugin registrar on an engine that isn't running; "
57  "call Run first."
58  << std::endl;
59  return nullptr;
60  }
61  return FlutterDesktopGetPluginRegistrar(engine_.get(), plugin_name.c_str());
62 }
63 
64 void FlutterEngine::RunEventLoopWithTimeout(std::chrono::milliseconds timeout) {
65  if (!engine_) {
66  std::cerr << "Cannot run event loop without a running engine; call "
67  "Run first."
68  << std::endl;
69  return;
70  }
71  uint32_t timeout_milliseconds;
72  if (timeout == std::chrono::milliseconds::max()) {
73  // The C API uses 0 to represent no timeout, so convert |max| to 0.
74  timeout_milliseconds = 0;
75  } else if (timeout.count() > UINT32_MAX) {
76  timeout_milliseconds = UINT32_MAX;
77  } else {
78  timeout_milliseconds = static_cast<uint32_t>(timeout.count());
79  }
81  timeout_milliseconds);
82 }
83 
84 } // namespace flutter
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(const std::string &plugin_name) override
bool FlutterDesktopShutDownEngine(FlutterDesktopEngineRef engine_ref)
bool Start(const std::string &icu_data_path, const std::string &assets_path, const std::vector< std::string > &arguments, const std::string &aot_library_path="")
FlutterDesktopEngineRef FlutterDesktopRunEngine(const FlutterDesktopEngineProperties &properties)
void RunEventLoopWithTimeout(std::chrono::milliseconds timeout=std::chrono::milliseconds::max())
FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
void FlutterDesktopRunEngineEventLoopWithTimeout(FlutterDesktopEngineRef engine, uint32_t timeout_milliseconds)