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

Side by Side Diff: chrome_elf/chrome_elf_util.cc

Issue 154653002: Breakpad coverage for chrome_elf start up (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tests and Greg's comments Created 6 years, 10 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
(Empty)
1 // Copyright 2014 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 "chrome_elf/chrome_elf_util.h"
6
7 #include <sddl.h>
8 #include <windows.h>
grt (UTC plus 2) 2014/02/14 16:37:33 usually windows.h is included first. is there a re
Cait (Slow) 2014/02/14 23:12:04 Done.
9
10 #include "base/macros.h"
11 #include "base/strings/string16.h"
12
13 namespace {
14
15 const wchar_t kRegPathClientState[] = L"Software\\Google\\Update\\ClientState";
16 const wchar_t kRegPathClientStateMedium[] =
17 L"Software\\Google\\Update\\ClientStateMedium";
18 const wchar_t kRegValueUsageStats[] = L"usagestats";
19 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments";
20
21 const wchar_t kAppGuidCanary[] =
22 L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}";
23 const wchar_t kAppGuidGoogleChrome[] =
24 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
25 const wchar_t kAppGuidGoogleBinaries[] =
26 L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}";
27
28 bool ReadKeyValueString(bool system_install, const wchar_t* key_path,
29 const wchar_t* guid, const wchar_t* value_to_read,
30 base::string16* value_out) {
31 HKEY h_key = NULL;
32 value_out->clear();
33
34 base::string16 full_key_path(key_path);
35 full_key_path.append(1, L'\\');
36 full_key_path.append(guid);
37
38 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
39 full_key_path.c_str(), 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY,
grt (UTC plus 2) 2014/02/14 16:37:33 nit: align with open paren above
Cait (Slow) 2014/02/14 23:12:04 Done.
40 &h_key) != ERROR_SUCCESS) {
41 return false;
42 }
43
44 const size_t kMaxStringLength = 1024;
45 wchar_t raw_value[kMaxStringLength] = {};
46 DWORD size = sizeof(raw_value);
47 DWORD type = REG_SZ;
48 LONG result = ::RegQueryValueEx(h_key, value_to_read, 0, &type,
49 reinterpret_cast<LPBYTE>(raw_value), &size);
50
51 if (result == ERROR_SUCCESS) {
52 if (type != REG_SZ) {
grt (UTC plus 2) 2014/02/14 16:37:33 since an odd number of bytes doesn't make sense, h
Cait (Slow) 2014/02/14 23:12:04 Done.
53 result = ERROR_NOT_SUPPORTED;
54 } else if (size == 0) {
55 *raw_value = L'\0';
56 } else if (raw_value[size/sizeof(wchar_t) - 1] != L'\0') {
57 if ((size / sizeof(wchar_t)) < kMaxStringLength)
58 raw_value[size / sizeof(wchar_t)] = L'\0';
59 else
60 result = ERROR_MORE_DATA;
61 }
62 }
63
64 *value_out = raw_value;
65
66 ::RegCloseKey(h_key);
67
68 return ERROR_SUCCESS == result;
69 }
70
71 bool ReadKeyValueDW(bool system_install, const wchar_t* key_path,
72 base::string16 guid, const wchar_t* value_to_read,
73 DWORD* value_out) {
74 HKEY h_key = NULL;
75 *value_out = 0;
76
77 base::string16 full_key_path(key_path);
78 full_key_path.append(1, L'\\');
79 full_key_path.append(guid);
80
81 if (::RegOpenKeyEx(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
82 full_key_path.c_str(), 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY,
grt (UTC plus 2) 2014/02/14 16:37:33 align with open paren above
Cait (Slow) 2014/02/14 23:12:04 Done.
83 &h_key) != ERROR_SUCCESS) {
84 return false;
85 }
86
87 DWORD size = sizeof(DWORD);
88 DWORD type = REG_DWORD;
89 LONG result = ::RegQueryValueEx(h_key, value_to_read, 0, &type,
90 reinterpret_cast<BYTE*>(value_out), &size);
grt (UTC plus 2) 2014/02/14 16:37:33 align with open paren above
Cait (Slow) 2014/02/14 23:12:04 Done.
91
92 ::RegCloseKey(h_key);
93
94 return ERROR_SUCCESS == result;
95 }
96
97 } // namespace
98
99 bool IsCanary(const wchar_t* exe_path) {
100 return wcsstr(exe_path, L"Chrome SxS\\Application") != NULL;
101 }
102
103 bool IsSystemInstall(const wchar_t* exe_path) {
104 wchar_t program_dir[MAX_PATH] = {};
105 DWORD ret = ::GetEnvironmentVariable(L"PROGRAMFILES", program_dir,
106 arraysize(program_dir));
107 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret))
108 return true;
109
110 ret = ::GetEnvironmentVariable(L"PROGRAMFILES(X86)", program_dir,
111 arraysize(program_dir));
112 if (ret && ret < MAX_PATH && !wcsncmp(exe_path, program_dir, ret))
113 return true;
114
115 return false;
116 }
117
118 bool IsMultiInstall(bool is_system_install) {
119 base::string16 args;
120 if (!ReadKeyValueString(is_system_install, kRegPathClientState,
121 kAppGuidGoogleChrome, kUninstallArgumentsField, &args)) {
grt (UTC plus 2) 2014/02/14 16:37:33 align with open paren above
Cait (Slow) 2014/02/14 23:12:04 Done.
122 return false;
123 }
124 return args.find(L"--multi-install") != base::string16::npos;
125 }
126
127 bool AreUsageStatsEnabled(const wchar_t* exe_path) {
128 #if !defined(GOOGLE_CHROME_BUILD)
129 return false;
130 #else
131 bool system_install = IsSystemInstall(exe_path);
132 base::string16 app_guid;
133
134 if (IsCanary(exe_path)) {
135 app_guid = kAppGuidCanary;
136 } else {
137 app_guid = IsMultiInstall(system_install) ? kAppGuidGoogleBinaries :
138 kAppGuidGoogleChrome;
139 }
140
141 DWORD out_value = 0;
142 if (system_install && ReadKeyValueDW(system_install,
grt (UTC plus 2) 2014/02/14 16:37:33 if (system_install && ReadKeyValueDW(system_
Cait (Slow) 2014/02/14 23:12:04 Done.
143 kRegPathClientStateMedium, app_guid, kRegValueUsageStats,
144 &out_value)) {
145 return out_value == 1;
146 }
147
148 return ReadKeyValueDW(system_install, kRegPathClientState,
grt (UTC plus 2) 2014/02/14 16:37:33 return ReadKeyValueDW(system_install, kRegPathClie
Cait (Slow) 2014/02/14 23:12:04 Done.
149 app_guid, kRegValueUsageStats, &out_value) && out_value == 1;
150 #endif // defined(GOOGLE_CHROME_BUILD)
151 }
152
153 bool GetUserSidString(base::string16* user_sid) {
154 // Get the current token.
155 HANDLE token = NULL;
156 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token))
grt (UTC plus 2) 2014/02/14 16:37:33 can you use base::win::ScopedHandle for the token?
Cait (Slow) 2014/02/14 23:12:04 Done.
157 return false;
158
159 DWORD size = sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE;
160 BYTE user_bytes[sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE] = {};
grt (UTC plus 2) 2014/02/14 16:37:33 BYTE user_bytes[size] = {};
Cait (Slow) 2014/02/14 23:12:04 Compiler gets upset: d:\chrome_git2\src\chrome_elf
161 TOKEN_USER* user = reinterpret_cast<TOKEN_USER*>(&user_bytes);
grt (UTC plus 2) 2014/02/14 16:37:33 &user_bytes will be a pointer to the array rather
Cait (Slow) 2014/02/14 23:12:04 Done.
162
163 if (!::GetTokenInformation(token, TokenUser, user, size, &size))
164 return false;
165
166 if (!user->User.Sid)
167 return false;
168
169 // Convert the data to a string.
170 wchar_t* sid_string;
171 if (!::ConvertSidToStringSid(user->User.Sid, &sid_string))
172 return false;
173
174 *user_sid = sid_string;
175
176 ::LocalFree(sid_string);
177
178 return true;
179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698