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