1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.service.js; 18 19 import androidx.javascriptengine.JavaScriptIsolate; 20 21 import com.android.internal.util.Preconditions; 22 23 /** Class used to set startup parameters for {@link JavaScriptIsolate}. */ 24 public final class IsolateSettings { 25 private final long mMaxHeapSizeBytes; 26 private final boolean mEnforceMaxHeapSizeFeature; 27 28 /** 29 * Constructor to set the initial values of isolate settings 30 * 31 * @param enforceMaxHeapSizeFeature boolean value if the max heap size restriction should be 32 * enforced 33 * @param maxHeapSizeBytes value of the max heap memory size 34 */ IsolateSettings(boolean enforceMaxHeapSizeFeature, long maxHeapSizeBytes)35 private IsolateSettings(boolean enforceMaxHeapSizeFeature, long maxHeapSizeBytes) { 36 Preconditions.checkArgument(maxHeapSizeBytes >= 0, "maxHeapSizeBytes should be >= 0"); 37 this.mEnforceMaxHeapSizeFeature = enforceMaxHeapSizeFeature; 38 this.mMaxHeapSizeBytes = maxHeapSizeBytes; 39 } 40 41 /** 42 * Gets the max heap size used by the {@link JavaScriptIsolate}. 43 * 44 * <p>The default value is 0 which indicates no heap size limit. 45 * 46 * @return heap size in bytes 47 */ getMaxHeapSizeBytes()48 public long getMaxHeapSizeBytes() { 49 return mMaxHeapSizeBytes; 50 } 51 52 /** 53 * Gets the condition if the Max Heap feature is enforced for JS Isolate 54 * 55 * <p>The default value is false 56 * 57 * @return boolean value stating if the feature is enforced 58 */ getEnforceMaxHeapSizeFeature()59 public boolean getEnforceMaxHeapSizeFeature() { 60 return mEnforceMaxHeapSizeFeature; 61 } 62 63 /** Creates setting for which memory restrictions are not enforced */ forMaxHeapSizeEnforcementDisabled()64 public static IsolateSettings forMaxHeapSizeEnforcementDisabled() { 65 return new IsolateSettings(false, 0); 66 } 67 68 /** Creates setting for which memory restrictions are enforced and sets the max heap memory */ forMaxHeapSizeEnforcementEnabled(long maxHeapSizeBytes)69 public static IsolateSettings forMaxHeapSizeEnforcementEnabled(long maxHeapSizeBytes) { 70 return new IsolateSettings(true, maxHeapSizeBytes); 71 } 72 } 73