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. To continue watching the | |
eroman
2014/10/10 21:34:35
This comment seems more appropriate as part of Sta
rvargas (doing something else)
2014/10/10 22:22:05
Moved the last part to StartWatching() but I'm not
| |
31 // object, StartWatching must be called again. | |
32 typedef base::Callback<void()> ChangeCallback; | |
33 | |
28 RegKey(); | 34 RegKey(); |
29 explicit RegKey(HKEY key); | 35 explicit RegKey(HKEY key); |
30 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 36 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
31 ~RegKey(); | 37 ~RegKey(); |
32 | 38 |
33 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); | 39 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); |
34 | 40 |
35 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, | 41 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, |
36 DWORD* disposition, REGSAM access); | 42 DWORD* disposition, REGSAM access); |
37 | 43 |
(...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); | 119 LONG WriteValue(const wchar_t* name, const wchar_t* in_value); |
114 | 120 |
115 // Sets raw data, including type. | 121 // Sets raw data, including type. |
116 LONG WriteValue(const wchar_t* name, | 122 LONG WriteValue(const wchar_t* name, |
117 const void* data, | 123 const void* data, |
118 DWORD dsize, | 124 DWORD dsize, |
119 DWORD dtype); | 125 DWORD dtype); |
120 | 126 |
121 // Starts watching the key to see if any of its values have changed. | 127 // 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. | 128 // The key must have been opened with the KEY_NOTIFY access privilege. |
123 LONG StartWatching(); | 129 // Returns true on success. |
130 // To stop watching, delete this RegKey object. | |
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 // objectWatcher::Delegate notification. | |
151 void OnObjectSignaled(HANDLE object); | |
152 | |
150 HKEY key_; // The registry key being iterated. | 153 HKEY key_; // The registry key being iterated. |
151 HANDLE watch_event_; | |
152 REGSAM wow64access_; | 154 REGSAM wow64access_; |
155 scoped_ptr<Watcher> key_watcher_; | |
156 ChangeCallback callback_; | |
eroman
2014/10/10 21:34:35
I suggest keeping this as an internal detail of Wa
rvargas (doing something else)
2014/10/10 22:22:05
Done.
| |
153 | 157 |
154 DISALLOW_COPY_AND_ASSIGN(RegKey); | 158 DISALLOW_COPY_AND_ASSIGN(RegKey); |
155 }; | 159 }; |
156 | 160 |
157 // Iterates the entries found in a particular folder on the registry. | 161 // Iterates the entries found in a particular folder on the registry. |
158 class BASE_EXPORT RegistryValueIterator { | 162 class BASE_EXPORT RegistryValueIterator { |
159 public: | 163 public: |
160 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); | 164 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); |
161 | 165 |
162 ~RegistryValueIterator(); | 166 ~RegistryValueIterator(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 | 230 |
227 wchar_t name_[MAX_PATH]; | 231 wchar_t name_[MAX_PATH]; |
228 | 232 |
229 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); | 233 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); |
230 }; | 234 }; |
231 | 235 |
232 } // namespace win | 236 } // namespace win |
233 } // namespace base | 237 } // namespace base |
234 | 238 |
235 #endif // BASE_WIN_REGISTRY_H_ | 239 #endif // BASE_WIN_REGISTRY_H_ |
OLD | NEW |