From 30732e3800721eea77f68332c11543a5e209da60 Mon Sep 17 00:00:00 2001 From: George Wright Date: Fri, 16 Oct 2020 15:53:27 -0700 Subject: [PATCH 1/4] Plumb through Dart entrypoint arguments on the Linux embedder --- shell/platform/linux/fl_dart_project.cc | 22 +++++++++++++++ shell/platform/linux/fl_engine.cc | 1 + .../public/flutter_linux/fl_dart_project.h | 28 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/shell/platform/linux/fl_dart_project.cc b/shell/platform/linux/fl_dart_project.cc index b9c0b3623f9f6..8434b2a57576c 100644 --- a/shell/platform/linux/fl_dart_project.cc +++ b/shell/platform/linux/fl_dart_project.cc @@ -19,6 +19,8 @@ struct _FlDartProject { gchar* aot_library_path; gchar* assets_path; gchar* icu_data_path; + int dart_entrypoint_argc; + const char** dart_entrypoint_argv; }; G_DEFINE_TYPE(FlDartProject, fl_dart_project, G_TYPE_OBJECT) @@ -43,6 +45,9 @@ static void fl_dart_project_dispose(GObject* object) { g_clear_pointer(&self->assets_path, g_free); g_clear_pointer(&self->icu_data_path, g_free); + // We don't have ownership of this, so can just null it out + self->dart_entrypoint_argv = nullptr; + G_OBJECT_CLASS(fl_dart_project_parent_class)->dispose(object); } @@ -64,6 +69,9 @@ G_MODULE_EXPORT FlDartProject* fl_dart_project_new() { self->icu_data_path = g_build_filename(executable_dir, "data", "icudtl.dat", nullptr); + self->dart_entrypoint_argc = 0; + self->dart_entrypoint_argv = nullptr; + return self; } @@ -98,6 +106,20 @@ G_MODULE_EXPORT const gchar* fl_dart_project_get_icu_data_path( return self->icu_data_path; } +G_MODULE_EXPORT const char** fl_dart_project_get_dart_entrypoint_arguments( + FlDartProject* self, int* argc) { + g_return_if_fail(FL_IS_DART_PROJECT(self)); + *argc = self->dart_entrypoint_argc; + return self->dart_entrypoint_argv; +} + +G_MODULE_EXPORT void fl_dart_project_set_dart_entrypoint_arguments( + FlDartProject* self, int argc, const char** argv) { + g_return_if_fail(FL_IS_DART_PROJECT(self)); + self->dart_entrypoint_argc = argc; + self->dart_entrypoint_argv = argv; +} + GPtrArray* fl_dart_project_get_switches(FlDartProject* self) { GPtrArray* switches = g_ptr_array_new_with_free_func(g_free); std::vector env_switches = flutter::GetSwitchesFromEnvironment(); diff --git a/shell/platform/linux/fl_engine.cc b/shell/platform/linux/fl_engine.cc index e9cb23a749d59..d942162ebcf9d 100644 --- a/shell/platform/linux/fl_engine.cc +++ b/shell/platform/linux/fl_engine.cc @@ -391,6 +391,7 @@ gboolean fl_engine_start(FlEngine* self, GError** error) { args.platform_message_callback = fl_engine_platform_message_cb; args.custom_task_runners = &custom_task_runners; args.shutdown_dart_vm_when_done = true; + args.dart_entrypoint_argv = fl_dart_project_get_dart_entrypoint_arguments(self->project, &args.dart_entrypoint_argc); if (FlutterEngineRunsAOTCompiledDartCode()) { FlutterEngineAOTDataSource source = {}; diff --git a/shell/platform/linux/public/flutter_linux/fl_dart_project.h b/shell/platform/linux/public/flutter_linux/fl_dart_project.h index 3a0e91f245133..09cd2a10468b4 100644 --- a/shell/platform/linux/public/flutter_linux/fl_dart_project.h +++ b/shell/platform/linux/public/flutter_linux/fl_dart_project.h @@ -96,6 +96,34 @@ const gchar* fl_dart_project_get_assets_path(FlDartProject* project); */ const gchar* fl_dart_project_get_icu_data_path(FlDartProject* project); +/** + * fl_dart_project_set_dart_entrypoint_arguments: + * @project: an #FlDartProject. + * @argc: the number of command line arguments in @argv + * @argv: a pointer to an array of C strings containing the command line arguments. + * + * Sets the command line arguments to be passed through to the Dart + * entrypoint function. + * + * FlDartProject will not keep a deep copy nor ownership of these arguments; + * the caller is responsible for ensuring that they are not freed before the + * Flutter Engine is initialized, at which point they will be copied and can + * be safely freed. + */ +void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject* project, int argc, const char** argv); + +/** + * fl_dart_project_get_dart_entrypoint_arguments: + * @project: an #FlDartProject. + * @argc: a pointer to an int which will be set to the number of command line arguments + * + * Gets the command line arguments to be passed through to the Dart entrypoint function. + * + * Returns: a pointer to an array of C strings containing the command line arguments. This + * should not be deallocated by the caller. + */ +const char** fl_dart_project_get_dart_entrypoint_arguments(FlDartProject* project, int* argc); + G_END_DECLS #endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_DART_PROJECT_H_ From 6dd98c68524eeb9fe2fd76dd4b124c6078a1eb18 Mon Sep 17 00:00:00 2001 From: George Wright Date: Fri, 16 Oct 2020 17:24:21 -0700 Subject: [PATCH 2/4] Review updates --- shell/platform/linux/fl_dart_project.cc | 25 ++++++++----------- shell/platform/linux/fl_engine.cc | 6 ++++- .../public/flutter_linux/fl_dart_project.h | 13 ++++------ 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/shell/platform/linux/fl_dart_project.cc b/shell/platform/linux/fl_dart_project.cc index 8434b2a57576c..9366ff3d18d7e 100644 --- a/shell/platform/linux/fl_dart_project.cc +++ b/shell/platform/linux/fl_dart_project.cc @@ -19,8 +19,7 @@ struct _FlDartProject { gchar* aot_library_path; gchar* assets_path; gchar* icu_data_path; - int dart_entrypoint_argc; - const char** dart_entrypoint_argv; + GPtrArray* dart_entrypoint_args; }; G_DEFINE_TYPE(FlDartProject, fl_dart_project, G_TYPE_OBJECT) @@ -44,9 +43,7 @@ static void fl_dart_project_dispose(GObject* object) { g_clear_pointer(&self->aot_library_path, g_free); g_clear_pointer(&self->assets_path, g_free); g_clear_pointer(&self->icu_data_path, g_free); - - // We don't have ownership of this, so can just null it out - self->dart_entrypoint_argv = nullptr; + g_ptr_array_unref(self->dart_entrypoint_args); G_OBJECT_CLASS(fl_dart_project_parent_class)->dispose(object); } @@ -69,8 +66,7 @@ G_MODULE_EXPORT FlDartProject* fl_dart_project_new() { self->icu_data_path = g_build_filename(executable_dir, "data", "icudtl.dat", nullptr); - self->dart_entrypoint_argc = 0; - self->dart_entrypoint_argv = nullptr; + self->dart_entrypoint_args = g_ptr_array_new_with_free_func(g_free); return self; } @@ -106,18 +102,19 @@ G_MODULE_EXPORT const gchar* fl_dart_project_get_icu_data_path( return self->icu_data_path; } -G_MODULE_EXPORT const char** fl_dart_project_get_dart_entrypoint_arguments( - FlDartProject* self, int* argc) { - g_return_if_fail(FL_IS_DART_PROJECT(self)); - *argc = self->dart_entrypoint_argc; - return self->dart_entrypoint_argv; +G_MODULE_EXPORT GPtrArray* fl_dart_project_get_dart_entrypoint_arguments( + FlDartProject* self) { + g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr); + return g_ptr_array_ref(self->dart_entrypoint_args); } G_MODULE_EXPORT void fl_dart_project_set_dart_entrypoint_arguments( FlDartProject* self, int argc, const char** argv) { g_return_if_fail(FL_IS_DART_PROJECT(self)); - self->dart_entrypoint_argc = argc; - self->dart_entrypoint_argv = argv; + g_ptr_array_remove_range(self->dart_entrypoint_args, 0, self->dart_entrypoint_args->len); + for (int i = 0; i < argc; ++i) { + g_ptr_array_add(self->dart_entrypoint_args, g_strdup(argv[i])); + } } GPtrArray* fl_dart_project_get_switches(FlDartProject* self) { diff --git a/shell/platform/linux/fl_engine.cc b/shell/platform/linux/fl_engine.cc index d942162ebcf9d..a6add8f075802 100644 --- a/shell/platform/linux/fl_engine.cc +++ b/shell/platform/linux/fl_engine.cc @@ -381,6 +381,9 @@ gboolean fl_engine_start(FlEngine* self, GError** error) { // so that all switches are used. g_ptr_array_insert(command_line_args, 0, g_strdup("flutter")); + g_autoptr(GPtrArray) dart_entrypoint_args = + fl_dart_project_get_dart_entrypoint_arguments(self->project); + FlutterProjectArgs args = {}; args.struct_size = sizeof(FlutterProjectArgs); args.assets_path = fl_dart_project_get_assets_path(self->project); @@ -391,7 +394,8 @@ gboolean fl_engine_start(FlEngine* self, GError** error) { args.platform_message_callback = fl_engine_platform_message_cb; args.custom_task_runners = &custom_task_runners; args.shutdown_dart_vm_when_done = true; - args.dart_entrypoint_argv = fl_dart_project_get_dart_entrypoint_arguments(self->project, &args.dart_entrypoint_argc); + args.dart_entrypoint_argc = dart_entrypoint_args->len; + args.dart_entrypoint_argv = reinterpret_cast(dart_entrypoint_args->pdata); if (FlutterEngineRunsAOTCompiledDartCode()) { FlutterEngineAOTDataSource source = {}; diff --git a/shell/platform/linux/public/flutter_linux/fl_dart_project.h b/shell/platform/linux/public/flutter_linux/fl_dart_project.h index 09cd2a10468b4..dbf10b49dcf57 100644 --- a/shell/platform/linux/public/flutter_linux/fl_dart_project.h +++ b/shell/platform/linux/public/flutter_linux/fl_dart_project.h @@ -105,24 +105,21 @@ const gchar* fl_dart_project_get_icu_data_path(FlDartProject* project); * Sets the command line arguments to be passed through to the Dart * entrypoint function. * - * FlDartProject will not keep a deep copy nor ownership of these arguments; - * the caller is responsible for ensuring that they are not freed before the - * Flutter Engine is initialized, at which point they will be copied and can - * be safely freed. + * FlDartProject makes a deep copy of the arguments passed in here. The caller can + * safely deallocate their copy as soon as this call returns. */ void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject* project, int argc, const char** argv); /** * fl_dart_project_get_dart_entrypoint_arguments: * @project: an #FlDartProject. - * @argc: a pointer to an int which will be set to the number of command line arguments * * Gets the command line arguments to be passed through to the Dart entrypoint function. * - * Returns: a pointer to an array of C strings containing the command line arguments. This - * should not be deallocated by the caller. + * Returns: a GPtrArray containing the command line arguments to be passed to the Dart + * entrypoint. */ -const char** fl_dart_project_get_dart_entrypoint_arguments(FlDartProject* project, int* argc); +GPtrArray* fl_dart_project_get_dart_entrypoint_arguments(FlDartProject* project); G_END_DECLS From 0b3f9f49d0cb9338b88357c89153ff5d1e9e7eb3 Mon Sep 17 00:00:00 2001 From: George Wright Date: Mon, 19 Oct 2020 12:23:33 -0700 Subject: [PATCH 3/4] Don't use GPtrArray, review updates --- shell/platform/linux/fl_dart_project.cc | 18 +++++++----------- shell/platform/linux/fl_engine.cc | 6 +++--- .../public/flutter_linux/fl_dart_project.h | 14 +++++--------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/shell/platform/linux/fl_dart_project.cc b/shell/platform/linux/fl_dart_project.cc index 9366ff3d18d7e..b61520c2e8fc8 100644 --- a/shell/platform/linux/fl_dart_project.cc +++ b/shell/platform/linux/fl_dart_project.cc @@ -19,7 +19,7 @@ struct _FlDartProject { gchar* aot_library_path; gchar* assets_path; gchar* icu_data_path; - GPtrArray* dart_entrypoint_args; + gchar** dart_entrypoint_args; }; G_DEFINE_TYPE(FlDartProject, fl_dart_project, G_TYPE_OBJECT) @@ -43,7 +43,7 @@ static void fl_dart_project_dispose(GObject* object) { g_clear_pointer(&self->aot_library_path, g_free); g_clear_pointer(&self->assets_path, g_free); g_clear_pointer(&self->icu_data_path, g_free); - g_ptr_array_unref(self->dart_entrypoint_args); + g_clear_pointer(&self->dart_entrypoint_args, g_strfreev); G_OBJECT_CLASS(fl_dart_project_parent_class)->dispose(object); } @@ -66,8 +66,6 @@ G_MODULE_EXPORT FlDartProject* fl_dart_project_new() { self->icu_data_path = g_build_filename(executable_dir, "data", "icudtl.dat", nullptr); - self->dart_entrypoint_args = g_ptr_array_new_with_free_func(g_free); - return self; } @@ -102,19 +100,17 @@ G_MODULE_EXPORT const gchar* fl_dart_project_get_icu_data_path( return self->icu_data_path; } -G_MODULE_EXPORT GPtrArray* fl_dart_project_get_dart_entrypoint_arguments( +G_MODULE_EXPORT gchar** fl_dart_project_get_dart_entrypoint_arguments( FlDartProject* self) { g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr); - return g_ptr_array_ref(self->dart_entrypoint_args); + return self->dart_entrypoint_args; } G_MODULE_EXPORT void fl_dart_project_set_dart_entrypoint_arguments( - FlDartProject* self, int argc, const char** argv) { + FlDartProject* self, char** argv) { g_return_if_fail(FL_IS_DART_PROJECT(self)); - g_ptr_array_remove_range(self->dart_entrypoint_args, 0, self->dart_entrypoint_args->len); - for (int i = 0; i < argc; ++i) { - g_ptr_array_add(self->dart_entrypoint_args, g_strdup(argv[i])); - } + g_clear_pointer(&self->dart_entrypoint_args, g_strfreev); + self->dart_entrypoint_args = g_strdupv(argv); } GPtrArray* fl_dart_project_get_switches(FlDartProject* self) { diff --git a/shell/platform/linux/fl_engine.cc b/shell/platform/linux/fl_engine.cc index a6add8f075802..8cf2562a9a6e9 100644 --- a/shell/platform/linux/fl_engine.cc +++ b/shell/platform/linux/fl_engine.cc @@ -381,7 +381,7 @@ gboolean fl_engine_start(FlEngine* self, GError** error) { // so that all switches are used. g_ptr_array_insert(command_line_args, 0, g_strdup("flutter")); - g_autoptr(GPtrArray) dart_entrypoint_args = + gchar** dart_entrypoint_args = fl_dart_project_get_dart_entrypoint_arguments(self->project); FlutterProjectArgs args = {}; @@ -394,8 +394,8 @@ gboolean fl_engine_start(FlEngine* self, GError** error) { args.platform_message_callback = fl_engine_platform_message_cb; args.custom_task_runners = &custom_task_runners; args.shutdown_dart_vm_when_done = true; - args.dart_entrypoint_argc = dart_entrypoint_args->len; - args.dart_entrypoint_argv = reinterpret_cast(dart_entrypoint_args->pdata); + args.dart_entrypoint_argc = g_strv_length(dart_entrypoint_args); + args.dart_entrypoint_argv = reinterpret_cast(dart_entrypoint_args); if (FlutterEngineRunsAOTCompiledDartCode()) { FlutterEngineAOTDataSource source = {}; diff --git a/shell/platform/linux/public/flutter_linux/fl_dart_project.h b/shell/platform/linux/public/flutter_linux/fl_dart_project.h index dbf10b49dcf57..8e3afd855a72b 100644 --- a/shell/platform/linux/public/flutter_linux/fl_dart_project.h +++ b/shell/platform/linux/public/flutter_linux/fl_dart_project.h @@ -99,16 +99,12 @@ const gchar* fl_dart_project_get_icu_data_path(FlDartProject* project); /** * fl_dart_project_set_dart_entrypoint_arguments: * @project: an #FlDartProject. - * @argc: the number of command line arguments in @argv - * @argv: a pointer to an array of C strings containing the command line arguments. + * @argv: a pointer to a NULL-terminated array of C strings containing the command line arguments. * * Sets the command line arguments to be passed through to the Dart * entrypoint function. - * - * FlDartProject makes a deep copy of the arguments passed in here. The caller can - * safely deallocate their copy as soon as this call returns. */ -void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject* project, int argc, const char** argv); +void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject* project, char** argv); /** * fl_dart_project_get_dart_entrypoint_arguments: @@ -116,10 +112,10 @@ void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject* project, int a * * Gets the command line arguments to be passed through to the Dart entrypoint function. * - * Returns: a GPtrArray containing the command line arguments to be passed to the Dart - * entrypoint. + * Returns: a NULL-terminated array of strings containing the command line arguments + * to be passed to the Dart entrypoint. */ -GPtrArray* fl_dart_project_get_dart_entrypoint_arguments(FlDartProject* project); +gchar** fl_dart_project_get_dart_entrypoint_arguments(FlDartProject* project); G_END_DECLS From ac676c140436388b94b7c0464a77c46029784d8e Mon Sep 17 00:00:00 2001 From: George Wright Date: Mon, 19 Oct 2020 15:36:42 -0700 Subject: [PATCH 4/4] formatting --- shell/platform/linux/fl_dart_project.cc | 3 ++- shell/platform/linux/fl_engine.cc | 3 ++- .../linux/public/flutter_linux/fl_dart_project.h | 13 ++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/shell/platform/linux/fl_dart_project.cc b/shell/platform/linux/fl_dart_project.cc index b61520c2e8fc8..25551c72403f9 100644 --- a/shell/platform/linux/fl_dart_project.cc +++ b/shell/platform/linux/fl_dart_project.cc @@ -107,7 +107,8 @@ G_MODULE_EXPORT gchar** fl_dart_project_get_dart_entrypoint_arguments( } G_MODULE_EXPORT void fl_dart_project_set_dart_entrypoint_arguments( - FlDartProject* self, char** argv) { + FlDartProject* self, + char** argv) { g_return_if_fail(FL_IS_DART_PROJECT(self)); g_clear_pointer(&self->dart_entrypoint_args, g_strfreev); self->dart_entrypoint_args = g_strdupv(argv); diff --git a/shell/platform/linux/fl_engine.cc b/shell/platform/linux/fl_engine.cc index 8cf2562a9a6e9..6a53f91442017 100644 --- a/shell/platform/linux/fl_engine.cc +++ b/shell/platform/linux/fl_engine.cc @@ -395,7 +395,8 @@ gboolean fl_engine_start(FlEngine* self, GError** error) { args.custom_task_runners = &custom_task_runners; args.shutdown_dart_vm_when_done = true; args.dart_entrypoint_argc = g_strv_length(dart_entrypoint_args); - args.dart_entrypoint_argv = reinterpret_cast(dart_entrypoint_args); + args.dart_entrypoint_argv = + reinterpret_cast(dart_entrypoint_args); if (FlutterEngineRunsAOTCompiledDartCode()) { FlutterEngineAOTDataSource source = {}; diff --git a/shell/platform/linux/public/flutter_linux/fl_dart_project.h b/shell/platform/linux/public/flutter_linux/fl_dart_project.h index 8e3afd855a72b..1b56e24c32627 100644 --- a/shell/platform/linux/public/flutter_linux/fl_dart_project.h +++ b/shell/platform/linux/public/flutter_linux/fl_dart_project.h @@ -99,21 +99,24 @@ const gchar* fl_dart_project_get_icu_data_path(FlDartProject* project); /** * fl_dart_project_set_dart_entrypoint_arguments: * @project: an #FlDartProject. - * @argv: a pointer to a NULL-terminated array of C strings containing the command line arguments. + * @argv: a pointer to a NULL-terminated array of C strings containing the + * command line arguments. * * Sets the command line arguments to be passed through to the Dart * entrypoint function. */ -void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject* project, char** argv); +void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject* project, + char** argv); /** * fl_dart_project_get_dart_entrypoint_arguments: * @project: an #FlDartProject. * - * Gets the command line arguments to be passed through to the Dart entrypoint function. + * Gets the command line arguments to be passed through to the Dart entrypoint + * function. * - * Returns: a NULL-terminated array of strings containing the command line arguments - * to be passed to the Dart entrypoint. + * Returns: a NULL-terminated array of strings containing the command line + * arguments to be passed to the Dart entrypoint. */ gchar** fl_dart_project_get_dart_entrypoint_arguments(FlDartProject* project);