host_settings_win.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/host_settings_win.h"
  5. #include <string>
  6. #include "base/compiler_specific.h"
  7. #include "base/logging.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "base/win/registry.h"
  10. #include "base/win/windows_types.h"
  11. #include "remoting/base/logging.h"
  12. namespace remoting {
  13. namespace {
  14. #if defined(OFFICIAL_BUILD)
  15. static constexpr wchar_t kHostSettingsKeyName[] =
  16. L"SOFTWARE\\Google\\Chrome Remote Desktop\\HostSettings";
  17. #else
  18. static constexpr wchar_t kHostSettingsKeyName[] =
  19. L"SOFTWARE\\Chromoting\\HostSettings";
  20. #endif
  21. } // namespace
  22. HostSettingsWin::HostSettingsWin() = default;
  23. HostSettingsWin::~HostSettingsWin() = default;
  24. void HostSettingsWin::InitializeInstance() {
  25. LONG result;
  26. result = settings_root_key_.Open(HKEY_LOCAL_MACHINE, kHostSettingsKeyName,
  27. KEY_READ | KEY_WRITE);
  28. if (result == ERROR_ACCESS_DENIED) {
  29. HOST_LOG << "Cannot open registry key with write permission. Trying again "
  30. << "with readonly permission instead.";
  31. result = settings_root_key_.Open(HKEY_LOCAL_MACHINE, kHostSettingsKeyName,
  32. KEY_READ);
  33. }
  34. if (result != ERROR_SUCCESS) {
  35. LOG(ERROR) << "Failed to open key HKLM\\" << kHostSettingsKeyName
  36. << ", result: " << result;
  37. }
  38. }
  39. std::string HostSettingsWin::GetString(const HostSettingKey key,
  40. const std::string& default_value) const {
  41. std::wstring value;
  42. std::wstring wide_key = base::UTF8ToWide(key);
  43. LONG result = settings_root_key_.ReadValue(wide_key.c_str(), &value);
  44. if (result == ERROR_FILE_NOT_FOUND) {
  45. return default_value;
  46. } else if (result != ERROR_SUCCESS) {
  47. LOG(ERROR) << "Failed to read value for " << key << ", result: " << result;
  48. return default_value;
  49. }
  50. return base::WideToUTF8(value);
  51. }
  52. void HostSettingsWin::SetString(const HostSettingKey key,
  53. const std::string& value) {
  54. std::wstring wide_key = base::UTF8ToWide(key);
  55. std::wstring wide_value = base::UTF8ToWide(value);
  56. LONG result =
  57. settings_root_key_.WriteValue(wide_key.c_str(), wide_value.c_str());
  58. if (result != ERROR_SUCCESS) {
  59. LOG(ERROR) << "Failed to write value " << value << " to key " << key
  60. << ", result: " << result;
  61. }
  62. }
  63. } // namespace remoting