Bug 1460874 - Part 4: Clean up threading assumptions. r=snorp
☠☠ backed out by cd616c3e3eea ☠ ☠
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 13 Feb 2019 20:10:17 +0000
changeset 459047 02f24dec5ae4
parent 459046 c7fce6c79bef
child 459048 81c4e88003a5
push id111913
push usershindli@mozilla.com
push dateThu, 14 Feb 2019 05:01:59 +0000
treeherdermozilla-inbound@a0752d7e8073 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1460874
milestone67.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 1460874 - Part 4: Clean up threading assumptions. r=snorp In practice, everything in the GeckoFontScaleListener will run on the UI thread, so get rid of the `synchronized` methods and just enforce the threading assumptions in the public API. Differential Revision: https://phabricator.services.mozilla.com/D18505
mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java
@@ -1,16 +1,17 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * 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/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.preferences.GeckoPreferences;
+import org.mozilla.gecko.util.ThreadUtils;
 
 import android.annotation.SuppressLint;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.database.ContentObserver;
 import android.net.Uri;
 import android.provider.Settings;
@@ -36,58 +37,63 @@ class GeckoFontScaleListener
     private boolean mAttached;
     private boolean mRunning;
 
     public static GeckoFontScaleListener getInstance() {
         return sInstance;
     }
 
     private GeckoFontScaleListener() {
-        super(null);
+        // Ensure the ContentObserver callback runs on the UI thread.
+        super(ThreadUtils.getUiHandler());
     }
 
-    public synchronized void attachToContext(final Context context) {
+    public void attachToContext(final Context context) {
+        ThreadUtils.assertOnUiThread();
+
         if (mAttached) {
             Log.w(LOGTAG, "Already attached!");
             return;
         }
 
         mApplicationContext = context.getApplicationContext();
         SharedPreferences prefs = GeckoSharedPrefs.forApp(mApplicationContext);
         prefs.registerOnSharedPreferenceChangeListener(this);
         onPrefChange(prefs);
         mAttached = true;
     }
 
-    public synchronized void detachFromContext() {
+    public void detachFromContext() {
+        ThreadUtils.assertOnUiThread();
+
         if (!mAttached) {
             Log.w(LOGTAG, "Already detached!");
             return;
         }
 
         GeckoSharedPrefs.forApp(mApplicationContext).unregisterOnSharedPreferenceChangeListener(this);
         stop();
         mApplicationContext = null;
         mAttached = false;
     }
 
-    private synchronized void start() {
+    private void start() {
         if (mRunning) {
             return;
         }
 
         ContentResolver contentResolver = mApplicationContext.getContentResolver();
         Uri fontSizeSetting = Settings.System.getUriFor(Settings.System.FONT_SCALE);
         contentResolver.registerContentObserver(fontSizeSetting, false, this);
         onSystemFontScaleChange(contentResolver, false);
 
         mRunning = true;
     }
 
-    private synchronized void stop() {
+    private void stop() {
         if (!mRunning) {
             return;
         }
 
         ContentResolver contentResolver = mApplicationContext.getContentResolver();
         contentResolver.unregisterContentObserver(this);
         onSystemFontScaleChange(contentResolver, /*stopping*/ true);
 
@@ -115,16 +121,17 @@ class GeckoFontScaleListener
 
         if (useSystemFontScale) {
             start();
         } else {
             stop();
         }
     }
 
+    @UiThread // See constructor.
     @Override
     public void onChange(boolean selfChange) {
         onSystemFontScaleChange(mApplicationContext.getContentResolver(), false);
     }
 
     @UiThread // According to the docs.
     @Override
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {