| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "services/preferences/tracked/registry_hash_store_contents_win.h" | 5 #include "services/preferences/tracked/registry_hash_store_contents_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/values.h" | 17 #include "base/values.h" |
| 16 #include "base/win/registry.h" | 18 #include "base/win/registry.h" |
| 17 #include "services/preferences/public/cpp/tracked/tracked_preference_histogram_n
ames.h" | 19 #include "services/preferences/public/cpp/tracked/tracked_preference_histogram_n
ames.h" |
| 18 | 20 |
| 19 using base::win::RegistryValueIterator; | 21 using base::win::RegistryValueIterator; |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 const std::string& split_key_name) { | 61 const std::string& split_key_name) { |
| 60 base::win::RegKey key; | 62 base::win::RegKey key; |
| 61 if (key.Open(HKEY_CURRENT_USER, | 63 if (key.Open(HKEY_CURRENT_USER, |
| 62 GetSplitPrefKeyName(reg_key_name, split_key_name).c_str(), | 64 GetSplitPrefKeyName(reg_key_name, split_key_name).c_str(), |
| 63 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | 65 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 64 return key.DeleteKey(L"") == ERROR_SUCCESS; | 66 return key.DeleteKey(L"") == ERROR_SUCCESS; |
| 65 } | 67 } |
| 66 return false; | 68 return false; |
| 67 } | 69 } |
| 68 | 70 |
| 69 // Deletes |reg_key_name| if it exists. | |
| 70 void DeleteRegistryKey(const base::string16& reg_key_name) { | |
| 71 base::win::RegKey key; | |
| 72 if (key.Open(HKEY_CURRENT_USER, reg_key_name.c_str(), | |
| 73 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
| 74 LONG result = key.DeleteKey(L""); | |
| 75 DCHECK(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND) << result; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 } // namespace | 71 } // namespace |
| 80 | 72 |
| 81 void TempScopedDirRegistryCleaner::SetRegistryPath( | 73 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( |
| 82 const base::string16& registry_path) { | 74 const base::string16& registry_path, |
| 83 if (registry_path_.empty()) | 75 const base::string16& store_key) |
| 84 registry_path_ = registry_path; | 76 : preference_key_name_(registry_path + L"\\PreferenceMACs\\" + store_key), |
| 85 else | 77 reset_on_delete_(base::StartsWith(store_key, |
| 86 DCHECK_EQ(registry_path_, registry_path); | 78 base::ScopedTempDir::GetTempDirPrefix(), |
| 79 base::CompareCase::INSENSITIVE_ASCII)) { |
| 87 } | 80 } |
| 88 | 81 |
| 89 TempScopedDirRegistryCleaner::~TempScopedDirRegistryCleaner() { | 82 RegistryHashStoreContentsWin::~RegistryHashStoreContentsWin() { |
| 90 DCHECK(!registry_path_.empty()); | 83 if (reset_on_delete_) |
| 91 DeleteRegistryKey(registry_path_); | 84 Reset(); |
| 92 } | 85 } |
| 93 | 86 |
| 94 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( | 87 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( |
| 95 const base::string16& registry_path, | |
| 96 const base::string16& store_key, | |
| 97 scoped_refptr<TempScopedDirCleaner> temp_dir_cleaner) | |
| 98 : preference_key_name_(registry_path + L"\\PreferenceMACs\\" + store_key), | |
| 99 temp_dir_cleaner_(std::move(temp_dir_cleaner)) { | |
| 100 if (temp_dir_cleaner_) | |
| 101 static_cast<TempScopedDirRegistryCleaner*>(temp_dir_cleaner_.get()) | |
| 102 ->SetRegistryPath(preference_key_name_); | |
| 103 } | |
| 104 | |
| 105 RegistryHashStoreContentsWin::~RegistryHashStoreContentsWin() = default; | |
| 106 | |
| 107 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( | |
| 108 const RegistryHashStoreContentsWin& other) = default; | 88 const RegistryHashStoreContentsWin& other) = default; |
| 109 | 89 |
| 110 bool RegistryHashStoreContentsWin::IsCopyable() const { | 90 bool RegistryHashStoreContentsWin::IsCopyable() const { |
| 111 return true; | 91 return true; |
| 112 } | 92 } |
| 113 | 93 |
| 114 std::unique_ptr<HashStoreContents> RegistryHashStoreContentsWin::MakeCopy() | 94 std::unique_ptr<HashStoreContents> RegistryHashStoreContentsWin::MakeCopy() |
| 115 const { | 95 const { |
| 116 return base::WrapUnique(new RegistryHashStoreContentsWin(*this)); | 96 return base::WrapUnique(new RegistryHashStoreContentsWin(*this)); |
| 117 } | 97 } |
| 118 | 98 |
| 119 base::StringPiece RegistryHashStoreContentsWin::GetUMASuffix() const { | 99 base::StringPiece RegistryHashStoreContentsWin::GetUMASuffix() const { |
| 120 return user_prefs::tracked::kTrackedPrefRegistryValidationSuffix; | 100 return user_prefs::tracked::kTrackedPrefRegistryValidationSuffix; |
| 121 } | 101 } |
| 122 | 102 |
| 123 void RegistryHashStoreContentsWin::Reset() { | 103 void RegistryHashStoreContentsWin::Reset() { |
| 124 DeleteRegistryKey(preference_key_name_); | 104 base::win::RegKey key; |
| 105 if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(), |
| 106 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 107 LONG result = key.DeleteKey(L""); |
| 108 DCHECK(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND || |
| 109 result == ERROR_ACCESS_DENIED || result == ERROR_KEY_DELETED) |
| 110 << result; |
| 111 } |
| 125 } | 112 } |
| 126 | 113 |
| 127 bool RegistryHashStoreContentsWin::GetMac(const std::string& path, | 114 bool RegistryHashStoreContentsWin::GetMac(const std::string& path, |
| 128 std::string* out_value) { | 115 std::string* out_value) { |
| 129 base::win::RegKey key; | 116 base::win::RegKey key; |
| 130 if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(), | 117 if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(), |
| 131 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | 118 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 132 return ReadMacFromRegistry(key, path, out_value); | 119 return ReadMacFromRegistry(key, path, out_value); |
| 133 } | 120 } |
| 134 | 121 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 std::string RegistryHashStoreContentsWin::GetSuperMac() const { | 186 std::string RegistryHashStoreContentsWin::GetSuperMac() const { |
| 200 NOTREACHED() | 187 NOTREACHED() |
| 201 << "RegistryHashStoreContents does not support the GetSuperMac operation"; | 188 << "RegistryHashStoreContents does not support the GetSuperMac operation"; |
| 202 return NULL; | 189 return NULL; |
| 203 } | 190 } |
| 204 | 191 |
| 205 void RegistryHashStoreContentsWin::SetSuperMac(const std::string& super_mac) { | 192 void RegistryHashStoreContentsWin::SetSuperMac(const std::string& super_mac) { |
| 206 NOTREACHED() | 193 NOTREACHED() |
| 207 << "RegistryHashStoreContents does not support the SetSuperMac operation"; | 194 << "RegistryHashStoreContents does not support the SetSuperMac operation"; |
| 208 } | 195 } |
| OLD | NEW |