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

Side by Side Diff: chrome/installer/gcapi/gcapi.cc

Issue 6713107: Make the windows_version.h functions threadsafe by using a singleton. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/gcapi/gcapi.h" 5 #include "chrome/installer/gcapi/gcapi.h"
6 6
7 #include <atlbase.h> 7 #include <atlbase.h>
8 #include <atlcom.h> 8 #include <atlcom.h>
9 #include <windows.h> 9 #include <windows.h>
10 #include <sddl.h> 10 #include <sddl.h>
11 #include <stdlib.h> 11 #define STRSAFE_NO_DEPRECATE
12 #include <strsafe.h> 12 #include <strsafe.h>
13 #include <tlhelp32.h> 13 #include <tlhelp32.h>
14 14
15 #include <cstdlib>
16
15 #include "google_update_idl.h" 17 #include "google_update_idl.h"
18 #include "base/win/windows_version.h"
16 19
17 namespace { 20 namespace {
18 21
19 const wchar_t kChromeRegClientsKey[] = 22 const wchar_t kChromeRegClientsKey[] =
20 L"Software\\Google\\Update\\Clients\\" 23 L"Software\\Google\\Update\\Clients\\"
21 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; 24 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
22 const wchar_t kChromeRegClientStateKey[] = 25 const wchar_t kChromeRegClientStateKey[] =
23 L"Software\\Google\\Update\\ClientState\\" 26 L"Software\\Google\\Update\\ClientState\\"
24 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; 27 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
25 const wchar_t kChromeRegLaunchCmd[] = L"InstallerSuccessLaunchCmdLine"; 28 const wchar_t kChromeRegLaunchCmd[] = L"InstallerSuccessLaunchCmdLine";
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 reinterpret_cast<LPDWORD>(size)) == ERROR_SUCCESS)) { 152 reinterpret_cast<LPDWORD>(size)) == ERROR_SUCCESS)) {
150 ::RegCloseKey(key); 153 ::RegCloseKey(key);
151 return true; 154 return true;
152 } 155 }
153 return false; 156 return false;
154 } 157 }
155 158
156 bool IsChromeInstalled(HKEY root_key) { 159 bool IsChromeInstalled(HKEY root_key) {
157 wchar_t version[64]; 160 wchar_t version[64];
158 size_t size = _countof(version); 161 size_t size = _countof(version);
159 if (ReadValueFromRegistry(root_key, kChromeRegClientsKey, kChromeRegVersion, 162 return ReadValueFromRegistry(root_key, kChromeRegClientsKey,
160 version, &size)) 163 kChromeRegVersion, version, &size);
161 return true;
162 return false;
163 }
164
165 bool IsWinXPSp2OrLater(bool* is_vista_or_later) {
166 OSVERSIONINFOEX osviex = { sizeof(OSVERSIONINFOEX) };
167 int r = ::GetVersionEx((LPOSVERSIONINFO)&osviex);
168 // If this failed we're on Win9X or a pre NT4SP6 OS.
169 if (!r)
170 return false;
171
172 if (osviex.dwMajorVersion < 5)
173 return false;
174
175 if (osviex.dwMajorVersion > 5) {
176 *is_vista_or_later = true;
177 return true; // way beyond Windows XP;
178 }
179
180 if (osviex.dwMinorVersion >= 1 && osviex.wServicePackMajor >= 2)
181 return true; // Windows XP SP2 or better.
182
183 return false; // Windows 2000, WinXP no Service Pack.
184 } 164 }
185 165
186 // Note this function should not be called on old Windows versions where these 166 // Note this function should not be called on old Windows versions where these
187 // Windows API are not available. We always invoke this function after checking 167 // Windows API are not available. We always invoke this function after checking
188 // that current OS is Vista or later. 168 // that current OS is Vista or later.
189 bool VerifyAdminGroup() { 169 bool VerifyAdminGroup() {
190 SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; 170 SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
191 PSID Group; 171 PSID Group;
192 BOOL check = ::AllocateAndInitializeSid(&NtAuthority, 2, 172 BOOL check = ::AllocateAndInitializeSid(&NtAuthority, 2,
193 SECURITY_BUILTIN_DOMAIN_RID, 173 SECURITY_BUILTIN_DOMAIN_RID,
(...skipping 29 matching lines...) Expand all
223 RegDeleteKey(key, NULL); 203 RegDeleteKey(key, NULL);
224 204
225 RegCloseKey(key); 205 RegCloseKey(key);
226 } 206 }
227 207
228 return result; 208 return result;
229 } 209 }
230 210
231 bool IsRunningElevated() { 211 bool IsRunningElevated() {
232 // This method should be called only for Vista or later. 212 // This method should be called only for Vista or later.
233 bool is_vista_or_later = false; 213 if ((base::win::GetVersion() < base::win::VERSION_VISTA) ||
234 IsWinXPSp2OrLater(&is_vista_or_later); 214 !VerifyAdminGroup())
235 if (!is_vista_or_later || !VerifyAdminGroup())
236 return false; 215 return false;
237 216
238 HANDLE process_token; 217 HANDLE process_token;
239 if (!::OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token)) 218 if (!::OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token))
240 return false; 219 return false;
241 220
242 TOKEN_ELEVATION_TYPE elevation_type = TokenElevationTypeDefault; 221 TOKEN_ELEVATION_TYPE elevation_type = TokenElevationTypeDefault;
243 DWORD size_returned = 0; 222 DWORD size_returned = 0;
244 if (!::GetTokenInformation(process_token, TokenElevationType, 223 if (!::GetTokenInformation(process_token, TokenElevationType,
245 &elevation_type, sizeof(elevation_type), 224 &elevation_type, sizeof(elevation_type),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 ::CloseHandle(process_handle); 259 ::CloseHandle(process_handle);
281 return result; 260 return result;
282 } 261 }
283 } // namespace 262 } // namespace
284 263
285 #pragma comment(linker, "/EXPORT:GoogleChromeCompatibilityCheck=_GoogleChromeCom patibilityCheck@8,PRIVATE") 264 #pragma comment(linker, "/EXPORT:GoogleChromeCompatibilityCheck=_GoogleChromeCom patibilityCheck@8,PRIVATE")
286 DLLEXPORT BOOL __stdcall GoogleChromeCompatibilityCheck(BOOL set_flag, 265 DLLEXPORT BOOL __stdcall GoogleChromeCompatibilityCheck(BOOL set_flag,
287 DWORD *reasons) { 266 DWORD *reasons) {
288 DWORD local_reasons = 0; 267 DWORD local_reasons = 0;
289 268
290 bool is_vista_or_later = false; 269 base::win::Version version = base::win::GetVersion();
291 // System requirements? 270 // System requirements?
292 if (!IsWinXPSp2OrLater(&is_vista_or_later)) 271 if ((version < base::win::VERSION_XP) ||
272 ((version == base::win::VERSION_XP) &&
273 (base::win::OSInfo::GetInstance()->service_pack()[0] < 2)))
293 local_reasons |= GCCC_ERROR_OSNOTSUPPORTED; 274 local_reasons |= GCCC_ERROR_OSNOTSUPPORTED;
294 275
295 if (IsChromeInstalled(HKEY_LOCAL_MACHINE)) 276 if (IsChromeInstalled(HKEY_LOCAL_MACHINE))
296 local_reasons |= GCCC_ERROR_SYSTEMLEVELALREADYPRESENT; 277 local_reasons |= GCCC_ERROR_SYSTEMLEVELALREADYPRESENT;
297 278
298 if (IsChromeInstalled(HKEY_CURRENT_USER)) 279 if (IsChromeInstalled(HKEY_CURRENT_USER))
299 local_reasons |= GCCC_ERROR_USERLEVELALREADYPRESENT; 280 local_reasons |= GCCC_ERROR_USERLEVELALREADYPRESENT;
300 281
301 if (!VerifyHKLMAccess(kChromeRegClientsKey)) { 282 if (!VerifyHKLMAccess(kChromeRegClientsKey)) {
302 local_reasons |= GCCC_ERROR_ACCESSDENIED; 283 local_reasons |= GCCC_ERROR_ACCESSDENIED;
303 } else if (is_vista_or_later && !VerifyAdminGroup()) { 284 } else if ((version >= base::win::VERSION_VISTA) && !VerifyAdminGroup()) {
304 // For Vista or later check for elevation since even for admin user we could 285 // For Vista or later check for elevation since even for admin user we could
305 // be running in non-elevated mode. We require integrity level High. 286 // be running in non-elevated mode. We require integrity level High.
306 local_reasons |= GCCC_ERROR_INTEGRITYLEVEL; 287 local_reasons |= GCCC_ERROR_INTEGRITYLEVEL;
307 } 288 }
308 289
309 // Then only check whether we can re-offer, if everything else is OK. 290 // Then only check whether we can re-offer, if everything else is OK.
310 if (local_reasons == 0 && !CanReOfferChrome(set_flag)) 291 if (local_reasons == 0 && !CanReOfferChrome(set_flag))
311 local_reasons |= GCCC_ERROR_ALREADYOFFERED; 292 local_reasons |= GCCC_ERROR_ALREADYOFFERED;
312 293
313 // Done. Copy/return results. 294 // Done. Copy/return results.
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 // This loop iterates through all of the top-level Windows named 423 // This loop iterates through all of the top-level Windows named
443 // Chrome_WindowImpl_0, and looks for the first one with any children. 424 // Chrome_WindowImpl_0, and looks for the first one with any children.
444 while (handle && !FindWindowEx(handle, NULL, L"Chrome_WindowImpl_0", NULL)) { 425 while (handle && !FindWindowEx(handle, NULL, L"Chrome_WindowImpl_0", NULL)) {
445 // Get the next top-level Chrome window. 426 // Get the next top-level Chrome window.
446 handle = FindWindowEx(NULL, handle, L"Chrome_WindowImpl_0", NULL); 427 handle = FindWindowEx(NULL, handle, L"Chrome_WindowImpl_0", NULL);
447 } 428 }
448 429
449 return (handle && 430 return (handle &&
450 SetWindowPos(handle, 0, x, y, width, height, SWP_NOZORDER)); 431 SetWindowPos(handle, 0, x, y, width, height, SWP_NOZORDER));
451 } 432 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698