Bug 1403690 Part 5: Android change pref and color caching to match approach used in gtk, macOS, and Windows. r=snorp
authorBrad Werth <bwerth@mozilla.com>
Tue, 10 Oct 2017 13:12:28 -0700
changeset 688096 436c11e3c38952bf698fe6e5a14f7376b7905a31
parent 688095 4df8244690cf51becf42e70ce846c91c92e3780c
child 688097 c9589d98c0b89ff3a34c5473b957df6e9f97530a
push id86669
push userhikezoe@mozilla.com
push dateSat, 28 Oct 2017 10:13:18 +0000
reviewerssnorp
bugs1403690
milestone58.0a1
Bug 1403690 Part 5: Android change pref and color caching to match approach used in gtk, macOS, and Windows. r=snorp MozReview-Commit-ID: DoffjB9QMmH
widget/android/nsLookAndFeel.cpp
widget/android/nsLookAndFeel.h
--- a/widget/android/nsLookAndFeel.cpp
+++ b/widget/android/nsLookAndFeel.cpp
@@ -37,26 +37,21 @@ nsLookAndFeel::~nsLookAndFeel()
 #define DARK_GRAY_COLOR        NS_RGB(0x40,0x40,0x40)
 #define GRAY_COLOR             NS_RGB(0x80,0x80,0x80)
 #define LIGHT_GRAY_COLOR       NS_RGB(0xa0,0xa0,0xa0)
 #define RED_COLOR              NS_RGB(0xff,0x00,0x00)
 
 nsresult
 nsLookAndFeel::GetSystemColors()
 {
-    if (mInitializedSystemColors)
-        return NS_OK;
-
     if (!AndroidBridge::Bridge())
         return NS_ERROR_FAILURE;
 
     AndroidBridge::Bridge()->GetSystemColors(&mSystemColors);
 
-    mInitializedSystemColors = true;
-
     return NS_OK;
 }
 
 nsresult
 nsLookAndFeel::CallRemoteGetSystemColors()
 {
     // An array has to be used to get data from remote process
     InfallibleTArray<uint32_t> colors;
@@ -71,32 +66,46 @@ nsLookAndFeel::CallRemoteGetSystemColors
 
     if (colors.Length() < colorsCount)
         colorsCount = colors.Length();
 
     // Array elements correspond to the members of mSystemColors structure,
     // so just copy the memory block
     memcpy(&mSystemColors, colors.Elements(), sizeof(nscolor) * colorsCount);
 
-    mInitializedSystemColors = true;
+    return NS_OK;
+}
 
