From a56e2c9e5e8c27337e768817f9754093c858c0ff Mon Sep 17 00:00:00 2001 From: James Clarke Date: Mon, 19 Apr 2021 07:44:01 -0700 Subject: [PATCH] Windows: Add support for engine switches for WinUWP target (flutter/engine#25412) --- .../shell/platform/common/engine_switches.cc | 2 - .../client_wrapper/flutter_view_controller.cc | 3 +- .../include/flutter/flutter_view_controller.h | 9 ++++- .../windows/flutter_project_bundle.cc | 9 +++++ .../platform/windows/flutter_project_bundle.h | 6 +++ .../windows/flutter_windows_engine.cc | 5 +++ .../platform/windows/flutter_windows_engine.h | 3 ++ .../windows/flutter_windows_winuwp.cc | 38 +++++++++++++++++++ .../platform/windows/public/flutter_windows.h | 3 ++ 9 files changed, 73 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/common/engine_switches.cc b/engine/src/flutter/shell/platform/common/engine_switches.cc index 2295a5508f..5c750f13d1 100644 --- a/engine/src/flutter/shell/platform/common/engine_switches.cc +++ b/engine/src/flutter/shell/platform/common/engine_switches.cc @@ -16,8 +16,6 @@ std::vector GetSwitchesFromEnvironment() { // 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 -// TODO(clarkezone): figure out how to handle engine switches in UWP mode -// https://github.com/flutter/flutter/issues/74153 #ifndef WINUWP const char* switch_count_key = "FLUTTER_ENGINE_SWITCHES"; const int kMaxSwitchCount = 50; diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc index a4c5a465a1..5e47d8c382 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc @@ -12,10 +12,11 @@ namespace flutter { #ifdef WINUWP FlutterViewController::FlutterViewController( ABI::Windows::UI::Core::CoreWindow* window, + ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, const DartProject& project) { engine_ = std::make_unique(project); controller_ = FlutterDesktopViewControllerCreateFromCoreWindow( - window, engine_->RelinquishEngine()); + window, args, engine_->RelinquishEngine()); if (!controller_) { std::cerr << "Failed to create view controller." << std::endl; return; diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h index dcab4a7f84..66808fc135 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h @@ -18,6 +18,7 @@ #include "plugin_registry.h" #ifdef WINUWP +#include #include #endif @@ -43,8 +44,12 @@ class FlutterViewController { // either using CoreWindow. // // |dart_project| will be used to configure the engine backing this view. - explicit FlutterViewController(ABI::Windows::UI::Core::CoreWindow* window, - const DartProject& project); + // |IActivatedEventArgs| will be used to configure the engine switches. Can + // be set to nullptr. + explicit FlutterViewController( + ABI::Windows::UI::Core::CoreWindow* window, + ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, + const DartProject& project); #endif virtual ~FlutterViewController(); 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 a88946b19f..787ecd2100 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc @@ -80,8 +80,17 @@ UniqueAotDataPtr FlutterProjectBundle::LoadAotData( FlutterProjectBundle::~FlutterProjectBundle() {} +void FlutterProjectBundle::SetSwitches( + const std::vector& switches) { + engine_switches_ = switches; +} + const std::vector FlutterProjectBundle::GetSwitches() { +#if WINUWP + return engine_switches_; +#else return GetSwitchesFromEnvironment(); +#endif } } // 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 332b0060a9..3cb5d3ca61 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_project_bundle.h +++ b/engine/src/flutter/shell/platform/windows/flutter_project_bundle.h @@ -41,6 +41,9 @@ class FlutterProjectBundle { // Returns any switches that should be passed to the engine. const std::vector GetSwitches(); + // Sets engine switches. + void SetSwitches(const std::vector& switches); + // 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. // @@ -62,6 +65,9 @@ class FlutterProjectBundle { // Dart entrypoint arguments. std::vector dart_entrypoint_arguments_; + + // Engine switches. + std::vector engine_switches_; }; } // 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 914e8f700f..6eee033835 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -184,6 +184,11 @@ FlutterWindowsEngine::~FlutterWindowsEngine() { Stop(); } +void FlutterWindowsEngine::SetSwitches( + const std::vector& switches) { + project_->SetSwitches(switches); +} + bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) { if (!project_->HasValidPaths()) { std::cerr << "Missing or unresolvable paths to assets." << std::endl; 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 e046d1ccd9..3dc729b253 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -74,6 +74,9 @@ class FlutterWindowsEngine { void SetPluginRegistrarDestructionCallback( FlutterDesktopOnPluginRegistrarDestroyed callback); + // Sets switches member to the given switches. + void SetSwitches(const std::vector& switches); + FlutterDesktopMessengerRef messenger() { return messenger_.get(); } IncomingMessageDispatcher* message_dispatcher() { diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc index 942d4b1ec7..90363154fc 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc @@ -13,8 +13,12 @@ #include #include #include +#include #include +#include +#include "winrt/Windows.ApplicationModel.Core.h" + #include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h" #include "flutter/shell/platform/common/incoming_message_dispatcher.h" #include "flutter/shell/platform/windows/flutter_window_winuwp.h" // nogncheck @@ -25,9 +29,21 @@ static flutter::FlutterWindowsEngine* EngineFromHandle( return reinterpret_cast(ref); } +// Returns a list of discrete arguments splitting the input using a ",". +std::vector SplitCommaSeparatedString(const std::string& s) { + std::vector components; + std::istringstream stream(s); + std::string component; + while (getline(stream, component, ',')) { + components.push_back(component); + } + return (components); +} + FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreateFromCoreWindow( ABI::Windows::UI::Core::CoreWindow* window, + ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, FlutterDesktopEngineRef engine) { std::unique_ptr window_wrapper = std::make_unique(window); @@ -39,6 +55,28 @@ FlutterDesktopViewControllerCreateFromCoreWindow( state->view->SetEngine( std::unique_ptr(EngineFromHandle(engine))); state->view->CreateRenderSurface(); + + winrt::Windows::ApplicationModel::Activation::IActivatedEventArgs + arg_interface{nullptr}; + winrt::copy_from_abi(arg_interface, args); + + std::vector engine_switches; + winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs launch{ + nullptr}; + if (arg_interface.Kind() == + winrt::Windows::ApplicationModel::Activation::ActivationKind::Launch) { + launch = arg_interface.as(); + if (launch != nullptr) { + std::string launchargs = winrt::to_string(launch.Arguments()); + if (!launchargs.empty()) { + engine_switches = SplitCommaSeparatedString(launchargs); + } + } + } + + state->view->GetEngine()->SetSwitches(engine_switches); + if (!state->view->GetEngine()->running()) { if (!state->view->GetEngine()->RunWithEntrypoint(nullptr)) { return nullptr; 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 f96c731703..76802ba567 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -14,6 +14,7 @@ #include "flutter_plugin_registrar.h" #ifdef WINUWP +#include #include #endif @@ -57,6 +58,7 @@ typedef struct { // Array of Dart entrypoint arguments. This is deep copied during the call // to FlutterDesktopEngineCreate. const char** dart_entrypoint_argv; + } FlutterDesktopEngineProperties; // ========== View Controller ========== @@ -80,6 +82,7 @@ typedef struct { FLUTTER_EXPORT FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreateFromCoreWindow( ABI::Windows::UI::Core::CoreWindow* window, + ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, FlutterDesktopEngineRef engine); #else //! WINUWP // The Win32 implementation accepts width, height