Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome_elf/nt_registry/nt_registry.h

Issue 2507263002: Make nt_registry Create/OpenRegKey return a scoped object
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This API is a usability layer for direct registry access via NTDLL. 5 // This API is a usability layer for direct registry access via NTDLL.
6 // It allows for "advapi32-free" registry access, which is especially 6 // It allows for "advapi32-free" registry access, which is especially
7 // useful for accessing registy from DllMain (holding loader lock), 7 // useful for accessing registy from DllMain (holding loader lock),
8 // or if a dependency on/linkage of ADVAPI32.dll is not desired. 8 // or if a dependency on/linkage of ADVAPI32.dll is not desired.
9 9
10 // The implementation of this API should only use ntdll and kernel32 system 10 // The implementation of this API should only use ntdll and kernel32 system
(...skipping 26 matching lines...) Expand all
37 // Use HKLM or HKCU to override. 37 // Use HKLM or HKCU to override.
38 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; 38 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY;
39 39
40 // Used with wrapper functions to request registry redirection override. 40 // Used with wrapper functions to request registry redirection override.
41 // Maps to KEY_WOW64_32KEY and KEY_WOW64_64KEY access flags. 41 // Maps to KEY_WOW64_32KEY and KEY_WOW64_64KEY access flags.
42 enum WOW64_OVERRIDE { 42 enum WOW64_OVERRIDE {
43 NONE = 0L, 43 NONE = 0L,
44 WOW6432 = KEY_WOW64_32KEY, 44 WOW6432 = KEY_WOW64_32KEY,
45 WOW6464 = KEY_WOW64_64KEY 45 WOW6464 = KEY_WOW64_64KEY
46 }; 46 };
47 47
penny 2016/11/21 21:35:48 Please add a small class comment. " // ScopedHAND
scottmg 2016/11/22 22:47:36 Done.
48 class ScopedRegKeyHANDLE {
penny 2016/11/21 21:35:47 Let's make this class generic ScopedHANDLE. Nothi
scottmg 2016/11/22 22:47:36 The INVALID_HANDLE_VALUE vs nullptr was why I didn
49 public:
50 ScopedRegKeyHANDLE();
51 explicit ScopedRegKeyHANDLE(HANDLE handle);
penny 2016/11/21 21:35:47 Just for my own c++ learnin': why did you think t
scottmg 2016/11/22 22:47:37 Yeah, it's for this. Otherwise, any old HANDLE cou
52 ScopedRegKeyHANDLE(ScopedRegKeyHANDLE&& rvalue) : handle_(rvalue.release()) {}
penny 2016/11/21 21:35:47 Why did you have to release and then set in this c
scottmg 2016/11/22 22:47:36 The ones below are =delete, so they're not impleme
53 ScopedRegKeyHANDLE(const ScopedRegKeyHANDLE&) = delete;
penny 2016/11/21 21:35:47 Did you need to add one with "const"? Is that jus
scottmg 2016/11/22 22:47:36 You kind of lost me by this point. But the operato
54 void operator=(ScopedRegKeyHANDLE&) = delete;
55 ~ScopedRegKeyHANDLE();
56
57 HANDLE get() { return handle_; }
58 bool is_valid() const { return handle_ != INVALID_HANDLE_VALUE; }
penny 2016/11/21 21:35:47 I think I'd like is_valid to *also* check for NULL
scottmg 2016/11/22 22:47:36 How about I assert for non-null? I'd prefer to kee
59 HANDLE release();
penny 2016/11/21 21:35:47 Please add tiny comment like: " // Caller is resp
scottmg 2016/11/22 22:47:36 Done. Also made it private for now, since we don't
60
61 private:
62 HANDLE handle_;
63 };
64
48 //------------------------------------------------------------------------------ 65 //------------------------------------------------------------------------------
49 // Create, open, delete, close functions 66 // Create, open, delete, close functions
50 //------------------------------------------------------------------------------ 67 //------------------------------------------------------------------------------
51 68
52 // Create and/or open a registry key. 69 // Create and/or open a registry key.
53 // - This function will recursively create multiple sub-keys if required for 70 // - This function will recursively create multiple sub-keys if required for
54 // |key_path|. 71 // |key_path|.
55 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|.
56 // - This function will happily succeed if the key already exists. 72 // - This function will happily succeed if the key already exists.
57 // - Optional |out_handle|. If nullptr, function will close handle when done. 73 // - Optional |out_handle|. If nullptr, function will close handle when done.
penny 2016/11/21 21:35:47 Change this comment to be: // - On success, the r
scottmg 2016/11/22 22:47:36 Done.
58 // Otherwise, will hold the open handle to the deepest subkey. 74 // Otherwise, will hold the open handle to the deepest subkey.
59 // - Caller must call CloseRegKey on returned handle (on success). 75 ScopedRegKeyHANDLE CreateRegKey(ROOT_KEY root,
60 bool CreateRegKey(ROOT_KEY root, 76 const wchar_t* key_path,
61 const wchar_t* key_path, 77 ACCESS_MASK access);
62 ACCESS_MASK access,
63 HANDLE* out_handle OPTIONAL);
64 78
65 // Open existing registry key. 79 // Open existing registry key.
66 // - Caller must call CloseRegKey on returned handle (on success). 80 // - Optional error code can be returned on failure for extra detail. This will
67 // - Optional error code can be returned on failure for extra detail. 81 // be set if the returned ScopedRegKeyHANDLE.is_valid() == false.
68 bool OpenRegKey(ROOT_KEY root, 82 ScopedRegKeyHANDLE OpenRegKey(ROOT_KEY root,
69 const wchar_t* key_path, 83 const wchar_t* key_path,
70 ACCESS_MASK access, 84 ACCESS_MASK access,
71 HANDLE* out_handle, 85 NTSTATUS* error_code OPTIONAL);
72 NTSTATUS* error_code OPTIONAL);
73 86
74 // Delete a registry key. 87 // Delete a registry key.
75 // - Caller must still call CloseRegKey after the delete. 88 bool DeleteRegKey(HANDLE handle);
penny 2016/11/21 21:35:47 I was fine with the name change, but then I realiz
scottmg 2016/11/22 22:47:36 Oops, sorry, unintentional. I was fiddling a bit m
76 bool DeleteRegKey(HANDLE key);
77 89
78 // Delete a registry key. 90 // Delete a registry key.
79 // - WRAPPER: Function opens and closes the target key for caller. 91 // - WRAPPER: Function opens and closes the target key for caller.
80 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. 92 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE.
81 bool DeleteRegKey(ROOT_KEY root, 93 bool DeleteRegKey(ROOT_KEY root,
82 WOW64_OVERRIDE wow64_override, 94 WOW64_OVERRIDE wow64_override,
83 const wchar_t* key_path); 95 const wchar_t* key_path);
84 96
85 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey.
86 void CloseRegKey(HANDLE key);
87
88 //------------------------------------------------------------------------------ 97 //------------------------------------------------------------------------------
89 // Getter functions 98 // Getter functions
90 //------------------------------------------------------------------------------ 99 //------------------------------------------------------------------------------
91 100
92 // Main function to query a registry value. 101 // Main function to query a registry value.
93 // - Key handle should have been opened with CreateRegKey or OpenRegKey. 102 // - Key handle should have been opened with CreateRegKey or OpenRegKey.
94 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. 103 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ.
95 // - Caller is responsible for calling "delete[] *out_buffer" (on success). 104 // - Caller is responsible for calling "delete[] *out_buffer" (on success).
96 bool QueryRegKeyValue(HANDLE key, 105 bool QueryRegKeyValue(HANDLE key,
97 const wchar_t* value_name, 106 const wchar_t* value_name,
98 ULONG* out_type, 107 ULONG* out_type,
99 BYTE** out_buffer, 108 BYTE** out_buffer,
100 DWORD* out_size); 109 DWORD* out_size);
101 110
102 // Query DWORD value. 111 // Query DWORD value.
103 // - WRAPPER: Function works with DWORD data type. 112 // - WRAPPER: Function works with DWORD data type.
104 // - Key handle should have been opened with CreateRegKey or OpenRegKey. 113 // - Key handle should have been opened with CreateRegKey or OpenRegKey.
105 // - Handle will be left open. Caller must still call CloseRegKey when done.
106 bool QueryRegValueDWORD(HANDLE key, 114 bool QueryRegValueDWORD(HANDLE key,
107 const wchar_t* value_name, 115 const wchar_t* value_name,
108 DWORD* out_dword); 116 DWORD* out_dword);
109 117
110 // Query DWORD value. 118 // Query DWORD value.
111 // - WRAPPER: Function opens and closes the target key for caller, and works 119 // - WRAPPER: Function opens and closes the target key for caller, and works
112 // with DWORD data type. 120 // with DWORD data type.
113 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. 121 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE.
114 bool QueryRegValueDWORD(ROOT_KEY root, 122 bool QueryRegValueDWORD(ROOT_KEY root,
115 WOW64_OVERRIDE wow64_override, 123 WOW64_OVERRIDE wow64_override,
116 const wchar_t* key_path, 124 const wchar_t* key_path,
117 const wchar_t* value_name, 125 const wchar_t* value_name,
118 DWORD* out_dword); 126 DWORD* out_dword);
119 127
120 // Query SZ (string) value. 128 // Query SZ (string) value.
121 // - WRAPPER: Function works with SZ data type. 129 // - WRAPPER: Function works with SZ data type.
122 // - Key handle should have been opened with CreateRegKey or OpenRegKey. 130 // - Key handle should have been opened with CreateRegKey or OpenRegKey.
123 // - Handle will be left open. Caller must still call CloseRegKey when done.
124 bool QueryRegValueSZ(HANDLE key, 131 bool QueryRegValueSZ(HANDLE key,
125 const wchar_t* value_name, 132 const wchar_t* value_name,
126 std::wstring* out_sz); 133 std::wstring* out_sz);
127 134
128 // Query SZ (string) value. 135 // Query SZ (string) value.
129 // - WRAPPER: Function opens and closes the target key for caller, and works 136 // - WRAPPER: Function opens and closes the target key for caller, and works
130 // with SZ data type. 137 // with SZ data type.
131 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. 138 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE.
132 bool QueryRegValueSZ(ROOT_KEY root, 139 bool QueryRegValueSZ(ROOT_KEY root,
133 WOW64_OVERRIDE wow64_override, 140 WOW64_OVERRIDE wow64_override,
134 const wchar_t* key_path, 141 const wchar_t* key_path,
135 const wchar_t* value_name, 142 const wchar_t* value_name,
136 std::wstring* out_sz); 143 std::wstring* out_sz);
137 144
138 // Query MULTI_SZ (multiple strings) value. 145 // Query MULTI_SZ (multiple strings) value.
139 // - WRAPPER: Function works with MULTI_SZ data type. 146 // - WRAPPER: Function works with MULTI_SZ data type.
140 // - Key handle should have been opened with CreateRegKey or OpenRegKey. 147 // - Key handle should have been opened with CreateRegKey or OpenRegKey.
141 // - Handle will be left open. Caller must still call CloseRegKey when done.
142 bool QueryRegValueMULTISZ(HANDLE key, 148 bool QueryRegValueMULTISZ(HANDLE key,
143 const wchar_t* value_name, 149 const wchar_t* value_name,
144 std::vector<std::wstring>* out_multi_sz); 150 std::vector<std::wstring>* out_multi_sz);
145 151
146 // Query MULTI_SZ (multiple strings) value. 152 // Query MULTI_SZ (multiple strings) value.
147 // - WRAPPER: Function opens and closes the target key for caller, and works 153 // - WRAPPER: Function opens and closes the target key for caller, and works
148 // with MULTI_SZ data type. 154 // with MULTI_SZ data type.
149 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. 155 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE.
150 bool QueryRegValueMULTISZ(ROOT_KEY root, 156 bool QueryRegValueMULTISZ(ROOT_KEY root,
151 WOW64_OVERRIDE wow64_override, 157 WOW64_OVERRIDE wow64_override,
(...skipping 10 matching lines...) Expand all
162 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. 168 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ.
163 bool SetRegKeyValue(HANDLE key, 169 bool SetRegKeyValue(HANDLE key,
164 const wchar_t* value_name, 170 const wchar_t* value_name,
165 ULONG type, 171 ULONG type,
166 const BYTE* data, 172 const BYTE* data,
167 DWORD data_size); 173 DWORD data_size);
168 174
169 // Set DWORD value. 175 // Set DWORD value.
170 // - WRAPPER: Function works with DWORD data type. 176 // - WRAPPER: Function works with DWORD data type.
171 // - Key handle should have been opened with CreateRegKey or OpenRegKey. 177 // - Key handle should have been opened with CreateRegKey or OpenRegKey.
172 // - Handle will be left open. Caller must still call CloseRegKey when done.
173 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); 178 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value);
174 179
175 // Set DWORD value. 180 // Set DWORD value.
176 // - WRAPPER: Function opens and closes the target key for caller, and works 181 // - WRAPPER: Function opens and closes the target key for caller, and works
177 // with DWORD data type. 182 // with DWORD data type.
178 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. 183 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE.
179 bool SetRegValueDWORD(ROOT_KEY root, 184 bool SetRegValueDWORD(ROOT_KEY root,
180 WOW64_OVERRIDE wow64_override, 185 WOW64_OVERRIDE wow64_override,
181 const wchar_t* key_path, 186 const wchar_t* key_path,
182 const wchar_t* value_name, 187 const wchar_t* value_name,
183 DWORD value); 188 DWORD value);
184 189
185 // Set SZ (string) value. 190 // Set SZ (string) value.
186 // - WRAPPER: Function works with SZ data type. 191 // - WRAPPER: Function works with SZ data type.
187 // - Key handle should have been opened with CreateRegKey or OpenRegKey. 192 // - Key handle should have been opened with CreateRegKey or OpenRegKey.
188 // - Handle will be left open. Caller must still call CloseRegKey when done.
189 bool SetRegValueSZ(HANDLE key, 193 bool SetRegValueSZ(HANDLE key,
190 const wchar_t* value_name, 194 const wchar_t* value_name,
191 const std::wstring& value); 195 const std::wstring& value);
192 196
193 // Set SZ (string) value. 197 // Set SZ (string) value.
194 // - WRAPPER: Function opens and closes the target key for caller, and works 198 // - WRAPPER: Function opens and closes the target key for caller, and works
195 // with SZ data type. 199 // with SZ data type.
196 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. 200 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE.
197 bool SetRegValueSZ(ROOT_KEY root, 201 bool SetRegValueSZ(ROOT_KEY root,
198 WOW64_OVERRIDE wow64_override, 202 WOW64_OVERRIDE wow64_override,
199 const wchar_t* key_path, 203 const wchar_t* key_path,
200 const wchar_t* value_name, 204 const wchar_t* value_name,
201 const std::wstring& value); 205 const std::wstring& value);
202 206
203 // Set MULTI_SZ (multiple strings) value. 207 // Set MULTI_SZ (multiple strings) value.
204 // - WRAPPER: Function works with MULTI_SZ data type. 208 // - WRAPPER: Function works with MULTI_SZ data type.
205 // - Key handle should have been opened with CreateRegKey or OpenRegKey. 209 // - Key handle should have been opened with CreateRegKey or OpenRegKey.
206 // - Handle will be left open. Caller must still call CloseRegKey when done.
207 bool SetRegValueMULTISZ(HANDLE key, 210 bool SetRegValueMULTISZ(HANDLE key,
208 const wchar_t* value_name, 211 const wchar_t* value_name,
209 const std::vector<std::wstring>& values); 212 const std::vector<std::wstring>& values);
210 213
211 // Set MULTI_SZ (multiple strings) value. 214 // Set MULTI_SZ (multiple strings) value.
212 // - WRAPPER: Function opens and closes the target key for caller, and works 215 // - WRAPPER: Function opens and closes the target key for caller, and works
213 // with MULTI_SZ data type. 216 // with MULTI_SZ data type.
214 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. 217 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE.
215 bool SetRegValueMULTISZ(ROOT_KEY root, 218 bool SetRegValueMULTISZ(ROOT_KEY root,
216 WOW64_OVERRIDE wow64_override, 219 WOW64_OVERRIDE wow64_override,
(...skipping 13 matching lines...) Expand all
230 233
231 // Setter function for test suites that use reg redirection. 234 // Setter function for test suites that use reg redirection.
232 bool SetTestingOverride(ROOT_KEY root, const std::wstring& new_path); 235 bool SetTestingOverride(ROOT_KEY root, const std::wstring& new_path);
233 236
234 // Getter function for test suites that use reg redirection. 237 // Getter function for test suites that use reg redirection.
235 std::wstring GetTestingOverride(ROOT_KEY root); 238 std::wstring GetTestingOverride(ROOT_KEY root);
236 239
237 }; // namespace nt 240 }; // namespace nt
238 241
239 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ 242 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698