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

Make FlutterEngineGroup support dart entrypoint args #29096

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9de9409
Make FlutterEngineGroup support initialArguments
ColdPaleLight Oct 9, 2021
cf3fdac
Fix unit tests of FlutterEngineGroupComponentTest
ColdPaleLight Oct 9, 2021
d793a65
Fix unit tests for FlutterEngine.spawn in iOS
ColdPaleLight Oct 9, 2021
42b12cf
Add unit tests in shell_unittests.cc
ColdPaleLight Oct 9, 2021
e52adb8
Add unit tests in FlutterEngineGroupComponentTest
ColdPaleLight Oct 9, 2021
c847046
Add unit tests in FlutterEngineGroupTest
ColdPaleLight Oct 9, 2021
66c4809
Change the implementation details from based on persistent isolate da…
ColdPaleLight Oct 14, 2021
f282448
Fix unit tests
ColdPaleLight Oct 14, 2021
6d77594
Tweak the code
ColdPaleLight Oct 14, 2021
14735ea
Tweak the code
ColdPaleLight Oct 14, 2021
22731c3
Fix unit tests
ColdPaleLight Oct 15, 2021
babcabf
Add unit tests in shell_unittests
ColdPaleLight Oct 15, 2021
53f82fa
Update the comments
ColdPaleLight Oct 15, 2021
9c20577
Implements method PlatformDispatcher.getInitialArguments
ColdPaleLight Oct 18, 2021
86466a3
Add getInitialArguments to web
ColdPaleLight Oct 18, 2021
6714b9e
Set the last dart entrypoint args only in debug mode
ColdPaleLight Oct 19, 2021
d1958a6
Update implementation detail of method PlatformDispatcher.getInitialA…
ColdPaleLight Oct 19, 2021
38b645a
Avoid using dynamic in getInitialArguments
ColdPaleLight Oct 19, 2021
5a23489
Support platform to provide dart entrypoint args
ColdPaleLight Oct 22, 2021
9fd9d00
Merge branch 'master' into engine_group_with_initial_arguments
ColdPaleLight Oct 22, 2021
e8da07f
Tweak the code
ColdPaleLight Oct 22, 2021
01dfcec
Merge branch 'engine_group_with_initial_arguments' of https://github.…
ColdPaleLight Oct 22, 2021
e21a6d2
Add some comments
ColdPaleLight Oct 24, 2021
dbaf33a
Merge branch 'master' into engine_group_with_initial_arguments
ColdPaleLight Nov 1, 2021
bbd3221
Merge branch 'master' into engine_group_with_initial_arguments
ColdPaleLight Nov 5, 2021
028ddbb
[iOS] Rename createEngine to makeEngine in FlutterEngineGroup
ColdPaleLight Nov 10, 2021
753aa60
Merge branch 'master' into engine_group_with_initial_arguments
ColdPaleLight Nov 10, 2021
7eeeed7
Send a List and read from it in JNI
ColdPaleLight Nov 11, 2021
ae819c4
Merge branch 'master' into engine_group_with_initial_arguments
ColdPaleLight Nov 11, 2021
ca0ee63
Tweak the API 'FlutterEngineGroup.createAndRunEngine' to avoid too ma…
ColdPaleLight Nov 12, 2021
34200b6
Merge branch 'engine_group_with_initial_arguments' of https://github.…
ColdPaleLight Nov 12, 2021
8d83669
[iOS] Add FlutterEngineGroupOptions to avoid too many arguments when …
ColdPaleLight Nov 12, 2021
1d6d00b
Update gen_objcdoc
ColdPaleLight Nov 12, 2021
d88181e
Merge branch 'master' into engine_group_with_initial_arguments
ColdPaleLight Nov 12, 2021
6b8897e
Format code
ColdPaleLight Nov 12, 2021
505497d
[iOS] Add nullable to properties of FlutterEngineGroupOptions
ColdPaleLight Nov 16, 2021
6a5d40f
Throw an assert if Runconfiguration::dart_entrypoint_args and Setting…
ColdPaleLight Nov 16, 2021
3df8fc1
Tweak the code and add the TODO comment
ColdPaleLight Nov 18, 2021
1ab03be
Merge branch 'flutter:main' into engine_group_with_initial_arguments
ColdPaleLight Nov 18, 2021
131bd4e
Fix memory leak of FlutterEngineGroupOptions
ColdPaleLight Nov 18, 2021
61a8cee
Merge branch 'flutter:main' into engine_group_with_initial_arguments
ColdPaleLight Nov 18, 2021
f3d83ac
Merge branch 'flutter:main' into engine_group_with_initial_arguments
ColdPaleLight Nov 18, 2021
8fdf017
Merge branch 'flutter:main' into engine_group_with_initial_arguments
ColdPaleLight Nov 19, 2021
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
2 changes: 2 additions & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ struct Settings {
std::string temp_directory_path;
std::vector<std::string> dart_flags;
// Arguments passed as a List<String> to Dart's entrypoint function.
// TODO(93459): Remove it when it is no longer used.
// https://github.com/flutter/flutter/issues/93459
std::vector<std::string> dart_entrypoint_args;

// Isolate settings
Expand Down
29 changes: 29 additions & 0 deletions fml/platform/android/jni_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,35 @@ std::vector<std::string> StringArrayToVector(JNIEnv* env, jobjectArray array) {
return out;
}

std::vector<std::string> StringListToVector(JNIEnv* env, jobject list) {
std::vector<std::string> out;
if (env == nullptr || list == nullptr) {
return out;
}

ScopedJavaLocalRef<jclass> list_clazz(env, env->FindClass("java/util/List"));
FML_DCHECK(!list_clazz.is_null());

jmethodID list_get =
env->GetMethodID(list_clazz.obj(), "get", "(I)Ljava/lang/Object;");
jmethodID list_size = env->GetMethodID(list_clazz.obj(), "size", "()I");

jint size = env->CallIntMethod(list, list_size);

if (size == 0) {
return out;
}

out.resize(size);
for (jint i = 0; i < size; ++i) {
ScopedJavaLocalRef<jstring> java_string(
env, static_cast<jstring>(env->CallObjectMethod(list, list_get, i)));
out[i] = JavaStringToString(env, java_string.obj());
}

return out;
}

ScopedJavaLocalRef<jobjectArray> VectorToStringArray(
JNIEnv* env,
const std::vector<std::string>& vector) {
Expand Down
2 changes: 2 additions & 0 deletions fml/platform/android/jni_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ ScopedJavaLocalRef<jstring> StringToJavaString(JNIEnv* env,

std::vector<std::string> StringArrayToVector(JNIEnv* env, jobjectArray jargs);

std::vector<std::string> StringListToVector(JNIEnv* env, jobject list);

ScopedJavaLocalRef<jobjectArray> VectorToStringArray(
JNIEnv* env,
const std::vector<std::string>& vector);
Expand Down
15 changes: 11 additions & 4 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ std::weak_ptr<DartIsolate> DartIsolate::SpawnIsolate(
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
const std::vector<std::string>& dart_entrypoint_args,
std::unique_ptr<IsolateConfiguration> isolate_configuration) const {
return CreateRunningRootIsolate(
settings, //
Expand All @@ -104,6 +105,7 @@ std::weak_ptr<DartIsolate> DartIsolate::SpawnIsolate(
isolate_shutdown_callback, //
dart_entrypoint, //
dart_entrypoint_library, //
dart_entrypoint_args, //
std::move(isolate_configuration), //
UIDartState::Context{GetTaskRunners(), //
snapshot_delegate, //
Expand All @@ -128,6 +130,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
const std::vector<std::string>& dart_entrypoint_args,
std::unique_ptr<IsolateConfiguration> isolate_configuration,
const UIDartState::Context& context,
const DartIsolate* spawning_isolate) {
Expand Down Expand Up @@ -195,10 +198,14 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
root_isolate_create_callback();
}

if (!isolate->RunFromLibrary(dart_entrypoint_library, //
dart_entrypoint, //
settings.dart_entrypoint_args //
)) {
FML_DCHECK(dart_entrypoint_args.empty() ||
settings.dart_entrypoint_args.empty());
const std::vector<std::string>& args = !dart_entrypoint_args.empty()
? dart_entrypoint_args
: settings.dart_entrypoint_args;
if (!isolate->RunFromLibrary(dart_entrypoint_library, //
dart_entrypoint, //
args)) {
FML_LOG(ERROR) << "Could not run the run main Dart entrypoint.";
return {};
}
Expand Down
4 changes: 4 additions & 0 deletions runtime/dart_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class DartIsolate : public UIDartState {
/// function to invoke.
/// @param[in] dart_entrypoint_library The name of the dart library
/// containing the entrypoint.
/// @param[in] dart_entrypoint_args Arguments passed as a List<String>
/// to Dart's entrypoint function.
/// @param[in] isolate_configuration The isolate configuration used to
/// configure the isolate before
/// invoking the entrypoint.
Expand Down Expand Up @@ -215,6 +217,7 @@ class DartIsolate : public UIDartState {
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
const std::vector<std::string>& dart_entrypoint_args,
std::unique_ptr<IsolateConfiguration> isolate_configuration,
const UIDartState::Context& context,
const DartIsolate* spawning_isolate = nullptr);
Expand Down Expand Up @@ -245,6 +248,7 @@ class DartIsolate : public UIDartState {
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
const std::vector<std::string>& dart_entrypoint_args,
std::unique_ptr<IsolateConfiguration> isolate_configuration) const;

// |UIDartState|
Expand Down
6 changes: 6 additions & 0 deletions runtime/dart_isolate_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) {
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
std::nullopt, // dart entrypoint library
{}, // dart entrypoint arguments
std::move(isolate_configuration), // isolate configuration
std::move(context) // engine context
);
Expand Down Expand Up @@ -103,6 +104,7 @@ TEST_F(DartIsolateTest, SpawnIsolate) {
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
std::nullopt, // dart entrypoint library
{}, // dart entrypoint arguments
std::move(isolate_configuration), // isolate configuration
std::move(context) // engine context
);
Expand All @@ -123,6 +125,7 @@ TEST_F(DartIsolateTest, SpawnIsolate) {
/*isolate_shutdown_callback=*/settings.isolate_shutdown_callback,
/*dart_entrypoint=*/"main",
/*dart_entrypoint_library=*/std::nullopt,
/*dart_entrypoint_args=*/{},
/*isolate_configuration=*/std::move(spawn_configuration));
auto spawn = weak_spawn.lock();
ASSERT_TRUE(spawn);
Expand Down Expand Up @@ -177,6 +180,7 @@ TEST_F(DartIsolateTest, IsolateShutdownCallbackIsInIsolateScope) {
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
std::nullopt, // dart entrypoint library
{}, // dart entrypoint arguments
std::move(isolate_configuration), // isolate configuration
std::move(context) // engine context
);
Expand Down Expand Up @@ -400,6 +404,7 @@ TEST_F(DartIsolateTest, CanCreateServiceIsolate) {
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
std::nullopt, // dart entrypoint library
{}, // dart entrypoint arguments
std::move(isolate_configuration), // isolate configuration
std::move(context) // engine context
);
Expand Down Expand Up @@ -499,6 +504,7 @@ TEST_F(DartIsolateTest, InvalidLoadingUnitFails) {
settings.isolate_shutdown_callback, // isolate shutdown callback
"main", // dart entrypoint
std::nullopt, // dart entrypoint library
{}, // dart entrypoint arguments
std::move(isolate_configuration), // isolate configuration
std::move(context) // engine context
);
Expand Down
1 change: 1 addition & 0 deletions runtime/dart_lifecycle_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static std::shared_ptr<DartIsolate> CreateAndRunRootIsolate(
settings.isolate_shutdown_callback, // isolate shutdown callback,
entrypoint, // dart entrypoint
std::nullopt, // dart entrypoint library
{}, // dart entrypoint arguments
std::move(isolate_configuration), // isolate configuration
std::move(context) // engine context
)
Expand Down
2 changes: 2 additions & 0 deletions runtime/runtime_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ bool RuntimeController::LaunchRootIsolate(
fml::closure root_isolate_create_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
const std::vector<std::string>& dart_entrypoint_args,
std::unique_ptr<IsolateConfiguration> isolate_configuration) {
if (root_isolate_.lock()) {
FML_LOG(ERROR) << "Root isolate was already running.";
Expand All @@ -375,6 +376,7 @@ bool RuntimeController::LaunchRootIsolate(
isolate_shutdown_callback_, //
dart_entrypoint, //
dart_entrypoint_library, //
dart_entrypoint_args, //
std::move(isolate_configuration), //
context_, //
spawning_isolate_.lock().get()) //
Expand Down
3 changes: 3 additions & 0 deletions runtime/runtime_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class RuntimeController : public PlatformConfigurationClient {
/// @param[in] dart_entrypoint_library The dart entrypoint library. If
/// `std::nullopt` or empty, the core
/// library will be attempted.
/// @param[in] dart_entrypoint_args Arguments passed as a List<String>
/// to Dart's entrypoint function.
/// @param[in] isolate_configuration The isolate configuration
///
/// @return If the isolate could be launched and guided to the
Expand All @@ -142,6 +144,7 @@ class RuntimeController : public PlatformConfigurationClient {
fml::closure root_isolate_create_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
const std::vector<std::string>& dart_entrypoint_args,
std::unique_ptr<IsolateConfiguration> isolate_configuration);

//----------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ Engine::RunStatus Engine::Run(RunConfiguration configuration) {

last_entry_point_ = configuration.GetEntrypoint();
last_entry_point_library_ = configuration.GetEntrypointLibrary();
#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG)
// This is only used to support restart.
last_entry_point_args_ = configuration.GetEntrypointArgs();
#endif

UpdateAssetManager(configuration.GetAssetManager());

Expand All @@ -220,6 +224,7 @@ Engine::RunStatus Engine::Run(RunConfiguration configuration) {
root_isolate_create_callback, //
configuration.GetEntrypoint(), //
configuration.GetEntrypointLibrary(), //
configuration.GetEntrypointArgs(), //
configuration.TakeIsolateConfiguration()) //
) {
return RunStatus::Failure;
Expand Down Expand Up @@ -570,6 +575,10 @@ const std::string& Engine::GetLastEntrypointLibrary() const {
return last_entry_point_library_;
}

const std::vector<std::string>& Engine::GetLastEntrypointArgs() const {
return last_entry_point_args_;
}

// |RuntimeDelegate|
void Engine::RequestDartDeferredLibrary(intptr_t loading_unit_id) {
return delegate_.RequestDartDeferredLibrary(loading_unit_id);
Expand Down
8 changes: 8 additions & 0 deletions shell/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,13 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
///
const std::string& GetLastEntrypointLibrary() const;

//----------------------------------------------------------------------------
/// @brief Get the last Entrypoint Arguments that was used in the
/// RunConfiguration when |Engine::Run| was called.This is only
/// valid in debug mode.
///
const std::vector<std::string>& GetLastEntrypointArgs() const;

//----------------------------------------------------------------------------
/// @brief Getter for the initial route. This can be set with a platform
/// message.
Expand Down Expand Up @@ -970,6 +977,7 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {

std::string last_entry_point_;
std::string last_entry_point_library_;
std::vector<std::string> last_entry_point_args_;
std::string initial_route_;
ViewportMetrics viewport_metrics_;
std::shared_ptr<AssetManager> asset_manager_;
Expand Down
14 changes: 14 additions & 0 deletions shell/common/fixtures/shell_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,17 @@ void canAccessResourceFromAssetDir() async {
},
);
}

void notifyNativeWhenEngineRun(bool success) native 'NotifyNativeWhenEngineRun';

void notifyNativeWhenEngineSpawn(bool success) native 'NotifyNativeWhenEngineSpawn';

@pragma('vm:entry-point')
void canRecieveArgumentsWhenEngineRun(List<String> args) {
notifyNativeWhenEngineRun(args.length == 2 && args[0] == 'foo' && args[1] == 'bar');
}

@pragma('vm:entry-point')
void canRecieveArgumentsWhenEngineSpawn(List<String> args) {
notifyNativeWhenEngineSpawn(args.length == 2 && args[0] == 'arg1' && args[1] == 'arg2');
}
9 changes: 9 additions & 0 deletions shell/common/run_configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ void RunConfiguration::SetEntrypointAndLibrary(std::string entrypoint,
entrypoint_library_ = std::move(library);
}

void RunConfiguration::SetEntrypointArgs(
const std::vector<std::string>& entrypoint_args) {
entrypoint_args_ = entrypoint_args;
}

std::shared_ptr<AssetManager> RunConfiguration::GetAssetManager() const {
return asset_manager_;
}
Expand All @@ -89,6 +94,10 @@ const std::string& RunConfiguration::GetEntrypointLibrary() const {
return entrypoint_library_;
}

const std::vector<std::string>& RunConfiguration::GetEntrypointArgs() const {
return entrypoint_args_;
}

std::unique_ptr<IsolateConfiguration>
RunConfiguration::TakeIsolateConfiguration() {
return std::move(isolate_configuration_);
Expand Down
13 changes: 13 additions & 0 deletions shell/common/run_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ class RunConfiguration {
///
void SetEntrypointAndLibrary(std::string entrypoint, std::string library);

//----------------------------------------------------------------------------
/// @brief Updates the main application entrypoint arguments.
///
/// @param[in] entrypoint_args The entrypoint arguments to use.
void SetEntrypointArgs(const std::vector<std::string>& entrypoint_args);

//----------------------------------------------------------------------------
/// @return The asset manager referencing all previously registered asset
/// resolvers.
Expand All @@ -168,6 +174,12 @@ class RunConfiguration {
///
const std::string& GetEntrypointLibrary() const;

//----------------------------------------------------------------------------
/// @return Arguments passed as a List<String> to Dart's entrypoint
/// function.
///
const std::vector<std::string>& GetEntrypointArgs() const;

//----------------------------------------------------------------------------
/// @brief The engine uses this to take the isolate configuration from
/// the run configuration. The run configuration is no longer
Expand All @@ -184,6 +196,7 @@ class RunConfiguration {
std::shared_ptr<AssetManager> asset_manager_;
std::string entrypoint_ = "main";
std::string entrypoint_library_ = "";
std::vector<std::string> entrypoint_args_;

FML_DISALLOW_COPY_AND_ASSIGN(RunConfiguration);
};
Expand Down
1 change: 1 addition & 0 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ bool Shell::OnServiceProtocolRunInView(

configuration.SetEntrypointAndLibrary(engine_->GetLastEntrypoint(),
engine_->GetLastEntrypointLibrary());
configuration.SetEntrypointArgs(engine_->GetLastEntrypointArgs());

configuration.AddAssetResolver(std::make_unique<DirectoryAssetBundle>(
fml::OpenDirectory(asset_directory_path.c_str(), false,
Expand Down
Loading