diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 3c383909a8..99dda16cbf 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -157,6 +157,10 @@ struct Settings { // Dart code is executed. std::string advisory_script_entrypoint = "main"; + // The executable path associated with this process. This is returned by + // Platform.executable from dart:io. If unknown, defaults to "Flutter". + std::string executable_name = "Flutter"; + // Observatory settings // Whether the Dart VM service should be enabled. diff --git a/engine/src/flutter/runtime/dart_vm.cc b/engine/src/flutter/runtime/dart_vm.cc index 872726acae..f2e1269f60 100644 --- a/engine/src/flutter/runtime/dart_vm.cc +++ b/engine/src/flutter/runtime/dart_vm.cc @@ -436,6 +436,8 @@ DartVM::DartVM(std::shared_ptr vm_data, DartUI::InitForGlobal(); + dart::bin::SetExecutableName(settings_.executable_name.c_str()); + { TRACE_EVENT0("flutter", "Dart_Initialize"); Dart_InitializeParams params = {}; diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index 9812bcf8bc..86bd643017 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -233,6 +233,11 @@ std::unique_ptr GetSymbolMapping(std::string symbol_prefix, Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { Settings settings = {}; + // Set executable name. + if (command_line.has_argv0()) { + settings.executable_name = command_line.argv0(); + } + // Enable Observatory settings.enable_observatory = !command_line.HasOption(FlagForSwitch(Switch::DisableObservatory)); diff --git a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart index 8aa9a24d1b..eb06b121b7 100644 --- a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart +++ b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart @@ -41,6 +41,14 @@ void terminateExitCodeHandler() { } +@pragma('vm:entry-point') +void executableNameNotNull() { + notifyStringValue(Platform.executable); +} + +void notifyStringValue(String value) native 'NotifyStringValue'; + + @pragma('vm:entry-point') void invokePlatformTaskRunner() { PlatformDispatcher.instance.sendPlatformMessage('OhHi', null, null); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc index ae63a3220f..83c9e02447 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc @@ -102,8 +102,8 @@ EmbedderConfigBuilder::EmbedderConfigBuilder( SkImage::MakeFromBitmap(bitmap)); }; - // The first argument is treated as the executable name. Don't make tests have - // to do this manually. + // The first argument is always the executable name. Don't make tests have to + // do this manually. AddCommandLineArgument("embedder_unittest"); if (preference != InitializationPreference::kNoInitialize) { @@ -264,6 +264,13 @@ void EmbedderConfigBuilder::SetLocalizationCallbackHooks() { EmbedderTestContext::GetComputePlatformResolvedLocaleCallbackHook(); } +void EmbedderConfigBuilder::SetExecutableName(std::string executable_name) { + if (executable_name.size() == 0) { + return; + } + command_line_arguments_[0] = std::move(executable_name); +} + void EmbedderConfigBuilder::SetDartEntrypoint(std::string entrypoint) { if (entrypoint.size() == 0) { return; diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h index 73256a36b3..2e2eb18232 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h @@ -85,6 +85,8 @@ class EmbedderConfigBuilder { void SetLocalizationCallbackHooks(); + void SetExecutableName(std::string executable_name); + void SetDartEntrypoint(std::string entrypoint); void AddCommandLineArgument(std::string arg); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index 5928bef94f..decf1f4819 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -151,6 +151,28 @@ TEST_F(EmbedderTest, CanTerminateCleanly) { ASSERT_TRUE(engine.is_valid()); } +TEST_F(EmbedderTest, ExecutableNameNotNull) { + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); + + // Supply a callback to Dart for the test fixture to pass Platform.executable + // back to us. + fml::AutoResetWaitableEvent latch; + context.AddNativeCallback( + "NotifyStringValue", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { + const auto dart_string = tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 0)); + EXPECT_EQ("/path/to/binary", dart_string); + latch.Signal(); + })); + + EmbedderConfigBuilder builder(context); + builder.SetSoftwareRendererConfig(); + builder.SetDartEntrypoint("executableNameNotNull"); + builder.SetExecutableName("/path/to/binary"); + auto engine = builder.LaunchEngine(); + latch.Wait(); +} + std::atomic_size_t EmbedderTestTaskRunner::sEmbedderTaskRunnerIdentifiers = {}; TEST_F(EmbedderTest, CanSpecifyCustomPlatformTaskRunner) {