1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2005 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#ifndef ANDROID_IPC_THREAD_STATE_H
18edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define ANDROID_IPC_THREAD_STATE_H
19edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
20edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Errors.h>
21c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/Parcel.h>
22c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/ProcessState.h>
23edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Vector.h>
24edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
25edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#ifdef HAVE_WIN32_PROC
26edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projecttypedef  int  uid_t;
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
28edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
29edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
31edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass IPCThreadState
33edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
34edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectpublic:
35edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    static  IPCThreadState*     self();
361b6084354710ca52c04ea361d751d19174802d57Brad Fitzpatrick    static  IPCThreadState*     selfOrNull();  // self(), but won't instantiate
37edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
38edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            sp<ProcessState>    process();
39edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
40edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            clearLastError();
41edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
42ada11c5e67cf8587ad3b53dd99c27455ef26291fJeff Brown            int                 getCallingPid() const;
43ada11c5e67cf8587ad3b53dd99c27455ef26291fJeff Brown            int                 getCallingUid() const;
44702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick
45702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick            void                setStrictModePolicy(int32_t policy);
46702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick            int32_t             getStrictModePolicy() const;
475273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick
485273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick            void                setLastTransactionBinderFlags(int32_t flags);
495273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick            int32_t             getLastTransactionBinderFlags() const;
505273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick
51edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            int64_t             clearCallingIdentity();
52edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                restoreCallingIdentity(int64_t token);
53edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
548d96cab8bfc1b40a8e05c6f43b485750a5ae0a52Todd Poynor            int                 setupPolling(int* fd);
558d96cab8bfc1b40a8e05c6f43b485750a5ae0a52Todd Poynor            status_t            handlePolledCommands();
56edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                flushCommands();
57edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
58edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                joinThreadPool(bool isMain = true);
59edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
60edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // Stop the local process.
61edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                stopProcess(bool immediate = true);
62edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
63edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            transact(int32_t handle,
64edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                         uint32_t code, const Parcel& data,
65edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                         Parcel* reply, uint32_t flags);
66edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
67edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                incStrongHandle(int32_t handle);
68edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                decStrongHandle(int32_t handle);
69edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                incWeakHandle(int32_t handle);
70edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                decWeakHandle(int32_t handle);
71edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            attemptIncStrongHandle(int32_t handle);
72edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    static  void                expungeHandle(int32_t handle, IBinder* binder);
73edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            requestDeathNotification(   int32_t handle,
74edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                            BpBinder* proxy);
75edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            clearDeathNotification( int32_t handle,
76edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                        BpBinder* proxy);
77edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
78edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    static  void                shutdown();
79edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
808c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn    // Call this to disable switching threads to background scheduling when
818c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn    // receiving incoming IPC calls.  This is specifically here for the
828c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn    // Android system process, since it expects to have background apps calling
838c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn    // in to it but doesn't want to acquire locks in its services while in
848c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn    // the background.
858c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn    static  void                disableBackgroundScheduling(bool disable);
868c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn
87edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectprivate:
88edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                IPCThreadState();
89edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                ~IPCThreadState();
90edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
91edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            sendReply(const Parcel& reply, uint32_t flags);
92edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            waitForResponse(Parcel *reply,
93edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                status_t *acquireResult=NULL);
94edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            talkWithDriver(bool doReceive=true);
95edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            writeTransactionData(int32_t cmd,
96edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                     uint32_t binderFlags,
97edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                     int32_t handle,
98edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                     uint32_t code,
99edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                     const Parcel& data,
100edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                                     status_t* statusBuffer);
1018d96cab8bfc1b40a8e05c6f43b485750a5ae0a52Todd Poynor            status_t            getAndExecuteCommand();
102edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            executeCommand(int32_t command);
1038d96cab8bfc1b40a8e05c6f43b485750a5ae0a52Todd Poynor            void                processPendingDerefs();
104edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
105edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            void                clearCaller();
106edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
107edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    static  void                threadDestructor(void *st);
108edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    static  void                freeBuffer(Parcel* parcel,
109edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                           const uint8_t* data, size_t dataSize,
11084e625ac1e01f5a9c3ed16664da6498212ed662bArve Hjønnevåg                                           const binder_size_t* objects, size_t objectsSize,
111edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                                           void* cookie);
112edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
113edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const   sp<ProcessState>    mProcess;
1148c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn    const   pid_t               mMyThreadId;
115edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            Vector<BBinder*>    mPendingStrongDerefs;
116edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            Vector<RefBase::weakref_type*> mPendingWeakDerefs;
1178c6cedc9bc9a4b69616a79a95449f6f6b08c7bf1Dianne Hackborn
118edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            Parcel              mIn;
119edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            Parcel              mOut;
120edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            status_t            mLastError;
121edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            pid_t               mCallingPid;
122edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            uid_t               mCallingUid;
123702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick            int32_t             mStrictModePolicy;
1245273603e98d2db3bac656b7bcf5352c04c86d62fBrad Fitzpatrick            int32_t             mLastTransactionBinderFlags;
125edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project};
126702ea9d42f52fc145090c0f0bfbe64993e4b8b33Brad Fitzpatrick
127edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}; // namespace android
128edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
129edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
130edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
131edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif // ANDROID_IPC_THREAD_STATE_H
132