blob: 5ac36d901e960cfb9079853d0f00eaaf366e8be7 [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:
Dianne Hackborna94f1292012-02-09 16:12:18 -080037 static status_t publish(bool allowIsolated = false) {
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070038 sp<IServiceManager> sm(defaultServiceManager());
Mathias Agopiane3e43b32013-03-07 15:34:28 -080039 return sm->addService(
40 String16(SERVICE::getServiceName()),
41 new SERVICE(), allowIsolated);
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070042 }
43
Dianne Hackborna94f1292012-02-09 16:12:18 -080044 static void publishAndJoinThreadPool(bool allowIsolated = false) {
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070045 sp<IServiceManager> sm(defaultServiceManager());
Mathias Agopiane3e43b32013-03-07 15:34:28 -080046 sm->addService(
47 String16(SERVICE::getServiceName()),
48 new SERVICE(), allowIsolated);
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070049 ProcessState::self()->startThreadPool();
Mathias Agopiane3e43b32013-03-07 15:34:28 -080050 ProcessState::self()->giveThreadPoolName();
Mathias Agopiana1e6bc82010-07-14 18:41:18 -070051 IPCThreadState::self()->joinThreadPool();
52 }
53
54 static void instantiate() { publish(); }
55
56 static status_t shutdown() {
57 return NO_ERROR;
58 }
59};
60
61
62}; // namespace android
63// ---------------------------------------------------------------------------
64#endif // ANDROID_BINDER_SERVICE_H