blob: 15047152a19046ad338c429478278ca01152b689 [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
85#define RETURN_IF_ENTRY_ERASED(map, key) \
86 { \
Chih-Hung Hsieh8f6b9b32016-05-20 11:30:10 -070087 size_t num_erased = (map).erase(key); \
Samuel Tan0a312022015-11-23 18:22:12 -080088 if (num_erased) { \
89 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
90 return num_erased; \
91 } \
92 }
93
94status_t PersistableBundle::writeToParcel(Parcel* parcel) const {
95 /*
96 * Keep implementation in sync with writeToParcelInner() in
97 * frameworks/base/core/java/android/os/BaseBundle.java.
98 */
99
100 // Special case for empty bundles.
101 if (empty()) {
102 RETURN_IF_FAILED(parcel->writeInt32(0));
103 return NO_ERROR;
104 }
105
106 size_t length_pos = parcel->dataPosition();
107 RETURN_IF_FAILED(parcel->writeInt32(1)); // dummy, will hold length
Makoto Onuki39696bd2018-04-26 09:11:28 -0700108 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
Samuel Tan0a312022015-11-23 18:22:12 -0800109
110 size_t start_pos = parcel->dataPosition();
111 RETURN_IF_FAILED(writeToParcelInner(parcel));
112 size_t end_pos = parcel->dataPosition();
113
114 // Backpatch length. This length value includes the length header.
115 parcel->setDataPosition(length_pos);
116 size_t length = end_pos - start_pos;
117 if (length > std::numeric_limits<int32_t>::max()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800118 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
Samuel Tan0a312022015-11-23 18:22:12 -0800119 return BAD_VALUE;
120 }
121 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
122 parcel->setDataPosition(end_pos);
123 return NO_ERROR;
124}
125
126status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
127 /*
128 * Keep implementation in sync with readFromParcelInner() in
129 * frameworks/base/core/java/android/os/BaseBundle.java.
130 */
131 int32_t length = parcel->readInt32();
132 if (length < 0) {
133 ALOGE("Bad length in parcel: %d", length);
134 return UNEXPECTED_NULL;
135 }
136
137 return readFromParcelInner(parcel, static_cast<size_t>(length));
138}
139
140bool PersistableBundle::empty() const {
141 return size() == 0u;
142}
143
144size_t PersistableBundle::size() const {
145 return (mBoolMap.size() +
146 mIntMap.size() +
147 mLongMap.size() +
148 mDoubleMap.size() +
149 mStringMap.size() +
150 mBoolVectorMap.size() +
151 mIntVectorMap.size() +
152 mLongVectorMap.size() +
153 mDoubleVectorMap.size() +
154 mStringVectorMap.size() +
155 mPersistableBundleMap.size());
156}
157
158size_t PersistableBundle::erase(const String16& key) {
159 RETURN_IF_ENTRY_ERASED(mBoolMap, key);
160 RETURN_IF_ENTRY_ERASED(mIntMap, key);
161 RETURN_IF_ENTRY_ERASED(mLongMap, key);
162 RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
163 RETURN_IF_ENTRY_ERASED(mStringMap, key);
164 RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
165 RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
166 RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
167 RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
168 RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
169 return mPersistableBundleMap.erase(key);
170}
171
172void PersistableBundle::putBoolean(const String16& key, bool value) {
173 erase(key);
174 mBoolMap[key] = value;
175}
176
177void PersistableBundle::putInt(const String16& key, int32_t value) {
178 erase(key);
179 mIntMap[key] = value;
180}
181
182void PersistableBundle::putLong(const String16& key, int64_t value) {
183 erase(key);
184 mLongMap[key] = value;
185}
186
187void PersistableBundle::putDouble(const String16& key, double value) {
188 erase(key);
189 mDoubleMap[key] = value;
190}
191
192void PersistableBundle::putString(const String16& key, const String16& value) {
193 erase(key);
194 mStringMap[key] = value;
195}
196
Samuel Tan52545f22016-02-12 13:56:17 -0800197void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800198 erase(key);
199 mBoolVectorMap[key] = value;
200}
201
Samuel Tan52545f22016-02-12 13:56:17 -0800202void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800203 erase(key);
204 mIntVectorMap[key] = value;
205}
206
Samuel Tan52545f22016-02-12 13:56:17 -0800207void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800208 erase(key);
209 mLongVectorMap[key] = value;
210}
211
Samuel Tan52545f22016-02-12 13:56:17 -0800212void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800213 erase(key);
214 mDoubleVectorMap[key] = value;
215}
216
Samuel Tan52545f22016-02-12 13:56:17 -0800217void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800218 erase(key);
219 mStringVectorMap[key] = value;
220}
221
222void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
223 erase(key);
224 mPersistableBundleMap[key] = value;
225}
226
227bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
228 return getValue(key, out, mBoolMap);
229}
230
231bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
232 return getValue(key, out, mIntMap);
233}
234
235bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
236 return getValue(key, out, mLongMap);
237}
238
239bool PersistableBundle::getDouble(const String16& key, double* out) const {
240 return getValue(key, out, mDoubleMap);
241}
242
243bool PersistableBundle::getString(const String16& key, String16* out) const {
244 return getValue(key, out, mStringMap);
245}
246
Samuel Tan52545f22016-02-12 13:56:17 -0800247bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800248 return getValue(key, out, mBoolVectorMap);
249}
250
Samuel Tan52545f22016-02-12 13:56:17 -0800251bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800252 return getValue(key, out, mIntVectorMap);
253}
254
Samuel Tan52545f22016-02-12 13:56:17 -0800255bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800256 return getValue(key, out, mLongVectorMap);
257}
258
Samuel Tan52545f22016-02-12 13:56:17 -0800259bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800260 return getValue(key, out, mDoubleVectorMap);
261}
262
Samuel Tan52545f22016-02-12 13:56:17 -0800263bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800264 return getValue(key, out, mStringVectorMap);
265}
266
267bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
268 return getValue(key, out, mPersistableBundleMap);
269}
270
Samuel Tan52545f22016-02-12 13:56:17 -0800271set<String16> PersistableBundle::getBooleanKeys() const {
272 return getKeys(mBoolMap);
273}
274
275set<String16> PersistableBundle::getIntKeys() const {
276 return getKeys(mIntMap);
277}
278
279set<String16> PersistableBundle::getLongKeys() const {
280 return getKeys(mLongMap);
281}
282
283set<String16> PersistableBundle::getDoubleKeys() const {
284 return getKeys(mDoubleMap);
285}
286
287set<String16> PersistableBundle::getStringKeys() const {
288 return getKeys(mStringMap);
289}
290
291set<String16> PersistableBundle::getBooleanVectorKeys() const {
292 return getKeys(mBoolVectorMap);
293}
294
295set<String16> PersistableBundle::getIntVectorKeys() const {
296 return getKeys(mIntVectorMap);
297}
298
299set<String16> PersistableBundle::getLongVectorKeys() const {
300 return getKeys(mLongVectorMap);
301}
302
303set<String16> PersistableBundle::getDoubleVectorKeys() const {
304 return getKeys(mDoubleVectorMap);
305}
306
307set<String16> PersistableBundle::getStringVectorKeys() const {
308 return getKeys(mStringVectorMap);
309}
310
311set<String16> PersistableBundle::getPersistableBundleKeys() const {
312 return getKeys(mPersistableBundleMap);
313}
314
Samuel Tan0a312022015-11-23 18:22:12 -0800315status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
316 /*
317 * To keep this implementation in sync with writeArrayMapInternal() in
318 * frameworks/base/core/java/android/os/Parcel.java, the number of key
319 * value pairs must be written into the parcel before writing the key-value
320 * pairs themselves.
321 */
322 size_t num_entries = size();
323 if (num_entries > std::numeric_limits<int32_t>::max()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800324 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
Samuel Tan0a312022015-11-23 18:22:12 -0800325 num_entries);
326 return BAD_VALUE;
327 }
328 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
329
330 for (const auto& key_val_pair : mBoolMap) {
331 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
332 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
333 RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
334 }
335 for (const auto& key_val_pair : mIntMap) {
336 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
337 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
338 RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
339 }
340 for (const auto& key_val_pair : mLongMap) {
341 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
342 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
343 RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
344 }
345 for (const auto& key_val_pair : mDoubleMap) {
346 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
347 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
348 RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
349 }
350 for (const auto& key_val_pair : mStringMap) {
351 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
352 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
353 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
354 }
355 for (const auto& key_val_pair : mBoolVectorMap) {
356 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
357 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
358 RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
359 }
360 for (const auto& key_val_pair : mIntVectorMap) {
361 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
362 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
363 RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
364 }
365 for (const auto& key_val_pair : mLongVectorMap) {
366 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
367 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
368 RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
369 }
370 for (const auto& key_val_pair : mDoubleVectorMap) {
371 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
372 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
373 RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
374 }
375 for (const auto& key_val_pair : mStringVectorMap) {
376 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
377 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
378 RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
379 }
380 for (const auto& key_val_pair : mPersistableBundleMap) {
381 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
382 RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
383 RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
384 }
385 return NO_ERROR;
386}
387
388status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
389 /*
390 * Note: we don't actually use length for anything other than an empty PersistableBundle
391 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
392 * implementation.
393 */
394 if (length == 0) {
395 // Empty PersistableBundle or end of data.
396 return NO_ERROR;
397 }
398
399 int32_t magic;
400 RETURN_IF_FAILED(parcel->readInt32(&magic));
Makoto Onuki39696bd2018-04-26 09:11:28 -0700401 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
Samuel Tan0a312022015-11-23 18:22:12 -0800402 ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
403 return BAD_VALUE;
404 }
405
406 /*
407 * To keep this implementation in sync with unparcel() in
408 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
409 * key-value pairs must be read from the parcel before reading the key-value
410 * pairs themselves.
411 */
412 int32_t num_entries;
413 RETURN_IF_FAILED(parcel->readInt32(&num_entries));
414
415 for (; num_entries > 0; --num_entries) {
Samuel Tan0a312022015-11-23 18:22:12 -0800416 String16 key;
417 int32_t value_type;
418 RETURN_IF_FAILED(parcel->readString16(&key));
419 RETURN_IF_FAILED(parcel->readInt32(&value_type));
420
421 /*
422 * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
423 * are unique.
424 */
425 switch (value_type) {
426 case VAL_STRING: {
427 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
428 break;
429 }
430 case VAL_INTEGER: {
431 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
432 break;
433 }
434 case VAL_LONG: {
435 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
436 break;
437 }
438 case VAL_DOUBLE: {
439 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
440 break;
441 }
442 case VAL_BOOLEAN: {
443 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
444 break;
445 }
446 case VAL_STRINGARRAY: {
447 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
448 break;
449 }
450 case VAL_INTARRAY: {
451 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
452 break;
453 }
454 case VAL_LONGARRAY: {
455 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
456 break;
457 }
458 case VAL_BOOLEANARRAY: {
459 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
460 break;
461 }
462 case VAL_PERSISTABLEBUNDLE: {
463 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
464 break;
465 }
466 case VAL_DOUBLEARRAY: {
467 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
468 break;
469 }
470 default: {
471 ALOGE("Unrecognized type: %d", value_type);
472 return BAD_TYPE;
473 break;
474 }
475 }
476 }
477
478 return NO_ERROR;
479}
480
481} // namespace os
482
483} // namespace android