The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | #ifndef ANDROID_PARCEL_H |
| 18 | #define ANDROID_PARCEL_H |
| 19 | |
| 20 | #include <cutils/native_handle.h> |
| 21 | #include <utils/Errors.h> |
| 22 | #include <utils/RefBase.h> |
| 23 | #include <utils/String16.h> |
| 24 | #include <utils/Vector.h> |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 25 | #include <utils/Flattenable.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | // --------------------------------------------------------------------------- |
| 28 | namespace android { |
| 29 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 30 | template <typename T> class LightFlattenable; |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 31 | class Flattenable; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | class IBinder; |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 33 | class IPCThreadState; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | class ProcessState; |
| 35 | class String8; |
| 36 | class TextOutput; |
| 37 | |
| 38 | struct flat_binder_object; // defined in support_p/binder_module.h |
| 39 | |
| 40 | class Parcel |
| 41 | { |
| 42 | public: |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 43 | class ReadableBlob; |
| 44 | class WritableBlob; |
| 45 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | Parcel(); |
| 47 | ~Parcel(); |
| 48 | |
| 49 | const uint8_t* data() const; |
| 50 | size_t dataSize() const; |
| 51 | size_t dataAvail() const; |
| 52 | size_t dataPosition() const; |
| 53 | size_t dataCapacity() const; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 54 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | status_t setDataSize(size_t size); |
| 56 | void setDataPosition(size_t pos) const; |
| 57 | status_t setDataCapacity(size_t size); |
| 58 | |
| 59 | status_t setData(const uint8_t* buffer, size_t len); |
| 60 | |
Andreas Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 61 | status_t appendFrom(const Parcel *parcel, |
| 62 | size_t start, size_t len); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 64 | bool pushAllowFds(bool allowFds); |
| 65 | void restoreAllowFds(bool lastValue); |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 66 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | bool hasFileDescriptors() const; |
| 68 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 69 | // Writes the RPC header. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | status_t writeInterfaceToken(const String16& interface); |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 71 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 72 | // Parses the RPC header, returning true if the interface name |
| 73 | // in the header matches the expected interface from the caller. |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 74 | // |
| 75 | // Additionally, enforceInterface does part of the work of |
| 76 | // propagating the StrictMode policy mask, populating the current |
| 77 | // IPCThreadState, which as an optimization may optionally be |
| 78 | // passed in. |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 79 | bool enforceInterface(const String16& interface, |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 80 | IPCThreadState* threadState = NULL) const; |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 81 | bool checkInterface(IBinder*) const; |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | void freeData(); |
| 84 | |
| 85 | const size_t* objects() const; |
| 86 | size_t objectsCount() const; |
| 87 | |
| 88 | status_t errorCheck() const; |
| 89 | void setError(status_t err); |
| 90 | |
| 91 | status_t write(const void* data, size_t len); |
| 92 | void* writeInplace(size_t len); |
| 93 | status_t writeUnpadded(const void* data, size_t len); |
| 94 | status_t writeInt32(int32_t val); |
| 95 | status_t writeInt64(int64_t val); |
| 96 | status_t writeFloat(float val); |
| 97 | status_t writeDouble(double val); |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 98 | status_t writeIntPtr(intptr_t val); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | status_t writeCString(const char* str); |
| 100 | status_t writeString8(const String8& str); |
| 101 | status_t writeString16(const String16& str); |
| 102 | status_t writeString16(const char16_t* str, size_t len); |
| 103 | status_t writeStrongBinder(const sp<IBinder>& val); |
| 104 | status_t writeWeakBinder(const wp<IBinder>& val); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 105 | status_t write(const Flattenable& val); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 107 | template<typename T> |
| 108 | status_t write(const LightFlattenable<T>& val); |
| 109 | |
| 110 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 111 | // Place a native_handle into the parcel (the native_handle's file- |
| 112 | // descriptors are dup'ed, so it is safe to delete the native_handle |
| 113 | // when this function returns). |
| 114 | // Doesn't take ownership of the native_handle. |
| 115 | status_t writeNativeHandle(const native_handle* handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
| 117 | // Place a file descriptor into the parcel. The given fd must remain |
| 118 | // valid for the lifetime of the parcel. |
Jeff Brown | 93ff1f9 | 2011-11-04 19:01:44 -0700 | [diff] [blame] | 119 | // The Parcel does not take ownership of the given fd unless you ask it to. |
| 120 | status_t writeFileDescriptor(int fd, bool takeOwnership = false); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | |
| 122 | // Place a file descriptor into the parcel. A dup of the fd is made, which |
| 123 | // will be closed once the parcel is destroyed. |
| 124 | status_t writeDupFileDescriptor(int fd); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 125 | |
| 126 | // Writes a blob to the parcel. |
| 127 | // If the blob is small, then it is stored in-place, otherwise it is |
| 128 | // transferred by way of an anonymous shared memory region. |
| 129 | // The caller should call release() on the blob after writing its contents. |
| 130 | status_t writeBlob(size_t len, WritableBlob* outBlob); |
| 131 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | status_t writeObject(const flat_binder_object& val, bool nullMetaData); |
| 133 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 134 | // Like Parcel.java's writeNoException(). Just writes a zero int32. |
| 135 | // Currently the native implementation doesn't do any of the StrictMode |
| 136 | // stack gathering and serialization that the Java implementation does. |
| 137 | status_t writeNoException(); |
| 138 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | void remove(size_t start, size_t amt); |
| 140 | |
| 141 | status_t read(void* outData, size_t len) const; |
| 142 | const void* readInplace(size_t len) const; |
| 143 | int32_t readInt32() const; |
| 144 | status_t readInt32(int32_t *pArg) const; |
| 145 | int64_t readInt64() const; |
| 146 | status_t readInt64(int64_t *pArg) const; |
| 147 | float readFloat() const; |
| 148 | status_t readFloat(float *pArg) const; |
| 149 | double readDouble() const; |
| 150 | status_t readDouble(double *pArg) const; |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 151 | intptr_t readIntPtr() const; |
| 152 | status_t readIntPtr(intptr_t *pArg) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | |
| 154 | const char* readCString() const; |
| 155 | String8 readString8() const; |
| 156 | String16 readString16() const; |
| 157 | const char16_t* readString16Inplace(size_t* outLen) const; |
| 158 | sp<IBinder> readStrongBinder() const; |
| 159 | wp<IBinder> readWeakBinder() const; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 160 | status_t read(Flattenable& val) const; |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 161 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 162 | template<typename T> |
| 163 | status_t read(LightFlattenable<T>& val) const; |
| 164 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 165 | // Like Parcel.java's readExceptionCode(). Reads the first int32 |
| 166 | // off of a Parcel's header, returning 0 or the negative error |
| 167 | // code on exceptions, but also deals with skipping over rich |
| 168 | // response headers. Callers should use this to read & parse the |
| 169 | // response headers rather than doing it by hand. |
| 170 | int32_t readExceptionCode() const; |
| 171 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 172 | // Retrieve native_handle from the parcel. This returns a copy of the |
| 173 | // parcel's native_handle (the caller takes ownership). The caller |
| 174 | // must free the native_handle with native_handle_close() and |
| 175 | // native_handle_delete(). |
| 176 | native_handle* readNativeHandle() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
| 178 | |
| 179 | // Retrieve a file descriptor from the parcel. This returns the raw fd |
| 180 | // in the parcel, which you do not own -- use dup() to get your own copy. |
| 181 | int readFileDescriptor() const; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 182 | |
| 183 | // Reads a blob from the parcel. |
| 184 | // The caller should call release() on the blob after reading its contents. |
| 185 | status_t readBlob(size_t len, ReadableBlob* outBlob) const; |
| 186 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | const flat_binder_object* readObject(bool nullMetaData) const; |
| 188 | |
| 189 | // Explicitly close all file descriptors in the parcel. |
| 190 | void closeFileDescriptors(); |
| 191 | |
| 192 | typedef void (*release_func)(Parcel* parcel, |
| 193 | const uint8_t* data, size_t dataSize, |
| 194 | const size_t* objects, size_t objectsSize, |
| 195 | void* cookie); |
| 196 | |
| 197 | const uint8_t* ipcData() const; |
| 198 | size_t ipcDataSize() const; |
| 199 | const size_t* ipcObjects() const; |
| 200 | size_t ipcObjectsCount() const; |
| 201 | void ipcSetDataReference(const uint8_t* data, size_t dataSize, |
| 202 | const size_t* objects, size_t objectsCount, |
| 203 | release_func relFunc, void* relCookie); |
| 204 | |
| 205 | void print(TextOutput& to, uint32_t flags = 0) const; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 206 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | private: |
| 208 | Parcel(const Parcel& o); |
| 209 | Parcel& operator=(const Parcel& o); |
| 210 | |
| 211 | status_t finishWrite(size_t len); |
| 212 | void releaseObjects(); |
| 213 | void acquireObjects(); |
| 214 | status_t growData(size_t len); |
| 215 | status_t restartWrite(size_t desired); |
| 216 | status_t continueWrite(size_t desired); |
| 217 | void freeDataNoInit(); |
| 218 | void initState(); |
| 219 | void scanForFds() const; |
| 220 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 221 | template<class T> |
| 222 | status_t readAligned(T *pArg) const; |
| 223 | |
| 224 | template<class T> T readAligned() const; |
| 225 | |
| 226 | template<class T> |
| 227 | status_t writeAligned(T val); |
| 228 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | status_t mError; |
| 230 | uint8_t* mData; |
| 231 | size_t mDataSize; |
| 232 | size_t mDataCapacity; |
| 233 | mutable size_t mDataPos; |
| 234 | size_t* mObjects; |
| 235 | size_t mObjectsSize; |
| 236 | size_t mObjectsCapacity; |
| 237 | mutable size_t mNextObjectHint; |
| 238 | |
| 239 | mutable bool mFdsKnown; |
| 240 | mutable bool mHasFds; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 241 | bool mAllowFds; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | |
| 243 | release_func mOwner; |
| 244 | void* mOwnerCookie; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 245 | |
| 246 | class Blob { |
| 247 | public: |
| 248 | Blob(); |
| 249 | ~Blob(); |
| 250 | |
| 251 | void release(); |
| 252 | inline size_t size() const { return mSize; } |
| 253 | |
| 254 | protected: |
| 255 | void init(bool mapped, void* data, size_t size); |
| 256 | void clear(); |
| 257 | |
| 258 | bool mMapped; |
| 259 | void* mData; |
| 260 | size_t mSize; |
| 261 | }; |
| 262 | |
| 263 | public: |
| 264 | class ReadableBlob : public Blob { |
| 265 | friend class Parcel; |
| 266 | public: |
| 267 | inline const void* data() const { return mData; } |
| 268 | }; |
| 269 | |
| 270 | class WritableBlob : public Blob { |
| 271 | friend class Parcel; |
| 272 | public: |
| 273 | inline void* data() { return mData; } |
| 274 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | // --------------------------------------------------------------------------- |
| 278 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 279 | template<typename T> |
| 280 | status_t Parcel::write(const LightFlattenable<T>& val) { |
| 281 | size_t size(val.getSize()); |
| 282 | if (!val.isFixedSize()) { |
| 283 | status_t err = writeInt32(size); |
| 284 | if (err != NO_ERROR) { |
| 285 | return err; |
| 286 | } |
| 287 | } |
Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 288 | if (size) { |
| 289 | void* buffer = writeInplace(size); |
| 290 | return buffer == NULL ? NO_MEMORY : |
| 291 | val.flatten(buffer); |
| 292 | } |
| 293 | return NO_ERROR; |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | template<typename T> |
| 297 | status_t Parcel::read(LightFlattenable<T>& val) const { |
| 298 | size_t size; |
| 299 | if (val.isFixedSize()) { |
| 300 | size = val.getSize(); |
| 301 | } else { |
| 302 | int32_t s; |
| 303 | status_t err = readInt32(&s); |
| 304 | if (err != NO_ERROR) { |
| 305 | return err; |
| 306 | } |
| 307 | size = s; |
| 308 | } |
Mathias Agopian | 2098517 | 2012-08-31 14:25:22 -0700 | [diff] [blame] | 309 | if (size) { |
| 310 | void const* buffer = readInplace(size); |
| 311 | return buffer == NULL ? NO_MEMORY : |
| 312 | val.unflatten(buffer, size); |
| 313 | } |
| 314 | return NO_ERROR; |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // --------------------------------------------------------------------------- |
| 318 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) |
| 320 | { |
| 321 | parcel.print(to); |
| 322 | return to; |
| 323 | } |
| 324 | |
| 325 | // --------------------------------------------------------------------------- |
| 326 | |
| 327 | // Generic acquire and release of objects. |
| 328 | void acquire_object(const sp<ProcessState>& proc, |
| 329 | const flat_binder_object& obj, const void* who); |
| 330 | void release_object(const sp<ProcessState>& proc, |
| 331 | const flat_binder_object& obj, const void* who); |
| 332 | |
| 333 | void flatten_binder(const sp<ProcessState>& proc, |
| 334 | const sp<IBinder>& binder, flat_binder_object* out); |
| 335 | void flatten_binder(const sp<ProcessState>& proc, |
| 336 | const wp<IBinder>& binder, flat_binder_object* out); |
| 337 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 338 | const flat_binder_object& flat, sp<IBinder>* out); |
| 339 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 340 | const flat_binder_object& flat, wp<IBinder>* out); |
| 341 | |
| 342 | }; // namespace android |
| 343 | |
| 344 | // --------------------------------------------------------------------------- |
| 345 | |
| 346 | #endif // ANDROID_PARCEL_H |