Parcel.h revision 837a0d0fb2c3fba8082d47d04cb6120af1eb9a54
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 // Writes the RPC header. 60 status_t writeInterfaceToken(const String16& interface); 61 // Parses the RPC header, returning true if the interface name 62 // in the header matches the expected interface from the caller. 63 bool enforceInterface(const String16& interface) const; 64 bool checkInterface(IBinder*) const; 65 66 void freeData(); 67 68 const size_t* objects() const; 69 size_t objectsCount() const; 70 71 status_t errorCheck() const; 72 void setError(status_t err); 73 74 status_t write(const void* data, size_t len); 75 void* writeInplace(size_t len); 76 status_t writeUnpadded(const void* data, size_t len); 77 status_t writeInt32(int32_t val); 78 status_t writeInt64(int64_t val); 79 status_t writeFloat(float val); 80 status_t writeDouble(double val); 81 status_t writeIntPtr(intptr_t val); 82 status_t writeCString(const char* str); 83 status_t writeString8(const String8& str); 84 status_t writeString16(const String16& str); 85 status_t writeString16(const char16_t* str, size_t len); 86 status_t writeStrongBinder(const sp<IBinder>& val); 87 status_t writeWeakBinder(const wp<IBinder>& val); 88 status_t write(const Flattenable& val); 89 90 // Place a native_handle into the parcel (the native_handle's file- 91 // descriptors are dup'ed, so it is safe to delete the native_handle 92 // when this function returns). 93 // Doesn't take ownership of the native_handle. 94 status_t writeNativeHandle(const native_handle* handle); 95 96 // Place a file descriptor into the parcel. The given fd must remain 97 // valid for the lifetime of the parcel. 98 status_t writeFileDescriptor(int fd); 99 100 // Place a file descriptor into the parcel. A dup of the fd is made, which 101 // will be closed once the parcel is destroyed. 102 status_t writeDupFileDescriptor(int fd); 103 104 status_t writeObject(const flat_binder_object& val, bool nullMetaData); 105 106 // Like Parcel.java's writeNoException(). Just writes a zero int32. 107 // Currently the native implementation doesn't do any of the StrictMode 108 // stack gathering and serialization that the Java implementation does. 109 status_t writeNoException(); 110 111 void remove(size_t start, size_t amt); 112 113 status_t read(void* outData, size_t len) const; 114 const void* readInplace(size_t len) const; 115 int32_t readInt32() const; 116 status_t readInt32(int32_t *pArg) const; 117 int64_t readInt64() const; 118 status_t readInt64(int64_t *pArg) const; 119 float readFloat() const; 120 status_t readFloat(float *pArg) const; 121 double readDouble() const; 122 status_t readDouble(double *pArg) const; 123 intptr_t readIntPtr() const; 124 status_t readIntPtr(intptr_t *pArg) const; 125 126 const char* readCString() const; 127 String8 readString8() const; 128 String16 readString16() const; 129 const char16_t* readString16Inplace(size_t* outLen) const; 130 sp<IBinder> readStrongBinder() const; 131 wp<IBinder> readWeakBinder() const; 132 status_t read(Flattenable& val) const; 133 134 // Like Parcel.java's readExceptionCode(). Reads the first int32 135 // off of a Parcel's header, returning 0 or the negative error 136 // code on exceptions, but also deals with skipping over rich 137 // response headers. Callers should use this to read & parse the 138 // response headers rather than doing it by hand. 139 int32_t readExceptionCode() const; 140 141 // Retrieve native_handle from the parcel. This returns a copy of the 142 // parcel's native_handle (the caller takes ownership). The caller 143 // must free the native_handle with native_handle_close() and 144 // native_handle_delete(). 145 native_handle* readNativeHandle() const; 146 147 148 // Retrieve a file descriptor from the parcel. This returns the raw fd 149 // in the parcel, which you do not own -- use dup() to get your own copy. 150 int readFileDescriptor() const; 151 152 const flat_binder_object* readObject(bool nullMetaData) const; 153 154 // Explicitly close all file descriptors in the parcel. 155 void closeFileDescriptors(); 156 157 typedef void (*release_func)(Parcel* parcel, 158 const uint8_t* data, size_t dataSize, 159 const size_t* objects, size_t objectsSize, 160 void* cookie); 161 162 const uint8_t* ipcData() const; 163 size_t ipcDataSize() const; 164 const size_t* ipcObjects() const; 165 size_t ipcObjectsCount() const; 166 void ipcSetDataReference(const uint8_t* data, size_t dataSize, 167 const size_t* objects, size_t objectsCount, 168 release_func relFunc, void* relCookie); 169 170 void print(TextOutput& to, uint32_t flags = 0) const; 171 172private: 173 Parcel(const Parcel& o); 174 Parcel& operator=(const Parcel& o); 175 176 status_t finishWrite(size_t len); 177 void releaseObjects(); 178 void acquireObjects(); 179 status_t growData(size_t len); 180 status_t restartWrite(size_t desired); 181 status_t continueWrite(size_t desired); 182 void freeDataNoInit(); 183 void initState(); 184 void scanForFds() const; 185 186 template<class T> 187 status_t readAligned(T *pArg) const; 188 189 template<class T> T readAligned() const; 190 191 template<class T> 192 status_t writeAligned(T val); 193 194 status_t mError; 195 uint8_t* mData; 196 size_t mDataSize; 197 size_t mDataCapacity; 198 mutable size_t mDataPos; 199 size_t* mObjects; 200 size_t mObjectsSize; 201 size_t mObjectsCapacity; 202 mutable size_t mNextObjectHint; 203 204 mutable bool mFdsKnown; 205 mutable bool mHasFds; 206 207 release_func mOwner; 208 void* mOwnerCookie; 209}; 210 211// --------------------------------------------------------------------------- 212 213inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) 214{ 215 parcel.print(to); 216 return to; 217} 218 219// --------------------------------------------------------------------------- 220 221// Generic acquire and release of objects. 222void acquire_object(const sp<ProcessState>& proc, 223 const flat_binder_object& obj, const void* who); 224void release_object(const sp<ProcessState>& proc, 225 const flat_binder_object& obj, const void* who); 226 227void flatten_binder(const sp<ProcessState>& proc, 228 const sp<IBinder>& binder, flat_binder_object* out); 229void flatten_binder(const sp<ProcessState>& proc, 230 const wp<IBinder>& binder, flat_binder_object* out); 231status_t unflatten_binder(const sp<ProcessState>& proc, 232 const flat_binder_object& flat, sp<IBinder>* out); 233status_t unflatten_binder(const sp<ProcessState>& proc, 234 const flat_binder_object& flat, wp<IBinder>* out); 235 236}; // namespace android 237 238// --------------------------------------------------------------------------- 239 240#endif // ANDROID_PARCEL_H 241