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 // WARNING: deprecated - DO NOT USE - prefer to setup service directly.
30 //
31 // This class embellishes a class with a few static methods which can be used in
32 // limited circumstances (when one service needs to be registered and
33 // published). However, this is an anti-pattern:
34 // - these methods are aliases of existing methods, and as such, represent an
35 //   incremental amount of information required to understand the system but
36 //   which does not actually save in terms of lines of code. For instance, users
37 //   of this class should be surprised to know that this will start up to 16
38 //   threads in the binder threadpool.
39 // - the template instantiation costs need to be paid, even though everything
40 //   done here is generic.
41 // - the getServiceName API here is undocumented and non-local (for instance,
42 //   this unnecessarily assumes a single service type will only be instantiated
43 //   once with no arguments).
44 //
45 // So, DO NOT USE.
46 
47 // ---------------------------------------------------------------------------
48 namespace android {
49 
50 template<typename SERVICE>
51 class BinderService
52 {
53 public:
54     static status_t publish(bool allowIsolated = false,
55                             int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
56         sp<IServiceManager> sm(defaultServiceManager());
57         return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated,
58                               dumpFlags);
59     }
60 
61     static void publishAndJoinThreadPool(
62             bool allowIsolated = false,
63             int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
64         publish(allowIsolated, dumpFlags);
65         joinThreadPool();
66     }
67 
instantiate()68     static void instantiate() { publish(); }
69 
shutdown()70     static status_t shutdown() { return NO_ERROR; }
71 
72 private:
joinThreadPool()73     static void joinThreadPool() {
74         sp<ProcessState> ps(ProcessState::self());
75         ps->startThreadPool();
76         ps->giveThreadPoolName();
77         IPCThreadState::self()->joinThreadPool();
78     }
79 };
80 
81 
82 } // namespace android
83 // ---------------------------------------------------------------------------
84