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