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