From 9ac15d1f01fe08d975a0aaabd7f29ce89b9e21fd Mon Sep 17 00:00:00 2001 From: xiaowei guan <60122246+xiaowei-guan@users.noreply.github.com> Date: Wed, 21 Jul 2021 14:22:49 +0800 Subject: [PATCH] Support dart entrypoint (#149) * Support dart entrypoint * Copy dart_entrypoint_argv to FlutterProjectArgs.dart_entrypoint.argv. * Add description for RunEngine. * Update unittest code * Fix build error --- shell/platform/tizen/flutter_project_bundle.cc | 5 +++++ shell/platform/tizen/flutter_project_bundle.h | 9 +++++++++ shell/platform/tizen/flutter_tizen.cc | 2 +- shell/platform/tizen/flutter_tizen_engine.cc | 17 ++++++++++++++++- shell/platform/tizen/flutter_tizen_engine.h | 7 +++++-- .../tizen/flutter_tizen_engine_unittest.cc | 18 +++++++++--------- shell/platform/tizen/public/flutter_tizen.h | 8 ++++++++ 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/shell/platform/tizen/flutter_project_bundle.cc b/shell/platform/tizen/flutter_project_bundle.cc index 982217c381020..f3515318b996a 100644 --- a/shell/platform/tizen/flutter_project_bundle.cc +++ b/shell/platform/tizen/flutter_project_bundle.cc @@ -43,6 +43,11 @@ FlutterProjectBundle::FlutterProjectBundle( aot_library_path_ = std::filesystem::path(properties.aot_library_path); } + for (int i = 0; i < properties.dart_entrypoint_argc; i++) { + dart_entrypoint_arguments_.push_back( + std::string(properties.dart_entrypoint_argv[i])); + } + // Resolve any relative paths. if (assets_path_.is_relative() || icu_path_.is_relative() || (!aot_library_path_.empty() && aot_library_path_.is_relative())) { diff --git a/shell/platform/tizen/flutter_project_bundle.h b/shell/platform/tizen/flutter_project_bundle.h index bf0f24fdc51b6..a24be0984fe5b 100644 --- a/shell/platform/tizen/flutter_project_bundle.h +++ b/shell/platform/tizen/flutter_project_bundle.h @@ -48,6 +48,12 @@ class FlutterProjectBundle { // Logs and returns nullptr on failure. UniqueAotDataPtr LoadAotData(const FlutterEngineProcTable& engine_procs); + // Returns the command line arguments to be passed through to the Dart + // entrypoint. + const std::vector& dart_entrypoint_arguments() const { + return dart_entrypoint_arguments_; + } + private: std::filesystem::path assets_path_; std::filesystem::path icu_path_; @@ -55,6 +61,9 @@ class FlutterProjectBundle { // Path to the AOT library file, if any. std::filesystem::path aot_library_path_; + + // Dart entrypoint arguments. + std::vector dart_entrypoint_arguments_; }; } // namespace flutter diff --git a/shell/platform/tizen/flutter_tizen.cc b/shell/platform/tizen/flutter_tizen.cc index 08f57c49127b2..71feef2f20a3d 100644 --- a/shell/platform/tizen/flutter_tizen.cc +++ b/shell/platform/tizen/flutter_tizen.cc @@ -40,7 +40,7 @@ FlutterDesktopEngineRef FlutterDesktopRunEngine( window_properties.height, window_properties.transparent, window_properties.focusable); } - if (!engine->RunEngine()) { + if (!engine->RunEngine(engine_properties.entry_point)) { FT_LOG(Error) << "Failed to start the Flutter engine."; return nullptr; } diff --git a/shell/platform/tizen/flutter_tizen_engine.cc b/shell/platform/tizen/flutter_tizen_engine.cc index 442ceda0a05a8..91b112b044d98 100644 --- a/shell/platform/tizen/flutter_tizen_engine.cc +++ b/shell/platform/tizen/flutter_tizen_engine.cc @@ -111,7 +111,7 @@ void FlutterTizenEngine::InitializeRenderer(int32_t x, #endif } -bool FlutterTizenEngine::RunEngine() { +bool FlutterTizenEngine::RunEngine(const char* entrypoint) { if (engine_ != nullptr) { FT_LOG(Error) << "The engine has already started."; return false; @@ -149,6 +149,14 @@ bool FlutterTizenEngine::RunEngine() { Logger::SetLoggingLevel(kLogLevelDebug); } + const std::vector& entrypoint_args = + project_->dart_entrypoint_arguments(); + std::vector entrypoint_argv; + std::transform( + entrypoint_args.begin(), entrypoint_args.end(), + std::back_inserter(entrypoint_argv), + [](const std::string& arg) -> const char* { return arg.c_str(); }); + // Configure task runners. FlutterTaskRunnerDescription platform_task_runner = {}; platform_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription); @@ -190,6 +198,9 @@ bool FlutterTizenEngine::RunEngine() { args.icu_data_path = icu_path_string.c_str(); args.command_line_argc = static_cast(argv.size()); args.command_line_argv = argv.size() > 0 ? argv.data() : nullptr; + args.dart_entrypoint_argc = static_cast(entrypoint_argv.size()); + args.dart_entrypoint_argv = + entrypoint_argv.size() > 0 ? entrypoint_argv.data() : nullptr; args.platform_message_callback = [](const FlutterPlatformMessage* engine_message, void* user_data) { if (engine_message->struct_size != sizeof(FlutterPlatformMessage)) { @@ -215,6 +226,10 @@ bool FlutterTizenEngine::RunEngine() { args.aot_data = aot_data_.get(); } + if (entrypoint) { + args.custom_dart_entrypoint = entrypoint; + } + FlutterRendererConfig renderer_config = GetRendererConfig(); auto result = embedder_api_.Run(FLUTTER_ENGINE_VERSION, &renderer_config, diff --git a/shell/platform/tizen/flutter_tizen_engine.h b/shell/platform/tizen/flutter_tizen_engine.h index 9ab3dcc8934b0..92294e5bf88b9 100644 --- a/shell/platform/tizen/flutter_tizen_engine.h +++ b/shell/platform/tizen/flutter_tizen_engine.h @@ -66,8 +66,11 @@ class FlutterTizenEngine : public TizenRenderer::Delegate { bool transparent, bool focusable); - // Starts running the engine. - bool RunEngine(); + // Starts running the engine with the given entrypoint. If null, defaults to + // main(). + // + // Returns false if the engine couldn't be started. + bool RunEngine(const char* entrypoint); // Stops the engine. bool StopEngine(); diff --git a/shell/platform/tizen/flutter_tizen_engine_unittest.cc b/shell/platform/tizen/flutter_tizen_engine_unittest.cc index 1a7fc1dbff529..90bb960a72604 100644 --- a/shell/platform/tizen/flutter_tizen_engine_unittest.cc +++ b/shell/platform/tizen/flutter_tizen_engine_unittest.cc @@ -50,37 +50,37 @@ class FlutterTizenEngineTestHeaded : public FlutterTizenEngineTest { TEST_F(FlutterTizenEngineTest, Run) { EXPECT_TRUE(engine_ != nullptr); - EXPECT_TRUE(engine_->RunEngine()); + EXPECT_TRUE(engine_->RunEngine(nullptr)); } TEST_F(FlutterTizenEngineTest, Run_Twice) { - EXPECT_TRUE(engine_->RunEngine()); - EXPECT_FALSE(engine_->RunEngine()); + EXPECT_TRUE(engine_->RunEngine(nullptr)); + EXPECT_FALSE(engine_->RunEngine(nullptr)); } TEST_F(FlutterTizenEngineTest, Stop) { - EXPECT_TRUE(engine_->RunEngine()); + EXPECT_TRUE(engine_->RunEngine(nullptr)); EXPECT_TRUE(engine_->StopEngine()); } TEST_F(FlutterTizenEngineTest, Stop_Twice) { - EXPECT_TRUE(engine_->RunEngine()); + EXPECT_TRUE(engine_->RunEngine(nullptr)); EXPECT_TRUE(engine_->StopEngine()); EXPECT_FALSE(engine_->StopEngine()); } TEST_F(FlutterTizenEngineTest, GetPluginRegistrar) { - EXPECT_TRUE(engine_->RunEngine()); + EXPECT_TRUE(engine_->RunEngine(nullptr)); EXPECT_TRUE(engine_->GetPluginRegistrar() != nullptr); } TEST_F(FlutterTizenEngineTest, GetTextureRegistrar) { - EXPECT_TRUE(engine_->RunEngine()); + EXPECT_TRUE(engine_->RunEngine(nullptr)); EXPECT_TRUE(engine_->GetTextureRegistrar() == nullptr); } TEST_F(FlutterTizenEngineTestHeaded, GetTextureRegistrar) { - EXPECT_TRUE(engine_->RunEngine()); + EXPECT_TRUE(engine_->RunEngine(nullptr)); EXPECT_TRUE(engine_->GetTextureRegistrar() != nullptr); } @@ -134,7 +134,7 @@ TEST_F(FlutterTizenEngineTest, RunDoesExpectedInitialization) { return kSuccess; })); - engine_->RunEngine(); + engine_->RunEngine(nullptr); EXPECT_TRUE(run_called); EXPECT_TRUE(update_locales_called); diff --git a/shell/platform/tizen/public/flutter_tizen.h b/shell/platform/tizen/public/flutter_tizen.h index 49c9a8937b64c..f17ba82607fe1 100644 --- a/shell/platform/tizen/public/flutter_tizen.h +++ b/shell/platform/tizen/public/flutter_tizen.h @@ -55,6 +55,14 @@ typedef struct { const char** switches; // The number of elements in |switches|. size_t switches_count; + // The optional entry point in the Dart project, if the entry point is null, + // defaults to main(). + const char* entry_point; + // Number of elements in the array passed in as dart_entrypoint_argv. + int dart_entrypoint_argc; + // Array of Dart entrypoint arguments. This is deep copied during the call + // to FlutterDesktopEngineCreate. + const char** dart_entrypoint_argv; } FlutterDesktopEngineProperties; // Runs an instance of a Flutter engine with the given properties.