1 /*
2 * Copyright (C) 2020 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 #include "fakeservicemanager/FakeServiceManager.h"
18
19 namespace android {
20
FakeServiceManager()21 FakeServiceManager::FakeServiceManager() {}
22
getService(const String16 & name) const23 sp<IBinder> FakeServiceManager::getService( const String16& name) const {
24 // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons.
25 return checkService(name);
26 }
27
checkService(const String16 & name) const28 sp<IBinder> FakeServiceManager::checkService( const String16& name) const {
29 auto it = mNameToService.find(name);
30 if (it == mNameToService.end()) {
31 return nullptr;
32 }
33 return it->second;
34 }
35
addService(const String16 & name,const sp<IBinder> & service,bool,int)36 status_t FakeServiceManager::addService(const String16& name, const sp<IBinder>& service,
37 bool /*allowIsolated*/,
38 int /*dumpsysFlags*/) {
39 if (service == nullptr) {
40 return UNEXPECTED_NULL;
41 }
42 mNameToService[name] = service;
43 return NO_ERROR;
44 }
45
listServices(int)46 Vector<String16> FakeServiceManager::listServices(int /*dumpsysFlags*/) {
47 Vector<String16> services;
48 for (auto const& [name, service] : mNameToService) {
49 (void) service;
50 services.push_back(name);
51 }
52 return services;
53 }
54
onAsBinder()55 IBinder* FakeServiceManager::onAsBinder() {
56 return nullptr;
57 }
58
waitForService(const String16 & name)59 sp<IBinder> FakeServiceManager::waitForService(const String16& name) {
60 return checkService(name);
61 }
62
isDeclared(const String16 & name)63 bool FakeServiceManager::isDeclared(const String16& name) {
64 return mNameToService.find(name) != mNameToService.end();
65 }
66
getDeclaredInstances(const String16 & name)67 Vector<String16> FakeServiceManager::getDeclaredInstances(const String16& name) {
68 Vector<String16> out;
69 const String16 prefix = name + String16("/");
70 for (const auto& [registeredName, service] : mNameToService) {
71 (void) service;
72 if (registeredName.startsWith(prefix)) {
73 out.add(String16(registeredName.string() + prefix.size()));
74 }
75 }
76 return out;
77 }
78
updatableViaApex(const String16 & name)79 std::optional<String16> FakeServiceManager::updatableViaApex(const String16& name) {
80 (void)name;
81 return std::nullopt;
82 }
83
getUpdatableNames(const String16 & apexName)84 Vector<String16> FakeServiceManager::getUpdatableNames(const String16& apexName) {
85 (void)apexName;
86 return {};
87 }
88
getConnectionInfo(const String16 & name)89 std::optional<IServiceManager::ConnectionInfo> FakeServiceManager::getConnectionInfo(
90 const String16& name) {
91 (void)name;
92 return std::nullopt;
93 }
94
registerForNotifications(const String16 &,const sp<LocalRegistrationCallback> &)95 status_t FakeServiceManager::registerForNotifications(const String16&,
96 const sp<LocalRegistrationCallback>&) {
97 return INVALID_OPERATION;
98 }
99
unregisterForNotifications(const String16 &,const sp<LocalRegistrationCallback> &)100 status_t FakeServiceManager::unregisterForNotifications(const String16&,
101 const sp<LocalRegistrationCallback>&) {
102 return INVALID_OPERATION;
103 }
104
getServiceDebugInfo()105 std::vector<IServiceManager::ServiceDebugInfo> FakeServiceManager::getServiceDebugInfo() {
106 std::vector<IServiceManager::ServiceDebugInfo> ret;
107 return ret;
108 }
109
clear()110 void FakeServiceManager::clear() {
111 mNameToService.clear();
112 }
113 } // namespace android
114