blob: 5b157cc7c384d6d32513d92ab2c18eb9c837c37b [file] [log] [blame]
Samuel Tan0a312022015-11-23 18:22:12 -08001/*
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#define LOG_TAG "PersistableBundle"
18
19#include <binder/PersistableBundle.h>
20
21#include <limits>
22
23#include <binder/IBinder.h>
24#include <binder/Parcel.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070025#include <log/log.h>
Samuel Tan0a312022015-11-23 18:22:12 -080026#include <utils/Errors.h>
27
Steven Moreland491e1332019-07-16 16:43:18 -070028#include "ParcelValTypes.h"
29
Jiyong Parkea2e23f2020-11-13 17:26:33 +090030using android::binder::VAL_BOOLEAN;
31using android::binder::VAL_INTEGER;
32using android::binder::VAL_LONG;
33using android::binder::VAL_DOUBLE;
34using android::binder::VAL_STRING;
35using android::binder::VAL_BOOLEANARRAY;
36using android::binder::VAL_INTARRAY;
37using android::binder::VAL_LONGARRAY;
38using android::binder::VAL_DOUBLEARRAY;
39using android::binder::VAL_STRINGARRAY;
40using android::binder::VAL_PERSISTABLEBUNDLE;
41
Samuel Tan52545f22016-02-12 13:56:17 -080042using std::map;
43using std::set;
44using std::vector;
Samuel Tan0a312022015-11-23 18:22:12 -080045
46enum {
Makoto Onuki39696bd2018-04-26 09:11:28 -070047 // Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
Samuel Tan0a312022015-11-23 18:22:12 -080048 BUNDLE_MAGIC = 0x4C444E42,
Makoto Onuki39696bd2018-04-26 09:11:28 -070049 BUNDLE_MAGIC_NATIVE = 0x4C444E44,
Samuel Tan0a312022015-11-23 18:22:12 -080050};
51
Samuel Tan0a312022015-11-23 18:22:12 -080052namespace {
53template <typename T>
Samuel Tan52545f22016-02-12 13:56:17 -080054bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
Samuel Tan0a312022015-11-23 18:22:12 -080055 const auto& it = map.find(key);
56 if (it == map.end()) return false;
57 *out = it->second;
58 return true;
59}
Samuel Tan52545f22016-02-12 13:56:17 -080060
61template <typename T>
62set<android::String16> getKeys(const map<android::String16, T>& map) {
63 if (map.empty()) return set<android::String16>();
64 set<android::String16> keys;
65 for (const auto& key_value_pair : map) {
66 keys.emplace(key_value_pair.first);
67 }
68 return keys;
69}
Samuel Tan0a312022015-11-23 18:22:12 -080070} // namespace
71
72namespace android {
73
74namespace os {
75
76#define RETURN_IF_FAILED(calledOnce) \
77 { \
78 status_t returnStatus = calledOnce; \
79 if (returnStatus) { \
80 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
81 return returnStatus; \
82 } \
83 }
84
Abhishek Gadewar76f00822024-05-09 17:02:18 -070085#define RETURN_IF_ENTRY_ERASED(map, key) \
86 { \
87 size_t num_erased = (map).erase(key); \
88 if (num_erased) { \
89 return num_erased; \
90 } \
Samuel Tan0a312022015-11-23 18:22:12 -080091 }
92
93status_t PersistableBundle::writeToParcel(Parcel* parcel) const {
94 /*
95 * Keep implementation in sync with writeToParcelInner() in
96 * frameworks/base/core/java/android/os/BaseBundle.java.
97 */
98
99 // Special case for empty bundles.
100 if (empty()) {
101 RETURN_IF_FAILED(parcel->writeInt32(0));
102 return NO_ERROR;
103 }
104
105 size_t length_pos = parcel->dataPosition();
106 RETURN_IF_FAILED(parcel->writeInt32(1)); // dummy, will hold length
Makoto Onuki39696bd2018-04-26 09:11:28 -0700107 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
Samuel Tan0a312022015-11-23 18:22:12 -0800108
109 size_t start_pos = parcel->dataPosition();
110 RETURN_IF_FAILED(writeToParcelInner(parcel));
111 size_t end_pos = parcel->dataPosition();
112
113 // Backpatch length. This length value includes the length header.
114 parcel->setDataPosition(length_pos);
115 size_t length = end_pos - start_pos;
116 if (length > std::numeric_limits<int32_t>::max()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800117 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
Samuel Tan0a312022015-11-23 18:22:12 -0800118 return BAD_VALUE;
119 }
120 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
121 parcel->setDataPosition(end_pos);
122 return NO_ERROR;
123}
124
125status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
126 /*
127 * Keep implementation in sync with readFromParcelInner() in
128 * frameworks/base/core/java/android/os/BaseBundle.java.
129 */
130 int32_t length = parcel->readInt32();
131 if (length < 0) {
132 ALOGE("Bad length in parcel: %d", length);
133 return UNEXPECTED_NULL;
134 }
135
136 return readFromParcelInner(parcel, static_cast<size_t>(length));
137}
138
139bool PersistableBundle::empty() const {
140 return size() == 0u;
141}
142
143size_t PersistableBundle::size() const {
144 return (mBoolMap.size() +
145 mIntMap.size() +
146 mLongMap.size() +
147 mDoubleMap.size() +
148 mStringMap.size() +
149 mBoolVectorMap.size() +
150 mIntVectorMap.size() +
151 mLongVectorMap.size() +
152 mDoubleVectorMap.size() +
153 mStringVectorMap.size() +
154 mPersistableBundleMap.size());
155}
156
157size_t PersistableBundle::erase(const String16& key) {
158 RETURN_IF_ENTRY_ERASED(mBoolMap, key);
159 RETURN_IF_ENTRY_ERASED(mIntMap, key);
160 RETURN_IF_ENTRY_ERASED(mLongMap, key);
161 RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
162 RETURN_IF_ENTRY_ERASED(mStringMap, key);
163 RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
164 RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
165 RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
166 RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
167 RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
168 return mPersistableBundleMap.erase(key);
169}
170
171void PersistableBundle::putBoolean(const String16& key, bool value) {
172 erase(key);
173 mBoolMap[key] = value;
174}
175
176void PersistableBundle::putInt(const String16& key, int32_t value) {
177 erase(key);
178 mIntMap[key] = value;
179}
180
181void PersistableBundle::putLong(const String16& key, int64_t value) {
182 erase(key);
183 mLongMap[key] = value;
184}
185
186void PersistableBundle::putDouble(const String16& key, double value) {
187 erase(key);
188 mDoubleMap[key] = value;
189}
190
191void PersistableBundle::putString(const String16& key, const String16& value) {
192 erase(key);
193 mStringMap[key] = value;
194}
195
Samuel Tan52545f22016-02-12 13:56:17 -0800196void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800197 erase(key);
198 mBoolVectorMap[key] = value;
199}
200
Samuel Tan52545f22016-02-12 13:56:17 -0800201void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800202 erase(key);
203 mIntVectorMap[key] = value;
204}
205
Samuel Tan52545f22016-02-12 13:56:17 -0800206void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800207 erase(key);
208 mLongVectorMap[key] = value;
209}
210
Samuel Tan52545f22016-02-12 13:56:17 -0800211void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800212 erase(key);
213 mDoubleVectorMap[key] = value;
214}
215
Samuel Tan52545f22016-02-12 13:56:17 -0800216void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800217 erase(key);
218 mStringVectorMap[key] = value;
219}
220
221void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
222 erase(key);
223 mPersistableBundleMap[key] = value;
224}
225
226bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
227 return getValue(key, out, mBoolMap);
228}
229
230bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
231 return getValue(key, out, mIntMap);
232}
233
234bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
235 return getValue(key, out, mLongMap);
236}
237
238bool PersistableBundle::getDouble(const String16& key, double* out) const {
239 return getValue(key, out, mDoubleMap);
240}
241
242bool PersistableBundle::getString(const String16& key, String16* out) const {
243 return getValue(key, out, mStringMap);
244}
245
Samuel Tan52545f22016-02-12 13:56:17 -0800246bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800247 return getValue(key, out, mBoolVectorMap);
248}
249
Samuel Tan52545f22016-02-12 13:56:17 -0800250bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800251 return getValue(key, out, mIntVectorMap);
252}
253
Samuel Tan52545f22016-02-12 13:56:17 -0800254bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800255 return getValue(key, out, mLongVectorMap);
256}
257
Samuel Tan52545f22016-02-12 13:56:17 -0800258bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800259 return getValue(key, out, mDoubleVectorMap);
260}
261
Samuel Tan52545f22016-02-12 13:56:17 -0800262bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800263 return getValue(key, out, mStringVectorMap);
264}
265
266bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
267 return getValue(key, out, mPersistableBundleMap);
268}
269
Samuel Tan52545f22016-02-12 13:56:17 -0800270set<String16> PersistableBundle::getBooleanKeys() const {
271 return getKeys(mBoolMap);
272}
273
274set<String16> PersistableBundle::getIntKeys() const {
275 return getKeys(mIntMap);
276}
277
278set<String16> PersistableBundle::getLongKeys() const {
279 return getKeys(mLongMap);
280}
281
282set<String16> PersistableBundle::getDoubleKeys() const {
283 return getKeys(mDoubleMap);
284}
285
286set<String16> PersistableBundle::getStringKeys() const {
287 return getKeys(mStringMap);
288}
289
290set<String16> PersistableBundle::getBooleanVectorKeys() const {
291 return getKeys(mBoolVectorMap);
292}
293
294set<String16> PersistableBundle::getIntVectorKeys() const {
295 return getKeys(mIntVectorMap);
296}
297
298set<String16> PersistableBundle::getLongVectorKeys() const {
299 return getKeys(mLongVectorMap);
300}
301
302set<String16> PersistableBundle::getDoubleVectorKeys() const {
303 return getKeys(mDoubleVectorMap);
304}
305
306set<String16> PersistableBundle::getStringVectorKeys() const {
307 return getKeys(mStringVectorMap);
308}
309
310set<String16> PersistableBundle::getPersistableBundleKeys() const {
311 return getKeys(mPersistableBundleMap);
312}
313
Samuel Tan0a312022015-11-23 18:22:12 -0800314status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
315 /*
316 * To keep this implementation in sync with writeArrayMapInternal() in
317 * frameworks/base/core/java/android/os/Parcel.java, the number of key
318 * value pairs must be written into the parcel before writing the key-value
319 * pairs themselves.
320 */
321 size_t num_entries = size();
322 if (num_entries > std::numeric_limits<int32_t>::max()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800323 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
Samuel Tan0a312022015-11-23 18:22:12 -0800324 num_entries);
325 return BAD_VALUE;
326 }
327 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
328
329 for (const auto& key_val_pair : mBoolMap) {
330 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
331 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
332 RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
333 }
334 for (const auto& key_val_pair : mIntMap) {
335 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
336 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
337 RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
338 }
339 for (const auto& key_val_pair : mLongMap) {
340 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
341 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
342 RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
343 }
344 for (const auto& key_val_pair : mDoubleMap) {
345 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
346 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
347 RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
348 }
349 for (const auto& key_val_pair : mStringMap) {
350 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
351 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
352 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
353 }
354 for (const auto& key_val_pair : mBoolVectorMap) {
355 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
356 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
357 RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
358 }
359 for (const auto& key_val_pair : mIntVectorMap) {
360 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
361 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
362 RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
363 }
364 for (const auto& key_val_pair : mLongVectorMap) {
365 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
366 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
367 RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
368 }
369 for (const auto& key_val_pair : mDoubleVectorMap) {
370 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
371 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
372 RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
373 }
374 for (const auto& key_val_pair : mStringVectorMap) {
375 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
376 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
377 RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
378 }
379 for (const auto& key_val_pair : mPersistableBundleMap) {
380 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
381 RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
382 RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
383 }
384 return NO_ERROR;
385}
386
387status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
388 /*
389 * Note: we don't actually use length for anything other than an empty PersistableBundle
390 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
391 * implementation.
392 */
393 if (length == 0) {
394 // Empty PersistableBundle or end of data.
395 return NO_ERROR;
396 }
397
398 int32_t magic;
399 RETURN_IF_FAILED(parcel->readInt32(&magic));
Makoto Onuki39696bd2018-04-26 09:11:28 -0700400 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
Samuel Tan0a312022015-11-23 18:22:12 -0800401 ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
402 return BAD_VALUE;
403 }
404
405 /*
406 * To keep this implementation in sync with unparcel() in
407 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
408 * key-value pairs must be read from the parcel before reading the key-value
409 * pairs themselves.
410 */
411 int32_t num_entries;
412 RETURN_IF_FAILED(parcel->readInt32(&num_entries));
413
414 for (; num_entries > 0; --num_entries) {
Samuel Tan0a312022015-11-23 18:22:12 -0800415 String16 key;
416 int32_t value_type;
417 RETURN_IF_FAILED(parcel->readString16(&key));
418 RETURN_IF_FAILED(parcel->readInt32(&value_type));
419
420 /*
421 * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
422 * are unique.
423 */
424 switch (value_type) {
425 case VAL_STRING: {
426 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
427 break;
428 }
429 case VAL_INTEGER: {
430 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
431 break;
432 }
433 case VAL_LONG: {
434 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
435 break;
436 }
437 case VAL_DOUBLE: {
438 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
439 break;
440 }
441 case VAL_BOOLEAN: {
442 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
443 break;
444 }
445 case VAL_STRINGARRAY: {
446 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
447 break;
448 }
449 case VAL_INTARRAY: {
450 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
451 break;
452 }
453 case VAL_LONGARRAY: {
454 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
455 break;
456 }
457 case VAL_BOOLEANARRAY: {
458 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
459 break;
460 }
461 case VAL_PERSISTABLEBUNDLE: {
462 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
463 break;
464 }
465 case VAL_DOUBLEARRAY: {
466 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
467 break;
468 }
469 default: {
470 ALOGE("Unrecognized type: %d", value_type);
471 return BAD_TYPE;
472 break;
473 }
474 }
475 }
476
477 return NO_ERROR;
478}
479
480} // namespace os
481
482} // namespace android