• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.adservices.service.Flags;
22 import com.android.internal.util.Preconditions;
23 
24 import com.google.auto.value.AutoValue;
25 
26 /** Class used to set startup parameters for {@link JavaScriptIsolate}. */
27 @AutoValue
28 public abstract class IsolateSettings {
29     /**
30      * Gets the max heap size used by the {@link JavaScriptIsolate}.
31      *
32      * <p>The default value is 0 which indicates no heap size limit.
33      *
34      * @return heap size in bytes
35      */
getMaxHeapSizeBytes()36     public abstract long getMaxHeapSizeBytes();
37 
38     /**
39      * Gets the condition if the console message in logs is enabled for JS Isolate.
40      *
41      * <p>The default value is false
42      *
43      * @return boolean value stating if the feature is enabled.
44      */
getIsolateConsoleMessageInLogsEnabled()45     public abstract boolean getIsolateConsoleMessageInLogsEnabled();
46 
47     /** Creates setting for which memory restrictions are not enforced */
forMaxHeapSizeEnforcementEnabled( boolean isolateConsoleMessageInLogsEnabled)48     public static IsolateSettings forMaxHeapSizeEnforcementEnabled(
49             boolean isolateConsoleMessageInLogsEnabled) {
50         return IsolateSettings.builder()
51                 .setMaxHeapSizeBytes(Flags.ISOLATE_MAX_HEAP_SIZE_BYTES)
52                 .setIsolateConsoleMessageInLogsEnabled(isolateConsoleMessageInLogsEnabled)
53                 .build();
54     }
55 
56     /**
57      * @return {@link Builder} for {@link IsolateSettings}
58      */
builder()59     public static IsolateSettings.Builder builder() {
60         return new AutoValue_IsolateSettings.Builder();
61     }
62 
63     /** Builder clsas for {@link IsolateSettings} */
64     @AutoValue.Builder
65     public abstract static class Builder {
maxHeapSizeBytes(long maxHeapSizeBytes)66         abstract Builder maxHeapSizeBytes(long maxHeapSizeBytes);
67 
isolateConsoleMessageInLogsEnabled(boolean value)68         abstract Builder isolateConsoleMessageInLogsEnabled(boolean value);
69 
70         /** Sets the max heap size used by the {@link JavaScriptIsolate}. */
setMaxHeapSizeBytes(long maxHeapSizeBytes)71         public Builder setMaxHeapSizeBytes(long maxHeapSizeBytes) {
72             Preconditions.checkArgument(maxHeapSizeBytes >= 0, "maxHeapSizeBytes should be >= 0");
73             return maxHeapSizeBytes(maxHeapSizeBytes);
74         }
75 
76         /** Sets the condition if the console message in logs is enabled for JS Isolate. */
setIsolateConsoleMessageInLogsEnabled(boolean value)77         public Builder setIsolateConsoleMessageInLogsEnabled(boolean value) {
78             return isolateConsoleMessageInLogsEnabled(value);
79         }
80 
81         /**
82          * @return {@link IsolateSettings}
83          */
build()84         public abstract IsolateSettings build();
85     }
86 }
87