blob: 08f30de6370fc1ddc2302747489a7baea31b9edd [file] [log] [blame]
Brett Chabotc9ee3302020-01-31 09:20:28 -08001/*
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
Devin Moore1621b4b2023-02-03 22:01:09 +000017#include "fakeservicemanager/FakeServiceManager.h"
Brett Chabotc9ee3302020-01-31 09:20:28 -080018
Pawan Wagh2811ae22023-08-22 00:07:03 +000019using android::sp;
20using android::FakeServiceManager;
21using android::setDefaultServiceManager;
22
Brett Chabotc9ee3302020-01-31 09:20:28 -080023namespace android {
24
Devin Moore1621b4b2023-02-03 22:01:09 +000025FakeServiceManager::FakeServiceManager() {}
Brett Chabotc9ee3302020-01-31 09:20:28 -080026
Devin Moore1621b4b2023-02-03 22:01:09 +000027sp<IBinder> FakeServiceManager::getService( const String16& name) const {
Brett Chabotc9ee3302020-01-31 09:20:28 -080028 // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons.
29 return checkService(name);
30}
31
Devin Moore1621b4b2023-02-03 22:01:09 +000032sp<IBinder> FakeServiceManager::checkService( const String16& name) const {
Steven Moreland639ffc42023-08-01 17:53:56 +000033 std::lock_guard<std::mutex> l(mMutex);
34
Brett Chabotc9ee3302020-01-31 09:20:28 -080035 auto it = mNameToService.find(name);
36 if (it == mNameToService.end()) {
37 return nullptr;
38 }
39 return it->second;
40}
41
Devin Moore1621b4b2023-02-03 22:01:09 +000042status_t FakeServiceManager::addService(const String16& name, const sp<IBinder>& service,
Brett Chabotc9ee3302020-01-31 09:20:28 -080043 bool /*allowIsolated*/,
44 int /*dumpsysFlags*/) {
Steven Moreland639ffc42023-08-01 17:53:56 +000045 std::lock_guard<std::mutex> l(mMutex);
46
Devin Moore5f6ded82022-11-18 18:36:30 +000047 if (service == nullptr) {
48 return UNEXPECTED_NULL;
49 }
Brett Chabotc9ee3302020-01-31 09:20:28 -080050 mNameToService[name] = service;
51 return NO_ERROR;
52}
53
Devin Moore1621b4b2023-02-03 22:01:09 +000054Vector<String16> FakeServiceManager::listServices(int /*dumpsysFlags*/) {
Steven Moreland639ffc42023-08-01 17:53:56 +000055 std::lock_guard<std::mutex> l(mMutex);
56
Brett Chabotc9ee3302020-01-31 09:20:28 -080057 Vector<String16> services;
58 for (auto const& [name, service] : mNameToService) {
59 (void) service;
60 services.push_back(name);
61 }
62 return services;
63}
64
Devin Moore1621b4b2023-02-03 22:01:09 +000065IBinder* FakeServiceManager::onAsBinder() {
Brett Chabotc9ee3302020-01-31 09:20:28 -080066 return nullptr;
67}
68
Devin Moore1621b4b2023-02-03 22:01:09 +000069sp<IBinder> FakeServiceManager::waitForService(const String16& name) {
Brett Chabotc9ee3302020-01-31 09:20:28 -080070 return checkService(name);
71}
72
Devin Moore1621b4b2023-02-03 22:01:09 +000073bool FakeServiceManager::isDeclared(const String16& name) {
Steven Moreland639ffc42023-08-01 17:53:56 +000074 std::lock_guard<std::mutex> l(mMutex);
75
Brett Chabotc9ee3302020-01-31 09:20:28 -080076 return mNameToService.find(name) != mNameToService.end();
77}
78
Devin Moore1621b4b2023-02-03 22:01:09 +000079Vector<String16> FakeServiceManager::getDeclaredInstances(const String16& name) {
Steven Moreland639ffc42023-08-01 17:53:56 +000080 std::lock_guard<std::mutex> l(mMutex);
81
Steven Moreland2e293aa2020-09-23 00:25:16 +000082 Vector<String16> out;
83 const String16 prefix = name + String16("/");
84 for (const auto& [registeredName, service] : mNameToService) {
85 (void) service;
86 if (registeredName.startsWith(prefix)) {
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +000087 out.add(String16(registeredName.c_str() + prefix.size()));
Steven Moreland2e293aa2020-09-23 00:25:16 +000088 }
89 }
90 return out;
91}
92
Devin Moore1621b4b2023-02-03 22:01:09 +000093std::optional<String16> FakeServiceManager::updatableViaApex(const String16& name) {
Steven Morelandedd4e072021-04-21 00:27:29 +000094 (void)name;
95 return std::nullopt;
96}
97
Devin Moore1621b4b2023-02-03 22:01:09 +000098Vector<String16> FakeServiceManager::getUpdatableNames(const String16& apexName) {
Jooyung Han76944fe2022-10-25 17:02:45 +090099 (void)apexName;
100 return {};
101}
102
Devin Moore1621b4b2023-02-03 22:01:09 +0000103std::optional<IServiceManager::ConnectionInfo> FakeServiceManager::getConnectionInfo(
Devin Moore5e4c2f12021-09-09 22:36:33 +0000104 const String16& name) {
105 (void)name;
106 return std::nullopt;
107}
108
Devin Moore1621b4b2023-02-03 22:01:09 +0000109status_t FakeServiceManager::registerForNotifications(const String16&,
Jayant Chowdhary30700942022-01-31 14:12:40 -0800110 const sp<LocalRegistrationCallback>&) {
111 return INVALID_OPERATION;
112}
113
Devin Moore1621b4b2023-02-03 22:01:09 +0000114status_t FakeServiceManager::unregisterForNotifications(const String16&,
Jayant Chowdhary30700942022-01-31 14:12:40 -0800115 const sp<LocalRegistrationCallback>&) {
116 return INVALID_OPERATION;
117}
118
Devin Moore1621b4b2023-02-03 22:01:09 +0000119std::vector<IServiceManager::ServiceDebugInfo> FakeServiceManager::getServiceDebugInfo() {
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000120 std::vector<IServiceManager::ServiceDebugInfo> ret;
121 return ret;
122}
Devin Moore8cc776a2022-11-18 18:43:53 +0000123
Devin Moore1621b4b2023-02-03 22:01:09 +0000124void FakeServiceManager::clear() {
Steven Moreland95ac79e2023-12-28 22:46:28 +0000125 std::map<String16, sp<IBinder>> backup;
Steven Moreland639ffc42023-08-01 17:53:56 +0000126
Steven Moreland95ac79e2023-12-28 22:46:28 +0000127 {
128 std::lock_guard<std::mutex> l(mMutex);
129 backup = mNameToService;
130 mNameToService.clear();
131 }
132
133 // destructors may access FSM, so avoid recursive lock
134 backup.clear(); // explicit
135
136 // TODO: destructors may have added more services here - may want
137 // to check this or abort
Devin Moore8cc776a2022-11-18 18:43:53 +0000138}
Brett Chabotc9ee3302020-01-31 09:20:28 -0800139} // namespace android
Pawan Wagh2811ae22023-08-22 00:07:03 +0000140
141[[clang::no_destroy]] static sp<FakeServiceManager> gFakeServiceManager;
142[[clang::no_destroy]] static std::once_flag gSmOnce;
143
144extern "C" {
145
146// Setup FakeServiceManager to mock dependencies in test using this API for rust backend
147void setupFakeServiceManager() {
148 /* Create a FakeServiceManager instance and add required services */
149 std::call_once(gSmOnce, [&]() {
150 gFakeServiceManager = new FakeServiceManager();
151 android::setDefaultServiceManager(gFakeServiceManager);
152 });
153}
154
155// Clear existing services from Fake SM for rust backend
156void clearFakeServiceManager() {
157 LOG_ALWAYS_FATAL_IF(gFakeServiceManager == nullptr, "Fake Service Manager is not available. Forgot to call setupFakeServiceManager?");
158 gFakeServiceManager->clear();
159}
Steven Moreland95ac79e2023-12-28 22:46:28 +0000160} //extern "C"