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