Mathias Agopian | a1e6bc8 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 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 | |
| 17 | #ifndef ANDROID_BINDER_SERVICE_H |
| 18 | #define ANDROID_BINDER_SERVICE_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/String16.h> |
| 24 | |
| 25 | #include <binder/IServiceManager.h> |
| 26 | #include <binder/IPCThreadState.h> |
| 27 | #include <binder/ProcessState.h> |
| 28 | #include <binder/IServiceManager.h> |
| 29 | |
| 30 | // --------------------------------------------------------------------------- |
| 31 | namespace android { |
| 32 | |
| 33 | template<typename SERVICE> |
| 34 | class BinderService |
| 35 | { |
| 36 | public: |
Dianne Hackborn | a94f129 | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 37 | static status_t publish(bool allowIsolated = false) { |
Mathias Agopian | a1e6bc8 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 38 | sp<IServiceManager> sm(defaultServiceManager()); |
Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 39 | return sm->addService( |
| 40 | String16(SERVICE::getServiceName()), |
| 41 | new SERVICE(), allowIsolated); |
Mathias Agopian | a1e6bc8 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Dianne Hackborn | a94f129 | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 44 | static void publishAndJoinThreadPool(bool allowIsolated = false) { |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 45 | publish(allowIsolated); |
| 46 | joinThreadPool(); |
Mathias Agopian | a1e6bc8 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static void instantiate() { publish(); } |
| 50 | |
Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 51 | static status_t shutdown() { return NO_ERROR; } |
| 52 | |
| 53 | private: |
| 54 | static void joinThreadPool() { |
| 55 | sp<ProcessState> ps(ProcessState::self()); |
| 56 | ps->startThreadPool(); |
| 57 | ps->giveThreadPoolName(); |
| 58 | IPCThreadState::self()->joinThreadPool(); |
Mathias Agopian | a1e6bc8 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 59 | } |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | }; // namespace android |
| 64 | // --------------------------------------------------------------------------- |
| 65 | #endif // ANDROID_BINDER_SERVICE_H |