Bug 669028 part.1 Remove nsIObserver from nsXPLookAndFeel r=roc
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 09 Sep 2011 11:27:11 +0900
changeset 76807 71486524f1ed28a41a8bfb6ac6188f5c31202858
parent 76806 cc0753a23f8b1411ca0780c7b018beb4166655bc
child 76808 7e1031d213ba3764606578c23e85f97fef9ace57
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersroc
bugs669028
milestone9.0a1
Bug 669028 part.1 Remove nsIObserver from nsXPLookAndFeel r=roc
widget/src/xpwidgets/nsXPLookAndFeel.cpp
widget/src/xpwidgets/nsXPLookAndFeel.h
--- a/widget/src/xpwidgets/nsXPLookAndFeel.cpp
+++ b/widget/src/xpwidgets/nsXPLookAndFeel.cpp
@@ -46,17 +46,17 @@
 #include "qcms.h"
 
 #ifdef DEBUG
 #include "nsSize.h"
 #endif
 
 using namespace mozilla;
 
-NS_IMPL_ISUPPORTS2(nsXPLookAndFeel, nsILookAndFeel, nsIObserver)
+NS_IMPL_ISUPPORTS1(nsXPLookAndFeel, nsILookAndFeel)
 
 nsLookAndFeelIntPref nsXPLookAndFeel::sIntPrefs[] =
 {
   { "ui.caretBlinkTime", eMetric_CaretBlinkTime, PR_FALSE, nsLookAndFeelTypeInt, 0 },
   { "ui.caretWidth", eMetric_CaretWidth, PR_FALSE, nsLookAndFeelTypeInt, 0 },
   { "ui.caretVisibleWithSelection", eMetric_ShowCaretDuringSelection, PR_FALSE, nsLookAndFeelTypeInt, 0 },
   { "ui.submenuDelay", eMetric_SubmenuDelay, PR_FALSE, nsLookAndFeelTypeInt, 0 },
   { "ui.dragThresholdX", eMetric_DragThresholdX, PR_FALSE, nsLookAndFeelTypeInt, 0 },
@@ -212,16 +212,17 @@ PRInt32 nsXPLookAndFeel::sCachedColorBit
 
 PRBool nsXPLookAndFeel::sInitialized = PR_FALSE;
 PRBool nsXPLookAndFeel::sUseNativeColors = PR_TRUE;
 
 nsXPLookAndFeel::nsXPLookAndFeel() : nsILookAndFeel()
 {
 }
 
+// static
 void
 nsXPLookAndFeel::IntPrefChanged (nsLookAndFeelIntPref *data)
 {
   if (!data) {
     return;
   }
 
   PRInt32 intpref;
@@ -231,16 +232,17 @@ nsXPLookAndFeel::IntPrefChanged (nsLookA
   }
   data->intVar = intpref;
   data->isSet = PR_TRUE;
 #ifdef DEBUG_akkana
   printf("====== Changed int pref %s to %d\n", data->name, data->intVar);
 #endif
 }
 
+// static
 void
 nsXPLookAndFeel::FloatPrefChanged (nsLookAndFeelFloatPref *data)
 {
   if (!data) {
     return;
   }
 
   PRInt32 intpref;
@@ -250,16 +252,17 @@ nsXPLookAndFeel::FloatPrefChanged (nsLoo
   }
   data->floatVar = (float)intpref / 100.;
   data->isSet = PR_TRUE;
 #ifdef DEBUG_akkana
   printf("====== Changed float pref %s to %f\n", data->name, data->floatVar);
 #endif
 }
 
+// static
 void
 nsXPLookAndFeel::ColorPrefChanged (unsigned int index, const char *prefName)
 {
   nsAutoString colorStr;
   nsresult rv = Preferences::GetString(prefName, &colorStr);
   if (NS_FAILED(rv)) {
     return;
   }
@@ -325,76 +328,79 @@ nsXPLookAndFeel::InitColorFromPref(PRInt
     if (NS_HexToRGB(hexString, &thecolor)) {
       CACHE_COLOR(i, thecolor);
     }
   } else if (NS_ColorNameToRGB(colorStr, &thecolor)) {
     CACHE_COLOR(i, thecolor);
   }
 }
 
-NS_IMETHODIMP
-nsXPLookAndFeel::Observe(nsISupports*     aSubject,
-                         const char*      aTopic,
-                         const PRUnichar* aData)
+// static
+int
+nsXPLookAndFeel::OnPrefChanged(const char* aPref, void* aClosure)
 {
 
   // looping in the same order as in ::Init
 
+  nsDependentCString prefName(aPref);
   unsigned int i;
   for (i = 0; i < NS_ARRAY_LENGTH(sIntPrefs); ++i) {
-    if (nsDependentString(aData).EqualsASCII(sIntPrefs[i].name)) {
+    if (prefName.Equals(sIntPrefs[i].name)) {
       IntPrefChanged(&sIntPrefs[i]);
-      return NS_OK;
+      return 0;
     }
   }
 
   for (i = 0; i < NS_ARRAY_LENGTH(sFloatPrefs); ++i) {
-    if (nsDependentString(aData).EqualsASCII(sFloatPrefs[i].name)) {
+    if (prefName.Equals(sFloatPrefs[i].name)) {
       FloatPrefChanged(&sFloatPrefs[i]);
-      return NS_OK;
+      return 0;
     }
   }
 
   for (i = 0; i < NS_ARRAY_LENGTH(sColorPrefs); ++i) {
-    if (nsDependentString(aData).EqualsASCII(sColorPrefs[i])) {
+    if (prefName.Equals(sColorPrefs[i])) {
       ColorPrefChanged(i, sColorPrefs[i]);
-      return NS_OK;
+      return 0;
     }
   }
 
-  return NS_OK;
+  return 0;
 }
 
 //
 // Read values from the user's preferences.
 // This is done once at startup, but since the user's preferences
 // haven't actually been read yet at that time, we also have to
 // set a callback to inform us of changes to each pref.
 //
 void
 nsXPLookAndFeel::Init()
 {
   // Say we're already initialized, and take the chance that it might fail;
   // protects against some other process writing to our static variables.
   sInitialized = PR_TRUE;
 
+  // XXX If we could reorganize the pref names, we should separate the branch
+  //     for each types.  Then, we could reduce the unnecessary loop from
+  //     nsXPLookAndFeel::OnPrefChanged().
+  Preferences::RegisterCallback(OnPrefChanged, "ui.");
+  Preferences::RegisterCallback(OnPrefChanged, "accessibility.tabfocus");
+
   unsigned int i;
   for (i = 0; i < NS_ARRAY_LENGTH(sIntPrefs); ++i) {
     InitFromPref(&sIntPrefs[i]);
-    Preferences::AddStrongObserver(this, sIntPrefs[i].name);
   }
 
   for (i = 0; i < NS_ARRAY_LENGTH(sFloatPrefs); ++i) {
     InitFromPref(&sFloatPrefs[i]);
-    Preferences::AddStrongObserver(this, sFloatPrefs[i].name);
   }
 
   for (i = 0; i < NS_ARRAY_LENGTH(sColorPrefs); ++i) {
     InitColorFromPref(i);
-    Preferences::AddStrongObserver(this, sColorPrefs[i]);
   }
 
   PRBool val;
   if (NS_SUCCEEDED(Preferences::GetBool("ui.use_native_colors", &val))) {
     sUseNativeColors = val;
   }
 }
 
--- a/widget/src/xpwidgets/nsXPLookAndFeel.h
+++ b/widget/src/xpwidgets/nsXPLookAndFeel.h
@@ -35,17 +35,16 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef __nsXPLookAndFeel
 #define __nsXPLookAndFeel
 
 #include "nsILookAndFeel.h"
 #include "nsCOMPtr.h"
-#include "nsIObserver.h"
 
 #ifdef NS_DEBUG
 struct nsSize;
 #endif
 
 typedef enum {
   nsLookAndFeelTypeInt,
   nsLookAndFeelTypeFloat,
@@ -75,26 +74,24 @@ struct nsLookAndFeelFloatPref
 
 #define COLOR_CACHE_SIZE   (CACHE_BLOCK(nsILookAndFeel::eColor_LAST_COLOR) + 1)
 #define IS_COLOR_CACHED(x) (CACHE_BIT(x) & nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)])
 #define CLEAR_COLOR_CACHE(x) nsXPLookAndFeel::sCachedColors[(x)] =0; \
               nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)] &= ~(CACHE_BIT(x));
 #define CACHE_COLOR(x, y)  nsXPLookAndFeel::sCachedColors[(x)] = y; \
               nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)] |= CACHE_BIT(x);
 
-class nsXPLookAndFeel: public nsILookAndFeel, public nsIObserver
+class nsXPLookAndFeel: public nsILookAndFeel
 {
 public:
   nsXPLookAndFeel();
   virtual ~nsXPLookAndFeel();
 
   NS_DECL_ISUPPORTS
 
-  NS_DECL_NSIOBSERVER
-
   void Init();
 
   //
   // All these routines will return NS_OK if they have a value,
   // in which case the nsLookAndFeel should use that value;
   // otherwise we'll return NS_ERROR_NOT_AVAILABLE, in which case, the
   // platform-specific nsLookAndFeel should use its own values instead.
   //
@@ -107,25 +104,27 @@ public:
 #ifdef NS_DEBUG
   NS_IMETHOD GetNavSize(const nsMetricNavWidgetID aWidgetID,
                         const nsMetricNavFontID   aFontID, 
                         const PRInt32             aFontSize, 
                         nsSize &aSize);
 #endif
 
 protected:
-  void IntPrefChanged(nsLookAndFeelIntPref *data);
-  void FloatPrefChanged(nsLookAndFeelFloatPref *data);
-  void ColorPrefChanged(unsigned int index, const char *prefName);
+  static void IntPrefChanged(nsLookAndFeelIntPref *data);
+  static void FloatPrefChanged(nsLookAndFeelFloatPref *data);
+  static void ColorPrefChanged(unsigned int index, const char *prefName);
   void InitFromPref(nsLookAndFeelIntPref* aPref);
   void InitFromPref(nsLookAndFeelFloatPref* aPref);
   void InitColorFromPref(PRInt32 aIndex);
   virtual nsresult NativeGetColor(const nsColorID aID, nscolor& aColor) = 0;
   PRBool IsSpecialColor(const nsColorID aID, nscolor &aColor);
 
+  static int OnPrefChanged(const char* aPref, void* aClosure);
+
   static PRBool sInitialized;
   static nsLookAndFeelIntPref sIntPrefs[];
   static nsLookAndFeelFloatPref sFloatPrefs[];
   /* this length must not be shorter than the length of the longest string in the array
    * see nsXPLookAndFeel.cpp
    */
   static const char sColorPrefs[][38];
   static PRInt32 sCachedColors[nsILookAndFeel::eColor_LAST_COLOR];