Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #pragma once |
| 18 | |
| 19 | #include <android/os/BnServiceManager.h> |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 20 | #include <android/os/IClientCallback.h> |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 21 | #include <android/os/IServiceCallback.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 22 | |
| 23 | #include "Access.h" |
| 24 | |
| 25 | namespace android { |
| 26 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 27 | using os::ConnectionInfo; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 28 | using os::IClientCallback; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 29 | using os::IServiceCallback; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 30 | using os::ServiceDebugInfo; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 31 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 32 | class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient { |
| 33 | public: |
| 34 | ServiceManager(std::unique_ptr<Access>&& access); |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 35 | ~ServiceManager(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 36 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 37 | // getService will try to start any services it cannot find |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 38 | binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override; |
| 39 | binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 40 | binder::Status addService(const std::string& name, const sp<IBinder>& binder, |
| 41 | bool allowIsolated, int32_t dumpPriority) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 42 | binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 43 | binder::Status registerForNotifications(const std::string& name, |
| 44 | const sp<IServiceCallback>& callback) override; |
| 45 | binder::Status unregisterForNotifications(const std::string& name, |
| 46 | const sp<IServiceCallback>& callback) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 47 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 48 | binder::Status isDeclared(const std::string& name, bool* outReturn) override; |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 49 | binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override; |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 50 | binder::Status updatableViaApex(const std::string& name, |
| 51 | std::optional<std::string>* outReturn) override; |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame^] | 52 | binder::Status getUpdatableNames(const std::string& apexName, |
| 53 | std::vector<std::string>* outReturn) override; |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 54 | binder::Status getConnectionInfo(const std::string& name, |
| 55 | std::optional<ConnectionInfo>* outReturn) override; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 56 | binder::Status registerClientCallback(const std::string& name, const sp<IBinder>& service, |
| 57 | const sp<IClientCallback>& cb) override; |
| 58 | binder::Status tryUnregisterService(const std::string& name, const sp<IBinder>& binder) override; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 59 | binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 60 | void binderDied(const wp<IBinder>& who) override; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 61 | void handleClientCallbacks(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 62 | |
Pawan Wagh | 243888e | 2022-09-20 19:37:35 +0000 | [diff] [blame] | 63 | /** |
| 64 | * This API is added for debug purposes. It clears members which hold service and callback |
| 65 | * information. |
| 66 | */ |
| 67 | void clear(); |
| 68 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 69 | protected: |
| 70 | virtual void tryStartService(const std::string& name); |
| 71 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 72 | private: |
| 73 | struct Service { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 74 | sp<IBinder> binder; // not null |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 75 | bool allowIsolated; |
| 76 | int32_t dumpPriority; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 77 | bool hasClients = false; // notifications sent on true -> false. |
| 78 | bool guaranteeClient = false; // forces the client check to true |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 79 | Access::CallingContext ctx; // process that originally registers this |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 80 | |
| 81 | // the number of clients of the service, including servicemanager itself |
| 82 | ssize_t getNodeStrongRefCount(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 85 | using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 86 | using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 87 | using ServiceMap = std::map<std::string, Service>; |
| 88 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 89 | // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 90 | // this updates iterator to the next location |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 91 | void removeRegistrationCallback(const wp<IBinder>& who, |
| 92 | ServiceCallbackMap::iterator* it, |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 93 | bool* found); |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 94 | ssize_t handleServiceClientCallback(const std::string& serviceName, bool isCalledOnInterval); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 95 | // Also updates mHasClients (of what the last callback was) |
| 96 | void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients); |
| 97 | // removes a callback from mNameToClientCallback, deleting the entry if the vector is empty |
| 98 | // this updates the iterator to the next location |
| 99 | void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it); |
| 100 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 101 | sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 102 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 103 | ServiceMap mNameToService; |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 104 | ServiceCallbackMap mNameToRegistrationCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 105 | ClientCallbackMap mNameToClientCallback; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 106 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 107 | std::unique_ptr<Access> mAccess; |
| 108 | }; |
| 109 | |
| 110 | } // namespace android |