diff --git a/engine/src/flutter/shell/platform/windows/fixtures/main.dart b/engine/src/flutter/shell/platform/windows/fixtures/main.dart index e2d76aa617..57ba615d98 100644 --- a/engine/src/flutter/shell/platform/windows/fixtures/main.dart +++ b/engine/src/flutter/shell/platform/windows/fixtures/main.dart @@ -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 'dart:io' as io; import 'dart:ui' as ui; // Signals a waiting latch in the native test. @@ -10,6 +11,9 @@ void signal() native 'Signal'; // Signals a waiting latch in the native test, passing a boolean value. void signalBoolValue(bool value) native 'SignalBoolValue'; +// Signals a waiting latch in the native test, passing a string value. +void signalStringValue(String value) native 'SignalStringValue'; + // Signals a waiting latch in the native test, which returns a value to the fixture. bool signalBoolReturn() native 'SignalBoolReturn'; @@ -39,6 +43,11 @@ void verifyNativeFunctionWithReturn() { signalBoolValue(value); } +@pragma('vm:entry-point') +void readPlatformExecutable() { + signalStringValue(io.Platform.executable); +} + @pragma('vm:entry-point') void drawHelloWorld() { ui.PlatformDispatcher.instance.onBeginFrame = (Duration duration) { 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 6c3d728f7e..9396a9e2c1 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -11,6 +11,7 @@ #include #include "flutter/fml/logging.h" +#include "flutter/fml/paths.h" #include "flutter/fml/platform/win/wstring_conversion.h" #include "flutter/shell/platform/common/client_wrapper/binary_messenger_impl.h" #include "flutter/shell/platform/common/path_utils.h" @@ -224,8 +225,9 @@ bool FlutterWindowsEngine::Run(std::string_view 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::string executable_name = GetExecutableName(); + std::vector argv = {executable_name.c_str()}; std::vector switches = project_->GetSwitches(); - std::vector argv = {"placeholder"}; std::transform( switches.begin(), switches.end(), std::back_inserter(argv), [](const std::string& arg) -> const char* { return arg.c_str(); }); @@ -532,10 +534,9 @@ void FlutterWindowsEngine::SendSystemLocales() { // Convert the locale list to the locale pointer list that must be provided. std::vector flutter_locale_list; flutter_locale_list.reserve(flutter_locales.size()); - std::transform( - flutter_locales.begin(), flutter_locales.end(), - std::back_inserter(flutter_locale_list), - [](const auto& arg) -> const auto* { return &arg; }); + std::transform(flutter_locales.begin(), flutter_locales.end(), + std::back_inserter(flutter_locale_list), + [](const auto& arg) -> const auto* { return &arg; }); embedder_api_.UpdateLocales(engine_, flutter_locale_list.data(), flutter_locale_list.size()); } @@ -592,4 +593,18 @@ gfx::NativeViewAccessible FlutterWindowsEngine::GetNativeAccessibleFromId( return node_delegate->GetNativeViewAccessible(); } +std::string FlutterWindowsEngine::GetExecutableName() const { + std::pair result = fml::paths::GetExecutablePath(); + if (result.first) { + const std::string& executable_path = result.second; + size_t last_separator = executable_path.find_last_of("/\\"); + if (last_separator == std::string::npos || + last_separator == executable_path.size() - 1) { + return executable_path; + } + return executable_path.substr(last_separator + 1); + } + return "Flutter"; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index 3ead42238f..700f7889a2 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -220,6 +220,9 @@ class FlutterWindowsEngine { root_isolate_create_callback_ = callback; } + // Returns the executable name for this process or "Flutter" if unknown. + std::string GetExecutableName() const; + private: // Allows swapping out embedder_api_ calls in tests. friend class EngineModifier; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc index 87315efad5..1417fe48a3 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -404,5 +404,10 @@ TEST(FlutterWindowsEngine, SetNextFrameCallback) { EXPECT_TRUE(called); } +TEST(FlutterWindowsEngine, GetExecutableName) { + std::unique_ptr engine = GetTestEngine(); + EXPECT_EQ(engine->GetExecutableName(), "flutter_windows_unittests.exe"); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc index 720c7b3fbc..40cd9e32a3 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc @@ -118,6 +118,30 @@ TEST_F(WindowsTest, VerifyNativeFunctionWithParameters) { EXPECT_TRUE(bool_value); } +// Verify that Platform.executable returns the executable name. +TEST_F(WindowsTest, PlatformExecutable) { + auto& context = GetContext(); + WindowsConfigBuilder builder(context); + builder.SetDartEntrypoint("readPlatformExecutable"); + + std::string executable_name; + fml::AutoResetWaitableEvent latch; + auto native_entry = CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { + auto handle = Dart_GetNativeArgument(args, 0); + ASSERT_FALSE(Dart_IsError(handle)); + executable_name = tonic::DartConverter::FromDart(handle); + latch.Signal(); + }); + context.AddNativeFunction("SignalStringValue", native_entry); + + ViewControllerPtr controller{builder.Run()}; + ASSERT_NE(controller, nullptr); + + // Wait until signalStringValue has been called. + latch.Wait(); + EXPECT_EQ(executable_name, "flutter_windows_unittests.exe"); +} + // Verify that native functions that return values can be registered and // resolved. TEST_F(WindowsTest, VerifyNativeFunctionWithReturn) {