| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // All Rights Reserved. | |
| 5 | 4 |
| 6 #ifndef BASE_REGISTRY_H_ | 5 #ifndef BASE_REGISTRY_H_ |
| 7 #define BASE_REGISTRY_H_ | 6 #define BASE_REGISTRY_H_ |
| 8 #pragma once | 7 #pragma once |
| 9 | 8 |
| 10 #include <windows.h> | 9 #include <windows.h> |
| 11 #include <tchar.h> | 10 |
| 12 #include <shlwapi.h> | |
| 13 #include <string> | 11 #include <string> |
| 14 | 12 |
| 15 // The shared file uses a bunch of header files that define types that we don't. | 13 #include "base/basictypes.h" |
| 16 // To avoid changing much code from the standard version, and also to avoid | |
| 17 // polluting our namespace with extra types we don't want, we define these types | |
| 18 // here with the preprocessor and undefine them at the end of the file. | |
| 19 #define tchar TCHAR | |
| 20 #define CTP const tchar* | |
| 21 #define tstr std::basic_string<tchar> | |
| 22 | 14 |
| 23 // RegKey | 15 // TODO(tfarina): Get rid of all the default arguments used in this file. |
| 24 // Utility class to read from and manipulate the registry. | 16 // They are not allowed by our style guide. |
| 17 |
| 18 // Utility class to read, write and manipulate the Windows Registry. |
| 25 // Registry vocabulary primer: a "key" is like a folder, in which there | 19 // Registry vocabulary primer: a "key" is like a folder, in which there |
| 26 // are "values", which are <name,data> pairs, with an associated data type. | 20 // are "values", which are <name, data> pairs, with an associated data type. |
| 27 | |
| 28 class RegKey { | 21 class RegKey { |
| 29 public: | 22 public: |
| 30 RegKey(HKEY rootkey = NULL, CTP subkey = NULL, REGSAM access = KEY_READ); | 23 RegKey(HKEY rootkey = NULL, const wchar_t* subkey = NULL, |
| 31 | 24 REGSAM access = KEY_READ); |
| 32 ~RegKey() { Close(); } | 25 ~RegKey() { Close(); } |
| 33 | 26 |
| 34 bool Create(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ); | 27 bool Create(HKEY rootkey, const wchar_t* subkey, REGSAM access = KEY_READ); |
| 35 | 28 |
| 36 bool CreateWithDisposition(HKEY rootkey, CTP subkey, DWORD* disposition, | 29 bool CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, |
| 37 REGSAM access = KEY_READ); | 30 DWORD* disposition, REGSAM access = KEY_READ); |
| 38 | 31 |
| 39 bool Open(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ); | 32 bool Open(HKEY rootkey, const wchar_t* subkey, REGSAM access = KEY_READ); |
| 40 | 33 |
| 41 // Create a subkey (or open if exists). | 34 // Creates a subkey or open it if it already exists. |
| 42 bool CreateKey(CTP name, REGSAM access); | 35 bool CreateKey(const wchar_t* name, REGSAM access); |
| 43 | 36 |
| 44 // Open a subkey | 37 // Opens a subkey |
| 45 bool OpenKey(CTP name, REGSAM access); | 38 bool OpenKey(const wchar_t* name, REGSAM access); |
| 46 | 39 |
| 47 // all done, eh? | |
| 48 void Close(); | 40 void Close(); |
| 49 | 41 |
| 50 // Count of the number of value extant. | |
| 51 DWORD ValueCount(); | 42 DWORD ValueCount(); |
| 52 | 43 |
| 53 // Determine the Nth value's name. | 44 // Determine the nth value's name. |
| 54 bool ReadName(int index, tstr* name); | 45 bool ReadName(int index, std::wstring* name); |
| 55 | 46 |
| 56 // True while the key is valid. | 47 // True while the key is valid. |
| 57 bool Valid() const { return key_ != NULL; } | 48 bool Valid() const { return key_ != NULL; } |
| 58 | 49 |
| 59 // Kill key and everything that liveth below it; please be careful out there. | 50 // Kill a key and everything that live below it; please be careful when using |
| 60 bool DeleteKey(CTP name); | 51 // it. |
| 52 bool DeleteKey(const wchar_t* name); |
| 61 | 53 |
| 62 // Delete a single value within the key. | 54 // Deletes a single value within the key. |
| 63 bool DeleteValue(CTP name); | 55 bool DeleteValue(const wchar_t* name); |
| 64 | 56 |
| 65 bool ValueExists(CTP name); | 57 bool ValueExists(const wchar_t* name); |
| 66 bool ReadValue(CTP name, void* data, DWORD* dsize, DWORD* dtype = NULL); | |
| 67 bool ReadValue(CTP name, tstr* value); | |
| 68 bool ReadValueDW(CTP name, DWORD* value); // Named to differ from tstr* | |
| 69 | 58 |
| 70 bool WriteValue(CTP name, const void* data, DWORD dsize, | 59 bool ReadValue(const wchar_t* name, void* data, DWORD* dsize, |
| 60 DWORD* dtype = NULL); |
| 61 bool ReadValue(const wchar_t* name, std::wstring* value); |
| 62 bool ReadValueDW(const wchar_t* name, DWORD* value); |
| 63 |
| 64 bool WriteValue(const wchar_t* name, const void* data, DWORD dsize, |
| 71 DWORD dtype = REG_BINARY); | 65 DWORD dtype = REG_BINARY); |
| 72 bool WriteValue(CTP name, CTP value); | 66 bool WriteValue(const wchar_t* name, const wchar_t* value); |
| 73 bool WriteValue(CTP name, DWORD value); | 67 bool WriteValue(const wchar_t* name, DWORD value); |
| 74 | 68 |
| 75 // Start watching the key to see if any of its values have changed. | 69 // Starts watching the key to see if any of its values have changed. |
| 76 // The key must have been opened with the KEY_NOTIFY access | 70 // The key must have been opened with the KEY_NOTIFY access privelege. |
| 77 // privelege. | |
| 78 bool StartWatching(); | 71 bool StartWatching(); |
| 79 | 72 |
| 80 // If StartWatching hasn't been called, always returns false. | 73 // If StartWatching hasn't been called, always returns false. |
| 81 // Otherwise, returns true if anything under the key has changed. | 74 // Otherwise, returns true if anything under the key has changed. |
| 82 // This can't be const because the |watch_event_| may be refreshed. | 75 // This can't be const because the |watch_event_| may be refreshed. |
| 83 bool HasChanged(); | 76 bool HasChanged(); |
| 84 | 77 |
| 85 // Will automatically be called by destructor if not manually called | 78 // Will automatically be called by destructor if not manually called |
| 86 // beforehand. Returns true if it was watching, false otherwise. | 79 // beforehand. Returns true if it was watching, false otherwise. |
| 87 bool StopWatching(); | 80 bool StopWatching(); |
| 88 | 81 |
| 89 inline bool IsWatching() const { return watch_event_ != 0; } | 82 inline bool IsWatching() const { return watch_event_ != 0; } |
| 90 HANDLE watch_event() const { return watch_event_; } | 83 HANDLE watch_event() const { return watch_event_; } |
| 91 HKEY Handle() const { return key_; } | 84 HKEY Handle() const { return key_; } |
| 92 | 85 |
| 93 private: | 86 private: |
| 94 HKEY key_; // The registry key being iterated. | 87 HKEY key_; // The registry key being iterated. |
| 95 HANDLE watch_event_; | 88 HANDLE watch_event_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(RegKey); |
| 96 }; | 91 }; |
| 97 | 92 |
| 98 // Iterates the entries found in a particular folder on the registry. | 93 // Iterates the entries found in a particular folder on the registry. |
| 99 // For this application I happen to know I wont need data size larger | 94 // For this application I happen to know I wont need data size larger |
| 100 // than MAX_PATH, but in real life this wouldn't neccessarily be | 95 // than MAX_PATH, but in real life this wouldn't neccessarily be |
| 101 // adequate. | 96 // adequate. |
| 102 class RegistryValueIterator { | 97 class RegistryValueIterator { |
| 103 public: | 98 public: |
| 104 // Specify a key in construction. | 99 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); |
| 105 RegistryValueIterator(HKEY root_key, LPCTSTR folder_key); | |
| 106 | 100 |
| 107 ~RegistryValueIterator(); | 101 ~RegistryValueIterator(); |
| 108 | 102 |
| 109 DWORD ValueCount() const; // Count of the number of subkeys extant. | 103 DWORD ValueCount() const; |
| 110 | 104 |
| 111 bool Valid() const; // True while the iterator is valid. | 105 // True while the iterator is valid. |
| 106 bool Valid() const; |
| 112 | 107 |
| 113 void operator++(); // Advance to the next entry in the folder. | 108 // Advances to the next registry entry. |
| 109 void operator++(); |
| 114 | 110 |
| 115 // The pointers returned by these functions are statics owned by the | 111 const wchar_t* Name() const { return name_; } |
| 116 // Name and Value functions. | 112 const wchar_t* Value() const { return value_; } |
| 117 CTP Name() const { return name_; } | |
| 118 CTP Value() const { return value_; } | |
| 119 DWORD ValueSize() const { return value_size_; } | 113 DWORD ValueSize() const { return value_size_; } |
| 120 DWORD Type() const { return type_; } | 114 DWORD Type() const { return type_; } |
| 121 | 115 |
| 122 int Index() const { return index_; } | 116 int Index() const { return index_; } |
| 123 | 117 |
| 124 private: | 118 private: |
| 125 bool Read(); // Read in the current values. | 119 // Read in the current values. |
| 120 bool Read(); |
| 126 | 121 |
| 127 HKEY key_; // The registry key being iterated. | 122 // The registry key being iterated. |
| 128 int index_; // Current index of the iteration. | 123 HKEY key_; |
| 124 |
| 125 // Current index of the iteration. |
| 126 int index_; |
| 129 | 127 |
| 130 // Current values. | 128 // Current values. |
| 131 TCHAR name_[MAX_PATH]; | 129 wchar_t name_[MAX_PATH]; |
| 132 TCHAR value_[MAX_PATH]; | 130 wchar_t value_[MAX_PATH]; |
| 133 DWORD value_size_; | 131 DWORD value_size_; |
| 134 DWORD type_; | 132 DWORD type_; |
| 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator); |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 | |
| 138 class RegistryKeyIterator { | 137 class RegistryKeyIterator { |
| 139 public: | 138 public: |
| 140 // Specify a parent key in construction. | 139 RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key); |
| 141 RegistryKeyIterator(HKEY root_key, LPCTSTR folder_key); | |
| 142 | 140 |
| 143 ~RegistryKeyIterator(); | 141 ~RegistryKeyIterator(); |
| 144 | 142 |
| 145 DWORD SubkeyCount() const; // Count of the number of subkeys extant. | 143 DWORD SubkeyCount() const; |
| 146 | 144 |
| 147 bool Valid() const; // True while the iterator is valid. | 145 // True while the iterator is valid. |
| 146 bool Valid() const; |
| 148 | 147 |
| 149 void operator++(); // Advance to the next entry in the folder. | 148 // Advances to the next entry in the folder. |
| 149 void operator++(); |
| 150 | 150 |
| 151 // The pointer returned by Name() is a static owned by the function. | 151 const wchar_t* Name() const { return name_; } |
| 152 CTP Name() const { return name_; } | |
| 153 | 152 |
| 154 int Index() const { return index_; } | 153 int Index() const { return index_; } |
| 155 | 154 |
| 156 private: | 155 private: |
| 157 bool Read(); // Read in the current values. | 156 // Read in the current values. |
| 157 bool Read(); |
| 158 | 158 |
| 159 HKEY key_; // The registry key being iterated. | 159 // The registry key being iterated. |
| 160 int index_; // Current index of the iteration. | 160 HKEY key_; |
| 161 | 161 |
| 162 // Current values. | 162 // Current index of the iteration. |
| 163 TCHAR name_[MAX_PATH]; | 163 int index_; |
| 164 |
| 165 wchar_t name_[MAX_PATH]; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); |
| 164 }; | 168 }; |
| 165 | 169 |
| 166 | |
| 167 // Register a COM object with the most usual properties. | |
| 168 bool RegisterCOMServer(const tchar* guid, const tchar* name, | |
| 169 const tchar* modulepath); | |
| 170 bool RegisterCOMServer(const tchar* guid, const tchar* name, HINSTANCE module); | |
| 171 bool UnregisterCOMServer(const tchar* guid); | |
| 172 | |
| 173 // undo the local types defined above | |
| 174 #undef tchar | |
| 175 #undef CTP | |
| 176 #undef tstr | |
| 177 | |
| 178 #endif // BASE_REGISTRY_H_ | 170 #endif // BASE_REGISTRY_H_ |
| OLD | NEW |