| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef BASE_WIN_REGISTRY_H_ | 5 #ifndef BASE_WIN_REGISTRY_H_ |
| 6 #define BASE_WIN_REGISTRY_H_ | 6 #define BASE_WIN_REGISTRY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 | 13 |
| 14 // Please ignore this part. This temporary hack exists |
| 15 // to detect if the return value is used as 'bool'. |
| 16 // Todo(amit): remove this before (or soon after) checkin. |
| 17 struct CatchBoolChecks { |
| 18 CatchBoolChecks(LONG l) : l_(l) {} |
| 19 LONG l_; |
| 20 operator LONG() { return l_; } |
| 21 LONG value() const { return l_; } |
| 22 bool operator == (LONG l) const { return l == l_; } |
| 23 bool operator != (LONG l) const { return l != l_; } |
| 24 private: |
| 25 // If you hit a compile error here, you most likely attempting to use the |
| 26 // return value of a RegKey helper as a bool. Please note that RegKey |
| 27 // methods return LONG now instead of bool. |
| 28 operator bool () { return false; } |
| 29 }; |
| 30 |
| 31 inline bool operator == (const LONG& l, const CatchBoolChecks& g) { |
| 32 return g.value() == l; |
| 33 } |
| 34 |
| 35 inline bool operator != (const LONG& l, const CatchBoolChecks& g) { |
| 36 return g.value() != l; |
| 37 } |
| 38 |
| 39 using std::ostream; |
| 40 inline ostream& operator <<(ostream &os, const CatchBoolChecks& g) { |
| 41 os << g.value(); |
| 42 return os; |
| 43 } |
| 44 |
| 45 typedef CatchBoolChecks GONG; |
| 46 |
| 14 namespace base { | 47 namespace base { |
| 15 namespace win { | 48 namespace win { |
| 16 | 49 |
| 17 // Utility class to read, write and manipulate the Windows Registry. | 50 // Utility class to read, write and manipulate the Windows Registry. |
| 18 // Registry vocabulary primer: a "key" is like a folder, in which there | 51 // Registry vocabulary primer: a "key" is like a folder, in which there |
| 19 // are "values", which are <name, data> pairs, with an associated data type. | 52 // are "values", which are <name, data> pairs, with an associated data type. |
| 53 // |
| 54 // Note: |
| 55 // ReadValue family of functions guarantee that the return arguments |
| 56 // are not touched in case of failure. |
| 20 class RegKey { | 57 class RegKey { |
| 21 public: | 58 public: |
| 22 RegKey(); | 59 RegKey(); |
| 23 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 60 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
| 24 ~RegKey(); | 61 ~RegKey(); |
| 25 | 62 |
| 26 bool Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 63 GONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
| 27 | 64 |
| 28 bool CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, | 65 GONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, |
| 29 DWORD* disposition, REGSAM access); | 66 DWORD* disposition, REGSAM access); |
| 30 | 67 |
| 31 bool Open(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 68 GONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
| 32 | 69 |
| 33 // Creates a subkey or open it if it already exists. | 70 // Creates a subkey or open it if it already exists. |
| 34 bool CreateKey(const wchar_t* name, REGSAM access); | 71 GONG CreateKey(const wchar_t* name, REGSAM access); |
| 35 | 72 |
| 36 // Opens a subkey | 73 // Opens a subkey |
| 37 bool OpenKey(const wchar_t* name, REGSAM access); | 74 GONG OpenKey(const wchar_t* name, REGSAM access); |
| 38 | 75 |
| 39 void Close(); | 76 void Close(); |
| 40 | 77 |
| 41 DWORD ValueCount() const; | 78 DWORD ValueCount() const; |
| 42 | 79 |
| 43 // Determine the nth value's name. | 80 // Determine the nth value's name. |
| 44 bool ReadName(int index, std::wstring* name) const; | 81 GONG ReadName(int index, std::wstring* name) const; |
| 45 | 82 |
| 46 // True while the key is valid. | 83 // True while the key is valid. |
| 47 bool Valid() const { return key_ != NULL; } | 84 bool Valid() const { return key_ != NULL; } |
| 48 | 85 |
| 49 // Kill a key and everything that live below it; please be careful when using | 86 // Kill a key and everything that live below it; please be careful when using |
| 50 // it. | 87 // it. |
| 51 bool DeleteKey(const wchar_t* name); | 88 GONG DeleteKey(const wchar_t* name); |
| 52 | 89 |
| 53 // Deletes a single value within the key. | 90 // Deletes a single value within the key. |
| 54 bool DeleteValue(const wchar_t* name); | 91 GONG DeleteValue(const wchar_t* name); |
| 55 | 92 |
| 56 bool ValueExists(const wchar_t* name); | 93 bool ValueExists(const wchar_t* name) const; |
| 57 | 94 |
| 58 bool ReadValue(const wchar_t* name, void* data, DWORD* dsize, | 95 GONG ReadValue(const wchar_t* name, void* data, DWORD* dsize, |
| 59 DWORD* dtype) const; | 96 DWORD* dtype) const; |
| 60 bool ReadValue(const wchar_t* name, std::wstring* value) const; | 97 GONG ReadValue(const wchar_t* name, std::wstring* value) const; |
| 61 bool ReadValueDW(const wchar_t* name, DWORD* value) const; | 98 GONG ReadValueDW(const wchar_t* name, DWORD* value) const; |
| 99 GONG ReadInt64(const wchar_t* name, int64* value) const; |
| 62 | 100 |
| 63 bool WriteValue(const wchar_t* name, const void* data, DWORD dsize, | 101 GONG WriteValue(const wchar_t* name, const void* data, DWORD dsize, |
| 64 DWORD dtype); | 102 DWORD dtype); |
| 65 bool WriteValue(const wchar_t* name, const wchar_t* value); | 103 GONG WriteValue(const wchar_t* name, const wchar_t* value); |
| 66 bool WriteValue(const wchar_t* name, DWORD value); | 104 GONG WriteValue(const wchar_t* name, DWORD value); |
| 67 | 105 |
| 68 // Starts watching the key to see if any of its values have changed. | 106 // Starts watching the key to see if any of its values have changed. |
| 69 // The key must have been opened with the KEY_NOTIFY access privilege. | 107 // The key must have been opened with the KEY_NOTIFY access privilege. |
| 70 bool StartWatching(); | 108 GONG StartWatching(); |
| 71 | 109 |
| 72 // If StartWatching hasn't been called, always returns false. | 110 // If StartWatching hasn't been called, always returns false. |
| 73 // Otherwise, returns true if anything under the key has changed. | 111 // Otherwise, returns true if anything under the key has changed. |
| 74 // This can't be const because the |watch_event_| may be refreshed. | 112 // This can't be const because the |watch_event_| may be refreshed. |
| 75 bool HasChanged(); | 113 bool HasChanged(); |
| 76 | 114 |
| 77 // Will automatically be called by destructor if not manually called | 115 // Will automatically be called by destructor if not manually called |
| 78 // beforehand. Returns true if it was watching, false otherwise. | 116 // beforehand. Returns true if it was watching, false otherwise. |
| 79 bool StopWatching(); | 117 GONG StopWatching(); |
| 80 | 118 |
| 81 inline bool IsWatching() const { return watch_event_ != 0; } | 119 inline bool IsWatching() const { return watch_event_ != 0; } |
| 82 HANDLE watch_event() const { return watch_event_; } | 120 HANDLE watch_event() const { return watch_event_; } |
| 83 HKEY Handle() const { return key_; } | 121 HKEY Handle() const { return key_; } |
| 84 | 122 |
| 85 private: | 123 private: |
| 86 HKEY key_; // The registry key being iterated. | 124 HKEY key_; // The registry key being iterated. |
| 87 HANDLE watch_event_; | 125 HANDLE watch_event_; |
| 88 | 126 |
| 89 DISALLOW_COPY_AND_ASSIGN(RegKey); | 127 DISALLOW_COPY_AND_ASSIGN(RegKey); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 201 |
| 164 wchar_t name_[MAX_PATH]; | 202 wchar_t name_[MAX_PATH]; |
| 165 | 203 |
| 166 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); | 204 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); |
| 167 }; | 205 }; |
| 168 | 206 |
| 169 } // namespace win | 207 } // namespace win |
| 170 } // namespace base | 208 } // namespace base |
| 171 | 209 |
| 172 #endif // BASE_WIN_REGISTRY_H_ | 210 #endif // BASE_WIN_REGISTRY_H_ |
| OLD | NEW |