diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 4b32abab21..ec06daf094 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -878,6 +878,9 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_message_codec_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_method_codec_unittests.cc +FILE: ../../../flutter/shell/platform/common/cpp/engine_switches.cc +FILE: ../../../flutter/shell/platform/common/cpp/engine_switches.h +FILE: ../../../flutter/shell/platform/common/cpp/engine_switches_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/incoming_message_dispatcher.cc FILE: ../../../flutter/shell/platform/common/cpp/incoming_message_dispatcher.h FILE: ../../../flutter/shell/platform/common/cpp/json_message_codec.cc diff --git a/engine/src/flutter/shell/platform/common/cpp/BUILD.gn b/engine/src/flutter/shell/platform/common/cpp/BUILD.gn index b6c5a86b4d..a25c38f652 100644 --- a/engine/src/flutter/shell/platform/common/cpp/BUILD.gn +++ b/engine/src/flutter/shell/platform/common/cpp/BUILD.gn @@ -2,6 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//flutter/common/config.gni") import("//flutter/testing/testing.gni") config("desktop_library_implementation") { @@ -42,12 +43,25 @@ source_set("common_cpp_input") { configs += [ ":desktop_library_implementation" ] + public_configs = [ "//flutter:config" ] + if (is_win) { # For wstring_conversion. See issue #50053. defines = [ "_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING" ] } } +source_set("common_cpp_switches") { + public = [ "engine_switches.h" ] + + sources = [ "engine_switches.cc" ] + + public_configs = [ + "//flutter:config", + "//flutter/common:flutter_config", + ] +} + source_set("common_cpp") { public = [ "incoming_message_dispatcher.h", @@ -65,6 +79,8 @@ source_set("common_cpp") { configs += [ ":desktop_library_implementation" ] + public_configs = [ "//flutter:config" ] + deps = [ ":common_cpp_library_headers", "//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper", @@ -85,6 +101,8 @@ source_set("common_cpp_core") { public = [ "path_utils.h" ] sources = [ "path_utils.cc" ] + + public_configs = [ "//flutter:config" ] } if (enable_unittests) { @@ -115,6 +133,7 @@ if (enable_unittests) { testonly = true sources = [ + "engine_switches_unittests.cc", "json_message_codec_unittests.cc", "json_method_codec_unittests.cc", "text_input_model_unittests.cc", @@ -124,6 +143,7 @@ if (enable_unittests) { ":common_cpp", ":common_cpp_fixtures", ":common_cpp_input", + ":common_cpp_switches", "//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper", "//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper_library_stubs", "//flutter/testing", diff --git a/engine/src/flutter/shell/platform/common/cpp/engine_switches.cc b/engine/src/flutter/shell/platform/common/cpp/engine_switches.cc new file mode 100644 index 0000000000..edc06c145b --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/engine_switches.cc @@ -0,0 +1,41 @@ +// 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/common/cpp/engine_switches.h" + +#include +#include +#include +#include + +namespace flutter { + +std::vector GetSwitchesFromEnvironment() { + std::vector switches; + // Read engine switches from the environment in debug/profile. If release mode + // support is needed in the future, it should likely use a whitelist. +#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/common/cpp/engine_switches.h b/engine/src/flutter/shell/platform/common/cpp/engine_switches.h new file mode 100644 index 0000000000..e539ead2bb --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/engine_switches.h @@ -0,0 +1,31 @@ +// 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. + +#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_ENGINE_SWITCHES_H_ +#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_ENGINE_SWITCHES_H_ + +#include +#include + +namespace flutter { + +// Returns an array of engine switches suitable to pass to the embedder API +// in FlutterProjectArgs, based on parsing variables from the environment in +// the form: +// FLUTTER_ENGINE_SWITCHES= +// FLUTTER_ENGINE_SWITCH_1=... +// FLUTTER_ENGINE_SWITCH_2=... +// ... +// Values should match those in shell/common/switches.h +// +// The returned array does not include the initial dummy argument expected by +// the embedder API, so command_line_argv should not be set directly from it. +// +// In release mode, not all switches from the environment will necessarily be +// returned. See the implementation for details. +std::vector GetSwitchesFromEnvironment(); + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_ENGINE_SWITCHES_H_ diff --git a/engine/src/flutter/shell/platform/common/cpp/engine_switches_unittests.cc b/engine/src/flutter/shell/platform/common/cpp/engine_switches_unittests.cc new file mode 100644 index 0000000000..2823fa876f --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/engine_switches_unittests.cc @@ -0,0 +1,88 @@ +// 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/common/cpp/engine_switches.h" + +#include "gtest/gtest.h" + +namespace flutter { + +namespace { +// Sets |key=value| in the environment of this process. +void SetEnvironmentVariable(const char* key, const char* value) { +#ifdef _WIN32 + _putenv_s(key, value); +#else + setenv(key, value, 1); +#endif +} + +// Removes |key| from the environment of this process, if present. +void ClearEnvironmentVariable(const char* key) { +#ifdef _WIN32 + _putenv_s(key, ""); +#else + unsetenv(key); +#endif +} +} // namespace + +TEST(FlutterProjectBundle, SwitchesEmpty) { + // Clear the main environment variable, since test order is not guaranteed. + ClearEnvironmentVariable("FLUTTER_ENGINE_SWITCHES"); + + EXPECT_EQ(GetSwitchesFromEnvironment().size(), 0U); +} + +#ifdef FLUTTER_RELEASE +TEST(FlutterProjectBundle, SwitchesIgnoredInRelease) { + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "2"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\""); + + std::vector switches = GetSwitchesFromEnvironment(); + EXPECT_EQ(switches.size(), 0U); +} +#endif // FLUTTER_RELEASE + +#ifndef FLUTTER_RELEASE +TEST(FlutterProjectBundle, Switches) { + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "2"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\""); + + std::vector switches = GetSwitchesFromEnvironment(); + EXPECT_EQ(switches.size(), 2U); + EXPECT_EQ(switches[0], "--abc"); + EXPECT_EQ(switches[1], "--foo=\"bar, baz\""); +} + +TEST(FlutterProjectBundle, SwitchesExtraValues) { + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "1"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\""); + + std::vector switches = GetSwitchesFromEnvironment(); + EXPECT_EQ(switches.size(), 1U); + EXPECT_EQ(switches[0], "--abc"); +} + +TEST(FlutterProjectBundle, SwitchesMissingValues) { + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCHES", "4"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_1", "abc"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_2", "foo=\"bar, baz\""); + ClearEnvironmentVariable("FLUTTER_ENGINE_SWITCH_3"); + SetEnvironmentVariable("FLUTTER_ENGINE_SWITCH_4", "oops"); + + std::vector switches = GetSwitchesFromEnvironment(); + EXPECT_EQ(switches.size(), 3U); + 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 flutter diff --git a/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn b/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn index 0eb36f594a..58a9e66bcb 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn @@ -67,6 +67,7 @@ source_set("flutter_framework_source") { sources += _flutter_framework_headers deps = [ + "//flutter/shell/platform/common/cpp:common_cpp_switches", "//flutter/shell/platform/darwin/common:framework_shared", "//flutter/shell/platform/embedder:embedder_as_internal_library", ] diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h index ea35b15f75..ab4f0d9e60 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h @@ -30,15 +30,11 @@ FLUTTER_EXPORT NS_DESIGNATED_INITIALIZER; /** - * 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. + * If set, allows the Flutter project to use the dart:mirrors library. * - * Note: This property WILL BE REMOVED in the future. If you use this property, please see - * https://github.com/flutter/flutter/issues/38569. + * Deprecated: This function is temporary and will be removed in a future release. */ -@property(nullable) NSArray* engineSwitches; +@property(nonatomic) bool enableMirrors; @end diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm index 1226d392b2..a1648b332a 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm @@ -7,6 +7,8 @@ #include +#include "flutter/shell/platform/common/cpp/engine_switches.h" + static NSString* const kICUBundlePath = @"icudtl.dat"; static NSString* const kAppBundleIdentifier = @"io.flutter.flutter.app"; @@ -67,13 +69,10 @@ static NSString* const kAppBundleIdentifier = @"io.flutter.flutter.app"; return path; } -- (std::vector)argv { - // FlutterProjectArgs expects 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 arguments = {"placeholder"}; - for (NSUInteger i = 0; i < _engineSwitches.count; ++i) { - arguments.push_back([_engineSwitches[i] UTF8String]); +- (std::vector)switches { + std::vector arguments = flutter::GetSwitchesFromEnvironment(); + if (self.enableMirrors) { + arguments.push_back("--dart-flags=--enable_mirrors=true"); } return arguments; } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h index e082a2731b..ecdfa8c490 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h @@ -7,6 +7,7 @@ #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h" +#include #include /** @@ -26,12 +27,8 @@ /** * The command line arguments array for the engine. - * - * WARNING: The pointers in this array are valid only until the next call to set `engineSwitches`. - * The returned vector should be used immediately, then discarded. It is returned this way for - * ease of use with FlutterProjectArgs. */ -@property(nonatomic, readonly) std::vector argv; +@property(nonatomic, readonly) std::vector switches; /** * Instead of looking up the assets and ICU data path in the application bundle, this initializer diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 6210378110..359f13bd83 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -6,6 +6,7 @@ #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h" #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h" +#include #include #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h" @@ -236,13 +237,20 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, // TODO(stuartmorgan): Move internal channel registration from FlutterViewController to here. + // 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.switches; + std::vector argv = {"placeholder"}; + std::transform(switches.begin(), switches.end(), std::back_inserter(argv), + [](const std::string& arg) -> const char* { return arg.c_str(); }); + FlutterProjectArgs flutterArguments = {}; flutterArguments.struct_size = sizeof(FlutterProjectArgs); flutterArguments.assets_path = _project.assetsPath.UTF8String; flutterArguments.icu_data_path = _project.ICUDataPath.UTF8String; - std::vector arguments = _project.argv; - flutterArguments.command_line_argc = static_cast(arguments.size()); - flutterArguments.command_line_argv = &arguments[0]; + flutterArguments.command_line_argc = static_cast(argv.size()); + flutterArguments.command_line_argv = argv.size() > 0 ? argv.data() : nullptr; flutterArguments.platform_message_callback = (FlutterPlatformMessageCallback)OnPlatformMessage; flutterArguments.custom_dart_entrypoint = entrypoint.UTF8String; static size_t sTaskRunnerIdentifiers = 0; diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index 1cf6a53d24..66b578cf0f 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -86,6 +86,7 @@ source_set("flutter_windows_source") { ":flutter_windows_headers", "//flutter/shell/platform/common/cpp:common_cpp", "//flutter/shell/platform/common/cpp:common_cpp_input", + "//flutter/shell/platform/common/cpp:common_cpp_switches", "//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper", "//flutter/shell/platform/embedder:embedder_as_internal_library", "//flutter/shell/platform/windows/client_wrapper:client_wrapper_windows", 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 af2d0d4a98..4b2c8dfef6 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc @@ -4,11 +4,10 @@ #include "flutter/shell/platform/windows/flutter_project_bundle.h" -#include #include #include -#include +#include "flutter/shell/platform/common/cpp/engine_switches.h" #include "flutter/shell/platform/common/cpp/path_utils.h" namespace flutter { @@ -76,29 +75,7 @@ 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; + return GetSwitchesFromEnvironment(); } } // namespace flutter 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 index 359e915993..c149fbe85c 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_project_bundle_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_project_bundle_unittests.cc @@ -64,43 +64,6 @@ TEST(FlutterProjectBundle, Switches) { 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