ipc/chromium/src/base/registry.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/registry.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,417 @@
     1.4 +// Copyright (c) 2006-2008 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 +// All Rights Reserved.
     1.8 +
     1.9 +#include "base/registry.h"
    1.10 +
    1.11 +#include <assert.h>
    1.12 +#include <shlwapi.h>
    1.13 +#include <windows.h>
    1.14 +
    1.15 +#pragma comment(lib, "shlwapi.lib")  // for SHDeleteKey
    1.16 +
    1.17 +// local types (see the same declarations in the header file)
    1.18 +#define tchar TCHAR
    1.19 +#define CTP const tchar*
    1.20 +#define tstr std::basic_string<tchar>
    1.21 +
    1.22 +//
    1.23 +// RegistryValueIterator
    1.24 +//
    1.25 +
    1.26 +RegistryValueIterator::RegistryValueIterator(HKEY root_key,
    1.27 +                                             LPCTSTR folder_key) {
    1.28 +  LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_);
    1.29 +  if (result != ERROR_SUCCESS) {
    1.30 +    key_ = NULL;
    1.31 +  } else {
    1.32 +    DWORD count = 0;
    1.33 +    result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count,
    1.34 +                               NULL, NULL, NULL, NULL);
    1.35 +
    1.36 +    if (result != ERROR_SUCCESS) {
    1.37 +      ::RegCloseKey(key_);
    1.38 +      key_ = NULL;
    1.39 +    } else {
    1.40 +      index_ = count - 1;
    1.41 +    }
    1.42 +  }
    1.43 +
    1.44 +  Read();
    1.45 +}
    1.46 +
    1.47 +RegistryValueIterator::~RegistryValueIterator() {
    1.48 +  if (key_)
    1.49 +    ::RegCloseKey(key_);
    1.50 +}
    1.51 +
    1.52 +bool RegistryValueIterator::Valid() const {
    1.53 +  // true while the iterator is valid
    1.54 +  return key_ != NULL && index_ >= 0;
    1.55 +}
    1.56 +
    1.57 +void RegistryValueIterator::operator ++ () {
    1.58 +  // advance to the next entry in the folder
    1.59 +  --index_;
    1.60 +  Read();
    1.61 +}
    1.62 +
    1.63 +bool RegistryValueIterator::Read() {
    1.64 +  if (Valid()) {
    1.65 +    DWORD ncount = sizeof(name_)/sizeof(*name_);
    1.66 +    value_size_ = sizeof(value_);
    1.67 +    LRESULT r = ::RegEnumValue(key_, index_, name_, &ncount, NULL, &type_,
    1.68 +                               reinterpret_cast<BYTE*>(value_), &value_size_);
    1.69 +    if (ERROR_SUCCESS == r)
    1.70 +      return true;
    1.71 +  }
    1.72 +
    1.73 +  name_[0] = '\0';
    1.74 +  value_[0] = '\0';
    1.75 +  value_size_ = 0;
    1.76 +  return false;
    1.77 +}
    1.78 +
    1.79 +DWORD RegistryValueIterator::ValueCount() const {
    1.80 +
    1.81 +  DWORD count = 0;
    1.82 +  HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL,
    1.83 +                                     &count, NULL, NULL, NULL, NULL);
    1.84 +
    1.85 +  if (result != ERROR_SUCCESS)
    1.86 +    return 0;
    1.87 +
    1.88 +  return count;
    1.89 +}
    1.90 +
    1.91 +//
    1.92 +// RegistryKeyIterator
    1.93 +//
    1.94 +
    1.95 +RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
    1.96 +                                         LPCTSTR folder_key) {
    1.97 +  LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_);
    1.98 +  if (result != ERROR_SUCCESS) {
    1.99 +    key_ = NULL;
   1.100 +  } else {
   1.101 +    DWORD count = 0;
   1.102 +    HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL,
   1.103 +                                       NULL, NULL, NULL, NULL, NULL);
   1.104 +
   1.105 +    if (result != ERROR_SUCCESS) {
   1.106 +      ::RegCloseKey(key_);
   1.107 +      key_ = NULL;
   1.108 +    } else {
   1.109 +      index_ = count - 1;
   1.110 +    }
   1.111 +  }
   1.112 +
   1.113 +  Read();
   1.114 +}
   1.115 +
   1.116 +RegistryKeyIterator::~RegistryKeyIterator() {
   1.117 +  if (key_)
   1.118 +    ::RegCloseKey(key_);
   1.119 +}
   1.120 +
   1.121 +bool RegistryKeyIterator::Valid() const {
   1.122 +  // true while the iterator is valid
   1.123 +  return key_ != NULL && index_ >= 0;
   1.124 +}
   1.125 +
   1.126 +void RegistryKeyIterator::operator ++ () {
   1.127 +  // advance to the next entry in the folder
   1.128 +  --index_;
   1.129 +  Read();
   1.130 +}
   1.131 +
   1.132 +bool RegistryKeyIterator::Read() {
   1.133 +  if (Valid()) {
   1.134 +    DWORD ncount = sizeof(name_)/sizeof(*name_);
   1.135 +    FILETIME written;
   1.136 +    LRESULT r = ::RegEnumKeyEx(key_, index_, name_, &ncount, NULL, NULL,
   1.137 +                               NULL, &written);
   1.138 +    if (ERROR_SUCCESS == r)
   1.139 +      return true;
   1.140 +  }
   1.141 +
   1.142 +  name_[0] = '\0';
   1.143 +  return false;
   1.144 +}
   1.145 +
   1.146 +DWORD RegistryKeyIterator::SubkeyCount() const {
   1.147 +
   1.148 +  DWORD count = 0;
   1.149 +  HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL,
   1.150 +                                     NULL, NULL, NULL, NULL, NULL);
   1.151 +
   1.152 +  if (result != ERROR_SUCCESS)
   1.153 +    return 0;
   1.154 +
   1.155 +  return count;
   1.156 +}
   1.157 +
   1.158 +//
   1.159 +// RegKey
   1.160 +//
   1.161 +
   1.162 +RegKey::RegKey(HKEY rootkey, const tchar* subkey, REGSAM access)
   1.163 +  : key_(NULL), watch_event_(0) {
   1.164 +  if (rootkey) {
   1.165 +    if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
   1.166 +      this->Create(rootkey, subkey, access);
   1.167 +    else
   1.168 +      this->Open(rootkey, subkey, access);
   1.169 +  }
   1.170 +  else assert(!subkey);
   1.171 +}
   1.172 +
   1.173 +void RegKey::Close() {
   1.174 +  StopWatching();
   1.175 +  if (key_) {
   1.176 +    ::RegCloseKey(key_);
   1.177 +    key_ = NULL;
   1.178 +  }
   1.179 +}
   1.180 +
   1.181 +bool RegKey::Create(HKEY rootkey, const tchar* subkey, REGSAM access) {
   1.182 +  DWORD disposition_value;
   1.183 +  return CreateWithDisposition(rootkey, subkey, &disposition_value, access);
   1.184 +}
   1.185 +
   1.186 +bool RegKey::CreateWithDisposition(HKEY rootkey, const tchar* subkey,
   1.187 +                                   DWORD* disposition, REGSAM access) {
   1.188 +  assert(rootkey && subkey && access && disposition);
   1.189 +  this->Close();
   1.190 +
   1.191 +  LONG const result = RegCreateKeyEx(rootkey,
   1.192 +                                     subkey,
   1.193 +                                     0,
   1.194 +                                     NULL,
   1.195 +                                     REG_OPTION_NON_VOLATILE,
   1.196 +                                     access,
   1.197 +                                     NULL,
   1.198 +                                     &key_,
   1.199 +                                     disposition );
   1.200 +  if (result != ERROR_SUCCESS) {
   1.201 +    key_ = NULL;
   1.202 +    return false;
   1.203 +  }
   1.204 +  else return true;
   1.205 +}
   1.206 +
   1.207 +bool RegKey::Open(HKEY rootkey, const tchar* subkey, REGSAM access) {
   1.208 +  assert(rootkey && subkey && access);
   1.209 +  this->Close();
   1.210 +
   1.211 +  LONG const result = RegOpenKeyEx(rootkey, subkey, 0,
   1.212 +                                   access, &key_ );
   1.213 +  if (result != ERROR_SUCCESS) {
   1.214 +    key_ = NULL;
   1.215 +    return false;
   1.216 +  }
   1.217 +  else return true;
   1.218 +}
   1.219 +
   1.220 +bool RegKey::CreateKey(const tchar* name, REGSAM access) {
   1.221 +  assert(name && access);
   1.222 +
   1.223 +  HKEY subkey = NULL;
   1.224 +  LONG const result = RegCreateKeyEx(key_, name, 0, NULL,
   1.225 +                                     REG_OPTION_NON_VOLATILE,
   1.226 +                                     access, NULL, &subkey, NULL);
   1.227 +  this->Close();
   1.228 +
   1.229 +  key_ = subkey;
   1.230 +  return (result == ERROR_SUCCESS);
   1.231 +}
   1.232 +
   1.233 +bool RegKey::OpenKey(const tchar* name, REGSAM access) {
   1.234 +  assert(name && access);
   1.235 +
   1.236 +  HKEY subkey = NULL;
   1.237 +  LONG const result = RegOpenKeyEx(key_, name, 0, access, &subkey);
   1.238 +
   1.239 +  this->Close();
   1.240 +
   1.241 +  key_ = subkey;
   1.242 +  return (result == ERROR_SUCCESS);
   1.243 +}
   1.244 +
   1.245 +DWORD RegKey::ValueCount() {
   1.246 +  DWORD count = 0;
   1.247 +  HRESULT const result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL,
   1.248 +                                     NULL, &count, NULL, NULL, NULL, NULL);
   1.249 +  return (result != ERROR_SUCCESS) ? 0 : count;
   1.250 +}
   1.251 +
   1.252 +bool RegKey::ReadName(int index, tstr* name) {
   1.253 +  tchar buf[256];
   1.254 +  DWORD bufsize = sizeof(buf)/sizeof(*buf);
   1.255 +  LRESULT r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL,
   1.256 +                             NULL, NULL);
   1.257 +  if (r != ERROR_SUCCESS)
   1.258 +    return false;
   1.259 +  if (name)
   1.260 +    *name = buf;
   1.261 +  return true;
   1.262 +}
   1.263 +
   1.264 +bool RegKey::ValueExists(const tchar* name) {
   1.265 +  if (!key_) return false;
   1.266 +  const HRESULT result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL);
   1.267 +  return (result == ERROR_SUCCESS);
   1.268 +}
   1.269 +
   1.270 +bool RegKey::ReadValue(const tchar* name, void* data,
   1.271 +                       DWORD* dsize, DWORD* dtype) {
   1.272 +  if (!key_) return false;
   1.273 +  HRESULT const result = RegQueryValueEx(key_, name, 0, dtype,
   1.274 +                                         reinterpret_cast<LPBYTE>(data),
   1.275 +                                         dsize);
   1.276 +  return (result == ERROR_SUCCESS);
   1.277 +}
   1.278 +
   1.279 +bool RegKey::ReadValue(const tchar* name, tstr * value) {
   1.280 +  assert(value);
   1.281 +  static const size_t kMaxStringLength = 1024;  // This is after expansion.
   1.282 +  // Use the one of the other forms of ReadValue if 1024 is too small for you.
   1.283 +  TCHAR raw_value[kMaxStringLength];
   1.284 +  DWORD type = REG_SZ, size = sizeof(raw_value);
   1.285 +  if (this->ReadValue(name, raw_value, &size, &type)) {
   1.286 +    if (type == REG_SZ) {
   1.287 +      *value = raw_value;
   1.288 +    } else if (type == REG_EXPAND_SZ) {
   1.289 +      TCHAR expanded[kMaxStringLength];
   1.290 +      size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength);
   1.291 +      // Success: returns the number of TCHARs copied
   1.292 +      // Fail: buffer too small, returns the size required
   1.293 +      // Fail: other, returns 0
   1.294 +      if (size == 0 || size > kMaxStringLength)
   1.295 +        return false;
   1.296 +      *value = expanded;
   1.297 +    } else {
   1.298 +      // Not a string. Oops.
   1.299 +      return false;
   1.300 +    }
   1.301 +    return true;
   1.302 +  }
   1.303 +  else return false;
   1.304 +}
   1.305 +
   1.306 +bool RegKey::ReadValueDW(const tchar* name, DWORD * value) {
   1.307 +  assert(value);
   1.308 +  DWORD type = REG_DWORD, size = sizeof(DWORD), result = 0;
   1.309 +  if (this->ReadValue(name, &result, &size, &type)
   1.310 +     && (type == REG_DWORD || type == REG_BINARY)
   1.311 +     && size == sizeof(DWORD)) {
   1.312 +    *value = result;
   1.313 +    return true;
   1.314 +  }
   1.315 +  else return false;
   1.316 +}
   1.317 +
   1.318 +bool RegKey::WriteValue(const tchar* name,
   1.319 +                        const void * data,
   1.320 +                        DWORD dsize,
   1.321 +                        DWORD dtype) {
   1.322 +  assert(data);
   1.323 +  if (!key_) return false;
   1.324 +  HRESULT const result = RegSetValueEx(
   1.325 +      key_,
   1.326 +      name,
   1.327 +      0,
   1.328 +      dtype,
   1.329 +      reinterpret_cast<LPBYTE>(const_cast<void*>(data)),
   1.330 +      dsize);
   1.331 +  return (result == ERROR_SUCCESS);
   1.332 +}
   1.333 +
   1.334 +bool RegKey::WriteValue(const tchar * name, const tchar * value) {
   1.335 +  return this->WriteValue(name, value,
   1.336 +    static_cast<DWORD>(sizeof(*value) * (_tcslen(value) + 1)), REG_SZ);
   1.337 +}
   1.338 +
   1.339 +bool RegKey::WriteValue(const tchar * name, DWORD value) {
   1.340 +  return this->WriteValue(name, &value,
   1.341 +    static_cast<DWORD>(sizeof(value)), REG_DWORD);
   1.342 +}
   1.343 +
   1.344 +bool RegKey::DeleteKey(const tchar * name) {
   1.345 +  if (!key_) return false;
   1.346 +  return (ERROR_SUCCESS == SHDeleteKey(key_, name));
   1.347 +}
   1.348 +
   1.349 +
   1.350 +bool RegKey::DeleteValue(const tchar * value_name) {
   1.351 +  assert(value_name);
   1.352 +  HRESULT const result = RegDeleteValue(key_, value_name);
   1.353 +  return (result == ERROR_SUCCESS);
   1.354 +}
   1.355 +
   1.356 +bool RegKey::StartWatching() {
   1.357 +  if (!watch_event_)
   1.358 +    watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL);
   1.359 +
   1.360 +  DWORD filter = REG_NOTIFY_CHANGE_NAME |
   1.361 +                 REG_NOTIFY_CHANGE_ATTRIBUTES |
   1.362 +                 REG_NOTIFY_CHANGE_LAST_SET |
   1.363 +                 REG_NOTIFY_CHANGE_SECURITY;
   1.364 +
   1.365 +  // Watch the registry key for a change of value.
   1.366 +  HRESULT result = RegNotifyChangeKeyValue(key_, TRUE, filter,
   1.367 +                                           watch_event_, TRUE);
   1.368 +  if (SUCCEEDED(result)) {
   1.369 +    return true;
   1.370 +  } else {
   1.371 +    CloseHandle(watch_event_);
   1.372 +    watch_event_ = 0;
   1.373 +    return false;
   1.374 +  }
   1.375 +}
   1.376 +
   1.377 +bool RegKey::StopWatching() {
   1.378 +  if (watch_event_) {
   1.379 +    CloseHandle(watch_event_);
   1.380 +    watch_event_ = 0;
   1.381 +    return true;
   1.382 +  }
   1.383 +  return false;
   1.384 +}
   1.385 +
   1.386 +bool RegKey::HasChanged() {
   1.387 +  if (watch_event_) {
   1.388 +    if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) {
   1.389 +      StartWatching();
   1.390 +      return true;
   1.391 +    }
   1.392 +  }
   1.393 +  return false;
   1.394 +}
   1.395 +
   1.396 +// Register a COM object with the most usual properties.
   1.397 +bool RegisterCOMServer(const tchar* guid,
   1.398 +                       const tchar* name,
   1.399 +                       const tchar* path) {
   1.400 +  RegKey key(HKEY_CLASSES_ROOT, _T("CLSID"), KEY_WRITE);
   1.401 +  key.CreateKey(guid, KEY_WRITE);
   1.402 +  key.WriteValue(NULL, name);
   1.403 +  key.CreateKey(_T("InprocServer32"), KEY_WRITE);
   1.404 +  key.WriteValue(NULL, path);
   1.405 +  key.WriteValue(_T("ThreadingModel"), _T("Apartment"));
   1.406 +  return true;
   1.407 +}
   1.408 +
   1.409 +bool RegisterCOMServer(const tchar* guid, const tchar* name, HINSTANCE module) {
   1.410 +  tchar module_path[MAX_PATH];
   1.411 +  ::GetModuleFileName(module, module_path, MAX_PATH);
   1.412 +  _tcslwr_s(module_path, MAX_PATH);
   1.413 +  return RegisterCOMServer(guid, name, module_path);
   1.414 +}
   1.415 +
   1.416 +bool UnregisterCOMServer(const tchar* guid) {
   1.417 +  RegKey key(HKEY_CLASSES_ROOT, _T("CLSID"), KEY_WRITE);
   1.418 +  key.DeleteKey(guid);
   1.419 +  return true;
   1.420 +}

mercurial