• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
90             return num_erased;                                           \
91          }                                                               \
92     }
93 
writeToParcel(Parcel * parcel) const94 status_t PersistableBundle::writeToParcel(Parcel* parcel) const {
95     /*
96      * Keep implementation in sync with writeToParcelInner() in
97      * frameworks/base/core/java/android/os/BaseBundle.java.
98      */
99 
100     // Special case for empty bundles.
101     if (empty()) {
102         RETURN_IF_FAILED(parcel->writeInt32(0));
103         return NO_ERROR;
104     }
105 
106     size_t length_pos = parcel->dataPosition();
107     RETURN_IF_FAILED(parcel->writeInt32(1));  // dummy, will hold length
108     RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
109 
110     size_t start_pos = parcel->dataPosition();
111     RETURN_IF_FAILED(writeToParcelInner(parcel));
112     size_t end_pos = parcel->dataPosition();
113 
114     // Backpatch length. This length value includes the length header.
115     parcel->setDataPosition(length_pos);
116     size_t length = end_pos - start_pos;
117     if (length > std::numeric_limits<int32_t>::max()) {
118         ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
119         return BAD_VALUE;
120     }
121     RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
122     parcel->setDataPosition(end_pos);
123     return NO_ERROR;
124 }
125 
readFromParcel(const Parcel * parcel)126 status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
127     /*
128      * Keep implementation in sync with readFromParcelInner() in
129      * frameworks/base/core/java/android/os/BaseBundle.java.
130      */
131     int32_t length = parcel->readInt32();
132     if (length < 0) {
133         ALOGE("Bad length in parcel: %d", length);
134         return UNEXPECTED_NULL;
135     }
136 
137     return readFromParcelInner(parcel, static_cast<size_t>(length));
138 }
139 
empty() const140 bool PersistableBundle::empty() const {
141     return size() == 0u;
142 }
143 
size() const144 size_t PersistableBundle::size() const {
145     return (mBoolMap.size() +
146             mIntMap.size() +
147             mLongMap.size() +
148             mDoubleMap.size() +
149             mStringMap.size() +
150             mBoolVectorMap.size() +
151             mIntVectorMap.size() +
152             mLongVectorMap.size() +
153             mDoubleVectorMap.size() +
154             mStringVectorMap.size() +
155             mPersistableBundleMap.size());
156 }
157 
erase(const String16 & key)158 size_t PersistableBundle::erase(const String16& key) {
159     RETURN_IF_ENTRY_ERASED(mBoolMap, key);
160     RETURN_IF_ENTRY_ERASED(mIntMap, key);
161     RETURN_IF_ENTRY_ERASED(mLongMap, key);
162     RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
163     RETURN_IF_ENTRY_ERASED(mStringMap, key);
164     RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
165     RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
166     RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
167     RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
168     RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
169     return mPersistableBundleMap.erase(key);
170 }
171 
putBoolean(const String16 & key,bool value)172 void PersistableBundle::putBoolean(const String16& key, bool value) {
173     erase(key);
174     mBoolMap[key] = value;
175 }
176 
putInt(const String16 & key,int32_t value)177 void PersistableBundle::putInt(const String16& key, int32_t value) {
178     erase(key);
179     mIntMap[key] = value;
180 }
181 
putLong(const String16 & key,int64_t value)182 void PersistableBundle::putLong(const String16& key, int64_t value) {
183     erase(key);
184     mLongMap[key] = value;
185 }
186 
putDouble(const String16 & key,double value)187 void PersistableBundle::putDouble(const String16& key, double value) {
188     erase(key);
189     mDoubleMap[key] = value;
190 }
191 
putString(const String16 & key,const String16 & value)192 void PersistableBundle::putString(const String16& key, const String16& value) {
193     erase(key);
194     mStringMap[key] = value;
195 }
196 
putBooleanVector(const String16 & key,const vector<bool> & value)197 void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
198     erase(key);
199     mBoolVectorMap[key] = value;
200 }
201 
putIntVector(const String16 & key,const vector<int32_t> & value)202 void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
203     erase(key);
204     mIntVectorMap[key] = value;
205 }
206 
putLongVector(const String16 & key,const vector<int64_t> & value)207 void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
208     erase(key);
209     mLongVectorMap[key] = value;
210 }
211 
putDoubleVector(const String16 & key,const vector<double> & value)212 void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
213     erase(key);
214     mDoubleVectorMap[key] = value;
215 }
216 
putStringVector(const String16 & key,const vector<String16> & value)217 void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
218     erase(key);
219     mStringVectorMap[key] = value;
220 }
221 
putPersistableBundle(const String16 & key,const PersistableBundle & value)222 void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
223     erase(key);
224     mPersistableBundleMap[key] = value;
225 }
226 
getBoolean(const String16 & key,bool * out) const227 bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
228     return getValue(key, out, mBoolMap);
229 }
230 
getInt(const String16 & key,int32_t * out) const231 bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
232     return getValue(key, out, mIntMap);
233 }
234 
getLong(const String16 & key,int64_t * out) const235 bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
236     return getValue(key, out, mLongMap);
237 }
238 
getDouble(const String16 & key,double * out) const239 bool PersistableBundle::getDouble(const String16& key, double* out) const {
240     return getValue(key, out, mDoubleMap);
241 }
242 
getString(const String16 & key,String16 * out) const243 bool PersistableBundle::getString(const String16& key, String16* out) const {
244     return getValue(key, out, mStringMap);
245 }
246 
getBooleanVector(const String16 & key,vector<bool> * out) const247 bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
248     return getValue(key, out, mBoolVectorMap);
249 }
250 
getIntVector(const String16 & key,vector<int32_t> * out) const251 bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
252     return getValue(key, out, mIntVectorMap);
253 }
254 
getLongVector(const String16 & key,vector<int64_t> * out) const255 bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
256     return getValue(key, out, mLongVectorMap);
257 }
258 
getDoubleVector(const String16 & key,vector<double> * out) const259 bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
260     return getValue(key, out, mDoubleVectorMap);
261 }
262 
getStringVector(const String16 & key,vector<String16> * out) const263 bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
264     return getValue(key, out, mStringVectorMap);
265 }
266 
getPersistableBundle(const String16 & key,PersistableBundle * out) const267 bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
268     return getValue(key, out, mPersistableBundleMap);
269 }
270 
getBooleanKeys() const271 set<String16> PersistableBundle::getBooleanKeys() const {
272     return getKeys(mBoolMap);
273 }
274 
getIntKeys() const275 set<String16> PersistableBundle::getIntKeys() const {
276     return getKeys(mIntMap);
277 }
278 
getLongKeys() const279 set<String16> PersistableBundle::getLongKeys() const {
280     return getKeys(mLongMap);
281 }
282 
getDoubleKeys() const283 set<String16> PersistableBundle::getDoubleKeys() const {
284     return getKeys(mDoubleMap);
285 }
286 
getStringKeys() const287 set<String16> PersistableBundle::getStringKeys() const {
288     return getKeys(mStringMap);
289 }
290 
getBooleanVectorKeys() const291 set<String16> PersistableBundle::getBooleanVectorKeys() const {
292     return getKeys(mBoolVectorMap);
293 }
294 
getIntVectorKeys() const295 set<String16> PersistableBundle::getIntVectorKeys() const {
296     return getKeys(mIntVectorMap);
297 }
298 
getLongVectorKeys() const299 set<String16> PersistableBundle::getLongVectorKeys() const {
300     return getKeys(mLongVectorMap);
301 }
302 
getDoubleVectorKeys() const303 set<String16> PersistableBundle::getDoubleVectorKeys() const {
304     return getKeys(mDoubleVectorMap);
305 }
306 
getStringVectorKeys() const307 set<String16> PersistableBundle::getStringVectorKeys() const {
308     return getKeys(mStringVectorMap);
309 }
310 
getPersistableBundleKeys() const311 set<String16> PersistableBundle::getPersistableBundleKeys() const {
312     return getKeys(mPersistableBundleMap);
313 }
314 
writeToParcelInner(Parcel * parcel) const315 status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
316     /*
317      * To keep this implementation in sync with writeArrayMapInternal() in
318      * frameworks/base/core/java/android/os/Parcel.java, the number of key
319      * value pairs must be written into the parcel before writing the key-value
320      * pairs themselves.
321      */
322     size_t num_entries = size();
323     if (num_entries > std::numeric_limits<int32_t>::max()) {
324         ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
325               num_entries);
326         return BAD_VALUE;
327     }
328     RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
329 
330     for (const auto& key_val_pair : mBoolMap) {
331         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
332         RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
333         RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
334     }
335     for (const auto& key_val_pair : mIntMap) {
336         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
337         RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
338         RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
339     }
340     for (const auto& key_val_pair : mLongMap) {
341         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
342         RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
343         RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
344     }
345     for (const auto& key_val_pair : mDoubleMap) {
346         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
347         RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
348         RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
349     }
350     for (const auto& key_val_pair : mStringMap) {
351         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
352         RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
353         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
354     }
355     for (const auto& key_val_pair : mBoolVectorMap) {
356         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
357         RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
358         RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
359     }
360     for (const auto& key_val_pair : mIntVectorMap) {
361         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
362         RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
363         RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
364     }
365     for (const auto& key_val_pair : mLongVectorMap) {
366         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
367         RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
368         RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
369     }
370     for (const auto& key_val_pair : mDoubleVectorMap) {
371         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
372         RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
373         RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
374     }
375     for (const auto& key_val_pair : mStringVectorMap) {
376         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
377         RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
378         RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
379     }
380     for (const auto& key_val_pair : mPersistableBundleMap) {
381         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
382         RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
383         RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
384     }
385     return NO_ERROR;
386 }
387 
readFromParcelInner(const Parcel * parcel,size_t length)388 status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
389     /*
390      * Note: we don't actually use length for anything other than an empty PersistableBundle
391      * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
392      * implementation.
393      */
394     if (length == 0) {
395         // Empty PersistableBundle or end of data.
396         return NO_ERROR;
397     }
398 
399     int32_t magic;
400     RETURN_IF_FAILED(parcel->readInt32(&magic));
401     if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
402         ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
403         return BAD_VALUE;
404     }
405 
406     /*
407      * To keep this implementation in sync with unparcel() in
408      * frameworks/base/core/java/android/os/BaseBundle.java, the number of
409      * key-value pairs must be read from the parcel before reading the key-value
410      * pairs themselves.
411      */
412     int32_t num_entries;
413     RETURN_IF_FAILED(parcel->readInt32(&num_entries));
414 
415     for (; num_entries > 0; --num_entries) {
416         String16 key;
417         int32_t value_type;
418         RETURN_IF_FAILED(parcel->readString16(&key));
419         RETURN_IF_FAILED(parcel->readInt32(&value_type));
420 
421         /*
422          * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
423          * are unique.
424          */
425         switch (value_type) {
426             case VAL_STRING: {
427                 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
428                 break;
429             }
430             case VAL_INTEGER: {
431                 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
432                 break;
433             }
434             case VAL_LONG: {
435                 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
436                 break;
437             }
438             case VAL_DOUBLE: {
439                 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
440                 break;
441             }
442             case VAL_BOOLEAN: {
443                 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
444                 break;
445             }
446             case VAL_STRINGARRAY: {
447                 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
448                 break;
449             }
450             case VAL_INTARRAY: {
451                 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
452                 break;
453             }
454             case VAL_LONGARRAY: {
455                 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
456                 break;
457             }
458             case VAL_BOOLEANARRAY: {
459                 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
460                 break;
461             }
462             case VAL_PERSISTABLEBUNDLE: {
463                 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
464                 break;
465             }
466             case VAL_DOUBLEARRAY: {
467                 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
468                 break;
469             }
470             default: {
471                 ALOGE("Unrecognized type: %d", value_type);
472                 return BAD_TYPE;
473                 break;
474             }
475         }
476     }
477 
478     return NO_ERROR;
479 }
480 
481 }  // namespace os
482 
483 }  // namespace android
484