Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #include <android/binder_libbinder.h> |
| 17 | #include <android/persistable_bundle.h> |
| 18 | #include <binder/PersistableBundle.h> |
| 19 | #include <log/log.h> |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
| 22 | #include <set> |
| 23 | |
Tomasz Wasilczyk | b70f471 | 2024-06-21 11:58:54 -0700 | [diff] [blame] | 24 | #include "persistable_bundle_internal.h" |
| 25 | |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 26 | __BEGIN_DECLS |
| 27 | |
| 28 | struct APersistableBundle { |
| 29 | APersistableBundle(const APersistableBundle& pBundle) : mPBundle(pBundle.mPBundle) {} |
| 30 | APersistableBundle(const android::os::PersistableBundle& pBundle) : mPBundle(pBundle) {} |
| 31 | APersistableBundle() = default; |
| 32 | android::os::PersistableBundle mPBundle; |
| 33 | }; |
| 34 | |
| 35 | APersistableBundle* _Nullable APersistableBundle_new() { |
| 36 | return new (std::nothrow) APersistableBundle(); |
| 37 | } |
| 38 | |
| 39 | APersistableBundle* _Nullable APersistableBundle_dup(const APersistableBundle* pBundle) { |
| 40 | if (pBundle) { |
| 41 | return new APersistableBundle(*pBundle); |
| 42 | } else { |
| 43 | return new APersistableBundle(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void APersistableBundle_delete(APersistableBundle* pBundle) { |
| 48 | free(pBundle); |
| 49 | } |
| 50 | |
| 51 | bool APersistableBundle_isEqual(const APersistableBundle* lhs, const APersistableBundle* rhs) { |
| 52 | if (lhs && rhs) { |
| 53 | return lhs->mPBundle == rhs->mPBundle; |
| 54 | } else if (lhs == rhs) { |
| 55 | return true; |
| 56 | } else { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | binder_status_t APersistableBundle_readFromParcel(const AParcel* parcel, |
| 62 | APersistableBundle* _Nullable* outPBundle) { |
| 63 | if (!parcel || !outPBundle) return STATUS_BAD_VALUE; |
| 64 | APersistableBundle* newPBundle = APersistableBundle_new(); |
| 65 | if (newPBundle == nullptr) return STATUS_NO_MEMORY; |
| 66 | binder_status_t status = |
| 67 | newPBundle->mPBundle.readFromParcel(AParcel_viewPlatformParcel(parcel)); |
| 68 | if (status == STATUS_OK) { |
| 69 | *outPBundle = newPBundle; |
| 70 | } |
| 71 | return status; |
| 72 | } |
| 73 | |
| 74 | binder_status_t APersistableBundle_writeToParcel(const APersistableBundle* pBundle, |
| 75 | AParcel* parcel) { |
| 76 | if (!parcel || !pBundle) return STATUS_BAD_VALUE; |
| 77 | return pBundle->mPBundle.writeToParcel(AParcel_viewPlatformParcel(parcel)); |
| 78 | } |
| 79 | |
Devin Moore | 412b220 | 2024-01-05 23:11:19 +0000 | [diff] [blame] | 80 | int32_t APersistableBundle_size(const APersistableBundle* pBundle) { |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 81 | size_t size = pBundle->mPBundle.size(); |
| 82 | LOG_ALWAYS_FATAL_IF(size > INT32_MAX, |
| 83 | "The APersistableBundle has gotten too large! There will be an overflow in " |
| 84 | "the reported size."); |
| 85 | return pBundle->mPBundle.size(); |
| 86 | } |
| 87 | int32_t APersistableBundle_erase(APersistableBundle* pBundle, const char* key) { |
| 88 | return pBundle->mPBundle.erase(android::String16(key)); |
| 89 | } |
| 90 | void APersistableBundle_putBoolean(APersistableBundle* pBundle, const char* key, bool val) { |
| 91 | pBundle->mPBundle.putBoolean(android::String16(key), val); |
| 92 | } |
| 93 | void APersistableBundle_putInt(APersistableBundle* pBundle, const char* key, int32_t val) { |
| 94 | pBundle->mPBundle.putInt(android::String16(key), val); |
| 95 | } |
| 96 | void APersistableBundle_putLong(APersistableBundle* pBundle, const char* key, int64_t val) { |
| 97 | pBundle->mPBundle.putLong(android::String16(key), val); |
| 98 | } |
| 99 | void APersistableBundle_putDouble(APersistableBundle* pBundle, const char* key, double val) { |
| 100 | pBundle->mPBundle.putDouble(android::String16(key), val); |
| 101 | } |
| 102 | void APersistableBundle_putString(APersistableBundle* pBundle, const char* key, const char* val) { |
| 103 | pBundle->mPBundle.putString(android::String16(key), android::String16(val)); |
| 104 | } |
| 105 | void APersistableBundle_putBooleanVector(APersistableBundle* pBundle, const char* key, |
| 106 | const bool* vec, int32_t num) { |
| 107 | LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid."); |
| 108 | std::vector<bool> newVec(num); |
| 109 | for (int32_t i = 0; i < num; i++) { |
| 110 | newVec[i] = vec[i]; |
| 111 | } |
| 112 | pBundle->mPBundle.putBooleanVector(android::String16(key), newVec); |
| 113 | } |
| 114 | void APersistableBundle_putIntVector(APersistableBundle* pBundle, const char* key, |
| 115 | const int32_t* vec, int32_t num) { |
| 116 | LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid."); |
| 117 | std::vector<int32_t> newVec(num); |
| 118 | for (int32_t i = 0; i < num; i++) { |
| 119 | newVec[i] = vec[i]; |
| 120 | } |
| 121 | pBundle->mPBundle.putIntVector(android::String16(key), newVec); |
| 122 | } |
| 123 | void APersistableBundle_putLongVector(APersistableBundle* pBundle, const char* key, |
| 124 | const int64_t* vec, int32_t num) { |
| 125 | LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid."); |
| 126 | std::vector<int64_t> newVec(num); |
| 127 | for (int32_t i = 0; i < num; i++) { |
| 128 | newVec[i] = vec[i]; |
| 129 | } |
| 130 | pBundle->mPBundle.putLongVector(android::String16(key), newVec); |
| 131 | } |
| 132 | void APersistableBundle_putDoubleVector(APersistableBundle* pBundle, const char* key, |
| 133 | const double* vec, int32_t num) { |
| 134 | LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid."); |
| 135 | std::vector<double> newVec(num); |
| 136 | for (int32_t i = 0; i < num; i++) { |
| 137 | newVec[i] = vec[i]; |
| 138 | } |
| 139 | pBundle->mPBundle.putDoubleVector(android::String16(key), newVec); |
| 140 | } |
| 141 | void APersistableBundle_putStringVector(APersistableBundle* pBundle, const char* key, |
| 142 | const char* const* vec, int32_t num) { |
| 143 | LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid."); |
| 144 | std::vector<android::String16> newVec(num); |
| 145 | for (int32_t i = 0; i < num; i++) { |
| 146 | newVec[i] = android::String16(vec[i]); |
| 147 | } |
| 148 | pBundle->mPBundle.putStringVector(android::String16(key), newVec); |
| 149 | } |
| 150 | void APersistableBundle_putPersistableBundle(APersistableBundle* pBundle, const char* key, |
| 151 | const APersistableBundle* val) { |
| 152 | pBundle->mPBundle.putPersistableBundle(android::String16(key), val->mPBundle); |
| 153 | } |
| 154 | bool APersistableBundle_getBoolean(const APersistableBundle* pBundle, const char* key, bool* val) { |
| 155 | return pBundle->mPBundle.getBoolean(android::String16(key), val); |
| 156 | } |
| 157 | bool APersistableBundle_getInt(const APersistableBundle* pBundle, const char* key, int32_t* val) { |
| 158 | return pBundle->mPBundle.getInt(android::String16(key), val); |
| 159 | } |
| 160 | bool APersistableBundle_getLong(const APersistableBundle* pBundle, const char* key, int64_t* val) { |
| 161 | return pBundle->mPBundle.getLong(android::String16(key), val); |
| 162 | } |
| 163 | bool APersistableBundle_getDouble(const APersistableBundle* pBundle, const char* key, double* val) { |
| 164 | return pBundle->mPBundle.getDouble(android::String16(key), val); |
| 165 | } |
| 166 | int32_t APersistableBundle_getString(const APersistableBundle* pBundle, const char* key, char** val, |
| 167 | APersistableBundle_stringAllocator stringAllocator, |
| 168 | void* context) { |
| 169 | android::String16 outVal; |
| 170 | bool ret = pBundle->mPBundle.getString(android::String16(key), &outVal); |
Devin Moore | 412b220 | 2024-01-05 23:11:19 +0000 | [diff] [blame] | 171 | if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND; |
| 172 | android::String8 tmp8(outVal); |
| 173 | *val = stringAllocator(tmp8.bytes() + 1, context); |
| 174 | if (*val) { |
| 175 | strncpy(*val, tmp8.c_str(), tmp8.bytes() + 1); |
| 176 | return tmp8.bytes(); |
| 177 | } else { |
| 178 | return APERSISTABLEBUNDLE_ALLOCATOR_FAILED; |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 179 | } |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 180 | } |
| 181 | int32_t APersistableBundle_getBooleanVector(const APersistableBundle* pBundle, const char* key, |
| 182 | bool* buffer, int32_t bufferSizeBytes) { |
| 183 | std::vector<bool> newVec; |
Devin Moore | 412b220 | 2024-01-05 23:11:19 +0000 | [diff] [blame] | 184 | bool ret = pBundle->mPBundle.getBooleanVector(android::String16(key), &newVec); |
| 185 | if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND; |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 186 | return getVecInternal<bool>(newVec, buffer, bufferSizeBytes); |
| 187 | } |
| 188 | int32_t APersistableBundle_getIntVector(const APersistableBundle* pBundle, const char* key, |
| 189 | int32_t* buffer, int32_t bufferSizeBytes) { |
| 190 | std::vector<int32_t> newVec; |
Devin Moore | 412b220 | 2024-01-05 23:11:19 +0000 | [diff] [blame] | 191 | bool ret = pBundle->mPBundle.getIntVector(android::String16(key), &newVec); |
| 192 | if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND; |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 193 | return getVecInternal<int32_t>(newVec, buffer, bufferSizeBytes); |
| 194 | } |
| 195 | int32_t APersistableBundle_getLongVector(const APersistableBundle* pBundle, const char* key, |
| 196 | int64_t* buffer, int32_t bufferSizeBytes) { |
| 197 | std::vector<int64_t> newVec; |
Devin Moore | 412b220 | 2024-01-05 23:11:19 +0000 | [diff] [blame] | 198 | bool ret = pBundle->mPBundle.getLongVector(android::String16(key), &newVec); |
| 199 | if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND; |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 200 | return getVecInternal<int64_t>(newVec, buffer, bufferSizeBytes); |
| 201 | } |
| 202 | int32_t APersistableBundle_getDoubleVector(const APersistableBundle* pBundle, const char* key, |
| 203 | double* buffer, int32_t bufferSizeBytes) { |
| 204 | std::vector<double> newVec; |
Devin Moore | 412b220 | 2024-01-05 23:11:19 +0000 | [diff] [blame] | 205 | bool ret = pBundle->mPBundle.getDoubleVector(android::String16(key), &newVec); |
| 206 | if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND; |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 207 | return getVecInternal<double>(newVec, buffer, bufferSizeBytes); |
| 208 | } |
| 209 | int32_t APersistableBundle_getStringVector(const APersistableBundle* pBundle, const char* key, |
| 210 | char** vec, int32_t bufferSizeBytes, |
| 211 | APersistableBundle_stringAllocator stringAllocator, |
| 212 | void* context) { |
| 213 | std::vector<android::String16> newVec; |
Devin Moore | 412b220 | 2024-01-05 23:11:19 +0000 | [diff] [blame] | 214 | bool ret = pBundle->mPBundle.getStringVector(android::String16(key), &newVec); |
| 215 | if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND; |
Devin Moore | 33ce330 | 2023-10-20 16:53:08 +0000 | [diff] [blame] | 216 | return getStringsInternal<std::vector<android::String16>>(newVec, vec, bufferSizeBytes, |
| 217 | stringAllocator, context); |
| 218 | } |
| 219 | bool APersistableBundle_getPersistableBundle(const APersistableBundle* pBundle, const char* key, |
| 220 | APersistableBundle** outBundle) { |
| 221 | APersistableBundle* bundle = APersistableBundle_new(); |
| 222 | bool ret = pBundle->mPBundle.getPersistableBundle(android::String16(key), &bundle->mPBundle); |
| 223 | if (ret) { |
| 224 | *outBundle = bundle; |
| 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | int32_t APersistableBundle_getBooleanKeys(const APersistableBundle* pBundle, char** outKeys, |
| 230 | int32_t bufferSizeBytes, |
| 231 | APersistableBundle_stringAllocator stringAllocator, |
| 232 | void* context) { |
| 233 | std::set<android::String16> ret = pBundle->mPBundle.getBooleanKeys(); |
| 234 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 235 | stringAllocator, context); |
| 236 | } |
| 237 | int32_t APersistableBundle_getIntKeys(const APersistableBundle* pBundle, char** outKeys, |
| 238 | int32_t bufferSizeBytes, |
| 239 | APersistableBundle_stringAllocator stringAllocator, |
| 240 | void* context) { |
| 241 | std::set<android::String16> ret = pBundle->mPBundle.getIntKeys(); |
| 242 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 243 | stringAllocator, context); |
| 244 | } |
| 245 | int32_t APersistableBundle_getLongKeys(const APersistableBundle* pBundle, char** outKeys, |
| 246 | int32_t bufferSizeBytes, |
| 247 | APersistableBundle_stringAllocator stringAllocator, |
| 248 | void* context) { |
| 249 | std::set<android::String16> ret = pBundle->mPBundle.getLongKeys(); |
| 250 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 251 | stringAllocator, context); |
| 252 | } |
| 253 | int32_t APersistableBundle_getDoubleKeys(const APersistableBundle* pBundle, char** outKeys, |
| 254 | int32_t bufferSizeBytes, |
| 255 | APersistableBundle_stringAllocator stringAllocator, |
| 256 | void* context) { |
| 257 | std::set<android::String16> ret = pBundle->mPBundle.getDoubleKeys(); |
| 258 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 259 | stringAllocator, context); |
| 260 | } |
| 261 | int32_t APersistableBundle_getStringKeys(const APersistableBundle* pBundle, char** outKeys, |
| 262 | int32_t bufferSizeBytes, |
| 263 | APersistableBundle_stringAllocator stringAllocator, |
| 264 | void* context) { |
| 265 | std::set<android::String16> ret = pBundle->mPBundle.getStringKeys(); |
| 266 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 267 | stringAllocator, context); |
| 268 | } |
| 269 | int32_t APersistableBundle_getBooleanVectorKeys(const APersistableBundle* pBundle, char** outKeys, |
| 270 | int32_t bufferSizeBytes, |
| 271 | APersistableBundle_stringAllocator stringAllocator, |
| 272 | void* context) { |
| 273 | std::set<android::String16> ret = pBundle->mPBundle.getBooleanVectorKeys(); |
| 274 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 275 | stringAllocator, context); |
| 276 | } |
| 277 | int32_t APersistableBundle_getIntVectorKeys(const APersistableBundle* pBundle, char** outKeys, |
| 278 | int32_t bufferSizeBytes, |
| 279 | APersistableBundle_stringAllocator stringAllocator, |
| 280 | void* context) { |
| 281 | std::set<android::String16> ret = pBundle->mPBundle.getIntVectorKeys(); |
| 282 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 283 | stringAllocator, context); |
| 284 | } |
| 285 | int32_t APersistableBundle_getLongVectorKeys(const APersistableBundle* pBundle, char** outKeys, |
| 286 | int32_t bufferSizeBytes, |
| 287 | APersistableBundle_stringAllocator stringAllocator, |
| 288 | void* context) { |
| 289 | std::set<android::String16> ret = pBundle->mPBundle.getLongVectorKeys(); |
| 290 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 291 | stringAllocator, context); |
| 292 | } |
| 293 | int32_t APersistableBundle_getDoubleVectorKeys(const APersistableBundle* pBundle, char** outKeys, |
| 294 | int32_t bufferSizeBytes, |
| 295 | APersistableBundle_stringAllocator stringAllocator, |
| 296 | void* context) { |
| 297 | std::set<android::String16> ret = pBundle->mPBundle.getDoubleVectorKeys(); |
| 298 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 299 | stringAllocator, context); |
| 300 | } |
| 301 | int32_t APersistableBundle_getStringVectorKeys(const APersistableBundle* pBundle, char** outKeys, |
| 302 | int32_t bufferSizeBytes, |
| 303 | APersistableBundle_stringAllocator stringAllocator, |
| 304 | void* context) { |
| 305 | std::set<android::String16> ret = pBundle->mPBundle.getStringVectorKeys(); |
| 306 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 307 | stringAllocator, context); |
| 308 | } |
| 309 | int32_t APersistableBundle_getPersistableBundleKeys( |
| 310 | const APersistableBundle* pBundle, char** outKeys, int32_t bufferSizeBytes, |
| 311 | APersistableBundle_stringAllocator stringAllocator, void* context) { |
| 312 | std::set<android::String16> ret = pBundle->mPBundle.getPersistableBundleKeys(); |
| 313 | return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes, |
| 314 | stringAllocator, context); |
| 315 | } |
| 316 | |
| 317 | __END_DECLS |