Windows: Add support for engine switches for WinUWP target (flutter/engine#25412)

This commit is contained in:
James Clarke
2021-04-19 07:44:01 -07:00
committed by GitHub
parent 988c4ffb83
commit a56e2c9e5e
9 changed files with 73 additions and 5 deletions

View File

@@ -16,8 +16,6 @@ std::vector<std::string> 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;

View File

@@ -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<FlutterEngine>(project);
controller_ = FlutterDesktopViewControllerCreateFromCoreWindow(
window, engine_->RelinquishEngine());
window, args, engine_->RelinquishEngine());
if (!controller_) {
std::cerr << "Failed to create view controller." << std::endl;
return;

View File

@@ -18,6 +18,7 @@
#include "plugin_registry.h"
#ifdef WINUWP
#include <windows.applicationmodel.activation.h>
#include <windows.ui.core.h>
#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();

View File

@@ -80,8 +80,17 @@ UniqueAotDataPtr FlutterProjectBundle::LoadAotData(
FlutterProjectBundle::~FlutterProjectBundle() {}
void FlutterProjectBundle::SetSwitches(
const std::vector<std::string>& switches) {
engine_switches_ = switches;
}
const std::vector<std::string> FlutterProjectBundle::GetSwitches() {
#if WINUWP
return engine_switches_;
#else
return GetSwitchesFromEnvironment();
#endif
}
} // namespace flutter

View File

@@ -41,6 +41,9 @@ class FlutterProjectBundle {
// Returns any switches that should be passed to the engine.
const std::vector<std::string> GetSwitches();
// Sets engine switches.
void SetSwitches(const std::vector<std::string>& 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<std::string> dart_entrypoint_arguments_;
// Engine switches.
std::vector<std::string> engine_switches_;
};
} // namespace flutter

View File

@@ -184,6 +184,11 @@ FlutterWindowsEngine::~FlutterWindowsEngine() {
Stop();
}
void FlutterWindowsEngine::SetSwitches(
const std::vector<std::string>& switches) {
project_->SetSwitches(switches);
}
bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) {
if (!project_->HasValidPaths()) {
std::cerr << "Missing or unresolvable paths to assets." << std::endl;

View File

@@ -74,6 +74,9 @@ class FlutterWindowsEngine {
void SetPluginRegistrarDestructionCallback(
FlutterDesktopOnPluginRegistrarDestroyed callback);
// Sets switches member to the given switches.
void SetSwitches(const std::vector<std::string>& switches);
FlutterDesktopMessengerRef messenger() { return messenger_.get(); }
IncomingMessageDispatcher* message_dispatcher() {

View File

@@ -13,8 +13,12 @@
#include <filesystem>
#include <iostream>
#include <memory>
#include <sstream>
#include <vector>
#include <winrt/Windows.ApplicationModel.Activation.h>
#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<flutter::FlutterWindowsEngine*>(ref);
}
// Returns a list of discrete arguments splitting the input using a ",".
std::vector<std::string> SplitCommaSeparatedString(const std::string& s) {
std::vector<std::string> 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<flutter::WindowBindingHandler> window_wrapper =
std::make_unique<flutter::FlutterWindowWinUWP>(window);
@@ -39,6 +55,28 @@ FlutterDesktopViewControllerCreateFromCoreWindow(
state->view->SetEngine(
std::unique_ptr<flutter::FlutterWindowsEngine>(EngineFromHandle(engine)));
state->view->CreateRenderSurface();
winrt::Windows::ApplicationModel::Activation::IActivatedEventArgs
arg_interface{nullptr};
winrt::copy_from_abi(arg_interface, args);
std::vector<std::string> engine_switches;
winrt::Windows::ApplicationModel::Activation::LaunchActivatedEventArgs launch{
nullptr};
if (arg_interface.Kind() ==
winrt::Windows::ApplicationModel::Activation::ActivationKind::Launch) {
launch = arg_interface.as<winrt::Windows::ApplicationModel::Activation::
LaunchActivatedEventArgs>();
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;

View File

@@ -14,6 +14,7 @@
#include "flutter_plugin_registrar.h"
#ifdef WINUWP
#include <windows.applicationmodel.activation.h>
#include <windows.ui.core.h>
#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