IInterface.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_IINTERFACE_H
  18. #define ANDROID_IINTERFACE_H
  19. #include <binder/Binder.h>
  20. namespace android {
  21. // ----------------------------------------------------------------------
  22. class IInterface : public virtual RefBase
  23. {
  24. public:
  25. IInterface();
  26. static sp<IBinder> asBinder(const IInterface*);
  27. static sp<IBinder> asBinder(const sp<IInterface>&);
  28. protected:
  29. virtual ~IInterface();
  30. virtual IBinder* onAsBinder() = 0;
  31. };
  32. // ----------------------------------------------------------------------
  33. template<typename INTERFACE>
  34. inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
  35. {
  36. return INTERFACE::asInterface(obj);
  37. }
  38. // ----------------------------------------------------------------------
  39. template<typename INTERFACE>
  40. class BnInterface : public INTERFACE, public BBinder
  41. {
  42. public:
  43. virtual sp<IInterface> queryLocalInterface(const String16& _descriptor);
  44. virtual const String16& getInterfaceDescriptor() const;
  45. protected:
  46. virtual IBinder* onAsBinder();
  47. };
  48. // ----------------------------------------------------------------------
  49. template<typename INTERFACE>
  50. class BpInterface : public INTERFACE, public BpRefBase
  51. {
  52. public:
  53. BpInterface(const sp<IBinder>& remote);
  54. protected:
  55. virtual IBinder* onAsBinder();
  56. };
  57. // ----------------------------------------------------------------------
  58. #define DECLARE_META_INTERFACE(INTERFACE) \
  59. static const android::String16 descriptor; \
  60. static android::sp<I##INTERFACE> asInterface( \
  61. const android::sp<android::IBinder>& obj); \
  62. virtual const android::String16& getInterfaceDescriptor() const; \
  63. I##INTERFACE(); \
  64. virtual ~I##INTERFACE(); \
  65. #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
  66. const android::String16 I##INTERFACE::descriptor(NAME); \
  67. const android::String16& \
  68. I##INTERFACE::getInterfaceDescriptor() const { \
  69. return I##INTERFACE::descriptor; \
  70. } \
  71. android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
  72. const android::sp<android::IBinder>& obj) \
  73. { \
  74. android::sp<I##INTERFACE> intr; \
  75. if (obj != NULL) { \
  76. intr = static_cast<I##INTERFACE*>( \
  77. obj->queryLocalInterface( \
  78. I##INTERFACE::descriptor).get()); \
  79. if (intr == NULL) { \
  80. intr = new Bp##INTERFACE(obj); \
  81. } \
  82. } \
  83. return intr; \
  84. } \
  85. I##INTERFACE::I##INTERFACE() { } \
  86. I##INTERFACE::~I##INTERFACE() { } \
  87. #define CHECK_INTERFACE(interface, data, reply) \
  88. if (!data.checkInterface(this)) { return PERMISSION_DENIED; } \
  89. // ----------------------------------------------------------------------
  90. // No user-serviceable parts after this...
  91. template<typename INTERFACE>
  92. inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
  93. const String16& _descriptor)
  94. {
  95. if (_descriptor == INTERFACE::descriptor) return this;
  96. return NULL;
  97. }
  98. template<typename INTERFACE>
  99. inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const
  100. {
  101. return INTERFACE::getInterfaceDescriptor();
  102. }
  103. template<typename INTERFACE>
  104. IBinder* BnInterface<INTERFACE>::onAsBinder()
  105. {
  106. return this;
  107. }
  108. template<typename INTERFACE>
  109. inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
  110. : BpRefBase(remote)
  111. {
  112. }
  113. template<typename INTERFACE>
  114. inline IBinder* BpInterface<INTERFACE>::onAsBinder()
  115. {
  116. return remote();
  117. }
  118. // ----------------------------------------------------------------------
  119. }; // namespace android
  120. #endif // ANDROID_IINTERFACE_H