Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,20 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
}
}

if (SAFE_ACCESS(args, dart_entrypoint_argc, 0) > 0) {
if (SAFE_ACCESS(args, dart_entrypoint_argv, nullptr) == nullptr) {
return LOG_EMBEDDER_ERROR(kInvalidArguments,
"Could not determine Dart entrypoint arguments "
"as dart_entrypoint_argc "
"was set, but dart_entrypoint_argv was null.");
}
std::vector<std::string> arguments(args->dart_entrypoint_argc);
for (int i = 0; i < args->dart_entrypoint_argc; ++i) {
arguments[i] = std::string{args->dart_entrypoint_argv[i]};
}
settings.dart_entrypoint_args = std::move(arguments);
}

if (!run_configuration.IsValid()) {
return LOG_EMBEDDER_ERROR(
kInvalidArguments,
Expand Down
13 changes: 13 additions & 0 deletions shell/platform/embedder/embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,19 @@ typedef struct {
/// matches what the platform would natively resolve to as possible.
FlutterComputePlatformResolvedLocaleCallback
compute_platform_resolved_locale_callback;

/// The command line argument count for arguments passed through to the Dart
/// entrypoint.
int dart_entrypoint_argc;

/// The command line arguments passed through to the Dart entrypoint. The
/// strings must be `NULL` terminated.
///
/// The strings will be copied out and so any strings passed in here can
/// be safely collected after initializing the engine with
/// `FlutterProjectArgs`.
const char* const* dart_entrypoint_argv;

} FlutterProjectArgs;

//------------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions shell/platform/embedder/fixtures/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,10 @@ void render_targets_are_recycled() {
};
window.scheduleFrame();
}

void nativeArgumentsCallback(List<String> args) native 'NativeArgumentsCallback';

@pragma('vm:entry-point')
void dart_entrypoint_args(List<String> args) {
nativeArgumentsCallback(args);
}
25 changes: 25 additions & 0 deletions shell/platform/embedder/tests/embedder_config_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ void EmbedderConfigBuilder::AddCommandLineArgument(std::string arg) {
command_line_arguments_.emplace_back(std::move(arg));
}

void EmbedderConfigBuilder::AddDartEntrypointArgument(std::string arg) {
if (arg.size() == 0) {
return;
}

dart_entrypoint_arguments_.emplace_back(std::move(arg));
}

void EmbedderConfigBuilder::SetPlatformTaskRunner(
const FlutterTaskRunnerDescription* runner) {
if (runner == nullptr) {
Expand Down Expand Up @@ -317,6 +325,23 @@ UniqueEngine EmbedderConfigBuilder::SetupEngine(bool run) const {
project_args.command_line_argc = 0;
}

std::vector<const char*> dart_args;
dart_args.reserve(dart_entrypoint_arguments_.size());

for (const auto& arg : dart_entrypoint_arguments_) {
dart_args.push_back(arg.c_str());
}

if (dart_args.size() > 0) {
project_args.dart_entrypoint_argv = dart_args.data();
project_args.dart_entrypoint_argc = dart_args.size();
} else {
// Clear it out in case this is not the first engine launch from the
// embedder config builder.
project_args.dart_entrypoint_argv = nullptr;
project_args.dart_entrypoint_argc = 0;
}

auto result =
run ? FlutterEngineRun(FLUTTER_ENGINE_VERSION, &renderer_config_,
&project_args, &context_, &engine)
Expand Down
3 changes: 3 additions & 0 deletions shell/platform/embedder/tests/embedder_config_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class EmbedderConfigBuilder {

void AddCommandLineArgument(std::string arg);

void AddDartEntrypointArgument(std::string arg);

void SetPlatformTaskRunner(const FlutterTaskRunnerDescription* runner);

void SetRenderTaskRunner(const FlutterTaskRunnerDescription* runner);
Expand Down Expand Up @@ -106,6 +108,7 @@ class EmbedderConfigBuilder {
FlutterCustomTaskRunners custom_task_runners_ = {};
FlutterCompositor compositor_ = {};
std::vector<std::string> command_line_arguments_;
std::vector<std::string> dart_entrypoint_arguments_;

UniqueEngine SetupEngine(bool run) const;

Expand Down
27 changes: 27 additions & 0 deletions shell/platform/embedder/tests/embedder_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,33 @@ TEST_F(EmbedderTest, VMShutsDownWhenNoEnginesInProcess) {
}
}

//------------------------------------------------------------------------------
///
TEST_F(EmbedderTest, DartEntrypointArgs) {
auto& context = GetEmbedderContext(ContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.AddDartEntrypointArgument("foo");
builder.AddDartEntrypointArgument("bar");
builder.SetDartEntrypoint("dart_entrypoint_args");
fml::AutoResetWaitableEvent callback_latch;
std::vector<std::string> callback_args;
auto nativeArgumentsCallback = [&callback_args,
&callback_latch](Dart_NativeArguments args) {
Dart_Handle exception = nullptr;
callback_args =
tonic::DartConverter<std::vector<std::string>>::FromArguments(
args, 0, exception);
callback_latch.Signal();
};
context.AddNativeCallback("NativeArgumentsCallback",
CREATE_NATIVE_ENTRY(nativeArgumentsCallback));
auto engine = builder.LaunchEngine();
callback_latch.Wait();
ASSERT_EQ(callback_args[0], "foo");
ASSERT_EQ(callback_args[1], "bar");
}

//------------------------------------------------------------------------------
/// These snapshots may be materialized from symbols and the size field may not
/// be relevant. Since this information is redundant, engine launch should not
Expand Down