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

Commit 37cccf5

Browse files
committed
Implements method PlatformDispatcher.getInitialArguments
1 parent 3f59b9f commit 37cccf5

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,13 @@ class PlatformDispatcher {
569569
/// platform channel may be used.
570570
ByteData? getPersistentIsolateData() native 'PlatformConfiguration_getPersistentIsolateData';
571571

572+
/// Get structured data provided by embedder from dart entrypoint args. it can be passed by
573+
/// `FlutterEngineGroup` when creating the engine.
574+
dynamic getInitialArguments({required List<String> rawArgs}) {
575+
if (rawArgs.isEmpty) return null;
576+
return json.decode(rawArgs[0]);
577+
}
578+
572579
/// Requests that, at the next appropriate opportunity, the [onBeginFrame] and
573580
/// [onDrawFrame] callbacks be invoked.
574581
///

shell/common/fixtures/shell_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,18 @@ void notifyNativeWhenEngineSpawn(bool success) native 'NotifyNativeWhenEngineSpa
231231

232232
@pragma('vm:entry-point')
233233
void canRecieveArgumentsWhenEngineRun(List<String> args) {
234-
notifyNativeWhenEngineRun(args.length == 1 && args[0] == 'arg1');
234+
final dynamic initialArguments = PlatformDispatcher.instance.getInitialArguments(rawArgs: args);
235+
notifyNativeWhenEngineRun(initialArguments['foo'] == 'arg1' && initialArguments['bar'] == 'arg2');
235236
}
236237

237238
@pragma('vm:entry-point')
238239
void canRecieveArgumentsWhenEngineSpawn(List<String> args) {
239-
notifyNativeWhenEngineSpawn(args.length == 1 && args[0] == 'arg2');
240+
final dynamic initialArguments = PlatformDispatcher.instance.getInitialArguments(rawArgs: args);
241+
notifyNativeWhenEngineSpawn(initialArguments[0] == 'arg1' && initialArguments[1] == 'arg2');
242+
}
243+
244+
@pragma('vm:entry-point')
245+
void canRecieveEmptyArguments(List<String> args) {
246+
final dynamic initialArguments = PlatformDispatcher.instance.getInitialArguments(rawArgs: args);
247+
notifyNativeWhenEngineRun(args.isEmpty && initialArguments == null);
240248
}

shell/common/shell_unittests.cc

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,13 +2836,14 @@ TEST_F(ShellTest, SpawnWithDartEntrypointArgs) {
28362836
auto configuration = RunConfiguration::InferFromSettings(settings);
28372837
ASSERT_TRUE(configuration.IsValid());
28382838
configuration.SetEntrypoint("canRecieveArgumentsWhenEngineRun");
2839-
const std::vector<std::string> entrypoint_args{"arg1"};
2839+
const std::vector<std::string> entrypoint_args{
2840+
"{\"foo\":\"arg1\",\"bar\":\"arg2\"}"};
28402841
configuration.SetEntrypointArgs(entrypoint_args);
28412842

28422843
auto second_configuration = RunConfiguration::InferFromSettings(settings);
28432844
ASSERT_TRUE(second_configuration.IsValid());
28442845
second_configuration.SetEntrypoint("canRecieveArgumentsWhenEngineSpawn");
2845-
const std::vector<std::string> second_entrypoint_args{"arg2"};
2846+
const std::vector<std::string> second_entrypoint_args{"[\"arg1\",\"arg2\"]"};
28462847
second_configuration.SetEntrypointArgs(second_entrypoint_args);
28472848

28482849
const std::string initial_route("/foo");
@@ -2935,6 +2936,36 @@ TEST_F(ShellTest, SpawnWithDartEntrypointArgs) {
29352936
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
29362937
}
29372938

2939+
TEST_F(ShellTest, CanRecieveEmptyArguments) {
2940+
auto settings = CreateSettingsForFixture();
2941+
auto shell = CreateShell(settings);
2942+
ASSERT_TRUE(ValidateShell(shell.get()));
2943+
2944+
auto configuration = RunConfiguration::InferFromSettings(settings);
2945+
ASSERT_TRUE(configuration.IsValid());
2946+
configuration.SetEntrypoint("canRecieveEmptyArguments");
2947+
2948+
fml::AutoResetWaitableEvent main_latch;
2949+
std::string last_entry_point;
2950+
2951+
AddNativeCallback("NotifyNativeWhenEngineRun",
2952+
CREATE_NATIVE_ENTRY(([&](Dart_NativeArguments args) {
2953+
ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(
2954+
Dart_GetNativeArgument(args, 0)));
2955+
last_entry_point =
2956+
shell->GetEngine()->GetLastEntrypoint();
2957+
main_latch.Signal();
2958+
})));
2959+
2960+
RunEngine(shell.get(), std::move(configuration));
2961+
main_latch.Wait();
2962+
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
2963+
ASSERT_EQ("canRecieveEmptyArguments", last_entry_point);
2964+
2965+
DestroyShell(std::move(shell));
2966+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
2967+
}
2968+
29382969
TEST_F(ShellTest, UpdateAssetResolverByTypeReplaces) {
29392970
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
29402971
Settings settings = CreateSettingsForFixture();

0 commit comments

Comments
 (0)