summaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
Diffstat (limited to 'services')
-rw-r--r--services/core/java/com/android/server/wm/DisplayContent.java30
-rw-r--r--services/core/java/com/android/server/wm/WindowManagerService.java28
2 files changed, 58 insertions, 0 deletions
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index e26163247020..8f7fc9e354a0 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -5534,4 +5534,34 @@ class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowCo
}
}
}
+
+ /**
+ * Returns the number of window tokens without surface on this display. A {@link WindowToken}
+ * won't have its {@link SurfaceControl} until a window is added to a {@link WindowToken}.
+ * The purpose of this method is to accumulate non-window containing {@link WindowToken}s and
+ * limit the usage if the count exceeds a number.
+ *
+ * @param callingUid app calling uid
+ * @return the number of window tokens without surface on this display
+ * @see WindowToken#addWindow(WindowState)
+ */
+ int getWindowTokensWithoutSurfaceCount(int callingUid) {
+ List<WindowToken> tokens = new ArrayList<>(mTokenMap.values());
+ int count = 0;
+ for (int i = tokens.size() - 1; i >= 0; i--) {
+ final WindowToken token = tokens.get(i);
+ if (callingUid != token.getOwnerUid()) {
+ continue;
+ }
+ // Skip if token is an Activity
+ if (token.asActivityRecord() != null) {
+ continue;
+ }
+ if (token.mSurfaceControl != null) {
+ continue;
+ }
+ count++;
+ }
+ return count;
+ }
}
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index fbdea01deb77..8eb4b2659cc0 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -23,6 +23,7 @@ import static android.Manifest.permission.MANAGE_APP_TOKENS;
import static android.Manifest.permission.READ_FRAME_BUFFER;
import static android.Manifest.permission.REGISTER_WINDOW_MANAGER_LISTENERS;
import static android.Manifest.permission.RESTRICTED_VR_ACCESS;
+import static android.Manifest.permission.STATUS_BAR_SERVICE;
import static android.Manifest.permission.WRITE_SECURE_SETTINGS;
import static android.app.ActivityManagerInternal.ALLOW_FULL_ONLY;
import static android.app.ActivityManagerInternal.ALLOW_NON_FULL;
@@ -74,6 +75,7 @@ import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
import static android.view.WindowManager.REMOVE_CONTENT_MODE_UNDEFINED;
import static android.view.WindowManagerGlobal.ADD_OKAY;
+import static android.view.WindowManagerGlobal.ADD_TOO_MANY_TOKENS;
import static android.view.WindowManagerGlobal.RELAYOUT_DEFER_SURFACE_DESTROY;
import static android.view.WindowManagerGlobal.RELAYOUT_RES_BLAST_SYNC;
import static android.view.WindowManagerGlobal.RELAYOUT_RES_SURFACE_CHANGED;
@@ -413,6 +415,12 @@ public class WindowManagerService extends IWindowManager.Stub
private static final int ANIMATION_COMPLETED_TIMEOUT_MS = 5000;
+ /** The maximum count of window tokens without surface that an app can register. */
+ private static final int MAXIMUM_WINDOW_TOKEN_COUNT_WITHOUT_SURFACE = 5;
+
+ /** System UI can create more window context... */
+ private static final int SYSTEM_UI_MULTIPLIER = 2;
+
// TODO(b/143053092): Remove the settings if it becomes stable.
private static final String FIXED_ROTATION_TRANSFORM_SETTING_NAME = "fixed_rotation_transform";
boolean mIsFixedRotationTransformEnabled;
@@ -2594,10 +2602,30 @@ public class WindowManagerService extends IWindowManager.Stub
@Override
public int addWindowTokenWithOptions(IBinder binder, int type, int displayId, Bundle options,
String packageName) {
+ if (tokenCountExceed()) {
+ return ADD_TOO_MANY_TOKENS;
+ }
return addWindowTokenWithOptions(binder, type, displayId, options, packageName,
true /* fromClientToken */);
}
+ private boolean tokenCountExceed() {
+ final int callingUid = Binder.getCallingUid();
+ // Don't check if caller is from system server.
+ if (callingUid == myPid()) {
+ return false;
+ }
+ final int limit =
+ (checkCallingPermission(STATUS_BAR_SERVICE, "addWindowTokenWithOptions"))
+ ? MAXIMUM_WINDOW_TOKEN_COUNT_WITHOUT_SURFACE * SYSTEM_UI_MULTIPLIER
+ : MAXIMUM_WINDOW_TOKEN_COUNT_WITHOUT_SURFACE;
+ synchronized (mGlobalLock) {
+ int[] count = new int[1];
+ mRoot.forAllDisplays(d -> count[0] += d.getWindowTokensWithoutSurfaceCount(callingUid));
+ return count[0] >= limit;
+ }
+ }
+
private int addWindowTokenWithOptions(IBinder binder, int type, int displayId, Bundle options,
String packageName, boolean fromClientToken) {
final boolean callerCanManageAppTokens =