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

Side by Side Diff: runtime/bin/platform_win.cc

Issue 3006873002: [dart:io] Adds Platform.operatingSystemVersion (Closed)
Patch Set: Updated CHANGELOG Created 3 years, 3 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
« no previous file with comments | « runtime/bin/platform_patch.dart ('k') | sdk/lib/_internal/js_runtime/lib/io_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(HOST_OS_WINDOWS) 6 #if defined(HOST_OS_WINDOWS)
7 7
8 #include "bin/platform.h" 8 #include "bin/platform.h"
9 9
10 #include <crtdbg.h> 10 #include <crtdbg.h>
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 int Platform::NumberOfProcessors() { 206 int Platform::NumberOfProcessors() {
207 SYSTEM_INFO info; 207 SYSTEM_INFO info;
208 GetSystemInfo(&info); 208 GetSystemInfo(&info);
209 return info.dwNumberOfProcessors; 209 return info.dwNumberOfProcessors;
210 } 210 }
211 211
212 const char* Platform::OperatingSystem() { 212 const char* Platform::OperatingSystem() {
213 return "windows"; 213 return "windows";
214 } 214 }
215 215
216 // We pull the version number, and other version information out of the
217 // registry because GetVersionEx() and friends lie about the OS version after
218 // Windows 8.1. See:
219 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724451(v=vs.85).as px
220 static const wchar_t* kCurrentVersion =
221 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
222
223 static bool GetCurrentVersionDWord(const wchar_t* field, DWORD* value) {
224 DWORD value_size = sizeof(*value);
225 LONG err = RegGetValue(HKEY_LOCAL_MACHINE, kCurrentVersion, field,
226 RRF_RT_REG_DWORD, NULL, value, &value_size);
227 return err == ERROR_SUCCESS;
228 }
229
230 static bool GetCurrentVersionString(const wchar_t* field, const char** value) {
231 wchar_t wversion[256];
232 DWORD wversion_size = sizeof(wversion);
233 LONG err = RegGetValue(HKEY_LOCAL_MACHINE, kCurrentVersion, field,
234 RRF_RT_REG_SZ, NULL, wversion, &wversion_size);
235 if (err != ERROR_SUCCESS) {
236 return false;
237 }
238 *value = StringUtilsWin::WideToUtf8(wversion);
239 return true;
240 }
241
242 static const char* VersionNumber() {
243 // Try to get CurrentMajorVersionNumber. If that fails, fall back on
244 // CurrentVersion. If it succeeds also get CurrentMinorVersionNumber.
245 DWORD major;
246 if (!GetCurrentVersionDWord(L"CurrentMajorVersionNumber", &major)) {
247 const char* version;
248 if (!GetCurrentVersionString(L"CurrentVersion", &version)) {
249 return NULL;
250 }
251 return version;
252 }
253
254 DWORD minor;
255 if (!GetCurrentVersionDWord(L"CurrentMinorVersionNumber", &minor)) {
256 return NULL;
257 }
258 const char* kFormat = "%d.%d";
259 int len = snprintf(NULL, 0, kFormat, major, minor);
260 if (len < 0) {
261 return NULL;
262 }
263 char* result = DartUtils::ScopedCString(len + 1);
264 ASSERT(result != NULL);
265 len = snprintf(result, len + 1, kFormat, major, minor);
266 if (len < 0) {
267 return NULL;
268 }
269 return result;
270 }
271
272 const char* Platform::OperatingSystemVersion() {
273 // Get the product name, e.g. "Windows 10 Home".
274 const char* name;
275 if (!GetCurrentVersionString(L"ProductName", &name)) {
276 return NULL;
277 }
278
279 // Get the version number, e.g. "10.0".
280 const char* version_number = VersionNumber();
281 if (version_number == NULL) {
282 return NULL;
283 }
284
285 // Get the build number.
286 const char* build;
287 if (!GetCurrentVersionString(L"CurrentBuild", &build)) {
288 return NULL;
289 }
290
291 // Put it all together.
292 const char* kFormat = "\"%s\" %s (Build %s)";
293 int len = snprintf(NULL, 0, kFormat, name, version_number, build);
294 char* result = DartUtils::ScopedCString(len + 1);
295 len = snprintf(result, len + 1, kFormat, name, version_number, build);
296 return result;
297 }
298
216 const char* Platform::LibraryPrefix() { 299 const char* Platform::LibraryPrefix() {
217 return ""; 300 return "";
218 } 301 }
219 302
220 const char* Platform::LibraryExtension() { 303 const char* Platform::LibraryExtension() {
221 return "dll"; 304 return "dll";
222 } 305 }
223 306
224 const char* Platform::LocaleName() { 307 const char* Platform::LocaleName() {
225 wchar_t locale_name[LOCALE_NAME_MAX_LENGTH]; 308 wchar_t locale_name[LOCALE_NAME_MAX_LENGTH];
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 PlatformWin::RestoreConsole(); 387 PlatformWin::RestoreConsole();
305 // On Windows we use ExitProcess so that threads can't clobber the exit_code. 388 // On Windows we use ExitProcess so that threads can't clobber the exit_code.
306 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 389 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870
307 ::ExitProcess(exit_code); 390 ::ExitProcess(exit_code);
308 } 391 }
309 392
310 } // namespace bin 393 } // namespace bin
311 } // namespace dart 394 } // namespace dart
312 395
313 #endif // defined(HOST_OS_WINDOWS) 396 #endif // defined(HOST_OS_WINDOWS)
OLDNEW
« no previous file with comments | « runtime/bin/platform_patch.dart ('k') | sdk/lib/_internal/js_runtime/lib/io_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698