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 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <utils/Errors.h>
22 #include <utils/String16.h>
23 
24 #include <binder/IServiceManager.h>
25 #include <binder/IPCThreadState.h>
26 #include <binder/ProcessState.h>
27 #include <binder/IServiceManager.h>
28 
29 // ---------------------------------------------------------------------------
30 namespace android {
31 
32 template<typename SERVICE>
33 class BinderService
34 {
35 public:
36     static status_t publish(bool allowIsolated = false,
37                             int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
38         sp<IServiceManager> sm(defaultServiceManager());
39         return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated,
40                               dumpFlags);
41     }
42 
43     static void publishAndJoinThreadPool(
44             bool allowIsolated = false,
45             int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
46         publish(allowIsolated, dumpFlags);
47         joinThreadPool();
48     }
49 
instantiate()50     static void instantiate() { publish(); }
51 
shutdown()52     static status_t shutdown() { return NO_ERROR; }
53 
54 private:
joinThreadPool()55     static void joinThreadPool() {
56         sp<ProcessState> ps(ProcessState::self());
57         ps->startThreadPool();
58         ps->giveThreadPoolName();
59         IPCThreadState::self()->joinThreadPool();
60     }
61 };
62 
63 
64 } // namespace android
65 // ---------------------------------------------------------------------------
66