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.server.art.model;
18 
19 import static com.android.server.art.ArtManagerLocal.AdjustCompilerFilterCallback;
20 import static com.android.server.art.ArtManagerLocal.BatchDexoptStartCallback;
21 import static com.android.server.art.ArtManagerLocal.DexoptDoneCallback;
22 import static com.android.server.art.ArtManagerLocal.ScheduleBackgroundDexoptJobCallback;
23 
24 import android.annotation.NonNull;
25 import android.annotation.Nullable;
26 
27 import com.android.internal.annotations.GuardedBy;
28 import com.android.server.art.ArtManagerLocal;
29 
30 import com.google.auto.value.AutoValue;
31 
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import java.util.concurrent.Executor;
36 
37 /**
38  * A class that stores the configurations set by the consumer of ART Service at runtime. This class
39  * is thread-safe.
40  *
41  * @hide
42  */
43 public class Config {
44     /** @see ArtManagerLocal#setBatchDexoptStartCallback(Executor, BatchDexoptStartCallback) */
45     @GuardedBy("this")
46     @Nullable
47     private Callback<BatchDexoptStartCallback, Void> mBatchDexoptStartCallback = null;
48 
49     /**
50      * @see ArtManagerLocal#setScheduleBackgroundDexoptJobCallback(Executor,
51      *         ScheduleBackgroundDexoptJobCallback)
52      */
53     @GuardedBy("this")
54     @Nullable
55     private Callback<ScheduleBackgroundDexoptJobCallback, Void>
56             mScheduleBackgroundDexoptJobCallback = null;
57 
58     /**
59      * @see ArtManagerLocal#addDexoptDoneCallback(Executor, DexoptDoneCallback)
60      */
61     @GuardedBy("this")
62     @NonNull
63     private LinkedHashMap<DexoptDoneCallback, Callback<DexoptDoneCallback, Boolean>>
64             mDexoptDoneCallbacks = new LinkedHashMap<>();
65 
66     /**
67      * @see ArtManagerLocal#setAdjustCompilerFilterCallback(Executor,
68      *         AdjustCompilerFilterCallback)
69      */
70     @GuardedBy("this")
71     @Nullable
72     private Callback<AdjustCompilerFilterCallback, Void> mAdjustCompilerFilterCallback = null;
73 
setBatchDexoptStartCallback( @onNull Executor executor, @NonNull BatchDexoptStartCallback callback)74     public synchronized void setBatchDexoptStartCallback(
75             @NonNull Executor executor, @NonNull BatchDexoptStartCallback callback) {
76         mBatchDexoptStartCallback = Callback.create(callback, executor);
77     }
78 
clearBatchDexoptStartCallback()79     public synchronized void clearBatchDexoptStartCallback() {
80         mBatchDexoptStartCallback = null;
81     }
82 
83     @Nullable
getBatchDexoptStartCallback()84     public synchronized Callback<BatchDexoptStartCallback, Void> getBatchDexoptStartCallback() {
85         return mBatchDexoptStartCallback;
86     }
87 
setScheduleBackgroundDexoptJobCallback( @onNull Executor executor, @NonNull ScheduleBackgroundDexoptJobCallback callback)88     public synchronized void setScheduleBackgroundDexoptJobCallback(
89             @NonNull Executor executor, @NonNull ScheduleBackgroundDexoptJobCallback callback) {
90         mScheduleBackgroundDexoptJobCallback = Callback.create(callback, executor);
91     }
92 
clearScheduleBackgroundDexoptJobCallback()93     public synchronized void clearScheduleBackgroundDexoptJobCallback() {
94         mScheduleBackgroundDexoptJobCallback = null;
95     }
96 
97     @Nullable
98     public synchronized Callback<ScheduleBackgroundDexoptJobCallback, Void>
getScheduleBackgroundDexoptJobCallback()99     getScheduleBackgroundDexoptJobCallback() {
100         return mScheduleBackgroundDexoptJobCallback;
101     }
102 
addDexoptDoneCallback(boolean onlyIncludeUpdates, @NonNull Executor executor, @NonNull DexoptDoneCallback callback)103     public synchronized void addDexoptDoneCallback(boolean onlyIncludeUpdates,
104             @NonNull Executor executor, @NonNull DexoptDoneCallback callback) {
105         if (mDexoptDoneCallbacks.putIfAbsent(
106                     callback, Callback.create(callback, executor, onlyIncludeUpdates))
107                 != null) {
108             throw new IllegalStateException("callback already added");
109         }
110     }
111 
removeDexoptDoneCallback(@onNull DexoptDoneCallback callback)112     public synchronized void removeDexoptDoneCallback(@NonNull DexoptDoneCallback callback) {
113         mDexoptDoneCallbacks.remove(callback);
114     }
115 
116     @NonNull
getDexoptDoneCallbacks()117     public synchronized List<Callback<DexoptDoneCallback, Boolean>> getDexoptDoneCallbacks() {
118         return new ArrayList<>(mDexoptDoneCallbacks.values());
119     }
120 
setAdjustCompilerFilterCallback( @onNull Executor executor, @NonNull AdjustCompilerFilterCallback callback)121     public synchronized void setAdjustCompilerFilterCallback(
122             @NonNull Executor executor, @NonNull AdjustCompilerFilterCallback callback) {
123         mAdjustCompilerFilterCallback = Callback.create(callback, executor);
124     }
125 
clearAdjustCompilerFilterCallback()126     public synchronized void clearAdjustCompilerFilterCallback() {
127         mAdjustCompilerFilterCallback = null;
128     }
129 
130     @Nullable
131     public synchronized Callback<AdjustCompilerFilterCallback, Void>
getAdjustCompilerFilterCallback()132     getAdjustCompilerFilterCallback() {
133         return mAdjustCompilerFilterCallback;
134     }
135 
136     @AutoValue
137     public static abstract class Callback<CallbackType, ExtraType> {
get()138         public abstract @NonNull CallbackType get();
executor()139         public abstract @NonNull Executor executor();
extra()140         public abstract @Nullable ExtraType extra();
create( @onNull CallbackType callback, @NonNull Executor executor, @Nullable ExtraType extra)141         static <CallbackType, ExtraType> @NonNull Callback<CallbackType, ExtraType> create(
142                 @NonNull CallbackType callback, @NonNull Executor executor,
143                 @Nullable ExtraType extra) {
144             return new AutoValue_Config_Callback<CallbackType, ExtraType>(
145                     callback, executor, extra);
146         }
create( @onNull CallbackType callback, @NonNull Executor executor)147         static <CallbackType> @NonNull Callback<CallbackType, Void> create(
148                 @NonNull CallbackType callback, @NonNull Executor executor) {
149             return new AutoValue_Config_Callback<CallbackType, Void>(
150                     callback, executor, null /* extra */);
151         }
152     }
153 }
154