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