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>
20 #include <string.h>
21
22 #include <set>
23
24 #include "persistable_bundle_internal.h"
25
26 __BEGIN_DECLS
27
28 struct APersistableBundle {
APersistableBundleAPersistableBundle29 APersistableBundle(const APersistableBundle& pBundle) : mPBundle(pBundle.mPBundle) {}
APersistableBundleAPersistableBundle30 APersistableBundle(const android::os::PersistableBundle& pBundle) : mPBundle(pBundle) {}
31 APersistableBundle() = default;
32 android::os::PersistableBundle mPBundle;
33 };
34
APersistableBundle_new()35 APersistableBundle* _Nullable APersistableBundle_new() {
36 return new (std::nothrow) APersistableBundle();
37 }
38
APersistableBundle_dup(const APersistableBundle * pBundle)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
APersistableBundle_delete(APersistableBundle * pBundle)47 void APersistableBundle_delete(APersistableBundle* pBundle) {
48 free(pBundle);
49 }
50
APersistableBundle_isEqual(const APersistableBundle * lhs,const APersistableBundle * rhs)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
APersistableBundle_readFromParcel(const AParcel * parcel,APersistableBundle * _Nullable * outPBundle)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
APersistableBundle_writeToParcel(const APersistableBundle * pBundle,AParcel * parcel)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
APersistableBundle_size(const APersistableBundle * pBundle)80 int32_t APersistableBundle_size(const APersistableBundle* pBundle) {
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 }
APersistableBundle_erase(APersistableBundle * pBundle,const char * key)87 int32_t APersistableBundle_erase(APersistableBundle* pBundle, const char* key) {
88 return pBundle->mPBundle.erase(android::String16(key));
89 }
APersistableBundle_putBoolean(APersistableBundle * pBundle,const char * key,bool val)90 void APersistableBundle_putBoolean(APersistableBundle* pBundle, const char* key, bool val) {
91 pBundle->mPBundle.putBoolean(android::String16(key), val);
92 }
APersistableBundle_putInt(APersistableBundle * pBundle,const char * key,int32_t val)93 void APersistableBundle_putInt(APersistableBundle* pBundle, const char* key, int32_t val) {
94 pBundle->mPBundle.putInt(android::String16(key), val);
95 }
APersistableBundle_putLong(APersistableBundle * pBundle,const char * key,int64_t val)96 void APersistableBundle_putLong(APersistableBundle* pBundle, const char* key, int64_t val) {
97 pBundle->mPBundle.putLong(android::String16(key), val);
98 }
APersistableBundle_putDouble(APersistableBundle * pBundle,const char * key,double val)99 void APersistableBundle_putDouble(APersistableBundle* pBundle, const char* key, double val) {
100 pBundle->mPBundle.putDouble(android::String16(key), val);
101 }
APersistableBundle_putString(APersistableBundle * pBundle,const char * key,const char * val)102 void APersistableBundle_putString(APersistableBundle* pBundle, const char* key, const char* val) {
103 pBundle->mPBundle.putString(android::String16(key), android::String16(val));
104 }
APersistableBundle_putBooleanVector(APersistableBundle * pBundle,const char * key,const bool * vec,int32_t num)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 }
APersistableBundle_putIntVector(APersistableBundle * pBundle,const char * key,const int32_t * vec,int32_t num)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 }
APersistableBundle_putLongVector(APersistableBundle * pBundle,const char * key,const int64_t * vec,int32_t num)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 }
APersistableBundle_putDoubleVector(APersistableBundle * pBundle,const char * key,const double * vec,int32_t num)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 }
APersistableBundle_putStringVector(APersistableBundle * pBundle,const char * key,const char * const * vec,int32_t num)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 }
APersistableBundle_putPersistableBundle(APersistableBundle * pBundle,const char * key,const APersistableBundle * val)150 void APersistableBundle_putPersistableBundle(APersistableBundle* pBundle, const char* key,
151 const APersistableBundle* val) {
152 pBundle->mPBundle.putPersistableBundle(android::String16(key), val->mPBundle);
153 }
APersistableBundle_getBoolean(const APersistableBundle * pBundle,const char * key,bool * val)154 bool APersistableBundle_getBoolean(const APersistableBundle* pBundle, const char* key, bool* val) {
155 return pBundle->mPBundle.getBoolean(android::String16(key), val);
156 }
APersistableBundle_getInt(const APersistableBundle * pBundle,const char * key,int32_t * val)157 bool APersistableBundle_getInt(const APersistableBundle* pBundle, const char* key, int32_t* val) {
158 return pBundle->mPBundle.getInt(android::String16(key), val);
159 }
APersistableBundle_getLong(const APersistableBundle * pBundle,const char * key,int64_t * val)160 bool APersistableBundle_getLong(const APersistableBundle* pBundle, const char* key, int64_t* val) {
161 return pBundle->mPBundle.getLong(android::String16(key), val);
162 }
APersistableBundle_getDouble(const APersistableBundle * pBundle,const char * key,double * val)163 bool APersistableBundle_getDouble(const APersistableBundle* pBundle, const char* key, double* val) {
164 return pBundle->mPBundle.getDouble(android::String16(key), val);
165 }
APersistableBundle_getString(const APersistableBundle * pBundle,const char * key,char ** val,APersistableBundle_stringAllocator stringAllocator,void * context)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);
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;
179 }
180 }
APersistableBundle_getBooleanVector(const APersistableBundle * pBundle,const char * key,bool * buffer,int32_t bufferSizeBytes)181 int32_t APersistableBundle_getBooleanVector(const APersistableBundle* pBundle, const char* key,
182 bool* buffer, int32_t bufferSizeBytes) {
183 std::vector<bool> newVec;
184 bool ret = pBundle->mPBundle.getBooleanVector(android::String16(key), &newVec);
185 if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND;
186 return getVecInternal<bool>(newVec, buffer, bufferSizeBytes);
187 }
APersistableBundle_getIntVector(const APersistableBundle * pBundle,const char * key,int32_t * buffer,int32_t bufferSizeBytes)188 int32_t APersistableBundle_getIntVector(const APersistableBundle* pBundle, const char* key,
189 int32_t* buffer, int32_t bufferSizeBytes) {
190 std::vector<int32_t> newVec;
191 bool ret = pBundle->mPBundle.getIntVector(android::String16(key), &newVec);
192 if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND;
193 return getVecInternal<int32_t>(newVec, buffer, bufferSizeBytes);
194 }
APersistableBundle_getLongVector(const APersistableBundle * pBundle,const char * key,int64_t * buffer,int32_t bufferSizeBytes)195 int32_t APersistableBundle_getLongVector(const APersistableBundle* pBundle, const char* key,
196 int64_t* buffer, int32_t bufferSizeBytes) {
197 std::vector<int64_t> newVec;
198 bool ret = pBundle->mPBundle.getLongVector(android::String16(key), &newVec);
199 if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND;
200 return getVecInternal<int64_t>(newVec, buffer, bufferSizeBytes);
201 }
APersistableBundle_getDoubleVector(const APersistableBundle * pBundle,const char * key,double * buffer,int32_t bufferSizeBytes)202 int32_t APersistableBundle_getDoubleVector(const APersistableBundle* pBundle, const char* key,
203 double* buffer, int32_t bufferSizeBytes) {
204 std::vector<double> newVec;
205 bool ret = pBundle->mPBundle.getDoubleVector(android::String16(key), &newVec);
206 if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND;
207 return getVecInternal<double>(newVec, buffer, bufferSizeBytes);
208 }
APersistableBundle_getStringVector(const APersistableBundle * pBundle,const char * key,char ** vec,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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;
214 bool ret = pBundle->mPBundle.getStringVector(android::String16(key), &newVec);
215 if (!ret) return APERSISTABLEBUNDLE_KEY_NOT_FOUND;
216 return getStringsInternal<std::vector<android::String16>>(newVec, vec, bufferSizeBytes,
217 stringAllocator, context);
218 }
APersistableBundle_getPersistableBundle(const APersistableBundle * pBundle,const char * key,APersistableBundle ** outBundle)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 }
APersistableBundle_getBooleanKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getIntKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getLongKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getDoubleKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getStringKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getBooleanVectorKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getIntVectorKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getLongVectorKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getDoubleVectorKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getStringVectorKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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 }
APersistableBundle_getPersistableBundleKeys(const APersistableBundle * pBundle,char ** outKeys,int32_t bufferSizeBytes,APersistableBundle_stringAllocator stringAllocator,void * context)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
318