henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Registry configuration wrappers class |
| 12 | // |
| 13 | // Offers static functions for convenient |
| 14 | // fast access for individual values |
| 15 | // |
| 16 | // Also provides a wrapper class for efficient |
| 17 | // batch operations on values of a given registry key. |
| 18 | // |
| 19 | |
| 20 | #ifndef WEBRTC_BASE_WIN32REGKEY_H_ |
| 21 | #define WEBRTC_BASE_WIN32REGKEY_H_ |
| 22 | |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "webrtc/base/basictypes.h" |
henrikg | 71df77b | 2015-09-18 01:48:34 -0700 | [diff] [blame] | 27 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | #include "webrtc/base/win32.h" |
| 29 | |
| 30 | namespace rtc { |
| 31 | |
| 32 | // maximum sizes registry key and value names |
| 33 | const int kMaxKeyNameChars = 255 + 1; |
| 34 | const int kMaxValueNameChars = 16383 + 1; |
| 35 | |
| 36 | class RegKey { |
| 37 | public: |
| 38 | // constructor |
| 39 | RegKey(); |
| 40 | |
| 41 | // destructor |
| 42 | ~RegKey(); |
| 43 | |
| 44 | // create a reg key |
| 45 | HRESULT Create(HKEY parent_key, const wchar_t* key_name); |
| 46 | |
| 47 | HRESULT Create(HKEY parent_key, |
| 48 | const wchar_t* key_name, |
| 49 | wchar_t* reg_class, |
| 50 | DWORD options, |
| 51 | REGSAM sam_desired, |
| 52 | LPSECURITY_ATTRIBUTES lp_sec_attr, |
| 53 | LPDWORD lp_disposition); |
| 54 | |
| 55 | // open an existing reg key |
| 56 | HRESULT Open(HKEY parent_key, const wchar_t* key_name); |
| 57 | |
| 58 | HRESULT Open(HKEY parent_key, const wchar_t* key_name, REGSAM sam_desired); |
| 59 | |
| 60 | // close this reg key |
| 61 | HRESULT Close(); |
| 62 | |
| 63 | // check if the key has a specified value |
| 64 | bool HasValue(const wchar_t* value_name) const; |
| 65 | |
| 66 | // get the number of values for this key |
| 67 | uint32 GetValueCount(); |
| 68 | |
| 69 | // Called to get the value name for the given value name index |
| 70 | // Use GetValueCount() to get the total value_name count for this key |
| 71 | // Returns failure if no key at the specified index |
| 72 | // If you modify the key while enumerating, the indexes will be out of order. |
| 73 | // Since the index order is not guaranteed, you need to reset your counting |
| 74 | // loop. |
| 75 | // 'type' refers to REG_DWORD, REG_QWORD, etc.. |
| 76 | // 'type' can be NULL if not interested in the value type |
| 77 | HRESULT GetValueNameAt(int index, std::wstring* value_name, DWORD* type); |
| 78 | |
| 79 | // check if the current key has the specified subkey |
| 80 | bool HasSubkey(const wchar_t* key_name) const; |
| 81 | |
| 82 | // get the number of subkeys for this key |
| 83 | uint32 GetSubkeyCount(); |
| 84 | |
| 85 | // Called to get the key name for the given key index |
| 86 | // Use GetSubkeyCount() to get the total count for this key |
| 87 | // Returns failure if no key at the specified index |
| 88 | // If you modify the key while enumerating, the indexes will be out of order. |
| 89 | // Since the index order is not guaranteed, you need to reset your counting |
| 90 | // loop. |
| 91 | HRESULT GetSubkeyNameAt(int index, std::wstring* key_name); |
| 92 | |
| 93 | // SETTERS |
| 94 | |
| 95 | // set an int32 value - use when reading multiple values from a key |
| 96 | HRESULT SetValue(const wchar_t* value_name, DWORD value) const; |
| 97 | |
| 98 | // set an int64 value |
| 99 | HRESULT SetValue(const wchar_t* value_name, DWORD64 value) const; |
| 100 | |
| 101 | // set a string value |
| 102 | HRESULT SetValue(const wchar_t* value_name, const wchar_t* value) const; |
| 103 | |
| 104 | // set binary data |
| 105 | HRESULT SetValue(const wchar_t* value_name, |
| 106 | const uint8* value, |
| 107 | DWORD byte_count) const; |
| 108 | |
| 109 | // set raw data, including type |
| 110 | HRESULT SetValue(const wchar_t* value_name, |
| 111 | const uint8* value, |
| 112 | DWORD byte_count, |
| 113 | DWORD type) const; |
| 114 | |
| 115 | // GETTERS |
| 116 | |
| 117 | // get an int32 value |
| 118 | HRESULT GetValue(const wchar_t* value_name, DWORD* value) const; |
| 119 | |
| 120 | // get an int64 value |
| 121 | HRESULT GetValue(const wchar_t* value_name, DWORD64* value) const; |
| 122 | |
| 123 | // get a string value - the caller must free the return buffer |
| 124 | HRESULT GetValue(const wchar_t* value_name, wchar_t** value) const; |
| 125 | |
| 126 | // get a string value |
| 127 | HRESULT GetValue(const wchar_t* value_name, std::wstring* value) const; |
| 128 | |
| 129 | // get a std::vector<std::wstring> value from REG_MULTI_SZ type |
| 130 | HRESULT GetValue(const wchar_t* value_name, |
| 131 | std::vector<std::wstring>* value) const; |
| 132 | |
| 133 | // get binary data - the caller must free the return buffer |
| 134 | HRESULT GetValue(const wchar_t* value_name, |
| 135 | uint8** value, |
| 136 | DWORD* byte_count) const; |
| 137 | |
| 138 | // get raw data, including type - the caller must free the return buffer |
| 139 | HRESULT GetValue(const wchar_t* value_name, |
| 140 | uint8** value, |
| 141 | DWORD* byte_count, |
| 142 | DWORD* type) const; |
| 143 | |
| 144 | // STATIC VERSIONS |
| 145 | |
| 146 | // flush |
| 147 | static HRESULT FlushKey(const wchar_t* full_key_name); |
| 148 | |
| 149 | // check if a key exists |
| 150 | static bool HasKey(const wchar_t* full_key_name); |
| 151 | |
| 152 | // check if the key has a specified value |
| 153 | static bool HasValue(const wchar_t* full_key_name, const wchar_t* value_name); |
| 154 | |
| 155 | // SETTERS |
| 156 | |
| 157 | // STATIC int32 set |
| 158 | static HRESULT SetValue(const wchar_t* full_key_name, |
| 159 | const wchar_t* value_name, |
| 160 | DWORD value); |
| 161 | |
| 162 | // STATIC int64 set |
| 163 | static HRESULT SetValue(const wchar_t* full_key_name, |
| 164 | const wchar_t* value_name, |
| 165 | DWORD64 value); |
| 166 | |
| 167 | // STATIC float set |
| 168 | static HRESULT SetValue(const wchar_t* full_key_name, |
| 169 | const wchar_t* value_name, |
| 170 | float value); |
| 171 | |
| 172 | // STATIC double set |
| 173 | static HRESULT SetValue(const wchar_t* full_key_name, |
| 174 | const wchar_t* value_name, |
| 175 | double value); |
| 176 | |
| 177 | // STATIC string set |
| 178 | static HRESULT SetValue(const wchar_t* full_key_name, |
| 179 | const wchar_t* value_name, |
| 180 | const wchar_t* value); |
| 181 | |
| 182 | // STATIC binary data set |
| 183 | static HRESULT SetValue(const wchar_t* full_key_name, |
| 184 | const wchar_t* value_name, |
| 185 | const uint8* value, |
| 186 | DWORD byte_count); |
| 187 | |
| 188 | // STATIC multi-string set |
| 189 | static HRESULT SetValueMultiSZ(const wchar_t* full_key_name, |
| 190 | const TCHAR* value_name, |
| 191 | const uint8* value, |
| 192 | DWORD byte_count); |
| 193 | |
| 194 | // GETTERS |
| 195 | |
| 196 | // STATIC int32 get |
| 197 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 198 | const wchar_t* value_name, |
| 199 | DWORD* value); |
| 200 | |
| 201 | // STATIC int64 get |
| 202 | // |
| 203 | // Note: if you are using time64 you should |
| 204 | // likely use GetLimitedTimeValue (util.h) instead of this method. |
| 205 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 206 | const wchar_t* value_name, |
| 207 | DWORD64* value); |
| 208 | |
| 209 | // STATIC float get |
| 210 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 211 | const wchar_t* value_name, |
| 212 | float* value); |
| 213 | |
| 214 | // STATIC double get |
| 215 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 216 | const wchar_t* value_name, |
| 217 | double* value); |
| 218 | |
| 219 | // STATIC string get |
| 220 | // Note: the caller must free the return buffer for wchar_t* version |
| 221 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 222 | const wchar_t* value_name, |
| 223 | wchar_t** value); |
| 224 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 225 | const wchar_t* value_name, |
| 226 | std::wstring* value); |
| 227 | |
| 228 | // STATIC REG_MULTI_SZ get |
| 229 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 230 | const wchar_t* value_name, |
| 231 | std::vector<std::wstring>* value); |
| 232 | |
| 233 | // STATIC get binary data - the caller must free the return buffer |
| 234 | static HRESULT GetValue(const wchar_t* full_key_name, |
| 235 | const wchar_t* value_name, |
| 236 | uint8** value, |
| 237 | DWORD* byte_count); |
| 238 | |
| 239 | // Get type of a registry value |
| 240 | static HRESULT GetValueType(const wchar_t* full_key_name, |
| 241 | const wchar_t* value_name, |
| 242 | DWORD* value_type); |
| 243 | |
| 244 | // delete a subkey of the current key (with no subkeys) |
| 245 | HRESULT DeleteSubKey(const wchar_t* key_name); |
| 246 | |
| 247 | // recursively delete a sub key of the current key (and all its subkeys) |
| 248 | HRESULT RecurseDeleteSubKey(const wchar_t* key_name); |
| 249 | |
| 250 | // STATIC version of delete key - handles nested keys also |
| 251 | // delete a key and all its sub-keys recursively |
| 252 | // Returns S_FALSE if key didn't exist, S_OK if deletion was successful, |
| 253 | // and failure otherwise. |
| 254 | static HRESULT DeleteKey(const wchar_t* full_key_name); |
| 255 | |
| 256 | // STATIC version of delete key |
| 257 | // delete a key recursively or non-recursively |
| 258 | // Returns S_FALSE if key didn't exist, S_OK if deletion was successful, |
| 259 | // and failure otherwise. |
| 260 | static HRESULT DeleteKey(const wchar_t* full_key_name, bool recursive); |
| 261 | |
| 262 | // delete the specified value |
| 263 | HRESULT DeleteValue(const wchar_t* value_name); |
| 264 | |
| 265 | // STATIC version of delete value |
| 266 | // Returns S_FALSE if key didn't exist, S_OK if deletion was successful, |
| 267 | // and failure otherwise. |
| 268 | static HRESULT DeleteValue(const wchar_t* full_key_name, |
| 269 | const wchar_t* value_name); |
| 270 | |
| 271 | // Peek inside (use a RegKey as a smart wrapper around a registry handle) |
| 272 | HKEY key() { return h_key_; } |
| 273 | |
| 274 | // helper function to get the HKEY and the root key from a string |
| 275 | // modifies the argument in place and returns the key name |
| 276 | // e.g. HKLM\\Software\\Google\... returns HKLM, "Software\\Google\..." |
| 277 | // Necessary for the static versions that use the full name of the reg key |
| 278 | static HKEY GetRootKeyInfo(std::wstring* full_key_name); |
| 279 | |
| 280 | // Returns true if this key name is 'safe' for deletion (doesn't specify a key |
| 281 | // root) |
| 282 | static bool SafeKeyNameForDeletion(const wchar_t* key_name); |
| 283 | |
| 284 | // save the key and all of its subkeys and values to a file |
| 285 | static HRESULT Save(const wchar_t* full_key_name, const wchar_t* file_name); |
| 286 | |
| 287 | // restore the key and all of its subkeys and values which are saved into a |
| 288 | // file |
| 289 | static HRESULT Restore(const wchar_t* full_key_name, |
| 290 | const wchar_t* file_name); |
| 291 | |
| 292 | // Is the key empty: having no sub-keys and values |
| 293 | static bool IsKeyEmpty(const wchar_t* full_key_name); |
| 294 | |
| 295 | private: |
| 296 | |
| 297 | // helper function to get any value from the registry |
| 298 | // used when the size of the data is unknown |
| 299 | HRESULT GetValueHelper(const wchar_t* value_name, |
| 300 | DWORD* type, uint8** value, |
| 301 | DWORD* byte_count) const; |
| 302 | |
| 303 | // helper function to get the parent key name and the subkey from a string |
| 304 | // modifies the argument in place and returns the key name |
| 305 | // Necessary for the static versions that use the full name of the reg key |
| 306 | static std::wstring GetParentKeyInfo(std::wstring* key_name); |
| 307 | |
| 308 | // common SET Helper for the static case |
| 309 | static HRESULT SetValueStaticHelper(const wchar_t* full_key_name, |
| 310 | const wchar_t* value_name, |
| 311 | DWORD type, |
| 312 | LPVOID value, |
| 313 | DWORD byte_count = 0); |
| 314 | |
| 315 | // common GET Helper for the static case |
| 316 | static HRESULT GetValueStaticHelper(const wchar_t* full_key_name, |
| 317 | const wchar_t* value_name, |
| 318 | DWORD type, |
| 319 | LPVOID value, |
| 320 | DWORD* byte_count = NULL); |
| 321 | |
| 322 | // convert REG_MULTI_SZ bytes to string array |
| 323 | static HRESULT MultiSZBytesToStringArray(const uint8* buffer, |
| 324 | DWORD byte_count, |
| 325 | std::vector<std::wstring>* value); |
| 326 | |
| 327 | // the HKEY for the current key |
| 328 | HKEY h_key_; |
| 329 | |
| 330 | // for unittest |
| 331 | friend void RegKeyHelperFunctionsTest(); |
| 332 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 333 | RTC_DISALLOW_COPY_AND_ASSIGN(RegKey); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | } // namespace rtc |
| 337 | |
| 338 | #endif // WEBRTC_BASE_WIN32REGKEY_H_ |