OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/numerics/safe_conversions.h" | |
12 #include "base/strings/string_split.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "base/win/registry.h" | |
17 #include "components/user_prefs/tracked/tracked_preference_histogram_names.h" | |
18 | |
19 using base::win::RegistryValueIterator; | |
20 | |
21 namespace { | |
22 | |
23 constexpr size_t kMacSize = 64; | |
24 | |
25 base::string16 GetSplitPrefKeyName(const base::string16& reg_key_name, | |
26 const std::string& split_key_name) { | |
27 return reg_key_name + L"\\" + base::UTF8ToUTF16(split_key_name); | |
28 } | |
29 | |
30 bool ReadMacFromRegistry(const base::win::RegKey& key, | |
31 const std::string& value_name, | |
32 std::string* out_mac) { | |
33 base::string16 string_value; | |
34 if (key.ReadValue(base::UTF8ToUTF16(value_name).c_str(), &string_value) == | |
35 ERROR_SUCCESS && | |
36 string_value.size() == kMacSize) { | |
37 out_mac->assign(base::UTF16ToUTF8(string_value)); | |
38 return true; | |
39 } | |
40 return false; | |
41 } | |
42 | |
43 // Removes |value_name| under |reg_key_name|. Returns true if found and | |
44 // successfully removed. | |
45 bool ClearAtomicMac(const base::string16& reg_key_name, | |
46 const std::string& value_name) { | |
47 base::win::RegKey key; | |
48 if (key.Open(HKEY_CURRENT_USER, reg_key_name.c_str(), | |
49 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
50 return key.DeleteValue(base::UTF8ToUTF16(value_name).c_str()) == | |
51 ERROR_SUCCESS; | |
52 } | |
53 return false; | |
54 } | |
55 | |
56 // Deletes |split_key_name| under |reg_key_name|. Returns true if found and | |
57 // successfully removed. | |
58 bool ClearSplitMac(const base::string16& reg_key_name, | |
59 const std::string& split_key_name) { | |
60 base::win::RegKey key; | |
61 if (key.Open(HKEY_CURRENT_USER, | |
62 GetSplitPrefKeyName(reg_key_name, split_key_name).c_str(), | |
63 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
64 return key.DeleteKey(L"") == ERROR_SUCCESS; | |
65 } | |
66 return false; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( | |
72 const base::string16& registry_path, | |
73 const base::string16& store_key) | |
74 : preference_key_name_(registry_path + L"\\PreferenceMACs\\" + store_key) {} | |
75 | |
76 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( | |
77 const RegistryHashStoreContentsWin& other) = default; | |
78 | |
79 bool RegistryHashStoreContentsWin::IsCopyable() const { | |
80 return true; | |
81 } | |
82 | |
83 std::unique_ptr<HashStoreContents> RegistryHashStoreContentsWin::MakeCopy() | |
84 const { | |
85 return base::WrapUnique(new RegistryHashStoreContentsWin(*this)); | |
86 } | |
87 | |
88 base::StringPiece RegistryHashStoreContentsWin::GetUMASuffix() const { | |
89 return user_prefs::tracked::kTrackedPrefRegistryValidationSuffix; | |
90 } | |
91 | |
92 void RegistryHashStoreContentsWin::Reset() { | |
93 base::win::RegKey key; | |
94 if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(), | |
95 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
96 LONG result = key.DeleteKey(L""); | |
97 DCHECK(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND) << result; | |
98 } | |
99 } | |
100 | |
101 bool RegistryHashStoreContentsWin::GetMac(const std::string& path, | |
102 std::string* out_value) { | |
103 base::win::RegKey key; | |
104 if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(), | |
105 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
106 return ReadMacFromRegistry(key, path, out_value); | |
107 } | |
108 | |
109 return false; | |
110 } | |
111 | |
112 bool RegistryHashStoreContentsWin::GetSplitMacs( | |
113 const std::string& path, | |
114 std::map<std::string, std::string>* split_macs) { | |
115 DCHECK(split_macs); | |
116 DCHECK(split_macs->empty()); | |
117 | |
118 RegistryValueIterator iter_key( | |
119 HKEY_CURRENT_USER, | |
120 GetSplitPrefKeyName(preference_key_name_, path).c_str()); | |
121 | |
122 for (; iter_key.Valid(); ++iter_key) { | |
123 split_macs->insert(make_pair(base::UTF16ToUTF8(iter_key.Name()), | |
124 base::UTF16ToUTF8(iter_key.Value()))); | |
125 } | |
126 | |
127 return !split_macs->empty(); | |
128 } | |
129 | |
130 void RegistryHashStoreContentsWin::SetMac(const std::string& path, | |
131 const std::string& value) { | |
132 base::win::RegKey key; | |
133 DCHECK_EQ(kMacSize, value.size()); | |
134 | |
135 if (key.Create(HKEY_CURRENT_USER, preference_key_name_.c_str(), | |
136 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
137 key.WriteValue(base::UTF8ToUTF16(path).c_str(), | |
138 base::UTF8ToUTF16(value).c_str()); | |
139 } | |
140 } | |
141 | |
142 void RegistryHashStoreContentsWin::SetSplitMac(const std::string& path, | |
143 const std::string& split_path, | |
144 const std::string& value) { | |
145 base::win::RegKey key; | |
146 DCHECK_EQ(kMacSize, value.size()); | |
147 | |
148 if (key.Create(HKEY_CURRENT_USER, | |
149 GetSplitPrefKeyName(preference_key_name_, path).c_str(), | |
150 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
151 key.WriteValue(base::UTF8ToUTF16(split_path).c_str(), | |
152 base::UTF8ToUTF16(value).c_str()); | |
153 } | |
154 } | |
155 | |
156 bool RegistryHashStoreContentsWin::RemoveEntry(const std::string& path) { | |
157 return ClearAtomicMac(preference_key_name_, path) || | |
158 ClearSplitMac(preference_key_name_, path); | |
159 } | |
160 | |
161 void RegistryHashStoreContentsWin::ImportEntry(const std::string& path, | |
162 const base::Value* in_value) { | |
163 NOTREACHED() | |
164 << "RegistryHashStoreContents does not support the ImportEntry operation"; | |
165 } | |
166 | |
167 const base::DictionaryValue* RegistryHashStoreContentsWin::GetContents() const { | |
168 NOTREACHED() | |
169 << "RegistryHashStoreContents does not support the GetContents operation"; | |
170 return NULL; | |
171 } | |
172 | |
173 std::string RegistryHashStoreContentsWin::GetSuperMac() const { | |
174 NOTREACHED() | |
175 << "RegistryHashStoreContents does not support the GetSuperMac operation"; | |
176 return NULL; | |
177 } | |
178 | |
179 void RegistryHashStoreContentsWin::SetSuperMac(const std::string& super_mac) { | |
180 NOTREACHED() | |
181 << "RegistryHashStoreContents does not support the SetSuperMac operation"; | |
182 } | |
OLD | NEW |