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