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 | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 933 | impl.remove(packageName, databaseName, namespace, id); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 934 | ++operationSuccessCount; |
Alexander Dorokhine | 17f7937 | 2021-04-21 11:23:09 -0700 | [diff] [blame] | 935 | resultBuilder.setSuccess(id, /*result= */ null); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 936 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 937 | AppSearchResult<Void> result = throwableToFailedResult(t); |
| 938 | resultBuilder.setResult(id, result); |
| 939 | // Since we can only include one status code in the atom, |
| 940 | // for failures, we would just save the one for the last failure |
| 941 | statusCode = result.getResultCode(); |
| 942 | ++operationFailureCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 943 | } |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 944 | } |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 945 | // Now that the batch has been written. Persist the newly written data. |
| 946 | impl.persistToDisk(PersistType.Code.LITE); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 947 | invokeCallbackOnResult(callback, resultBuilder.build()); |
| 948 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 949 | ++operationFailureCount; |
| 950 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 951 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 952 | } finally { |
| 953 | if (logger != null) { |
| 954 | int estimatedBinderLatencyMillis = |
| 955 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 956 | int totalLatencyMillis = |
| 957 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
| 958 | CallStats.Builder cBuilder = new CallStats.Builder(packageName, |
| 959 | databaseName) |
| 960 | .setCallType(CallStats.CALL_TYPE_REMOVE_DOCUMENTS_BY_ID) |
| 961 | // TODO(b/173532925) check the existing binder call latency chart |
| 962 | // is good enough for us: |
| 963 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 964 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 965 | .setNumOperationsSucceeded(operationSuccessCount) |
| 966 | .setNumOperationsFailed(operationFailureCount); |
| 967 | cBuilder.getGeneralStatsBuilder() |
| 968 | .setStatusCode(statusCode) |
| 969 | .setTotalLatencyMillis(totalLatencyMillis); |
| 970 | logger.logStats(cBuilder.build()); |
| 971 | } |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 972 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 973 | }); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 977 | public void removeByQuery( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 978 | @NonNull String packageName, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 979 | @NonNull String databaseName, |
| 980 | @NonNull String queryExpression, |
| 981 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 982 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 983 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 984 | @NonNull IAppSearchResultCallback callback) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 985 | // 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] | 986 | Objects.requireNonNull(packageName); |
| 987 | Objects.requireNonNull(databaseName); |
| 988 | Objects.requireNonNull(queryExpression); |
| 989 | Objects.requireNonNull(searchSpecBundle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 990 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 991 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 992 | |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 993 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 994 | int callingUid = Binder.getCallingUid(); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 995 | UserHandle callingUser = handleIncomingUser(userHandle, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 996 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 997 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
| 998 | PlatformLogger logger = null; |
| 999 | int operationSuccessCount = 0; |
| 1000 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1001 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1002 | verifyUserUnlocked(callingUser); |
| 1003 | verifyCallingPackage(callingUser, callingUid, packageName); |
| 1004 | AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(callingUser); |
| 1005 | logger = mLoggerInstanceManager.getPlatformLogger(callingUser); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1006 | impl.removeByQuery( |
| 1007 | packageName, |
| 1008 | databaseName, |
| 1009 | queryExpression, |
| 1010 | new SearchSpec(searchSpecBundle)); |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 1011 | // Now that the batch has been written. Persist the newly written data. |
| 1012 | impl.persistToDisk(PersistType.Code.LITE); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1013 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1014 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 1015 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1016 | ++operationFailureCount; |
| 1017 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1018 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1019 | } finally { |
| 1020 | if (logger != null) { |
| 1021 | int estimatedBinderLatencyMillis = |
| 1022 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 1023 | int totalLatencyMillis = |
| 1024 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
| 1025 | CallStats.Builder cBuilder = new CallStats.Builder(packageName, |
| 1026 | databaseName) |
| 1027 | .setCallType(CallStats.CALL_TYPE_REMOVE_DOCUMENTS_BY_SEARCH) |
| 1028 | // TODO(b/173532925) check the existing binder call latency chart |
| 1029 | // is good enough for us: |
| 1030 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 1031 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 1032 | .setNumOperationsSucceeded(operationSuccessCount) |
| 1033 | .setNumOperationsFailed(operationFailureCount); |
| 1034 | cBuilder.getGeneralStatsBuilder() |
| 1035 | .setStatusCode(statusCode) |
| 1036 | .setTotalLatencyMillis(totalLatencyMillis); |
| 1037 | logger.logStats(cBuilder.build()); |
| 1038 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1039 | } |
| 1040 | }); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1043 | @Override |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1044 | public void getStorageInfo( |
| 1045 | @NonNull String packageName, |
| 1046 | @NonNull String databaseName, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1047 | @NonNull UserHandle userHandle, |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1048 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1049 | Objects.requireNonNull(packageName); |
| 1050 | Objects.requireNonNull(databaseName); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1051 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1052 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1053 | |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1054 | int callingUid = Binder.getCallingUid(); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1055 | UserHandle callingUser = handleIncomingUser(userHandle, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1056 | EXECUTOR.execute(() -> { |
| 1057 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1058 | verifyUserUnlocked(callingUser); |
| 1059 | verifyCallingPackage(callingUser, callingUid, packageName); |
| 1060 | AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(callingUser); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1061 | StorageInfo storageInfo = impl.getStorageInfoForDatabase(packageName, |
| 1062 | databaseName); |
| 1063 | Bundle storageInfoBundle = storageInfo.getBundle(); |
| 1064 | invokeCallbackOnResult( |
| 1065 | callback, AppSearchResult.newSuccessfulResult(storageInfoBundle)); |
| 1066 | } catch (Throwable t) { |
| 1067 | invokeCallbackOnError(callback, t); |
| 1068 | } |
| 1069 | }); |
Cassie Wang | 8f0df49 | 2021-03-24 09:23:18 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | @Override |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1073 | public void persistToDisk( |
| 1074 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1075 | @ElapsedRealtimeLong long binderCallStartTimeMillis) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1076 | Objects.requireNonNull(userHandle); |
| 1077 | |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1078 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Cassie Wang | b0d6012 | 2021-03-30 12:38:46 -0700 | [diff] [blame] | 1079 | int callingUid = Binder.getCallingUid(); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1080 | UserHandle callingUser = handleIncomingUser(userHandle, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1081 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1082 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
| 1083 | PlatformLogger logger = null; |
| 1084 | int operationSuccessCount = 0; |
| 1085 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1086 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1087 | verifyUserUnlocked(callingUser); |
| 1088 | AppSearchImpl impl = mImplInstanceManager.getAppSearchImpl(callingUser); |
| 1089 | logger = mLoggerInstanceManager.getPlatformLogger(callingUser); |
Alexander Dorokhine | c77f444 | 2021-04-14 09:26:06 -0700 | [diff] [blame] | 1090 | impl.persistToDisk(PersistType.Code.FULL); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1091 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1092 | } catch (Throwable t) { |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1093 | ++operationFailureCount; |
| 1094 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1095 | Log.e(TAG, "Unable to persist the data to disk", t); |
Xiaoyu Jin | c5a7577 | 2021-05-09 20:12:44 -0700 | [diff] [blame] | 1096 | } finally { |
| 1097 | if (logger != null) { |
| 1098 | int estimatedBinderLatencyMillis = |
| 1099 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 1100 | int totalLatencyMillis = |
| 1101 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
| 1102 | CallStats.Builder cBuilder = new CallStats.Builder(/*packageName=*/ "", |
| 1103 | /*databaseName=*/ "") |
| 1104 | .setCallType(CallStats.CALL_TYPE_FLUSH) |
| 1105 | // TODO(b/173532925) check the existing binder call latency chart |
| 1106 | // is good enough for us: |
| 1107 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 1108 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 1109 | .setNumOperationsSucceeded(operationSuccessCount) |
| 1110 | .setNumOperationsFailed(operationFailureCount); |
| 1111 | cBuilder.getGeneralStatsBuilder() |
| 1112 | .setStatusCode(statusCode) |
| 1113 | .setTotalLatencyMillis(totalLatencyMillis); |
| 1114 | logger.logStats(cBuilder.build()); |
| 1115 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1116 | } |
| 1117 | }); |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | @Override |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1121 | public void initialize( |
| 1122 | @NonNull UserHandle userHandle, |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1123 | @ElapsedRealtimeLong long binderCallStartTimeMillis, |
| 1124 | @NonNull IAppSearchResultCallback callback) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1125 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | 5258ad2 | 2021-04-15 10:59:55 -0700 | [diff] [blame] | 1126 | Objects.requireNonNull(callback); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1127 | |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1128 | long totalLatencyStartTimeMillis = SystemClock.elapsedRealtime(); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1129 | int callingUid = Binder.getCallingUid(); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1130 | UserHandle callingUser = handleIncomingUser(userHandle, callingUid); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1131 | EXECUTOR.execute(() -> { |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1132 | @AppSearchResult.ResultCode int statusCode = AppSearchResult.RESULT_OK; |
| 1133 | PlatformLogger logger = null; |
| 1134 | int operationSuccessCount = 0; |
| 1135 | int operationFailureCount = 0; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1136 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1137 | verifyUserUnlocked(callingUser); |
| 1138 | logger = mLoggerInstanceManager.getOrCreatePlatformLogger( |
| 1139 | mContext, callingUser); |
| 1140 | mImplInstanceManager.getOrCreateAppSearchImpl(mContext, callingUser, logger); |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1141 | ++operationSuccessCount; |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1142 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 1143 | } catch (Throwable t) { |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1144 | ++operationFailureCount; |
| 1145 | statusCode = throwableToFailedResult(t).getResultCode(); |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1146 | invokeCallbackOnError(callback, t); |
Xiaoyu Jin | c410c4f | 2021-05-09 14:15:50 -0700 | [diff] [blame] | 1147 | } finally { |
| 1148 | if (logger != null) { |
| 1149 | int estimatedBinderLatencyMillis = |
| 1150 | 2 * (int) (totalLatencyStartTimeMillis - binderCallStartTimeMillis); |
| 1151 | int totalLatencyMillis = |
| 1152 | (int) (SystemClock.elapsedRealtime() - totalLatencyStartTimeMillis); |
| 1153 | // TODO(b/173532925) make packageName and database nullable after |
| 1154 | // removing generalStats |
| 1155 | CallStats.Builder cBuilder = new CallStats.Builder(/*packageName=*/"", |
| 1156 | /*database=*/ "") |
| 1157 | .setCallType(CallStats.CALL_TYPE_INITIALIZE) |
| 1158 | // TODO(b/173532925) check the existing binder call latency chart |
| 1159 | // is good enough for us: |
| 1160 | // http://dashboards/view/_72c98f9a_91d9_41d4_ab9a_bc14f79742b4 |
| 1161 | .setEstimatedBinderLatencyMillis(estimatedBinderLatencyMillis) |
| 1162 | .setNumOperationsSucceeded(operationSuccessCount) |
| 1163 | .setNumOperationsFailed(operationFailureCount); |
| 1164 | cBuilder.getGeneralStatsBuilder() |
| 1165 | .setStatusCode(statusCode) |
| 1166 | .setTotalLatencyMillis(totalLatencyMillis); |
| 1167 | logger.logStats(cBuilder.build()); |
| 1168 | } |
Terry Wang | e04ceab | 2021-03-29 19:25:12 -0700 | [diff] [blame] | 1169 | } |
| 1170 | }); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1173 | private void verifyCallingPackage( |
| 1174 | @NonNull UserHandle actualCallingUser, |
| 1175 | int actualCallingUid, |
| 1176 | @NonNull String claimedCallingPackage) { |
| 1177 | Objects.requireNonNull(actualCallingUser); |
| 1178 | Objects.requireNonNull(claimedCallingPackage); |
| 1179 | |
| 1180 | int claimedCallingUid; |
| 1181 | try { |
| 1182 | Context claimedCallingContext = |
| 1183 | mContext.createContextAsUser(actualCallingUser, /*flags=*/ 0); |
| 1184 | claimedCallingUid = claimedCallingContext.getPackageManager().getPackageUid( |
| 1185 | claimedCallingPackage, /*flags=*/ 0); |
| 1186 | } catch (PackageManager.NameNotFoundException e) { |
| 1187 | throw new SecurityException( |
| 1188 | "Specified calling package [" + claimedCallingPackage + "] not found"); |
| 1189 | } |
| 1190 | if (claimedCallingUid != actualCallingUid) { |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1191 | throw new SecurityException( |
| 1192 | "Specified calling package [" |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1193 | + claimedCallingPackage |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1194 | + "] does not match the calling uid " |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1195 | + actualCallingUid); |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 1196 | } |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1199 | /** Invokes the {@link IAppSearchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1200 | private void invokeCallbackOnResult( |
| 1201 | IAppSearchResultCallback callback, AppSearchResult<?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1202 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1203 | callback.onResult(new AppSearchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1204 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1205 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1206 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1207 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1208 | |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1209 | /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1210 | private void invokeCallbackOnResult( |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1211 | IAppSearchBatchResultCallback callback, AppSearchBatchResult<String, ?> result) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1212 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1213 | callback.onResult(new AppSearchBatchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1214 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1215 | Log.e(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1216 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1220 | * Invokes the {@link IAppSearchResultCallback} with an throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1221 | * |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1222 | * <p>The throwable is convert to a {@link AppSearchResult}; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1223 | */ |
| 1224 | private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1225 | AppSearchResult<?> result = throwableToFailedResult(throwable); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1226 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1227 | callback.onResult(new AppSearchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1228 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1229 | Log.e(TAG, "Unable to send result to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | /** |
Alexander Dorokhine | 8c5ba91 | 2020-12-14 22:58:12 -0800 | [diff] [blame] | 1234 | * Invokes the {@link IAppSearchBatchResultCallback} with an unexpected internal throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1235 | * |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 1236 | * <p>The throwable is converted to {@link AppSearchResult}. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1237 | */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1238 | private void invokeCallbackOnError( |
Alexander Dorokhine | b5d34b1 | 2021-04-15 00:32:15 -0700 | [diff] [blame] | 1239 | @NonNull IAppSearchBatchResultCallback callback, @NonNull Throwable throwable) { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1240 | AppSearchResult<?> result = throwableToFailedResult(throwable); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1241 | try { |
Alexander Dorokhine | d5e8eda | 2021-05-04 13:24:47 -0700 | [diff] [blame] | 1242 | callback.onSystemError(new AppSearchResultParcel<>(result)); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1243 | } catch (RemoteException e) { |
Terry Wang | 2da1785 | 2020-12-16 19:59:08 -0800 | [diff] [blame] | 1244 | Log.e(TAG, "Unable to send error to the callback", e); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 1245 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 1246 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1247 | } |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1248 | |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1249 | /** |
| 1250 | * Helper for dealing with incoming user arguments to system service calls. |
| 1251 | * |
| 1252 | * <p>Takes care of checking permissions and converting USER_CURRENT to the actual current user. |
| 1253 | * |
| 1254 | * @return the user handle that the call should run as. Will always be a concrete user. |
| 1255 | */ |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1256 | // TODO(b/173553485) verifying that the caller has permission to access target user's data |
| 1257 | // TODO(b/173553485) Handle ACTION_USER_REMOVED broadcast |
| 1258 | // TODO(b/173553485) Implement SystemService.onUserStopping() |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1259 | @NonNull |
| 1260 | private static UserHandle handleIncomingUser(@NonNull UserHandle userHandle, int callingUid) { |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1261 | int callingPid = Binder.getCallingPid(); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1262 | int finalUserId = ActivityManager.handleIncomingUser( |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1263 | callingPid, |
| 1264 | callingUid, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1265 | userHandle.getIdentifier(), |
Cassie Wang | 0c62d99 | 2021-01-15 14:39:30 -0800 | [diff] [blame] | 1266 | /*allowAll=*/ false, |
| 1267 | /*requireFull=*/ false, |
| 1268 | /*name=*/ null, |
| 1269 | /*callerPackage=*/ null); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1270 | return UserHandle.of(finalUserId); |
Terry Wang | f209307 | 2020-11-30 04:47:19 -0800 | [diff] [blame] | 1271 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1272 | |
| 1273 | // TODO(b/179160886): Cache the previous storage stats. |
| 1274 | private class AppSearchStorageStatsAugmenter implements StorageStatsAugmenter { |
| 1275 | @Override |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1276 | public void augmentStatsForPackageForUser( |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1277 | @NonNull PackageStats stats, |
| 1278 | @NonNull String packageName, |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1279 | @NonNull UserHandle userHandle, |
| 1280 | boolean canCallerAccessAllStats) { |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1281 | Objects.requireNonNull(stats); |
| 1282 | Objects.requireNonNull(packageName); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1283 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1284 | |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1285 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1286 | verifyUserUnlocked(userHandle); |
| 1287 | PlatformLogger logger = mLoggerInstanceManager.getOrCreatePlatformLogger( |
| 1288 | mContext, userHandle); |
| 1289 | AppSearchImpl impl = mImplInstanceManager.getOrCreateAppSearchImpl( |
| 1290 | mContext, userHandle, logger); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1291 | stats.dataSize += impl.getStorageInfoForPackage(packageName).getSizeBytes(); |
| 1292 | } catch (Throwable t) { |
| 1293 | Log.e( |
| 1294 | TAG, |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1295 | "Unable to augment storage stats for " |
| 1296 | + userHandle |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1297 | + " packageName " |
| 1298 | + packageName, |
| 1299 | t); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | @Override |
| 1304 | public void augmentStatsForUid( |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1305 | @NonNull PackageStats stats, int uid, boolean canCallerAccessAllStats) { |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1306 | Objects.requireNonNull(stats); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1307 | |
| 1308 | UserHandle userHandle = UserHandle.getUserHandleForUid(uid); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1309 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1310 | verifyUserUnlocked(userHandle); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1311 | String[] packagesForUid = mPackageManager.getPackagesForUid(uid); |
| 1312 | if (packagesForUid == null) { |
| 1313 | return; |
| 1314 | } |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1315 | PlatformLogger logger = mLoggerInstanceManager.getOrCreatePlatformLogger( |
| 1316 | mContext, userHandle); |
| 1317 | AppSearchImpl impl = mImplInstanceManager.getOrCreateAppSearchImpl( |
| 1318 | mContext, userHandle, logger); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1319 | for (int i = 0; i < packagesForUid.length; i++) { |
| 1320 | stats.dataSize += |
| 1321 | impl.getStorageInfoForPackage(packagesForUid[i]).getSizeBytes(); |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } catch (Throwable t) { |
| 1324 | Log.e(TAG, "Unable to augment storage stats for uid " + uid, t); |
| 1325 | } |
| 1326 | } |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1327 | |
| 1328 | @Override |
| 1329 | public void augmentStatsForUser( |
| 1330 | @NonNull PackageStats stats, @NonNull UserHandle userHandle) { |
| 1331 | // TODO(b/179160886): this implementation could incur many jni calls and a lot of |
| 1332 | // in-memory processing from getStorageInfoForPackage. Instead, we can just compute the |
| 1333 | // size of the icing dir (or use the overall StorageInfo without interpolating it). |
| 1334 | Objects.requireNonNull(stats); |
| 1335 | Objects.requireNonNull(userHandle); |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1336 | |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1337 | try { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1338 | verifyUserUnlocked(userHandle); |
| 1339 | List<PackageInfo> packagesForUser = mPackageManager.getInstalledPackagesAsUser( |
| 1340 | /*flags=*/0, userHandle.getIdentifier()); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1341 | if (packagesForUser == null) { |
| 1342 | return; |
| 1343 | } |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1344 | PlatformLogger logger = mLoggerInstanceManager.getOrCreatePlatformLogger( |
| 1345 | mContext, userHandle); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1346 | AppSearchImpl impl = |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1347 | mImplInstanceManager.getOrCreateAppSearchImpl(mContext, userHandle, logger); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1348 | for (int i = 0; i < packagesForUser.size(); i++) { |
| 1349 | String packageName = packagesForUser.get(i).packageName; |
| 1350 | stats.dataSize += impl.getStorageInfoForPackage(packageName).getSizeBytes(); |
| 1351 | } |
| 1352 | } catch (Throwable t) { |
Alexander Dorokhine | bea5560 | 2021-05-19 13:59:45 -0700 | [diff] [blame^] | 1353 | Log.e(TAG, "Unable to augment storage stats for " + userHandle, t); |
Alexander Dorokhine | a5d0d32 | 2021-05-10 15:59:31 -0700 | [diff] [blame] | 1354 | } |
| 1355 | } |
Yang Yu | 0fcd51a | 2021-04-23 11:25:44 -0700 | [diff] [blame] | 1356 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1357 | } |