-    return NS_OK;
+void
+nsLookAndFeel::NativeInit()
+{
+    EnsureInitSystemColors();
+    EnsureInitShowPassword();
+}
+
+/* virtual */
+void
+nsLookAndFeel::RefreshImpl()
+{
+    nsXPLookAndFeel::RefreshImpl();
+
+    mInitializedSystemColors = false;
+    mInitializedShowPassword = false;
 }
 
 nsresult
 nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor)
 {
     nsresult rv = NS_OK;
 
+    EnsureInitSystemColors();
     if (!mInitializedSystemColors) {
-        if (XRE_IsParentProcess())
-            rv = GetSystemColors();
-        else
-            rv = CallRemoteGetSystemColors();
-        NS_ENSURE_SUCCESS(rv, rv);
+        // Failure to initialize colors is an error condition. Return black.
+        aColor = 0;
+        return NS_ERROR_FAILURE;
     }
 
     // XXX we'll want to use context.obtainStyledAttributes on the java side to
     // get all of these; see TextView.java for a good exmaple.
 
     switch (aID) {
         // These colors don't seem to be used for anything anymore in Mozilla
         // (except here at least TextSelectBackground and TextSelectForeground)
@@ -338,17 +347,16 @@ nsLookAndFeel::NativeGetColor(ColorID aI
         aColor = 0;
         rv = NS_ERROR_FAILURE;
         break;
     }
 
     return rv;
 }
 
-
 nsresult
 nsLookAndFeel::GetIntImpl(IntID aID, int32_t &aResult)
 {
     nsresult rv = nsXPLookAndFeel::GetIntImpl(aID, aResult);
     if (NS_SUCCEEDED(rv))
         return rv;
 
     rv = NS_OK;
@@ -464,24 +472,17 @@ nsLookAndFeel::GetFontImpl(FontID aID, n
     aFontStyle.systemFont = true;
     return true;
 }
 
 /*virtual*/
 bool
 nsLookAndFeel::GetEchoPasswordImpl()
 {
-    if (!mInitializedShowPassword) {
-        if (XRE_IsParentProcess()) {
-            mShowPassword = java::GeckoAppShell::GetShowPasswordSetting();
-        } else {
-            ContentChild::GetSingleton()->SendGetShowPasswordSetting(&mShowPassword);
-        }
-        mInitializedShowPassword = true;
-    }
+    EnsureInitShowPassword();
     return mShowPassword;
 }
 
 uint32_t
 nsLookAndFeel::GetPasswordMaskDelayImpl()
 {
   // This value is hard-coded in Android OS's PasswordTransformationMethod.java
   return 1500;
@@ -489,8 +490,35 @@ nsLookAndFeel::GetPasswordMaskDelayImpl(
 
 /* virtual */
 char16_t
 nsLookAndFeel::GetPasswordCharacterImpl()
 {
   // This value is hard-coded in Android OS's PasswordTransformationMethod.java
   return UNICODE_BULLET;
 }
+
+void
+nsLookAndFeel::EnsureInitSystemColors()
+{
+    if (!mInitializedSystemColors) {
+        nsresult rv;
+        if (XRE_IsParentProcess()) {
+            rv = GetSystemColors();
+        } else {
+            rv = CallRemoteGetSystemColors();
+        }
+        mInitializedSystemColors = NS_SUCCEEDED(rv);
+    }
+}
+
+void
+nsLookAndFeel::EnsureInitShowPassword()
+{
+    if (!mInitializedShowPassword) {
+        if (XRE_IsParentProcess()) {
+            mShowPassword = java::GeckoAppShell::GetShowPasswordSetting();
+        } else {
+            ContentChild::GetSingleton()->SendGetShowPasswordSetting(&mShowPassword);
+        }
+        mInitializedShowPassword = true;
+    }
+}
--- a/widget/android/nsLookAndFeel.h
+++ b/widget/android/nsLookAndFeel.h
@@ -9,29 +9,33 @@
 #include "AndroidBridge.h"
 
 class nsLookAndFeel final : public nsXPLookAndFeel
 {
 public:
     nsLookAndFeel();
     virtual ~nsLookAndFeel();
 
+    virtual void NativeInit() final;
+    virtual void RefreshImpl();
     virtual nsresult NativeGetColor(ColorID aID, nscolor &aResult);
-    virtual void NativeInit() final {};
     virtual nsresult GetIntImpl(IntID aID, int32_t &aResult);
     virtual nsresult GetFloatImpl(FloatID aID, float &aResult);
     virtual bool GetFontImpl(FontID aID, nsString& aName, gfxFontStyle& aStyle,
                              float aDevPixPerCSSPixel);
     virtual bool GetEchoPasswordImpl();
     virtual uint32_t GetPasswordMaskDelayImpl();
     virtual char16_t GetPasswordCharacterImpl();
 
 protected:
     static bool mInitializedSystemColors;
     static mozilla::AndroidSystemColors mSystemColors;
     static bool mInitializedShowPassword;
     static bool mShowPassword;
 
     nsresult GetSystemColors();
     nsresult CallRemoteGetSystemColors();
+
+    void EnsureInitSystemColors();
+    void EnsureInitShowPassword();
 };
 
 #endif