1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/win/registry.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,219 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_WIN_REGISTRY_H_ 1.9 +#define BASE_WIN_REGISTRY_H_ 1.10 + 1.11 +#include <windows.h> 1.12 +#include <string> 1.13 +#include <vector> 1.14 + 1.15 +#include "base/base_export.h" 1.16 +#include "base/basictypes.h" 1.17 +#include "base/stl_util.h" 1.18 + 1.19 +namespace base { 1.20 +namespace win { 1.21 + 1.22 +// Utility class to read, write and manipulate the Windows Registry. 1.23 +// Registry vocabulary primer: a "key" is like a folder, in which there 1.24 +// are "values", which are <name, data> pairs, with an associated data type. 1.25 +// 1.26 +// Note: 1.27 +// ReadValue family of functions guarantee that the return arguments 1.28 +// are not touched in case of failure. 1.29 +class BASE_EXPORT RegKey { 1.30 + public: 1.31 + RegKey(); 1.32 + explicit RegKey(HKEY key); 1.33 + RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access); 1.34 + ~RegKey(); 1.35 + 1.36 + LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access); 1.37 + 1.38 + LONG CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, 1.39 + DWORD* disposition, REGSAM access); 1.40 + 1.41 + // Creates a subkey or open it if it already exists. 1.42 + LONG CreateKey(const wchar_t* name, REGSAM access); 1.43 + 1.44 + // Opens an existing reg key. 1.45 + LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access); 1.46 + 1.47 + // Opens an existing reg key, given the relative key name. 1.48 + LONG OpenKey(const wchar_t* relative_key_name, REGSAM access); 1.49 + 1.50 + // Closes this reg key. 1.51 + void Close(); 1.52 + 1.53 + // Replaces the handle of the registry key and takes ownership of the handle. 1.54 + void Set(HKEY key); 1.55 + 1.56 + // Transfers ownership away from this object. 1.57 + HKEY Take(); 1.58 + 1.59 + // Returns false if this key does not have the specified value, of if an error 1.60 + // occurrs while attempting to access it. 1.61 + bool HasValue(const wchar_t* value_name) const; 1.62 + 1.63 + // Returns the number of values for this key, of 0 if the number cannot be 1.64 + // determined. 1.65 + DWORD GetValueCount() const; 1.66 + 1.67 + // Determine the nth value's name. 1.68 + LONG GetValueNameAt(int index, std::wstring* name) const; 1.69 + 1.70 + // True while the key is valid. 1.71 + bool Valid() const { return key_ != NULL; } 1.72 + 1.73 + // Kill a key and everything that live below it; please be careful when using 1.74 + // it. 1.75 + LONG DeleteKey(const wchar_t* name); 1.76 + 1.77 + // Deletes a single value within the key. 1.78 + LONG DeleteValue(const wchar_t* name); 1.79 + 1.80 + // Getters: 1.81 + 1.82 + // Returns an int32 value. If |name| is NULL or empty, returns the default 1.83 + // value, if any. 1.84 + LONG ReadValueDW(const wchar_t* name, DWORD* out_value) const; 1.85 + 1.86 + // Returns an int64 value. If |name| is NULL or empty, returns the default 1.87 + // value, if any. 1.88 + LONG ReadInt64(const wchar_t* name, int64* out_value) const; 1.89 + 1.90 + // Returns a string value. If |name| is NULL or empty, returns the default 1.91 + // value, if any. 1.92 + LONG ReadValue(const wchar_t* name, std::wstring* out_value) const; 1.93 + 1.94 + // Reads a REG_MULTI_SZ registry field into a vector of strings. Clears 1.95 + // |values| initially and adds further strings to the list. Returns 1.96 + // ERROR_CANTREAD if type is not REG_MULTI_SZ. 1.97 + LONG ReadValues(const wchar_t* name, std::vector<std::wstring>* values); 1.98 + 1.99 + // Returns raw data. If |name| is NULL or empty, returns the default 1.100 + // value, if any. 1.101 + LONG ReadValue(const wchar_t* name, 1.102 + void* data, 1.103 + DWORD* dsize, 1.104 + DWORD* dtype) const; 1.105 + 1.106 + // Setters: 1.107 + 1.108 + // Sets an int32 value. 1.109 + LONG WriteValue(const wchar_t* name, DWORD in_value); 1.110 + 1.111 + // Sets a string value. 1.112 + LONG WriteValue(const wchar_t* name, const wchar_t* in_value); 1.113 + 1.114 + // Sets raw data, including type. 1.115 + LONG WriteValue(const wchar_t* name, 1.116 + const void* data, 1.117 + DWORD dsize, 1.118 + DWORD dtype); 1.119 + 1.120 + // Starts watching the key to see if any of its values have changed. 1.121 + // The key must have been opened with the KEY_NOTIFY access privilege. 1.122 + LONG StartWatching(); 1.123 + 1.124 + // If StartWatching hasn't been called, always returns false. 1.125 + // Otherwise, returns true if anything under the key has changed. 1.126 + // This can't be const because the |watch_event_| may be refreshed. 1.127 + bool HasChanged(); 1.128 + 1.129 + // Will automatically be called by destructor if not manually called 1.130 + // beforehand. Returns true if it was watching, false otherwise. 1.131 + LONG StopWatching(); 1.132 + 1.133 + inline bool IsWatching() const { return watch_event_ != 0; } 1.134 + HANDLE watch_event() const { return watch_event_; } 1.135 + HKEY Handle() const { return key_; } 1.136 + 1.137 + private: 1.138 + HKEY key_; // The registry key being iterated. 1.139 + HANDLE watch_event_; 1.140 + 1.141 + DISALLOW_COPY_AND_ASSIGN(RegKey); 1.142 +}; 1.143 + 1.144 +// Iterates the entries found in a particular folder on the registry. 1.145 +class BASE_EXPORT RegistryValueIterator { 1.146 + public: 1.147 + RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); 1.148 + 1.149 + ~RegistryValueIterator(); 1.150 + 1.151 + DWORD ValueCount() const; 1.152 + 1.153 + // True while the iterator is valid. 1.154 + bool Valid() const; 1.155 + 1.156 + // Advances to the next registry entry. 1.157 + void operator++(); 1.158 + 1.159 + const wchar_t* Name() const { return name_.c_str(); } 1.160 + const wchar_t* Value() const { return vector_as_array(&value_); } 1.161 + // ValueSize() is in bytes. 1.162 + DWORD ValueSize() const { return value_size_; } 1.163 + DWORD Type() const { return type_; } 1.164 + 1.165 + int Index() const { return index_; } 1.166 + 1.167 + private: 1.168 + // Read in the current values. 1.169 + bool Read(); 1.170 + 1.171 + // The registry key being iterated. 1.172 + HKEY key_; 1.173 + 1.174 + // Current index of the iteration. 1.175 + int index_; 1.176 + 1.177 + // Current values. 1.178 + std::wstring name_; 1.179 + std::vector<wchar_t> value_; 1.180 + DWORD value_size_; 1.181 + DWORD type_; 1.182 + 1.183 + DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator); 1.184 +}; 1.185 + 1.186 +class BASE_EXPORT RegistryKeyIterator { 1.187 + public: 1.188 + RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key); 1.189 + 1.190 + ~RegistryKeyIterator(); 1.191 + 1.192 + DWORD SubkeyCount() const; 1.193 + 1.194 + // True while the iterator is valid. 1.195 + bool Valid() const; 1.196 + 1.197 + // Advances to the next entry in the folder. 1.198 + void operator++(); 1.199 + 1.200 + const wchar_t* Name() const { return name_; } 1.201 + 1.202 + int Index() const { return index_; } 1.203 + 1.204 + private: 1.205 + // Read in the current values. 1.206 + bool Read(); 1.207 + 1.208 + // The registry key being iterated. 1.209 + HKEY key_; 1.210 + 1.211 + // Current index of the iteration. 1.212 + int index_; 1.213 + 1.214 + wchar_t name_[MAX_PATH]; 1.215 + 1.216 + DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); 1.217 +}; 1.218 + 1.219 +} // namespace win 1.220 +} // namespace base 1.221 + 1.222 +#endif // BASE_WIN_REGISTRY_H_