blob: c807cfe89a6d2fcca4088d2ef879bd3bf0d563db [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
Samuel Tan0a312022015-11-23 18:22:12 -080030using android::BAD_TYPE;
31using android::BAD_VALUE;
32using android::NO_ERROR;
33using android::Parcel;
34using android::sp;
35using android::status_t;
36using android::UNEXPECTED_NULL;
Jiyong Parkea2e23f2020-11-13 17:26:33 +090037
38using android::binder::VAL_BOOLEAN;
39using android::binder::VAL_INTEGER;
40using android::binder::VAL_LONG;
41using android::binder::VAL_DOUBLE;
42using android::binder::VAL_STRING;
43using android::binder::VAL_BOOLEANARRAY;
44using android::binder::VAL_INTARRAY;
45using android::binder::VAL_LONGARRAY;
46using android::binder::VAL_DOUBLEARRAY;
47using android::binder::VAL_STRINGARRAY;
48using android::binder::VAL_PERSISTABLEBUNDLE;
49
Samuel Tan52545f22016-02-12 13:56:17 -080050using std::map;
51using std::set;
52using std::vector;
Samuel Tan0a312022015-11-23 18:22:12 -080053
54enum {
Makoto Onuki39696bd2018-04-26 09:11:28 -070055 // Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
Samuel Tan0a312022015-11-23 18:22:12 -080056 BUNDLE_MAGIC = 0x4C444E42,
Makoto Onuki39696bd2018-04-26 09:11:28 -070057 BUNDLE_MAGIC_NATIVE = 0x4C444E44,
Samuel Tan0a312022015-11-23 18:22:12 -080058};
59
Samuel Tan0a312022015-11-23 18:22:12 -080060namespace {
61template <typename T>
Samuel Tan52545f22016-02-12 13:56:17 -080062bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
Samuel Tan0a312022015-11-23 18:22:12 -080063 const auto& it = map.find(key);
64 if (it == map.end()) return false;
65 *out = it->second;
66 return true;
67}
Samuel Tan52545f22016-02-12 13:56:17 -080068
69template <typename T>
70set<android::String16> getKeys(const map<android::String16, T>& map) {
71 if (map.empty()) return set<android::String16>();
72 set<android::String16> keys;
73 for (const auto& key_value_pair : map) {
74 keys.emplace(key_value_pair.first);
75 }
76 return keys;
77}
Samuel Tan0a312022015-11-23 18:22:12 -080078} // namespace
79
80namespace android {
81
82namespace os {
83
84#define RETURN_IF_FAILED(calledOnce) \
85 { \
86 status_t returnStatus = calledOnce; \
87 if (returnStatus) { \
88 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
89 return returnStatus; \
90 } \
91 }
92
93#define RETURN_IF_ENTRY_ERASED(map, key) \
94 { \
Chih-Hung Hsieh8f6b9b32016-05-20 11:30:10 -070095 size_t num_erased = (map).erase(key); \
Samuel Tan0a312022015-11-23 18:22:12 -080096 if (num_erased) { \
97 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
98 return num_erased; \
99 } \
100 }
101
102status_t PersistableBundle::writeToParcel(Parcel* parcel) const {
103 /*
104 * Keep implementation in sync with writeToParcelInner() in
105 * frameworks/base/core/java/android/os/BaseBundle.java.
106 */
107
108 // Special case for empty bundles.
109 if (empty()) {
110 RETURN_IF_FAILED(parcel->writeInt32(0));
111 return NO_ERROR;
112 }
113
114 size_t length_pos = parcel->dataPosition();
115 RETURN_IF_FAILED(parcel->writeInt32(1)); // dummy, will hold length
Makoto Onuki39696bd2018-04-26 09:11:28 -0700116 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
Samuel Tan0a312022015-11-23 18:22:12 -0800117
118 size_t start_pos = parcel->dataPosition();
119 RETURN_IF_FAILED(writeToParcelInner(parcel));
120 size_t end_pos = parcel->dataPosition();
121
122 // Backpatch length. This length value includes the length header.
123 parcel->setDataPosition(length_pos);
124 size_t length = end_pos - start_pos;
125 if (length > std::numeric_limits<int32_t>::max()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800126 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
Samuel Tan0a312022015-11-23 18:22:12 -0800127 return BAD_VALUE;
128 }
129 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
130 parcel->setDataPosition(end_pos);
131 return NO_ERROR;
132}
133
134status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
135 /*
136 * Keep implementation in sync with readFromParcelInner() in
137 * frameworks/base/core/java/android/os/BaseBundle.java.
138 */
139 int32_t length = parcel->readInt32();
140 if (length < 0) {
141 ALOGE("Bad length in parcel: %d", length);
142 return UNEXPECTED_NULL;
143 }
144
145 return readFromParcelInner(parcel, static_cast<size_t>(length));
146}
147
148bool PersistableBundle::empty() const {
149 return size() == 0u;
150}
151
152size_t PersistableBundle::size() const {
153 return (mBoolMap.size() +
154 mIntMap.size() +
155 mLongMap.size() +
156 mDoubleMap.size() +
157 mStringMap.size() +
158 mBoolVectorMap.size() +
159 mIntVectorMap.size() +
160 mLongVectorMap.size() +
161 mDoubleVectorMap.size() +
162 mStringVectorMap.size() +
163 mPersistableBundleMap.size());
164}
165
166size_t PersistableBundle::erase(const String16& key) {
167 RETURN_IF_ENTRY_ERASED(mBoolMap, key);
168 RETURN_IF_ENTRY_ERASED(mIntMap, key);
169 RETURN_IF_ENTRY_ERASED(mLongMap, key);
170 RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
171 RETURN_IF_ENTRY_ERASED(mStringMap, key);
172 RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
173 RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
174 RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
175 RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
176 RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
177 return mPersistableBundleMap.erase(key);
178}
179
180void PersistableBundle::putBoolean(const String16& key, bool value) {
181 erase(key);
182 mBoolMap[key] = value;
183}
184
185void PersistableBundle::putInt(const String16& key, int32_t value) {
186 erase(key);
187 mIntMap[key] = value;
188}
189
190void PersistableBundle::putLong(const String16& key, int64_t value) {
191 erase(key);
192 mLongMap[key] = value;
193}
194
195void PersistableBundle::putDouble(const String16& key, double value) {
196 erase(key);
197 mDoubleMap[key] = value;
198}
199
200void PersistableBundle::putString(const String16& key, const String16& value) {
201 erase(key);
202 mStringMap[key] = value;
203}
204
Samuel Tan52545f22016-02-12 13:56:17 -0800205void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800206 erase(key);
207 mBoolVectorMap[key] = value;
208}
209
Samuel Tan52545f22016-02-12 13:56:17 -0800210void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800211 erase(key);
212 mIntVectorMap[key] = value;
213}
214
Samuel Tan52545f22016-02-12 13:56:17 -0800215void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800216 erase(key);
217 mLongVectorMap[key] = value;
218}
219
Samuel Tan52545f22016-02-12 13:56:17 -0800220void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800221 erase(key);
222 mDoubleVectorMap[key] = value;
223}
224
Samuel Tan52545f22016-02-12 13:56:17 -0800225void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800226 erase(key);
227 mStringVectorMap[key] = value;
228}
229
230void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
231 erase(key);
232 mPersistableBundleMap[key] = value;
233}
234
235bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
236 return getValue(key, out, mBoolMap);
237}
238
239bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
240 return getValue(key, out, mIntMap);
241}
242
243bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
244 return getValue(key, out, mLongMap);
245}
246
247bool PersistableBundle::getDouble(const String16& key, double* out) const {
248 return getValue(key, out, mDoubleMap);
249}
250
251bool PersistableBundle::getString(const String16& key, String16* out) const {
252 return getValue(key, out, mStringMap);
253}
254
Samuel Tan52545f22016-02-12 13:56:17 -0800255bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800256 return getValue(key, out, mBoolVectorMap);
257}
258
Samuel Tan52545f22016-02-12 13:56:17 -0800259bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800260 return getValue(key, out, mIntVectorMap);
261}
262
Samuel Tan52545f22016-02-12 13:56:17 -0800263bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800264 return getValue(key, out, mLongVectorMap);
265}
266
Samuel Tan52545f22016-02-12 13:56:17 -0800267bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800268 return getValue(key, out, mDoubleVectorMap);
269}
270
Samuel Tan52545f22016-02-12 13:56:17 -0800271bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800272 return getValue(key, out, mStringVectorMap);
273}
274
275bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
276 return getValue(key, out, mPersistableBundleMap);
277}
278
Samuel Tan52545f22016-02-12 13:56:17 -0800279set<String16> PersistableBundle::getBooleanKeys() const {
280 return getKeys(mBoolMap);
281}
282
283set<String16> PersistableBundle::getIntKeys() const {
284 return getKeys(mIntMap);
285}
286
287set<String16> PersistableBundle::getLongKeys() const {
288 return getKeys(mLongMap);
289}
290
291set<String16> PersistableBundle::getDoubleKeys() const {
292 return getKeys(mDoubleMap);
293}
294
295set<String16> PersistableBundle::getStringKeys() const {
296 return getKeys(mStringMap);
297}
298
299set<String16> PersistableBundle::getBooleanVectorKeys() const {
300 return getKeys(mBoolVectorMap);
301}
302
303set<String16> PersistableBundle::getIntVectorKeys() const {
304 return getKeys(mIntVectorMap);
305}
306
307set<String16> PersistableBundle::getLongVectorKeys() const {
308 return getKeys(mLongVectorMap);
309}
310
311set<String16> PersistableBundle::getDoubleVectorKeys() const {
312 return getKeys(mDoubleVectorMap);
313}
314
315set<String16> PersistableBundle::getStringVectorKeys() const {
316 return getKeys(mStringVectorMap);
317}
318
319set<String16> PersistableBundle::getPersistableBundleKeys() const {
320 return getKeys(mPersistableBundleMap);
321}
322
Samuel Tan0a312022015-11-23 18:22:12 -0800323status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
324 /*
325 * To keep this implementation in sync with writeArrayMapInternal() in
326 * frameworks/base/core/java/android/os/Parcel.java, the number of key
327 * value pairs must be written into the parcel before writing the key-value
328 * pairs themselves.
329 */
330 size_t num_entries = size();
331 if (num_entries > std::numeric_limits<int32_t>::max()) {
Samuel Tan715dec72015-12-16 17:13:29 -0800332 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
Samuel Tan0a312022015-11-23 18:22:12 -0800333 num_entries);
334 return BAD_VALUE;
335 }
336 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
337
338 for (const auto& key_val_pair : mBoolMap) {
339 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
340 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
341 RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
342 }
343 for (const auto& key_val_pair : mIntMap) {
344 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
345 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
346 RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
347 }
348 for (const auto& key_val_pair : mLongMap) {
349 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
350 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
351 RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
352 }
353 for (const auto& key_val_pair : mDoubleMap) {
354 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
355 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
356 RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
357 }
358 for (const auto& key_val_pair : mStringMap) {
359 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
360 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
361 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
362 }
363 for (const auto& key_val_pair : mBoolVectorMap) {
364 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
365 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
366 RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
367 }
368 for (const auto& key_val_pair : mIntVectorMap) {
369 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
370 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
371 RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
372 }
373 for (const auto& key_val_pair : mLongVectorMap) {
374 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
375 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
376 RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
377 }
378 for (const auto& key_val_pair : mDoubleVectorMap) {
379 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
380 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
381 RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
382 }
383 for (const auto& key_val_pair : mStringVectorMap) {
384 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
385 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
386 RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
387 }
388 for (const auto& key_val_pair : mPersistableBundleMap) {
389 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
390 RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
391 RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
392 }
393 return NO_ERROR;
394}
395
396status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
397 /*
398 * Note: we don't actually use length for anything other than an empty PersistableBundle
399 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
400 * implementation.
401 */
402 if (length == 0) {
403 // Empty PersistableBundle or end of data.
404 return NO_ERROR;
405 }
406
407 int32_t magic;
408 RETURN_IF_FAILED(parcel->readInt32(&magic));
Makoto Onuki39696bd2018-04-26 09:11:28 -0700409 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
Samuel Tan0a312022015-11-23 18:22:12 -0800410 ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
411 return BAD_VALUE;
412 }
413
414 /*
415 * To keep this implementation in sync with unparcel() in
416 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
417 * key-value pairs must be read from the parcel before reading the key-value
418 * pairs themselves.
419 */
420 int32_t num_entries;
421 RETURN_IF_FAILED(parcel->readInt32(&num_entries));
422
423 for (; num_entries > 0; --num_entries) {
Samuel Tan0a312022015-11-23 18:22:12 -0800424 String16 key;
425 int32_t value_type;
426 RETURN_IF_FAILED(parcel->readString16(&key));
427 RETURN_IF_FAILED(parcel->readInt32(&value_type));
428
429 /*
430 * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
431 * are unique.
432 */
433 switch (value_type) {
434 case VAL_STRING: {
435 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
436 break;
437 }
438 case VAL_INTEGER: {
439 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
440 break;
441 }
442 case VAL_LONG: {
443 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
444 break;
445 }
446 case VAL_DOUBLE: {
447 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
448 break;
449 }
450 case VAL_BOOLEAN: {
451 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
452 break;
453 }
454 case VAL_STRINGARRAY: {
455 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
456 break;
457 }
458 case VAL_INTARRAY: {
459 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
460 break;
461 }
462 case VAL_LONGARRAY: {
463 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
464 break;
465 }
466 case VAL_BOOLEANARRAY: {
467 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
468 break;
469 }
470 case VAL_PERSISTABLEBUNDLE: {
471 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
472 break;
473 }
474 case VAL_DOUBLEARRAY: {
475 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
476 break;
477 }
478 default: {
479 ALOGE("Unrecognized type: %d", value_type);
480 return BAD_TYPE;
481 break;
482 }
483 }
484 }
485
486 return NO_ERROR;
487}
488
489} // namespace os
490
491} // namespace android