The Android Open Source Project | edbf3b6 | 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 | |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 29 | class Flattenable; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | class IBinder; |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 31 | class IPCThreadState; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | class ProcessState; |
| 33 | class String8; |
| 34 | class TextOutput; |
| 35 | |
| 36 | struct flat_binder_object; // defined in support_p/binder_module.h |
| 37 | |
| 38 | class Parcel |
| 39 | { |
| 40 | public: |
| 41 | Parcel(); |
| 42 | ~Parcel(); |
| 43 | |
| 44 | const uint8_t* data() const; |
| 45 | size_t dataSize() const; |
| 46 | size_t dataAvail() const; |
| 47 | size_t dataPosition() const; |
| 48 | size_t dataCapacity() const; |
| 49 | |
| 50 | status_t setDataSize(size_t size); |
| 51 | void setDataPosition(size_t pos) const; |
| 52 | status_t setDataCapacity(size_t size); |
| 53 | |
| 54 | status_t setData(const uint8_t* buffer, size_t len); |
| 55 | |
Andreas Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 56 | status_t appendFrom(const Parcel *parcel, |
| 57 | size_t start, size_t len); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 59 | bool hasFileDescriptors() const; |
| 60 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 61 | // Writes the RPC header. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | status_t writeInterfaceToken(const String16& interface); |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 63 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 64 | // Parses the RPC header, returning true if the interface name |
| 65 | // in the header matches the expected interface from the caller. |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 66 | // |
| 67 | // Additionally, enforceInterface does part of the work of |
| 68 | // propagating the StrictMode policy mask, populating the current |
| 69 | // IPCThreadState, which as an optimization may optionally be |
| 70 | // passed in. |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 71 | bool enforceInterface(const String16& interface, |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 72 | IPCThreadState* threadState = NULL) const; |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 73 | bool checkInterface(IBinder*) const; |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 74 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | void freeData(); |
| 76 | |
| 77 | const size_t* objects() const; |
| 78 | size_t objectsCount() const; |
| 79 | |
| 80 | status_t errorCheck() const; |
| 81 | void setError(status_t err); |
| 82 | |
| 83 | status_t write(const void* data, size_t len); |
| 84 | void* writeInplace(size_t len); |
| 85 | status_t writeUnpadded(const void* data, size_t len); |
| 86 | status_t writeInt32(int32_t val); |
| 87 | status_t writeInt64(int64_t val); |
| 88 | status_t writeFloat(float val); |
| 89 | status_t writeDouble(double val); |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 90 | status_t writeIntPtr(intptr_t val); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | status_t writeCString(const char* str); |
| 92 | status_t writeString8(const String8& str); |
| 93 | status_t writeString16(const String16& str); |
| 94 | status_t writeString16(const char16_t* str, size_t len); |
| 95 | status_t writeStrongBinder(const sp<IBinder>& val); |
| 96 | status_t writeWeakBinder(const wp<IBinder>& val); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 97 | status_t write(const Flattenable& val); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 99 | // Place a native_handle into the parcel (the native_handle's file- |
| 100 | // descriptors are dup'ed, so it is safe to delete the native_handle |
| 101 | // when this function returns). |
| 102 | // Doesn't take ownership of the native_handle. |
| 103 | status_t writeNativeHandle(const native_handle* handle); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | |
| 105 | // Place a file descriptor into the parcel. The given fd must remain |
| 106 | // valid for the lifetime of the parcel. |
| 107 | status_t writeFileDescriptor(int fd); |
| 108 | |
| 109 | // Place a file descriptor into the parcel. A dup of the fd is made, which |
| 110 | // will be closed once the parcel is destroyed. |
| 111 | status_t writeDupFileDescriptor(int fd); |
Bart Sears | 8acda78 | 2011-09-25 14:30:21 -0700 | [diff] [blame] | 112 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | status_t writeObject(const flat_binder_object& val, bool nullMetaData); |
| 114 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 115 | // Like Parcel.java's writeNoException(). Just writes a zero int32. |
| 116 | // Currently the native implementation doesn't do any of the StrictMode |
| 117 | // stack gathering and serialization that the Java implementation does. |
| 118 | status_t writeNoException(); |
| 119 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | void remove(size_t start, size_t amt); |
| 121 | |
| 122 | status_t read(void* outData, size_t len) const; |
| 123 | const void* readInplace(size_t len) const; |
| 124 | int32_t readInt32() const; |
| 125 | status_t readInt32(int32_t *pArg) const; |
| 126 | int64_t readInt64() const; |
| 127 | status_t readInt64(int64_t *pArg) const; |
| 128 | float readFloat() const; |
| 129 | status_t readFloat(float *pArg) const; |
| 130 | double readDouble() const; |
| 131 | status_t readDouble(double *pArg) const; |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 132 | intptr_t readIntPtr() const; |
| 133 | status_t readIntPtr(intptr_t *pArg) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | |
| 135 | const char* readCString() const; |
| 136 | String8 readString8() const; |
| 137 | String16 readString16() const; |
| 138 | const char16_t* readString16Inplace(size_t* outLen) const; |
| 139 | sp<IBinder> readStrongBinder() const; |
| 140 | wp<IBinder> readWeakBinder() const; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 141 | status_t read(Flattenable& val) const; |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 142 | |
| 143 | // Like Parcel.java's readExceptionCode(). Reads the first int32 |
| 144 | // off of a Parcel's header, returning 0 or the negative error |
| 145 | // code on exceptions, but also deals with skipping over rich |
| 146 | // response headers. Callers should use this to read & parse the |
| 147 | // response headers rather than doing it by hand. |
| 148 | int32_t readExceptionCode() const; |
| 149 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 150 | // Retrieve native_handle from the parcel. This returns a copy of the |
| 151 | // parcel's native_handle (the caller takes ownership). The caller |
| 152 | // must free the native_handle with native_handle_close() and |
| 153 | // native_handle_delete(). |
| 154 | native_handle* readNativeHandle() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
| 156 | |
| 157 | // Retrieve a file descriptor from the parcel. This returns the raw fd |
| 158 | // in the parcel, which you do not own -- use dup() to get your own copy. |
| 159 | int readFileDescriptor() const; |
Bart Sears | 8acda78 | 2011-09-25 14:30:21 -0700 | [diff] [blame] | 160 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | const flat_binder_object* readObject(bool nullMetaData) const; |
| 162 | |
| 163 | // Explicitly close all file descriptors in the parcel. |
| 164 | void closeFileDescriptors(); |
| 165 | |
| 166 | typedef void (*release_func)(Parcel* parcel, |
| 167 | const uint8_t* data, size_t dataSize, |
| 168 | const size_t* objects, size_t objectsSize, |
| 169 | void* cookie); |
| 170 | |
| 171 | const uint8_t* ipcData() const; |
| 172 | size_t ipcDataSize() const; |
| 173 | const size_t* ipcObjects() const; |
| 174 | size_t ipcObjectsCount() const; |
| 175 | void ipcSetDataReference(const uint8_t* data, size_t dataSize, |
| 176 | const size_t* objects, size_t objectsCount, |
| 177 | release_func relFunc, void* relCookie); |
| 178 | |
| 179 | void print(TextOutput& to, uint32_t flags = 0) const; |
Bart Sears | 8acda78 | 2011-09-25 14:30:21 -0700 | [diff] [blame] | 180 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | private: |
| 182 | Parcel(const Parcel& o); |
| 183 | Parcel& operator=(const Parcel& o); |
| 184 | |
| 185 | status_t finishWrite(size_t len); |
| 186 | void releaseObjects(); |
| 187 | void acquireObjects(); |
| 188 | status_t growData(size_t len); |
| 189 | status_t restartWrite(size_t desired); |
| 190 | status_t continueWrite(size_t desired); |
| 191 | void freeDataNoInit(); |
| 192 | void initState(); |
| 193 | void scanForFds() const; |
| 194 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 195 | template<class T> |
| 196 | status_t readAligned(T *pArg) const; |
| 197 | |
| 198 | template<class T> T readAligned() const; |
| 199 | |
| 200 | template<class T> |
| 201 | status_t writeAligned(T val); |
| 202 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | status_t mError; |
| 204 | uint8_t* mData; |
| 205 | size_t mDataSize; |
| 206 | size_t mDataCapacity; |
| 207 | mutable size_t mDataPos; |
| 208 | size_t* mObjects; |
| 209 | size_t mObjectsSize; |
| 210 | size_t mObjectsCapacity; |
| 211 | mutable size_t mNextObjectHint; |
| 212 | |
| 213 | mutable bool mFdsKnown; |
| 214 | mutable bool mHasFds; |
| 215 | |
| 216 | release_func mOwner; |
| 217 | void* mOwnerCookie; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | // --------------------------------------------------------------------------- |
| 221 | |
| 222 | inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) |
| 223 | { |
| 224 | parcel.print(to); |
| 225 | return to; |
| 226 | } |
| 227 | |
| 228 | // --------------------------------------------------------------------------- |
| 229 | |
| 230 | // Generic acquire and release of objects. |
| 231 | void acquire_object(const sp<ProcessState>& proc, |
| 232 | const flat_binder_object& obj, const void* who); |
| 233 | void release_object(const sp<ProcessState>& proc, |
| 234 | const flat_binder_object& obj, const void* who); |
| 235 | |
| 236 | void flatten_binder(const sp<ProcessState>& proc, |
| 237 | const sp<IBinder>& binder, flat_binder_object* out); |
| 238 | void flatten_binder(const sp<ProcessState>& proc, |
| 239 | const wp<IBinder>& binder, flat_binder_object* out); |
| 240 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 241 | const flat_binder_object& flat, sp<IBinder>* out); |
| 242 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 243 | const flat_binder_object& flat, wp<IBinder>* out); |
| 244 | |
| 245 | }; // namespace android |
| 246 | |
| 247 | // --------------------------------------------------------------------------- |
| 248 | |
| 249 | #endif // ANDROID_PARCEL_H |