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

Side by Side Diff: chrome/installer/util/google_update_settings.cc

Issue 6090006: Regkey functions return error code instead of bool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/installer/util/google_update_settings.h" 5 #include "chrome/installer/util/google_update_settings.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
(...skipping 22 matching lines...) Expand all
33 FOUND_SAME_SETTING 33 FOUND_SAME_SETTING
34 }; 34 };
35 35
36 bool ReadGoogleUpdateStrKey(const wchar_t* const name, std::wstring* value) { 36 bool ReadGoogleUpdateStrKey(const wchar_t* const name, std::wstring* value) {
37 // The registry functions below will end up going to disk. Do this on another 37 // The registry functions below will end up going to disk. Do this on another
38 // thread to avoid slowing the IO thread. http://crbug.com/62121 38 // thread to avoid slowing the IO thread. http://crbug.com/62121
39 base::ThreadRestrictions::ScopedAllowIO allow_io; 39 base::ThreadRestrictions::ScopedAllowIO allow_io;
40 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 40 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
41 std::wstring reg_path = dist->GetStateKey(); 41 std::wstring reg_path = dist->GetStateKey();
42 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ); 42 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ);
43 if (!key.ReadValue(name, value)) { 43 if (key.ReadValue(name, value) != ERROR_SUCCESS) {
44 RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ); 44 RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ);
45 return hklm_key.ReadValue(name, value); 45 return (hklm_key.ReadValue(name, value) == ERROR_SUCCESS);
46 } 46 }
47 return true; 47 return true;
48 } 48 }
49 49
50 bool WriteGoogleUpdateStrKey(const wchar_t* const name, 50 bool WriteGoogleUpdateStrKey(const wchar_t* const name,
51 const std::wstring& value) { 51 const std::wstring& value) {
52 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 52 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
53 std::wstring reg_path = dist->GetStateKey(); 53 std::wstring reg_path = dist->GetStateKey();
54 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE); 54 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
55 return key.WriteValue(name, value.c_str()); 55 return key.WriteValue(name, value.c_str()) == ERROR_SUCCESS;
56 } 56 }
57 57
58 bool ClearGoogleUpdateStrKey(const wchar_t* const name) { 58 bool ClearGoogleUpdateStrKey(const wchar_t* const name) {
59 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 59 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
60 std::wstring reg_path = dist->GetStateKey(); 60 std::wstring reg_path = dist->GetStateKey();
61 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE); 61 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
62 std::wstring value; 62 std::wstring value;
63 if (!key.ReadValue(name, &value)) 63 if (key.ReadValue(name, &value) != ERROR_SUCCESS)
64 return false; 64 return false;
65 return key.WriteValue(name, L""); 65 return key.WriteValue(name, L"") == ERROR_SUCCESS;
66 } 66 }
67 67
68 bool RemoveGoogleUpdateStrKey(const wchar_t* const name) { 68 bool RemoveGoogleUpdateStrKey(const wchar_t* const name) {
69 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 69 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
70 std::wstring reg_path = dist->GetStateKey(); 70 std::wstring reg_path = dist->GetStateKey();
71 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE); 71 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
72 if (!key.ValueExists(name)) 72 if (!key.ValueExists(name))
73 return true; 73 return true;
74 return key.DeleteValue(name); 74 return key.DeleteValue(name) == ERROR_SUCCESS;
75 } 75 }
76 76
77 EulaSearchResult HasEULASetting(HKEY root, const std::wstring& state_key, 77 EulaSearchResult HasEULASetting(HKEY root, const std::wstring& state_key,
78 bool setting) { 78 bool setting) {
79 RegKey key; 79 RegKey key;
80 DWORD previous_value; 80 DWORD previous_value = setting ? 1 : 0;
81 81
82 if (!key.Open(root, state_key.c_str(), KEY_QUERY_VALUE)) 82 if (key.Open(root, state_key.c_str(), KEY_QUERY_VALUE) != ERROR_SUCCESS)
83 return NO_SETTING; 83 return NO_SETTING;
84 84
85 if (!key.ReadValueDW(google_update::kRegEULAAceptedField, &previous_value)) 85 LONG result = key.ReadValueDW(google_update::kRegEULAAceptedField,
86 &previous_value);
87 if (result != ERROR_SUCCESS)
86 return FOUND_CLIENT_STATE; 88 return FOUND_CLIENT_STATE;
87 89
88 return ((previous_value != 0) == setting) ? 90 return ((previous_value != 0) == setting) ?
89 FOUND_SAME_SETTING : FOUND_OPPOSITE_SETTING; 91 FOUND_SAME_SETTING : FOUND_OPPOSITE_SETTING;
90 } 92 }
91 93
92 } // namespace. 94 } // namespace.
93 95
94 bool GoogleUpdateSettings::GetCollectStatsConsent() { 96 bool GoogleUpdateSettings::GetCollectStatsConsent() {
95 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 97 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
96 std::wstring reg_path = dist->GetStateKey(); 98 std::wstring reg_path = dist->GetStateKey();
97 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ); 99 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ);
98 DWORD value; 100 DWORD value = 0;
99 if (!key.ReadValueDW(google_update::kRegUsageStatsField, &value)) { 101 LONG result = key.ReadValueDW(google_update::kRegUsageStatsField, &value);
102 if (result != ERROR_SUCCESS) {
100 RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ); 103 RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ);
grt (UTC plus 2) 2011/01/11 03:51:30 how about use key.Open() rather than making a new
amit 2011/01/12 04:11:23 Done.
101 if (!hklm_key.ReadValueDW(google_update::kRegUsageStatsField, &value)) 104 hklm_key.ReadValueDW(google_update::kRegUsageStatsField, &value);
grt (UTC plus 2) 2011/01/11 03:51:30 I prefer checking the return value here rather tha
102 return false;
103 } 105 }
104 return (1 == value); 106 return (1 == value);
105 } 107 }
106 108
107 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) { 109 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
108 DWORD value = consented? 1 : 0; 110 DWORD value = consented? 1 : 0;
109 // Writing to HKLM is only a best effort deal. 111 // Writing to HKLM is only a best effort deal.
110 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 112 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
111 std::wstring reg_path = dist->GetStateMediumKey(); 113 std::wstring reg_path = dist->GetStateMediumKey();
112 RegKey key_hklm(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ | KEY_WRITE); 114 RegKey key_hklm(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ | KEY_WRITE);
113 key_hklm.WriteValue(google_update::kRegUsageStatsField, value); 115 key_hklm.WriteValue(google_update::kRegUsageStatsField, value);
114 // Writing to HKCU is used both by chrome and by the crash reporter. 116 // Writing to HKCU is used both by chrome and by the crash reporter.
115 reg_path = dist->GetStateKey(); 117 reg_path = dist->GetStateKey();
116 RegKey key_hkcu(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE); 118 RegKey key_hkcu(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
grt (UTC plus 2) 2011/01/11 03:51:30 Change to use only one RegKey instance?
amit 2011/01/12 04:11:23 Done.
117 return key_hkcu.WriteValue(google_update::kRegUsageStatsField, value); 119 LONG result = key_hkcu.WriteValue(google_update::kRegUsageStatsField, value);
120 return result == ERROR_SUCCESS;
118 } 121 }
119 122
120 bool GoogleUpdateSettings::GetMetricsId(std::wstring* metrics_id) { 123 bool GoogleUpdateSettings::GetMetricsId(std::wstring* metrics_id) {
121 return ReadGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id); 124 return ReadGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id);
122 } 125 }
123 126
124 bool GoogleUpdateSettings::SetMetricsId(const std::wstring& metrics_id) { 127 bool GoogleUpdateSettings::SetMetricsId(const std::wstring& metrics_id) {
125 return WriteGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id); 128 return WriteGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id);
126 } 129 }
127 130
(...skipping 21 matching lines...) Expand all
149 reg_path = product.distribution()->GetStateMediumKey(); 152 reg_path = product.distribution()->GetStateMediumKey();
150 } 153 }
151 } 154 }
152 if (status == NO_SETTING) { 155 if (status == NO_SETTING) {
153 LOG(WARNING) 156 LOG(WARNING)
154 << "eulaaccepted value not found; setting consent on package"; 157 << "eulaaccepted value not found; setting consent on package";
155 reg_path = package.properties()->GetStateMediumKey(); 158 reg_path = package.properties()->GetStateMediumKey();
156 } 159 }
157 } 160 }
158 RegKey key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE); 161 RegKey key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE);
159 return key.WriteValue(google_update::kRegEULAAceptedField, consented ? 1 : 0); 162 LONG result = key.WriteValue(google_update::kRegEULAAceptedField,
163 consented ? 1 : 0);
164 return result == ERROR_SUCCESS;
160 } 165 }
161 166
162 int GoogleUpdateSettings::GetLastRunTime() { 167 int GoogleUpdateSettings::GetLastRunTime() {
163 std::wstring time_s; 168 std::wstring time_s;
164 if (!ReadGoogleUpdateStrKey(google_update::kRegLastRunTimeField, &time_s)) 169 if (!ReadGoogleUpdateStrKey(google_update::kRegLastRunTimeField, &time_s))
165 return -1; 170 return -1;
166 int64 time_i; 171 int64 time_i;
167 if (!base::StringToInt64(time_s, &time_i)) 172 if (!base::StringToInt64(time_s, &time_i))
168 return -1; 173 return -1;
169 base::TimeDelta td = 174 base::TimeDelta td =
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 void GoogleUpdateSettings::UpdateDiffInstallStatus(bool system_install, 245 void GoogleUpdateSettings::UpdateDiffInstallStatus(bool system_install,
241 bool incremental_install, int install_return_code, 246 bool incremental_install, int install_return_code,
242 const std::wstring& product_guid) { 247 const std::wstring& product_guid) {
243 HKEY reg_root = (system_install) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 248 HKEY reg_root = (system_install) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
244 249
245 RegKey key; 250 RegKey key;
246 installer::ChannelInfo channel_info; 251 installer::ChannelInfo channel_info;
247 std::wstring reg_key(google_update::kRegPathClientState); 252 std::wstring reg_key(google_update::kRegPathClientState);
248 reg_key.append(L"\\"); 253 reg_key.append(L"\\");
249 reg_key.append(product_guid); 254 reg_key.append(product_guid);
250 if (!key.Open(reg_root, reg_key.c_str(), KEY_ALL_ACCESS) || 255 LONG result = key.Open(reg_root, reg_key.c_str(), KEY_ALL_ACCESS);
251 !channel_info.Initialize(key)) { 256 if (result != ERROR_SUCCESS || !channel_info.Initialize(key)) {
252 VLOG(1) << "Application key not found."; 257 VLOG(1) << "Application key not found.";
253 if (!incremental_install || !install_return_code) { 258 if (!incremental_install || !install_return_code) {
254 VLOG(1) << "Returning without changing application key."; 259 VLOG(1) << "Returning without changing application key.";
255 key.Close(); 260 key.Close();
grt (UTC plus 2) 2011/01/12 15:06:38 Remove
amit 2011/01/14 05:23:33 Done.
256 return; 261 return;
257 } else if (!key.Valid()) { 262 } else if (!key.Valid()) {
258 reg_key.assign(google_update::kRegPathClientState); 263 reg_key.assign(google_update::kRegPathClientState);
259 if (!key.Open(reg_root, reg_key.c_str(), KEY_ALL_ACCESS) || 264 result = key.Open(reg_root, reg_key.c_str(), KEY_ALL_ACCESS);
260 !key.CreateKey(product_guid.c_str(), KEY_ALL_ACCESS)) { 265 if (result != ERROR_SUCCESS)
grt (UTC plus 2) 2011/01/11 03:51:30 result == ERROR_SUCCESS
amit 2011/01/12 04:11:23 Are you sure? As per the old code, key.CreateKey w
grt (UTC plus 2) 2011/01/12 15:06:38 I'm pretty sure CreateKey executes only if Open su
amit 2011/01/14 05:23:33 Done.
266 key.CreateKey(product_guid.c_str(), KEY_ALL_ACCESS);
grt (UTC plus 2) 2011/01/11 03:51:30 result = key.CreateKey(...)
grt (UTC plus 2) 2011/01/12 15:06:38 Ping.
amit 2011/01/14 05:23:33 Done.
267
268 if (result != ERROR_SUCCESS) {
261 LOG(ERROR) << "Failed to create application key."; 269 LOG(ERROR) << "Failed to create application key.";
262 key.Close(); 270 key.Close();
grt (UTC plus 2) 2011/01/12 15:06:38 Remove
amit 2011/01/14 05:23:33 Done.
263 return; 271 return;
264 } 272 }
265 } 273 }
266 } 274 }
267 275
268 if (UpdateGoogleUpdateApKey(incremental_install, install_return_code, 276 if (UpdateGoogleUpdateApKey(incremental_install, install_return_code,
269 &channel_info) && 277 &channel_info) &&
270 !channel_info.Write(&key)) { 278 !channel_info.Write(&key)) {
271 LOG(ERROR) << "Failed to write value " << channel_info.value() 279 LOG(ERROR) << "Failed to write value " << channel_info.value()
272 << " to the registry field " << google_update::kRegApField; 280 << " to the registry field " << google_update::kRegApField;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 }; 346 };
339 const wchar_t** end = &kBrands[arraysize(kBrands)]; 347 const wchar_t** end = &kBrands[arraysize(kBrands)];
340 const wchar_t** found = std::find(&kBrands[0], end, brand); 348 const wchar_t** found = std::find(&kBrands[0], end, brand);
341 if (found != end) 349 if (found != end)
342 return true; 350 return true;
343 if (StartsWith(brand, L"EUB", true) || StartsWith(brand, L"EUC", true) || 351 if (StartsWith(brand, L"EUB", true) || StartsWith(brand, L"EUC", true) ||
344 StartsWith(brand, L"GGR", true)) 352 StartsWith(brand, L"GGR", true))
345 return true; 353 return true;
346 return false; 354 return false;
347 } 355 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698