Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0d1d28a

Browse files
committed
Support windows registry access
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. This adds a RegistryKey class that does automatic resource management for registry keys and provides convenience functions for accessing keys and navigating the key hierarchy. This is library code to be used in uwptool, which adds an adb-like helper tool for install, uninstall, and launch of UWP applications for Windows platforms. See: flutter/flutter#81756
1 parent 4aace54 commit 0d1d28a

File tree

5 files changed

+215
-1
lines changed

5 files changed

+215
-1
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,9 @@ FILE: ../../../flutter/shell/platform/windows/platform_handler_win32.h
15801580
FILE: ../../../flutter/shell/platform/windows/platform_handler_winuwp.cc
15811581
FILE: ../../../flutter/shell/platform/windows/platform_handler_winuwp.h
15821582
FILE: ../../../flutter/shell/platform/windows/public/flutter_windows.h
1583+
FILE: ../../../flutter/shell/platform/windows/registry.cc
1584+
FILE: ../../../flutter/shell/platform/windows/registry.h
1585+
FILE: ../../../flutter/shell/platform/windows/registry_unittests.cc
15831586
FILE: ../../../flutter/shell/platform/windows/string_conversion.cc
15841587
FILE: ../../../flutter/shell/platform/windows/string_conversion.h
15851588
FILE: ../../../flutter/shell/platform/windows/string_conversion_unittests.cc

shell/platform/windows/BUILD.gn

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ source_set("flutter_windows_source") {
171171
]
172172
}
173173

