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