• 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 android.app.sdksandbox;
18 
19 import android.annotation.NonNull;
20 import android.os.IBinder;
21 import android.os.RemoteException;
22 import android.util.Log;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 
26 import java.util.Objects;
27 
28 /**
29  * Singleton for a privacy sandbox, which is initialised when the sandbox is created.
30  *
31  * @hide
32  */
33 public class SdkSandboxLocalSingleton {
34 
35     private static final String TAG = "SandboxLocalSingleton";
36     private static SdkSandboxLocalSingleton sInstance = null;
37     private final ISdkToServiceCallback mSdkToServiceCallback;
38 
SdkSandboxLocalSingleton(ISdkToServiceCallback sdkToServiceCallback)39     private SdkSandboxLocalSingleton(ISdkToServiceCallback sdkToServiceCallback) {
40         mSdkToServiceCallback = sdkToServiceCallback;
41     }
42 
43     /**
44      * Returns a singleton instance of this class. TODO(b/247313241): Fix parameter once aidl issues
45      * are fixed.
46      *
47      * @param sdkToServiceBinder callback to support communication with the {@link
48      *     com.android.server.sdksandbox.SdkSandboxManagerService}
49      * @throws IllegalStateException if singleton is already initialised
50      * @throws UnsupportedOperationException if the interface passed is not of type {@link
51      *     ISdkToServiceCallback}
52      */
initInstance(@onNull IBinder sdkToServiceBinder)53     public static synchronized void initInstance(@NonNull IBinder sdkToServiceBinder) {
54         if (sInstance != null) {
55             Log.d(TAG, "Already Initialised");
56             return;
57         }
58         try {
59             if (Objects.nonNull(sdkToServiceBinder)
60                     && sdkToServiceBinder
61                             .getInterfaceDescriptor()
62                             .equals(ISdkToServiceCallback.DESCRIPTOR)) {
63                 sInstance =
64                         new SdkSandboxLocalSingleton(
65                                 ISdkToServiceCallback.Stub.asInterface(sdkToServiceBinder));
66                 return;
67             }
68         } catch (RemoteException e) {
69             // Fall through to the failure case.
70         }
71         throw new UnsupportedOperationException("IBinder not supported");
72     }
73 
74     /** Returns an already initialised singleton instance of this class. */
getExistingInstance()75     public static SdkSandboxLocalSingleton getExistingInstance() {
76         if (sInstance == null) {
77             throw new IllegalStateException("SdkSandboxLocalSingleton not found");
78         }
79         return sInstance;
80     }
81 
82     /** To reset the singleton. Only for Testing. */
83     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
destroySingleton()84     public static void destroySingleton() {
85         sInstance = null;
86     }
87 
88     /** Gets the callback to the {@link com.android.server.sdksandbox.SdkSandboxManagerService} */
getSdkToServiceCallback()89     public ISdkToServiceCallback getSdkToServiceCallback() {
90         return mSdkToServiceCallback;
91     }
92 }
93