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