1 /*
2 * Copyright (C) 2005 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 //
18 #ifndef ANDROID_ISERVICE_MANAGER_H
19 #define ANDROID_ISERVICE_MANAGER_H
20
21 #include <binder/IInterface.h>
22 #include <utils/Vector.h>
23 #include <utils/String16.h>
24
25 namespace android {
26
27 // ----------------------------------------------------------------------
28
29 /**
30 * Service manager for C++ services.
31 *
32 * IInterface is only for legacy ABI compatibility
33 */
34 class IServiceManager : public IInterface
35 {
36 public:
37 // for ABI compatibility
38 virtual const String16& getInterfaceDescriptor() const;
39
40 IServiceManager();
41 virtual ~IServiceManager();
42
43 /**
44 * Must match values in IServiceManager.aidl
45 */
46 /* Allows services to dump sections according to priorities. */
47 static const int DUMP_FLAG_PRIORITY_CRITICAL = 1 << 0;
48 static const int DUMP_FLAG_PRIORITY_HIGH = 1 << 1;
49 static const int DUMP_FLAG_PRIORITY_NORMAL = 1 << 2;
50 /**
51 * Services are by default registered with a DEFAULT dump priority. DEFAULT priority has the
52 * same priority as NORMAL priority but the services are not called with dump priority
53 * arguments.
54 */
55 static const int DUMP_FLAG_PRIORITY_DEFAULT = 1 << 3;
56 static const int DUMP_FLAG_PRIORITY_ALL = DUMP_FLAG_PRIORITY_CRITICAL |
57 DUMP_FLAG_PRIORITY_HIGH | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PRIORITY_DEFAULT;
58 static const int DUMP_FLAG_PROTO = 1 << 4;
59
60 /**
61 * Retrieve an existing service, blocking for a few seconds
62 * if it doesn't yet exist.
63 */
64 virtual sp<IBinder> getService( const String16& name) const = 0;
65
66 /**
67 * Retrieve an existing service, non-blocking.
68 */
69 virtual sp<IBinder> checkService( const String16& name) const = 0;
70
71 /**
72 * Register a service.
73 */
74 // NOLINTNEXTLINE(google-default-arguments)
75 virtual status_t addService(const String16& name, const sp<IBinder>& service,
76 bool allowIsolated = false,
77 int dumpsysFlags = DUMP_FLAG_PRIORITY_DEFAULT) = 0;
78
79 /**
80 * Return list of all existing services.
81 */
82 // NOLINTNEXTLINE(google-default-arguments)
83 virtual Vector<String16> listServices(int dumpsysFlags = DUMP_FLAG_PRIORITY_ALL) = 0;
84
85 /**
86 * Efficiently wait for a service.
87 *
88 * Returns nullptr only for permission problem or fatal error.
89 */
90 virtual sp<IBinder> waitForService(const String16& name) = 0;
91
92 /**
93 * Check if a service is declared (e.g. VINTF manifest).
94 *
95 * If this returns true, waitForService should always be able to return the
96 * service.
97 */
98 virtual bool isDeclared(const String16& name) = 0;
99 };
100
101 sp<IServiceManager> defaultServiceManager();
102
103 /**
104 * Directly set the default service manager. Only used for testing.
105 * Note that the caller is responsible for caling this method
106 * *before* any call to defaultServiceManager(); if the latter is
107 * called first, setDefaultServiceManager() will abort.
108 */
109 void setDefaultServiceManager(const sp<IServiceManager>& sm);
110
111 template<typename INTERFACE>
waitForService(const String16 & name)112 sp<INTERFACE> waitForService(const String16& name) {
113 const sp<IServiceManager> sm = defaultServiceManager();
114 return interface_cast<INTERFACE>(sm->waitForService(name));
115 }
116
117 template<typename INTERFACE>
waitForDeclaredService(const String16 & name)118 sp<INTERFACE> waitForDeclaredService(const String16& name) {
119 const sp<IServiceManager> sm = defaultServiceManager();
120 if (!sm->isDeclared(name)) return nullptr;
121 return interface_cast<INTERFACE>(sm->waitForService(name));
122 }
123
124 template <typename INTERFACE>
checkDeclaredService(const String16 & name)125 sp<INTERFACE> checkDeclaredService(const String16& name) {
126 const sp<IServiceManager> sm = defaultServiceManager();
127 if (!sm->isDeclared(name)) return nullptr;
128 return interface_cast<INTERFACE>(sm->checkService(name));
129 }
130
131 template<typename INTERFACE>
132 sp<INTERFACE> waitForVintfService(
133 const String16& instance = String16("default")) {
134 return waitForDeclaredService<INTERFACE>(
135 INTERFACE::descriptor + String16("/") + instance);
136 }
137
138 template<typename INTERFACE>
139 sp<INTERFACE> checkVintfService(
140 const String16& instance = String16("default")) {
141 return checkDeclaredService<INTERFACE>(
142 INTERFACE::descriptor + String16("/") + instance);
143 }
144
145 template<typename INTERFACE>
getService(const String16 & name,sp<INTERFACE> * outService)146 status_t getService(const String16& name, sp<INTERFACE>* outService)
147 {
148 const sp<IServiceManager> sm = defaultServiceManager();
149 if (sm != nullptr) {
150 *outService = interface_cast<INTERFACE>(sm->getService(name));
151 if ((*outService) != nullptr) return NO_ERROR;
152 }
153 return NAME_NOT_FOUND;
154 }
155
156 bool checkCallingPermission(const String16& permission);
157 bool checkCallingPermission(const String16& permission,
158 int32_t* outPid, int32_t* outUid);
159 bool checkPermission(const String16& permission, pid_t pid, uid_t uid);
160
161 } // namespace android
162
163 #endif // ANDROID_ISERVICE_MANAGER_H
164
165