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> 20 #include <android/os/IClientCallback.h> 21 #include <android/os/IServiceCallback.h> 22 23 #include "Access.h" 24 25 namespace android { 26 27 using os::ConnectionInfo; 28 using os::IClientCallback; 29 using os::IServiceCallback; 30 using os::ServiceDebugInfo; 31 32 class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient { 33 public: 34 ServiceManager(std::unique_ptr<Access>&& access); 35 ~ServiceManager(); 36 37 // getService will try to start any services it cannot find 38 binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override; 39 binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override; 40 binder::Status addService(const std::string& name, const sp<IBinder>& binder, 41 bool allowIsolated, int32_t dumpPriority) override; 42 binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override; 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; 47 48 binder::Status isDeclared(const std::string& name, bool* outReturn) override; 49 binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override; 50 binder::Status updatableViaApex(const std::string& name, 51 std::optional<std::string>* outReturn) override; 52 binder::Status getUpdatableNames(const std::string& apexName, 53 std::vector<std::string>* outReturn) override; 54 binder::Status getConnectionInfo(const std::string& name, 55 std::optional<ConnectionInfo>* outReturn) override; 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; 59 binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override; 60 void binderDied(const wp<IBinder>& who) override; 61 void handleClientCallbacks(); 62 63 /** 64 * This API is added for debug purposes. It clears members which hold service and callback 65 * information. 66 */ 67 void clear(); 68 69 protected: 70 virtual void tryStartService(const Access::CallingContext& ctx, const std::string& name); 71 72 private: 73 struct Service { 74 sp<IBinder> binder; // not null 75 bool allowIsolated; 76 int32_t dumpPriority; 77 bool hasClients = false; // notifications sent on true -> false. 78 bool guaranteeClient = false; // forces the client check to true 79 Access::CallingContext ctx; // process that originally registers this 80 81 // the number of clients of the service, including servicemanager itself 82 ssize_t getNodeStrongRefCount(); 83 84 ~Service(); 85 }; 86 87 using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>; 88 using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>; 89 using ServiceMap = std::map<std::string, Service>; 90 91 // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty 92 // this updates iterator to the next location 93 void removeRegistrationCallback(const wp<IBinder>& who, 94 ServiceCallbackMap::iterator* it, 95 bool* found); 96 // returns whether there are known clients in addition to the count provided 97 bool handleServiceClientCallback(size_t knownClients, const std::string& serviceName, 98 bool isCalledOnInterval); 99 // Also updates mHasClients (of what the last callback was) 100 void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients, 101 const char* context); 102 // removes a callback from mNameToClientCallback, deleting the entry if the vector is empty 103 // this updates the iterator to the next location 104 void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it); 105 106 sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound); 107 108 ServiceMap mNameToService; 109 ServiceCallbackMap mNameToRegistrationCallback; 110 ClientCallbackMap mNameToClientCallback; 111 112 std::unique_ptr<Access> mAccess; 113 }; 114 115 } // namespace android 116