Bug 1132558: Add Windows install year to telemetry; r=aklotz
authorAnton Myagkov <myagkov-a@yandex.ru>
Wed, 22 Apr 2015 21:28:43 +0300
changeset 242162 b8af7eb3f5131d98e63a23792ca9c2a89b0a7ade
parent 242137 dc5f85980a828aaa32b599b15b93ab8d1287ff5b
child 242163 92f70afc7a26d85844b40aa864836644a92c6dc3
push id28683
push usercbook@mozilla.com
push dateMon, 04 May 2015 11:42:26 +0000
treeherdermozilla-central@34828fed1639 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1132558
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1132558: Add Windows install year to telemetry; r=aklotz
toolkit/components/telemetry/TelemetryEnvironment.jsm
xpcom/base/nsSystemInfo.cpp
--- a/toolkit/components/telemetry/TelemetryEnvironment.jsm
+++ b/toolkit/components/telemetry/TelemetryEnvironment.jsm
@@ -982,16 +982,17 @@ EnvironmentCache.prototype = {
     return {
       name: getSysinfoProperty("name", null),
       version: getSysinfoProperty("version", null),
 #if defined(MOZ_WIDGET_GONK) || defined(MOZ_WIDGET_ANDROID)
       kernelVersion: getSysinfoProperty("kernel_version", null),
 #elif defined(XP_WIN)
       servicePackMajor: servicePack.major,
       servicePackMinor: servicePack.minor,
+      installYear: getSysinfoProperty("installYear", null),
 #endif
       locale: getSystemLocale(),
     };
   },
 
   /**
    * Get the HDD information.
    * @return Object containing the HDD data.
--- a/xpcom/base/nsSystemInfo.cpp
+++ b/xpcom/base/nsSystemInfo.cpp
@@ -9,23 +9,25 @@
 #include "nsSystemInfo.h"
 #include "prsystem.h"
 #include "prio.h"
 #include "prprf.h"
 #include "mozilla/SSE.h"
 #include "mozilla/arm.h"
 
 #ifdef XP_WIN
+#include <time.h>
 #include <windows.h>
 #include <winioctl.h>
 #include "base/scoped_handle_win.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsDirectoryServiceUtils.h"
 #include "nsIObserverService.h"
+#include "nsWindowsHelpers.h"
 #endif
 
 #ifdef MOZ_WIDGET_GTK
 #include <gtk/gtk.h>
 #endif
 
 #ifdef MOZ_WIDGET_ANDROID
 #include "AndroidBridge.h"
@@ -125,16 +127,56 @@ GetHDDInfo(const char* aSpecialDirName, 
   if (deviceOutput->ProductRevisionOffset) {
     aRevision = reinterpret_cast<char*>(deviceOutput) +
       deviceOutput->ProductRevisionOffset;
     aRevision.CompressWhitespace();
   }
   free(deviceOutput);
   return NS_OK;
 }
+
+nsresult GetInstallYear(uint32_t& aYear)
+{
+  HKEY hKey;
+  LONG status = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+                              NS_LITERAL_STRING(
+                              "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
+                              ).get(),
+                              0, KEY_READ | KEY_WOW64_64KEY, &hKey);
+
+  if (status != ERROR_SUCCESS) {
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  nsAutoRegKey key(hKey);
+
+  DWORD type = 0;
+  time_t raw_time = 0;
+  DWORD time_size = sizeof(time_t);
+
+  status = RegQueryValueExW(hKey, NS_LITERAL_STRING("InstallDate").get(),
+                            nullptr, &type, (LPBYTE)&raw_time, &time_size);
+
+  if (status != ERROR_SUCCESS) {
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  if (type != REG_DWORD) {
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  tm time;
+  if (localtime_s(&time, &raw_time) != 0) {
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  aYear = 1900UL + time.tm_year;
+  return NS_OK;
+}
+
 } // anonymous namespace
 #endif // defined(XP_WIN)
 
 using namespace mozilla;
 
 nsSystemInfo::nsSystemInfo()
 {
 }
@@ -247,16 +289,24 @@ nsSystemInfo::Init()
   }
   if (NS_SUCCEEDED(GetHDDInfo(NS_WIN_WINDOWS_DIR, hddModel, hddRevision))) {
     rv = SetPropertyAsACString(NS_LITERAL_STRING("winHDDModel"), hddModel);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = SetPropertyAsACString(NS_LITERAL_STRING("winHDDRevision"),
                                hddRevision);
     NS_ENSURE_SUCCESS(rv, rv);
   }
+
+  uint32_t installYear = 0;
+  if (NS_SUCCEEDED(GetInstallYear(installYear))) {
+    rv = SetPropertyAsUint32(NS_LITERAL_STRING("installYear"), installYear);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+  }
 #endif
 
 #if defined(MOZ_WIDGET_GTK)
   // This must be done here because NSPR can only separate OS's when compiled, not libraries.
   char* gtkver = PR_smprintf("GTK %u.%u.%u", gtk_major_version,
                              gtk_minor_version, gtk_micro_version);
   if (gtkver) {
     rv = SetPropertyAsACString(NS_LITERAL_STRING("secondaryLibrary"),