IPCThreadState.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef ANDROID_IPC_THREAD_STATE_H
  17. #define ANDROID_IPC_THREAD_STATE_H
  18. #include <utils/Errors.h>
  19. #include <binder/Parcel.h>
  20. #include <binder/ProcessState.h>
  21. #include <utils/Vector.h>
  22. #if defined(_WIN32)
  23. typedef int uid_t;
  24. #endif
  25. // ---------------------------------------------------------------------------
  26. namespace android {
  27. class IPCThreadState
  28. {
  29. public:
  30. static IPCThreadState* self();
  31. static IPCThreadState* selfOrNull(); // self(), but won't instantiate
  32. sp<ProcessState> process();
  33. status_t clearLastError();
  34. pid_t getCallingPid() const;
  35. uid_t getCallingUid() const;
  36. void setStrictModePolicy(int32_t policy);
  37. int32_t getStrictModePolicy() const;
  38. void setLastTransactionBinderFlags(int32_t flags);
  39. int32_t getLastTransactionBinderFlags() const;
  40. int64_t clearCallingIdentity();
  41. void restoreCallingIdentity(int64_t token);
  42. int setupPolling(int* fd);
  43. status_t handlePolledCommands();
  44. void flushCommands();
  45. void joinThreadPool(bool isMain = true);
  46. // Stop the local process.
  47. void stopProcess(bool immediate = true);
  48. status_t transact(int32_t handle,
  49. uint32_t code, const Parcel& data,
  50. Parcel* reply, uint32_t flags);
  51. void incStrongHandle(int32_t handle);
  52. void decStrongHandle(int32_t handle);
  53. void incWeakHandle(int32_t handle);
  54. void decWeakHandle(int32_t handle);
  55. status_t attemptIncStrongHandle(int32_t handle);
  56. static void expungeHandle(int32_t handle, IBinder* binder);
  57. status_t requestDeathNotification( int32_t handle,
  58. BpBinder* proxy);
  59. status_t clearDeathNotification( int32_t handle,
  60. BpBinder* proxy);
  61. static void shutdown();
  62. // Call this to disable switching threads to background scheduling when
  63. // receiving incoming IPC calls. This is specifically here for the
  64. // Android system process, since it expects to have background apps calling
  65. // in to it but doesn't want to acquire locks in its services while in
  66. // the background.
  67. static void disableBackgroundScheduling(bool disable);
  68. // Call blocks until the number of executing binder threads is less than
  69. // the maximum number of binder threads threads allowed for this process.
  70. void blockUntilThreadAvailable();
  71. private:
  72. IPCThreadState();
  73. ~IPCThreadState();
  74. status_t sendReply(const Parcel& reply, uint32_t flags);
  75. status_t waitForResponse(Parcel *reply,
  76. status_t *acquireResult=NULL);
  77. status_t talkWithDriver(bool doReceive=true);
  78. status_t writeTransactionData(int32_t cmd,
  79. uint32_t binderFlags,
  80. int32_t handle,
  81. uint32_t code,
  82. const Parcel& data,
  83. status_t* statusBuffer);
  84. status_t getAndExecuteCommand();
  85. status_t executeCommand(int32_t command);
  86. void processPendingDerefs();
  87. void clearCaller();
  88. static void threadDestructor(void *st);
  89. static void freeBuffer(Parcel* parcel,
  90. const uint8_t* data, size_t dataSize,
  91. const binder_size_t* objects, size_t objectsSize,
  92. void* cookie);
  93. const sp<ProcessState> mProcess;
  94. const pid_t mMyThreadId;
  95. Vector<BBinder*> mPendingStrongDerefs;
  96. Vector<RefBase::weakref_type*> mPendingWeakDerefs;
  97. Parcel mIn;
  98. Parcel mOut;
  99. status_t mLastError;
  100. pid_t mCallingPid;
  101. uid_t mCallingUid;
  102. int32_t mStrictModePolicy;
  103. int32_t mLastTransactionBinderFlags;
  104. };
  105. }; // namespace android
  106. // ---------------------------------------------------------------------------
  107. #endif // ANDROID_IPC_THREAD_STATE_H