2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "PersistableBundle"
19 #include <binder/PersistableBundle.h>
23 #include <binder/IBinder.h>
24 #include <binder/Parcel.h>
26 #include <utils/Errors.h>
28 using android::BAD_TYPE;
29 using android::BAD_VALUE;
30 using android::NO_ERROR;
31 using android::Parcel;
33 using android::status_t;
34 using android::UNEXPECTED_NULL;
37 // Keep in sync with BUNDLE_MAGIC in frameworks/base/core/java/android/os/BaseBundle.java.
38 BUNDLE_MAGIC = 0x4C444E42,
42 // Keep in sync with frameworks/base/core/java/android/os/Parcel.java.
51 VAL_BOOLEANARRAY = 23,
52 VAL_PERSISTABLEBUNDLE = 25,
58 bool getValue(const android::String16& key, T* out, const std::map<android::String16, T>& map) {
59 const auto& it = map.find(key);
60 if (it == map.end()) return false;
70 #define RETURN_IF_FAILED(calledOnce) \
72 status_t returnStatus = calledOnce; \
74 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
75 return returnStatus; \
79 #define RETURN_IF_ENTRY_ERASED(map, key) \
81 size_t num_erased = map.erase(key); \
83 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
88 status_t PersistableBundle::writeToParcel(Parcel* parcel) const {
90 * Keep implementation in sync with writeToParcelInner() in
91 * frameworks/base/core/java/android/os/BaseBundle.java.
94 // Special case for empty bundles.
96 RETURN_IF_FAILED(parcel->writeInt32(0));
100 size_t length_pos = parcel->dataPosition();
101 RETURN_IF_FAILED(parcel->writeInt32(1)); // dummy, will hold length
102 RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC));
104 size_t start_pos = parcel->dataPosition();
105 RETURN_IF_FAILED(writeToParcelInner(parcel));
106 size_t end_pos = parcel->dataPosition();
108 // Backpatch length. This length value includes the length header.
109 parcel->setDataPosition(length_pos);
110 size_t length = end_pos - start_pos;
111 if (length > std::numeric_limits<int32_t>::max()) {
112 ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
115 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
116 parcel->setDataPosition(end_pos);
120 status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
122 * Keep implementation in sync with readFromParcelInner() in
123 * frameworks/base/core/java/android/os/BaseBundle.java.
125 int32_t length = parcel->readInt32();
127 ALOGE("Bad length in parcel: %d", length);
128 return UNEXPECTED_NULL;
131 return readFromParcelInner(parcel, static_cast<size_t>(length));
134 bool PersistableBundle::empty() const {
138 size_t PersistableBundle::size() const {
139 return (mBoolMap.size() +
144 mBoolVectorMap.size() +
145 mIntVectorMap.size() +
146 mLongVectorMap.size() +
147 mDoubleVectorMap.size() +
148 mStringVectorMap.size() +
149 mPersistableBundleMap.size());
152 size_t PersistableBundle::erase(const String16& key) {
153 RETURN_IF_ENTRY_ERASED(mBoolMap, key);
154 RETURN_IF_ENTRY_ERASED(mIntMap, key);
155 RETURN_IF_ENTRY_ERASED(mLongMap, key);
156 RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
157 RETURN_IF_ENTRY_ERASED(mStringMap, key);
158 RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
159 RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
160 RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
161 RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
162 RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
163 return mPersistableBundleMap.erase(key);
166 void PersistableBundle::putBoolean(const String16& key, bool value) {
168 mBoolMap[key] = value;
171 void PersistableBundle::putInt(const String16& key, int32_t value) {
173 mIntMap[key] = value;
176 void PersistableBundle::putLong(const String16& key, int64_t value) {
178 mLongMap[key] = value;
181 void PersistableBundle::putDouble(const String16& key, double value) {
183 mDoubleMap[key] = value;
186 void PersistableBundle::putString(const String16& key, const String16& value) {
188 mStringMap[key] = value;
191 void PersistableBundle::putBooleanVector(const String16& key, const std::vector<bool>& value) {
193 mBoolVectorMap[key] = value;
196 void PersistableBundle::putIntVector(const String16& key, const std::vector<int32_t>& value) {
198 mIntVectorMap[key] = value;
201 void PersistableBundle::putLongVector(const String16& key, const std::vector<int64_t>& value) {
203 mLongVectorMap[key] = value;
206 void PersistableBundle::putDoubleVector(const String16& key, const std::vector<double>& value) {
208 mDoubleVectorMap[key] = value;
211 void PersistableBundle::putStringVector(const String16& key, const std::vector<String16>& value) {
213 mStringVectorMap[key] = value;
216 void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
218 mPersistableBundleMap[key] = value;
221 bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
222 return getValue(key, out, mBoolMap);
225 bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
226 return getValue(key, out, mIntMap);
229 bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
230 return getValue(key, out, mLongMap);
233 bool PersistableBundle::getDouble(const String16& key, double* out) const {
234 return getValue(key, out, mDoubleMap);
237 bool PersistableBundle::getString(const String16& key, String16* out) const {
238 return getValue(key, out, mStringMap);
241 bool PersistableBundle::getBooleanVector(const String16& key, std::vector<bool>* out) const {
242 return getValue(key, out, mBoolVectorMap);
245 bool PersistableBundle::getIntVector(const String16& key, std::vector<int32_t>* out) const {
246 return getValue(key, out, mIntVectorMap);
249 bool PersistableBundle::getLongVector(const String16& key, std::vector<int64_t>* out) const {
250 return getValue(key, out, mLongVectorMap);
253 bool PersistableBundle::getDoubleVector(const String16& key, std::vector<double>* out) const {
254 return getValue(key, out, mDoubleVectorMap);
257 bool PersistableBundle::getStringVector(const String16& key, std::vector<String16>* out) const {
258 return getValue(key, out, mStringVectorMap);
261 bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
262 return getValue(key, out, mPersistableBundleMap);
265 status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
267 * To keep this implementation in sync with writeArrayMapInternal() in
268 * frameworks/base/core/java/android/os/Parcel.java, the number of key
269 * value pairs must be written into the parcel before writing the key-value
272 size_t num_entries = size();
273 if (num_entries > std::numeric_limits<int32_t>::max()) {
274 ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
278 RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
280 for (const auto& key_val_pair : mBoolMap) {
281 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
282 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
283 RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
285 for (const auto& key_val_pair : mIntMap) {
286 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
287 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
288 RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
290 for (const auto& key_val_pair : mLongMap) {
291 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
292 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
293 RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
295 for (const auto& key_val_pair : mDoubleMap) {
296 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
297 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
298 RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
300 for (const auto& key_val_pair : mStringMap) {
301 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
302 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
303 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
305 for (const auto& key_val_pair : mBoolVectorMap) {
306 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
307 RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
308 RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
310 for (const auto& key_val_pair : mIntVectorMap) {
311 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
312 RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
313 RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
315 for (const auto& key_val_pair : mLongVectorMap) {
316 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
317 RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
318 RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
320 for (const auto& key_val_pair : mDoubleVectorMap) {
321 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
322 RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
323 RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
325 for (const auto& key_val_pair : mStringVectorMap) {
326 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
327 RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
328 RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
330 for (const auto& key_val_pair : mPersistableBundleMap) {
331 RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
332 RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
333 RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
338 status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
340 * Note: we don't actually use length for anything other than an empty PersistableBundle
341 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
345 // Empty PersistableBundle or end of data.
350 RETURN_IF_FAILED(parcel->readInt32(&magic));
351 if (magic != BUNDLE_MAGIC) {
352 ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
357 * To keep this implementation in sync with unparcel() in
358 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
359 * key-value pairs must be read from the parcel before reading the key-value
363 RETURN_IF_FAILED(parcel->readInt32(&num_entries));
365 for (; num_entries > 0; --num_entries) {
366 size_t start_pos = parcel->dataPosition();
369 RETURN_IF_FAILED(parcel->readString16(&key));
370 RETURN_IF_FAILED(parcel->readInt32(&value_type));
373 * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
376 switch (value_type) {
378 RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
382 RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
386 RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
390 RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
394 RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
397 case VAL_STRINGARRAY: {
398 RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
402 RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
405 case VAL_LONGARRAY: {
406 RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
409 case VAL_BOOLEANARRAY: {
410 RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
413 case VAL_PERSISTABLEBUNDLE: {
414 RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
417 case VAL_DOUBLEARRAY: {
418 RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
422 ALOGE("Unrecognized type: %d", value_type);
434 } // namespace android