blob: 80661c1cbd93341d8a011e83f6afb1c72c5ed85f [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
19namespace android {
20
Devin Moore1621b4b2023-02-03 22:01:09 +000021FakeServiceManager::FakeServiceManager() {}
Brett Chabotc9ee3302020-01-31 09:20:28 -080022
Devin Moore1621b4b2023-02-03 22:01:09 +000023sp<IBinder> FakeServiceManager::getService( const String16& name) const {
Brett Chabotc9ee3302020-01-31 09:20:28 -080024 // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons.
25 return checkService(name);
26}
27
Devin Moore1621b4b2023-02-03 22:01:09 +000028sp<IBinder> FakeServiceManager::checkService( const String16& name) const {
Steven Moreland639ffc42023-08-01 17:53:56 +000029 std::lock_guard<std::mutex> l(mMutex);
30
Brett Chabotc9ee3302020-01-31 09:20:28 -080031 auto it = mNameToService.find(name);
32 if (it == mNameToService.end()) {
33 return nullptr;
34 }
35 return it->second;
36}
37
Devin Moore1621b4b2023-02-03 22:01:09 +000038status_t FakeServiceManager::addService(const String16& name, const sp<IBinder>& service,
Brett Chabotc9ee3302020-01-31 09:20:28 -080039 bool /*allowIsolated*/,
40 int /*dumpsysFlags*/) {
Steven Moreland639ffc42023-08-01 17:53:56 +000041 std::lock_guard<std::mutex> l(mMutex);
42
Devin Moore5f6ded82022-11-18 18:36:30 +000043 if (service == nullptr) {
44 return UNEXPECTED_NULL;
45 }
Brett Chabotc9ee3302020-01-31 09:20:28 -080046 mNameToService[name] = service;
47 return NO_ERROR;
48}
49
Devin Moore1621b4b2023-02-03 22:01:09 +000050Vector<String16> FakeServiceManager::listServices(int /*dumpsysFlags*/) {
Steven Moreland639ffc42023-08-01 17:53:56 +000051 std::lock_guard<std::mutex> l(mMutex);
52
Brett Chabotc9ee3302020-01-31 09:20:28 -080053 Vector<String16> services;
54 for (auto const& [name, service] : mNameToService) {
55 (void) service;
56 services.push_back(name);
57 }
58 return services;
59}
60
Devin Moore1621b4b2023-02-03 22:01:09 +000061IBinder* FakeServiceManager::onAsBinder() {
Brett Chabotc9ee3302020-01-31 09:20:28 -080062 return nullptr;
63}
64
Devin Moore1621b4b2023-02-03 22:01:09 +000065sp<IBinder> FakeServiceManager::waitForService(const String16& name) {
Brett Chabotc9ee3302020-01-31 09:20:28 -080066 return checkService(name);
67}
68
Devin Moore1621b4b2023-02-03 22:01:09 +000069bool FakeServiceManager::isDeclared(const String16& name) {
Steven Moreland639ffc42023-08-01 17:53:56 +000070 std::lock_guard<std::mutex> l(mMutex);
71
Brett Chabotc9ee3302020-01-31 09:20:28 -080072 return mNameToService.find(name) != mNameToService.end();
73}
74
Devin Moore1621b4b2023-02-03 22:01:09 +000075Vector<String16> FakeServiceManager::getDeclaredInstances(const String16& name) {
Steven Moreland639ffc42023-08-01 17:53:56 +000076 std::lock_guard<std::mutex> l(mMutex);
77
Steven Moreland2e293aa2020-09-23 00:25:16 +000078 Vector<String16> out;
79 const String16 prefix = name + String16("/");
80 for (const auto& [registeredName, service] : mNameToService) {
81 (void) service;
82 if (registeredName.startsWith(prefix)) {
83 out.add(String16(registeredName.string() + prefix.size()));
84 }
85 }
86 return out;
87}
88
Devin Moore1621b4b2023-02-03 22:01:09 +000089std::optional<String16> FakeServiceManager::updatableViaApex(const String16& name) {
Steven Morelandedd4e072021-04-21 00:27:29 +000090 (void)name;
91 return std::nullopt;
92}
93
Devin Moore1621b4b2023-02-03 22:01:09 +000094Vector<String16> FakeServiceManager::getUpdatableNames(const String16& apexName) {
Jooyung Han76944fe2022-10-25 17:02:45 +090095 (void)apexName;
96 return {};
97}
98
Devin Moore1621b4b2023-02-03 22:01:09 +000099std::optional<IServiceManager::ConnectionInfo> FakeServiceManager::getConnectionInfo(
Devin Moore5e4c2f12021-09-09 22:36:33 +0000100 const String16& name) {
101 (void)name;
102 return std::nullopt;
103}
104
Devin Moore1621b4b2023-02-03 22:01:09 +0000105status_t FakeServiceManager::registerForNotifications(const String16&,
Jayant Chowdhary30700942022-01-31 14:12:40 -0800106 const sp<LocalRegistrationCallback>&) {
107 return INVALID_OPERATION;
108}
109
Devin Moore1621b4b2023-02-03 22:01:09 +0000110status_t FakeServiceManager::unregisterForNotifications(const String16&,
Jayant Chowdhary30700942022-01-31 14:12:40 -0800111 const sp<LocalRegistrationCallback>&) {
112 return INVALID_OPERATION;
113}
114
Devin Moore1621b4b2023-02-03 22:01:09 +0000115std::vector<IServiceManager::ServiceDebugInfo> FakeServiceManager::getServiceDebugInfo() {
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000116 std::vector<IServiceManager::ServiceDebugInfo> ret;
117 return ret;
118}
Devin Moore8cc776a2022-11-18 18:43:53 +0000119
Devin Moore1621b4b2023-02-03 22:01:09 +0000120void FakeServiceManager::clear() {
Steven Moreland639ffc42023-08-01 17:53:56 +0000121 std::lock_guard<std::mutex> l(mMutex);
122
Devin Moore8cc776a2022-11-18 18:43:53 +0000123 mNameToService.clear();
124}
Brett Chabotc9ee3302020-01-31 09:20:28 -0800125} // namespace android