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::BAD_TYPE;
31 using android::BAD_VALUE;
32 using android::NO_ERROR;
33 using android::Parcel;
34 using android::sp;
35 using android::status_t;
36 using android::UNEXPECTED_NULL;
37 using std::map;
38 using std::set;
39 using std::vector;
40 using namespace ::android::binder;
41
42 enum {
43 // Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
44 BUNDLE_MAGIC = 0x4C444E42,
45 BUNDLE_MAGIC_NATIVE = 0x4C444E44,
46 };
47
48 namespace {
49 template <typename T>
getValue(const android::String16 & key,T * out,const map<android::String16,T> & map)50 bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
51 const auto& it = map.find(key);
52 if (it == map.end()) return false;
53 *out = it->second;
54 return true;
55 }
56
57 template <typename T>
getKeys(const map<android::String16,T> & map)58 set<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 }
66 } // namespace
67
68 namespace android {
69
70 namespace 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 { \
83 size_t num_erased = (map).erase(key); \
84 if (num_erased) { \
85 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
86 return num_erased; \
87 } \
88 }
89
writeToParcel(Parcel * parcel) const90 status_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
104 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
105
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()) {
114 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
115 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
readFromParcel(const Parcel * parcel)122 status_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
empty() const136 bool PersistableBundle::empty() const {
137 return size() == 0u;
138 }
139
size() const140 size_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
erase(const String16 & key)154 size_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
putBoolean(const String16 & key,bool value)168 void PersistableBundle::putBoolean(const String16& key, bool value) {
169 erase(key);
170 mBoolMap[key] = value;
171 }
172
putInt(const String16 & key,int32_t value)173 void PersistableBundle::putInt(const String16& key, int32_t value) {
174 erase(key);
175 mIntMap[key] = value;
176 }
177
putLong(const String16 & key,int64_t value)178 void PersistableBundle::putLong(const String16& key, int64_t value) {
179 erase(key);
180 mLongMap[key] = value;
181 }
182
putDouble(const String16 & key,double value)183 void PersistableBundle::putDouble(const String16& key, double value) {
184 erase(key);
185 mDoubleMap[key] = value;
186 }
187
putString(const String16 & key,const String16 & value)188 void PersistableBundle::putString(const String16& key, const String16& value) {
189 erase(key);
190 mStringMap[key] = value;
191 }
192
putBooleanVector(const String16 & key,const vector<bool> & value)193 void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
194 erase(key);
195 mBoolVectorMap[key] = value;
196 }
197
putIntVector(const String16 & key,const vector<int32_t> & value)198 void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
199 erase(key);
200 mIntVectorMap[key] = value;
201 }
202
putLongVector(const String16 & key,const vector<int64_t> & value)203 void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
204 erase(key);
205 mLongVectorMap[key] = value;
206 }
207
putDoubleVector(const String16 & key,const vector<double> & value)208 void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
209 erase(key);
210 mDoubleVectorMap[key] = value;
211 }
212
putStringVector(const String16 & key,const vector<String16> & value)213 void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
214 erase(key);
215 mStringVectorMap[key] = value;
216 }
217
putPersistableBundle(const String16 & key,const PersistableBundle & value)218 void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
219 erase(key);
220 mPersistableBundleMap[key] = value;
221 }
222
getBoolean(const String16 & key,bool * out) const223 bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
224 return getValue(key, out, mBoolMap);
225 }
226
getInt(const String16 & key,int32_t * out) const227 bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
228 return getValue(key, out, mIntMap);
229 }
230
getLong(const String16 & key,int64_t * out) const231 bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
232 return getValue(key, out, mLongMap);
233 }
234
getDouble(const String16 & key,double * out) const235 bool PersistableBundle::getDouble(const String16& key, double* out) const {
236 return getValue(key, out, mDoubleMap);
237 }
238
getString(const String16 & key,String16 * out) const239 bool PersistableBundle::getString(const String16& key, String16* out) const {
240 return getValue(key, out, mStringMap);
241 }
242
getBooleanVector(const String16 & key,vector<bool> * out) const243 bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
244 return getValue(key, out, mBoolVectorMap);
245 }
246
getIntVector(const String16 & key,vector<int32_t> * out) const247 bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
248 return getValue(key, out, mIntVectorMap);
249 }
250
getLongVector(const String16 & key,vector<int64_t> * out) const251 bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
252 return getValue(key, out, mLongVectorMap);
253 }
254
getDoubleVector(const String16 & key,vector<double> * out) const255 bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
256 return getValue(key, out, mDoubleVectorMap);
257 }
258
getStringVector(const String16 & key,vector<String16> * out) const259 bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
260 return getValue(key, out, mStringVectorMap);
261 }
262
getPersistableBundle(const String16 & key,PersistableBundle * out) const263 bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
264 return getValue(key, out, mPersistableBundleMap);
265 }
266
getBooleanKeys() const267 set<String16> PersistableBundle::getBooleanKeys() const {
268 return getKeys(mBoolMap);
269 }
270
getIntKeys() const271 set<String16> PersistableBundle::getIntKeys() const {
272 return getKeys(mIntMap);
273 }
274
getLongKeys() const275 set<String16> PersistableBundle::getLongKeys() const {
276 return getKeys(mLongMap);
277 }
278
getDoubleKeys() const279 set<String16> PersistableBundle::getDoubleKeys() const {
280 return getKeys(mDoubleMap);
281 }
282
getStringKeys() const283 set<String16> PersistableBundle::getStringKeys() const {
284 return getKeys(mStringMap);
285 }
286
getBooleanVectorKeys() const287 set<String16> PersistableBundle::getBooleanVectorKeys() const {
288 return getKeys(mBoolVectorMap);
289 }
290
getIntVectorKeys() const291 set<String16> PersistableBundle::getIntVectorKeys() const {
292 return getKeys(mIntVectorMap);
293 }
294
getLongVectorKeys() const295 set<String16> PersistableBundle::getLongVectorKeys() const {
296 return getKeys(mLongVectorMap);
297 }
298
getDoubleVectorKeys() const299 set<String16> PersistableBundle::getDoubleVectorKeys() const {
300 return getKeys(mDoubleVectorMap);
301 }
302
getStringVectorKeys() const303 set<String16> PersistableBundle::getStringVectorKeys() const {
304 return getKeys(mStringVectorMap);
305 }
306
getPersistableBundleKeys() const307 set<String16> PersistableBundle::getPersistableBundleKeys() const {
308 return getKeys(mPersistableBundleMap);
309 }
310
writeToParcelInner(Parcel * parcel) const311 status_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()) {
320 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
321 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
readFromParcelInner(const Parcel * parcel,size_t length)384 status_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));
397 if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
398 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) {
412 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
480