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)
26typedef  int  uid_t;
27#endif
28
29// ---------------------------------------------------------------------------
30namespace android {
31namespace hardware {
32
33class IPCThreadState
34{
35public:
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);
69            void                decStrongHandle(int32_t handle);
70            void                incWeakHandle(int32_t handle);
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);
94private:
95                                IPCThreadState();
96                                ~IPCThreadState();
97
98            status_t            sendReply(const Parcel& reply, uint32_t flags);
99            status_t            waitForResponse(Parcel *reply,
100                                                status_t *acquireResult=NULL);
101            status_t            talkWithDriver(bool doReceive=true);
102            status_t            writeTransactionData(int32_t cmd,
103                                                     uint32_t binderFlags,
104                                                     int32_t handle,
105                                                     uint32_t code,
106                                                     const Parcel& data,
107                                                     status_t* statusBuffer);
108            status_t            getAndExecuteCommand();
109            status_t            executeCommand(int32_t command);
110            void                processPendingDerefs();
111
112            void                clearCaller();
113
114    static  void                threadDestructor(void *st);
115    static  void                freeBuffer(Parcel* parcel,
116                                           const uint8_t* data, size_t dataSize,
117                                           const binder_size_t* objects, size_t objectsSize,
118                                           void* cookie);
119
120    const   sp<ProcessState>    mProcess;
121    const   pid_t               mMyThreadId;
122            Vector<BHwBinder*>    mPendingStrongDerefs;
123            Vector<RefBase::weakref_type*> mPendingWeakDerefs;
124
125            Parcel              mIn;
126            Parcel              mOut;
127            status_t            mLastError;
128            pid_t               mCallingPid;
129            uid_t               mCallingUid;
130            int32_t             mStrictModePolicy;
131            int32_t             mLastTransactionBinderFlags;
132            sp<BHwBinder>         mContextObject;
133};
134
135}; // namespace hardware
136}; // namespace android
137
138// ---------------------------------------------------------------------------
139
140#endif // ANDROID_HARDWARE_IPC_THREAD_STATE_H
141