1 /*
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>
25 #include <log/log.h>
26 #include <utils/Errors.h>
27
28 #include "ParcelValTypes.h"
29
30 using android::binder::VAL_BOOLEAN;
31 using android::binder::VAL_INTEGER;
32 using android::binder::VAL_LONG;
33 using android::binder::VAL_DOUBLE;
34 using android::binder::VAL_STRING;
35 using android::binder::VAL_BOOLEANARRAY;
36 using android::binder::VAL_INTARRAY;
37 using android::binder::VAL_LONGARRAY;
38 using android::binder::VAL_DOUBLEARRAY;
39 using android::binder::VAL_STRINGARRAY;
40 using android::binder::VAL_PERSISTABLEBUNDLE;
41
42 using std::map;
43 using std::set;
44 using std::vector;
45
46 enum {
47 // Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
48 BUNDLE_MAGIC = 0x4C444E42,
49 BUNDLE_MAGIC_NATIVE = 0x4C444E44,
50 };
51
52 namespace {
53 template <typename T>
getValue(const android::String16 & key,T * out,const map<android::String16,T> & map)54 bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
55 const auto& it = map.find(key);
56 if (it == map.end()) return false;
57 *out = it->second;
58 return true;
59 }
60
61 template <typename T>
getKeys(const map<android::String16,T> & map)62 set<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 }
70 } // namespace
71
72 namespace android {
73
74 namespace 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 { \
87 size_t num_erased = (map).erase(key); \
88 if (num_erased) { \
89 return num_erased; \
90 } \
91 }
92
writeToParcel(Parcel * parcel) const93 status_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
107 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
108
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;
116 if (length > std::numeric_limits<int32_t>::max()) {
117 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
118 return BAD_VALUE;
119 }
120 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
121 parcel->setDataPosition(end_pos);
122 return NO_ERROR;
123 }
124
readFromParcel(const Parcel * parcel)125 status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
126 /*
127 * Keep implementation in sync with readFromParcelInner() in
128 * frameworks/base/core/java/android/os/BaseBundle.java.
129 */
130 int32_t length = parcel->readInt32();
131 if (length < 0) {
132 ALOGE("Bad length in parcel: %d", length);
133 return UNEXPECTED_NULL;
134 }
135
136 return readFromParcelInner(parcel, static_cast<size_t>(length));
137 }
138
empty() const139 bool PersistableBundle::empty() const {
140 return size() == 0u;
141 }
142
size() const143 size_t PersistableBundle::size() const {
144 return (mBoolMap.size() +
145 mIntMap.size() +
146 mLongMap.size() +
147 mDoubleMap.size() +
148 mStringMap.size() +
149 mBoolVectorMap.size() +
150 mIntVectorMap.size() +
151 mLongVectorMap.size() +
152 mDoubleVectorMap.size() +
153 mStringVectorMap.size() +
154 mPersistableBundleMap.size());
155 }
156
erase(const String16 & key)157 size_t PersistableBundle::erase(const String16& key) {
158 RETURN_IF_ENTRY_ERASED(mBoolMap, key);
159 RETURN_IF_ENTRY_ERASED(mIntMap, key);
160 RETURN_IF_ENTRY_ERASED(mLongMap, key);
161 RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
162 RETURN_IF_ENTRY_ERASED(mStringMap, key);
163 RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
164 RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
165 RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
166 RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
167 RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
168 return mPersistableBundleMap.erase(key);
169 }
170
putBoolean(const String16 & key,bool value)171 void PersistableBundle::putBoolean(const String16& key, bool value) {
172 erase(key);
173 mBoolMap[key] = value;
174 }
175
putInt(const String16 & key,int32_t value)176 void PersistableBundle::putInt(const String16& key, int32_t value) {
177 erase(key);
178 mIntMap[key] = value;
179 }
180
putLong(const String16 & key,int64_t value)181 void PersistableBundle::putLong(const String16& key, int64_t value) {
182 erase(key);
183 mLongMap[key] = value;
184 }
185
putDouble(const String16 & key,double value)186 void PersistableBundle::putDouble(const String16& key, double value) {
187 erase(key);
188 mDoubleMap[key] = value;
189 }
190
putString(const String16 & key,const String16 & value)191 void PersistableBundle::putString(const String16& key, const String16& value) {
192 erase(key);
193 mStringMap[key] = value;
194 }
195
putBooleanVector(const String16 & key,const vector<bool> & value)196 void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
197 erase(key);
198 mBoolVectorMap[key] = value;
199 }
200
putIntVector(const String16 & key,const vector<int32_t> & value)201 void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
202 erase(key);
203 mIntVectorMap[key] = value;
204 }
205
putLongVector(const String16 & key,const vector<int64_t> & value)206 void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
207 erase(key);
208 mLongVectorMap[key] = value;
209 }
210
putDoubleVector(const String16 & key,const vector<double> & value)211 void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
212 erase(key);
213 mDoubleVectorMap[key] = value;
214 }
215
putStringVector(const String16 & key,const vector<String16> & value)216 void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
217 erase(key);
218 mStringVectorMap[key] = value;
219 }
220
putPersistableBundle(const String16 & key,const PersistableBundle & value)221 void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
222 erase(key);
223 mPersistableBundleMap[key] = value;
224 }
225
getBoolean(const String16 & key,bool * out) const226 bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
227 return getValue(key, out, mBoolMap);
228 }
229
getInt(const String16 & key,int32_t * out) const230 bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
231 return getValue(key, out, mIntMap);
232 }
233
getLong(const String16 & key,int64_t * out) const234 bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
235 return getValue(key, out, mLongMap);
236 }
237
getDouble(const String16 & key,double * out) const238 bool PersistableBundle::getDouble(const String16& key, double* out) const {
239 return getValue(key, out, mDoubleMap);
240 }
241
getString(const String16 & key,String16 * out) const242 bool PersistableBundle::getString(const String16& key, String16* out) const {
243 return getValue(key, out, mStringMap);
244 }
245
getBooleanVector(const String16 & key,vector<bool> * out) const246 bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
247 return getValue(key, out, mBoolVectorMap);
248 }
249
getIntVector(const String16 & key,vector<int32_t> * out) const250 bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
251 return getValue(key, out, mIntVectorMap);
252 }
253
getLongVector(const String16 & key,vector<int64_t> * out) const254 bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
255 return getValue(key, out, mLongVectorMap);
256 }
257
getDoubleVector(const String16 & key,vector<double> * out) const258 bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
259 return getValue(key, out, mDoubleVectorMap);
260 }
261
getStringVector(const String16 & key,vector<String16> * out) const262 bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
263 return getValue(key, out, mStringVectorMap);
264 }
265
getPersistableBundle(const String16 & key,PersistableBundle * out) const266 bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
267 return getValue(key, out, mPersistableBundleMap);
268 }
269
getBooleanKeys() const270 set<String16> PersistableBundle::getBooleanKeys() const {
271 return getKeys(mBoolMap);
272 }
273
getIntKeys() const274 set<String16> PersistableBundle::getIntKeys() const {
275 return getKeys(mIntMap);
276 }
277
getLongKeys() const278 set<String16> PersistableBundle::getLongKeys() const {
279 return getKeys(mLongMap);
280 }
281
getDoubleKeys() const282 set<String16> PersistableBundle::getDoubleKeys() const {
283 return getKeys(mDoubleMap);
284 }
285
getStringKeys() const286 set<String16> PersistableBundle::getStringKeys() const {
287 return getKeys(mStringMap);
288 }
289
getBooleanVectorKeys() const290 set<String16> PersistableBundle::getBooleanVectorKeys() const {
291 return getKeys(mBoolVectorMap);
292 }
293
getIntVectorKeys() const294 set<String16> PersistableBundle::getIntVectorKeys() const {
295 return getKeys(mIntVectorMap);
296 }
297
getLongVectorKeys() const298 set<String16> PersistableBundle::getLongVectorKeys() const {
299 return getKeys(mLongVectorMap);
300 }
301
getDoubleVectorKeys() const302 set<String16> PersistableBundle::getDoubleVectorKeys() const {
303 return getKeys(mDoubleVectorMap);
304 }
305
getStringVectorKeys() const306 set<String16> PersistableBundle::getStringVectorKeys() const {
307 return getKeys(mStringVectorMap);
308 }
309
getPersistableBundleKeys() const310 set<String16> PersistableBundle::getPersistableBundleKeys() const {
311 return getKeys(mPersistableBundleMap);
312 }
313
writeToParcelInner(Parcel * parcel) const314 status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
315 /*
316 * To keep this implementation in sync with writeArrayMapInternal() in
317 * frameworks/base/core/java/android/os/Parcel.java, the number of key
318 * value pairs must be written into the parcel before writing the key-value
319 * pairs themselves.
320 */
321 size_t num_entries = size();
322 if (num_entries > std::numeric_limits<int32_t>::max()) {
323 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
324 num_entries);
325 return BAD_VALUE;
326 }
327 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
328
329 for (const auto& key_val_pair : mBoolMap) {
330 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
331 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
332 RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
333 }
334 for (const auto& key_val_pair : mIntMap) {
335 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
336 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
337 RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
338 }
339 for (const auto& key_val_pair : mLongMap) {
340 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
341 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
342 RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
343 }
344 for (const auto& key_val_pair : mDoubleMap) {
345 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
346 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
347 RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
348 }
349 for (const auto& key_val_pair : mStringMap) {
350 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
351 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
352 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
353 }
354 for (const auto& key_val_pair : mBoolVectorMap) {
355 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
356 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
357 RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
358 }
359 for (const auto& key_val_pair : mIntVectorMap) {
360 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
361 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
362 RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
363 }
364 for (const auto& key_val_pair : mLongVectorMap) {
365 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
366 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
367 RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
368 }
369 for (const auto& key_val_pair : mDoubleVectorMap) {
370 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
371 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
372 RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
373 }
374 for (const auto& key_val_pair : mStringVectorMap) {
375 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
376 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
377 RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
378 }
379 for (const auto& key_val_pair : mPersistableBundleMap) {
380 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
381 RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
382 RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
383 }
384 return NO_ERROR;
385 }
386
readFromParcelInner(const Parcel * parcel,size_t length)387 status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
388 /*
389 * Note: we don't actually use length for anything other than an empty PersistableBundle
390 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
391 * implementation.
392 */
393 if (length == 0) {
394 // Empty PersistableBundle or end of data.
395 return NO_ERROR;
396 }
397
398 int32_t magic;
399 RETURN_IF_FAILED(parcel->readInt32(&magic));
400 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
401 ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
402 return BAD_VALUE;
403 }
404
405 /*
406 * To keep this implementation in sync with unparcel() in
407 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
408 * key-value pairs must be read from the parcel before reading the key-value
409 * pairs themselves.
410 */
411 int32_t num_entries;
412 RETURN_IF_FAILED(parcel->readInt32(&num_entries));
413
414 for (; num_entries > 0; --num_entries) {
415 String16 key;
416 int32_t value_type;
417 RETURN_IF_FAILED(parcel->readString16(&key));
418 RETURN_IF_FAILED(parcel->readInt32(&value_type));
419
420 /*
421 * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
422 * are unique.
423 */
424 switch (value_type) {
425 case VAL_STRING: {
426 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
427 break;
428 }
429 case VAL_INTEGER: {
430 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
431 break;
432 }
433 case VAL_LONG: {
434 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
435 break;
436 }
437 case VAL_DOUBLE: {
438 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
439 break;
440 }
441 case VAL_BOOLEAN: {
442 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
443 break;
444 }
445 case VAL_STRINGARRAY: {
446 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
447 break;
448 }
449 case VAL_INTARRAY: {
450 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
451 break;
452 }
453 case VAL_LONGARRAY: {
454 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
455 break;
456 }
457 case VAL_BOOLEANARRAY: {
458 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
459 break;
460 }
461 case VAL_PERSISTABLEBUNDLE: {
462 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
463 break;
464 }
465 case VAL_DOUBLEARRAY: {
466 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
467 break;
468 }
469 default: {
470 ALOGE("Unrecognized type: %d", value_type);
471 return BAD_TYPE;
472 break;
473 }
474 }
475 }
476
477 return NO_ERROR;
478 }
479
480 } // namespace os
481
482 } // namespace android
483