1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/registry.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,225 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 +// All Rights Reserved. 1.8 + 1.9 +#ifndef BASE_REGISTRY_H__ 1.10 +#define BASE_REGISTRY_H__ 1.11 + 1.12 +#include <windows.h> 1.13 +#include <tchar.h> 1.14 +#include <shlwapi.h> 1.15 +#include <string> 1.16 + 1.17 +// The shared file uses a bunch of header files that define types that we don't. 1.18 +// To avoid changing much code from the standard version, and also to avoid 1.19 +// polluting our namespace with extra types we don't want, we define these types 1.20 +// here with the preprocessor and undefine them at the end of the file. 1.21 +#define tchar TCHAR 1.22 +#define CTP const tchar* 1.23 +#define tstr std::basic_string<tchar> 1.24 + 1.25 +// RegKey 1.26 +// Utility class to read from and manipulate the registry. 1.27 +// Registry vocabulary primer: a "key" is like a folder, in which there 1.28 +// are "values", which are <name,data> pairs, with an associated data type. 1.29 + 1.30 +class RegKey { 1.31 + public: 1.32 + RegKey(HKEY rootkey = NULL, CTP subkey = NULL, REGSAM access = KEY_READ); 1.33 + // start there 1.34 + 1.35 + ~RegKey() { this->Close(); } 1.36 + 1.37 + bool Create(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ); 1.38 + 1.39 + bool CreateWithDisposition(HKEY rootkey, CTP subkey, DWORD* disposition, 1.40 + REGSAM access = KEY_READ); 1.41 + 1.42 + bool Open(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ); 1.43 + 1.44 + // Create a subkey (or open if exists) 1.45 + bool CreateKey(CTP name, REGSAM access); 1.46 + 1.47 + // Open a subkey 1.48 + bool OpenKey(CTP name, REGSAM access); 1.49 + 1.50 + // all done, eh? 1.51 + void Close(); 1.52 + 1.53 + DWORD ValueCount(); // Count of the number of value extant 1.54 + 1.55 + bool ReadName(int index, tstr* name); // Determine the Nth value's name 1.56 + 1.57 + // True while the key is valid 1.58 + bool Valid() const { return NULL != key_; } 1.59 + 1.60 + // Kill key and everything that liveth below it; please be careful out there 1.61 + bool DeleteKey(CTP name); 1.62 + 1.63 + // Delete a single value within the key 1.64 + bool DeleteValue(CTP name); 1.65 + 1.66 + bool ValueExists(CTP name); 1.67 + bool ReadValue(CTP name, void * data, DWORD * dsize, DWORD * dtype = NULL); 1.68 + bool ReadValue(CTP name, tstr * value); 1.69 + bool ReadValueDW(CTP name, DWORD * value); // Named to differ from tstr* 1.70 + 1.71 + bool WriteValue(CTP name, const void * data, DWORD dsize, 1.72 + DWORD dtype = REG_BINARY); 1.73 + bool WriteValue(CTP name, CTP value); 1.74 + bool WriteValue(CTP name, DWORD value); 1.75 + 1.76 + // StartWatching() 1.77 + // Start watching the key to see if any of its values have changed. 1.78 + // The key must have been opened with the KEY_NOTIFY access 1.79 + // privelege. 1.80 + bool StartWatching(); 1.81 + 1.82 + // HasChanged() 1.83 + // If StartWatching hasn't been called, always returns false. 1.84 + // Otherwise, returns true if anything under the key has changed. 1.85 + // This can't be const because the watch_event_ may be refreshed. 1.86 + bool HasChanged(); 1.87 + 1.88 + // StopWatching() 1.89 + // Will automatically be called by destructor if not manually called 1.90 + // beforehand. Returns true if it was watching, false otherwise. 1.91 + bool StopWatching(); 1.92 + 1.93 + inline bool IsWatching() const { return watch_event_ != 0; } 1.94 + HANDLE watch_event() const { return watch_event_; } 1.95 + HKEY Handle() const { return key_; } 1.96 + 1.97 + private: 1.98 + HKEY key_; // the registry key being iterated 1.99 + HANDLE watch_event_; 1.100 +}; 1.101 + 1.102 + 1.103 +// Standalone registry functions -- sorta deprecated, they now map to 1.104 +// using RegKey 1.105 + 1.106 + 1.107 +// Add a raw data to the registry -- you can pass NULL for the data if 1.108 +// you just want to create a key 1.109 +inline bool AddToRegistry(HKEY root_key, CTP key, CTP value_name, 1.110 + void const * data, DWORD dsize, 1.111 + DWORD dtype = REG_BINARY) { 1.112 + return RegKey(root_key, key, KEY_WRITE).WriteValue(value_name, data, dsize, 1.113 + dtype); 1.114 +} 1.115 + 1.116 +// Convenience routine to add a string value to the registry 1.117 +inline bool AddToRegistry(HKEY root_key, CTP key, CTP value_name, CTP value) { 1.118 + return AddToRegistry(root_key, key, value_name, value, 1.119 + sizeof(*value) * (lstrlen(value) + 1), REG_SZ); 1.120 +} 1.121 + 1.122 +// Read raw data from the registry -- pass something as the dtype 1.123 +// parameter if you care to learn what type the value was stored as 1.124 +inline bool ReadFromRegistry(HKEY root_key, CTP key, CTP value_name, 1.125 + void* data, DWORD* dsize, DWORD* dtype = NULL) { 1.126 + return RegKey(root_key, key).ReadValue(value_name, data, dsize, dtype); 1.127 +} 1.128 + 1.129 + 1.130 +// Delete a value or a key from the registry 1.131 +inline bool DeleteFromRegistry(HKEY root_key, CTP subkey, CTP value_name) { 1.132 + if (value_name) 1.133 + return ERROR_SUCCESS == ::SHDeleteValue(root_key, subkey, value_name); 1.134 + else 1.135 + return ERROR_SUCCESS == ::SHDeleteKey(root_key, subkey); 1.136 +} 1.137 + 1.138 + 1.139 + 1.140 +// delete a key and all subkeys from the registry 1.141 +inline bool DeleteKeyFromRegistry(HKEY root_key, CTP key_path, CTP key_name) { 1.142 + RegKey key; 1.143 + return key.Open(root_key, key_path, KEY_WRITE) 1.144 + && key.DeleteKey(key_name); 1.145 +} 1.146 + 1.147 + 1.148 +// Iterates the entries found in a particular folder on the registry. 1.149 +// For this application I happen to know I wont need data size larger 1.150 +// than MAX_PATH, but in real life this wouldn't neccessarily be 1.151 +// adequate. 1.152 +class RegistryValueIterator { 1.153 + public: 1.154 + // Specify a key in construction 1.155 + RegistryValueIterator(HKEY root_key, LPCTSTR folder_key); 1.156 + 1.157 + ~RegistryValueIterator(); 1.158 + 1.159 + DWORD ValueCount() const; // count of the number of subkeys extant 1.160 + 1.161 + bool Valid() const; // true while the iterator is valid 1.162 + 1.163 + void operator++(); // advance to the next entry in the folder 1.164 + 1.165 + // The pointers returned by these functions are statics owned by the 1.166 + // Name and Value functions 1.167 + CTP Name() const { return name_; } 1.168 + CTP Value() const { return value_; } 1.169 + DWORD ValueSize() const { return value_size_; } 1.170 + DWORD Type() const { return type_; } 1.171 + 1.172 + int Index() const { return index_; } 1.173 + 1.174 + private: 1.175 + bool Read(); // read in the current values 1.176 + 1.177 + HKEY key_; // the registry key being iterated 1.178 + int index_; // current index of the iteration 1.179 + 1.180 + // Current values 1.181 + TCHAR name_[MAX_PATH]; 1.182 + TCHAR value_[MAX_PATH]; 1.183 + DWORD value_size_; 1.184 + DWORD type_; 1.185 +}; 1.186 + 1.187 + 1.188 +class RegistryKeyIterator { 1.189 + public: 1.190 + // Specify a parent key in construction 1.191 + RegistryKeyIterator(HKEY root_key, LPCTSTR folder_key); 1.192 + 1.193 + ~RegistryKeyIterator(); 1.194 + 1.195 + DWORD SubkeyCount() const; // count of the number of subkeys extant 1.196 + 1.197 + bool Valid() const; // true while the iterator is valid 1.198 + 1.199 + void operator++(); // advance to the next entry in the folder 1.200 + 1.201 + // The pointer returned by Name() is a static owned by the function 1.202 + CTP Name() const { return name_; } 1.203 + 1.204 + int Index() const { return index_; } 1.205 + 1.206 + private: 1.207 + bool Read(); // read in the current values 1.208 + 1.209 + HKEY key_; // the registry key being iterated 1.210 + int index_; // current index of the iteration 1.211 + 1.212 + // Current values 1.213 + TCHAR name_[MAX_PATH]; 1.214 +}; 1.215 + 1.216 + 1.217 +// Register a COM object with the most usual properties. 1.218 +bool RegisterCOMServer(const tchar* guid, const tchar* name, 1.219 + const tchar* modulepath); 1.220 +bool RegisterCOMServer(const tchar* guid, const tchar* name, HINSTANCE module); 1.221 +bool UnregisterCOMServer(const tchar* guid); 1.222 + 1.223 +// undo the local types defined above 1.224 +#undef tchar 1.225 +#undef CTP 1.226 +#undef tstr 1.227 + 1.228 +#endif // BASE_REGISTRY_H__