BinderService.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2010 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_BINDER_SERVICE_H
  17. #define ANDROID_BINDER_SERVICE_H
  18. #include <stdint.h>
  19. #include <utils/Errors.h>
  20. #include <utils/String16.h>
  21. #include <binder/IServiceManager.h>
  22. #include <binder/IPCThreadState.h>
  23. #include <binder/ProcessState.h>
  24. #include <binder/IServiceManager.h>
  25. // ---------------------------------------------------------------------------
  26. namespace android {
  27. template<typename SERVICE>
  28. class BinderService
  29. {
  30. public:
  31. static status_t publish(bool allowIsolated = false) {
  32. sp<IServiceManager> sm(defaultServiceManager());
  33. return sm->addService(
  34. String16(SERVICE::getServiceName()),
  35. new SERVICE(), allowIsolated);
  36. }
  37. static void publishAndJoinThreadPool(bool allowIsolated = false) {
  38. publish(allowIsolated);
  39. joinThreadPool();
  40. }
  41. static void instantiate() { publish(); }
  42. static status_t shutdown() { return NO_ERROR; }
  43. private:
  44. static void joinThreadPool() {
  45. sp<ProcessState> ps(ProcessState::self());
  46. ps->startThreadPool();
  47. ps->giveThreadPoolName();
  48. IPCThreadState::self()->joinThreadPool();
  49. }
  50. };
  51. }; // namespace android
  52. // ---------------------------------------------------------------------------
  53. #endif // ANDROID_BINDER_SERVICE_H