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