diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 2160530656..6eaaf9ed09 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1605,6 +1605,10 @@ FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc FILE: ../../../flutter/shell/platform/windows/text_input_plugin.h FILE: ../../../flutter/shell/platform/windows/text_input_plugin_delegate.h FILE: ../../../flutter/shell/platform/windows/text_input_plugin_unittest.cc +FILE: ../../../flutter/shell/platform/windows/uwptool_main.cc +FILE: ../../../flutter/shell/platform/windows/uwptool_utils.cc +FILE: ../../../flutter/shell/platform/windows/uwptool_utils.h +FILE: ../../../flutter/shell/platform/windows/uwptool_utils_unittests.cc FILE: ../../../flutter/shell/platform/windows/window_binding_handler.h FILE: ../../../flutter/shell/platform/windows/window_binding_handler_delegate.h FILE: ../../../flutter/shell/platform/windows/window_proc_delegate_manager_win32.cc diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index 16ef73cc17..e57c6f9926 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -241,6 +241,7 @@ executable("flutter_windows_unittests") { # Target-specific sources. if (target_os == "winuwp") { + sources += [ "uwptool_utils_unittests.cc" ] # TODO(clarkezone) add UWP tests # https://github.com/flutter/flutter/issues/70197 } else { @@ -279,6 +280,10 @@ executable("flutter_windows_unittests") { "//flutter/testing", "//third_party/rapidjson", ] + + if (target_os == "winuwp") { + deps += [ ":uwptool_utils" ] + } } shared_library("flutter_windows_glfw") { @@ -301,6 +306,7 @@ group("windows") { deps = [ ":flutter_windows_winuwp", ":publish_headers_windows", + ":uwptool", "//flutter/shell/platform/windows/client_wrapper:publish_wrapper_windows", ] } else { @@ -315,3 +321,39 @@ group("windows") { deps += [ ":windows_glfw" ] } } + +# uwptool utilities. +source_set("uwptool_utils") { + if (target_os == "winuwp") { + configs += [ ":cppwinrt_defs" ] + cflags = [ "/EHsc" ] + } + + sources = [ + "uwptool_utils.cc", + "uwptool_utils.h", + ] + + deps = [ ":registry" ] +} + +# Command-line tool used by the flutter tool that serves a similar purpose to +# adb for Android or libimobiledevice for iOS apps. Supports installing, +# uninstalling, launching apps. +executable("uwptool") { + if (target_os == "winuwp") { + configs += [ ":cppwinrt_defs" ] + cflags = [ "/EHsc" ] + libs = [ + "windowsapp.lib", + "user32.lib", + ] + } + + sources = [ "uwptool_main.cc" ] + deps = [ + ":string_conversion", + ":uwptool_utils", + "//flutter/fml:command_line", + ] +} diff --git a/engine/src/flutter/shell/platform/windows/registry.cc b/engine/src/flutter/shell/platform/windows/registry.cc index e9cae57120..cb6ddc6cc8 100644 --- a/engine/src/flutter/shell/platform/windows/registry.cc +++ b/engine/src/flutter/shell/platform/windows/registry.cc @@ -54,8 +54,8 @@ std::vector RegistryKey::GetSubKeyNames() const { for (int i = 0; i < subkey_count; ++i) { DWORD key_buf_size = max_key_buf_size; auto key_buf = std::make_unique(max_key_buf_size); - result = ::RegEnumKeyEx(key_, i, key_buf.get(), &key_buf_size, nullptr, - nullptr, nullptr, nullptr); + result = ::RegEnumKeyExW(key_, i, key_buf.get(), &key_buf_size, nullptr, + nullptr, nullptr, nullptr); if (result == ERROR_SUCCESS) { subkey_names.emplace_back(key_buf.get()); } diff --git a/engine/src/flutter/shell/platform/windows/uwptool_main.cc b/engine/src/flutter/shell/platform/windows/uwptool_main.cc new file mode 100644 index 0000000000..45f01ec79b --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/uwptool_main.cc @@ -0,0 +1,92 @@ +// 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 +#include + +#include +#include +#include +#include +#include + +#include "flutter/fml/command_line.h" +#include "flutter/shell/platform/windows/string_conversion.h" +#include "flutter/shell/platform/windows/uwptool_utils.h" + +namespace { + +// Prints a list of installed UWP apps to stdout. +void PrintInstalledApps() { + flutter::ApplicationStore app_store; + for (const flutter::Application& app : app_store.GetInstalledApplications()) { + std::wcout << app.GetPackageId() << std::endl; + } +} + +// Launches the app installed on the system whose Application User Model ID is +// prefixed with app_id, with the specified arguments list. +// +// Returns -1 if no matching app, or multiple matching apps are found, or if +// the app fails to launch. Otherwise, the process ID of the launched app is +// returned. +int LaunchApp(const std::wstring_view app_id, const std::wstring_view args) { + flutter::Application app(app_id); + DWORD process_id = app.Launch(args); + if (process_id == -1) { + std::wcerr << L"Failed to launch app " << app.GetPackageId() << std::endl; + return 1; + } + return 0; +} + +// Prints the command usage to stderr. +void PrintUsage() { + std::cerr << "usage: uwptool COMMAND [APP_ID]" << std::endl; + std::cerr << "commands:" << std::endl; + std::cerr << " listapps list installed applications" << std::endl; + std::cerr << " launch launch an application" << std::endl; +} + +} // namespace + +int main(int argc, char** argv) { + winrt::init_apartment(); + + auto command_line = fml::CommandLineFromArgcArgv(argc, argv); + if (command_line.positional_args().size() < 1) { + PrintUsage(); + return 1; + } + + const std::vector& args = command_line.positional_args(); + std::string command = args[0]; + if (command == "listapps") { + PrintInstalledApps(); + return 0; + } else if (command == "launch") { + if (command_line.positional_args().size() < 1) { + PrintUsage(); + return 1; + } + + // Get the package ID. + std::string package_id = args[1]; + + // Concatenate the remaining args, space-separated. + std::ostringstream app_args; + for (int i = 2; i < args.size(); ++i) { + app_args << args[i]; + if (i < args.size() - 1) { + app_args << " "; + } + } + return LaunchApp(flutter::Utf16FromUtf8(package_id), + flutter::Utf16FromUtf8(app_args.str())); + } + + std::cerr << "Unknown command: " << command << std::endl; + PrintUsage(); + return 1; +} diff --git a/engine/src/flutter/shell/platform/windows/uwptool_utils.cc b/engine/src/flutter/shell/platform/windows/uwptool_utils.cc new file mode 100644 index 0000000000..e63dac7707 --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/uwptool_utils.cc @@ -0,0 +1,60 @@ +// 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/windows/uwptool_utils.h" + +#include +#include +#include +#include + +#include +#include +#include + +namespace flutter { + +Application::Application(const std::wstring_view package_id) + : package_id_(package_id) {} + +int Application::Launch(const std::wstring_view args) { + // Create the ApplicationActivationManager. + winrt::com_ptr activation_manager; + HRESULT hresult = ::CoCreateInstance( + CLSID_ApplicationActivationManager, nullptr, CLSCTX_INPROC_SERVER, + IID_IApplicationActivationManager, activation_manager.put_void()); + if (FAILED(hresult)) { + return -1; + } + + // Launch the application. + DWORD process_id; + ACTIVATEOPTIONS options = AO_NONE; + std::wstring app_user_model_id = package_id_ + L"!App"; + hresult = activation_manager->ActivateApplication( + app_user_model_id.data(), args.data(), options, &process_id); + if (FAILED(hresult)) { + return -1; + } + return process_id; +} + +std::vector ApplicationStore::GetInstalledApplications() { + constexpr wchar_t kMappingsKey[] = + L"\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion" + L"\\AppModel\\Repository\\Families"; + RegistryKey mappings_key(HKEY_CLASSES_ROOT, kMappingsKey, KEY_READ); + if (!mappings_key.IsValid()) { + return {}; + } + + std::unordered_set package_ids; + for (const std::wstring& subkey_name : mappings_key.GetSubKeyNames()) { + package_ids.emplace(subkey_name); + } + std::vector apps(package_ids.begin(), package_ids.end()); + return apps; +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/uwptool_utils.h b/engine/src/flutter/shell/platform/windows/uwptool_utils.h new file mode 100644 index 0000000000..8e44ee7388 --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/uwptool_utils.h @@ -0,0 +1,47 @@ +// 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_WINDOWS_UWPTOOL_UTILS_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_UWPTOOL_UTILS_H_ + +#include +#include + +#include "flutter/shell/platform/windows/registry.h" + +namespace flutter { + +// A UWP application. +class Application { + public: + explicit Application(const std::wstring_view package_id); + Application(const Application& other) = default; + Application& operator=(const Application& other) = default; + + // Returns the application user model ID. + std::wstring GetPackageId() const { return package_id_; } + + // Launches the application with the specified list of launch arguments. + int Launch(const std::wstring_view args); + + private: + std::wstring package_id_; +}; + +// The machine-local store of installed applications. +class ApplicationStore { + public: + ApplicationStore() = default; + + // Prevent copying. + ApplicationStore(const ApplicationStore& other) = delete; + ApplicationStore& operator=(const ApplicationStore& other) = delete; + + // Returns a list of all installed application user model IDs. + std::vector GetInstalledApplications(); +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_UWPTOOL_UTILS_H_ diff --git a/engine/src/flutter/shell/platform/windows/uwptool_utils_unittests.cc b/engine/src/flutter/shell/platform/windows/uwptool_utils_unittests.cc new file mode 100644 index 0000000000..6ede6b1b35 --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/uwptool_utils_unittests.cc @@ -0,0 +1,35 @@ +// 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/windows/uwptool_utils.h" + +#include +#include +#include +#include + +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +// TODO(cbracken): write registry values to be tested, then cleanup, refactor +// to support a mock registry. +// https://github.com/flutter/flutter/issues/82095 + +// Verify that at least one Microsoft app (e.g. Microsoft.WindowsCalculator) is +// installed and can be found. +TEST(ApplicationStore, GetInstalledApplications) { + ApplicationStore app_store; + std::vector apps = app_store.GetInstalledApplications(); + EXPECT_FALSE(apps.empty()); + + auto ms_pos = std::find_if(apps.begin(), apps.end(), [](const auto& app) { + return app.GetPackageId().rfind(L"Microsoft.", 0); + }); + EXPECT_TRUE(ms_pos != apps.end()); +} + +} // namespace testing +} // namespace flutter