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