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