Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1 | /* |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 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 | package com.android.server.appsearch; |
| 17 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 18 | import static android.app.appsearch.AppSearchResult.throwableToFailedResult; |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 19 | import static android.os.Process.INVALID_UID; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 20 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 21 | import android.Manifest; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 22 | import android.annotation.ElapsedRealtimeLong; |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 24 | import android.annotation.WorkerThread; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 25 | import android.app.appsearch.AppSearchBatchResult; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 26 | import android.app.appsearch.AppSearchMigrationHelper; |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 27 | import android.app.appsearch.AppSearchResult; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 28 | import android.app.appsearch.AppSearchSchema; |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 29 | import android.app.appsearch.GenericDocument; |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 30 | import android.app.appsearch.GetSchemaResponse; |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 31 | import android.app.appsearch.SearchResultPage; |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 32 | import android.app.appsearch.SearchSpec; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 33 | import android.app.appsearch.SetSchemaResponse; |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 34 | import android.app.appsearch.StorageInfo; |
Terry Wang | 5b0c561 | 2021-12-23 15:57:19 -0800 | [diff] [blame] | 35 | import android.app.appsearch.VisibilityDocument; |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 36 | import android.app.appsearch.aidl.AppSearchBatchResultParcel; |
| 37 | import android.app.appsearch.aidl.AppSearchResultParcel; |
| 38 | import android.app.appsearch.aidl.IAppSearchBatchResultCallback; |
| 39 | import android.app.appsearch.aidl.IAppSearchManager; |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 40 | import android.app.appsearch.aidl.IAppSearchObserverProxy; |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 41 | import android.app.appsearch.aidl.IAppSearchResultCallback; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 42 | import android.app.appsearch.exceptions.AppSearchException; |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 43 | import android.app.appsearch.observer.ObserverSpec; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 44 | import android.content.BroadcastReceiver; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 45 | import android.content.Context; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 46 | import android.content.Intent; |
| 47 | import android.content.IntentFilter; |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 48 | import android.content.pm.PackageInfo; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 49 | import android.content.pm.PackageManager; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 50 | import android.content.pm.PackageStats; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 51 | import android.os.Binder; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 52 | import android.os.Bundle; |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 53 | import android.os.ParcelFileDescriptor; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 54 | import android.os.RemoteException; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 55 | import android.os.SystemClock; |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 56 | import android.os.UserHandle; |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 57 | import android.os.UserManager; |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 58 | import android.util.ArraySet; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 59 | import android.util.Log; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 60 | |
Cassie Wang | 15c8697 | 2021-02-09 13:43:25 -0800 | [diff] [blame] | 61 | import com.android.internal.annotations.GuardedBy; |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 62 | import com.android.server.LocalManagerRegistry; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 63 | import com.android.server.SystemService; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 64 | import com.android.server.appsearch.external.localstorage.stats.CallStats; |
Alexander Dorokhine | 14816e2 | 2021-07-12 10:53:24 -0700 | [diff] [blame] | 65 | import com.android.server.appsearch.external.localstorage.stats.OptimizeStats; |
Alexander Dorokhine | 72f5e37 | 2022-02-07 17:23:49 -0800 | [diff] [blame] | 66 | import com.android.server.appsearch.external.localstorage.visibilitystore.CallerAccess; |
Alexander Dorokhine | 44c0497 | 2021-06-22 12:32:15 -0700 | [diff] [blame] | 67 | import com.android.server.appsearch.external.localstorage.visibilitystore.VisibilityStore; |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 68 | import com.android.server.appsearch.observer.AppSearchObserverProxy; |
Xiaoyu Jin | 1032947 | 2021-06-23 16:50:03 -0700 | [diff] [blame] | 69 | import com.android.server.appsearch.stats.StatsCollector; |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 70 | import com.android.server.appsearch.util.PackageUtil; |
Alexander Dorokhine | 655aec0 | 2022-02-07 17:24:01 -0800 | [diff] [blame] | 71 | import com.android.server.appsearch.visibilitystore.FrameworkCallerAccess; |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 72 | import com.android.server.usage.StorageStatsManagerLocal; |
| 73 | import com.android.server.usage.StorageStatsManagerLocal.StorageStatsAugmenter; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 74 | |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 75 | import com.google.android.icing.proto.PersistType; |
| 76 | |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 77 | import java.io.DataInputStream; |
| 78 | import java.io.DataOutputStream; |
| 79 | import java.io.EOFException; |
| 80 | import java.io.FileInputStream; |
| 81 | import java.io.FileOutputStream; |
Alexander Dorokhine | 6a99f94 | 2020-12-04 02:57:22 -0800 | [diff] [blame] | 82 | import java.util.ArrayList; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 83 | import java.util.List; |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 84 | import java.util.Map; |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 85 | import java.util.Objects; |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 86 | import java.util.Set; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 87 | import java.util.concurrent.Executor; |
Terry Wang | d2186e5 | 2021-04-14 13:19:45 -0700 | [diff] [blame] | 88 | import java.util.concurrent.LinkedBlockingQueue; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 89 | import java.util.concurrent.ThreadPoolExecutor; |
| 90 | import java.util.concurrent.TimeUnit; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 91 | |
Alexander Dorokhine | 5d2bb0b | 2021-06-24 22:58:46 -0700 | [diff] [blame] | 92 | /** |
| 93 | * The main service implementation which contains AppSearch's platform functionality. |
Yang Yu | c047f7b | 2021-06-25 12:22:42 -0700 | [diff] [blame] | 94 | * |
Alexander Dorokhine | 5d2bb0b | 2021-06-24 22:58:46 -0700 | [diff] [blame] | 95 | * @hide |
| 96 | */ |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 97 | public class AppSearchManagerService extends SystemService { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 98 | private static final String TAG = "AppSearchManagerService"; |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 99 | private final Context mContext; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 100 | private PackageManager mPackageManager; |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 101 | private UserManager mUserManager; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 102 | private AppSearchUserInstanceManager mAppSearchUserInstanceManager; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 103 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 104 | // Never call shutdownNow(). It will cancel the futures it's returned. And since |
| 105 | // Executor#execute won't return anything, we will hang forever waiting for the execution. |
| 106 | // AppSearch multi-thread execution is guarded by Read & Write Lock in AppSearchImpl, all |
| 107 | // mutate requests will need to gain write lock and query requests need to gain read lock. |
| 108 | private static final Executor EXECUTOR = new ThreadPoolExecutor(/*corePoolSize=*/1, |
| 109 | Runtime.getRuntime().availableProcessors(), /*keepAliveTime*/ 60L, TimeUnit.SECONDS, |
Terry Wang | d2186e5 | 2021-04-14 13:19:45 -0700 | [diff] [blame] | 110 | new LinkedBlockingQueue<>()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 111 | |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 112 | // Cache of unlocked users so we don't have to query UserManager service each time. The "locked" |
| 113 | // suffix refers to the fact that access to the field should be locked; unrelated to the |
| 114 | // unlocked status of users. |
| 115 | @GuardedBy("mUnlockedUsersLocked") |
| 116 | private final Set<UserHandle> mUnlockedUsersLocked = new ArraySet<>(); |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 117 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 118 | public AppSearchManagerService(Context context) { |
| 119 | super(context); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 120 | mContext = context; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onStart() { |
| 125 | publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 126 | mPackageManager = getContext().getPackageManager(); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 127 | mAppSearchUserInstanceManager = AppSearchUserInstanceManager.getInstance(); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 128 | mUserManager = mContext.getSystemService(UserManager.class); |
| 129 | registerReceivers(); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 130 | LocalManagerRegistry.getManager(StorageStatsManagerLocal.class) |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 131 | .registerStorageStatsAugmenter(new AppSearchStorageStatsAugmenter(), TAG); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Xiaoyu Jin | 1032947 | 2021-06-23 16:50:03 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public void onBootPhase(/* @BootPhase */ int phase) { |
| 136 | if (phase == PHASE_BOOT_COMPLETED) { |
| 137 | StatsCollector.getInstance(mContext, EXECUTOR); |
| 138 | } |
| 139 | } |
| 140 | |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 141 | private void registerReceivers() { |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 142 | mContext.registerReceiverForAllUsers( |
| 143 | new UserActionReceiver(), |
| 144 | new IntentFilter(Intent.ACTION_USER_REMOVED), |
| 145 | /*broadcastPermission=*/ null, |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 146 | /*scheduler=*/ null); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 147 | |
| 148 | //TODO(b/145759910) Add a direct callback when user clears the data instead of relying on |
| 149 | // broadcasts |
| 150 | IntentFilter packageChangedFilter = new IntentFilter(); |
| 151 | packageChangedFilter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED); |
| 152 | packageChangedFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); |
| 153 | packageChangedFilter.addDataScheme("package"); |
| 154 | packageChangedFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 155 | mContext.registerReceiverForAllUsers( |
| 156 | new PackageChangedReceiver(), |
| 157 | packageChangedFilter, |
| 158 | /*broadcastPermission=*/ null, |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 159 | /*scheduler=*/ null); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private class UserActionReceiver extends BroadcastReceiver { |
| 163 | @Override |
| 164 | public void onReceive(@NonNull Context context, @NonNull Intent intent) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 165 | Objects.requireNonNull(context); |
| 166 | Objects.requireNonNull(intent); |
| 167 | |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 168 | switch (intent.getAction()) { |
| 169 | case Intent.ACTION_USER_REMOVED: |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 170 | UserHandle userHandle = intent.getParcelableExtra(Intent.EXTRA_USER); |
| 171 | if (userHandle == null) { |
| 172 | Log.e(TAG, "Extra " |
| 173 | + Intent.EXTRA_USER + " is missing in the intent: " + intent); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 176 | handleUserRemoved(userHandle); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 177 | break; |
| 178 | default: |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 179 | Log.e(TAG, "Received unknown intent: " + intent); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Handles user removed action. |
| 186 | * |
| 187 | * <p>Only need to clear the AppSearchImpl instance. The data of AppSearch is saved in the |
| 188 | * "credential encrypted" system directory of each user. That directory will be auto-deleted |
| 189 | * when a user is removed. |
| 190 | * |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 191 | * @param userHandle The multi-user handle of the user that need to be removed. |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 192 | * @see android.os.Environment#getDataSystemCeDirectory |
| 193 | */ |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 194 | private void handleUserRemoved(@NonNull UserHandle userHandle) { |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 195 | try { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 196 | mAppSearchUserInstanceManager.closeAndRemoveUserInstance(userHandle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 197 | Log.i(TAG, "Removed AppSearchImpl instance for: " + userHandle); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 198 | } catch (Throwable t) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 199 | Log.e(TAG, "Unable to remove data for: " + userHandle, t); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | private class PackageChangedReceiver extends BroadcastReceiver { |
| 204 | @Override |
| 205 | public void onReceive(@NonNull Context context, @NonNull Intent intent) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 206 | Objects.requireNonNull(context); |
| 207 | Objects.requireNonNull(intent); |
| 208 | |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 209 | switch (intent.getAction()) { |
| 210 | case Intent.ACTION_PACKAGE_FULLY_REMOVED: |
| 211 | case Intent.ACTION_PACKAGE_DATA_CLEARED: |
| 212 | String packageName = intent.getData().getSchemeSpecificPart(); |
| 213 | if (packageName == null) { |
| 214 | Log.e(TAG, "Package name is missing in the intent: " + intent); |
| 215 | return; |
| 216 | } |
| 217 | int uid = intent.getIntExtra(Intent.EXTRA_UID, INVALID_UID); |
| 218 | if (uid == INVALID_UID) { |
| 219 | Log.e(TAG, "uid is missing in the intent: " + intent); |
| 220 | return; |
| 221 | } |
| 222 | handlePackageRemoved(packageName, uid); |
| 223 | break; |
| 224 | default: |
| 225 | Log.e(TAG, "Received unknown intent: " + intent); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 230 | private void handlePackageRemoved(@NonNull String packageName, int uid) { |
| 231 | UserHandle userHandle = UserHandle.getUserHandleForUid(uid); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 232 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 233 | if (isUserLocked(userHandle)) { |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 234 | // We cannot access a locked user's directry and remove package data from it. |
| 235 | // We should remove those uninstalled package data when the user is unlocking. |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 236 | return; |
| 237 | } |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 238 | // Only clear the package's data if AppSearch exists for this user. |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 239 | if (AppSearchUserInstanceManager.getAppSearchDir(userHandle).exists()) { |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 240 | Context userContext = mContext.createContextAsUser(userHandle, /*flags=*/ 0); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 241 | AppSearchUserInstance instance = |
| 242 | mAppSearchUserInstanceManager.getOrCreateUserInstance( |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 243 | userContext, userHandle, AppSearchConfig.getInstance(EXECUTOR)); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 244 | //TODO(b/145759910) clear visibility setting for package. |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 245 | instance.getAppSearchImpl().clearPackageData(packageName); |
| 246 | instance.getLogger().removeCachedUidForPackage(packageName); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 247 | } |
| 248 | } catch (Throwable t) { |
| 249 | Log.e(TAG, "Unable to remove data for package: " + packageName, t); |
Terry Wang | 12dc6c0 | 2021-03-31 19:26:16 -0700 | [diff] [blame] | 250 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 253 | @Override |
Pinyao Ting | d5c2ed9 | 2021-03-18 14:51:54 -0700 | [diff] [blame] | 254 | public void onUserUnlocking(@NonNull TargetUser user) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 255 | Objects.requireNonNull(user); |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 256 | UserHandle userHandle = user.getUserHandle(); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 257 | synchronized (mUnlockedUsersLocked) { |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 258 | mUnlockedUsersLocked.add(userHandle); |
Cassie Wang | 15c8697 | 2021-02-09 13:43:25 -0800 | [diff] [blame] | 259 | } |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 260 | EXECUTOR.execute(() -> { |
| 261 | try { |
| 262 | // Only clear the package's data if AppSearch exists for this user. |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 263 | if (AppSearchUserInstanceManager.getAppSearchDir(userHandle).exists()) { |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 264 | Context userContext = mContext.createContextAsUser(userHandle, /*flags=*/ 0); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 265 | AppSearchUserInstance instance = |
| 266 | mAppSearchUserInstanceManager.getOrCreateUserInstance( |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 267 | userContext, userHandle, AppSearchConfig.getInstance(EXECUTOR)); |
| 268 | List<PackageInfo> installedPackageInfos = userContext |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 269 | .getPackageManager() |
| 270 | .getInstalledPackages(/*flags=*/0); |
| 271 | Set<String> packagesToKeep = new ArraySet<>(installedPackageInfos.size()); |
| 272 | for (int i = 0; i < installedPackageInfos.size(); i++) { |
| 273 | packagesToKeep.add(installedPackageInfos.get(i).packageName); |
| 274 | } |
Terry Wang | 0be9c30 | 2022-01-21 14:36:18 -0800 | [diff] [blame] | 275 | packagesToKeep.add(VisibilityStore.VISIBILITY_PACKAGE_NAME); |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 276 | //TODO(b/145759910) clear visibility setting for package. |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 277 | instance.getAppSearchImpl().prunePackageData(packagesToKeep); |
Terry Wang | a9e6e21 | 2021-04-23 15:51:37 -0700 | [diff] [blame] | 278 | } |
| 279 | } catch (Throwable t) { |
| 280 | Log.e(TAG, "Unable to prune packages for " + user, t); |
| 281 | } |
| 282 | }); |
Cassie Wang | 9ba9ae1 | 2021-02-01 16:39:37 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Terry Wang | de9f338 | 2021-04-28 19:45:07 -0700 | [diff] [blame] | 285 | @Override |
| 286 | public void onUserStopping(@NonNull TargetUser user) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 287 | Objects.requireNonNull(user); |
| 288 | |
| 289 | synchronized (mUnlockedUsersLocked) { |
| 290 | UserHandle userHandle = user.getUserHandle(); |
| 291 | mUnlockedUsersLocked.remove(userHandle); |
Terry Wang | de9f338 | 2021-04-28 19:45:07 -0700 | [diff] [blame] | 292 | try { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 293 | mAppSearchUserInstanceManager.closeAndRemoveUserInstance(userHandle); |
Terry Wang | de9f338 | 2021-04-28 19:45:07 -0700 | [diff] [blame] | 294 | } catch (Throwable t) { |
| 295 | Log.e(TAG, "Error handling user stopping.", t); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 300 | private void verifyUserUnlocked(@NonNull UserHandle callingUser) { |
| 301 | if (isUserLocked(callingUser)) { |
| 302 | throw new IllegalStateException(callingUser + " is locked or not running."); |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 306 | private boolean isUserLocked(@NonNull UserHandle callingUser) { |
| 307 | synchronized (mUnlockedUsersLocked) { |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 308 | // First, check the local copy. |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 309 | if (mUnlockedUsersLocked.contains(callingUser)) { |
Terry Wang | e201dc0 | 2021-04-16 01:03:20 -0700 | [diff] [blame] | 310 | return false; |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 311 | } |
| 312 | // If the local copy says the user is locked, check with UM for the actual state, |
| 313 | // since the user might just have been unlocked. |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 314 | return !mUserManager.isUserUnlockingOrUnlocked(callingUser); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 318 | private class Stub extends IAppSearchManager.Stub { |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 319 | @Override |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 320 | public void setSchema( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 321 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 322 | @NonNull String databaseName, |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 323 | @NonNull List<Bundle> schemaBundles, |
Terry Wang | 5b0c561 | 2021-12-23 15:57:19 -0800 | [diff] [blame] | 324 | @NonNull List<Bundle> visibilityBundles, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 325 | boolean forceOverride, |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 326 | int schemaVersion, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 327 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 328 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 329 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 330 | Objects.requireNonNull(packageName); |
| 331 | Objects.requireNonNull(databaseName); |
| 332 | Objects.requireNonNull(schemaBundles); |
Terry Wang | 5b0c561 | 2021-12-23 15:57:19 -0800 | [diff] [blame] | 333 | Objects.requireNonNull(visibilityBundles); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 334 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 335 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 336 | |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 337 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 338 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 339 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 340 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 341 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 342 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 343 | int operationSuccessCount = 0; |
| 344 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 345 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 346 | verifyCaller(callingUid, packageName); |
| 347 | |
| 348 | // Obtain the user where the client wants to run the operations in. This should |
| 349 | // end up being the same as userHandle, assuming it is not a special user and |
| 350 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 351 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 352 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 353 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 354 | List<AppSearchSchema> schemas = new ArrayList<>(schemaBundles.size()); |
| 355 | for (int i = 0; i < schemaBundles.size(); i++) { |
| 356 | schemas.add(new AppSearchSchema(schemaBundles.get(i))); |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 357 | } |
Terry Wang | 5b0c561 | 2021-12-23 15:57:19 -0800 | [diff] [blame] | 358 | List<VisibilityDocument> visibilityDocuments = |
| 359 | new ArrayList<>(visibilityBundles.size()); |
| 360 | for (int i = 0; i < visibilityBundles.size(); i++) { |
| 361 | visibilityDocuments.add( |
| 362 | new VisibilityDocument(visibilityBundles.get(i))); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 363 | } |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 364 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | ba42f39 | 2021-08-17 15:17:40 -0700 | [diff] [blame] | 365 | // TODO(b/173532925): Implement logging for statsBuilder |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 366 | SetSchemaResponse setSchemaResponse = instance.getAppSearchImpl().setSchema( |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 367 | packageName, |
| 368 | databaseName, |
| 369 | schemas, |
Terry Wang | e15ea58 | 2021-10-21 18:59:21 -0700 | [diff] [blame] | 370 | visibilityDocuments, |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 371 | forceOverride, |
Alexander Dorokhine | ba42f39 | 2021-08-17 15:17:40 -0700 | [diff] [blame] | 372 | schemaVersion, |
| 373 | /*setSchemaStatsBuilder=*/ null); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 374 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 375 | invokeCallbackOnResult(callback, |
| 376 | AppSearchResult.newSuccessfulResult(setSchemaResponse.getBundle())); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 377 | |
| 378 | // setSchema will sync the schemas in the request to AppSearch, any existing |
| 379 | // schemas which is not included in the request will be delete if we force |
| 380 | // override incompatible schemas. And all documents of these types will be |
| 381 | // deleted as well. We should checkForOptimize for these deletion. |
| 382 | checkForOptimize(instance); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 383 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 384 | ++operationFailureCount; |
| 385 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 386 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 387 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 388 | if (instance != null) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 389 | int estimatedBinderLatencyMillis = |
| 390 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 391 | int totalLatencyMillis = |
| 392 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 393 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 394 | .setPackageName(packageName) |
| 395 | .setDatabase(databaseName) |
| 396 | .setStatusCode(statusCode) |
| 397 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 398 | .setCallType(CallStats.CALL_TYPE_SET_SCHEMA) |
| 399 | // TODO(b/173532925) check the existing binder call latency chart |
| 400 | // is good enough for us: |
| 401 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 402 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 403 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 404 | .setNumOperationsFailed(operationFailureCount) |
| 405 | .build()); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 406 | } |
Alexander Dorokhine | ab78906 | 2021-01-11 21:00:00 -0800 | [diff] [blame] | 407 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 408 | }); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 412 | public void getSchema( |
Alexander Dorokhine | d41def4 | 2022-01-25 15:45:34 -0800 | [diff] [blame] | 413 | @NonNull String callingPackageName, |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 414 | @NonNull String packageName, |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 415 | @NonNull String databaseName, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 416 | @NonNull UserHandle userHandle, |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 417 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | d41def4 | 2022-01-25 15:45:34 -0800 | [diff] [blame] | 418 | Objects.requireNonNull(callingPackageName); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 419 | Objects.requireNonNull(packageName); |
| 420 | Objects.requireNonNull(databaseName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 421 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 422 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 423 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 424 | int callingPid = Binder.getCallingPid(); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 425 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 426 | EXECUTOR.execute(() -> { |
| 427 | try { |
Alexander Dorokhine | d41def4 | 2022-01-25 15:45:34 -0800 | [diff] [blame] | 428 | verifyCaller(callingUid, callingPackageName); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 429 | |
| 430 | // Obtain the user where the client wants to run the operations in. This should |
| 431 | // end up being the same as userHandle, assuming it is not a special user and |
| 432 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 433 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 434 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 435 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 436 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 437 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 438 | GetSchemaResponse response = |
Alexander Dorokhine | d41def4 | 2022-01-25 15:45:34 -0800 | [diff] [blame] | 439 | instance.getAppSearchImpl().getSchema( |
Alexander Dorokhine | 661ef9a | 2022-01-28 11:30:45 -0800 | [diff] [blame] | 440 | packageName, |
| 441 | databaseName, |
Alexander Dorokhine | 655aec0 | 2022-02-07 17:24:01 -0800 | [diff] [blame] | 442 | new FrameworkCallerAccess( |
Alexander Dorokhine | 72f5e37 | 2022-02-07 17:23:49 -0800 | [diff] [blame] | 443 | callingPackageName, |
| 444 | callingUid, |
| 445 | instance.getVisibilityChecker() |
| 446 | .doesCallerHaveSystemAccess( |
| 447 | callingPackageName))); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 448 | invokeCallbackOnResult( |
| 449 | callback, |
| 450 | AppSearchResult.newSuccessfulResult(response.getBundle())); |
| 451 | } catch (Throwable t) { |
| 452 | invokeCallbackOnError(callback, t); |
| 453 | } |
| 454 | }); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public void getNamespaces( |
| 459 | @NonNull String packageName, |
| 460 | @NonNull String databaseName, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 461 | @NonNull UserHandle userHandle, |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 462 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 463 | Objects.requireNonNull(packageName); |
| 464 | Objects.requireNonNull(databaseName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 465 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 466 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 467 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 468 | int callingPid = Binder.getCallingPid(); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 469 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 470 | EXECUTOR.execute(() -> { |
| 471 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 472 | verifyCaller(callingUid, packageName); |
| 473 | |
| 474 | // Obtain the user where the client wants to run the operations in. This should |
| 475 | // end up being the same as userHandle, assuming it is not a special user and |
| 476 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 477 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 478 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 479 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 480 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 481 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 482 | List<String> namespaces = |
| 483 | instance.getAppSearchImpl().getNamespaces(packageName, databaseName); |
| 484 | invokeCallbackOnResult( |
| 485 | callback, AppSearchResult.newSuccessfulResult(namespaces)); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 486 | } catch (Throwable t) { |
| 487 | invokeCallbackOnError(callback, t); |
| 488 | } |
| 489 | }); |
Terry Wang | 83a2493 | 2020-12-09 21:00:18 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Override |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 493 | public void putDocuments( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 494 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 495 | @NonNull String databaseName, |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 496 | @NonNull List<Bundle> documentBundles, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 497 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 498 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 499 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 500 | Objects.requireNonNull(packageName); |
| 501 | Objects.requireNonNull(databaseName); |
| 502 | Objects.requireNonNull(documentBundles); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 503 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 504 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 505 | |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 506 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 507 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 508 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 509 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 510 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 511 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 512 | int operationSuccessCount = 0; |
| 513 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 514 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 515 | verifyCaller(callingUid, packageName); |
| 516 | |
| 517 | // Obtain the user where the client wants to run the operations in. This should |
| 518 | // end up being the same as userHandle, assuming it is not a special user and |
| 519 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 520 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 521 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 522 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 523 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 524 | new AppSearchBatchResult.Builder<>(); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 525 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 526 | for (int i = 0; i < documentBundles.size(); i++) { |
| 527 | GenericDocument document = new GenericDocument(documentBundles.get(i)); |
| 528 | try { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 529 | instance.getAppSearchImpl().putDocument( |
| 530 | packageName, databaseName, document, instance.getLogger()); |
| 531 | resultBuilder.setSuccess(document.getId(), /*value=*/ null); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 532 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 533 | } catch (Throwable t) { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 534 | resultBuilder.setResult(document.getId(), throwableToFailedResult(t)); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 535 | AppSearchResult<Void> result = throwableToFailedResult(t); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 536 | resultBuilder.setResult(document.getId(), result); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 537 | // Since we can only include one status code in the atom, |
| 538 | // for failures, we would just save the one for the last failure |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 539 | statusCode = result.getResultCode(); |
| 540 | ++operationFailureCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 541 | } |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 542 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 543 | // Now that the batch has been written. Persist the newly written data. |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 544 | instance.getAppSearchImpl().persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 545 | invokeCallbackOnResult(callback, resultBuilder.build()); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 546 | |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 547 | // Schedule a task to dispatch change notifications. See requirements for where |
| 548 | // the method is called documented in the method description. |
| 549 | dispatchChangeNotifications(instance); |
| 550 | |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 551 | // The existing documents with same ID will be deleted, so there may be some |
| 552 | // resources that could be released after optimize(). |
| 553 | checkForOptimize(instance, /*mutateBatchSize=*/ documentBundles.size()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 554 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 555 | ++operationFailureCount; |
| 556 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 557 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 558 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 559 | if (instance != null) { |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 560 | int estimatedBinderLatencyMillis = |
| 561 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 562 | int totalLatencyMillis = |
| 563 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 564 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 565 | .setPackageName(packageName) |
| 566 | .setDatabase(databaseName) |
| 567 | .setStatusCode(statusCode) |
| 568 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 569 | .setCallType(CallStats.CALL_TYPE_PUT_DOCUMENTS) |
| 570 | // TODO(b/173532925) check the existing binder call latency chart |
| 571 | // is good enough for us: |
| 572 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 573 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 574 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 575 | .setNumOperationsFailed(operationFailureCount) |
| 576 | .build()); |
Xiaoyu Jin | f8cc201 | 2021-03-12 11:00:54 -0800 | [diff] [blame] | 577 | } |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 578 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 579 | }); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 580 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 581 | |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 582 | @Override |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 583 | public void getDocuments( |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 584 | @NonNull String callingPackageName, |
| 585 | @NonNull String targetPackageName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 586 | @NonNull String databaseName, |
| 587 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 588 | @NonNull List<String> ids, |
Alexander Dorokhine | 87cdd15 | 2021-01-20 15:41:25 -0800 | [diff] [blame] | 589 | @NonNull Map<String, List<String>> typePropertyPaths, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 590 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 591 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 592 | boolean global, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 593 | @NonNull IAppSearchBatchResultCallback callback) { |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 594 | Objects.requireNonNull(callingPackageName); |
| 595 | Objects.requireNonNull(targetPackageName); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 596 | Objects.requireNonNull(databaseName); |
| 597 | Objects.requireNonNull(namespace); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 598 | Objects.requireNonNull(ids); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 599 | Objects.requireNonNull(typePropertyPaths); |
| 600 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 601 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 602 | |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 603 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 604 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 605 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 606 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 607 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 608 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 609 | int operationSuccessCount = 0; |
| 610 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 611 | try { |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 612 | verifyCaller(callingUid, callingPackageName); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 613 | |
| 614 | // Obtain the user where the client wants to run the operations in. This should |
| 615 | // end up being the same as userHandle, assuming it is not a special user and |
| 616 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 617 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 618 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 619 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 620 | AppSearchBatchResult.Builder<String, Bundle> resultBuilder = |
| 621 | new AppSearchBatchResult.Builder<>(); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 622 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 623 | for (int i = 0; i < ids.size(); i++) { |
| 624 | String id = ids.get(i); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 625 | try { |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 626 | GenericDocument document; |
| 627 | if (global) { |
| 628 | // TODO(b/203819101) add test to try out security by binding |
| 629 | // directly to binder and passing callingpackage param |
| 630 | document = instance.getAppSearchImpl().globalGetDocument( |
| 631 | targetPackageName, |
| 632 | databaseName, |
| 633 | namespace, |
| 634 | id, |
| 635 | typePropertyPaths, |
Alexander Dorokhine | 655aec0 | 2022-02-07 17:24:01 -0800 | [diff] [blame] | 636 | new FrameworkCallerAccess( |
Alexander Dorokhine | 72f5e37 | 2022-02-07 17:23:49 -0800 | [diff] [blame] | 637 | callingPackageName, |
| 638 | callingUid, |
| 639 | instance.getVisibilityChecker() |
| 640 | .doesCallerHaveSystemAccess( |
| 641 | callingPackageName))); |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 642 | } else { |
| 643 | document = instance.getAppSearchImpl().getDocument( |
| 644 | targetPackageName, |
| 645 | databaseName, |
| 646 | namespace, |
| 647 | id, |
| 648 | typePropertyPaths); |
| 649 | } |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 650 | ++operationSuccessCount; |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 651 | resultBuilder.setSuccess(id, document.getBundle()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 652 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 653 | // Since we can only include one status code in the atom, |
| 654 | // for failures, we would just save the one for the last failure |
| 655 | AppSearchResult<Bundle> result = throwableToFailedResult(t); |
| 656 | resultBuilder.setResult(id, result); |
| 657 | statusCode = result.getResultCode(); |
| 658 | ++operationFailureCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 659 | } |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 660 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 661 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 662 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 663 | ++operationFailureCount; |
| 664 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 665 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 666 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 667 | if (instance != null) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 668 | int estimatedBinderLatencyMillis = |
| 669 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 670 | int totalLatencyMillis = |
| 671 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 672 | instance.getLogger().logStats(new CallStats.Builder() |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 673 | .setPackageName(targetPackageName) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 674 | .setDatabase(databaseName) |
| 675 | .setStatusCode(statusCode) |
| 676 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 677 | .setCallType(CallStats.CALL_TYPE_GET_DOCUMENTS) |
Alex Saveliev | 8763193 | 2022-01-21 00:48:44 +0000 | [diff] [blame] | 678 | // TODO(b/203819101) need mark the call as a global getById |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 679 | // TODO(b/173532925) check the existing binder call latency chart |
| 680 | // is good enough for us: |
| 681 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 682 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 683 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 684 | .setNumOperationsFailed(operationFailureCount) |
| 685 | .build()); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 686 | } |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 687 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 688 | }); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 689 | } |
| 690 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 691 | @Override |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 692 | public void query( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 693 | @NonNull String packageName, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 694 | @NonNull String databaseName, |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 695 | @NonNull String queryExpression, |
| 696 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 697 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 698 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 699 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 700 | Objects.requireNonNull(packageName); |
| 701 | Objects.requireNonNull(databaseName); |
| 702 | Objects.requireNonNull(queryExpression); |
| 703 | Objects.requireNonNull(searchSpecBundle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 704 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 705 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 706 | |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 707 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 708 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 709 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 710 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 711 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 712 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 713 | int operationSuccessCount = 0; |
| 714 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 715 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 716 | verifyCaller(callingUid, packageName); |
| 717 | |
| 718 | // Obtain the user where the client wants to run the operations in. This should |
| 719 | // end up being the same as userHandle, assuming it is not a special user and |
| 720 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 721 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 722 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 723 | |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 724 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 725 | SearchResultPage searchResultPage = instance.getAppSearchImpl().query( |
| 726 | packageName, |
| 727 | databaseName, |
| 728 | queryExpression, |
| 729 | new SearchSpec(searchSpecBundle), |
| 730 | instance.getLogger()); |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 731 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 732 | invokeCallbackOnResult( |
| 733 | callback, |
| 734 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
| 735 | } catch (Throwable t) { |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 736 | ++operationFailureCount; |
| 737 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 738 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 739 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 740 | if (instance != null) { |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 741 | int estimatedBinderLatencyMillis = |
| 742 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 743 | int totalLatencyMillis = |
| 744 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 745 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 746 | .setPackageName(packageName) |
| 747 | .setDatabase(databaseName) |
| 748 | .setStatusCode(statusCode) |
| 749 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 750 | .setCallType(CallStats.CALL_TYPE_SEARCH) |
| 751 | // TODO(b/173532925) check the existing binder call latency chart |
| 752 | // is good enough for us: |
| 753 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 754 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 755 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 756 | .setNumOperationsFailed(operationFailureCount) |
| 757 | .build()); |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 758 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 759 | } |
| 760 | }); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 763 | @Override |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 764 | public void globalQuery( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 765 | @NonNull String packageName, |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 766 | @NonNull String queryExpression, |
| 767 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 768 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 769 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 770 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 771 | Objects.requireNonNull(packageName); |
| 772 | Objects.requireNonNull(queryExpression); |
| 773 | Objects.requireNonNull(searchSpecBundle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 774 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 775 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 776 | |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 777 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 778 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 779 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 780 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 781 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 782 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 783 | int operationSuccessCount = 0; |
| 784 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 785 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 786 | verifyCaller(callingUid, packageName); |
| 787 | |
| 788 | // Obtain the user where the client wants to run the operations in. This should |
| 789 | // end up being the same as userHandle, assuming it is not a special user and |
| 790 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 791 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 792 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 793 | |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 794 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 795 | |
Alexander Dorokhine | 661ef9a | 2022-01-28 11:30:45 -0800 | [diff] [blame] | 796 | boolean callerHasSystemAccess = instance.getVisibilityChecker() |
Terry Wang | 0be9c30 | 2022-01-21 14:36:18 -0800 | [diff] [blame] | 797 | .doesCallerHaveSystemAccess(packageName); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 798 | SearchResultPage searchResultPage = instance.getAppSearchImpl().globalQuery( |
| 799 | queryExpression, |
| 800 | new SearchSpec(searchSpecBundle), |
Alexander Dorokhine | 655aec0 | 2022-02-07 17:24:01 -0800 | [diff] [blame] | 801 | new FrameworkCallerAccess( |
Alexander Dorokhine | 72f5e37 | 2022-02-07 17:23:49 -0800 | [diff] [blame] | 802 | packageName, |
| 803 | callingUid, |
| 804 | callerHasSystemAccess), |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 805 | instance.getLogger()); |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 806 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 807 | invokeCallbackOnResult( |
| 808 | callback, |
| 809 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 810 | } catch (Throwable t) { |
| 811 | ++operationFailureCount; |
| 812 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 813 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 814 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 815 | if (instance != null) { |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 816 | int estimatedBinderLatencyMillis = |
| 817 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 818 | int totalLatencyMillis = |
| 819 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 820 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 821 | .setPackageName(packageName) |
| 822 | .setStatusCode(statusCode) |
| 823 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 824 | .setCallType(CallStats.CALL_TYPE_GLOBAL_SEARCH) |
| 825 | // TODO(b/173532925) check the existing binder call latency chart |
| 826 | // is good enough for us: |
| 827 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 828 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 829 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 830 | .setNumOperationsFailed(operationFailureCount) |
| 831 | .build()); |
Xiaoyu Jin | ef66a42 | 2021-05-09 17:38:59 -0700 | [diff] [blame] | 832 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 833 | } |
| 834 | }); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 837 | @Override |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 838 | public void getNextPage( |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 839 | @NonNull String packageName, |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 840 | long nextPageToken, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 841 | @NonNull UserHandle userHandle, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 842 | @NonNull IAppSearchResultCallback callback) { |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 843 | Objects.requireNonNull(packageName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 844 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 845 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 846 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 847 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 848 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 849 | EXECUTOR.execute(() -> { |
| 850 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 851 | verifyCaller(callingUid, packageName); |
| 852 | |
| 853 | // Obtain the user where the client wants to run the operations in. This should |
| 854 | // end up being the same as userHandle, assuming it is not a special user and |
| 855 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 856 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 857 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 858 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 859 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 860 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | ba42f39 | 2021-08-17 15:17:40 -0700 | [diff] [blame] | 861 | // TODO(b/173532925): Implement logging for statsBuilder |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 862 | SearchResultPage searchResultPage = |
Alexander Dorokhine | ba42f39 | 2021-08-17 15:17:40 -0700 | [diff] [blame] | 863 | instance.getAppSearchImpl().getNextPage( |
| 864 | packageName, nextPageToken, /*statsBuilder=*/ null); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 865 | invokeCallbackOnResult( |
| 866 | callback, |
| 867 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
| 868 | } catch (Throwable t) { |
| 869 | invokeCallbackOnError(callback, t); |
| 870 | } |
| 871 | }); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | @Override |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 875 | public void invalidateNextPageToken(@NonNull String packageName, long nextPageToken, |
| 876 | @NonNull UserHandle userHandle) { |
| 877 | Objects.requireNonNull(packageName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 878 | Objects.requireNonNull(userHandle); |
| 879 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 880 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 881 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 882 | EXECUTOR.execute(() -> { |
| 883 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 884 | verifyCaller(callingUid, packageName); |
| 885 | |
| 886 | // Obtain the user where the client wants to run the operations in. This should |
| 887 | // end up being the same as userHandle, assuming it is not a special user and |
| 888 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 889 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 890 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 891 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 892 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 893 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | d7e3989 | 2021-07-02 14:50:05 -0700 | [diff] [blame] | 894 | instance.getAppSearchImpl().invalidateNextPageToken(packageName, nextPageToken); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 895 | } catch (Throwable t) { |
| 896 | Log.e(TAG, "Unable to invalidate the query page token", t); |
| 897 | } |
| 898 | }); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 899 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 900 | |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 901 | @Override |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 902 | public void writeQueryResultsToFile( |
| 903 | @NonNull String packageName, |
| 904 | @NonNull String databaseName, |
| 905 | @NonNull ParcelFileDescriptor fileDescriptor, |
| 906 | @NonNull String queryExpression, |
| 907 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 908 | @NonNull UserHandle userHandle, |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 909 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 910 | Objects.requireNonNull(packageName); |
| 911 | Objects.requireNonNull(databaseName); |
| 912 | Objects.requireNonNull(fileDescriptor); |
| 913 | Objects.requireNonNull(queryExpression); |
| 914 | Objects.requireNonNull(searchSpecBundle); |
| 915 | Objects.requireNonNull(userHandle); |
| 916 | Objects.requireNonNull(callback); |
| 917 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 918 | int callingPid = Binder.getCallingPid(); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 919 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 920 | EXECUTOR.execute(() -> { |
| 921 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 922 | verifyCaller(callingUid, packageName); |
| 923 | |
| 924 | // Obtain the user where the client wants to run the operations in. This should |
| 925 | // end up being the same as userHandle, assuming it is not a special user and |
| 926 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 927 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 928 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 929 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 930 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 931 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 932 | // we don't need to append the file. The file is always brand new. |
| 933 | try (DataOutputStream outputStream = new DataOutputStream( |
| 934 | new FileOutputStream(fileDescriptor.getFileDescriptor()))) { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 935 | SearchResultPage searchResultPage = instance.getAppSearchImpl().query( |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 936 | packageName, |
| 937 | databaseName, |
| 938 | queryExpression, |
Alexander Dorokhine | 7cbc471 | 2021-04-27 14:47:39 -0700 | [diff] [blame] | 939 | new SearchSpec(searchSpecBundle), |
| 940 | /*logger=*/ null); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 941 | while (!searchResultPage.getResults().isEmpty()) { |
| 942 | for (int i = 0; i < searchResultPage.getResults().size(); i++) { |
| 943 | AppSearchMigrationHelper.writeBundleToOutputStream( |
| 944 | outputStream, searchResultPage.getResults().get(i) |
| 945 | .getGenericDocument().getBundle()); |
| 946 | } |
Alexander Dorokhine | ba42f39 | 2021-08-17 15:17:40 -0700 | [diff] [blame] | 947 | // TODO(b/173532925): Implement logging for statsBuilder |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 948 | searchResultPage = instance.getAppSearchImpl().getNextPage( |
Alexander Dorokhine | ba42f39 | 2021-08-17 15:17:40 -0700 | [diff] [blame] | 949 | packageName, |
| 950 | searchResultPage.getNextPageToken(), |
| 951 | /*statsBuilder=*/ null); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 952 | } |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 953 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 954 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 955 | } catch (Throwable t) { |
| 956 | invokeCallbackOnError(callback, t); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 957 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 958 | }); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | @Override |
| 962 | public void putDocumentsFromFile( |
| 963 | @NonNull String packageName, |
| 964 | @NonNull String databaseName, |
| 965 | @NonNull ParcelFileDescriptor fileDescriptor, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 966 | @NonNull UserHandle userHandle, |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 967 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 968 | Objects.requireNonNull(packageName); |
| 969 | Objects.requireNonNull(databaseName); |
| 970 | Objects.requireNonNull(fileDescriptor); |
| 971 | Objects.requireNonNull(userHandle); |
| 972 | Objects.requireNonNull(callback); |
| 973 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 974 | int callingPid = Binder.getCallingPid(); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 975 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 976 | EXECUTOR.execute(() -> { |
| 977 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 978 | verifyCaller(callingUid, packageName); |
| 979 | |
| 980 | // Obtain the user where the client wants to run the operations in. This should |
| 981 | // end up being the same as userHandle, assuming it is not a special user and |
| 982 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 983 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 984 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 985 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 986 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 987 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 988 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 989 | GenericDocument document; |
| 990 | ArrayList<Bundle> migrationFailureBundles = new ArrayList<>(); |
| 991 | try (DataInputStream inputStream = new DataInputStream( |
| 992 | new FileInputStream(fileDescriptor.getFileDescriptor()))) { |
| 993 | while (true) { |
| 994 | try { |
| 995 | document = AppSearchMigrationHelper |
| 996 | .readDocumentFromInputStream(inputStream); |
| 997 | } catch (EOFException e) { |
| 998 | // nothing wrong, we just finish the reading. |
| 999 | break; |
| 1000 | } |
| 1001 | try { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1002 | instance.getAppSearchImpl().putDocument( |
| 1003 | packageName, databaseName, document, /*logger=*/ null); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1004 | } catch (Throwable t) { |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1005 | migrationFailureBundles.add(new SetSchemaResponse.MigrationFailure( |
| 1006 | document.getNamespace(), |
| 1007 | document.getId(), |
| 1008 | document.getSchemaType(), |
| 1009 | AppSearchResult.throwableToFailedResult(t)) |
| 1010 | .getBundle()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1011 | } |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 1012 | } |
| 1013 | } |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1014 | instance.getAppSearchImpl().persistToDisk(PersistType.Code.FULL); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1015 | invokeCallbackOnResult(callback, |
| 1016 | AppSearchResult.newSuccessfulResult(migrationFailureBundles)); |
| 1017 | } catch (Throwable t) { |
| 1018 | invokeCallbackOnError(callback, t); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 1019 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1020 | }); |
Terry Wang | 623e3b0 | 2021-02-02 20:27:33 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | @Override |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 1024 | public void reportUsage( |
| 1025 | @NonNull String packageName, |
| 1026 | @NonNull String databaseName, |
| 1027 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1028 | @NonNull String documentId, |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 1029 | long usageTimeMillis, |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 1030 | boolean systemUsage, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1031 | @NonNull UserHandle userHandle, |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 1032 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1033 | Objects.requireNonNull(packageName); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 1034 | Objects.requireNonNull(databaseName); |
| 1035 | Objects.requireNonNull(namespace); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1036 | Objects.requireNonNull(documentId); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1037 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 1038 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1039 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1040 | int callingPid = Binder.getCallingPid(); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 1041 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1042 | EXECUTOR.execute(() -> { |
| 1043 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1044 | verifyCaller(callingUid, packageName); |
| 1045 | |
| 1046 | // Obtain the user where the client wants to run the operations in. This should |
| 1047 | // end up being the same as userHandle, assuming it is not a special user and |
| 1048 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1049 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1050 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1051 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1052 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1053 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 1054 | |
Alexander Dorokhine | 661ef9a | 2022-01-28 11:30:45 -0800 | [diff] [blame] | 1055 | if (systemUsage && !instance.getVisibilityChecker(). |
Terry Wang | 0be9c30 | 2022-01-21 14:36:18 -0800 | [diff] [blame] | 1056 | doesCallerHaveSystemAccess(packageName)) { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1057 | throw new AppSearchException( |
| 1058 | AppSearchResult.RESULT_SECURITY_ERROR, |
| 1059 | packageName + " does not have access to report system usage"); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1062 | instance.getAppSearchImpl().reportUsage( |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1063 | packageName, databaseName, namespace, documentId, |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1064 | usageTimeMillis, systemUsage); |
| 1065 | invokeCallbackOnResult( |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1066 | callback, AppSearchResult.newSuccessfulResult(/*value=*/ null)); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1067 | } catch (Throwable t) { |
| 1068 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 9795b51 | 2021-03-23 22:06:59 -0700 | [diff] [blame] | 1069 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1070 | }); |
Alexander Dorokhine | d18f884 | 2021-01-20 15:26:13 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | @Override |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1074 | public void removeByDocumentId( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1075 | @NonNull String packageName, |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1076 | @NonNull String databaseName, |
| 1077 | @NonNull String namespace, |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1078 | @NonNull List<String> ids, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1079 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1080 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1081 | @NonNull IAppSearchBatchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1082 | Objects.requireNonNull(packageName); |
| 1083 | Objects.requireNonNull(databaseName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1084 | Objects.requireNonNull(namespace); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1085 | Objects.requireNonNull(ids); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1086 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1087 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1088 | |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1089 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1090 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1091 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1092 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1093 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1094 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1095 | int operationSuccessCount = 0; |
| 1096 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1097 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1098 | verifyCaller(callingUid, packageName); |
| 1099 | |
| 1100 | // Obtain the user where the client wants to run the operations in. This should |
| 1101 | // end up being the same as userHandle, assuming it is not a special user and |
| 1102 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1103 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1104 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1105 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1106 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 1107 | new AppSearchBatchResult.Builder<>(); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1108 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1109 | for (int i = 0; i < ids.size(); i++) { |
| 1110 | String id = ids.get(i); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1111 | try { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1112 | instance.getAppSearchImpl().remove( |
Alexander Dorokhine | a4b5bab | 2021-05-20 14:24:50 -0700 | [diff] [blame] | 1113 | packageName, |
| 1114 | databaseName, |
| 1115 | namespace, |
| 1116 | id, |
| 1117 | /*removeStatsBuilder=*/ null); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1118 | ++operationSuccessCount; |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 1119 | resultBuilder.setSuccess(id, /*result= */ null); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1120 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1121 | AppSearchResult<Void> result = throwableToFailedResult(t); |
| 1122 | resultBuilder.setResult(id, result); |
| 1123 | // Since we can only include one status code in the atom, |
| 1124 | // for failures, we would just save the one for the last failure |
| 1125 | statusCode = result.getResultCode(); |
| 1126 | ++operationFailureCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1127 | } |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 1128 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 1129 | // Now that the batch has been written. Persist the newly written data. |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1130 | instance.getAppSearchImpl().persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1131 | invokeCallbackOnResult(callback, resultBuilder.build()); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1132 | |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1133 | // Schedule a task to dispatch change notifications. See requirements for where |
| 1134 | // the method is called documented in the method description. |
| 1135 | dispatchChangeNotifications(instance); |
| 1136 | |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1137 | checkForOptimize(instance, ids.size()); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1138 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1139 | ++operationFailureCount; |
| 1140 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1141 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1142 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1143 | if (instance != null) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1144 | int estimatedBinderLatencyMillis = |
| 1145 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 1146 | int totalLatencyMillis = |
| 1147 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1148 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1149 | .setPackageName(packageName) |
| 1150 | .setDatabase(databaseName) |
| 1151 | .setStatusCode(statusCode) |
| 1152 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1153 | .setCallType(CallStats.CALL_TYPE_REMOVE_DOCUMENTS_BY_ID) |
| 1154 | // TODO(b/173532925) check the existing binder call latency chart |
| 1155 | // is good enough for us: |
| 1156 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 1157 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 1158 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1159 | .setNumOperationsFailed(operationFailureCount) |
| 1160 | .build()); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1161 | } |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 1162 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1163 | }); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 1167 | public void removeByQuery( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1168 | @NonNull String packageName, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 1169 | @NonNull String databaseName, |
| 1170 | @NonNull String queryExpression, |
| 1171 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1172 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1173 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 1174 | @NonNull IAppSearchResultCallback callback) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1175 | // TODO(b/173532925) log CallStats once we have CALL_TYPE_REMOVE_BY_QUERY added |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1176 | Objects.requireNonNull(packageName); |
| 1177 | Objects.requireNonNull(databaseName); |
| 1178 | Objects.requireNonNull(queryExpression); |
| 1179 | Objects.requireNonNull(searchSpecBundle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1180 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1181 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1182 | |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1183 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1184 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1185 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1186 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1187 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1188 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1189 | int operationSuccessCount = 0; |
| 1190 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1191 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1192 | verifyCaller(callingUid, packageName); |
| 1193 | |
| 1194 | // Obtain the user where the client wants to run the operations in. This should |
| 1195 | // end up being the same as userHandle, assuming it is not a special user and |
| 1196 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1197 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1198 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1199 | |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1200 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1201 | instance.getAppSearchImpl().removeByQuery( |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1202 | packageName, |
| 1203 | databaseName, |
| 1204 | queryExpression, |
Alexander Dorokhine | a4b5bab | 2021-05-20 14:24:50 -0700 | [diff] [blame] | 1205 | new SearchSpec(searchSpecBundle), |
| 1206 | /*removeStatsBuilder=*/ null); |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 1207 | // Now that the batch has been written. Persist the newly written data. |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1208 | instance.getAppSearchImpl().persistToDisk(PersistType.Code.LITE); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1209 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1210 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1211 | |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1212 | // Schedule a task to dispatch change notifications. See requirements for where |
| 1213 | // the method is called documented in the method description. |
| 1214 | dispatchChangeNotifications(instance); |
| 1215 | |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1216 | checkForOptimize(instance); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1217 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1218 | ++operationFailureCount; |
| 1219 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1220 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1221 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1222 | if (instance != null) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1223 | int estimatedBinderLatencyMillis = |
| 1224 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 1225 | int totalLatencyMillis = |
| 1226 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1227 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1228 | .setPackageName(packageName) |
| 1229 | .setDatabase(databaseName) |
| 1230 | .setStatusCode(statusCode) |
| 1231 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1232 | .setCallType(CallStats.CALL_TYPE_REMOVE_DOCUMENTS_BY_SEARCH) |
| 1233 | // TODO(b/173532925) check the existing binder call latency chart |
| 1234 | // is good enough for us: |
| 1235 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 1236 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 1237 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1238 | .setNumOperationsFailed(operationFailureCount) |
| 1239 | .build()); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1240 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1241 | } |
| 1242 | }); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1245 | @Override |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1246 | public void getStorageInfo( |
| 1247 | @NonNull String packageName, |
| 1248 | @NonNull String databaseName, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1249 | @NonNull UserHandle userHandle, |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1250 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1251 | Objects.requireNonNull(packageName); |
| 1252 | Objects.requireNonNull(databaseName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1253 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1254 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1255 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1256 | int callingPid = Binder.getCallingPid(); |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1257 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1258 | EXECUTOR.execute(() -> { |
| 1259 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1260 | verifyCaller(callingUid, packageName); |
| 1261 | |
| 1262 | // Obtain the user where the client wants to run the operations in. This should |
| 1263 | // end up being the same as userHandle, assuming it is not a special user and |
| 1264 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1265 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1266 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1267 | |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1268 | AppSearchUserInstance instance = |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1269 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1270 | StorageInfo storageInfo = instance.getAppSearchImpl() |
| 1271 | .getStorageInfoForDatabase(packageName, databaseName); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1272 | Bundle storageInfoBundle = storageInfo.getBundle(); |
| 1273 | invokeCallbackOnResult( |
| 1274 | callback, AppSearchResult.newSuccessfulResult(storageInfoBundle)); |
| 1275 | } catch (Throwable t) { |
| 1276 | invokeCallbackOnError(callback, t); |
| 1277 | } |
| 1278 | }); |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | @Override |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1282 | public void persistToDisk( |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1283 | @NonNull String packageName, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1284 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1285 | @ElapsedRealtimeLong long binderCallStartTimeMillis) { |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1286 | Objects.requireNonNull(packageName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1287 | Objects.requireNonNull(userHandle); |
| 1288 | |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1289 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1290 | int callingPid = Binder.getCallingPid(); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 1291 | int callingUid = Binder.getCallingUid(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1292 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1293 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1294 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1295 | int operationSuccessCount = 0; |
| 1296 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1297 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1298 | verifyCaller(callingUid, packageName); |
| 1299 | |
| 1300 | // Obtain the user where the client wants to run the operations in. This should |
| 1301 | // end up being the same as userHandle, assuming it is not a special user and |
| 1302 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1303 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1304 | verifyUserUnlocked(targetUser); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1305 | |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1306 | instance = mAppSearchUserInstanceManager.getUserInstance(targetUser); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1307 | instance.getAppSearchImpl().persistToDisk(PersistType.Code.FULL); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1308 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1309 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1310 | ++operationFailureCount; |
| 1311 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1312 | Log.e(TAG, "Unable to persist the data to disk", t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1313 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1314 | if (instance != null) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1315 | int estimatedBinderLatencyMillis = |
| 1316 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 1317 | int totalLatencyMillis = |
| 1318 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1319 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1320 | .setStatusCode(statusCode) |
| 1321 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1322 | .setCallType(CallStats.CALL_TYPE_FLUSH) |
| 1323 | // TODO(b/173532925) check the existing binder call latency chart |
| 1324 | // is good enough for us: |
| 1325 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 1326 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 1327 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1328 | .setNumOperationsFailed(operationFailureCount) |
| 1329 | .build()); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1330 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1331 | } |
| 1332 | }); |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | @Override |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1336 | public AppSearchResultParcel<Void> addObserver( |
| 1337 | @NonNull String callingPackage, |
Alexander Dorokhine | 661ef9a | 2022-01-28 11:30:45 -0800 | [diff] [blame] | 1338 | @NonNull String targetPackageName, |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1339 | @NonNull Bundle observerSpecBundle, |
| 1340 | @NonNull UserHandle userHandle, |
| 1341 | @NonNull IAppSearchObserverProxy observerProxyStub) { |
| 1342 | Objects.requireNonNull(callingPackage); |
Alexander Dorokhine | 661ef9a | 2022-01-28 11:30:45 -0800 | [diff] [blame] | 1343 | Objects.requireNonNull(targetPackageName); |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1344 | Objects.requireNonNull(observerSpecBundle); |
| 1345 | Objects.requireNonNull(userHandle); |
| 1346 | Objects.requireNonNull(observerProxyStub); |
| 1347 | |
| 1348 | int callingPid = Binder.getCallingPid(); |
| 1349 | int callingUid = Binder.getCallingUid(); |
| 1350 | long callingIdentity = Binder.clearCallingIdentity(); |
| 1351 | |
| 1352 | // Note: addObserver is performed on the binder thread, unlike most AppSearch APIs |
| 1353 | try { |
| 1354 | verifyCaller(callingUid, callingPackage); |
| 1355 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
| 1356 | verifyUserUnlocked(targetUser); |
| 1357 | |
| 1358 | AppSearchUserInstance instance = |
| 1359 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
| 1360 | instance.getAppSearchImpl().addObserver( |
Alexander Dorokhine | 655aec0 | 2022-02-07 17:24:01 -0800 | [diff] [blame] | 1361 | new FrameworkCallerAccess( |
Alexander Dorokhine | 72f5e37 | 2022-02-07 17:23:49 -0800 | [diff] [blame] | 1362 | callingPackage, |
| 1363 | callingUid, |
| 1364 | instance.getVisibilityChecker() |
| 1365 | .doesCallerHaveSystemAccess(callingPackage)), |
Alexander Dorokhine | 661ef9a | 2022-01-28 11:30:45 -0800 | [diff] [blame] | 1366 | targetPackageName, |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1367 | new ObserverSpec(observerSpecBundle), |
| 1368 | EXECUTOR, |
| 1369 | new AppSearchObserverProxy(observerProxyStub)); |
| 1370 | return new AppSearchResultParcel<>(AppSearchResult.newSuccessfulResult(null)); |
| 1371 | } catch (Throwable t) { |
| 1372 | return new AppSearchResultParcel<>(throwableToFailedResult(t)); |
| 1373 | } finally { |
| 1374 | Binder.restoreCallingIdentity(callingIdentity); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | @Override |
Alexander Dorokhine | 04f94d2 | 2022-01-24 23:38:50 -0800 | [diff] [blame] | 1379 | public AppSearchResultParcel<Void> removeObserver( |
| 1380 | @NonNull String callingPackage, |
| 1381 | @NonNull String observedPackage, |
| 1382 | @NonNull UserHandle userHandle, |
| 1383 | @NonNull IAppSearchObserverProxy observerProxyStub) { |
| 1384 | Objects.requireNonNull(callingPackage); |
| 1385 | Objects.requireNonNull(observedPackage); |
| 1386 | Objects.requireNonNull(userHandle); |
| 1387 | Objects.requireNonNull(observerProxyStub); |
| 1388 | |
| 1389 | int callingPid = Binder.getCallingPid(); |
| 1390 | int callingUid = Binder.getCallingUid(); |
| 1391 | long callingIdentity = Binder.clearCallingIdentity(); |
| 1392 | |
| 1393 | // Note: removeObserver is performed on the binder thread, unlike most AppSearch APIs |
| 1394 | try { |
| 1395 | verifyCaller(callingUid, callingPackage); |
| 1396 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
| 1397 | verifyUserUnlocked(targetUser); |
| 1398 | |
| 1399 | AppSearchUserInstance instance = |
| 1400 | mAppSearchUserInstanceManager.getUserInstance(targetUser); |
| 1401 | instance.getAppSearchImpl().removeObserver( |
| 1402 | observedPackage, |
| 1403 | new AppSearchObserverProxy(observerProxyStub)); |
| 1404 | return new AppSearchResultParcel<>(AppSearchResult.newSuccessfulResult(null)); |
| 1405 | } catch (Throwable t) { |
| 1406 | return new AppSearchResultParcel<>(throwableToFailedResult(t)); |
| 1407 | } finally { |
| 1408 | Binder.restoreCallingIdentity(callingIdentity); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | @Override |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1413 | public void initialize( |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1414 | @NonNull String packageName, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1415 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1416 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
| 1417 | @NonNull IAppSearchResultCallback callback) { |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1418 | Objects.requireNonNull(packageName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1419 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1420 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1421 | |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1422 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1423 | int callingPid = Binder.getCallingPid(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1424 | int callingUid = Binder.getCallingUid(); |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1425 | |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1426 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1427 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1428 | AppSearchUserInstance instance = null; |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1429 | int operationSuccessCount = 0; |
| 1430 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1431 | try { |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1432 | verifyCaller(callingUid, packageName); |
| 1433 | |
| 1434 | // Obtain the user where the client wants to run the operations in. This should |
| 1435 | // end up being the same as userHandle, assuming it is not a special user and |
| 1436 | // the client is allowed to run operations in that user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1437 | UserHandle targetUser = handleIncomingUser(userHandle, callingPid, callingUid); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1438 | verifyUserUnlocked(targetUser); |
| 1439 | |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1440 | Context targetUserContext = mContext.createContextAsUser(targetUser, |
| 1441 | /*flags=*/ 0); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1442 | instance = mAppSearchUserInstanceManager.getOrCreateUserInstance( |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1443 | targetUserContext, targetUser, AppSearchConfig.getInstance(EXECUTOR)); |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1444 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1445 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 1446 | } catch (Throwable t) { |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1447 | ++operationFailureCount; |
| 1448 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1449 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1450 | } finally { |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1451 | if (instance != null) { |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1452 | int estimatedBinderLatencyMillis = |
| 1453 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 1454 | int totalLatencyMillis = |
| 1455 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1456 | instance.getLogger().logStats(new CallStats.Builder() |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1457 | .setStatusCode(statusCode) |
| 1458 | .setTotalLatencyMillis(totalLatencyMillis) |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1459 | .setCallType(CallStats.CALL_TYPE_INITIALIZE) |
| 1460 | // TODO(b/173532925) check the existing binder call latency chart |
| 1461 | // is good enough for us: |
| 1462 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 1463 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 1464 | .setNumOperationsSucceeded(operationSuccessCount) |
Alexander Dorokhine | 82a3872 | 2021-06-09 16:35:22 -0700 | [diff] [blame] | 1465 | .setNumOperationsFailed(operationFailureCount) |
| 1466 | .build()); |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1467 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1468 | } |
| 1469 | }); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1472 | /** Invokes the {@link IAppSearchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1473 | private void invokeCallbackOnResult( |
| 1474 | IAppSearchResultCallback callback, AppSearchResult<?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1475 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1476 | callback.onResult(new AppSearchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1477 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1478 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1479 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1480 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1481 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1482 | /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1483 | private void invokeCallbackOnResult( |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1484 | IAppSearchBatchResultCallback callback, AppSearchBatchResult<String, ?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1485 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1486 | callback.onResult(new AppSearchBatchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1487 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1488 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1489 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1493 | * Invokes the {@link IAppSearchResultCallback} with an throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1494 | * |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1495 | * <p>The throwable is convert to a {@link AppSearchResult}; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1496 | */ |
| 1497 | private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1498 | AppSearchResult<?> result = throwableToFailedResult(throwable); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1499 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1500 | callback.onResult(new AppSearchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1501 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1502 | Log.e(TAG, "Unable to send result to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1507 | * Invokes the {@link IAppSearchBatchResultCallback} with an unexpected internal throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1508 | * |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 1509 | * <p>The throwable is converted to {@link AppSearchResult}. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1510 | */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1511 | private void invokeCallbackOnError( |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 1512 | @NonNull IAppSearchBatchResultCallback callback, @NonNull Throwable throwable) { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1513 | AppSearchResult<?> result = throwableToFailedResult(throwable); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1514 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1515 | callback.onSystemError(new AppSearchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1516 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1517 | Log.e(TAG, "Unable to send error to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1518 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1519 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1520 | } |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1521 | |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1522 | /** |
| 1523 | * Helper for dealing with incoming user arguments to system service calls. |
| 1524 | * |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1525 | * <p>Takes care of checking permissions and if the target is special user, this method will |
| 1526 | * simply throw. |
| 1527 | * |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1528 | * @param targetUserHandle The user which the caller is requesting to execute as. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1529 | * @param callingPid The actual pid of the caller as determined by Binder. |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 1530 | * @param callingUid The actual uid of the caller as determined by Binder. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1531 | * |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1532 | * @return the user handle that the call should run as. Will always be a concrete user. |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1533 | * |
| 1534 | * @throws IllegalArgumentException if the target user is a special user. |
| 1535 | * @throws SecurityException if caller trying to interact across user without |
| 1536 | * {@link Manifest.permission#INTERACT_ACROSS_USERS_FULL} |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1537 | */ |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1538 | @NonNull |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1539 | private UserHandle handleIncomingUser(@NonNull UserHandle targetUserHandle, int callingPid, |
| 1540 | int callingUid) { |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1541 | UserHandle callingUserHandle = UserHandle.getUserHandleForUid(callingUid); |
| 1542 | if (callingUserHandle.equals(targetUserHandle)) { |
| 1543 | return targetUserHandle; |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 1544 | } |
Cassie Wang | 4bff4ff | 2021-07-16 13:03:19 -0700 | [diff] [blame] | 1545 | |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 1546 | // Duplicates UserController#ensureNotSpecialUser |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1547 | if (targetUserHandle.getIdentifier() < 0) { |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 1548 | throw new IllegalArgumentException( |
Cassie Wang | 2feb34d | 2021-07-22 18:55:19 +0000 | [diff] [blame] | 1549 | "Call does not support special user " + targetUserHandle); |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 1550 | } |
Cassie Wang | 4bff4ff | 2021-07-16 13:03:19 -0700 | [diff] [blame] | 1551 | |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1552 | if (mContext.checkPermission( |
| 1553 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1554 | callingPid, |
| 1555 | callingUid) == PackageManager.PERMISSION_GRANTED) { |
| 1556 | return targetUserHandle; |
| 1557 | } |
Alexander Dorokhine | 5c41677 | 2021-06-04 09:05:00 -0700 | [diff] [blame] | 1558 | throw new SecurityException( |
Cassie Wang | 2e2d055 | 2021-08-04 16:18:10 +0000 | [diff] [blame] | 1559 | "Permission denied while calling from uid " + callingUid |
| 1560 | + " with " + targetUserHandle + "; Requires permission: " |
| 1561 | + Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1562 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1563 | |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1564 | /** |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1565 | * Verify various aspects of the calling user. |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1566 | * |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1567 | * @param callingUid Uid of the caller, usually retrieved from Binder for authenticity. |
| 1568 | * @param claimedCallingPackage Package name the caller claims to be. |
| 1569 | */ |
| 1570 | private void verifyCaller(int callingUid, @NonNull String claimedCallingPackage) { |
| 1571 | // Obtain the user where the client is running in. Note that this could be different from |
| 1572 | // the userHandle where the client wants to run the AppSearch operation in. |
| 1573 | UserHandle callingUserHandle = UserHandle.getUserHandleForUid(callingUid); |
| 1574 | Context callingUserContext = mContext.createContextAsUser(callingUserHandle, |
| 1575 | /*flags=*/ 0); |
| 1576 | |
| 1577 | verifyCallingPackage(callingUserContext, callingUid, claimedCallingPackage); |
| 1578 | verifyNotInstantApp(callingUserContext, claimedCallingPackage); |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * Check that the caller's supposed package name matches the uid making the call. |
| 1583 | * |
| 1584 | * @throws SecurityException if the package name and uid don't match. |
| 1585 | */ |
| 1586 | private void verifyCallingPackage( |
| 1587 | @NonNull Context actualCallingUserContext, |
| 1588 | int actualCallingUid, |
| 1589 | @NonNull String claimedCallingPackage) { |
| 1590 | int claimedCallingUid = PackageUtil.getPackageUid( |
| 1591 | actualCallingUserContext, claimedCallingPackage); |
Cassie Wang | d51ed16 | 2021-07-29 22:22:30 +0000 | [diff] [blame] | 1592 | if (claimedCallingUid != actualCallingUid) { |
| 1593 | throw new SecurityException( |
| 1594 | "Specified calling package [" |
| 1595 | + claimedCallingPackage |
| 1596 | + "] does not match the calling uid " |
| 1597 | + actualCallingUid); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | /** |
| 1602 | * Ensure instant apps can't make calls to AppSearch. |
| 1603 | * |
Cassie Wang | 84e33a0 | 2021-06-18 14:13:31 -0700 | [diff] [blame] | 1604 | * @throws SecurityException if the caller is an instant app. |
| 1605 | */ |
| 1606 | private void verifyNotInstantApp(@NonNull Context userContext, @NonNull String packageName) { |
| 1607 | PackageManager callingPackageManager = userContext.getPackageManager(); |
| 1608 | if (callingPackageManager.isInstantApp(packageName)) { |
| 1609 | throw new SecurityException("Caller not allowed to create AppSearch session" |
| 1610 | + "; userHandle=" + userContext.getUser() + ", callingPackage=" + packageName); |
| 1611 | } |
| 1612 | } |
| 1613 | |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1614 | private class AppSearchStorageStatsAugmenter implements StorageStatsAugmenter { |
| 1615 | @Override |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1616 | public void augmentStatsForPackageForUser( |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1617 | @NonNull PackageStats stats, |
| 1618 | @NonNull String packageName, |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1619 | @NonNull UserHandle userHandle, |
| 1620 | boolean canCallerAccessAllStats) { |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1621 | Objects.requireNonNull(stats); |
| 1622 | Objects.requireNonNull(packageName); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1623 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1624 | |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1625 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1626 | verifyUserUnlocked(userHandle); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1627 | AppSearchUserInstance instance = |
Yang Yu | c047f7b | 2021-06-25 12:22:42 -0700 | [diff] [blame] | 1628 | mAppSearchUserInstanceManager.getUserInstanceOrNull(userHandle); |
| 1629 | if (instance == null) { |
| 1630 | // augment storage info from file |
| 1631 | UserStorageInfo userStorageInfo = |
| 1632 | mAppSearchUserInstanceManager.getOrCreateUserStorageInfoInstance( |
| 1633 | userHandle); |
| 1634 | stats.dataSize += |
| 1635 | userStorageInfo.getSizeBytesForPackage(packageName); |
| 1636 | } else { |
| 1637 | stats.dataSize += instance.getAppSearchImpl() |
| 1638 | .getStorageInfoForPackage(packageName).getSizeBytes(); |
| 1639 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1640 | } catch (Throwable t) { |
| 1641 | Log.e( |
| 1642 | TAG, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1643 | "Unable to augment storage stats for " |
| 1644 | + userHandle |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1645 | + " packageName " |
| 1646 | + packageName, |
| 1647 | t); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | @Override |
| 1652 | public void augmentStatsForUid( |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1653 | @NonNull PackageStats stats, int uid, boolean canCallerAccessAllStats) { |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1654 | Objects.requireNonNull(stats); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1655 | |
| 1656 | UserHandle userHandle = UserHandle.getUserHandleForUid(uid); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1657 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1658 | verifyUserUnlocked(userHandle); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1659 | String[] packagesForUid = mPackageManager.getPackagesForUid(uid); |
| 1660 | if (packagesForUid == null) { |
| 1661 | return; |
| 1662 | } |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1663 | AppSearchUserInstance instance = |
Yang Yu | c047f7b | 2021-06-25 12:22:42 -0700 | [diff] [blame] | 1664 | mAppSearchUserInstanceManager.getUserInstanceOrNull(userHandle); |
| 1665 | if (instance == null) { |
| 1666 | // augment storage info from file |
| 1667 | UserStorageInfo userStorageInfo = |
| 1668 | mAppSearchUserInstanceManager.getOrCreateUserStorageInfoInstance( |
| 1669 | userHandle); |
| 1670 | for (int i = 0; i < packagesForUid.length; i++) { |
| 1671 | stats.dataSize += userStorageInfo.getSizeBytesForPackage( |
| 1672 | packagesForUid[i]); |
| 1673 | } |
| 1674 | } else { |
| 1675 | for (int i = 0; i < packagesForUid.length; i++) { |
| 1676 | stats.dataSize += instance.getAppSearchImpl() |
| 1677 | .getStorageInfoForPackage(packagesForUid[i]).getSizeBytes(); |
| 1678 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1679 | } |
| 1680 | } catch (Throwable t) { |
| 1681 | Log.e(TAG, "Unable to augment storage stats for uid " + uid, t); |
| 1682 | } |
| 1683 | } |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1684 | |
| 1685 | @Override |
| 1686 | public void augmentStatsForUser( |
| 1687 | @NonNull PackageStats stats, @NonNull UserHandle userHandle) { |
| 1688 | // TODO(b/179160886): this implementation could incur many jni calls and a lot of |
| 1689 | // in-memory processing from getStorageInfoForPackage. Instead, we can just compute the |
| 1690 | // size of the icing dir (or use the overall StorageInfo without interpolating it). |
| 1691 | Objects.requireNonNull(stats); |
| 1692 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1693 | |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1694 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1695 | verifyUserUnlocked(userHandle); |
Alexander Dorokhine | 4d05163 | 2021-06-18 10:48:58 -0700 | [diff] [blame] | 1696 | AppSearchUserInstance instance = |
Yang Yu | c047f7b | 2021-06-25 12:22:42 -0700 | [diff] [blame] | 1697 | mAppSearchUserInstanceManager.getUserInstanceOrNull(userHandle); |
| 1698 | if (instance == null) { |
| 1699 | // augment storage info from file |
| 1700 | UserStorageInfo userStorageInfo = |
| 1701 | mAppSearchUserInstanceManager.getOrCreateUserStorageInfoInstance( |
| 1702 | userHandle); |
| 1703 | stats.dataSize += userStorageInfo.getTotalSizeBytes(); |
| 1704 | } else { |
| 1705 | List<PackageInfo> packagesForUser = mPackageManager.getInstalledPackagesAsUser( |
| 1706 | /*flags=*/0, userHandle.getIdentifier()); |
| 1707 | if (packagesForUser != null) { |
| 1708 | for (int i = 0; i < packagesForUser.size(); i++) { |
| 1709 | String packageName = packagesForUser.get(i).packageName; |
| 1710 | stats.dataSize += instance.getAppSearchImpl() |
| 1711 | .getStorageInfoForPackage(packageName).getSizeBytes(); |
| 1712 | } |
| 1713 | } |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1714 | } |
| 1715 | } catch (Throwable t) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame] | 1716 | Log.e(TAG, "Unable to augment storage stats for " + userHandle, t); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1717 | } |
| 1718 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1719 | } |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1720 | |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1721 | /** |
| 1722 | * Dispatches change notifications if there are any to dispatch. |
| 1723 | * |
| 1724 | * <p>This method is async; notifications are dispatched onto their own registered executors. |
| 1725 | * |
| 1726 | * <p>IMPORTANT: You must always call this within the background task that contains the |
| 1727 | * operation that mutated the index. If you called it outside of that task, it could start |
| 1728 | * before the task completes, causing notifications to be missed. |
| 1729 | */ |
| 1730 | @WorkerThread |
| 1731 | private void dispatchChangeNotifications(@NonNull AppSearchUserInstance instance) { |
| 1732 | instance.getAppSearchImpl().dispatchAndClearChangeNotifications(); |
| 1733 | } |
| 1734 | |
| 1735 | @WorkerThread |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1736 | private void checkForOptimize(AppSearchUserInstance instance, int mutateBatchSize) { |
| 1737 | EXECUTOR.execute(() -> { |
Alexander Dorokhine | 14816e2 | 2021-07-12 10:53:24 -0700 | [diff] [blame] | 1738 | long totalLatencyStartMillis = SystemClock.elapsedRealtime(); |
| 1739 | OptimizeStats.Builder builder = new OptimizeStats.Builder(); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1740 | try { |
Alexander Dorokhine | 14816e2 | 2021-07-12 10:53:24 -0700 | [diff] [blame] | 1741 | instance.getAppSearchImpl().checkForOptimize(mutateBatchSize, builder); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1742 | } catch (AppSearchException e) { |
| 1743 | Log.w(TAG, "Error occurred when check for optimize", e); |
Alexander Dorokhine | 14816e2 | 2021-07-12 10:53:24 -0700 | [diff] [blame] | 1744 | } finally { |
| 1745 | OptimizeStats oStats = builder |
| 1746 | .setTotalLatencyMillis( |
| 1747 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartMillis)) |
| 1748 | .build(); |
| 1749 | if (oStats.getOriginalDocumentCount() > 0) { |
| 1750 | // see if optimize has been run by checking originalDocumentCount |
| 1751 | instance.getLogger().logStats(oStats); |
| 1752 | } |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1753 | } |
| 1754 | }); |
| 1755 | } |
| 1756 | |
Alexander Dorokhine | e0587d3 | 2021-12-07 17:02:17 -0800 | [diff] [blame] | 1757 | @WorkerThread |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1758 | private void checkForOptimize(AppSearchUserInstance instance) { |
| 1759 | EXECUTOR.execute(() -> { |
Alexander Dorokhine | 14816e2 | 2021-07-12 10:53:24 -0700 | [diff] [blame] | 1760 | long totalLatencyStartMillis = SystemClock.elapsedRealtime(); |
| 1761 | OptimizeStats.Builder builder = new OptimizeStats.Builder(); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1762 | try { |
Alexander Dorokhine | 14816e2 | 2021-07-12 10:53:24 -0700 | [diff] [blame] | 1763 | instance.getAppSearchImpl().checkForOptimize(builder); |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1764 | } catch (AppSearchException e) { |
| 1765 | Log.w(TAG, "Error occurred when check for optimize", e); |
Alexander Dorokhine | 14816e2 | 2021-07-12 10:53:24 -0700 | [diff] [blame] | 1766 | } finally { |
| 1767 | OptimizeStats oStats = builder |
| 1768 | .setTotalLatencyMillis( |
| 1769 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartMillis)) |
| 1770 | .build(); |
| 1771 | if (oStats.getOriginalDocumentCount() > 0) { |
| 1772 | // see if optimize has been run by checking originalDocumentCount |
| 1773 | instance.getLogger().logStats(oStats); |
| 1774 | } |
Terry Wang | f4d219b | 2021-07-01 19:11:39 -0700 | [diff] [blame] | 1775 | } |
| 1776 | }); |
| 1777 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1778 | } |