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#ifdef HAVE_WIN32_PROC
26typedef  int  uid_t;
27#endif
28
29// ---------------------------------------------------------------------------
30namespace android {
31
32class IPCThreadState
33{
34public:
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            int                 getCallingPid();
43            int                 getCallingUid();
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            void                flushCommands();
55
56            void                joinThreadPool(bool isMain = true);
57
58            // Stop the local process.
59            void                stopProcess(bool immediate = true);
60
61            status_t            transact(int32_t handle,
62                                         uint32_t code, const Parcel& data,
63                                         Parcel* reply, uint32_t flags);
64
65            void                incStrongHandle(int32_t handle);
66            void                decStrongHandle(int32_t handle);
67            void                incWeakHandle(int32_t handle);
68            void                decWeakHandle(int32_t handle);
69            status_t            attemptIncStrongHandle(int32_t handle);
70    static  void                expungeHandle(int32_t handle, IBinder* binder);
71            status_t            requestDeathNotification(   int32_t handle,
72                                                            BpBinder* proxy);
73            status_t            clearDeathNotification( int32_t handle,
74                                                        BpBinder* proxy);
75
76    static  void                shutdown();
77
78    // Call this to disable switching threads to background scheduling when
79    // receiving incoming IPC calls.  This is specifically here for the
80    // Android system process, since it expects to have background apps calling
81    // in to it but doesn't want to acquire locks in its services while in
82    // the background.
83    static  void                disableBackgroundScheduling(bool disable);
84
85private:
86                                IPCThreadState();
87                                ~IPCThreadState();
88
89            status_t            sendReply(const Parcel& reply, uint32_t flags);
90            status_t            waitForResponse(Parcel *reply,
91                                                status_t *acquireResult=NULL);
92            status_t            talkWithDriver(bool doReceive=true);
93            status_t            writeTransactionData(int32_t cmd,
94                                                     uint32_t binderFlags,
95                                                     int32_t handle,
96                                                     uint32_t code,
97                                                     const Parcel& data,
98                                                     status_t* statusBuffer);
99            status_t            executeCommand(int32_t command);
100
101            void                clearCaller();
102
103    static  void                threadDestructor(void *st);
104    static  void                freeBuffer(Parcel* parcel,
105                                           const uint8_t* data, size_t dataSize,
106                                           const size_t* objects, size_t objectsSize,
107                                           void* cookie);
108
109    const   sp<ProcessState>    mProcess;
110    const   pid_t               mMyThreadId;
111            Vector<BBinder*>    mPendingStrongDerefs;
112            Vector<RefBase::weakref_type*> mPendingWeakDerefs;
113
114            Parcel              mIn;
115            Parcel              mOut;
116            status_t            mLastError;
117            pid_t               mCallingPid;
118            uid_t               mCallingUid;
119            int32_t             mStrictModePolicy;
120            int32_t             mLastTransactionBinderFlags;
121};
122
123}; // namespace android
124
125// ---------------------------------------------------------------------------
126
127#endif // ANDROID_IPC_THREAD_STATE_H
128