ipc/chromium/src/base/registry.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4 // All Rights Reserved.
michael@0 5
michael@0 6 #ifndef BASE_REGISTRY_H__
michael@0 7 #define BASE_REGISTRY_H__
michael@0 8
michael@0 9 #include <windows.h>
michael@0 10 #include <tchar.h>
michael@0 11 #include <shlwapi.h>
michael@0 12 #include <string>
michael@0 13
michael@0 14 // The shared file uses a bunch of header files that define types that we don't.
michael@0 15 // To avoid changing much code from the standard version, and also to avoid
michael@0 16 // polluting our namespace with extra types we don't want, we define these types
michael@0 17 // here with the preprocessor and undefine them at the end of the file.
michael@0 18 #define tchar TCHAR
michael@0 19 #define CTP const tchar*
michael@0 20 #define tstr std::basic_string<tchar>
michael@0 21
michael@0 22 // RegKey
michael@0 23 // Utility class to read from and manipulate the registry.
michael@0 24 // Registry vocabulary primer: a "key" is like a folder, in which there
michael@0 25 // are "values", which are <name,data> pairs, with an associated data type.
michael@0 26
michael@0 27 class RegKey {
michael@0 28 public:
michael@0 29 RegKey(HKEY rootkey = NULL, CTP subkey = NULL, REGSAM access = KEY_READ);
michael@0 30 // start there
michael@0 31
michael@0 32 ~RegKey() { this->Close(); }
michael@0 33
michael@0 34 bool Create(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ);
michael@0 35
michael@0 36 bool CreateWithDisposition(HKEY rootkey, CTP subkey, DWORD* disposition,
michael@0 37 REGSAM access = KEY_READ);
michael@0 38
michael@0 39 bool Open(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ);
michael@0 40
michael@0 41 // Create a subkey (or open if exists)
michael@0 42 bool CreateKey(CTP name, REGSAM access);
michael@0 43
michael@0 44 // Open a subkey
michael@0 45 bool OpenKey(CTP name, REGSAM access);
michael@0 46
michael@0 47 // all done, eh?
michael@0 48 void Close();
michael@0 49
michael@0 50 DWORD ValueCount(); // Count of the number of value extant
michael@0 51
michael@0 52 bool ReadName(int index, tstr* name); // Determine the Nth value's name
michael@0 53
michael@0 54 // True while the key is valid
michael@0 55 bool Valid() const { return NULL != key_; }
michael@0 56
michael@0 57 // Kill key and everything that liveth below it; please be careful out there
michael@0 58 bool DeleteKey(CTP name);
michael@0 59
michael@0 60 // Delete a single value within the key
michael@0 61 bool DeleteValue(CTP name);
michael@0 62
michael@0 63 bool ValueExists(CTP name);
michael@0 64 bool ReadValue(CTP name, void * data, DWORD * dsize, DWORD * dtype = NULL);
michael@0 65 bool ReadValue(CTP name, tstr * value);
michael@0 66 bool ReadValueDW(CTP name, DWORD * value); // Named to differ from tstr*
michael@0 67
michael@0 68 bool WriteValue(CTP name, const void * data, DWORD dsize,
michael@0 69 DWORD dtype = REG_BINARY);
michael@0 70 bool WriteValue(CTP name, CTP value);
michael@0 71 bool WriteValue(CTP name, DWORD value);
michael@0 72
michael@0 73 // StartWatching()
michael@0 74 // Start watching the key to see if any of its values have changed.
michael@0 75 // The key must have been opened with the KEY_NOTIFY access
michael@0 76 // privelege.
michael@0 77 bool StartWatching();
michael@0 78
michael@0 79 // HasChanged()
michael@0 80 // If StartWatching hasn't been called, always returns false.
michael@0 81 // Otherwise, returns true if anything under the key has changed.
michael@0 82 // This can't be const because the watch_event_ may be refreshed.
michael@0 83 bool HasChanged();
michael@0 84
michael@0 85 // StopWatching()
michael@0 86 // Will automatically be called by destructor if not manually called
michael@0 87 // beforehand. Returns true if it was watching, false otherwise.
michael@0 88 bool StopWatching();
michael@0 89
michael@0 90 inline bool IsWatching() const { return watch_event_ != 0; }
michael@0 91 HANDLE watch_event() const { return watch_event_; }
michael@0 92 HKEY Handle() const { return key_; }
michael@0 93
michael@0 94 private:
michael@0 95 HKEY key_; // the registry key being iterated
michael@0 96 HANDLE watch_event_;
michael@0 97 };
michael@0 98
michael@0 99
michael@0 100 // Standalone registry functions -- sorta deprecated, they now map to
michael@0 101 // using RegKey
michael@0 102
michael@0 103
michael@0 104 // Add a raw data to the registry -- you can pass NULL for the data if
michael@0 105 // you just want to create a key
michael@0 106 inline bool AddToRegistry(HKEY root_key, CTP key, CTP value_name,
michael@0 107 void const * data, DWORD dsize,
michael@0 108 DWORD dtype = REG_BINARY) {
michael@0 109 return RegKey(root_key, key, KEY_WRITE).WriteValue(value_name, data, dsize,
michael@0 110 dtype);
michael@0 111 }
michael@0 112
michael@0 113 // Convenience routine to add a string value to the registry
michael@0 114 inline bool AddToRegistry(HKEY root_key, CTP key, CTP value_name, CTP value) {
michael@0 115 return AddToRegistry(root_key, key, value_name, value,
michael@0 116 sizeof(*value) * (lstrlen(value) + 1), REG_SZ);
michael@0 117 }
michael@0 118
michael@0 119 // Read raw data from the registry -- pass something as the dtype
michael@0 120 // parameter if you care to learn what type the value was stored as
michael@0 121 inline bool ReadFromRegistry(HKEY root_key, CTP key, CTP value_name,
michael@0 122 void* data, DWORD* dsize, DWORD* dtype = NULL) {
michael@0 123 return RegKey(root_key, key).ReadValue(value_name, data, dsize, dtype);
michael@0 124 }
michael@0 125
michael@0 126
michael@0 127 // Delete a value or a key from the registry
michael@0 128 inline bool DeleteFromRegistry(HKEY root_key, CTP subkey, CTP value_name) {
michael@0 129 if (value_name)
michael@0 130 return ERROR_SUCCESS == ::SHDeleteValue(root_key, subkey, value_name);
michael@0 131 else
michael@0 132 return ERROR_SUCCESS == ::SHDeleteKey(root_key, subkey);
michael@0 133 }
michael@0 134
michael@0 135
michael@0 136
michael@0 137 // delete a key and all subkeys from the registry
michael@0 138 inline bool DeleteKeyFromRegistry(HKEY root_key, CTP key_path, CTP key_name) {
michael@0 139 RegKey key;
michael@0 140 return key.Open(root_key, key_path, KEY_WRITE)
michael@0 141 && key.DeleteKey(key_name);
michael@0 142 }
michael@0 143
michael@0 144
michael@0 145 // Iterates the entries found in a particular folder on the registry.
michael@0 146 // For this application I happen to know I wont need data size larger
michael@0 147 // than MAX_PATH, but in real life this wouldn't neccessarily be
michael@0 148 // adequate.
michael@0 149 class RegistryValueIterator {
michael@0 150 public:
michael@0 151 // Specify a key in construction
michael@0 152 RegistryValueIterator(HKEY root_key, LPCTSTR folder_key);
michael@0 153
michael@0 154 ~RegistryValueIterator();
michael@0 155
michael@0 156 DWORD ValueCount() const; // count of the number of subkeys extant
michael@0 157
michael@0 158 bool Valid() const; // true while the iterator is valid
michael@0 159
michael@0 160 void operator++(); // advance to the next entry in the folder
michael@0 161
michael@0 162 // The pointers returned by these functions are statics owned by the
michael@0 163 // Name and Value functions
michael@0 164 CTP Name() const { return name_; }
michael@0 165 CTP Value() const { return value_; }
michael@0 166 DWORD ValueSize() const { return value_size_; }
michael@0 167 DWORD Type() const { return type_; }
michael@0 168
michael@0 169 int Index() const { return index_; }
michael@0 170
michael@0 171 private:
michael@0 172 bool Read(); // read in the current values
michael@0 173
michael@0 174 HKEY key_; // the registry key being iterated
michael@0 175 int index_; // current index of the iteration
michael@0 176
michael@0 177 // Current values
michael@0 178 TCHAR name_[MAX_PATH];
michael@0 179 TCHAR value_[MAX_PATH];
michael@0 180 DWORD value_size_;
michael@0 181 DWORD type_;
michael@0 182 };
michael@0 183
michael@0 184
michael@0 185 class RegistryKeyIterator {
michael@0 186 public:
michael@0 187 // Specify a parent key in construction
michael@0 188 RegistryKeyIterator(HKEY root_key, LPCTSTR folder_key);
michael@0 189
michael@0 190 ~RegistryKeyIterator();
michael@0 191
michael@0 192 DWORD SubkeyCount() const; // count of the number of subkeys extant
michael@0 193
michael@0 194 bool Valid() const; // true while the iterator is valid
michael@0 195
michael@0 196 void operator++(); // advance to the next entry in the folder
michael@0 197
michael@0 198 // The pointer returned by Name() is a static owned by the function
michael@0 199 CTP Name() const { return name_; }
michael@0 200
michael@0 201 int Index() const { return index_; }
michael@0 202
michael@0 203 private:
michael@0 204 bool Read(); // read in the current values
michael@0 205
michael@0 206 HKEY key_; // the registry key being iterated
michael@0 207 int index_; // current index of the iteration
michael@0 208
michael@0 209 // Current values
michael@0 210 TCHAR name_[MAX_PATH];
michael@0 211 };
michael@0 212
michael@0 213
michael@0 214 // Register a COM object with the most usual properties.
michael@0 215 bool RegisterCOMServer(const tchar* guid, const tchar* name,
michael@0 216 const tchar* modulepath);
michael@0 217 bool RegisterCOMServer(const tchar* guid, const tchar* name, HINSTANCE module);
michael@0 218 bool UnregisterCOMServer(const tchar* guid);
michael@0 219
michael@0 220 // undo the local types defined above
michael@0 221 #undef tchar
michael@0 222 #undef CTP
michael@0 223 #undef tstr
michael@0 224
michael@0 225 #endif // BASE_REGISTRY_H__

mercurial