Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 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 | |
| 17 | package android.os; |
| 18 | |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 19 | import android.annotation.CallbackExecutor; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 20 | import android.annotation.IntDef; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.RequiresPermission; |
Nandana Dutt | 0a18820 | 2019-01-23 20:02:29 +0000 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 25 | import android.annotation.SystemService; |
| 26 | import android.content.Context; |
| 27 | import android.os.IBinder.DeathRecipient; |
| 28 | |
| 29 | import java.io.FileDescriptor; |
| 30 | import java.lang.annotation.Retention; |
| 31 | import java.lang.annotation.RetentionPolicy; |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 32 | import java.util.concurrent.Executor; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Class that provides a privileged API to capture and consume bugreports. |
| 36 | * |
| 37 | * @hide |
| 38 | */ |
Nandana Dutt | 0a18820 | 2019-01-23 20:02:29 +0000 | [diff] [blame] | 39 | @SystemApi |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 40 | @SystemService(Context.BUGREPORT_SERVICE) |
| 41 | public class BugreportManager { |
| 42 | private final Context mContext; |
| 43 | private final IDumpstate mBinder; |
| 44 | |
| 45 | /** @hide */ |
| 46 | public BugreportManager(@NonNull Context context, IDumpstate binder) { |
| 47 | mContext = context; |
| 48 | mBinder = binder; |
| 49 | } |
| 50 | |
| 51 | /** |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 52 | * An interface describing the callback for bugreport progress and status. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 53 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 54 | public abstract static class BugreportCallback { |
| 55 | /** @hide */ |
| 56 | @Retention(RetentionPolicy.SOURCE) |
| 57 | @IntDef(prefix = { "BUGREPORT_ERROR_" }, value = { |
| 58 | BUGREPORT_ERROR_INVALID_INPUT, |
| 59 | BUGREPORT_ERROR_RUNTIME, |
| 60 | BUGREPORT_ERROR_USER_DENIED_CONSENT, |
| 61 | BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT |
| 62 | }) |
| 63 | |
| 64 | /** Possible error codes taking a bugreport can encounter */ |
| 65 | public @interface BugreportErrorCode {} |
| 66 | |
| 67 | /** The input options were invalid */ |
| 68 | public static final int BUGREPORT_ERROR_INVALID_INPUT = |
| 69 | IDumpstateListener.BUGREPORT_ERROR_INVALID_INPUT; |
| 70 | |
| 71 | /** A runtime error occured */ |
| 72 | public static final int BUGREPORT_ERROR_RUNTIME = |
| 73 | IDumpstateListener.BUGREPORT_ERROR_RUNTIME_ERROR; |
| 74 | |
| 75 | /** User denied consent to share the bugreport */ |
| 76 | public static final int BUGREPORT_ERROR_USER_DENIED_CONSENT = |
| 77 | IDumpstateListener.BUGREPORT_ERROR_USER_DENIED_CONSENT; |
| 78 | |
| 79 | /** The request to get user consent timed out. */ |
| 80 | public static final int BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT = |
| 81 | IDumpstateListener.BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT; |
| 82 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 83 | /** |
| 84 | * Called when there is a progress update. |
| 85 | * @param progress the progress in [0.0, 100.0] |
| 86 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 87 | public void onProgress(float progress) {} |
Nandana Dutt | bba7e82 | 2019-01-23 19:11:01 +0000 | [diff] [blame] | 88 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Called when taking bugreport resulted in an error. |
| 91 | * |
Nandana Dutt | bba7e82 | 2019-01-23 19:11:01 +0000 | [diff] [blame] | 92 | * <p>If {@code BUGREPORT_ERROR_USER_DENIED_CONSENT} is passed, then the user did not |
| 93 | * consent to sharing the bugreport with the calling app. |
| 94 | * |
| 95 | * <p>If {@code BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT} is passed, then the consent timed |
| 96 | * out, but the bugreport could be available in the internal directory of dumpstate for |
| 97 | * manual retrieval. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 98 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 99 | public void onError(@BugreportErrorCode int errorCode) {} |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 102 | * Called when taking bugreport finishes successfully. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 103 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 104 | public void onFinished() {} |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 108 | * Starts a bugreport. |
| 109 | * |
| 110 | * <p>This starts a bugreport in the background. However the call itself can take several |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 111 | * seconds to return in the worst case. {@code callback} will receive progress and status |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 112 | * updates. |
| 113 | * |
| 114 | * <p>The bugreport artifacts will be copied over to the given file descriptors only if the |
| 115 | * user consents to sharing with the calling app. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 116 | * |
| 117 | * @param bugreportFd file to write the bugreport. This should be opened in write-only, |
| 118 | * append mode. |
| 119 | * @param screenshotFd file to write the screenshot, if necessary. This should be opened |
| 120 | * in write-only, append mode. |
| 121 | * @param params options that specify what kind of a bugreport should be taken |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 122 | * @param callback callback for progress and status updates |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 123 | */ |
| 124 | @RequiresPermission(android.Manifest.permission.DUMP) |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 125 | public void startBugreport(@NonNull ParcelFileDescriptor bugreportFd, |
| 126 | @Nullable ParcelFileDescriptor screenshotFd, |
| 127 | @NonNull BugreportParams params, |
| 128 | @NonNull @CallbackExecutor Executor executor, |
| 129 | @NonNull BugreportCallback callback) { |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 130 | // TODO(b/111441001): Enforce android.Manifest.permission.DUMP if necessary. |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 131 | DumpstateListener dsListener = new DumpstateListener(executor, callback); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 132 | try { |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 133 | // Note: mBinder can get callingUid from the binder transaction. |
| 134 | mBinder.startBugreport(-1 /* callingUid */, |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 135 | mContext.getOpPackageName(), |
| 136 | (bugreportFd != null ? bugreportFd.getFileDescriptor() : new FileDescriptor()), |
| 137 | (screenshotFd != null |
| 138 | ? screenshotFd.getFileDescriptor() : new FileDescriptor()), |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 139 | params.getMode(), dsListener); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 140 | } catch (RemoteException e) { |
| 141 | throw e.rethrowFromSystemServer(); |
| 142 | } |
| 143 | } |
| 144 | |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 145 | /* |
| 146 | * Cancels a currently running bugreport. |
| 147 | */ |
| 148 | @RequiresPermission(android.Manifest.permission.DUMP) |
| 149 | public void cancelBugreport() { |
| 150 | try { |
| 151 | mBinder.cancelBugreport(); |
| 152 | } catch (RemoteException e) { |
| 153 | throw e.rethrowFromSystemServer(); |
| 154 | } |
| 155 | } |
| 156 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 157 | private final class DumpstateListener extends IDumpstateListener.Stub |
| 158 | implements DeathRecipient { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 159 | private final Executor mExecutor; |
| 160 | private final BugreportCallback mCallback; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 161 | |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 162 | DumpstateListener(Executor executor, @Nullable BugreportCallback callback) { |
| 163 | mExecutor = executor; |
| 164 | mCallback = callback; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void binderDied() { |
| 169 | // TODO(b/111441001): implement |
| 170 | } |
| 171 | |
| 172 | @Override |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 173 | public void onProgress(int progress) throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 174 | final long identity = Binder.clearCallingIdentity(); |
| 175 | try { |
| 176 | mExecutor.execute(() -> { |
| 177 | mCallback.onProgress(progress); |
| 178 | }); |
| 179 | } finally { |
| 180 | Binder.restoreCallingIdentity(identity); |
| 181 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onError(int errorCode) throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 186 | final long identity = Binder.clearCallingIdentity(); |
| 187 | try { |
| 188 | mExecutor.execute(() -> { |
| 189 | mCallback.onError(errorCode); |
| 190 | }); |
| 191 | } finally { |
| 192 | Binder.restoreCallingIdentity(identity); |
| 193 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 197 | public void onFinished() throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 198 | final long identity = Binder.clearCallingIdentity(); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 199 | try { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 200 | mExecutor.execute(() -> { |
| 201 | mCallback.onFinished(); |
| 202 | }); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 203 | } finally { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 204 | Binder.restoreCallingIdentity(identity); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 205 | // The bugreport has finished. Let's shutdown the service to minimize its footprint. |
| 206 | cancelBugreport(); |
| 207 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Old methods; should go away |
| 211 | @Override |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 212 | public void onProgressUpdated(int progress) throws RemoteException { |
Nandana Dutt | c8c77bc | 2019-01-15 12:48:12 +0000 | [diff] [blame] | 213 | // TODO(b/111441001): remove from interface |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void onMaxProgressUpdated(int maxProgress) throws RemoteException { |
Nandana Dutt | c8c77bc | 2019-01-15 12:48:12 +0000 | [diff] [blame] | 218 | // TODO(b/111441001): remove from interface |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void onSectionComplete(String title, int status, int size, int durationMs) |
| 223 | throws RemoteException { |
Nandana Dutt | c8c77bc | 2019-01-15 12:48:12 +0000 | [diff] [blame] | 224 | // TODO(b/111441001): remove from interface |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |