1a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian/*
2a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * Copyright (C) 2010 The Android Open Source Project
3a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian *
4a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * you may not use this file except in compliance with the License.
6a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * You may obtain a copy of the License at
7a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian *
8a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian *
10a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * See the License for the specific language governing permissions and
14a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian * limitations under the License.
15a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian */
16a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
17a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#ifndef ANDROID_BINDER_SERVICE_H
18a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#define ANDROID_BINDER_SERVICE_H
19a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
20a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#include <stdint.h>
21a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
22a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#include <utils/Errors.h>
23a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#include <utils/String16.h>
24a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
25a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#include <binder/IServiceManager.h>
26a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#include <binder/IPCThreadState.h>
27a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#include <binder/ProcessState.h>
28a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#include <binder/IServiceManager.h>
29a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
30a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian// ---------------------------------------------------------------------------
31a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopiannamespace android {
32a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
33a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopiantemplate<typename SERVICE>
34a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopianclass BinderService
35a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian{
36a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopianpublic:
37a94f129a7bbaa8ea45c50e49ba3e6127ca2a15ecDianne Hackborn    static status_t publish(bool allowIsolated = false) {
38a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian        sp<IServiceManager> sm(defaultServiceManager());
39e3e43b384e0603e5883b501cdb169641fab8fea2Mathias Agopian        return sm->addService(
40e3e43b384e0603e5883b501cdb169641fab8fea2Mathias Agopian                String16(SERVICE::getServiceName()),
41e3e43b384e0603e5883b501cdb169641fab8fea2Mathias Agopian                new SERVICE(), allowIsolated);
42a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian    }
43a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
44a94f129a7bbaa8ea45c50e49ba3e6127ca2a15ecDianne Hackborn    static void publishAndJoinThreadPool(bool allowIsolated = false) {
45b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian        publish(allowIsolated);
46b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian        joinThreadPool();
47a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian    }
48a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
49a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian    static void instantiate() { publish(); }
50a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
51b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    static status_t shutdown() { return NO_ERROR; }
52b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian
53b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopianprivate:
54b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    static void joinThreadPool() {
55b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian        sp<ProcessState> ps(ProcessState::self());
56b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian        ps->startThreadPool();
57b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian        ps->giveThreadPoolName();
58b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian        IPCThreadState::self()->joinThreadPool();
59a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian    }
60a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian};
61a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
62a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian
63a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian}; // namespace android
64a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian// ---------------------------------------------------------------------------
65a1e6bc864fb821c1b470b7aad9b75c441f54eeb4Mathias Agopian#endif // ANDROID_BINDER_SERVICE_H
66