diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index bfa1b74aed..87c5731531 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1360,6 +1360,7 @@ FILE: ../../../flutter/shell/platform/windows/cursor_handler.cc FILE: ../../../flutter/shell/platform/windows/cursor_handler.h FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle.cc FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle.h +FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle_unittests.cc FILE: ../../../flutter/shell/platform/windows/flutter_windows.cc FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine.cc FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine.h diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index d8e3c99434..1cf6a53d24 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -121,6 +121,7 @@ executable("flutter_windows_unittests") { testonly = true sources = [ + "flutter_project_bundle_unittests.cc", "string_conversion_unittests.cc", "system_utils_unittests.cc", "testing/mock_win32_window.cc", diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/dart_project_unittests.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/dart_project_unittests.cc index 7be4593e0a..116fba4408 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/dart_project_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/dart_project_unittests.cc @@ -26,12 +26,6 @@ class DartProjectTest : public ::testing::Test { std::wstring GetProjectAotLibraryPath(const DartProject& project) { return project.aot_library_path(); } - - // Wrapper for accessing private engine_switches. - std::vector GetProjectEngineSwitches( - const DartProject& project) { - return project.engine_switches(); - } }; TEST_F(DartProjectTest, StandardProjectFormat) { @@ -41,12 +35,4 @@ TEST_F(DartProjectTest, StandardProjectFormat) { EXPECT_EQ(GetProjectAotLibraryPath(project), L"test\\app.so"); } -TEST_F(DartProjectTest, Switches) { - DartProject project(L"test"); - std::vector switches = {"--foo", "--bar"}; - project.SetEngineSwitches(switches); - EXPECT_EQ(GetProjectEngineSwitches(project).size(), 2); - EXPECT_EQ(GetProjectEngineSwitches(project)[0], "--foo"); -} - } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc index affadb42dc..f6073f7f84 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc @@ -16,15 +16,6 @@ FlutterEngine::FlutterEngine(const DartProject& project) { c_engine_properties.assets_path = project.assets_path().c_str(); c_engine_properties.icu_data_path = project.icu_data_path().c_str(); c_engine_properties.aot_library_path = project.aot_library_path().c_str(); - std::vector engine_switches; - std::transform( - project.engine_switches().begin(), project.engine_switches().end(), - std::back_inserter(engine_switches), - [](const std::string& arg) -> const char* { return arg.c_str(); }); - if (engine_switches.size() > 0) { - c_engine_properties.switches = &engine_switches[0]; - c_engine_properties.switches_count = engine_switches.size(); - } engine_ = FlutterDesktopEngineCreate(c_engine_properties); diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/dart_project.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/dart_project.h index 6c94ffa757..9f064417bf 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/dart_project.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/dart_project.h @@ -29,17 +29,6 @@ class DartProject { ~DartProject() = default; - // Switches to pass to the Flutter engine. See - // https://github.com/flutter/engine/blob/master/shell/common/switches.h - // for details. Not all switches will apply to embedding mode. Switches have - // not stability guarantee, and are subject to change without notice. - // - // Note: This WILL BE REMOVED in the future. If you call this, please see - // https://github.com/flutter/flutter/issues/38569. - void SetEngineSwitches(const std::vector& switches) { - engine_switches_ = switches; - } - private: // Accessors for internals are private, so that they can be changed if more // flexible options for project structures are needed later without it @@ -52,9 +41,6 @@ class DartProject { const std::wstring& assets_path() const { return assets_path_; } const std::wstring& icu_data_path() const { return icu_data_path_; } const std::wstring& aot_library_path() const { return aot_library_path_; } - const std::vector& engine_switches() const { - return engine_switches_; - } // The path to the assets directory. std::wstring assets_path_; @@ -63,8 +49,6 @@ class DartProject { // The path to the AOT library. This will always return a path, but non-AOT // builds will not be expected to actually have a library at that path. std::wstring aot_library_path_; - // Switches to pass to the engine. - std::vector engine_switches_; }; } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc b/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc index 2459311524..af2d0d4a98 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc @@ -4,8 +4,10 @@ #include "flutter/shell/platform/windows/flutter_project_bundle.h" +#include #include #include +#include #include "flutter/shell/platform/common/cpp/path_utils.h" @@ -38,11 +40,6 @@ FlutterProjectBundle::FlutterProjectBundle( } } } - - if (properties.switches_count > 0) { - switches_.insert(switches_.end(), &properties.switches[0], - &properties.switches[properties.switches_count]); - } } bool FlutterProjectBundle::HasValidPaths() { @@ -78,4 +75,30 @@ UniqueAotDataPtr FlutterProjectBundle::LoadAotData() { FlutterProjectBundle::~FlutterProjectBundle() {} +const std::vector FlutterProjectBundle::GetSwitches() { + std::vector switches; + // Read engine switches from the environment in debug/profile. +#ifndef FLUTTER_RELEASE + const char* switch_count_key = "FLUTTER_ENGINE_SWITCHES"; + const int kMaxSwitchCount = 50; + const char* switch_count_string = std::getenv(switch_count_key); + int switch_count = std::min( + kMaxSwitchCount, switch_count_string ? atoi(switch_count_string) : 0); + for (int i = 1; i <= switch_count; ++i) { + std::ostringstream switch_key; + switch_key << "FLUTTER_ENGINE_SWITCH_" << i; + const char* switch_value = std::getenv(switch_key.str().c_str()); + if (switch_value) { + std::ostringstream switch_value_as_flag; + switch_value_as_flag << "--" << switch_value; + switches.push_back(switch_value_as_flag.str()); + } else { + std::cerr << switch_count << " keys expected from " << switch_count_key + << ", but " << switch_key.str() << " is missing." << std::endl; + } + } +#endif // !FLUTTER_RELEASE + return switches; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_project_bundle.h b/engine/src/flutter/shell/platform/windows/flutter_project_bundle.h index f8ab57be1e..fc77adfe8c 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_project_bundle.h +++ b/engine/src/flutter/shell/platform/windows/flutter_project_bundle.h @@ -43,7 +43,7 @@ class FlutterProjectBundle { const std::filesystem::path& icu_path() { return icu_path_; } // Returns any switches that should be passed to the engine. - const std::vector& switches() { return switches_; } + const std::vector GetSwitches(); // Attempts to load AOT data for this bundle. The returned data must be // retained until any engine instance it is passed to has been shut down. @@ -54,7 +54,6 @@ class FlutterProjectBundle { private: std::filesystem::path assets_path_; std::filesystem::path icu_path_; - std::vector switches_; // Path to the AOT library file, if any. std::filesystem::path aot_library_path_; diff --git a/engine/src/flutter/shell/platform/windows/flutter_project_bundle_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_project_bundle_unittests.cc new file mode 100644 index 0000000000..359e915993 --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/flutter_project_bundle_unittests.cc @@ -0,0 +1,107 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/windows/flutter_project_bundle.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +TEST(FlutterProjectBundle, BasicPropertiesAbsolutePaths) { + FlutterDesktopEngineProperties properties = {}; + properties.assets_path = L"C:\\foo\\flutter_assets"; + properties.icu_data_path = L"C:\\foo\\icudtl.dat"; + + FlutterProjectBundle project(properties); + + EXPECT_TRUE(project.HasValidPaths()); + EXPECT_EQ(project.assets_path().string(), "C:\\foo\\flutter_assets"); + EXPECT_EQ(project.icu_path().string(), "C:\\foo\\icudtl.dat"); +} + +TEST(FlutterProjectBundle, BasicPropertiesRelativePaths) { + FlutterDesktopEngineProperties properties = {}; + properties.assets_path = L"foo\\flutter_assets"; + properties.icu_data_path = L"foo\\icudtl.dat"; + + FlutterProjectBundle project(properties); + + EXPECT_TRUE(project.HasValidPaths()); + EXPECT_TRUE(project.assets_path().is_absolute()); + EXPECT_EQ(project.assets_path().filename().string(), "flutter_assets"); + EXPECT_TRUE(project.icu_path().is_absolute()); + EXPECT_EQ(project.icu_path().filename().string(), "icudtl.dat"); +} + +TEST(FlutterProjectBundle, SwitchesEmpty) { + FlutterDesktopEngineProperties properties = {}; + properties.assets_path = L"foo\\flutter_assets"; + properties.icu_data_path = L"foo\\icudtl.dat"; + + // Clear the main environment variable, since test order is not guaranteed. + _putenv_s("FLUTTER_ENGINE_SWITCHES", ""); + + FlutterProjectBundle project(properties); + + EXPECT_EQ(project.GetSwitches().size(), 0); +} + +#ifndef FLUTTER_RELEASE +TEST(FlutterProjectBundle, Switches) { + FlutterDesktopEngineProperties properties = {}; + properties.assets_path = L"foo\\flutter_assets"; + properties.icu_data_path = L"foo\\icudtl.dat"; + + _putenv_s("FLUTTER_ENGINE_SWITCHES", "2"); + _putenv_s("FLUTTER_ENGINE_SWITCH_1", "abc"); + _putenv_s("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\""); + + FlutterProjectBundle project(properties); + + std::vector switches = project.GetSwitches(); + EXPECT_EQ(switches.size(), 2); + EXPECT_EQ(switches[0], "--abc"); + EXPECT_EQ(switches[1], "--foo=\"bar, baz\""); +} + +TEST(FlutterProjectBundle, SwitchesExtraValues) { + FlutterDesktopEngineProperties properties = {}; + properties.assets_path = L"foo\\flutter_assets"; + properties.icu_data_path = L"foo\\icudtl.dat"; + + _putenv_s("FLUTTER_ENGINE_SWITCHES", "1"); + _putenv_s("FLUTTER_ENGINE_SWITCH_1", "abc"); + _putenv_s("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\""); + + FlutterProjectBundle project(properties); + + std::vector switches = project.GetSwitches(); + EXPECT_EQ(switches.size(), 1); + EXPECT_EQ(switches[0], "--abc"); +} + +TEST(FlutterProjectBundle, SwitchesMissingValues) { + FlutterDesktopEngineProperties properties = {}; + properties.assets_path = L"foo\\flutter_assets"; + properties.icu_data_path = L"foo\\icudtl.dat"; + + _putenv_s("FLUTTER_ENGINE_SWITCHES", "4"); + _putenv_s("FLUTTER_ENGINE_SWITCH_1", "abc"); + _putenv_s("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\""); + _putenv_s("FLUTTER_ENGINE_SWITCH_4", "oops"); + + FlutterProjectBundle project(properties); + + std::vector switches = project.GetSwitches(); + EXPECT_EQ(switches.size(), 3); + EXPECT_EQ(switches[0], "--abc"); + EXPECT_EQ(switches[1], "--foo=\"bar, baz\""); + // The missing switch should be skipped, leaving SWITCH_4 as the third + // switch in the array. + EXPECT_EQ(switches[2], "--oops"); +} +#endif // !FLUTTER_RELEASE + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 36aa92dc21..d70ca504c1 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -137,10 +137,10 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) { // FlutterProjectArgs is expecting a full argv, so when processing it for // flags the first item is treated as the executable and ignored. Add a dummy // value so that all provided arguments are used. + std::vector switches = project_->GetSwitches(); std::vector argv = {"placeholder"}; std::transform( - project_->switches().begin(), project_->switches().end(), - std::back_inserter(argv), + switches.begin(), switches.end(), std::back_inserter(argv), [](const std::string& arg) -> const char* { return arg.c_str(); }); // Configure task runners. diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 6862a3a21c..25414937e5 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -46,15 +46,6 @@ typedef struct { // containing the executable. This can be nullptr for a non-AOT build, as // it will be ignored in that case. const wchar_t* aot_library_path; - - // The switches to pass to the Flutter engine. - // - // See: https://github.com/flutter/engine/blob/master/shell/common/switches.h - // for details. Not all arguments will apply to desktop. - const char** switches; - - // The number of elements in |switches|. - size_t switches_count; } FlutterDesktopEngineProperties; // ========== View Controller ==========