summaryrefslogtreecommitdiff
path: root/sdksandbox/framework/java/android/app/sdksandbox/SandboxedSdkContext.java
blob: f90886fdf1d606c13d21a26a5e71c1d1cfe2c2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.sdksandbox;

import static android.app.sdksandbox.SdkSandboxSystemServiceRegistry.ServiceMutator;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;

import com.android.internal.annotations.VisibleForTesting;

import java.io.File;

/**
 * Refers to the context of the SDK loaded in the SDK sandbox process.
 *
 * <p>It is a wrapper of the client application (which loading SDK to the sandbox) context, to
 * represent the context of the SDK loaded by that application.
 *
 * <p>An instance of the {@link SandboxedSdkContext} will be created by the SDK sandbox, and then
 * attached to the {@link SandboxedSdkProvider} after the SDK is loaded.
 *
 * <p>Each sdk will get their own private storage directory and the file storage API on this object
 * will utilize those area.
 *
 * <p>Note: All APIs defined in this class are not stable and subject to change.
 *
 * @hide
 */
public final class SandboxedSdkContext extends ContextWrapper {

    private final Resources mResources;
    private final AssetManager mAssets;
    private final String mClientPackageName;
    private final String mSdkName;
    private final ApplicationInfo mSdkProviderInfo;
    @Nullable private final File mCeDataDir;
    @Nullable private final File mDeDataDir;
    private final SdkSandboxSystemServiceRegistry mSdkSandboxSystemServiceRegistry;
    private final ClassLoader mClassLoader;

    public SandboxedSdkContext(
            @NonNull Context baseContext,
            @NonNull ClassLoader classLoader,
            @NonNull String clientPackageName,
            @NonNull ApplicationInfo info,
            @NonNull String sdkName,
            @Nullable String sdkCeDataDir,
            @Nullable String sdkDeDataDir) {
        this(
                baseContext,
                classLoader,
                clientPackageName,
                info,
                sdkName,
                sdkCeDataDir,
                sdkDeDataDir,
                SdkSandboxSystemServiceRegistry.getInstance());
    }

    @VisibleForTesting
    public SandboxedSdkContext(
            @NonNull Context baseContext,
            @NonNull ClassLoader classLoader,
            @NonNull String clientPackageName,
            @NonNull ApplicationInfo info,
            @NonNull String sdkName,
            @Nullable String sdkCeDataDir,
            @Nullable String sdkDeDataDir,
            SdkSandboxSystemServiceRegistry sdkSandboxSystemServiceRegistry) {
        super(baseContext);
        mClientPackageName = clientPackageName;
        mSdkName = sdkName;
        mSdkProviderInfo = info;
        Resources resources = null;
        try {
            resources = baseContext.getPackageManager().getResourcesForApplication(info);
        } catch (Exception ignored) {
        }

        if (resources != null) {
            mResources = resources;
            mAssets = resources.getAssets();
        } else {
            mResources = null;
            mAssets = null;
        }

        mCeDataDir = (sdkCeDataDir != null) ? new File(sdkCeDataDir) : null;
        mDeDataDir = (sdkDeDataDir != null) ? new File(sdkDeDataDir) : null;

        mSdkSandboxSystemServiceRegistry = sdkSandboxSystemServiceRegistry;
        mClassLoader = classLoader;
    }

    /**
     * Return a new Context object for the current SandboxedSdkContext but whose storage APIs are
     * backed by sdk specific credential-protected storage.
     *
     * @see Context#isCredentialProtectedStorage()
     */
    @Override
    @NonNull
    public Context createCredentialProtectedStorageContext() {
        Context newBaseContext = getBaseContext().createCredentialProtectedStorageContext();
        return new SandboxedSdkContext(
                newBaseContext,
                mClassLoader,
                mClientPackageName,
                mSdkProviderInfo,
                mSdkName,
                (mCeDataDir != null) ? mCeDataDir.toString() : null,
                (mDeDataDir != null) ? mDeDataDir.toString() : null);
    }

    /**
     * Return a new Context object for the current SandboxedSdkContext but whose storage
     * APIs are backed by sdk specific device-protected storage.
     *
     * @see Context#isDeviceProtectedStorage()
     */
    @Override
    @NonNull
    public Context createDeviceProtectedStorageContext() {
        Context newBaseContext = getBaseContext().createDeviceProtectedStorageContext();
        return new SandboxedSdkContext(
                newBaseContext,
                mClassLoader,
                mClientPackageName,
                mSdkProviderInfo,
                mSdkName,
                (mCeDataDir != null) ? mCeDataDir.toString() : null,
                (mDeDataDir != null) ? mDeDataDir.toString() : null);
    }

    /**
     * Returns the SDK name defined in the SDK's manifest.
     */
    @NonNull
    public String getSdkName() {
        return mSdkName;
    }

    /**
     * Returns the SDK package name defined in the SDK's manifest.
     *
     * @hide
     */
    @NonNull
    public String getSdkPackageName() {
        return mSdkProviderInfo.packageName;
    }

    /**
     * Returns the package name of the client application corresponding to the sandbox.
     *
     */
    @NonNull
    public String getClientPackageName() {
        return mClientPackageName;
    }

    /** Returns the resources defined in the SDK's .apk file. */
    @Override
    @Nullable
    public Resources getResources() {
        return mResources;
    }

    /** Returns the assets defined in the SDK's .apk file. */
    @Override
    @Nullable
    public AssetManager getAssets() {
        return mAssets;
    }

    /** Returns sdk-specific internal storage directory. */
    @Override
    @Nullable
    public File getDataDir() {
        File res = null;
        if (isCredentialProtectedStorage()) {
            res = mCeDataDir;
        } else if (isDeviceProtectedStorage()) {
            res = mDeDataDir;
        }
        if (res == null) {
            throw new RuntimeException("No data directory found for sdk: " + getSdkName());
        }
        return res;
    }

    @Override
    @Nullable
    public Object getSystemService(String name) {
        if (name == null) {
            return null;
        }
        Object service = getBaseContext().getSystemService(name);
        ServiceMutator serviceMutator = mSdkSandboxSystemServiceRegistry.getServiceMutator(name);
        if (serviceMutator != null) {
            service = serviceMutator.setContext(service, this);
        }
        return service;
    }

    @Override
    public ClassLoader getClassLoader() {
        return mClassLoader;
    }
}