Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: services/preferences/tracked/registry_hash_store_contents_win.cc

Issue 2926453002: Fix a race condition in ~RegistryHashStoreContentsWin (Closed)
Patch Set: Add temp_scoped_dir_cleaner to a build file Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
10 #include "base/logging.h" 9 #include "base/logging.h"
11 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
12 #include "base/numerics/safe_conversions.h" 11 #include "base/numerics/safe_conversions.h"
13 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
17 #include "base/values.h" 15 #include "base/values.h"
18 #include "base/win/registry.h" 16 #include "base/win/registry.h"
19 #include "services/preferences/public/cpp/tracked/tracked_preference_histogram_n ames.h" 17 #include "services/preferences/public/cpp/tracked/tracked_preference_histogram_n ames.h"
20 18
21 using base::win::RegistryValueIterator; 19 using base::win::RegistryValueIterator;
22 20
23 namespace { 21 namespace {
24 22
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 const std::string& split_key_name) { 59 const std::string& split_key_name) {
62 base::win::RegKey key; 60 base::win::RegKey key;
63 if (key.Open(HKEY_CURRENT_USER, 61 if (key.Open(HKEY_CURRENT_USER,
64 GetSplitPrefKeyName(reg_key_name, split_key_name).c_str(), 62 GetSplitPrefKeyName(reg_key_name, split_key_name).c_str(),
65 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { 63 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
66 return key.DeleteKey(L"") == ERROR_SUCCESS; 64 return key.DeleteKey(L"") == ERROR_SUCCESS;
67 } 65 }
68 return false; 66 return false;
69 } 67 }
70 68
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
71 } // namespace 79 } // namespace
72 80
73 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( 81 void TempScopedDirRegistryCleaner::SetRegistryPath(
74 const base::string16& registry_path, 82 const base::string16& registry_path) {
75 const base::string16& store_key) 83 if (registry_path_.empty())
76 : preference_key_name_(registry_path + L"\\PreferenceMACs\\" + store_key), 84 registry_path_ = registry_path;
77 reset_on_delete_(base::StartsWith(store_key, 85 else
78 base::ScopedTempDir::GetTempDirPrefix(), 86 DCHECK_EQ(registry_path_, registry_path);
79 base::CompareCase::INSENSITIVE_ASCII)) {
80 } 87 }
81 88
82 RegistryHashStoreContentsWin::~RegistryHashStoreContentsWin() { 89 TempScopedDirRegistryCleaner::~TempScopedDirRegistryCleaner() {
83 if (reset_on_delete_) 90 DCHECK(!registry_path_.empty());
84 Reset(); 91 DeleteRegistryKey(registry_path_);
85 } 92 }
86 93
87 RegistryHashStoreContentsWin::RegistryHashStoreContentsWin( 94 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(
88 const RegistryHashStoreContentsWin& other) = default; 108 const RegistryHashStoreContentsWin& other) = default;
89 109
90 bool RegistryHashStoreContentsWin::IsCopyable() const { 110 bool RegistryHashStoreContentsWin::IsCopyable() const {
91 return true; 111 return true;
92 } 112 }
93 113
94 std::unique_ptr<HashStoreContents> RegistryHashStoreContentsWin::MakeCopy() 114 std::unique_ptr<HashStoreContents> RegistryHashStoreContentsWin::MakeCopy()
95 const { 115 const {
96 return base::WrapUnique(new RegistryHashStoreContentsWin(*this)); 116 return base::WrapUnique(new RegistryHashStoreContentsWin(*this));
97 } 117 }
98 118
99 base::StringPiece RegistryHashStoreContentsWin::GetUMASuffix() const { 119 base::StringPiece RegistryHashStoreContentsWin::GetUMASuffix() const {
100 return user_prefs::tracked::kTrackedPrefRegistryValidationSuffix; 120 return user_prefs::tracked::kTrackedPrefRegistryValidationSuffix;
101 } 121 }
102 122
103 void RegistryHashStoreContentsWin::Reset() { 123 void RegistryHashStoreContentsWin::Reset() {
104 base::win::RegKey key; 124 DeleteRegistryKey(preference_key_name_);
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 }
112 } 125 }
113 126
114 bool RegistryHashStoreContentsWin::GetMac(const std::string& path, 127 bool RegistryHashStoreContentsWin::GetMac(const std::string& path,
115 std::string* out_value) { 128 std::string* out_value) {
116 base::win::RegKey key; 129 base::win::RegKey key;
117 if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(), 130 if (key.Open(HKEY_CURRENT_USER, preference_key_name_.c_str(),
118 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { 131 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
119 return ReadMacFromRegistry(key, path, out_value); 132 return ReadMacFromRegistry(key, path, out_value);
120 } 133 }
121 134
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 std::string RegistryHashStoreContentsWin::GetSuperMac() const { 199 std::string RegistryHashStoreContentsWin::GetSuperMac() const {
187 NOTREACHED() 200 NOTREACHED()
188 << "RegistryHashStoreContents does not support the GetSuperMac operation"; 201 << "RegistryHashStoreContents does not support the GetSuperMac operation";
189 return NULL; 202 return NULL;
190 } 203 }
191 204
192 void RegistryHashStoreContentsWin::SetSuperMac(const std::string& super_mac) { 205 void RegistryHashStoreContentsWin::SetSuperMac(const std::string& super_mac) {
193 NOTREACHED() 206 NOTREACHED()
194 << "RegistryHashStoreContents does not support the SetSuperMac operation"; 207 << "RegistryHashStoreContents does not support the SetSuperMac operation";
195 } 208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698