Stop linking tcmalloc into shared library components.
[chromium-blink-merge.git] / base / win / registry.h
blob7a3d97042896c8b36f2d6e381f8cd04152ecb07b
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 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access);
30 ~RegKey();
32 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access);
34 LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
35 DWORD* disposition, REGSAM access);
37 // Creates a subkey or open it if it already exists.
38 LONG CreateKey(const wchar_t* name, REGSAM access);
40 // Opens an existing reg key.
41 LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access);
43 // Opens an existing reg key, given the relative key name.
44 LONG OpenKey(const wchar_t* relative_key_name, REGSAM access);
46 // Closes this reg key.
47 void Close();
49 // Returns false if this key does not have the specified value, of if an error
50 // occurrs while attempting to access it.
51 bool HasValue(const wchar_t* value_name) const;
53 // Returns the number of values for this key, of 0 if the number cannot be
54 // determined.
55 DWORD GetValueCount() const;
57 // Determine the nth value's name.
58 LONG GetValueNameAt(int index, std::wstring* name) const;
60 // True while the key is valid.
61 bool Valid() const { return key_ != NULL; }
63 // Kill a key and everything that live below it; please be careful when using
64 // it.
65 LONG DeleteKey(const wchar_t* name);
67 // Deletes a single value within the key.
68 LONG DeleteValue(const wchar_t* name);
70 // Getters:
72 // Returns an int32 value. If |name| is NULL or empty, returns the default
73 // value, if any.
74 LONG ReadValueDW(const wchar_t* name, DWORD* out_value) const;
76 // Returns an int64 value. If |name| is NULL or empty, returns the default
77 // value, if any.
78 LONG ReadInt64(const wchar_t* name, int64* out_value) const;
80 // Returns a string value. If |name| is NULL or empty, returns the default
81 // value, if any.
82 LONG ReadValue(const wchar_t* name, std::wstring* out_value) const;
84 // Reads a REG_MULTI_SZ registry field into a vector of strings. Clears
85 // |values| initially and adds further strings to the list. Returns
86 // ERROR_CANTREAD if type is not REG_MULTI_SZ.
87 LONG ReadValues(const wchar_t* name, std::vector<std::wstring>* values);
89 // Returns raw data. If |name| is NULL or empty, returns the default
90 // value, if any.
91 LONG ReadValue(const wchar_t* name,
92 void* data,
93 DWORD* dsize,
94 DWORD* dtype) const;
96 // Setters:
98 // Sets an int32 value.
99 LONG WriteValue(const wchar_t* name, DWORD in_value);
101 // Sets a string value.
102 LONG WriteValue(const wchar_t* name, const wchar_t* in_value);
104 // Sets raw data, including type.
105 LONG WriteValue(const wchar_t* name,
106 const void* data,
107 DWORD dsize,
108 DWORD dtype);
110 // Starts watching the key to see if any of its values have changed.
111 // The key must have been opened with the KEY_NOTIFY access privilege.
112 LONG StartWatching();
114 // If StartWatching hasn't been called, always returns false.
115 // Otherwise, returns true if anything under the key has changed.
116 // This can't be const because the |watch_event_| may be refreshed.
117 bool HasChanged();
119 // Will automatically be called by destructor if not manually called
120 // beforehand. Returns true if it was watching, false otherwise.
121 LONG StopWatching();
123 inline bool IsWatching() const { return watch_event_ != 0; }
124 HANDLE watch_event() const { return watch_event_; }
125 HKEY Handle() const { return key_; }
127 private:
128 HKEY key_; // The registry key being iterated.
129 HANDLE watch_event_;
131 DISALLOW_COPY_AND_ASSIGN(RegKey);
134 // Iterates the entries found in a particular folder on the registry.
135 class BASE_EXPORT RegistryValueIterator {
136 public:
137 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key);
139 ~RegistryValueIterator();
141 DWORD ValueCount() const;
143 // True while the iterator is valid.
144 bool Valid() const;
146 // Advances to the next registry entry.
147 void operator++();
149 const wchar_t* Name() const { return name_.c_str(); }
150 const wchar_t* Value() const { return vector_as_array(&value_); }
151 // ValueSize() is in bytes.
152 DWORD ValueSize() const { return value_size_; }
153 DWORD Type() const { return type_; }
155 int Index() const { return index_; }
157 private:
158 // Read in the current values.
159 bool Read();
161 // The registry key being iterated.
162 HKEY key_;
164 // Current index of the iteration.
165 int index_;
167 // Current values.
168 std::wstring name_;
169 std::vector<wchar_t> value_;
170 DWORD value_size_;
171 DWORD type_;
173 DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator);
176 class BASE_EXPORT RegistryKeyIterator {
177 public:
178 RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key);
180 ~RegistryKeyIterator();
182 DWORD SubkeyCount() const;
184 // True while the iterator is valid.
185 bool Valid() const;
187 // Advances to the next entry in the folder.
188 void operator++();
190 const wchar_t* Name() const { return name_; }
192 int Index() const { return index_; }
194 private:
195 // Read in the current values.
196 bool Read();
198 // The registry key being iterated.
199 HKEY key_;
201 // Current index of the iteration.
202 int index_;
204 wchar_t name_[MAX_PATH];
206 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator);
209 } // namespace win
210 } // namespace base
212 #endif // BASE_WIN_REGISTRY_H_