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