0

AW SafeMode: expose setSafeMode() method

This exposes the setSafeMode() method to allow other classes in the same
process to invoke this. This is useful when running in Contexts which
are not permitted to bind to a Service (such as a BroadcastReceiver).

Bug: 1310104
Test: run_webview_instrumentation_test_apk -f SafeModeTest.*
Change-Id: I1ef6c92251710fcfbda517068bb23fd825535e07
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3653297
Auto-Submit: Nate Fischer <ntfschr@chromium.org>
Commit-Queue: Richard Coles <torne@chromium.org>
Reviewed-by: Richard Coles <torne@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1004810}
This commit is contained in:
Nate Fischer
2022-05-18 17:00:41 +00:00
committed by Chromium LUCI CQ
parent a478505731
commit 7b5f9aba67
2 changed files with 35 additions and 5 deletions
android_webview
javatests
src
org
chromium
android_webview
nonembedded
java
src
org
chromium
android_webview

@ -120,6 +120,28 @@ public class SafeModeTest {
SafeModeController.getInstance().isSafeModeEnabled(TEST_WEBVIEW_PACKAGE_NAME));
}
@Test
@MediumTest
@Feature({"AndroidWebView"})
public void testSafeModeState_enableWithMethod() throws Throwable {
SafeModeService.setSafeMode(Arrays.asList(SAFEMODE_ACTION_NAME));
Assert.assertTrue("SafeMode should be enabled",
SafeModeController.getInstance().isSafeModeEnabled(TEST_WEBVIEW_PACKAGE_NAME));
}
@Test
@MediumTest
@Feature({"AndroidWebView"})
public void testSafeModeState_disableWithMethod() throws Throwable {
SafeModeService.setSafeMode(Arrays.asList(SAFEMODE_ACTION_NAME));
Assert.assertTrue("SafeMode should be enabled",
SafeModeController.getInstance().isSafeModeEnabled(TEST_WEBVIEW_PACKAGE_NAME));
SafeModeService.setSafeMode(Arrays.asList());
Assert.assertFalse("SafeMode should be re-disabled",
SafeModeController.getInstance().isSafeModeEnabled(TEST_WEBVIEW_PACKAGE_NAME));
}
@Test
@MediumTest
@Feature({"AndroidWebView"})

@ -218,9 +218,7 @@ public final class SafeModeService extends Service {
throw new SecurityException("setSafeMode() may only be called by a trusted app");
}
synchronized (sLock) {
SafeModeService.setSafeMode(actions);
}
SafeModeService.setSafeMode(actions);
}
};
@ -237,9 +235,19 @@ public final class SafeModeService extends Service {
/**
* Sets the SafeMode config. This includes persisting the set of actions, toggling component
* state, etc.
*
* <p>This may only be called from the same process SafeModeService is declared to run in via
* the "android:process" attribute. Callers from other processes must bind to the Service via
* the AIDL interface.
*/
public static void setSafeMode(List<String> actions) {
synchronized (sLock) {
SafeModeService.setSafeModeLocked(actions);
}
}
@GuardedBy("sLock")
private static void setSafeMode(List<String> actions) {
private static void setSafeModeLocked(List<String> actions) {
boolean enableSafeMode = actions != null && !actions.isEmpty();
SharedPreferences.Editor editor = getSharedPreferences().edit();
@ -269,7 +277,7 @@ public final class SafeModeService extends Service {
@GuardedBy("sLock")
private static void disableSafeMode() {
setSafeMode(Arrays.asList());
setSafeModeLocked(Arrays.asList());
}
@GuardedBy("sLock")