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