Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / base / win / registry.h
blob2ef759bf4cba5724b0caa2c1ab77f60f76f1aebc
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
3 // found in the LICENSE file.
5 #ifndef BASE_WIN_REGISTRY_H_
6 #define BASE_WIN_REGISTRY_H_
8 #include <windows.h>
9 #include <string>
10 #include <vector>
12 #include "base/base_export.h"
13 #include "base/basictypes.h"
14 #include "base/stl_util.h"
16 namespace base {
17 namespace win {
19 // Utility class to read, write and manipulate the Windows Registry.
20 // 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 // Note:
24 // ReadValue family of functions guarantee that the return arguments
25 // are not touched in case of failure.
26 class BASE_EXPORT RegKey {
27 public:
28 RegKey();
29 explicit RegKey(HKEY key);
30 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access);
31 ~RegKey();
33 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access);
35 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
36 DWORD* disposition, REGSAM access);
38 // Creates a subkey or open it if it already exists.
39 LONG CreateKey(const wchar_t* name, REGSAM access);
41 // Opens an existing reg key.
42 LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access);
44 // Opens an existing reg key, given the relative key name.
45 LONG OpenKey(const wchar_t* relative_key_name, REGSAM access);
47 // Closes this reg key.
48 void Close();
50 // Replaces the handle of the registry key and takes ownership of the handle.
51 void Set(HKEY key);
53 // Transfers ownership away from this object.
54 HKEY Take();
56 // Returns false if this key does not have the specified value, or if an error
57 // occurrs while attempting to access it.
58 bool HasValue(const wchar_t* value_name) const;
60 // Returns the number of values for this key, or 0 if the number cannot be
61 // determined.
62 DWORD GetValueCount() const;
64 // Determine the nth value's name.
65 LONG GetValueNameAt(int index, std::wstring* name) const;
67 // True while the key is valid.
68 bool Valid() const { return key_ != NULL; }
70 // Kill a key and everything that live below it; please be careful when using
71 // it.
72 LONG DeleteKey(const wchar_t* name);
74 // Deletes a single value within the key.
75 LONG DeleteValue(const wchar_t* name);
77 // Getters:
79 // Returns an int32 value. If |name| is NULL or empty, returns the default
80 // value, if any.
81 LONG ReadValueDW(const wchar_t* name, DWORD* out_value) const;
83 // Returns an int64 value. If |name| is NULL or empty, returns the default
84 // value, if any.
85 LONG ReadInt64(const wchar_t* name, int64* out_value) const;
87 // Returns a string value. If |name| is NULL or empty, returns the default
88 // value, if any.
89 LONG ReadValue(const wchar_t* name, std::wstring* out_value) const;
91 // Reads a REG_MULTI_SZ registry field into a vector of strings. Clears
92 // |values| initially and adds further strings to the list. Returns
93 // ERROR_CANTREAD if type is not REG_MULTI_SZ.
94 LONG ReadValues(const wchar_t* name, std::vector<std::wstring>* values);
96 // Returns raw data. If |name| is NULL or empty, returns the default
97 // value, if any.
98 LONG ReadValue(const wchar_t* name,
99 void* data,
100 DWORD* dsize,
101 DWORD* dtype) const;
103 // Setters:
105 // Sets an int32 value.
106 LONG WriteValue(const wchar_t* name, DWORD in_value);
108 // Sets a string value.
109 LONG WriteValue(const wchar_t* name, const wchar_t* in_value);
111 // Sets raw data, including type.
112 LONG WriteValue(const wchar_t* name,
113 const void* data,
114 DWORD dsize,
115 DWORD dtype);
117 // Starts watching the key to see if any of its values have changed.
118 // The key must have been opened with the KEY_NOTIFY access privilege.
119 LONG StartWatching();
121 // If StartWatching hasn't been called, always returns false.
122 // Otherwise, returns true if anything under the key has changed.
123 // This can't be const because the |watch_event_| may be refreshed.
124 bool HasChanged();
126 // Will automatically be called by destructor if not manually called
127 // beforehand. Returns true if it was watching, false otherwise.
128 LONG StopWatching();
130 inline bool IsWatching() const { return watch_event_ != 0; }
131 HANDLE watch_event() const { return watch_event_; }
132 HKEY Handle() const { return key_; }
134 private:
135 HKEY key_; // The registry key being iterated.
136 HANDLE watch_event_;
138 DISALLOW_COPY_AND_ASSIGN(RegKey);
141 // Iterates the entries found in a particular folder on the registry.
142 class BASE_EXPORT RegistryValueIterator {
143 public:
144 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key);
146 ~RegistryValueIterator();
148 DWORD ValueCount() const;
150 // True while the iterator is valid.
151 bool Valid() const;
153 // Advances to the next registry entry.
154 void operator++();
156 const wchar_t* Name() const { return name_.c_str(); }
157 const wchar_t* Value() const { return vector_as_array(&value_); }
158 // ValueSize() is in bytes.
159 DWORD ValueSize() const { return value_size_; }
160 DWORD Type() const { return type_; }
162 int Index() const { return index_; }
164 private:
165 // Read in the current values.
166 bool Read();
168 // The registry key being iterated.
169 HKEY key_;
171 // Current index of the iteration.
172 int index_;
174 // Current values.
175 std::wstring name_;
176 std::vector<wchar_t> value_;
177 DWORD value_size_;
178 DWORD type_;
180 DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator);
183 class BASE_EXPORT RegistryKeyIterator {
184 public:
185 RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key);
187 ~RegistryKeyIterator();
189 DWORD SubkeyCount() const;
191 // True while the iterator is valid.
192 bool Valid() const;
194 // Advances to the next entry in the folder.
195 void operator++();
197 const wchar_t* Name() const { return name_; }
199 int Index() const { return index_; }
201 private:
202 // Read in the current values.
203 bool Read();
205 // The registry key being iterated.
206 HKEY key_;
208 // Current index of the iteration.
209 int index_;
211 wchar_t name_[MAX_PATH];
213 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator);
216 } // namespace win
217 } // namespace base
219 #endif // BASE_WIN_REGISTRY_H_