Bug 1026100: Add profile-do-change observer to nsSystemInfo to gather profile-dependent system information; r=bsmedberg
authorAaron Klotz <aklotz@mozilla.com>
Mon, 30 Jun 2014 21:48:57 -0600
changeset 191641 6e4201be6c45689f8e4d87f46e05ad2c6615a5c5
parent 191640 8e851f64e99fbf19a03bada9b7b3a25d01930ea5
child 191642 b21454d24b051c083fa4ab2aa2f424559db5da61
push id27055
push usercbook@mozilla.com
push dateTue, 01 Jul 2014 12:01:46 +0000
treeherdermozilla-central@4a9353b5762d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1026100
milestone33.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 1026100: Add profile-do-change observer to nsSystemInfo to gather profile-dependent system information; r=bsmedberg
xpcom/base/nsSystemInfo.cpp
xpcom/base/nsSystemInfo.h
--- a/xpcom/base/nsSystemInfo.cpp
+++ b/xpcom/base/nsSystemInfo.cpp
@@ -15,16 +15,17 @@
 
 #ifdef XP_WIN
 #include <windows.h>
 #include <winioctl.h>
 #include "base/scoped_handle_win.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsDirectoryServiceUtils.h"
+#include "nsIObserverService.h"
 #endif
 
 #ifdef MOZ_WIDGET_GTK
 #include <gtk/gtk.h>
 #endif
 
 #ifdef MOZ_WIDGET_ANDROID
 #include "AndroidBridge.h"
@@ -226,25 +227,29 @@ nsSystemInfo::Init()
   BOOL gotWow64Value = IsWow64Process(GetCurrentProcess(), &isWow64);
   NS_WARN_IF_FALSE(gotWow64Value, "IsWow64Process failed");
   if (gotWow64Value) {
     rv = SetPropertyAsBool(NS_LITERAL_STRING("isWow64"), !!isWow64);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
+  if (NS_FAILED(GetProfileHDDInfo())) {
+    // We might have been called before profile-do-change. We'll observe that
+    // event so that we can fill this in later.
+    nsCOMPtr<nsIObserverService> obsService = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+    rv = obsService->AddObserver(this, "profile-do-change", false);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+  }
   nsAutoCString hddModel, hddRevision;
-  if (NS_SUCCEEDED(GetHDDInfo(NS_APP_USER_PROFILE_50_DIR, hddModel,
-                              hddRevision))) {
-    rv = SetPropertyAsACString(NS_LITERAL_STRING("profileHDDModel"), hddModel);
-    NS_ENSURE_SUCCESS(rv, rv);
-    rv = SetPropertyAsACString(NS_LITERAL_STRING("profileHDDRevision"),
-                               hddRevision);
-    NS_ENSURE_SUCCESS(rv, rv);
-  }
   if (NS_SUCCEEDED(GetHDDInfo(NS_GRE_DIR, hddModel, hddRevision))) {
     rv = SetPropertyAsACString(NS_LITERAL_STRING("binHDDModel"), hddModel);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = SetPropertyAsACString(NS_LITERAL_STRING("binHDDRevision"),
                                hddRevision);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   if (NS_SUCCEEDED(GetHDDInfo(NS_WIN_WINDOWS_DIR, hddModel, hddRevision))) {
@@ -378,8 +383,50 @@ nsSystemInfo::SetUint64Property(const ns
   if (aValue > 0) {
 #ifdef DEBUG
     nsresult rv =
 #endif
       SetPropertyAsUint64(aPropertyName, aValue);
     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Unable to set property");
   }
 }
+
+#if defined(XP_WIN)
+NS_IMETHODIMP
+nsSystemInfo::Observe(nsISupports* aSubject, const char* aTopic,
+                      const char16_t* aData)
+{
+  if (!strcmp(aTopic, "profile-do-change")) {
+    nsresult rv;
+    nsCOMPtr<nsIObserverService> obsService = do_GetService(
+                                              NS_OBSERVERSERVICE_CONTRACTID, &rv);
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+    rv = obsService->RemoveObserver(this, "profile-do-change");
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+    return GetProfileHDDInfo();
+  }
+  return NS_OK;
+}
+
+nsresult
+nsSystemInfo::GetProfileHDDInfo()
+{
+  nsAutoCString hddModel, hddRevision;
+  nsresult rv = GetHDDInfo(NS_APP_USER_PROFILE_50_DIR, hddModel, hddRevision);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  rv = SetPropertyAsACString(NS_LITERAL_STRING("profileHDDModel"), hddModel);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  rv = SetPropertyAsACString(NS_LITERAL_STRING("profileHDDRevision"),
+                             hddRevision);
+  return rv;
+}
+
+NS_IMPL_ISUPPORTS_INHERITED(nsSystemInfo, nsHashPropertyBag, nsIObserver)
+#endif // defined(XP_WIN)
+
--- a/xpcom/base/nsSystemInfo.h
+++ b/xpcom/base/nsSystemInfo.h
@@ -3,20 +3,32 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef _NSSYSTEMINFO_H_
 #define _NSSYSTEMINFO_H_
 
 #include "nsHashPropertyBag.h"
+#if defined(XP_WIN)
+#include "nsIObserver.h"
+#endif // defined(XP_WIN)
 
-class nsSystemInfo : public nsHashPropertyBag
+class nsSystemInfo
+  : public nsHashPropertyBag
+#if defined(XP_WIN)
+  , public nsIObserver
+#endif // defined(XP_WIN)
 {
 public:
+#if defined(XP_WIN)
+  NS_DECL_ISUPPORTS_INHERITED
+  NS_DECL_NSIOBSERVER
+#endif // defined(XP_WIN)
+
   nsSystemInfo();
 
   nsresult Init();
 
   // Slot for NS_InitXPCOM2 to pass information to nsSystemInfo::Init.
   // See comments above the variable definition and in NS_InitXPCOM2.
   static uint32_t gUserUmask;
 
@@ -25,16 +37,20 @@ protected:
                         const int32_t aValue);
   void SetUint32Property(const nsAString& aPropertyName,
                          const uint32_t aValue);
   void SetUint64Property(const nsAString& aPropertyName,
                          const uint64_t aValue);
 
 private:
   ~nsSystemInfo();
+
+#if defined(XP_WIN)
+  nsresult GetProfileHDDInfo();
+#endif // defined(XP_WIN)
 };
 
 #define NS_SYSTEMINFO_CONTRACTID "@mozilla.org/system-info;1"
 #define NS_SYSTEMINFO_CID \
 { 0xd962398a, 0x99e5, 0x49b2, \
 { 0x85, 0x7a, 0xc1, 0x59, 0x04, 0x9c, 0x7f, 0x6c } }
 
 #endif /* _NSSYSTEMINFO_H_ */