forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Move SharedPreferencesManager to //base
SharedPreferencesManager only needed to be //chrome-layer because it has lists of the SharedPrefs keys used in Chrome. This CL: - Detaches the key registry from the key checking - Moves SharedPreferencesManager and Base/ChromePreferenceKeyChecker to //base - Leaves behind a temporary facade where //chrome SharedPreferencesManager is until the migration is finished. - Implements checking that all PreferenceKeyRegistries are listed in AllPreferenceKeyRegistries - Modifies ChromePreferenceKeysTest to check uniqueness across all known registries See go/base-shared-preferences for more details. Bug: 1069897,1483469 Change-Id: I1010671a43c0c23d9953f119c1970142e33afcc7 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4866653 Code-Coverage: [email protected] <[email protected]> Reviewed-by: Andrew Grieve <[email protected]> Commit-Queue: Henrique Nakashima <[email protected]> Cr-Commit-Position: refs/heads/main@{#1202809}
- Loading branch information
Henrique Nakashima
authored and
Chromium LUCI CQ
committed
Sep 28, 2023
1 parent
83168c3
commit d2cf18e
Showing
32 changed files
with
1,689 additions
and
1,417 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
base/android/java/src/org/chromium/base/shared_preferences/KnownPreferenceKeyRegistries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.base.shared_preferences; | ||
|
||
import com.google.common.collect.Sets; | ||
|
||
import org.chromium.base.ResettersForTesting; | ||
import org.chromium.build.BuildConfig; | ||
import org.chromium.build.annotations.CheckDiscard; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Ensures that all {@link PreferenceKeyRegistry}s used are known. | ||
* | ||
* A complement to ChromePreferenceKeysTest, which ensures that preference keys across all known | ||
* registries are unique. | ||
* | ||
* This checking is done in tests in which |initializeKnownRegistries()| is called, which happens | ||
* during browser process initialization. | ||
*/ | ||
@CheckDiscard("Preference key checking should only happen on build with asserts") | ||
public class KnownPreferenceKeyRegistries { | ||
private static Set<PreferenceKeyRegistry> sKnownRegistries; | ||
private static Set<PreferenceKeyRegistry> sRegistriesUsedBeforeInitialization = new HashSet<>(); | ||
|
||
public static void onRegistryUsed(PreferenceKeyRegistry registry) { | ||
if (!BuildConfig.ENABLE_ASSERTS) { | ||
return; | ||
} | ||
|
||
if (sKnownRegistries == null) { | ||
// Before initialization, keep track of registries used. | ||
sRegistriesUsedBeforeInitialization.add(registry); | ||
} else { | ||
// After initialization, check if registry is known. | ||
if (!sKnownRegistries.contains(registry)) { | ||
String message = | ||
"An unknown registry was used, PreferenceKeyRegistries must be declared as " | ||
+ "known in AllPreferenceKeyRegistries: " | ||
+ String.join(",", registry.toDebugString()); | ||
assert false : message; | ||
} | ||
} | ||
} | ||
|
||
public static void initializeKnownRegistries(Set<PreferenceKeyRegistry> knownRegistries) { | ||
if (!BuildConfig.ENABLE_ASSERTS) { | ||
return; | ||
} | ||
|
||
if (sKnownRegistries != null) { | ||
// Double initialization; make sure new known registries are the same. | ||
assert sKnownRegistries.equals(knownRegistries); | ||
return; | ||
} | ||
|
||
// Check that each registry already used is known; assert otherwise. | ||
Set<PreferenceKeyRegistry> unknownRegistries = | ||
Sets.difference(sRegistriesUsedBeforeInitialization, knownRegistries); | ||
if (!unknownRegistries.isEmpty()) { | ||
List<String> unknownRegistryNames = new ArrayList<>(); | ||
for (PreferenceKeyRegistry unknownRegistry : unknownRegistries) { | ||
unknownRegistryNames.add(unknownRegistry.toDebugString()); | ||
} | ||
String message = | ||
"Unknown registries were used, PreferenceKeyRegistries must be declared as " | ||
+ "known in AllPreferenceKeyRegistries: " | ||
+ String.join(",", unknownRegistryNames); | ||
assert false : message; | ||
} | ||
|
||
sKnownRegistries = knownRegistries; | ||
sRegistriesUsedBeforeInitialization = null; | ||
} | ||
|
||
static void clearForTesting() { | ||
Set<PreferenceKeyRegistry> previousKnownRegistries = sKnownRegistries; | ||
Set<PreferenceKeyRegistry> registriesUsedBeforeInitialization = | ||
sRegistriesUsedBeforeInitialization; | ||
|
||
ResettersForTesting.register(() -> { | ||
sKnownRegistries = previousKnownRegistries; | ||
sRegistriesUsedBeforeInitialization = registriesUsedBeforeInitialization; | ||
}); | ||
sKnownRegistries = null; | ||
sRegistriesUsedBeforeInitialization = new HashSet<>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
base/android/java/src/org/chromium/base/shared_preferences/PreferenceKeyRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.base.shared_preferences; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.chromium.build.annotations.CheckDiscard; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
@CheckDiscard("Preference key checking should only happen on build with asserts") | ||
public class PreferenceKeyRegistry { | ||
private final String mModule; | ||
public final HashSet<String> mKeysInUse; | ||
public final HashSet<String> mLegacyFormatKeys; | ||
public final List<KeyPrefix> mLegacyPrefixes; | ||
|
||
public PreferenceKeyRegistry(String module, List<String> keysInUse, List<String> legacyKeys, | ||
List<KeyPrefix> legacyPrefixes) { | ||
mModule = module; | ||
mKeysInUse = new HashSet<>(keysInUse); | ||
mLegacyFormatKeys = new HashSet<>(legacyKeys); | ||
mLegacyPrefixes = legacyPrefixes; | ||
} | ||
|
||
@NonNull | ||
public String toDebugString() { | ||
return String.format(Locale.getDefault(), "%s (%d in use, %d legacy, %d legacy prefixes)", | ||
mModule, mKeysInUse.size(), mLegacyFormatKeys.size(), mLegacyPrefixes.size()); | ||
} | ||
} |
Oops, something went wrong.