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