The Android Open Source Project | 9066cfe | 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 | |
| 29 | class IBinder; |
| 30 | class ProcessState; |
| 31 | class String8; |
| 32 | class TextOutput; |
| 33 | |
| 34 | struct flat_binder_object; // defined in support_p/binder_module.h |
| 35 | |
| 36 | class Parcel |
| 37 | { |
| 38 | public: |
| 39 | Parcel(); |
| 40 | ~Parcel(); |
| 41 | |
| 42 | const uint8_t* data() const; |
| 43 | size_t dataSize() const; |
| 44 | size_t dataAvail() const; |
| 45 | size_t dataPosition() const; |
| 46 | size_t dataCapacity() const; |
| 47 | |
| 48 | status_t setDataSize(size_t size); |
| 49 | void setDataPosition(size_t pos) const; |
| 50 | status_t setDataCapacity(size_t size); |
| 51 | |
| 52 | status_t setData(const uint8_t* buffer, size_t len); |
| 53 | |
| 54 | status_t appendFrom(Parcel *parcel, size_t start, size_t len); |
| 55 | |
| 56 | bool hasFileDescriptors() const; |
| 57 | |
| 58 | status_t writeInterfaceToken(const String16& interface); |
| 59 | bool enforceInterface(const String16& interface) const; |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 60 | bool checkInterface(IBinder*) const; |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | void freeData(); |
| 63 | |
| 64 | const size_t* objects() const; |
| 65 | size_t objectsCount() const; |
| 66 | |
| 67 | status_t errorCheck() const; |
| 68 | void setError(status_t err); |
| 69 | |
| 70 | status_t write(const void* data, size_t len); |
| 71 | void* writeInplace(size_t len); |
| 72 | status_t writeUnpadded(const void* data, size_t len); |
| 73 | status_t writeInt32(int32_t val); |
| 74 | status_t writeInt64(int64_t val); |
| 75 | status_t writeFloat(float val); |
| 76 | status_t writeDouble(double val); |
Andreas Huber | 2f10ae0 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 77 | status_t writeIntPtr(intptr_t val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | status_t writeCString(const char* str); |
| 79 | status_t writeString8(const String8& str); |
| 80 | status_t writeString16(const String16& str); |
| 81 | status_t writeString16(const char16_t* str, size_t len); |
| 82 | status_t writeStrongBinder(const sp<IBinder>& val); |
| 83 | status_t writeWeakBinder(const wp<IBinder>& val); |
| 84 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 85 | // Place a native_handle into the parcel (the native_handle's file- |
| 86 | // descriptors are dup'ed, so it is safe to delete the native_handle |
| 87 | // when this function returns). |
| 88 | // Doesn't take ownership of the native_handle. |
| 89 | status_t writeNativeHandle(const native_handle* handle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
| 91 | // Place a file descriptor into the parcel. The given fd must remain |
| 92 | // valid for the lifetime of the parcel. |
| 93 | status_t writeFileDescriptor(int fd); |
| 94 | |
| 95 | // Place a file descriptor into the parcel. A dup of the fd is made, which |
| 96 | // will be closed once the parcel is destroyed. |
| 97 | status_t writeDupFileDescriptor(int fd); |
| 98 | |
| 99 | status_t writeObject(const flat_binder_object& val, bool nullMetaData); |
| 100 | |
| 101 | void remove(size_t start, size_t amt); |
| 102 | |
| 103 | status_t read(void* outData, size_t len) const; |
| 104 | const void* readInplace(size_t len) const; |
| 105 | int32_t readInt32() const; |
| 106 | status_t readInt32(int32_t *pArg) const; |
| 107 | int64_t readInt64() const; |
| 108 | status_t readInt64(int64_t *pArg) const; |
| 109 | float readFloat() const; |
| 110 | status_t readFloat(float *pArg) const; |
| 111 | double readDouble() const; |
| 112 | status_t readDouble(double *pArg) const; |
Andreas Huber | 2f10ae0 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 113 | intptr_t readIntPtr() const; |
| 114 | status_t readIntPtr(intptr_t *pArg) const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
| 116 | const char* readCString() const; |
| 117 | String8 readString8() const; |
| 118 | String16 readString16() const; |
| 119 | const char16_t* readString16Inplace(size_t* outLen) const; |
| 120 | sp<IBinder> readStrongBinder() const; |
| 121 | wp<IBinder> readWeakBinder() const; |
| 122 | |
| 123 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 124 | // Retrieve native_handle from the parcel. This returns a copy of the |
| 125 | // parcel's native_handle (the caller takes ownership). The caller |
| 126 | // must free the native_handle with native_handle_close() and |
| 127 | // native_handle_delete(). |
| 128 | native_handle* readNativeHandle() const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
| 130 | |
| 131 | // Retrieve a file descriptor from the parcel. This returns the raw fd |
| 132 | // in the parcel, which you do not own -- use dup() to get your own copy. |
| 133 | int readFileDescriptor() const; |
| 134 | |
| 135 | const flat_binder_object* readObject(bool nullMetaData) const; |
| 136 | |
| 137 | // Explicitly close all file descriptors in the parcel. |
| 138 | void closeFileDescriptors(); |
| 139 | |
| 140 | typedef void (*release_func)(Parcel* parcel, |
| 141 | const uint8_t* data, size_t dataSize, |
| 142 | const size_t* objects, size_t objectsSize, |
| 143 | void* cookie); |
| 144 | |
| 145 | const uint8_t* ipcData() const; |
| 146 | size_t ipcDataSize() const; |
| 147 | const size_t* ipcObjects() const; |
| 148 | size_t ipcObjectsCount() const; |
| 149 | void ipcSetDataReference(const uint8_t* data, size_t dataSize, |
| 150 | const size_t* objects, size_t objectsCount, |
| 151 | release_func relFunc, void* relCookie); |
| 152 | |
| 153 | void print(TextOutput& to, uint32_t flags = 0) const; |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | private: |
| 156 | Parcel(const Parcel& o); |
| 157 | Parcel& operator=(const Parcel& o); |
| 158 | |
| 159 | status_t finishWrite(size_t len); |
| 160 | void releaseObjects(); |
| 161 | void acquireObjects(); |
| 162 | status_t growData(size_t len); |
| 163 | status_t restartWrite(size_t desired); |
| 164 | status_t continueWrite(size_t desired); |
| 165 | void freeDataNoInit(); |
| 166 | void initState(); |
| 167 | void scanForFds() const; |
| 168 | |
Andreas Huber | 2f10ae0 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 169 | template<class T> |
| 170 | status_t readAligned(T *pArg) const; |
| 171 | |
| 172 | template<class T> T readAligned() const; |
| 173 | |
| 174 | template<class T> |
| 175 | status_t writeAligned(T val); |
| 176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | status_t mError; |
| 178 | uint8_t* mData; |
| 179 | size_t mDataSize; |
| 180 | size_t mDataCapacity; |
| 181 | mutable size_t mDataPos; |
| 182 | size_t* mObjects; |
| 183 | size_t mObjectsSize; |
| 184 | size_t mObjectsCapacity; |
| 185 | mutable size_t mNextObjectHint; |
| 186 | |
| 187 | mutable bool mFdsKnown; |
| 188 | mutable bool mHasFds; |
| 189 | |
| 190 | release_func mOwner; |
| 191 | void* mOwnerCookie; |
| 192 | }; |
| 193 | |
| 194 | // --------------------------------------------------------------------------- |
| 195 | |
| 196 | inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) |
| 197 | { |
| 198 | parcel.print(to); |
| 199 | return to; |
| 200 | } |
| 201 | |
| 202 | // --------------------------------------------------------------------------- |
| 203 | |
| 204 | // Generic acquire and release of objects. |
| 205 | void acquire_object(const sp<ProcessState>& proc, |
| 206 | const flat_binder_object& obj, const void* who); |
| 207 | void release_object(const sp<ProcessState>& proc, |
| 208 | const flat_binder_object& obj, const void* who); |
| 209 | |
| 210 | void flatten_binder(const sp<ProcessState>& proc, |
| 211 | const sp<IBinder>& binder, flat_binder_object* out); |
| 212 | void flatten_binder(const sp<ProcessState>& proc, |
| 213 | const wp<IBinder>& binder, flat_binder_object* out); |
| 214 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 215 | const flat_binder_object& flat, sp<IBinder>* out); |
| 216 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 217 | const flat_binder_object& flat, wp<IBinder>* out); |
| 218 | |
| 219 | }; // namespace android |
| 220 | |
| 221 | // --------------------------------------------------------------------------- |
| 222 | |
| 223 | #endif // ANDROID_PARCEL_H |