user_settings_win.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2021 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.
  4. #include "remoting/base/user_settings_win.h"
  5. #include "base/logging.h"
  6. #include "base/notreached.h"
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "base/win/windows_types.h"
  9. namespace remoting {
  10. namespace {
  11. #if defined(OFFICIAL_BUILD)
  12. static constexpr wchar_t kUserSettingsKeyName[] =
  13. L"SOFTWARE\\Google\\Chrome Remote Desktop\\UserSettings";
  14. #else
  15. static constexpr wchar_t kUserSettingsKeyName[] =
  16. L"SOFTWARE\\Chromoting\\UserSettings";
  17. #endif
  18. } // namespace
  19. UserSettingsWin::UserSettingsWin() {
  20. LONG result;
  21. result = settings_root_key_.Create(HKEY_CURRENT_USER, kUserSettingsKeyName,
  22. KEY_READ | KEY_WRITE);
  23. if (result != ERROR_SUCCESS) {
  24. LOG(DFATAL) << "Failed to create/open key HKCU\\" << kUserSettingsKeyName
  25. << ", result: " << result;
  26. }
  27. }
  28. UserSettingsWin::~UserSettingsWin() = default;
  29. std::string UserSettingsWin::GetString(const UserSettingKey key) const {
  30. if (!settings_root_key_.Valid()) {
  31. LOG(ERROR) << "Can't get value for " << key
  32. << " since registry key is invalid.";
  33. return std::string();
  34. }
  35. std::wstring wide_key = base::UTF8ToWide(key);
  36. std::wstring wide_value;
  37. LONG result = settings_root_key_.ReadValue(wide_key.c_str(), &wide_value);
  38. if (result == ERROR_FILE_NOT_FOUND) {
  39. // No setting for the given key.
  40. return std::string();
  41. }
  42. if (result != ERROR_SUCCESS) {
  43. LOG(ERROR) << "Failed to read value for " << key << ", result: " << result;
  44. return std::string();
  45. }
  46. return base::WideToUTF8(wide_value);
  47. }
  48. void UserSettingsWin::SetString(const UserSettingKey key,
  49. const std::string& value) {
  50. if (!settings_root_key_.Valid()) {
  51. LOG(ERROR) << "Can't set value for " << key
  52. << " since registry key is invalid.";
  53. return;
  54. }
  55. std::wstring wide_key = base::UTF8ToWide(key);
  56. std::wstring wide_value = base::UTF8ToWide(value);
  57. LONG result =
  58. settings_root_key_.WriteValue(wide_key.c_str(), wide_value.c_str());
  59. if (result != ERROR_SUCCESS) {
  60. LOG(ERROR) << "Failed to write value " << value << " to key " << key
  61. << ", result: " << result;
  62. }
  63. }
  64. } // namespace remoting