Use PackageManager to locate installed UWP apps (flutter/engine#26120)

Replaces manual registry crawling with a lookup via
Windows.Management.Deployment.PackageManager. This eliminates the need
for the Registry class.

Covered by existing tests.

Issue: https://github.com/flutter/flutter/issues/81756
This commit is contained in:
Chris Bracken
2021-05-13 08:03:03 -07:00
committed by GitHub
parent 7641bf1f5b
commit 25714c4696
8 changed files with 11 additions and 230 deletions

View File

@@ -1583,9 +1583,6 @@ FILE: ../../../flutter/shell/platform/windows/platform_handler_win32.h
FILE: ../../../flutter/shell/platform/windows/platform_handler_winuwp.cc
FILE: ../../../flutter/shell/platform/windows/platform_handler_winuwp.h
FILE: ../../../flutter/shell/platform/windows/public/flutter_windows.h
FILE: ../../../flutter/shell/platform/windows/registry.cc
FILE: ../../../flutter/shell/platform/windows/registry.h
FILE: ../../../flutter/shell/platform/windows/registry_unittests.cc
FILE: ../../../flutter/shell/platform/windows/string_conversion.cc
FILE: ../../../flutter/shell/platform/windows/string_conversion.h
FILE: ../../../flutter/shell/platform/windows/string_conversion_unittests.cc

View File

@@ -171,18 +171,6 @@ source_set("flutter_windows_source") {
]
}
# Windows registry utilities.
source_set("registry") {
sources = [
"registry.cc",
"registry.h",
]
if (target_os == "winuwp") {
cflags = [ "/EHsc" ]
}
}
# String encoding conversion utilities.
source_set("string_conversion") {
sources = [
@@ -233,7 +221,6 @@ executable("flutter_windows_unittests") {
sources = [
# "flutter_project_bundle_unittests.cc", //TODO failing due to switches test failing. Blocked on https://github.com/flutter/flutter/issues/74153
# "flutter_windows_engine_unittests.cc", //TODO failing to send / receive platform message get plugins working first. Blocked on https://github.com/flutter/flutter/issues/74155
"registry_unittests.cc",
"string_conversion_unittests.cc",
"system_utils_unittests.cc",
"testing/engine_modifier.h",
@@ -273,7 +260,6 @@ executable("flutter_windows_unittests") {
":flutter_windows_fixtures",
":flutter_windows_headers",
":flutter_windows_source",
":registry",
"//flutter/shell/platform/common:common_cpp",
"//flutter/shell/platform/embedder:embedder_as_internal_library",
"//flutter/shell/platform/embedder:embedder_test_utils",
@@ -333,8 +319,6 @@ source_set("uwptool_utils") {
"uwptool_utils.cc",
"uwptool_utils.h",
]
deps = [ ":registry" ]
}
# Command-line tool used by the flutter tool that serves a similar purpose to

View File

@@ -1,87 +0,0 @@
// 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/registry.h"
#include <cassert>
#include <memory>
namespace flutter {
RegistryKey::RegistryKey(HKEY key, REGSAM access)
: RegistryKey(key, L"", access) {}
RegistryKey::RegistryKey(HKEY parent_key,
const std::wstring_view subkey,
REGSAM access) {
LSTATUS result = ::RegOpenKeyEx(parent_key, subkey.data(), 0, access, &key_);
if (result != ERROR_SUCCESS) {
key_ = nullptr;
}
}
RegistryKey::RegistryKey(const RegistryKey& parent_key,
const std::wstring_view subkey,
REGSAM access)
: RegistryKey(parent_key.key_, subkey, access) {}
RegistryKey::~RegistryKey() {
Close();
}
void RegistryKey::Close() {
if (IsValid()) {
::RegCloseKey(key_);
key_ = nullptr;
}
}
std::vector<std::wstring> RegistryKey::GetSubKeyNames() const {
if (!IsValid()) {
return {};
}
// Get the count of subkeys, and maximum key size in wchar_t.
DWORD max_key_buf_size;
DWORD subkey_count;
LSTATUS result = ::RegQueryInfoKey(
key_, nullptr, nullptr, nullptr, &subkey_count, &max_key_buf_size,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
// Collect all subkey names.
std::vector<std::wstring> subkey_names;
for (int i = 0; i < subkey_count; ++i) {
DWORD key_buf_size = max_key_buf_size;
auto key_buf = std::make_unique<wchar_t[]>(max_key_buf_size);
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());
}
}
return subkey_names;
}
LONG RegistryKey::ReadValue(const std::wstring_view name,
std::wstring* out_value) const {
assert(out_value != nullptr);
// Get the value size, in bytes.
DWORD value_size;
LSTATUS result = ::RegGetValueW(key_, L"", name.data(), RRF_RT_REG_SZ,
nullptr, nullptr, &value_size);
if (result != ERROR_SUCCESS) {
return result;
}
auto value_buf = std::make_unique<wchar_t[]>(value_size / sizeof(wchar_t));
result = ::RegGetValueW(key_, L"", name.data(), RRF_RT_REG_SZ, nullptr,
value_buf.get(), &value_size);
if (result == ERROR_SUCCESS) {
*out_value = value_buf.get();
}
return result;
}
} // namespace flutter

View File

@@ -1,61 +0,0 @@
// 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_REGISTRY_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_REGISTRY_H_
#include <Windows.h>
#include <Winreg.h>
#include <string>
#include <vector>
namespace flutter {
// A Windows Registry key.
//
// The Windows registry is structured as a hierarchy of named keys, each of
// which may contain a set of named values of various datatypes. RegistryKey
// objects own the underlying HKEY handle and ensure cleanup at or prior to
// destruction.
class RegistryKey {
public:
// Opens the specified key.
RegistryKey(HKEY key, REGSAM access);
// Opens a key relative to the specified parent key.
RegistryKey(HKEY parent_key, const std::wstring_view subkey, REGSAM access);
// Opens a key relative to the specified parent key.
RegistryKey(const RegistryKey& parent_key,
const std::wstring_view subkey,
REGSAM access);
~RegistryKey();
// Prevent copying.
RegistryKey(const RegistryKey& other) = delete;
RegistryKey& operator=(const RegistryKey& other) = delete;
// Closes the registry key and releases resources.
void Close();
// Returns true if the key is valid.
bool IsValid() const { return key_ != nullptr; }
// Returns a list of all direct subkey names for this key.
std::vector<std::wstring> GetSubKeyNames() const;
// Reads a string value from the key.
//
// Returns ERROR_SUCCESS on success.
LONG ReadValue(const std::wstring_view name, std::wstring* out_value) const;
private:
HKEY key_ = nullptr;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_REGISTRY_H_

View File

@@ -1,50 +0,0 @@
// 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/registry.h"
#include <Windows.h>
#include <Winreg.h>
#include <vector>
#include "gtest/gtest.h"
namespace flutter {
namespace testing {
// TODO(cbracken): write registry values to be tested, then cleanup.
// https://github.com/flutter/flutter/issues/82095
// Verify that a registry key is marked invalid after close.
TEST(RegistryKey, CloseInvalidates) {
RegistryKey key(HKEY_USERS, L".DEFAULT\\Environment", KEY_READ);
ASSERT_TRUE(key.IsValid());
key.Close();
ASSERT_FALSE(key.IsValid());
}
// Verify that subkeys can be read.
TEST(RegistryKey, GetSubKeyNames) {
RegistryKey key(HKEY_USERS, L".DEFAULT", KEY_READ);
ASSERT_TRUE(key.IsValid());
std::vector<std::wstring> subkey_names = key.GetSubKeyNames();
EXPECT_GE(subkey_names.size(), 1);
EXPECT_TRUE(std::find(subkey_names.begin(), subkey_names.end(),
L"Environment") != subkey_names.end());
}
// Verify that values can be read.
TEST(RegistryKey, GetValue) {
RegistryKey key(HKEY_USERS, L".DEFAULT\\Environment", KEY_READ);
ASSERT_TRUE(key.IsValid());
std::wstring path;
ASSERT_EQ(key.ReadValue(L"Path", &path), ERROR_SUCCESS);
EXPECT_FALSE(path.empty());
}
} // namespace testing
} // namespace flutter

View File

@@ -86,7 +86,7 @@ int main(int argc, char** argv) {
}
// Write an informative message for the user to stderr.
std::cerr << "Launched app with package_id " << package_id
std::cerr << "Launched app with package ID " << package_id
<< ". PID: " << std::endl;
// Write the PID to stdout. The flutter tool reads this value in.
std::cout << process_id << std::endl;

View File

@@ -7,6 +7,9 @@
#include <Windows.h>
#include <Winreg.h>
#include <shobjidl_core.h>
#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Management.Deployment.h>
#include <winrt/base.h>
#include <string>
@@ -41,17 +44,14 @@ int Application::Launch(const std::wstring_view args) {
}
std::vector<Application> 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 {};
}
using winrt::Windows::ApplicationModel::Package;
using winrt::Windows::Management::Deployment::PackageManager;
// Find packages for the current user (default for empty string).
PackageManager package_manager;
std::unordered_set<std::wstring> package_ids;
for (const std::wstring& subkey_name : mappings_key.GetSubKeyNames()) {
package_ids.emplace(subkey_name);
for (const Package& package : package_manager.FindPackagesForUser(L"")) {
package_ids.emplace(package.Id().FamilyName().c_str());
}
std::vector<Application> apps(package_ids.begin(), package_ids.end());
return apps;

View File

@@ -8,8 +8,6 @@
#include <string>
#include <vector>
#include "flutter/shell/platform/windows/registry.h"
namespace flutter {
// A UWP application.
@@ -19,7 +17,7 @@ class Application {
Application(const Application& other) = default;
Application& operator=(const Application& other) = default;
// Returns the application user model ID.
// Returns the package ID.
std::wstring GetPackageId() const { return package_id_; }
// Launches the application with the specified list of launch arguments.