blob: 99f9726c5e9dec92d5df2e0524df0cd6a64a1054 [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;
Tomasz Wasilczyk052c5132024-06-21 15:45:26 -0700116 if (length > static_cast<size_t>(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);
Nan Wue1226b02025-01-27 20:24:48 +0000122 // write mHasIntent to be consistent with BaseBundle.writeToBundle. But it would always be
123 // false since PersistableBundle won't contain an intent.
124 RETURN_IF_FAILED(parcel->writeBool(false));
Samuel Tan0a312022015-11-23 18:22:12 -0800125 return NO_ERROR;
126}
127
128status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
129 /*
130 * Keep implementation in sync with readFromParcelInner() in
131 * frameworks/base/core/java/android/os/BaseBundle.java.
132 */
133 int32_t length = parcel->readInt32();
134 if (length < 0) {
135 ALOGE("Bad length in parcel: %d", length);
136 return UNEXPECTED_NULL;
137 }
138
139 return readFromParcelInner(parcel, static_cast<size_t>(length));
140}
141
142bool PersistableBundle::empty() const {
143 return size() == 0u;
144}
145
146size_t PersistableBundle::size() const {
147 return (mBoolMap.size() +
148 mIntMap.size() +
149 mLongMap.size() +
150 mDoubleMap.size() +
151 mStringMap.size() +
152 mBoolVectorMap.size() +
153 mIntVectorMap.size() +
154 mLongVectorMap.size() +
155 mDoubleVectorMap.size() +
156 mStringVectorMap.size() +
157 mPersistableBundleMap.size());
158}
159
160size_t PersistableBundle::erase(const String16& key) {
161 RETURN_IF_ENTRY_ERASED(mBoolMap, key);
162 RETURN_IF_ENTRY_ERASED(mIntMap, key);
163 RETURN_IF_ENTRY_ERASED(mLongMap, key);
164 RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
165 RETURN_IF_ENTRY_ERASED(mStringMap, key);
166 RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
167 RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
168 RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
169 RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
170 RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
171 return mPersistableBundleMap.erase(key);
172}
173
174void PersistableBundle::putBoolean(const String16& key, bool value) {
175 erase(key);
176 mBoolMap[key] = value;
177}
178
179void PersistableBundle::putInt(const String16& key, int32_t value) {
180 erase(key);
181 mIntMap[key] = value;
182}
183
184void PersistableBundle::putLong(const String16& key, int64_t value) {
185 erase(key);
186 mLongMap[key] = value;
187}
188
189void PersistableBundle::putDouble(const String16& key, double value) {
190 erase(key);
191 mDoubleMap[key] = value;
192}
193
194void PersistableBundle::putString(const String16& key, const String16& value) {
195 erase(key);
196 mStringMap[key] = value;
197}
198
Samuel Tan52545f22016-02-12 13:56:17 -0800199void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800200 erase(key);
201 mBoolVectorMap[key] = value;
202}
203
Samuel Tan52545f22016-02-12 13:56:17 -0800204void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800205 erase(key);
206 mIntVectorMap[key] = value;
207}
208
Samuel Tan52545f22016-02-12 13:56:17 -0800209void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800210 erase(key);
211 mLongVectorMap[key] = value;
212}
213
Samuel Tan52545f22016-02-12 13:56:17 -0800214void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800215 erase(key);
216 mDoubleVectorMap[key] = value;
217}
218
Samuel Tan52545f22016-02-12 13:56:17 -0800219void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
Samuel Tan0a312022015-11-23 18:22:12 -0800220 erase(key);
221 mStringVectorMap[key] = value;
222}
223
224void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
225 erase(key);
226 mPersistableBundleMap[key] = value;
227}
228
229bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
230 return getValue(key, out, mBoolMap);
231}
232
233bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
234 return getValue(key, out, mIntMap);
235}
236
237bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
238 return getValue(key, out, mLongMap);
239}
240
241bool PersistableBundle::getDouble(const String16& key, double* out) const {
242 return getValue(key, out, mDoubleMap);
243}
244
245bool PersistableBundle::getString(const String16& key, String16* out) const {
246 return getValue(key, out, mStringMap);
247}
248
Samuel Tan52545f22016-02-12 13:56:17 -0800249bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800250 return getValue(key, out, mBoolVectorMap);
251}
252
Samuel Tan52545f22016-02-12 13:56:17 -0800253bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800254 return getValue(key, out, mIntVectorMap);
255}
256
Samuel Tan52545f22016-02-12 13:56:17 -0800257bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800258 return getValue(key, out, mLongVectorMap);
259}
260
Samuel Tan52545f22016-02-12 13:56:17 -0800261bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800262 return getValue(key, out, mDoubleVectorMap);
263}
264
Samuel Tan52545f22016-02-12 13:56:17 -0800265bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
Samuel Tan0a312022015-11-23 18:22:12 -0800266 return getValue(key, out, mStringVectorMap);
267}
268
269bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
270 return getValue(key, out, mPersistableBundleMap);
271}
272
Samuel Tan52545f22016-02-12 13:56:17 -0800273set<String16> PersistableBundle::getBooleanKeys() const {
274 return getKeys(mBoolMap);
275}
276
277set<String16> PersistableBundle::getIntKeys() const {
278 return getKeys(mIntMap);
279}
280
281set<String16> PersistableBundle::getLongKeys() const {
282 return getKeys(mLongMap);
283}
284
285set<String16> PersistableBundle::getDoubleKeys() const {
286 return getKeys(mDoubleMap);
287}
288
289set<String16> PersistableBundle::getStringKeys() const {
290 return getKeys(mStringMap);
291}
292
293set<String16> PersistableBundle::getBooleanVectorKeys() const {
294 return getKeys(mBoolVectorMap);
295}
296
297set<String16> PersistableBundle::getIntVectorKeys() const {
298 return getKeys(mIntVectorMap);
299}
300
301set<String16> PersistableBundle::getLongVectorKeys() const {
302 return getKeys(mLongVectorMap);
303}
304
305set<String16> PersistableBundle::getDoubleVectorKeys() const {
306 return getKeys(mDoubleVectorMap);
307}
308
309set<String16> PersistableBundle::getStringVectorKeys() const {
310 return getKeys(mStringVectorMap);
311}
312
313set<String16> PersistableBundle::getPersistableBundleKeys() const {
314 return getKeys(mPersistableBundleMap);
315}
316
Samuel Tan0a312022015-11-23 18:22:12 -0800317status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
318 /*
319 * To keep this implementation in sync with writeArrayMapInternal() in
320 * frameworks/base/core/java/android/os/Parcel.java, the number of key
321 * value pairs must be written into the parcel before writing the key-value
322 * pairs themselves.
323 */
324 size_t num_entries = size();
Tomasz Wasilczyk052c5132024-06-21 15:45:26 -0700325 if (num_entries > static_cast<size_t>(std::numeric_limits<int32_t>::max())) {
Samuel Tan715dec72015-12-16 17:13:29 -0800326 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
Samuel Tan0a312022015-11-23 18:22:12 -0800327 num_entries);
328 return BAD_VALUE;
329 }
330 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
331
332 for (const auto& key_val_pair : mBoolMap) {
333 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
334 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
335 RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
336 }
337 for (const auto& key_val_pair : mIntMap) {
338 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
339 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
340 RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
341 }
342 for (const auto& key_val_pair : mLongMap) {
343 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
344 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
345 RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
346 }
347 for (const auto& key_val_pair : mDoubleMap) {
348 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
349 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
350 RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
351 }
352 for (const auto& key_val_pair : mStringMap) {
353 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
354 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
355 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
356 }
357 for (const auto& key_val_pair : mBoolVectorMap) {
358 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
359 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
360 RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
361 }
362 for (const auto& key_val_pair : mIntVectorMap) {
363 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
364 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
365 RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
366 }
367 for (const auto& key_val_pair : mLongVectorMap) {
368 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
369 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
370 RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
371 }
372 for (const auto& key_val_pair : mDoubleVectorMap) {
373 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
374 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
375 RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
376 }
377 for (const auto& key_val_pair : mStringVectorMap) {
378 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
379 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
380 RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
381 }
382 for (const auto& key_val_pair : mPersistableBundleMap) {
383 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
384 RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
385 RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
386 }
387 return NO_ERROR;
388}
389
390status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
391 /*
392 * Note: we don't actually use length for anything other than an empty PersistableBundle
393 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
394 * implementation.
395 */
396 if (length == 0) {
397 // Empty PersistableBundle or end of data.
398 return NO_ERROR;
399 }
400
401 int32_t magic;
402 RETURN_IF_FAILED(parcel->readInt32(&magic));
Makoto Onuki39696bd2018-04-26 09:11:28 -0700403 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
Samuel Tan0a312022015-11-23 18:22:12 -0800404 ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
405 return BAD_VALUE;
406 }
407
408 /*
409 * To keep this implementation in sync with unparcel() in
410 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
411 * key-value pairs must be read from the parcel before reading the key-value
412 * pairs themselves.
413 */
414 int32_t num_entries;
415 RETURN_IF_FAILED(parcel->readInt32(&num_entries));
416
417 for (; num_entries > 0; --num_entries) {
Samuel Tan0a312022015-11-23 18:22:12 -0800418 String16 key;
419 int32_t value_type;
420 RETURN_IF_FAILED(parcel->readString16(&key));
421 RETURN_IF_FAILED(parcel->readInt32(&value_type));
422
423 /*
424 * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
425 * are unique.
426 */
427 switch (value_type) {
428 case VAL_STRING: {
429 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
430 break;
431 }
432 case VAL_INTEGER: {
433 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
434 break;
435 }
436 case VAL_LONG: {
437 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
438 break;
439 }
440 case VAL_DOUBLE: {
441 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
442 break;
443 }
444 case VAL_BOOLEAN: {
445 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
446 break;
447 }
448 case VAL_STRINGARRAY: {
449 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
450 break;
451 }
452 case VAL_INTARRAY: {
453 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
454 break;
455 }
456 case VAL_LONGARRAY: {
457 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
458 break;
459 }
460 case VAL_BOOLEANARRAY: {
461 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
462 break;
463 }
464 case VAL_PERSISTABLEBUNDLE: {
465 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
466 break;
467 }
468 case VAL_DOUBLEARRAY: {
469 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
470 break;
471 }
472 default: {
473 ALOGE("Unrecognized type: %d", value_type);
474 return BAD_TYPE;
475 break;
476 }
477 }
478 }
Nan Wue1226b02025-01-27 20:24:48 +0000479 // result intentional ignored since it will always be false;
480 RETURN_IF_FAILED(parcel->readBool());
Samuel Tan0a312022015-11-23 18:22:12 -0800481
482 return NO_ERROR;
483}
484
485} // namespace os
486
487} // namespace android