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