Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include <binder/Status.h> |
| 18 | |
| 19 | namespace android { |
| 20 | namespace binder { |
| 21 | |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 22 | Status Status::ok() { |
| 23 | return Status(); |
| 24 | } |
| 25 | |
| 26 | Status Status::fromExceptionCode(int32_t exceptionCode) { |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame^] | 27 | if (exceptionCode == EX_TRANSACTION_FAILED) { |
| 28 | return Status(exceptionCode, FAILED_TRANSACTION); |
| 29 | } |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 30 | return Status(exceptionCode, OK); |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | Status Status::fromExceptionCode(int32_t exceptionCode, |
| 34 | const String8& message) { |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame^] | 35 | if (exceptionCode == EX_TRANSACTION_FAILED) { |
| 36 | return Status(exceptionCode, FAILED_TRANSACTION, message); |
| 37 | } |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 38 | return Status(exceptionCode, OK, message); |
| 39 | } |
| 40 | |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 41 | Status Status::fromExceptionCode(int32_t exceptionCode, |
| 42 | const char* message) { |
| 43 | return fromExceptionCode(exceptionCode, String8(message)); |
| 44 | } |
| 45 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 46 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) { |
| 47 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode); |
| 48 | } |
| 49 | |
| 50 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 51 | const String8& message) { |
| 52 | return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Christopher Wiley | 1651ced | 2016-05-06 09:19:44 -0700 | [diff] [blame] | 55 | Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode, |
| 56 | const char* message) { |
| 57 | return fromServiceSpecificError(serviceSpecificErrorCode, String8(message)); |
| 58 | } |
| 59 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 60 | Status Status::fromStatusT(status_t status) { |
| 61 | Status ret; |
| 62 | ret.setFromStatusT(status); |
| 63 | return ret; |
| 64 | } |
| 65 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 66 | Status::Status(int32_t exceptionCode, int32_t errorCode) |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 67 | : mException(exceptionCode), |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 68 | mErrorCode(errorCode) {} |
| 69 | |
| 70 | Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message) |
| 71 | : mException(exceptionCode), |
| 72 | mErrorCode(errorCode), |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 73 | mMessage(message) {} |
| 74 | |
| 75 | status_t Status::readFromParcel(const Parcel& parcel) { |
| 76 | status_t status = parcel.readInt32(&mException); |
| 77 | if (status != OK) { |
| 78 | setFromStatusT(status); |
| 79 | return status; |
| 80 | } |
| 81 | |
| 82 | // Skip over fat response headers. Not used (or propagated) in native code. |
| 83 | if (mException == EX_HAS_REPLY_HEADER) { |
| 84 | // Note that the header size includes the 4 byte size field. |
| 85 | const int32_t header_start = parcel.dataPosition(); |
| 86 | int32_t header_size; |
| 87 | status = parcel.readInt32(&header_size); |
| 88 | if (status != OK) { |
| 89 | setFromStatusT(status); |
| 90 | return status; |
| 91 | } |
| 92 | parcel.setDataPosition(header_start + header_size); |
| 93 | // And fat response headers are currently only used when there are no |
| 94 | // exceptions, so act like there was no error. |
| 95 | mException = EX_NONE; |
| 96 | } |
| 97 | |
| 98 | if (mException == EX_NONE) { |
| 99 | return status; |
| 100 | } |
| 101 | |
| 102 | // The remote threw an exception. Get the message back. |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 103 | String16 message; |
| 104 | status = parcel.readString16(&message); |
| 105 | if (status != OK) { |
| 106 | setFromStatusT(status); |
| 107 | return status; |
| 108 | } |
| 109 | mMessage = String8(message); |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 110 | |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 111 | // Skip over the remote stack trace data |
| 112 | int32_t remote_stack_trace_header_size; |
| 113 | status = parcel.readInt32(&remote_stack_trace_header_size); |
| 114 | if (status != OK) { |
| 115 | setFromStatusT(status); |
| 116 | return status; |
| 117 | } |
| 118 | parcel.setDataPosition(parcel.dataPosition() + remote_stack_trace_header_size); |
| 119 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 120 | if (mException == EX_SERVICE_SPECIFIC) { |
| 121 | status = parcel.readInt32(&mErrorCode); |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 122 | } else if (mException == EX_PARCELABLE) { |
| 123 | // Skip over the blob of Parcelable data |
| 124 | const int32_t header_start = parcel.dataPosition(); |
| 125 | int32_t header_size; |
| 126 | status = parcel.readInt32(&header_size); |
| 127 | if (status != OK) { |
| 128 | setFromStatusT(status); |
| 129 | return status; |
| 130 | } |
| 131 | parcel.setDataPosition(header_start + header_size); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 132 | } |
| 133 | if (status != OK) { |
| 134 | setFromStatusT(status); |
| 135 | return status; |
| 136 | } |
| 137 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 138 | return status; |
| 139 | } |
| 140 | |
| 141 | status_t Status::writeToParcel(Parcel* parcel) const { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 142 | // Something really bad has happened, and we're not going to even |
| 143 | // try returning rich error data. |
| 144 | if (mException == EX_TRANSACTION_FAILED) { |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame^] | 145 | return mErrorCode; |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 148 | status_t status = parcel->writeInt32(mException); |
| 149 | if (status != OK) { return status; } |
| 150 | if (mException == EX_NONE) { |
| 151 | // We have no more information to write. |
| 152 | return status; |
| 153 | } |
| 154 | status = parcel->writeString16(String16(mMessage)); |
Fyodor Kupolov | d9ac374 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 155 | status = parcel->writeInt32(0); // Empty remote stack trace header |
Jeff Sharkey | c8dc4f0 | 2017-01-17 13:53:04 -0700 | [diff] [blame] | 156 | if (mException == EX_SERVICE_SPECIFIC) { |
| 157 | status = parcel->writeInt32(mErrorCode); |
| 158 | } else if (mException == EX_PARCELABLE) { |
| 159 | // Sending Parcelable blobs currently not supported |
| 160 | status = parcel->writeInt32(0); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 161 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 162 | return status; |
| 163 | } |
| 164 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 165 | void Status::setException(int32_t ex, const String8& message) { |
| 166 | mException = ex; |
Steven Moreland | fb26e72 | 2018-10-05 13:43:24 -0700 | [diff] [blame^] | 167 | mErrorCode = ex == EX_TRANSACTION_FAILED ? FAILED_TRANSACTION : NO_ERROR; |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 168 | mMessage.setTo(message); |
| 169 | } |
| 170 | |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 171 | void Status::setServiceSpecificError(int32_t errorCode, const String8& message) { |
| 172 | setException(EX_SERVICE_SPECIFIC, message); |
| 173 | mErrorCode = errorCode; |
| 174 | } |
| 175 | |
| 176 | void Status::setFromStatusT(status_t status) { |
| 177 | mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED; |
| 178 | mErrorCode = status; |
| 179 | mMessage.clear(); |
| 180 | } |
| 181 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 182 | String8 Status::toString8() const { |
| 183 | String8 ret; |
| 184 | if (mException == EX_NONE) { |
| 185 | ret.append("No error"); |
| 186 | } else { |
| 187 | ret.appendFormat("Status(%d): '", mException); |
Christopher Wiley | c1e491d | 2015-11-20 17:48:27 -0800 | [diff] [blame] | 188 | if (mException == EX_SERVICE_SPECIFIC || |
| 189 | mException == EX_TRANSACTION_FAILED) { |
Christopher Wiley | cff7f17 | 2015-11-22 16:29:58 -0800 | [diff] [blame] | 190 | ret.appendFormat("%d: ", mErrorCode); |
| 191 | } |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 192 | ret.append(String8(mMessage)); |
| 193 | ret.append("'"); |
| 194 | } |
| 195 | return ret; |
| 196 | } |
| 197 | |
Ralph Nathan | 00cb980 | 2016-04-13 12:42:06 -0700 | [diff] [blame] | 198 | std::stringstream& operator<< (std::stringstream& stream, const Status& s) { |
| 199 | stream << s.toString8().string(); |
| 200 | return stream; |
| 201 | } |
| 202 | |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 203 | } // namespace binder |
| 204 | } // namespace android |