0

[AW][SafeMode] Persist precautions.

Adds logic to persist precautions. Returns persisted precautions when
queried instead of a hardcoded one.

Adds a test to verify that persisting and reading back a precaution
different to the previously hardcoded one works as expected.

Bug: 1221610

Change-Id: I6b9e77f3619cb59ef76391778bb4ef9f33e8cc46
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2976340
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: Richard Coles <torne@chromium.org>
Reviewed-by: Nate Fischer <ntfschr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#896001}
This commit is contained in:
Mugdha Lakhani
2021-06-25 11:16:35 +00:00
committed by Chromium LUCI CQ
parent 02022f6e3b
commit 917492de7f
2 changed files with 33 additions and 11 deletions
android_webview
javatests
src
org
chromium
android_webview
nonembedded
java
src
org
chromium
android_webview

@ -71,6 +71,8 @@ public class SafeModeTest {
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
private static final String SAFEMODE_ACTION_NAME = "some_action_name";
private AtomicInteger mTestSafeModeActionExecutionCounter;
@Rule
@ -126,7 +128,7 @@ public class SafeModeTest {
try (ServiceConnectionHelper helper =
new ServiceConnectionHelper(intent, Context.BIND_AUTO_CREATE)) {
ISafeModeService service = ISafeModeService.Stub.asInterface(helper.getBinder());
service.setSafeMode(Arrays.asList("some_action_name"));
service.setSafeMode(Arrays.asList(SAFEMODE_ACTION_NAME));
}
Assert.assertTrue("SafeMode should be enabled",
@ -141,7 +143,7 @@ public class SafeModeTest {
try (ServiceConnectionHelper helper =
new ServiceConnectionHelper(intent, Context.BIND_AUTO_CREATE)) {
ISafeModeService service = ISafeModeService.Stub.asInterface(helper.getBinder());
service.setSafeMode(Arrays.asList("some_action_name"));
service.setSafeMode(Arrays.asList(SAFEMODE_ACTION_NAME));
}
Assert.assertTrue("SafeMode should be enabled",
@ -185,6 +187,25 @@ public class SafeModeTest {
SafeModeController.getInstance().queryActions(TEST_WEBVIEW_PACKAGE_NAME));
}
@Test
@MediumTest
@Feature({"AndroidWebView"})
public void testQueryActions_multipleActions() throws Throwable {
Intent intent = new Intent(ContextUtils.getApplicationContext(), SafeModeService.class);
final String variationsActionId = new VariationsSeedSafeModeAction().getId();
try (ServiceConnectionHelper helper =
new ServiceConnectionHelper(intent, Context.BIND_AUTO_CREATE)) {
ISafeModeService service = ISafeModeService.Stub.asInterface(helper.getBinder());
service.setSafeMode(Arrays.asList(SAFEMODE_ACTION_NAME, variationsActionId));
}
Assert.assertTrue("SafeMode should be enabled",
SafeModeController.getInstance().isSafeModeEnabled(TEST_WEBVIEW_PACKAGE_NAME));
Assert.assertEquals("Querying the ContentProvider should yield the action we set",
asSet(SAFEMODE_ACTION_NAME, variationsActionId),
SafeModeController.getInstance().queryActions(TEST_WEBVIEW_PACKAGE_NAME));
}
@Test
@MediumTest
@Feature({"AndroidWebView"})

@ -32,6 +32,7 @@ import org.chromium.base.compat.ApiHelperForP;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -47,6 +48,7 @@ import javax.annotation.concurrent.GuardedBy;
*/
public final class SafeModeService extends Service {
private static final String TAG = "WebViewSafeMode";
private static final String SAFEMODE_ACTIONS_KEY = "SAFEMODE_ACTIONS";
private static final Object sLock = new Object();
@ -243,8 +245,8 @@ public final class SafeModeService extends Service {
long currentTime = sClock.currentTimeMillis();
editor.putLong(LAST_MODIFIED_TIME_KEY, currentTime);
// TODO(ntfschr): persist the list of actions once we figure out the right
// representation on disk.
Set<String> actionsToPersist = new HashSet<>(actions);
editor.putStringSet(SAFEMODE_ACTIONS_KEY, actionsToPersist);
} else {
editor.clear();
}
@ -278,9 +280,7 @@ public final class SafeModeService extends Service {
}
}
// This must match the constant in VariationsSeedSafeModeAction.java
private static final String VARIATIONS_SAFEMODEACTION_ID = "delete_variations_seed";
@NonNull
public static Set<String> getSafeModeConfig() {
synchronized (sLock) {
final Context context = ContextUtils.getApplicationContext();
@ -291,10 +291,11 @@ public final class SafeModeService extends Service {
setSafeMode(Arrays.asList());
return new HashSet<>();
}
// TODO(ntfschr): fetch the list of actions from disk once we figure out the right
// representation on disk. At that point, we can delete the VARIATIONS_SAFEMODEACTION_ID
// constant in this class since there's no more benefit to hardcoding actions.
return new HashSet<>(Arrays.asList(VARIATIONS_SAFEMODEACTION_ID));
// Returning an empty Set in the absence of persisted actions ensures the caller
// doesn't crash when iterating over the return value.
return getSharedPreferences().getStringSet(
SAFEMODE_ACTIONS_KEY, Collections.emptySet());
}
}