Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6d488676 authored by Pete Gillin's avatar Pete Gillin Committed by Android (Google) Code Review
Browse files

Merge "Add new 'explicit GC' policy to StrictMode."

parents 6f1746aa b3b07312
Loading
Loading
Loading
Loading
+42 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.os.strictmode.ContentUriWithoutPermissionViolation;
import android.os.strictmode.CustomViolation;
import android.os.strictmode.DiskReadViolation;
import android.os.strictmode.DiskWriteViolation;
import android.os.strictmode.ExplicitGcViolation;
import android.os.strictmode.FileUriExposedViolation;
import android.os.strictmode.InstanceCountViolation;
import android.os.strictmode.IntentReceiverLeakedViolation;
@@ -212,13 +213,17 @@ public final class StrictMode {
    /** @hide */
    @TestApi public static final int DETECT_UNBUFFERED_IO = 0x20; // for ThreadPolicy

    /** @hide  */
    public static final int DETECT_EXPLICIT_GC = 0x40;  // for ThreadPolicy

    private static final int ALL_THREAD_DETECT_BITS =
            DETECT_DISK_WRITE
                    | DETECT_DISK_READ
                    | DETECT_NETWORK
                    | DETECT_CUSTOM
                    | DETECT_RESOURCE_MISMATCH
                    | DETECT_UNBUFFERED_IO;
                    | DETECT_UNBUFFERED_IO
                    | DETECT_EXPLICIT_GC;

    // Byte 2: Process-policy

@@ -583,6 +588,29 @@ public final class StrictMode {
                return disable(DETECT_DISK_WRITE);
            }

            /**
             * Detect explicit GC requests, i.e. calls to Runtime.gc().
             *
             * @hide
             */
            public Builder detectExplicitGc() {
                // TODO(b/3400644): Un-hide this for next API update
                // TODO(b/3400644): Un-hide ExplicitGcViolation for next API update
                // TODO(b/3400644): Make DETECT_EXPLICIT_GC a @TestApi for next API update
                // TODO(b/3400644): Call this from detectAll in next API update
                return enable(DETECT_EXPLICIT_GC);
            }

            /**
             * Disable detection of explicit GC requests, i.e. calls to Runtime.gc().
             *
             * @hide
             */
            public Builder permitExplicitGc() {
                // TODO(b/3400644): Un-hide this for next API update
                return disable(DETECT_EXPLICIT_GC);
            }

            /**
             * Show an annoying dialog to the developer on detected violations, rate-limited to be
             * only a little annoying.
@@ -1481,6 +1509,17 @@ public final class StrictMode {
            startHandlingViolationException(new NetworkViolation());
        }

        // Part of BlockGuard.Policy interface:
        public void onExplicitGc() {
            if ((mPolicyMask & DETECT_EXPLICIT_GC) == 0) {
                return;
            }
            if (tooManyViolationsThisLoop()) {
                return;
            }
            startHandlingViolationException(new ExplicitGcViolation());
        }

        public void setPolicyMask(int policyMask) {
            mPolicyMask = policyMask;
        }
@@ -2576,6 +2615,8 @@ public final class StrictMode {
                return DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION;
            } else if (mViolation instanceof UntaggedSocketViolation) {
                return DETECT_VM_UNTAGGED_SOCKET;
            } else if (mViolation instanceof ExplicitGcViolation) {
                return DETECT_EXPLICIT_GC;
            }
            throw new IllegalStateException("missing violation bit");
        }
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.os.strictmode;

/**
 * See #{@link android.os.StrictMode.ThreadPolicy.Builder#detectExplicitGc()}.
 *
 * @hide
 */
public final class ExplicitGcViolation extends Violation {
    /** @hide */
    public ExplicitGcViolation() {
        super(null);
    }
}