OLD | NEW |
---|---|
1 // Copyright (c) 2011 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 // NOTE: This code is a legacy utility API for partners to checking whether | |
grt (UTC plus 2)
2011/11/11 17:01:01
checking -> check
robertshield
2011/11/12 05:25:38
Done.
| |
6 // Chrome can be installed and launched. Recent updates are being made | |
7 // to add new functionality. These updates use code from Chromium, the old | |
8 // coded against the win32 api directly. If you have an itch to shave a | |
9 // yak, feel free to re-write the old code too. | |
10 | |
5 #include "chrome/installer/gcapi/gcapi.h" | 11 #include "chrome/installer/gcapi/gcapi.h" |
6 | 12 |
7 #include <atlbase.h> | 13 #include <atlbase.h> |
8 #include <atlcom.h> | 14 #include <atlcom.h> |
9 #include <windows.h> | |
10 #include <sddl.h> | 15 #include <sddl.h> |
11 #define STRSAFE_NO_DEPRECATE | 16 #define STRSAFE_NO_DEPRECATE |
12 #include <strsafe.h> | 17 #include <strsafe.h> |
13 #include <tlhelp32.h> | 18 #include <tlhelp32.h> |
19 #include <windows.h> | |
14 | 20 |
15 #include <cstdlib> | 21 #include <cstdlib> |
22 #include <limits> | |
23 #include <string> | |
16 | 24 |
17 #include "google_update_idl.h" | 25 #include "base/basictypes.h" |
26 #include "base/string_number_conversions.h" | |
27 #include "base/time.h" | |
28 #include "base/win/registry.h" | |
29 #include "chrome/installer/util/google_update_constants.h" | |
30 | |
31 #include "google_update_idl.h" // NOLINT | |
18 | 32 |
19 namespace { | 33 namespace { |
20 | 34 |
21 const wchar_t kChromeRegClientsKey[] = | 35 const wchar_t kChromeRegClientsKey[] = |
22 L"Software\\Google\\Update\\Clients\\" | 36 L"Software\\Google\\Update\\Clients\\" |
23 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | 37 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
24 const wchar_t kChromeRegClientStateKey[] = | 38 const wchar_t kChromeRegClientStateKey[] = |
25 L"Software\\Google\\Update\\ClientState\\" | 39 L"Software\\Google\\Update\\ClientState\\" |
26 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | 40 L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
27 const wchar_t kChromeRegLaunchCmd[] = L"InstallerSuccessLaunchCmdLine"; | 41 const wchar_t kChromeRegLaunchCmd[] = L"InstallerSuccessLaunchCmdLine"; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 memcpy(&lang, data, 4); | 77 memcpy(&lang, data, 4); |
64 ::StringCchPrintf(info_name, _countof(info_name), | 78 ::StringCchPrintf(info_name, _countof(info_name), |
65 L"\\StringFileInfo\\%02X%02X%02X%02X\\CompanyName", | 79 L"\\StringFileInfo\\%02X%02X%02X%02X\\CompanyName", |
66 (lang & 0xff00)>>8, (lang & 0xff), (lang & 0xff000000)>>24, | 80 (lang & 0xff00)>>8, (lang & 0xff), (lang & 0xff000000)>>24, |
67 (lang & 0xff0000)>>16); | 81 (lang & 0xff0000)>>16); |
68 | 82 |
69 data_len = 0; | 83 data_len = 0; |
70 if (!::VerQueryValue(file_version_info, info_name, | 84 if (!::VerQueryValue(file_version_info, info_name, |
71 reinterpret_cast<LPVOID *>(&data), reinterpret_cast<UINT *>(&data_len))) | 85 reinterpret_cast<LPVOID *>(&data), reinterpret_cast<UINT *>(&data_len))) |
72 return false; | 86 return false; |
73 if (data_len <= 0 || data_len >= out_len) | 87 if (data_len <= 0 || data_len >= (out_len / sizeof(wchar_t))) |
74 return false; | 88 return false; |
75 | 89 |
76 memset(buffer, 0, out_len); | 90 memset(buffer, 0, out_len); |
77 ::StringCchCopyN(buffer, out_len, (const wchar_t*)data, data_len); | 91 ::StringCchCopyN(buffer, |
92 (out_len / sizeof(wchar_t)), | |
93 reinterpret_cast<const wchar_t*>(data), | |
94 data_len); | |
78 return true; | 95 return true; |
79 } | 96 } |
80 | 97 |
81 // Return true if we can re-offer Chrome; false, otherwise. | 98 // Return true if we can re-offer Chrome; false, otherwise. |
82 // Each partner can only offer Chrome once every six months. | 99 // Each partner can only offer Chrome once every six months. |
83 bool CanReOfferChrome(BOOL set_flag) { | 100 bool CanReOfferChrome(BOOL set_flag) { |
84 wchar_t filename[MAX_PATH+1]; | 101 wchar_t filename[MAX_PATH+1]; |
85 wchar_t company[MAX_PATH]; | 102 wchar_t company[MAX_PATH]; |
86 | 103 |
87 // If we cannot retrieve the version info of the executable or company | 104 // If we cannot retrieve the version info of the executable or company |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 } | 150 } |
134 | 151 |
135 ::RegCloseKey(key); | 152 ::RegCloseKey(key); |
136 } | 153 } |
137 | 154 |
138 return can_re_offer; | 155 return can_re_offer; |
139 } | 156 } |
140 | 157 |
141 // Helper function to read a value from registry. Returns true if value | 158 // Helper function to read a value from registry. Returns true if value |
142 // is read successfully and stored in parameter value. Returns false otherwise. | 159 // is read successfully and stored in parameter value. Returns false otherwise. |
143 bool ReadValueFromRegistry(HKEY root_key, const wchar_t *sub_key, | 160 bool ReadValueFromRegistry(HKEY root_key, const wchar_t* sub_key, |
144 const wchar_t *value_name, wchar_t *value, | 161 const wchar_t* value_name, wchar_t* value, |
145 size_t *size) { | 162 size_t* size) { |
146 HKEY key; | 163 HKEY key; |
147 if ((::RegOpenKeyEx(root_key, sub_key, NULL, | 164 if ((::RegOpenKeyEx(root_key, sub_key, NULL, |
148 KEY_READ, &key) == ERROR_SUCCESS) && | 165 KEY_READ, &key) == ERROR_SUCCESS) && |
149 (::RegQueryValueEx(key, value_name, NULL, NULL, | 166 (::RegQueryValueEx(key, value_name, NULL, NULL, |
150 reinterpret_cast<LPBYTE>(value), | 167 reinterpret_cast<LPBYTE>(value), |
151 reinterpret_cast<LPDWORD>(size)) == ERROR_SUCCESS)) { | 168 reinterpret_cast<LPDWORD>(size)) == ERROR_SUCCESS)) { |
152 ::RegCloseKey(key); | 169 ::RegCloseKey(key); |
153 return true; | 170 return true; |
154 } | 171 } |
155 return false; | 172 return false; |
156 } | 173 } |
157 | 174 |
158 bool IsChromeInstalled(HKEY root_key) { | 175 bool IsChromeInstalled(HKEY root_key) { |
159 wchar_t version[64]; | 176 wchar_t version[64]; |
160 size_t size = _countof(version); | 177 size_t size = _countof(version); |
161 return ReadValueFromRegistry(root_key, kChromeRegClientsKey, | 178 return ReadValueFromRegistry(root_key, kChromeRegClientsKey, |
162 kChromeRegVersion, version, &size); | 179 kChromeRegVersion, version, &size); |
163 } | 180 } |
164 | 181 |
165 enum WindowsVersion { | 182 enum WindowsVersion { |
166 VERSION_BELOW_XP_SP2, | 183 VERSION_BELOW_XP_SP2, |
167 VERSION_XP_SP2_UP_TO_VISTA, // "but not including" | 184 VERSION_XP_SP2_UP_TO_VISTA, // "but not including" |
168 VERSION_VISTA_OR_HIGHER, | 185 VERSION_VISTA_OR_HIGHER, |
169 }; | 186 }; |
170 WindowsVersion GetWindowsVersion() { | 187 WindowsVersion GetWindowsVersion() { |
171 OSVERSIONINFOEX version_info = { sizeof version_info }; | 188 OSVERSIONINFOEX version_info = { sizeof version_info }; |
172 GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); | 189 GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); |
173 | 190 |
174 // Windows Vista is version 6.0. | 191 // Windows Vista is version 6.0. |
175 if (version_info.dwMajorVersion >= 6) | 192 if (version_info.dwMajorVersion >= 6) |
176 return VERSION_VISTA_OR_HIGHER; | 193 return VERSION_VISTA_OR_HIGHER; |
177 | 194 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 } | 295 } |
279 ::CloseHandle(process_token); | 296 ::CloseHandle(process_token); |
280 } | 297 } |
281 ::CloseHandle(process_handle); | 298 ::CloseHandle(process_handle); |
282 return result; | 299 return result; |
283 } | 300 } |
284 } // namespace | 301 } // namespace |
285 | 302 |
286 #pragma comment(linker, "/EXPORT:GoogleChromeCompatibilityCheck=_GoogleChromeCom patibilityCheck@8,PRIVATE") | 303 #pragma comment(linker, "/EXPORT:GoogleChromeCompatibilityCheck=_GoogleChromeCom patibilityCheck@8,PRIVATE") |
287 DLLEXPORT BOOL __stdcall GoogleChromeCompatibilityCheck(BOOL set_flag, | 304 DLLEXPORT BOOL __stdcall GoogleChromeCompatibilityCheck(BOOL set_flag, |
288 DWORD *reasons) { | 305 DWORD* reasons) { |
289 DWORD local_reasons = 0; | 306 DWORD local_reasons = 0; |
290 | 307 |
291 WindowsVersion windows_version = GetWindowsVersion(); | 308 WindowsVersion windows_version = GetWindowsVersion(); |
292 // System requirements? | 309 // System requirements? |
293 if (windows_version == VERSION_BELOW_XP_SP2) | 310 if (windows_version == VERSION_BELOW_XP_SP2) |
294 local_reasons |= GCCC_ERROR_OSNOTSUPPORTED; | 311 local_reasons |= GCCC_ERROR_OSNOTSUPPORTED; |
295 | 312 |
296 if (IsChromeInstalled(HKEY_LOCAL_MACHINE)) | 313 if (IsChromeInstalled(HKEY_LOCAL_MACHINE)) |
297 local_reasons |= GCCC_ERROR_SYSTEMLEVELALREADYPRESENT; | 314 local_reasons |= GCCC_ERROR_SYSTEMLEVELALREADYPRESENT; |
298 | 315 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 // This loop iterates through all of the top-level Windows named | 461 // This loop iterates through all of the top-level Windows named |
445 // Chrome_WindowImpl_0, and looks for the first one with any children. | 462 // Chrome_WindowImpl_0, and looks for the first one with any children. |
446 while (handle && !FindWindowEx(handle, NULL, L"Chrome_WindowImpl_0", NULL)) { | 463 while (handle && !FindWindowEx(handle, NULL, L"Chrome_WindowImpl_0", NULL)) { |
447 // Get the next top-level Chrome window. | 464 // Get the next top-level Chrome window. |
448 handle = FindWindowEx(NULL, handle, L"Chrome_WindowImpl_0", NULL); | 465 handle = FindWindowEx(NULL, handle, L"Chrome_WindowImpl_0", NULL); |
449 } | 466 } |
450 | 467 |
451 return (handle && | 468 return (handle && |
452 SetWindowPos(handle, 0, x, y, width, height, SWP_NOZORDER)); | 469 SetWindowPos(handle, 0, x, y, width, height, SWP_NOZORDER)); |
453 } | 470 } |
471 | |
472 #pragma comment(linker, "/EXPORT:LaunchGoogleChromeWithDimensions=_LaunchGoogleC hromeWithDimensions@24,PRIVATE") | |
grt (UTC plus 2)
2011/11/11 17:01:01
LaunchGoogleChromeWithDimensions -> GoogleChromeDa
robertshield
2011/11/12 05:25:38
heh, done
| |
473 DLLEXPORT int __stdcall GoogleChromeDaysSinceLastRun() { | |
474 using base::win::RegKey; | |
475 using base::Time; | |
476 using base::TimeDelta; | |
477 | |
478 int days_since_last_run = std::numeric_limits<int>::max(); | |
479 | |
480 HKEY hives[] = {HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER}; | |
grt (UTC plus 2)
2011/11/11 17:01:01
Chrome doesn't even try to write to HKLM since it
robertshield
2011/11/12 05:25:38
so, as discussed modified this to assume that one
| |
481 for (int i = 0; i < arraysize(hives); ++i) { | |
482 RegKey client_state(hives[i], | |
483 kChromeRegClientStateKey, | |
484 KEY_QUERY_VALUE); | |
485 if (client_state.Valid()) { | |
486 std::wstring last_run; | |
487 int64 last_run_value = 0; | |
488 if (client_state.ReadValue(google_update::kRegLastRunTimeField, | |
489 &last_run) == ERROR_SUCCESS && | |
490 base::StringToInt64(last_run, &last_run_value)) { | |
491 Time last_run_time = Time::FromInternalValue(last_run_value); | |
492 TimeDelta difference = Time::NowFromSystemTime() - last_run_time; | |
493 int days_elapsed = difference.InDays(); | |
grt (UTC plus 2)
2011/11/11 17:01:01
include <algorithm> and replace these four lines w
robertshield
2011/11/12 05:25:38
Done.
| |
494 if (days_elapsed < days_since_last_run) { | |
495 days_since_last_run = days_elapsed; | |
496 } | |
497 } | |
498 } | |
499 } | |
500 | |
501 if (days_since_last_run == std::numeric_limits<int>::max()) { | |
502 days_since_last_run = -1; | |
503 } | |
504 | |
505 return days_since_last_run; | |
506 } | |
OLD | NEW |