• 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             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 > static_cast<size_t>(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     // write mHasIntent to be consistent with BaseBundle.writeToBundle. But it would always be
123     // false since PersistableBundle won't contain an intent.
124     RETURN_IF_FAILED(parcel->writeBool(false));
125     return NO_ERROR;
126 }
127 
readFromParcel(const Parcel * parcel)128 status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
129     /*
130      * Keep implementation in sync with readFromParcelInner() in
131      * frameworks/base/core/java/android/os/BaseBundle.java.
132      */
133     int32_t length = parcel->readInt32();
134     if (length < 0) {
135         ALOGE("Bad length in parcel: %d", length);
136         return UNEXPECTED_NULL;
137     }
138 
139     return readFromParcelInner(parcel, static_cast<size_t>(length));
140 }
141 
empty() const142 bool PersistableBundle::empty() const {
143     return size() == 0u;
144 }
145 
size() const146 size_t PersistableBundle::size() const {
147     return (mBoolMap.size() +
148             mIntMap.size() +
149             mLongMap.size() +
150             mDoubleMap.size() +
151             mStringMap.size() +
152             mBoolVectorMap.size() +
153             mIntVectorMap.size() +
154             mLongVectorMap.size() +
155             mDoubleVectorMap.size() +
156             mStringVectorMap.size() +
157             mPersistableBundleMap.size());
158 }
159 
erase(const String16 & key)160 size_t PersistableBundle::erase(const String16& key) {
161     RETURN_IF_ENTRY_ERASED(mBoolMap, key);
162     RETURN_IF_ENTRY_ERASED(mIntMap, key);
163     RETURN_IF_ENTRY_ERASED(mLongMap, key);
164     RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
165     RETURN_IF_ENTRY_ERASED(mStringMap, key);
166     RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
167     RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
168     RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
169     RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
170     RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
171     return mPersistableBundleMap.erase(key);
172 }
173 
putBoolean(const String16 & key,bool value)174 void PersistableBundle::putBoolean(const String16& key, bool value) {
175     erase(key);
176     mBoolMap[key] = value;
177 }
178 
putInt(const String16 & key,int32_t value)179 void PersistableBundle::putInt(const String16& key, int32_t value) {
180     erase(key);
181     mIntMap[key] = value;
182 }
183 
putLong(const String16 & key,int64_t value)184 void PersistableBundle::putLong(const String16& key, int64_t value) {
185     erase(key);
186     mLongMap[key] = value;
187 }
188 
putDouble(const String16 & key,double value)189 void PersistableBundle::putDouble(const String16& key, double value) {
190     erase(key);
191     mDoubleMap[key] = value;
192 }
193 
putString(const String16 & key,const String16 & value)194 void PersistableBundle::putString(const String16& key, const String16& value) {
195     erase(key);
196     mStringMap[key] = value;
197 }
198 
putBooleanVector(const String16 & key,const vector<bool> & value)199 void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
200     erase(key);
201     mBoolVectorMap[key] = value;
202 }
203 
putIntVector(const String16 & key,const vector<int32_t> & value)204 void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
205     erase(key);
206     mIntVectorMap[key] = value;
207 }
208 
putLongVector(const String16 & key,const vector<int64_t> & value)209 void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
210     erase(key);
211     mLongVectorMap[key] = value;
212 }
213 
putDoubleVector(const String16 & key,const vector<double> & value)214 void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
215     erase(key);
216     mDoubleVectorMap[key] = value;
217 }
218 
putStringVector(const String16 & key,const vector<String16> & value)219 void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
220     erase(key);
221     mStringVectorMap[key] = value;
222 }
223 
putPersistableBundle(const String16 & key,const PersistableBundle & value)224 void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
225     erase(key);
226     mPersistableBundleMap[key] = value;
227 }
228 
getBoolean(const String16 & key,bool * out) const229 bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
230     return getValue(key, out, mBoolMap);
231 }
232 
getInt(const String16 & key,int32_t * out) const233 bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
234     return getValue(key, out, mIntMap);
235 }
236 
getLong(const String16 & key,int64_t * out) const237 bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
238     return getValue(key, out, mLongMap);
239 }
240 
getDouble(const String16 & key,double * out) const241 bool PersistableBundle::getDouble(const String16& key, double* out) const {
242     return getValue(key, out, mDoubleMap);
243 }
244 
getString(const String16 & key,String16 * out) const245 bool PersistableBundle::getString(const String16& key, String16* out) const {
246     return getValue(key, out, mStringMap);
247 }
248 
getBooleanVector(const String16 & key,vector<bool> * out) const249 bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
250     return getValue(key, out, mBoolVectorMap);
251 }
252 
getIntVector(const String16 & key,vector<int32_t> * out) const253 bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
254     return getValue(key, out, mIntVectorMap);
255 }
256 
getLongVector(const String16 & key,vector<int64_t> * out) const257 bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
258     return getValue(key, out, mLongVectorMap);
259 }
260 
getDoubleVector(const String16 & key,vector<double> * out) const261 bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
262     return getValue(key, out, mDoubleVectorMap);
263 }
264 
getStringVector(const String16 & key,vector<String16> * out) const265 bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
266     return getValue(key, out, mStringVectorMap);
267 }
268 
getPersistableBundle(const String16 & key,PersistableBundle * out) const269 bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
270     return getValue(key, out, mPersistableBundleMap);
271 }
272 
getBooleanKeys() const273 set<String16> PersistableBundle::getBooleanKeys() const {
274     return getKeys(mBoolMap);
275 }
276 
getIntKeys() const277 set<String16> PersistableBundle::getIntKeys() const {
278     return getKeys(mIntMap);
279 }
280 
getLongKeys() const281 set<String16> PersistableBundle::getLongKeys() const {
282     return getKeys(mLongMap);
283 }
284 
getDoubleKeys() const285 set<String16> PersistableBundle::getDoubleKeys() const {
286     return getKeys(mDoubleMap);
287 }
288 
getStringKeys() const289 set<String16> PersistableBundle::getStringKeys() const {
290     return getKeys(mStringMap);
291 }
292 
getBooleanVectorKeys() const293 set<String16> PersistableBundle::getBooleanVectorKeys() const {
294     return getKeys(mBoolVectorMap);
295 }
296 
getIntVectorKeys() const297 set<String16> PersistableBundle::getIntVectorKeys() const {
298     return getKeys(mIntVectorMap);
299 }
300 
getLongVectorKeys() const301 set<String16> PersistableBundle::getLongVectorKeys() const {
302     return getKeys(mLongVectorMap);
303 }
304 
getDoubleVectorKeys() const305 set<String16> PersistableBundle::getDoubleVectorKeys() const {
306     return getKeys(mDoubleVectorMap);
307 }
308 
getStringVectorKeys() const309 set<String16> PersistableBundle::getStringVectorKeys() const {
310     return getKeys(mStringVectorMap);
311 }
312 
getPersistableBundleKeys() const313 set<String16> PersistableBundle::getPersistableBundleKeys() const {
314     return getKeys(mPersistableBundleMap);
315 }
316 
writeToParcelInner(Parcel * parcel) const317 status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
318     /*
319      * To keep this implementation in sync with writeArrayMapInternal() in
320      * frameworks/base/core/java/android/os/Parcel.java, the number of key
321      * value pairs must be written into the parcel before writing the key-value
322      * pairs themselves.
323      */
324     size_t num_entries = size();
325     if (num_entries > static_cast<size_t>(std::numeric_limits<int32_t>::max())) {
326         ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
327               num_entries);
328         return BAD_VALUE;
329     }
330     RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
331 
332     for (const auto& key_val_pair : mBoolMap) {
333         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
334         RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
335         RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
336     }
337     for (const auto& key_val_pair : mIntMap) {
338         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
339         RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
340         RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
341     }
342     for (const auto& key_val_pair : mLongMap) {
343         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
344         RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
345         RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
346     }
347     for (const auto& key_val_pair : mDoubleMap) {
348         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
349         RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
350         RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
351     }
352     for (const auto& key_val_pair : mStringMap) {
353         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
354         RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
355         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
356     }
357     for (const auto& key_val_pair : mBoolVectorMap) {
358         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
359         RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
360         RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
361     }
362     for (const auto& key_val_pair : mIntVectorMap) {
363         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
364         RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
365         RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
366     }
367     for (const auto& key_val_pair : mLongVectorMap) {
368         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
369         RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
370         RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
371     }
372     for (const auto& key_val_pair : mDoubleVectorMap) {
373         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
374         RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
375         RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
376     }
377     for (const auto& key_val_pair : mStringVectorMap) {
378         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
379         RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
380         RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
381     }
382     for (const auto& key_val_pair : mPersistableBundleMap) {
383         RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
384         RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
385         RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
386     }
387     return NO_ERROR;
388 }
389 
readFromParcelInner(const Parcel * parcel,size_t length)390 status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
391     /*
392      * Note: we don't actually use length for anything other than an empty PersistableBundle
393      * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
394      * implementation.
395      */
396     if (length == 0) {
397         // Empty PersistableBundle or end of data.
398         return NO_ERROR;
399     }
400 
401     int32_t magic;
402     RETURN_IF_FAILED(parcel->readInt32(&magic));
403     if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
404         ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
405         return BAD_VALUE;
406     }
407 
408     /*
409      * To keep this implementation in sync with unparcel() in
410      * frameworks/base/core/java/android/os/BaseBundle.java, the number of
411      * key-value pairs must be read from the parcel before reading the key-value
412      * pairs themselves.
413      */
414     int32_t num_entries;
415     RETURN_IF_FAILED(parcel->readInt32(&num_entries));
416 
417     for (; num_entries > 0; --num_entries) {
418         String16 key;
419         int32_t value_type;
420         RETURN_IF_FAILED(parcel->readString16(&key));
421         RETURN_IF_FAILED(parcel->readInt32(&value_type));
422 
423         /*
424          * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
425          * are unique.
426          */
427         switch (value_type) {
428             case VAL_STRING: {
429                 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
430                 break;
431             }
432             case VAL_INTEGER: {
433                 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
434                 break;
435             }
436             case VAL_LONG: {
437                 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
438                 break;
439             }
440             case VAL_DOUBLE: {
441                 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
442                 break;
443             }
444             case VAL_BOOLEAN: {
445                 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
446                 break;
447             }
448             case VAL_STRINGARRAY: {
449                 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
450                 break;
451             }
452             case VAL_INTARRAY: {
453                 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
454                 break;
455             }
456             case VAL_LONGARRAY: {
457                 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
458                 break;
459             }
460             case VAL_BOOLEANARRAY: {
461                 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
462                 break;
463             }
464             case VAL_PERSISTABLEBUNDLE: {
465                 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
466                 break;
467             }
468             case VAL_DOUBLEARRAY: {
469                 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
470                 break;
471             }
472             default: {
473                 ALOGE("Unrecognized type: %d", value_type);
474                 return BAD_TYPE;
475                 break;
476             }
477         }
478     }
479     // result intentional ignored since it will always be false;
480     RETURN_IF_FAILED(parcel->readBool());
481 
482     return NO_ERROR;
483 }
484 
485 }  // namespace os
486 
487 }  // namespace android
488