Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 62d50af

Browse files
author
George Wright
authored
Add plumbing for command line arguments on Windows (#22094)
1 parent 632354d commit 62d50af

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

shell/platform/windows/client_wrapper/flutter_engine.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ FlutterEngine::FlutterEngine(const DartProject& project) {
1717
c_engine_properties.icu_data_path = project.icu_data_path().c_str();
1818
c_engine_properties.aot_library_path = project.aot_library_path().c_str();
1919

20+
const std::vector<std::string>& entrypoint_args =
21+
project.dart_entrypoint_arguments();
22+
std::vector<const char*> entrypoint_argv;
23+
std::transform(
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+
2033
engine_ = FlutterDesktopEngineCreate(c_engine_properties);
2134

2235
auto core_messenger = FlutterDesktopEngineGetMessenger(engine_);

shell/platform/windows/client_wrapper/flutter_engine_unittests.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
2020
FlutterDesktopEngineRef EngineCreate(
2121
const FlutterDesktopEngineProperties& engine_properties) {
2222
create_called_ = true;
23+
24+
// dart_entrypoint_argv is only guaranteed to exist until this method
25+
// returns, so copy it here for unit test validation
26+
dart_entrypoint_arguments_.clear();
27+
for (int i = 0; i < engine_properties.dart_entrypoint_argc; i++) {
28+
dart_entrypoint_arguments_.push_back(
29+
std::string(engine_properties.dart_entrypoint_argv[i]));
30+
}
2331
return reinterpret_cast<FlutterDesktopEngineRef>(1);
2432
}
2533

@@ -49,11 +57,16 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
4957

5058
bool reload_fonts_called() { return reload_fonts_called_; }
5159

60+
const std::vector<std::string>& dart_entrypoint_arguments() {
61+
return dart_entrypoint_arguments_;
62+
}
63+
5264
private:
5365
bool create_called_ = false;
5466
bool run_called_ = false;
5567
bool destroy_called_ = false;
5668
bool reload_fonts_called_ = false;
69+
std::vector<std::string> dart_entrypoint_arguments_;
5770
};
5871

5972
} // namespace
@@ -121,4 +134,21 @@ TEST(FlutterEngineTest, GetMessenger) {
121134
EXPECT_NE(engine.messenger(), nullptr);
122135
}
123136

137+
TEST(FlutterEngineTest, DartEntrypointArgs) {
138+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
139+
std::make_unique<TestFlutterWindowsApi>());
140+
auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
141+
142+
DartProject project(L"data");
143+
std::vector<std::string> arguments = {"one", "two"};
144+
project.set_dart_entrypoint_arguments(arguments);
145+
146+
FlutterEngine engine(project);
147+
const std::vector<std::string>& arguments_ref =
148+
test_api->dart_entrypoint_arguments();
149+
ASSERT_EQ(2, arguments_ref.size());
150+
EXPECT_TRUE(arguments[0] == arguments_ref[0]);
151+
EXPECT_TRUE(arguments[1] == arguments_ref[1]);
152+
}
153+
124154
} // namespace flutter

shell/platform/windows/client_wrapper/include/flutter/dart_project.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ class DartProject {
2929

3030
~DartProject() = default;
3131

32+
// Sets the command line arguments that should be passed to the Dart
33+
// entrypoint.
34+
void set_dart_entrypoint_arguments(std::vector<std::string> arguments) {
35+
dart_entrypoint_arguments_ = std::move(arguments);
36+
}
37+
38+
// Returns any command line arguments that should be passed to the Dart
39+
// entrypoint.
40+
const std::vector<std::string>& dart_entrypoint_arguments() const {
41+
return dart_entrypoint_arguments_;
42+
}
43+
3244
private:
3345
// Accessors for internals are private, so that they can be changed if more
3446
// flexible options for project structures are needed later without it
@@ -49,6 +61,8 @@ class DartProject {
4961
// The path to the AOT library. This will always return a path, but non-AOT
5062
// builds will not be expected to actually have a library at that path.
5163
std::wstring aot_library_path_;
64+
// The list of arguments to pass through to the Dart entrypoint.
65+
std::vector<std::string> dart_entrypoint_arguments_;
5266
};
5367

5468
} // namespace flutter

shell/platform/windows/flutter_project_bundle.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ FlutterProjectBundle::FlutterProjectBundle(
2020
aot_library_path_ = std::filesystem::path(properties.aot_library_path);
2121
}
2222

23+
for (int i = 0; i < properties.dart_entrypoint_argc; i++) {
24+
dart_entrypoint_arguments_.push_back(
25+
std::string(properties.dart_entrypoint_argv[i]));
26+
}
27+
2328
// Resolve any relative paths.
2429
if (assets_path_.is_relative() || icu_path_.is_relative() ||
2530
(!aot_library_path_.empty() && aot_library_path_.is_relative())) {

shell/platform/windows/flutter_project_bundle.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@ class FlutterProjectBundle {
5151
// Logs and returns nullptr on failure.
5252
UniqueAotDataPtr LoadAotData();
5353

54+
// Returns the command line arguments to be passed through to the Dart
55+
// entrypoint.
56+
const std::vector<std::string>& dart_entrypoint_arguments() const {
57+
return dart_entrypoint_arguments_;
58+
}
59+
5460
private:
5561
std::filesystem::path assets_path_;
5662
std::filesystem::path icu_path_;
5763

5864
// Path to the AOT library file, if any.
5965
std::filesystem::path aot_library_path_;
66+
67+
// Dart entrypoint arguments.
68+
std::vector<std::string> dart_entrypoint_arguments_;
6069
};
6170

6271
} // namespace flutter

shell/platform/windows/flutter_windows_engine.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) {
143143
switches.begin(), switches.end(), std::back_inserter(argv),
144144
[](const std::string& arg) -> const char* { return arg.c_str(); });
145145

146+
const std::vector<std::string>& entrypoint_args =
147+
project_->dart_entrypoint_arguments();
148+
std::vector<const char*> entrypoint_argv;
149+
std::transform(
150+
entrypoint_args.begin(), entrypoint_args.end(),
151+
std::back_inserter(entrypoint_argv),
152+
[](const std::string& arg) -> const char* { return arg.c_str(); });
153+
146154
// Configure task runners.
147155
FlutterTaskRunnerDescription platform_task_runner = {};
148156
platform_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
@@ -166,6 +174,9 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) {
166174
args.icu_data_path = icu_path_string.c_str();
167175
args.command_line_argc = static_cast<int>(argv.size());
168176
args.command_line_argv = argv.size() > 0 ? argv.data() : nullptr;
177+
args.dart_entrypoint_argc = static_cast<int>(entrypoint_argv.size());
178+
args.dart_entrypoint_argv =
179+
entrypoint_argv.size() > 0 ? entrypoint_argv.data() : nullptr;
169180
args.platform_message_callback =
170181
[](const FlutterPlatformMessage* engine_message,
171182
void* user_data) -> void {

shell/platform/windows/public/flutter_windows.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ typedef struct {
4646
// containing the executable. This can be nullptr for a non-AOT build, as
4747
// it will be ignored in that case.
4848
const wchar_t* aot_library_path;
49+
50+
// Number of elements in the array passed in as dart_entrypoint_argv.
51+
int dart_entrypoint_argc;
52+
53+
// Array of Dart entrypoint arguments. This is deep copied during the call
54+
// to FlutterDesktopEngineCreate.
55+
const char** dart_entrypoint_argv;
4956
} FlutterDesktopEngineProperties;
5057

5158
// ========== View Controller ==========

0 commit comments

Comments
 (0)