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_IPC_THREAD_STATE_H 18 #define ANDROID_IPC_THREAD_STATE_H 19 20 #include <utils/Errors.h> 21 #include <binder/Parcel.h> 22 #include <binder/ProcessState.h> 23 #include <utils/Vector.h> 24 25 #if defined(_WIN32) 26 typedef int uid_t; 27 #endif 28 29 // --------------------------------------------------------------------------- 30 namespace android { 31 32 class IPCThreadState 33 { 34 public: 35 static IPCThreadState* self(); 36 static IPCThreadState* selfOrNull(); // self(), but won't instantiate 37 38 sp<ProcessState> process(); 39 40 status_t clearLastError(); 41 42 pid_t getCallingPid() const; 43 uid_t getCallingUid() const; 44 45 void setStrictModePolicy(int32_t policy); 46 int32_t getStrictModePolicy() const; 47 48 void setLastTransactionBinderFlags(int32_t flags); 49 int32_t getLastTransactionBinderFlags() const; 50 51 int64_t clearCallingIdentity(); 52 void restoreCallingIdentity(int64_t token); 53 54 int setupPolling(int* fd); 55 status_t handlePolledCommands(); 56 void flushCommands(); 57 58 void joinThreadPool(bool isMain = true); 59 60 // Stop the local process. 61 void stopProcess(bool immediate = true); 62 63 status_t transact(int32_t handle, 64 uint32_t code, const Parcel& data, 65 Parcel* reply, uint32_t flags); 66 67 void incStrongHandle(int32_t handle, BpBinder *proxy); 68 void decStrongHandle(int32_t handle); 69 void incWeakHandle(int32_t handle, BpBinder *proxy); 70 void decWeakHandle(int32_t handle); 71 status_t attemptIncStrongHandle(int32_t handle); 72 static void expungeHandle(int32_t handle, IBinder* binder); 73 status_t requestDeathNotification( int32_t handle, 74 BpBinder* proxy); 75 status_t clearDeathNotification( int32_t handle, 76 BpBinder* proxy); 77 78 static void shutdown(); 79 80 // Call this to disable switching threads to background scheduling when 81 // receiving incoming IPC calls. This is specifically here for the 82 // Android system process, since it expects to have background apps calling 83 // in to it but doesn't want to acquire locks in its services while in 84 // the background. 85 static void disableBackgroundScheduling(bool disable); 86 bool backgroundSchedulingDisabled(); 87 88 // Call blocks until the number of executing binder threads is less than 89 // the maximum number of binder threads threads allowed for this process. 90 void blockUntilThreadAvailable(); 91 static void freeBuffer(Parcel* parcel, 92 const uint8_t* data, size_t dataSize, 93 const binder_size_t* objects, size_t objectsSize, 94 void* cookie); 95 static void freeBuffer1(Parcel* parcel, 96 const uint8_t* data, size_t dataSize, 97 const binder_size_t* objects, size_t objectsSize, 98 void* cookie); 99 private: 100 IPCThreadState(); 101 ~IPCThreadState(); 102 103 status_t sendReply(const Parcel& reply, uint32_t flags); 104 status_t waitForResponse(Parcel *reply, 105 status_t *acquireResult=NULL); 106 status_t talkWithDriver(bool doReceive=true); 107 status_t writeTransactionData(int32_t cmd, 108 uint32_t binderFlags, 109 int32_t handle, 110 uint32_t code, 111 const Parcel& data, 112 status_t* statusBuffer); 113 status_t getAndExecuteCommand(); 114 status_t executeCommand(int32_t command); 115 void processPendingDerefs(); 116 void processPostWriteDerefs(); 117 118 void clearCaller(); 119 120 static void threadDestructor(void *st); 121 122 const sp<ProcessState> mProcess; 123 Vector<BBinder*> mPendingStrongDerefs; 124 Vector<RefBase::weakref_type*> mPendingWeakDerefs; 125 Vector<RefBase*> mPostWriteStrongDerefs; 126 Vector<RefBase::weakref_type*> mPostWriteWeakDerefs; 127 Parcel mIn; 128 Parcel mOut; 129 status_t mLastError; 130 pid_t mCallingPid; 131 uid_t mCallingUid; 132 int32_t mStrictModePolicy; 133 int32_t mLastTransactionBinderFlags; 134 }; 135 136 }; // namespace android 137 138 // --------------------------------------------------------------------------- 139 140 #endif // ANDROID_IPC_THREAD_STATE_H 141