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 | 4 |
5 #ifndef BASE_REGISTRY_H_ | 5 #ifndef BASE_REGISTRY_H_ |
6 #define BASE_REGISTRY_H_ | 6 #define BASE_REGISTRY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <windows.h> | 9 // TODO(brettw) remove this file when all callers are converted to using the |
10 #include <string> | 10 // new location & namespace. |
| 11 #include "base/win/registry.h" |
11 | 12 |
12 #include "base/basictypes.h" | 13 class RegKey : public base::win::RegKey { |
13 | |
14 // Utility class to read, write and manipulate the Windows Registry. | |
15 // Registry vocabulary primer: a "key" is like a folder, in which there | |
16 // are "values", which are <name, data> pairs, with an associated data type. | |
17 class RegKey { | |
18 public: | 14 public: |
19 RegKey(); | 15 RegKey() {} |
20 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 16 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access) |
21 ~RegKey(); | 17 : base::win::RegKey(rootkey, subkey, access) {} |
22 | 18 ~RegKey() { base::win::RegKey::~RegKey(); } |
23 bool Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); | |
24 | |
25 bool CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, | |
26 DWORD* disposition, REGSAM access); | |
27 | |
28 bool Open(HKEY rootkey, const wchar_t* subkey, REGSAM access); | |
29 | |
30 // Creates a subkey or open it if it already exists. | |
31 bool CreateKey(const wchar_t* name, REGSAM access); | |
32 | |
33 // Opens a subkey | |
34 bool OpenKey(const wchar_t* name, REGSAM access); | |
35 | |
36 void Close(); | |
37 | |
38 DWORD ValueCount(); | |
39 | |
40 // Determine the nth value's name. | |
41 bool ReadName(int index, std::wstring* name); | |
42 | |
43 // True while the key is valid. | |
44 bool Valid() const { return key_ != NULL; } | |
45 | |
46 // Kill a key and everything that live below it; please be careful when using | |
47 // it. | |
48 bool DeleteKey(const wchar_t* name); | |
49 | |
50 // Deletes a single value within the key. | |
51 bool DeleteValue(const wchar_t* name); | |
52 | |
53 bool ValueExists(const wchar_t* name); | |
54 | |
55 bool ReadValue(const wchar_t* name, void* data, DWORD* dsize, DWORD* dtype); | |
56 bool ReadValue(const wchar_t* name, std::wstring* value); | |
57 bool ReadValueDW(const wchar_t* name, DWORD* value); | |
58 | |
59 bool WriteValue(const wchar_t* name, const void* data, DWORD dsize, | |
60 DWORD dtype); | |
61 bool WriteValue(const wchar_t* name, const wchar_t* value); | |
62 bool WriteValue(const wchar_t* name, DWORD value); | |
63 | |
64 // Starts watching the key to see if any of its values have changed. | |
65 // The key must have been opened with the KEY_NOTIFY access privelege. | |
66 bool StartWatching(); | |
67 | |
68 // If StartWatching hasn't been called, always returns false. | |
69 // Otherwise, returns true if anything under the key has changed. | |
70 // This can't be const because the |watch_event_| may be refreshed. | |
71 bool HasChanged(); | |
72 | |
73 // Will automatically be called by destructor if not manually called | |
74 // beforehand. Returns true if it was watching, false otherwise. | |
75 bool StopWatching(); | |
76 | |
77 inline bool IsWatching() const { return watch_event_ != 0; } | |
78 HANDLE watch_event() const { return watch_event_; } | |
79 HKEY Handle() const { return key_; } | |
80 | |
81 private: | |
82 HKEY key_; // The registry key being iterated. | |
83 HANDLE watch_event_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(RegKey); | |
86 }; | 19 }; |
87 | 20 |
88 // Iterates the entries found in a particular folder on the registry. | 21 class RegistryValueIterator : public base::win::RegistryValueIterator { |
89 // For this application I happen to know I wont need data size larger | |
90 // than MAX_PATH, but in real life this wouldn't neccessarily be | |
91 // adequate. | |
92 class RegistryValueIterator { | |
93 public: | 22 public: |
94 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); | 23 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key) |
95 | 24 : base::win::RegistryValueIterator(root_key, folder_key) {} |
96 ~RegistryValueIterator(); | 25 ~RegistryValueIterator() { |
97 | 26 base::win::RegistryValueIterator::~RegistryValueIterator(); |
98 DWORD ValueCount() const; | 27 } |
99 | |
100 // True while the iterator is valid. | |
101 bool Valid() const; | |
102 | |
103 // Advances to the next registry entry. | |
104 void operator++(); | |
105 | |
106 const wchar_t* Name() const { return name_; } | |
107 const wchar_t* Value() const { return value_; } | |
108 DWORD ValueSize() const { return value_size_; } | |
109 DWORD Type() const { return type_; } | |
110 | |
111 int Index() const { return index_; } | |
112 | |
113 private: | |
114 // Read in the current values. | |
115 bool Read(); | |
116 | |
117 // The registry key being iterated. | |
118 HKEY key_; | |
119 | |
120 // Current index of the iteration. | |
121 int index_; | |
122 | |
123 // Current values. | |
124 wchar_t name_[MAX_PATH]; | |
125 wchar_t value_[MAX_PATH]; | |
126 DWORD value_size_; | |
127 DWORD type_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator); | |
130 }; | 28 }; |
131 | 29 |
132 class RegistryKeyIterator { | 30 class RegistryKeyIterator : public base::win::RegistryKeyIterator { |
133 public: | 31 public: |
134 RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key); | 32 RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key) |
135 | 33 : base::win::RegistryKeyIterator(root_key, folder_key) {} |
136 ~RegistryKeyIterator(); | 34 ~RegistryKeyIterator() { |
137 | 35 base::win::RegistryKeyIterator::~RegistryKeyIterator(); |
138 DWORD SubkeyCount() const; | 36 } |
139 | |
140 // True while the iterator is valid. | |
141 bool Valid() const; | |
142 | |
143 // Advances to the next entry in the folder. | |
144 void operator++(); | |
145 | |
146 const wchar_t* Name() const { return name_; } | |
147 | |
148 int Index() const { return index_; } | |
149 | |
150 private: | |
151 // Read in the current values. | |
152 bool Read(); | |
153 | |
154 // The registry key being iterated. | |
155 HKEY key_; | |
156 | |
157 // Current index of the iteration. | |
158 int index_; | |
159 | |
160 wchar_t name_[MAX_PATH]; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); | |
163 }; | 37 }; |
164 | 38 |
165 #endif // BASE_REGISTRY_H_ | 39 #endif // BASE_REGISTRY_H_ |
OLD | NEW |