Bug 1660241 - Traverse children of arbitrary degree in RuntimeSettings. r=snorp
authorAgi Sferro <agi@sferro.dev>
Wed, 30 Sep 2020 19:57:28 +0000
changeset 550971 377777e1795731eb4c33e47498550334771653a0
parent 550970 6cf959e2de7b27c202b2631e238d0708d162f7e0
child 550972 be52a545a9864f2501d2eea9c72a46543bace7a1
push id127589
push userasferro@mozilla.com
push dateWed, 30 Sep 2020 21:37:21 +0000
treeherderautoland@be52a545a986 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1660241
milestone83.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 1660241 - Traverse children of arbitrary degree in RuntimeSettings. r=snorp When collecting prefs from a RuntimeSettings we only consider children of the instance. This change makes it so we traverse the whole tree, collecting childrens of children, etc. Differential Revision: https://phabricator.services.mozilla.com/D91674
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/RuntimeSettings.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/RuntimeSettings.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/RuntimeSettings.java
@@ -197,55 +197,53 @@ public abstract class RuntimeSettings im
     private void addChild(final @NonNull RuntimeSettings child) {
         mChildren.add(child);
     }
 
     /* pacakge */ void addPref(final Pref<?> pref) {
         mPrefs.add(pref);
     }
 
-
     /**
      * Return a mapping of the prefs managed in this settings, including child
      * settings.
      *
      * @return A key-value mapping of the prefs.
      */
     /* package */ @NonNull Map<String, Object> getPrefsMap() {
         final ArrayMap<String, Object> prefs = new ArrayMap<>();
+        forAllPrefs(pref -> prefs.put(pref.name, pref.get()));
 
-        for (final Pref<?> pref : mPrefs) {
-            prefs.put(pref.name, pref.get());
+        return Collections.unmodifiableMap(prefs);
+    }
+
+    /**
+     * Iterates through all prefs in this RuntimeSettings instance and
+     * in all children, grandchildren, etc.
+     */
+    private void forAllPrefs(final GeckoResult.Consumer<Pref<?>> visitor) {
+        for (final RuntimeSettings child : mChildren) {
+            child.forAllPrefs(visitor);
         }
 
-        for (final RuntimeSettings child : mChildren) {
-            prefs.putAll(child.getPrefsMap());
+        for (final Pref<?> pref : mPrefs) {
+            visitor.accept(pref);
         }
-
-        return Collections.unmodifiableMap(prefs);
     }
 
     /**
      * Reset the prefs managed by this settings and its children.
      *
-     * The actual prefs values are set via {@link getPrefsMap} during
-     * initialization and via {@link Pref.commit} during runtime for individual
+     * The actual prefs values are set via {@link #getPrefsMap} during
+     * initialization and via {@link Pref#commit} during runtime for individual
      * prefs.
      */
     /* package */ void commitResetPrefs() {
         final ArrayList<String> names = new ArrayList<String>();
-
-        for (final Pref<?> pref : mPrefs) {
-            names.add(pref.name);
-        }
-        for (final RuntimeSettings child : mChildren) {
-            for (final Pref<?> pref : child.mPrefs) {
-                names.add(pref.name);
-            }
-        }
+        forAllPrefs(pref -> names.add(pref.name));
 
         final GeckoBundle data = new GeckoBundle(1);
         data.putStringArray("names", names);
         EventDispatcher.getInstance().dispatch("GeckoView:ResetUserPrefs", data);
     }
 
     @Override // Parcelable
     @AnyThread