blob: 9230e89cdf40913dce7d3206ee9ce7969686a41c [file] [log] [blame]
Mathias Agopiana1e6bc82010-07-14 18:41:18 -07001/*
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// ---------------------------------------------------------------------------
31namespace android {
32
33template<typename SERVICE>
34class BinderService
35{
36public:
Vishnu Nairf56042d2017-09-19 15:25:10 -070037 static status_t publish(bool allowIsolated = false,
Vishnu Nair64afc022018-02-01 15:29:34 -080038 int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070039 sp<IServiceManager> sm(defaultServiceManager());
Vishnu Nairf56042d2017-09-19 15:25:10 -070040 return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated,
Vishnu Nair6a408532017-10-24 09:11:27 -070041 dumpFlags);
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070042 }
43
Vishnu Nair6a408532017-10-24 09:11:27 -070044 static void publishAndJoinThreadPool(
45 bool allowIsolated = false,
Vishnu Nair64afc022018-02-01 15:29:34 -080046 int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
Vishnu Nair6a408532017-10-24 09:11:27 -070047 publish(allowIsolated, dumpFlags);
Mathias Agopianb6df7d02013-05-09 14:53:35 -070048 joinThreadPool();
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070049 }
50
51 static void instantiate() { publish(); }
52
Mathias Agopianb6df7d02013-05-09 14:53:35 -070053 static status_t shutdown() { return NO_ERROR; }
54
55private:
56 static void joinThreadPool() {
57 sp<ProcessState> ps(ProcessState::self());
58 ps->startThreadPool();
59 ps->giveThreadPoolName();
60 IPCThreadState::self()->joinThreadPool();
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070061 }
62};
63
64
65}; // namespace android
66// ---------------------------------------------------------------------------
67#endif // ANDROID_BINDER_SERVICE_H