Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 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 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 17 | #include "fakeservicemanager/FakeServiceManager.h" |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 18 | |
| 19 | namespace android { |
| 20 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 21 | FakeServiceManager::FakeServiceManager() {} |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 22 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 23 | sp<IBinder> FakeServiceManager::getService( const String16& name) const { |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 24 | // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons. |
| 25 | return checkService(name); |
| 26 | } |
| 27 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 28 | sp<IBinder> FakeServiceManager::checkService( const String16& name) const { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 29 | std::lock_guard<std::mutex> l(mMutex); |
| 30 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 31 | auto it = mNameToService.find(name); |
| 32 | if (it == mNameToService.end()) { |
| 33 | return nullptr; |
| 34 | } |
| 35 | return it->second; |
| 36 | } |
| 37 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 38 | status_t FakeServiceManager::addService(const String16& name, const sp<IBinder>& service, |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 39 | bool /*allowIsolated*/, |
| 40 | int /*dumpsysFlags*/) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 41 | std::lock_guard<std::mutex> l(mMutex); |
| 42 | |
Devin Moore | 5f6ded8 | 2022-11-18 18:36:30 +0000 | [diff] [blame] | 43 | if (service == nullptr) { |
| 44 | return UNEXPECTED_NULL; |
| 45 | } |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 46 | mNameToService[name] = service; |
| 47 | return NO_ERROR; |
| 48 | } |
| 49 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 50 | Vector<String16> FakeServiceManager::listServices(int /*dumpsysFlags*/) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 51 | std::lock_guard<std::mutex> l(mMutex); |
| 52 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 53 | 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 Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 61 | IBinder* FakeServiceManager::onAsBinder() { |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 62 | return nullptr; |
| 63 | } |
| 64 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 65 | sp<IBinder> FakeServiceManager::waitForService(const String16& name) { |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 66 | return checkService(name); |
| 67 | } |
| 68 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 69 | bool FakeServiceManager::isDeclared(const String16& name) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 70 | std::lock_guard<std::mutex> l(mMutex); |
| 71 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 72 | return mNameToService.find(name) != mNameToService.end(); |
| 73 | } |
| 74 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 75 | Vector<String16> FakeServiceManager::getDeclaredInstances(const String16& name) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 76 | std::lock_guard<std::mutex> l(mMutex); |
| 77 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 78 | Vector<String16> out; |
| 79 | const String16 prefix = name + String16("/"); |
| 80 | for (const auto& [registeredName, service] : mNameToService) { |
| 81 | (void) service; |
| 82 | if (registeredName.startsWith(prefix)) { |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame^] | 83 | out.add(String16(registeredName.c_str() + prefix.size())); |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | return out; |
| 87 | } |
| 88 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 89 | std::optional<String16> FakeServiceManager::updatableViaApex(const String16& name) { |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 90 | (void)name; |
| 91 | return std::nullopt; |
| 92 | } |
| 93 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 94 | Vector<String16> FakeServiceManager::getUpdatableNames(const String16& apexName) { |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 95 | (void)apexName; |
| 96 | return {}; |
| 97 | } |
| 98 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 99 | std::optional<IServiceManager::ConnectionInfo> FakeServiceManager::getConnectionInfo( |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 100 | const String16& name) { |
| 101 | (void)name; |
| 102 | return std::nullopt; |
| 103 | } |
| 104 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 105 | status_t FakeServiceManager::registerForNotifications(const String16&, |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 106 | const sp<LocalRegistrationCallback>&) { |
| 107 | return INVALID_OPERATION; |
| 108 | } |
| 109 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 110 | status_t FakeServiceManager::unregisterForNotifications(const String16&, |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 111 | const sp<LocalRegistrationCallback>&) { |
| 112 | return INVALID_OPERATION; |
| 113 | } |
| 114 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 115 | std::vector<IServiceManager::ServiceDebugInfo> FakeServiceManager::getServiceDebugInfo() { |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 116 | std::vector<IServiceManager::ServiceDebugInfo> ret; |
| 117 | return ret; |
| 118 | } |
Devin Moore | 8cc776a | 2022-11-18 18:43:53 +0000 | [diff] [blame] | 119 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 120 | void FakeServiceManager::clear() { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 121 | std::lock_guard<std::mutex> l(mMutex); |
| 122 | |
Devin Moore | 8cc776a | 2022-11-18 18:43:53 +0000 | [diff] [blame] | 123 | mNameToService.clear(); |
| 124 | } |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 125 | } // namespace android |