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 import com.google.auto.value.AutoValue;
24 
25 /** Class used to set startup parameters for {@link JavaScriptIsolate}. */
26 @AutoValue
27 public abstract class IsolateSettings {
28     /**
29      * Gets the max heap size used by the {@link JavaScriptIsolate}.
30      *
31      * <p>The default value is 0 which indicates no heap size limit.
32      *
33      * @return heap size in bytes
34      */
getMaxHeapSizeBytes()35     public abstract long getMaxHeapSizeBytes();
36 
37     /**
38      * Gets the condition if the Max Heap feature is enforced for JS Isolate
39      *
40      * <p>The default value is false
41      *
42      * @return boolean value stating if the feature is enforced
43      */
getEnforceMaxHeapSizeFeature()44     public abstract boolean getEnforceMaxHeapSizeFeature();
45 
46     /**
47      * Gets the condition if the console message in logs is enabled for JS Isolate.
48      *
49      * <p>The default value is false
50      *
51      * @return boolean value stating if the feature is enabled.
52      */
getIsolateConsoleMessageInLogsEnabled()53     public abstract boolean getIsolateConsoleMessageInLogsEnabled();
54 
55     /** Creates setting for which memory restrictions are not enforced */
forMaxHeapSizeEnforcementDisabled( boolean isolateConsoleMessageInLogsEnabled)56     public static IsolateSettings forMaxHeapSizeEnforcementDisabled(
57             boolean isolateConsoleMessageInLogsEnabled) {
58         return IsolateSettings.builder()
59                 .setEnforceMaxHeapSizeFeature(false)
60                 .setMaxHeapSizeBytes(0)
61                 .setIsolateConsoleMessageInLogsEnabled(isolateConsoleMessageInLogsEnabled)
62                 .build();
63     }
64 
65     /**
66      * @return {@link Builder} for {@link IsolateSettings}
67      */
builder()68     public static IsolateSettings.Builder builder() {
69         return new AutoValue_IsolateSettings.Builder();
70     }
71 
72     /** Builder clsas for {@link IsolateSettings} */
73     @AutoValue.Builder
74     public abstract static class Builder {
maxHeapSizeBytes(long maxHeapSizeBytes)75         abstract Builder maxHeapSizeBytes(long maxHeapSizeBytes);
76 
enforceMaxHeapSizeFeature(boolean value)77         abstract Builder enforceMaxHeapSizeFeature(boolean value);
78 
isolateConsoleMessageInLogsEnabled(boolean value)79         abstract Builder isolateConsoleMessageInLogsEnabled(boolean value);
80 
81         /** Sets the max heap size used by the {@link JavaScriptIsolate}. */
setMaxHeapSizeBytes(long maxHeapSizeBytes)82         public Builder setMaxHeapSizeBytes(long maxHeapSizeBytes) {
83             Preconditions.checkArgument(maxHeapSizeBytes >= 0, "maxHeapSizeBytes should be >= 0");
84             return maxHeapSizeBytes(maxHeapSizeBytes);
85         }
86 
87         /** Sets the condition if the max heap size is enforced for JS Isolate. */
setEnforceMaxHeapSizeFeature(boolean value)88         public Builder setEnforceMaxHeapSizeFeature(boolean value) {
89             return enforceMaxHeapSizeFeature(value);
90         }
91 
92         /** Sets the condition if the console message in logs is enabled for JS Isolate. */
setIsolateConsoleMessageInLogsEnabled(boolean value)93         public Builder setIsolateConsoleMessageInLogsEnabled(boolean value) {
94             return isolateConsoleMessageInLogsEnabled(value);
95         }
96 
97         /**
98          * @return {@link IsolateSettings}
99          */
build()100         public abstract IsolateSettings build();
101     }
102 }
103