PersistableBundle.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. #define LOG_TAG "PersistableBundle"
  17. #include <binder/PersistableBundle.h>
  18. #include <private/binder/ParcelValTypes.h>
  19. #include <limits>
  20. #include <binder/IBinder.h>
  21. #include <binder/Parcel.h>
  22. #include <log/log.h>
  23. #include <utils/Errors.h>
  24. using android::BAD_TYPE;
  25. using android::BAD_VALUE;
  26. using android::NO_ERROR;
  27. using android::Parcel;
  28. using android::sp;
  29. using android::status_t;
  30. using android::UNEXPECTED_NULL;
  31. using std::map;
  32. using std::set;
  33. using std::vector;
  34. using namespace ::android::binder;
  35. enum {
  36. // Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
  37. BUNDLE_MAGIC = 0x4C444E42,
  38. BUNDLE_MAGIC_NATIVE = 0x4C444E44,
  39. };
  40. namespace {
  41. template <typename T>
  42. bool getValue(const android::String16& key, T* out, const map<android::String16, T>& map) {
  43. const auto& it = map.find(key);
  44. if (it == map.end()) return false;
  45. *out = it->second;
  46. return true;
  47. }
  48. template <typename T>
  49. set<android::String16> getKeys(const map<android::String16, T>& map) {
  50. if (map.empty()) return set<android::String16>();
  51. set<android::String16> keys;
  52. for (const auto& key_value_pair : map) {
  53. keys.emplace(key_value_pair.first);
  54. }
  55. return keys;
  56. }
  57. } // namespace
  58. namespace android {
  59. namespace os {
  60. #define RETURN_IF_FAILED(calledOnce) \
  61. { \
  62. status_t returnStatus = calledOnce; \
  63. if (returnStatus) { \
  64. ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
  65. return returnStatus; \
  66. } \
  67. }
  68. #define RETURN_IF_ENTRY_ERASED(map, key) \
  69. { \
  70. size_t num_erased = (map).erase(key); \
  71. if (num_erased) { \
  72. ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
  73. return num_erased; \
  74. } \
  75. }
  76. status_t PersistableBundle::writeToParcel(Parcel* parcel) const {
  77. /*
  78. * Keep implementation in sync with writeToParcelInner() in
  79. * frameworks/base/core/java/android/os/BaseBundle.java.
  80. */
  81. // Special case for empty bundles.
  82. if (empty()) {
  83. RETURN_IF_FAILED(parcel->writeInt32(0));
  84. return NO_ERROR;
  85. }
  86. size_t length_pos = parcel->dataPosition();
  87. RETURN_IF_FAILED(parcel->writeInt32(1)); // dummy, will hold length
  88. RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));
  89. size_t start_pos = parcel->dataPosition();
  90. RETURN_IF_FAILED(writeToParcelInner(parcel));
  91. size_t end_pos = parcel->dataPosition();
  92. // Backpatch length. This length value includes the length header.
  93. parcel->setDataPosition(length_pos);
  94. size_t length = end_pos - start_pos;
  95. if (length > std::numeric_limits<int32_t>::max()) {
  96. ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
  97. return BAD_VALUE;
  98. }
  99. RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
  100. parcel->setDataPosition(end_pos);
  101. return NO_ERROR;
  102. }
  103. status_t PersistableBundle::readFromParcel(const Parcel* parcel) {
  104. /*
  105. * Keep implementation in sync with readFromParcelInner() in
  106. * frameworks/base/core/java/android/os/BaseBundle.java.
  107. */
  108. int32_t length = parcel->readInt32();
  109. if (length < 0) {
  110. ALOGE("Bad length in parcel: %d", length);
  111. return UNEXPECTED_NULL;
  112. }
  113. return readFromParcelInner(parcel, static_cast<size_t>(length));
  114. }
  115. bool PersistableBundle::empty() const {
  116. return size() == 0u;
  117. }
  118. size_t PersistableBundle::size() const {
  119. return (mBoolMap.size() +
  120. mIntMap.size() +
  121. mLongMap.size() +
  122. mDoubleMap.size() +
  123. mStringMap.size() +
  124. mBoolVectorMap.size() +
  125. mIntVectorMap.size() +
  126. mLongVectorMap.size() +
  127. mDoubleVectorMap.size() +
  128. mStringVectorMap.size() +
  129. mPersistableBundleMap.size());
  130. }
  131. size_t PersistableBundle::erase(const String16& key) {
  132. RETURN_IF_ENTRY_ERASED(mBoolMap, key);
  133. RETURN_IF_ENTRY_ERASED(mIntMap, key);
  134. RETURN_IF_ENTRY_ERASED(mLongMap, key);
  135. RETURN_IF_ENTRY_ERASED(mDoubleMap, key);
  136. RETURN_IF_ENTRY_ERASED(mStringMap, key);
  137. RETURN_IF_ENTRY_ERASED(mBoolVectorMap, key);
  138. RETURN_IF_ENTRY_ERASED(mIntVectorMap, key);
  139. RETURN_IF_ENTRY_ERASED(mLongVectorMap, key);
  140. RETURN_IF_ENTRY_ERASED(mDoubleVectorMap, key);
  141. RETURN_IF_ENTRY_ERASED(mStringVectorMap, key);
  142. return mPersistableBundleMap.erase(key);
  143. }
  144. void PersistableBundle::putBoolean(const String16& key, bool value) {
  145. erase(key);
  146. mBoolMap[key] = value;
  147. }
  148. void PersistableBundle::putInt(const String16& key, int32_t value) {
  149. erase(key);
  150. mIntMap[key] = value;
  151. }
  152. void PersistableBundle::putLong(const String16& key, int64_t value) {
  153. erase(key);
  154. mLongMap[key] = value;
  155. }
  156. void PersistableBundle::putDouble(const String16& key, double value) {
  157. erase(key);
  158. mDoubleMap[key] = value;
  159. }
  160. void PersistableBundle::putString(const String16& key, const String16& value) {
  161. erase(key);
  162. mStringMap[key] = value;
  163. }
  164. void PersistableBundle::putBooleanVector(const String16& key, const vector<bool>& value) {
  165. erase(key);
  166. mBoolVectorMap[key] = value;
  167. }
  168. void PersistableBundle::putIntVector(const String16& key, const vector<int32_t>& value) {
  169. erase(key);
  170. mIntVectorMap[key] = value;
  171. }
  172. void PersistableBundle::putLongVector(const String16& key, const vector<int64_t>& value) {
  173. erase(key);
  174. mLongVectorMap[key] = value;
  175. }
  176. void PersistableBundle::putDoubleVector(const String16& key, const vector<double>& value) {
  177. erase(key);
  178. mDoubleVectorMap[key] = value;
  179. }
  180. void PersistableBundle::putStringVector(const String16& key, const vector<String16>& value) {
  181. erase(key);
  182. mStringVectorMap[key] = value;
  183. }
  184. void PersistableBundle::putPersistableBundle(const String16& key, const PersistableBundle& value) {
  185. erase(key);
  186. mPersistableBundleMap[key] = value;
  187. }
  188. bool PersistableBundle::getBoolean(const String16& key, bool* out) const {
  189. return getValue(key, out, mBoolMap);
  190. }
  191. bool PersistableBundle::getInt(const String16& key, int32_t* out) const {
  192. return getValue(key, out, mIntMap);
  193. }
  194. bool PersistableBundle::getLong(const String16& key, int64_t* out) const {
  195. return getValue(key, out, mLongMap);
  196. }
  197. bool PersistableBundle::getDouble(const String16& key, double* out) const {
  198. return getValue(key, out, mDoubleMap);
  199. }
  200. bool PersistableBundle::getString(const String16& key, String16* out) const {
  201. return getValue(key, out, mStringMap);
  202. }
  203. bool PersistableBundle::getBooleanVector(const String16& key, vector<bool>* out) const {
  204. return getValue(key, out, mBoolVectorMap);
  205. }
  206. bool PersistableBundle::getIntVector(const String16& key, vector<int32_t>* out) const {
  207. return getValue(key, out, mIntVectorMap);
  208. }
  209. bool PersistableBundle::getLongVector(const String16& key, vector<int64_t>* out) const {
  210. return getValue(key, out, mLongVectorMap);
  211. }
  212. bool PersistableBundle::getDoubleVector(const String16& key, vector<double>* out) const {
  213. return getValue(key, out, mDoubleVectorMap);
  214. }
  215. bool PersistableBundle::getStringVector(const String16& key, vector<String16>* out) const {
  216. return getValue(key, out, mStringVectorMap);
  217. }
  218. bool PersistableBundle::getPersistableBundle(const String16& key, PersistableBundle* out) const {
  219. return getValue(key, out, mPersistableBundleMap);
  220. }
  221. set<String16> PersistableBundle::getBooleanKeys() const {
  222. return getKeys(mBoolMap);
  223. }
  224. set<String16> PersistableBundle::getIntKeys() const {
  225. return getKeys(mIntMap);
  226. }
  227. set<String16> PersistableBundle::getLongKeys() const {
  228. return getKeys(mLongMap);
  229. }
  230. set<String16> PersistableBundle::getDoubleKeys() const {
  231. return getKeys(mDoubleMap);
  232. }
  233. set<String16> PersistableBundle::getStringKeys() const {
  234. return getKeys(mStringMap);
  235. }
  236. set<String16> PersistableBundle::getBooleanVectorKeys() const {
  237. return getKeys(mBoolVectorMap);
  238. }
  239. set<String16> PersistableBundle::getIntVectorKeys() const {
  240. return getKeys(mIntVectorMap);
  241. }
  242. set<String16> PersistableBundle::getLongVectorKeys() const {
  243. return getKeys(mLongVectorMap);
  244. }
  245. set<String16> PersistableBundle::getDoubleVectorKeys() const {
  246. return getKeys(mDoubleVectorMap);
  247. }
  248. set<String16> PersistableBundle::getStringVectorKeys() const {
  249. return getKeys(mStringVectorMap);
  250. }
  251. set<String16> PersistableBundle::getPersistableBundleKeys() const {
  252. return getKeys(mPersistableBundleMap);
  253. }
  254. status_t PersistableBundle::writeToParcelInner(Parcel* parcel) const {
  255. /*
  256. * To keep this implementation in sync with writeArrayMapInternal() in
  257. * frameworks/base/core/java/android/os/Parcel.java, the number of key
  258. * value pairs must be written into the parcel before writing the key-value
  259. * pairs themselves.
  260. */
  261. size_t num_entries = size();
  262. if (num_entries > std::numeric_limits<int32_t>::max()) {
  263. ALOGE("The size of this PersistableBundle (%zu) too large to store in 32-bit signed int",
  264. num_entries);
  265. return BAD_VALUE;
  266. }
  267. RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
  268. for (const auto& key_val_pair : mBoolMap) {
  269. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  270. RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEAN));
  271. RETURN_IF_FAILED(parcel->writeBool(key_val_pair.second));
  272. }
  273. for (const auto& key_val_pair : mIntMap) {
  274. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  275. RETURN_IF_FAILED(parcel->writeInt32(VAL_INTEGER));
  276. RETURN_IF_FAILED(parcel->writeInt32(key_val_pair.second));
  277. }
  278. for (const auto& key_val_pair : mLongMap) {
  279. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  280. RETURN_IF_FAILED(parcel->writeInt32(VAL_LONG));
  281. RETURN_IF_FAILED(parcel->writeInt64(key_val_pair.second));
  282. }
  283. for (const auto& key_val_pair : mDoubleMap) {
  284. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  285. RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLE));
  286. RETURN_IF_FAILED(parcel->writeDouble(key_val_pair.second));
  287. }
  288. for (const auto& key_val_pair : mStringMap) {
  289. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  290. RETURN_IF_FAILED(parcel->writeInt32(VAL_STRING));
  291. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.second));
  292. }
  293. for (const auto& key_val_pair : mBoolVectorMap) {
  294. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  295. RETURN_IF_FAILED(parcel->writeInt32(VAL_BOOLEANARRAY));
  296. RETURN_IF_FAILED(parcel->writeBoolVector(key_val_pair.second));
  297. }
  298. for (const auto& key_val_pair : mIntVectorMap) {
  299. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  300. RETURN_IF_FAILED(parcel->writeInt32(VAL_INTARRAY));
  301. RETURN_IF_FAILED(parcel->writeInt32Vector(key_val_pair.second));
  302. }
  303. for (const auto& key_val_pair : mLongVectorMap) {
  304. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  305. RETURN_IF_FAILED(parcel->writeInt32(VAL_LONGARRAY));
  306. RETURN_IF_FAILED(parcel->writeInt64Vector(key_val_pair.second));
  307. }
  308. for (const auto& key_val_pair : mDoubleVectorMap) {
  309. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  310. RETURN_IF_FAILED(parcel->writeInt32(VAL_DOUBLEARRAY));
  311. RETURN_IF_FAILED(parcel->writeDoubleVector(key_val_pair.second));
  312. }
  313. for (const auto& key_val_pair : mStringVectorMap) {
  314. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  315. RETURN_IF_FAILED(parcel->writeInt32(VAL_STRINGARRAY));
  316. RETURN_IF_FAILED(parcel->writeString16Vector(key_val_pair.second));
  317. }
  318. for (const auto& key_val_pair : mPersistableBundleMap) {
  319. RETURN_IF_FAILED(parcel->writeString16(key_val_pair.first));
  320. RETURN_IF_FAILED(parcel->writeInt32(VAL_PERSISTABLEBUNDLE));
  321. RETURN_IF_FAILED(key_val_pair.second.writeToParcel(parcel));
  322. }
  323. return NO_ERROR;
  324. }
  325. status_t PersistableBundle::readFromParcelInner(const Parcel* parcel, size_t length) {
  326. /*
  327. * Note: we don't actually use length for anything other than an empty PersistableBundle
  328. * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
  329. * implementation.
  330. */
  331. if (length == 0) {
  332. // Empty PersistableBundle or end of data.
  333. return NO_ERROR;
  334. }
  335. int32_t magic;
  336. RETURN_IF_FAILED(parcel->readInt32(&magic));
  337. if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
  338. ALOGE("Bad magic number for PersistableBundle: 0x%08x", magic);
  339. return BAD_VALUE;
  340. }
  341. /*
  342. * To keep this implementation in sync with unparcel() in
  343. * frameworks/base/core/java/android/os/BaseBundle.java, the number of
  344. * key-value pairs must be read from the parcel before reading the key-value
  345. * pairs themselves.
  346. */
  347. int32_t num_entries;
  348. RETURN_IF_FAILED(parcel->readInt32(&num_entries));
  349. for (; num_entries > 0; --num_entries) {
  350. String16 key;
  351. int32_t value_type;
  352. RETURN_IF_FAILED(parcel->readString16(&key));
  353. RETURN_IF_FAILED(parcel->readInt32(&value_type));
  354. /*
  355. * We assume that both the C++ and Java APIs ensure that all keys in a PersistableBundle
  356. * are unique.
  357. */
  358. switch (value_type) {
  359. case VAL_STRING: {
  360. RETURN_IF_FAILED(parcel->readString16(&mStringMap[key]));
  361. break;
  362. }
  363. case VAL_INTEGER: {
  364. RETURN_IF_FAILED(parcel->readInt32(&mIntMap[key]));
  365. break;
  366. }
  367. case VAL_LONG: {
  368. RETURN_IF_FAILED(parcel->readInt64(&mLongMap[key]));
  369. break;
  370. }
  371. case VAL_DOUBLE: {
  372. RETURN_IF_FAILED(parcel->readDouble(&mDoubleMap[key]));
  373. break;
  374. }
  375. case VAL_BOOLEAN: {
  376. RETURN_IF_FAILED(parcel->readBool(&mBoolMap[key]));
  377. break;
  378. }
  379. case VAL_STRINGARRAY: {
  380. RETURN_IF_FAILED(parcel->readString16Vector(&mStringVectorMap[key]));
  381. break;
  382. }
  383. case VAL_INTARRAY: {
  384. RETURN_IF_FAILED(parcel->readInt32Vector(&mIntVectorMap[key]));
  385. break;
  386. }
  387. case VAL_LONGARRAY: {
  388. RETURN_IF_FAILED(parcel->readInt64Vector(&mLongVectorMap[key]));
  389. break;
  390. }
  391. case VAL_BOOLEANARRAY: {
  392. RETURN_IF_FAILED(parcel->readBoolVector(&mBoolVectorMap[key]));
  393. break;
  394. }
  395. case VAL_PERSISTABLEBUNDLE: {
  396. RETURN_IF_FAILED(mPersistableBundleMap[key].readFromParcel(parcel));
  397. break;
  398. }
  399. case VAL_DOUBLEARRAY: {
  400. RETURN_IF_FAILED(parcel->readDoubleVector(&mDoubleVectorMap[key]));
  401. break;
  402. }
  403. default: {
  404. ALOGE("Unrecognized type: %d", value_type);
  405. return BAD_TYPE;
  406. break;
  407. }
  408. }
  409. }
  410. return NO_ERROR;
  411. }
  412. } // namespace os
  413. } // namespace android