174+
# Windows registry utilities.
175+
source_set("registry") {
176+
sources = [
177+
"registry.cc",
178+
"registry.h",
179+
]
180+
181+
if (target_os == "winuwp") {
182+
cflags = [ "/EHsc" ]
183+
}
184+
}
185+
174186
# String encoding conversion utilities.
175187
source_set("string_conversion") {
176188
sources = [
@@ -179,7 +191,6 @@ source_set("string_conversion") {
179191
]
180192

181193
if (target_os == "winuwp") {
182-
configs += [ ":cppwinrt_defs" ]
183194
cflags = [ "/EHsc" ]
184195
}
185196
}
@@ -222,6 +233,7 @@ executable("flutter_windows_unittests") {
222233
sources = [
223234
# "flutter_project_bundle_unittests.cc", //TODO failing due to switches test failing. Blocked on https://github.com/flutter/flutter/issues/74153
224235
# "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
236+
"registry_unittests.cc",
225237
"string_conversion_unittests.cc",
226238
"system_utils_unittests.cc",
227239
"testing/engine_modifier.h",
@@ -260,6 +272,7 @@ executable("flutter_windows_unittests") {
260272
":flutter_windows_fixtures",
261273
":flutter_windows_headers",
262274
":flutter_windows_source",
275+
":registry",
263276
"//flutter/shell/platform/common:common_cpp",
264277
"//flutter/shell/platform/embedder:embedder_as_internal_library",
265278
"//flutter/shell/platform/embedder:embedder_test_utils",

shell/platform/windows/registry.cc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/windows/registry.h"
6+
7+
#include <cassert>
8+
#include <memory>
9+
10+
namespace flutter {
11+
12+
RegistryKey::RegistryKey(HKEY key, REGSAM access)
13+
: RegistryKey(key, L"", access) {}
14+
15+
RegistryKey::RegistryKey(HKEY parent_key,
16+
const std::wstring_view subkey,
17+
REGSAM access) {
18+
LSTATUS result = ::RegOpenKeyEx(parent_key, subkey.data(), 0, access, &key_);
19+
if (result != ERROR_SUCCESS) {
20+
key_ = nullptr;
21+
}
22+
}
23+
24+
RegistryKey::RegistryKey(const RegistryKey& parent_key,
25+
const std::wstring_view subkey,
26+
REGSAM access)
27+
: RegistryKey(parent_key.key_, subkey, access) {}
28+
29+
RegistryKey::~RegistryKey() {
30+
Close();
31+
}
32+
33+
void RegistryKey::Close() {
34+
if (IsValid()) {
35+
::RegCloseKey(key_);
36+
key_ = nullptr;
37+
}
38+
}
39+
40+
std::vector<std::wstring> RegistryKey::GetSubKeyNames() const {
41+
if (!IsValid()) {
42+
return {};
43+
}
44+
45+
// Get the count of subkeys, and maximum key size in wchar_t.
46+
DWORD max_key_buf_size;
47+
DWORD subkey_count;
48+
LSTATUS result = ::RegQueryInfoKey(
49+
key_, nullptr, nullptr, nullptr, &subkey_count, &max_key_buf_size,
50+
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
51+
52+
// Collect all subkey names.
53+
std::vector<std::wstring> subkey_names;
54+
for (int i = 0; i < subkey_count; ++i) {
55+
DWORD key_buf_size = max_key_buf_size;
56+
auto key_buf = std::make_unique<wchar_t[]>(max_key_buf_size);
57+
result = ::RegEnumKeyEx(key_, i, key_buf.get(), &key_buf_size, nullptr,
58+
nullptr, nullptr, nullptr);
59+
if (result == ERROR_SUCCESS) {
60+
subkey_names.emplace_back(key_buf.get());
61+
}
62+
}
63+
return subkey_names;
64+
}
65+
66+
LONG RegistryKey::ReadValue(const std::wstring_view name,
67+
std::wstring* out_value) const {
68+
assert(out_value != nullptr);
69+
70+
// Get the value size, in bytes.
71+
DWORD value_size;
72+
LSTATUS result = ::RegGetValueW(key_, L"", name.data(), RRF_RT_REG_SZ,
73+
nullptr, nullptr, &value_size);
74+
if (result != ERROR_SUCCESS) {
75+
return result;
76+
}
77+
78+
auto value_buf = std::make_unique<wchar_t[]>(value_size / sizeof(wchar_t));
79+
result = ::RegGetValueW(key_, L"", name.data(), RRF_RT_REG_SZ, nullptr,
80+
value_buf.get(), &value_size);
81+
if (result == ERROR_SUCCESS) {
82+
*out_value = value_buf.get();
83+
}
84+
return result;
85+
}
86+
87+
} // namespace flutter

shell/platform/windows/registry.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_REGISTRY_H_
6+
#define FLUTTER_SHELL_PLATFORM_WINDOWS_REGISTRY_H_
7+
8+
#include <Windows.h>
9+
#include <Winreg.h>
10+
11+
#include <string>
12+
#include <vector>
13+
14+
namespace flutter {
15+
16+
// A Windows Registry key.
17+
//
18+
// The Windows registry is structured as a hierarchy of named keys, each of
19+
// which may contain a set of named values of various datatypes. RegistryKey
20+
// objects own the underlying HKEY handle and ensure cleanup at or prior to
21+
// destruction.
22+
class RegistryKey {
23+
public:
24+
// Opens the specified key.
25+
RegistryKey(HKEY key, REGSAM access);
26+
27+
// Opens a key relative to the specified parent key.
28+
RegistryKey(HKEY parent_key, const std::wstring_view subkey, REGSAM access);
29+
30+
// Opens a key relative to the specified parent key.
31+
RegistryKey(const RegistryKey& parent_key,
32+
const std::wstring_view subkey,
33+
REGSAM access);
34+
35+
~RegistryKey();
36+
37+
// Prevent copying.
38+
RegistryKey(const RegistryKey& other) = delete;
39+
RegistryKey& operator=(const RegistryKey& other) = delete;
40+
41+
// Closes the registry key and releases resources.
42+
void Close();
43+
44+
// Returns true if the key is valid.
45+
bool IsValid() const { return key_ != nullptr; }
46+
47+
// Returns a list of all direct subkey names for this key.
48+
std::vector<std::wstring> GetSubKeyNames() const;
49+
50+
// Reads a string value from the key.
51+
//
52+
// Returns ERROR_SUCCESS on success.
53+
LONG ReadValue(const std::wstring_view name, std::wstring* out_value) const;
54+
55+
private:
56+
HKEY key_ = nullptr;
57+
};
58+
59+
} // namespace flutter
60+
61+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_REGISTRY_H_
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/windows/registry.h"
6+
7+
#include <Windows.h>
8+
#include <Winreg.h>
9+
10+
#include <vector>
11+
12+
#include "gtest/gtest.h"
13+
14+
namespace flutter {
15+
namespace testing {
16+
17+
// TODO(cbracken): write registry values to be tested, then cleanup.
18+
// https://github.com/flutter/flutter/issues/82095
19+
20+
// Verify that a registry key is marked invalid after close.
21+
TEST(RegistryKey, CloseInvalidates) {
22+
RegistryKey key(HKEY_USERS, L".DEFAULT\\Environment", KEY_READ);
23+
ASSERT_TRUE(key.IsValid());
24+
key.Close();
25+
ASSERT_FALSE(key.IsValid());
26+
}
27+
28+
// Verify that subkeys can be read.
29+
TEST(RegistryKey, GetSubKeyNames) {
30+
RegistryKey key(HKEY_USERS, L".DEFAULT", KEY_READ);
31+
ASSERT_TRUE(key.IsValid());
32+
33+
std::vector<std::wstring> subkey_names = key.GetSubKeyNames();
34+
EXPECT_GE(subkey_names.size(), 1);
35+
EXPECT_TRUE(std::find(subkey_names.begin(), subkey_names.end(),
36+
L"Environment") != subkey_names.end());
37+
}
38+
39+
// Verify that values can be read.
40+
TEST(RegistryKey, GetValue) {
41+
RegistryKey key(HKEY_USERS, L".DEFAULT\\Environment", KEY_READ);
42+
ASSERT_TRUE(key.IsValid());
43+
44+
std::wstring path;
45+
ASSERT_EQ(key.ReadValue(L"Path", &path), ERROR_SUCCESS);
46+
EXPECT_FALSE(path.empty());
47+
}
48+
49+
} // namespace testing
50+
} // namespace flutter

0 commit comments

Comments
 (0)