| 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 |
| 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 WatcherDelegate; |
| 137 |
| 138 // Internal implementation of StartWatching. |
| 139 bool StartWatchingInternal(); |
| 140 |
| 139 // Calls RegDeleteKeyEx on supported platforms, alternatively falls back to | 141 // Calls RegDeleteKeyEx on supported platforms, alternatively falls back to |
| 140 // RegDeleteKey. | 142 // RegDeleteKey. |
| 141 static LONG RegDeleteKeyExWrapper(HKEY hKey, | 143 static LONG RegDeleteKeyExWrapper(HKEY hKey, |
| 142 const wchar_t* lpSubKey, | 144 const wchar_t* lpSubKey, |
| 143 REGSAM samDesired, | 145 REGSAM samDesired, |
| 144 DWORD Reserved); | 146 DWORD Reserved); |
| 145 | 147 |
| 146 // Recursively deletes a key and all of its subkeys. | 148 // Recursively deletes a key and all of its subkeys. |
| 147 static LONG RegDelRecurse(HKEY root_key, | 149 static LONG RegDelRecurse(HKEY root_key, |
| 148 const std::wstring& name, | 150 const std::wstring& name, |
| 149 REGSAM access); | 151 REGSAM access); |
| 152 |
| 153 // objectWatcher::Delegate notification. |
| 154 void OnObjectSignaled(HANDLE object); |
| 155 |
| 150 HKEY key_; // The registry key being iterated. | 156 HKEY key_; // The registry key being iterated. |
| 151 HANDLE watch_event_; | 157 ScopedHandle watch_event_; |
| 152 REGSAM wow64access_; | 158 REGSAM wow64access_; |
| 159 ObjectWatcher object_watcher_; |
| 160 scoped_ptr<WatcherDelegate> watcher_delegate_; |
| 161 ChangeCallback callback_; |
| 153 | 162 |
| 154 DISALLOW_COPY_AND_ASSIGN(RegKey); | 163 DISALLOW_COPY_AND_ASSIGN(RegKey); |
| 155 }; | 164 }; |
| 156 | 165 |
| 157 // Iterates the entries found in a particular folder on the registry. | 166 // Iterates the entries found in a particular folder on the registry. |
| 158 class BASE_EXPORT RegistryValueIterator { | 167 class BASE_EXPORT RegistryValueIterator { |
| 159 public: | 168 public: |
| 160 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); | 169 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); |
| 161 | 170 |
| 162 ~RegistryValueIterator(); | 171 ~RegistryValueIterator(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 235 |
| 227 wchar_t name_[MAX_PATH]; | 236 wchar_t name_[MAX_PATH]; |
| 228 | 237 |
| 229 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); | 238 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); |
| 230 }; | 239 }; |
| 231 | 240 |
| 232 } // namespace win | 241 } // namespace win |
| 233 } // namespace base | 242 } // namespace base |
| 234 | 243 |
| 235 #endif // BASE_WIN_REGISTRY_H_ | 244 #endif // BASE_WIN_REGISTRY_H_ |
| OLD | NEW |