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" |
15 | 17 |
16 namespace base { | 18 namespace base { |
17 namespace win { | 19 namespace win { |
18 | 20 |
19 // Utility class to read, write and manipulate the Windows Registry. | 21 // Utility class to read, write and manipulate the Windows Registry. |
20 // Registry vocabulary primer: a "key" is like a folder, in which there | 22 // Registry vocabulary primer: a "key" is like a folder, in which there |
21 // are "values", which are <name, data> pairs, with an associated data type. | 23 // are "values", which are <name, data> pairs, with an associated data type. |
22 // | 24 // |
23 // Note: | 25 // Note: |
24 // ReadValue family of functions guarantee that the return arguments | 26 // ReadValue family of functions guarantee that the return arguments |
25 // are not touched in case of failure. | 27 // are not touched in case of failure. |
26 class BASE_EXPORT RegKey { | 28 class BASE_EXPORT RegKey { |
27 public: | 29 public: |
| 30 // Called from the MessageLoop when the key changes. |
| 31 typedef base::Callback<void()> ChangeCallback; |
| 32 |
28 RegKey(); | 33 RegKey(); |
29 explicit RegKey(HKEY key); | 34 explicit RegKey(HKEY key); |
30 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 35 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
31 ~RegKey(); | 36 ~RegKey(); |
32 | 37 |
33 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 38 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
34 | 39 |
35 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, | 40 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, |
36 DWORD* disposition, REGSAM access); | 41 DWORD* disposition, REGSAM access); |
37 | 42 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 LONG WriteValue(const wchar_t* name, const wchar_t* in_value); | 118 LONG WriteValue(const wchar_t* name, const wchar_t* in_value); |
114 | 119 |
115 // Sets raw data, including type. | 120 // Sets raw data, including type. |
116 LONG WriteValue(const wchar_t* name, | 121 LONG WriteValue(const wchar_t* name, |
117 const void* data, | 122 const void* data, |
118 DWORD dsize, | 123 DWORD dsize, |
119 DWORD dtype); | 124 DWORD dtype); |
120 | 125 |
121 // Starts watching the key to see if any of its values have changed. | 126 // Starts watching the key to see if any of its values have changed. |
122 // The key must have been opened with the KEY_NOTIFY access privilege. | 127 // The key must have been opened with the KEY_NOTIFY access privilege. |
123 LONG StartWatching(); | 128 // Returns true on success. |
| 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); |
124 | 132 |
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_; } | |
136 HKEY Handle() const { return key_; } | 133 HKEY Handle() const { return key_; } |
137 | 134 |
138 private: | 135 private: |
| 136 class Watcher; |
| 137 |
139 // Calls RegDeleteKeyEx on supported platforms, alternatively falls back to | 138 // Calls RegDeleteKeyEx on supported platforms, alternatively falls back to |
140 // RegDeleteKey. | 139 // RegDeleteKey. |
141 static LONG RegDeleteKeyExWrapper(HKEY hKey, | 140 static LONG RegDeleteKeyExWrapper(HKEY hKey, |
142 const wchar_t* lpSubKey, | 141 const wchar_t* lpSubKey, |
143 REGSAM samDesired, | 142 REGSAM samDesired, |
144 DWORD Reserved); | 143 DWORD Reserved); |
145 | 144 |
146 // Recursively deletes a key and all of its subkeys. | 145 // Recursively deletes a key and all of its subkeys. |
147 static LONG RegDelRecurse(HKEY root_key, | 146 static LONG RegDelRecurse(HKEY root_key, |
148 const std::wstring& name, | 147 const std::wstring& name, |
149 REGSAM access); | 148 REGSAM access); |
| 149 |
150 HKEY key_; // The registry key being iterated. | 150 HKEY key_; // The registry key being iterated. |
151 HANDLE watch_event_; | |
152 REGSAM wow64access_; | 151 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 |