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 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame^] | 27 | using os::IClientCallback; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 28 | using os::IServiceCallback; |
| 29 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 30 | class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient { |
| 31 | public: |
| 32 | ServiceManager(std::unique_ptr<Access>&& access); |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 33 | ~ServiceManager(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 34 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 35 | // getService will try to start any services it cannot find |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 36 | binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override; |
| 37 | binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 38 | binder::Status addService(const std::string& name, const sp<IBinder>& binder, |
| 39 | bool allowIsolated, int32_t dumpPriority) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 40 | binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 41 | binder::Status registerForNotifications(const std::string& name, |
| 42 | const sp<IServiceCallback>& callback) override; |
| 43 | binder::Status unregisterForNotifications(const std::string& name, |
| 44 | const sp<IServiceCallback>& callback) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 45 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame^] | 46 | binder::Status isDeclared(const std::string& name, bool* outReturn) override; |
| 47 | binder::Status registerClientCallback(const std::string& name, const sp<IBinder>& service, |
| 48 | const sp<IClientCallback>& cb) override; |
| 49 | binder::Status tryUnregisterService(const std::string& name, const sp<IBinder>& binder) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 50 | void binderDied(const wp<IBinder>& who) override; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame^] | 51 | void handleClientCallbacks(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 52 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 53 | protected: |
| 54 | virtual void tryStartService(const std::string& name); |
| 55 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 56 | private: |
| 57 | struct Service { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 58 | sp<IBinder> binder; // not null |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 59 | bool allowIsolated; |
| 60 | int32_t dumpPriority; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame^] | 61 | bool hasClients = false; // notifications sent on true -> false. |
| 62 | bool guaranteeClient = false; // forces the client check to true |
| 63 | pid_t debugPid = 0; // the process in which this service runs |
| 64 | |
| 65 | // the number of clients of the service, including servicemanager itself |
| 66 | ssize_t getNodeStrongRefCount(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 69 | using CallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame^] | 70 | using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 71 | using ServiceMap = std::map<std::string, Service>; |
| 72 | |
| 73 | // removes a callback from mNameToCallback, removing it if the vector is empty |
| 74 | // this updates iterator to the next location |
| 75 | void removeCallback(const wp<IBinder>& who, |
| 76 | CallbackMap::iterator* it, |
| 77 | bool* found); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame^] | 78 | ssize_t handleServiceClientCallback(const std::string& serviceName); |
| 79 | // Also updates mHasClients (of what the last callback was) |
| 80 | void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients); |
| 81 | // removes a callback from mNameToClientCallback, deleting the entry if the vector is empty |
| 82 | // this updates the iterator to the next location |
| 83 | void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it); |
| 84 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 85 | sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 86 | |
| 87 | CallbackMap mNameToCallback; |
| 88 | ServiceMap mNameToService; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame^] | 89 | ClientCallbackMap mNameToClientCallback; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 90 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 91 | std::unique_ptr<Access> mAccess; |
| 92 | }; |
| 93 | |
| 94 | } // namespace android |