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