| Flutter Windows Embedder |

flutter_project_bundle.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/shell/platform/windows/flutter_project_bundle.h"
6
7 #include <filesystem>
8
9 #include "flutter/fml/logging.h"
10 #include "flutter/shell/platform/common/engine_switches.h" // nogncheck
11 #include "flutter/shell/platform/common/path_utils.h"
12
13 namespace flutter {
14
15 FlutterProjectBundle::FlutterProjectBundle(
16 const FlutterDesktopEngineProperties& properties)
17 : assets_path_(properties.assets_path),
18 icu_path_(properties.icu_data_path) {
19 if (properties.aot_library_path != nullptr) {
20 aot_library_path_ = std::filesystem::path(properties.aot_library_path);
21 }
22
23 if (properties.dart_entrypoint && properties.dart_entrypoint[0] != '\0') {
24 dart_entrypoint_ = properties.dart_entrypoint;
25 }
26
27 for (int i = 0; i < properties.dart_entrypoint_argc; i++) {
28 dart_entrypoint_arguments_.push_back(
29 std::string(properties.dart_entrypoint_argv[i]));
30 }
31
32 gpu_preference_ =
33 static_cast<FlutterGpuPreference>(properties.gpu_preference);
34
35 ui_thread_policy_ =
36 static_cast<FlutterUIThreadPolicy>(properties.ui_thread_policy);
37
38 // Resolve any relative paths.
39 if (assets_path_.is_relative() || icu_path_.is_relative() ||
40 (!aot_library_path_.empty() && aot_library_path_.is_relative())) {
41 std::filesystem::path executable_location = GetExecutableDirectory();
42 if (executable_location.empty()) {
43 FML_LOG(ERROR)
44 << "Unable to find executable location to resolve resource paths.";
45 assets_path_ = std::filesystem::path();
46 icu_path_ = std::filesystem::path();
47 } else {
48 assets_path_ = std::filesystem::path(executable_location) / assets_path_;
49 icu_path_ = std::filesystem::path(executable_location) / icu_path_;
50 if (!aot_library_path_.empty()) {
51 aot_library_path_ =
52 std::filesystem::path(executable_location) / aot_library_path_;
53 }
54 }
55 }
56 }
57
58 bool FlutterProjectBundle::HasValidPaths() {
59 return !assets_path_.empty() && !icu_path_.empty();
60 }
61
62 // Attempts to load AOT data from the given path, which must be absolute and
63 // non-empty. Logs and returns nullptr on failure.
64 UniqueAotDataPtr FlutterProjectBundle::LoadAotData(
65 const FlutterEngineProcTable& engine_procs) {
66 if (aot_library_path_.empty()) {
67 FML_LOG(ERROR)
68 << "Attempted to load AOT data, but no aot_library_path was provided.";
69 return UniqueAotDataPtr(nullptr, nullptr);
70 }
71 if (!std::filesystem::exists(aot_library_path_)) {
72 FML_LOG(ERROR) << "Can't load AOT data from " << aot_library_path_
73 << "; no such file.";
74 return UniqueAotDataPtr(nullptr, nullptr);
75 }
76 std::string path_string = aot_library_path_.string();
77 FlutterEngineAOTDataSource source = {};
78 source.type = kFlutterEngineAOTDataSourceTypeElfPath;
79 source.elf_path = path_string.c_str();
80 FlutterEngineAOTData data = nullptr;
81 auto result = engine_procs.CreateAOTData(&source, &data);
82 if (result != kSuccess) {
83 FML_LOG(ERROR) << "Failed to load AOT data from: " << path_string;
84 return UniqueAotDataPtr(nullptr, nullptr);
85 }
86 return UniqueAotDataPtr(data, engine_procs.CollectAOTData);
87 }
88
89 FlutterProjectBundle::~FlutterProjectBundle() {}
90
91 void FlutterProjectBundle::SetSwitches(
92 const std::vector<std::string>& switches) {
93 engine_switches_ = switches;
94 }
95
96 const std::vector<std::string> FlutterProjectBundle::GetSwitches() {
97 if (engine_switches_.size() == 0) {
98 return GetSwitchesFromEnvironment();
99 }
100 std::vector<std::string> switches;
101 switches.insert(switches.end(), engine_switches_.begin(),
102 engine_switches_.end());
103
104 auto env_switches = GetSwitchesFromEnvironment();
105 switches.insert(switches.end(), env_switches.begin(), env_switches.end());
106
107 return switches;
108 }
109
110 } // namespace flutter
flutter::FlutterProjectBundle::HasValidPaths
bool HasValidPaths()
Definition: flutter_project_bundle.cc:58
flutter::FlutterProjectBundle::GetSwitches
const std::vector< std::string > GetSwitches()
Definition: flutter_project_bundle.cc:96
flutter::FlutterProjectBundle::~FlutterProjectBundle
~FlutterProjectBundle()
Definition: flutter_project_bundle.cc:89
flutter::FlutterProjectBundle::FlutterProjectBundle
FlutterProjectBundle(const FlutterDesktopEngineProperties &properties)
Definition: flutter_project_bundle.cc:15
flutter::FlutterProjectBundle::LoadAotData
UniqueAotDataPtr LoadAotData(const FlutterEngineProcTable &engine_procs)
Definition: flutter_project_bundle.cc:64
flutter::FlutterProjectBundle::SetSwitches
void SetSwitches(const std::vector< std::string > &switches)
Definition: flutter_project_bundle.cc:91
engine_switches.h
flutter_project_bundle.h
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::FlutterUIThreadPolicy
FlutterUIThreadPolicy
Definition: flutter_project_bundle.h:25
flutter::GetSwitchesFromEnvironment
std::vector< std::string > GetSwitchesFromEnvironment()
Definition: engine_switches.cc:14
flutter::GetExecutableDirectory
std::filesystem::path GetExecutableDirectory()
Definition: path_utils.cc:16
flutter::FlutterGpuPreference
FlutterGpuPreference
Definition: flutter_project_bundle.h:20
flutter::UniqueAotDataPtr
std::unique_ptr< _FlutterEngineAOTData, FlutterEngineCollectAOTDataFnPtr > UniqueAotDataPtr
Definition: flutter_project_bundle.h:18
path_utils.h
FlutterDesktopEngineProperties
Definition: flutter_windows.h:60
FlutterDesktopEngineProperties::dart_entrypoint
const char * dart_entrypoint
Definition: flutter_windows.h:83
FlutterDesktopEngineProperties::dart_entrypoint_argc
int dart_entrypoint_argc
Definition: flutter_windows.h:86
FlutterDesktopEngineProperties::dart_entrypoint_argv
const char ** dart_entrypoint_argv
Definition: flutter_windows.h:90
FlutterDesktopEngineProperties::ui_thread_policy
FlutterDesktopUIThreadPolicy ui_thread_policy
Definition: flutter_windows.h:96
FlutterDesktopEngineProperties::aot_library_path
const wchar_t * aot_library_path
Definition: flutter_windows.h:75
FlutterDesktopEngineProperties::gpu_preference
FlutterDesktopGpuPreference gpu_preference
Definition: flutter_windows.h:93