OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/win/object_watcher.h" | |
16 #include "base/win/scoped_handle.h" | |
17 | 15 |
18 namespace base { | 16 namespace base { |
19 namespace win { | 17 namespace win { |
20 | 18 |
21 // Utility class to read, write and manipulate the Windows Registry. | 19 // Utility class to read, write and manipulate the Windows Registry. |
22 // Registry vocabulary primer: a "key" is like a folder, in which there | 20 // Registry vocabulary primer: a "key" is like a folder, in which there |
23 // are "values", which are <name, data> pairs, with an associated data type. | 21 // are "values", which are <name, data> pairs, with an associated data type. |
24 // | 22 // |
25 // Note: | 23 // Note: |
26 // ReadValue family of functions guarantee that the return arguments | 24 // ReadValue family of functions guarantee that the return arguments |
27 // are not touched in case of failure. | 25 // are not touched in case of failure. |
28 class BASE_EXPORT RegKey { | 26 class BASE_EXPORT RegKey { |
29 public: | 27 public: |
30 // Called from the MessageLoop when the key changes. | |
31 typedef base::Callback<void()> ChangeCallback; | |
32 | |
33 RegKey(); | 28 RegKey(); |
34 explicit RegKey(HKEY key); | 29 explicit RegKey(HKEY key); |
35 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 30 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
36 ~RegKey(); | 31 ~RegKey(); |
37 | 32 |
38 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 33 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
39 | 34 |
40 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, | 35 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, |
41 DWORD* disposition, REGSAM access); | 36 DWORD* disposition, REGSAM access); |
42 | 37 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 LONG WriteValue(const wchar_t* name, const wchar_t* in_value); | 113 LONG WriteValue(const wchar_t* name, const wchar_t* in_value); |
119 | 114 |
120 // Sets raw data, including type. | 115 // Sets raw data, including type. |
121 LONG WriteValue(const wchar_t* name, | 116 LONG WriteValue(const wchar_t* name, |
122 const void* data, | 117 const void* data, |
123 DWORD dsize, | 118 DWORD dsize, |
124 DWORD dtype); | 119 DWORD dtype); |
125 | 120 |
126 // Starts watching the key to see if any of its values have changed. | 121 // Starts watching the key to see if any of its values have changed. |
127 // The key must have been opened with the KEY_NOTIFY access privilege. | 122 // The key must have been opened with the KEY_NOTIFY access privilege. |
128 // Returns true on success. | 123 LONG StartWatching(); |
129 // To stop watching, delete this RegKey object. To continue watching the | |
130 // object after the callback is invoked, call StartWatching again. | |
131 bool StartWatching(const ChangeCallback& callback); | |
132 | 124 |
| 125 // If StartWatching hasn't been called, always returns false. |
| 126 // Otherwise, returns true if anything under the key has changed. |
| 127 // This can't be const because the |watch_event_| may be refreshed. |
| 128 bool HasChanged(); |
| 129 |
| 130 // Will automatically be called by destructor if not manually called |
| 131 // beforehand. Returns true if it was watching, false otherwise. |
| 132 LONG StopWatching(); |
| 133 |
| 134 inline bool IsWatching() const { return watch_event_ != 0; } |
| 135 HANDLE watch_event() const { return watch_event_; } |
133 HKEY Handle() const { return key_; } | 136 HKEY Handle() const { return key_; } |
134 | 137 |
135 private: | 138 private: |
136 class Watcher; | |
137 | |
138 // Calls RegDeleteKeyEx on supported platforms, alternatively falls back to | 139 // Calls RegDeleteKeyEx on supported platforms, alternatively falls back to |
139 // RegDeleteKey. | 140 // RegDeleteKey. |
140 static LONG RegDeleteKeyExWrapper(HKEY hKey, | 141 static LONG RegDeleteKeyExWrapper(HKEY hKey, |
141 const wchar_t* lpSubKey, | 142 const wchar_t* lpSubKey, |
142 REGSAM samDesired, | 143 REGSAM samDesired, |
143 DWORD Reserved); | 144 DWORD Reserved); |
144 | 145 |
145 // Recursively deletes a key and all of its subkeys. | 146 // Recursively deletes a key and all of its subkeys. |
146 static LONG RegDelRecurse(HKEY root_key, | 147 static LONG RegDelRecurse(HKEY root_key, |
147 const std::wstring& name, | 148 const std::wstring& name, |
148 REGSAM access); | 149 REGSAM access); |
149 | |
150 HKEY key_; // The registry key being iterated. | 150 HKEY key_; // The registry key being iterated. |
| 151 HANDLE watch_event_; |
151 REGSAM wow64access_; | 152 REGSAM wow64access_; |
152 scoped_ptr<Watcher> key_watcher_; | |
153 | 153 |
154 DISALLOW_COPY_AND_ASSIGN(RegKey); | 154 DISALLOW_COPY_AND_ASSIGN(RegKey); |
155 }; | 155 }; |
156 | 156 |
157 // Iterates the entries found in a particular folder on the registry. | 157 // Iterates the entries found in a particular folder on the registry. |
158 class BASE_EXPORT RegistryValueIterator { | 158 class BASE_EXPORT RegistryValueIterator { |
159 public: | 159 public: |
160 // Construct a Registry Value Iterator with default WOW64 access. | 160 // Construct a Registry Value Iterator with default WOW64 access. |
161 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); | 161 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); |
162 | 162 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 250 |
251 wchar_t name_[MAX_PATH]; | 251 wchar_t name_[MAX_PATH]; |
252 | 252 |
253 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); | 253 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); |
254 }; | 254 }; |
255 | 255 |
256 } // namespace win | 256 } // namespace win |
257 } // namespace base | 257 } // namespace base |
258 | 258 |
259 #endif // BASE_WIN_REGISTRY_H_ | 259 #endif // BASE_WIN_REGISTRY_H_ |
OLD | NEW |