security/sandbox/chromium/base/win/registry.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial