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); |
| 77 | status_t writeCString(const char* str); |
| 78 | status_t writeString8(const String8& str); |
| 79 | status_t writeString16(const String16& str); |
| 80 | status_t writeString16(const char16_t* str, size_t len); |
| 81 | status_t writeStrongBinder(const sp<IBinder>& val); |
| 82 | status_t writeWeakBinder(const wp<IBinder>& val); |
| 83 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 84 | // Place a native_handle into the parcel (the native_handle's file- |
| 85 | // descriptors are dup'ed, so it is safe to delete the native_handle |
| 86 | // when this function returns). |
| 87 | // Doesn't take ownership of the native_handle. |
| 88 | status_t writeNativeHandle(const native_handle* handle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | // Place a file descriptor into the parcel. The given fd must remain |
| 91 | // valid for the lifetime of the parcel. |
| 92 | status_t writeFileDescriptor(int fd); |
| 93 | |
| 94 | // Place a file descriptor into the parcel. A dup of the fd is made, which |
| 95 | // will be closed once the parcel is destroyed. |
| 96 | status_t writeDupFileDescriptor(int fd); |
| 97 | |
| 98 | status_t writeObject(const flat_binder_object& val, bool nullMetaData); |
| 99 | |
| 100 | void remove(size_t start, size_t amt); |
| 101 | |
| 102 | status_t read(void* outData, size_t len) const; |
| 103 | const void* readInplace(size_t len) const; |
| 104 | int32_t readInt32() const; |
| 105 | status_t readInt32(int32_t *pArg) const; |
| 106 | int64_t readInt64() const; |
| 107 | status_t readInt64(int64_t *pArg) const; |
| 108 | float readFloat() const; |
| 109 | status_t readFloat(float *pArg) const; |
| 110 | double readDouble() const; |
| 111 | status_t readDouble(double *pArg) const; |
| 112 | |
| 113 | const char* readCString() const; |
| 114 | String8 readString8() const; |
| 115 | String16 readString16() const; |
| 116 | const char16_t* readString16Inplace(size_t* outLen) const; |
| 117 | sp<IBinder> readStrongBinder() const; |
| 118 | wp<IBinder> readWeakBinder() const; |
| 119 | |
| 120 | |
Mathias Agopian | dfece80 | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 121 | // Retrieve native_handle from the parcel. This returns a copy of the |
| 122 | // parcel's native_handle (the caller takes ownership). The caller |
| 123 | // must free the native_handle with native_handle_close() and |
| 124 | // native_handle_delete(). |
| 125 | native_handle* readNativeHandle() const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | |
| 127 | |
| 128 | // Retrieve a file descriptor from the parcel. This returns the raw fd |
| 129 | // in the parcel, which you do not own -- use dup() to get your own copy. |
| 130 | int readFileDescriptor() const; |
| 131 | |
| 132 | const flat_binder_object* readObject(bool nullMetaData) const; |
| 133 | |
| 134 | // Explicitly close all file descriptors in the parcel. |
| 135 | void closeFileDescriptors(); |
| 136 | |
| 137 | typedef void (*release_func)(Parcel* parcel, |
| 138 | const uint8_t* data, size_t dataSize, |
| 139 | const size_t* objects, size_t objectsSize, |
| 140 | void* cookie); |
| 141 | |
| 142 | const uint8_t* ipcData() const; |
| 143 | size_t ipcDataSize() const; |
| 144 | const size_t* ipcObjects() const; |
| 145 | size_t ipcObjectsCount() const; |
| 146 | void ipcSetDataReference(const uint8_t* data, size_t dataSize, |
| 147 | const size_t* objects, size_t objectsCount, |
| 148 | release_func relFunc, void* relCookie); |
| 149 | |
| 150 | void print(TextOutput& to, uint32_t flags = 0) const; |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame^] | 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | private: |
| 153 | Parcel(const Parcel& o); |
| 154 | Parcel& operator=(const Parcel& o); |
| 155 | |
| 156 | status_t finishWrite(size_t len); |
| 157 | void releaseObjects(); |
| 158 | void acquireObjects(); |
| 159 | status_t growData(size_t len); |
| 160 | status_t restartWrite(size_t desired); |
| 161 | status_t continueWrite(size_t desired); |
| 162 | void freeDataNoInit(); |
| 163 | void initState(); |
| 164 | void scanForFds() const; |
| 165 | |
| 166 | status_t mError; |
| 167 | uint8_t* mData; |
| 168 | size_t mDataSize; |
| 169 | size_t mDataCapacity; |
| 170 | mutable size_t mDataPos; |
| 171 | size_t* mObjects; |
| 172 | size_t mObjectsSize; |
| 173 | size_t mObjectsCapacity; |
| 174 | mutable size_t mNextObjectHint; |
| 175 | |
| 176 | mutable bool mFdsKnown; |
| 177 | mutable bool mHasFds; |
| 178 | |
| 179 | release_func mOwner; |
| 180 | void* mOwnerCookie; |
| 181 | }; |
| 182 | |
| 183 | // --------------------------------------------------------------------------- |
| 184 | |
| 185 | inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) |
| 186 | { |
| 187 | parcel.print(to); |
| 188 | return to; |
| 189 | } |
| 190 | |
| 191 | // --------------------------------------------------------------------------- |
| 192 | |
| 193 | // Generic acquire and release of objects. |
| 194 | void acquire_object(const sp<ProcessState>& proc, |
| 195 | const flat_binder_object& obj, const void* who); |
| 196 | void release_object(const sp<ProcessState>& proc, |
| 197 | const flat_binder_object& obj, const void* who); |
| 198 | |
| 199 | void flatten_binder(const sp<ProcessState>& proc, |
| 200 | const sp<IBinder>& binder, flat_binder_object* out); |
| 201 | void flatten_binder(const sp<ProcessState>& proc, |
| 202 | const wp<IBinder>& binder, flat_binder_object* out); |
| 203 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 204 | const flat_binder_object& flat, sp<IBinder>* out); |
| 205 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 206 | const flat_binder_object& flat, wp<IBinder>* out); |
| 207 | |
| 208 | }; // namespace android |
| 209 | |
| 210 | // --------------------------------------------------------------------------- |
| 211 | |
| 212 | #endif // ANDROID_PARCEL_H |