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> |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 20 | #include <android/os/IServiceCallback.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 21 | |
| 22 | #include "Access.h" |
| 23 | |
| 24 | namespace android { |
| 25 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 26 | using os::IServiceCallback; |
| 27 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 28 | class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient { |
| 29 | public: |
| 30 | ServiceManager(std::unique_ptr<Access>&& access); |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 31 | ~ServiceManager(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 32 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 33 | // getService will try to start any services it cannot find |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 34 | binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override; |
| 35 | binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 36 | binder::Status addService(const std::string& name, const sp<IBinder>& binder, |
| 37 | bool allowIsolated, int32_t dumpPriority) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 38 | binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 39 | binder::Status registerForNotifications(const std::string& name, |
| 40 | const sp<IServiceCallback>& callback) override; |
| 41 | binder::Status unregisterForNotifications(const std::string& name, |
| 42 | const sp<IServiceCallback>& callback) override; |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 43 | binder::Status isDeclared(const std::string& name, bool* outReturn) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 44 | |
| 45 | void binderDied(const wp<IBinder>& who) override; |
| 46 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 47 | protected: |
| 48 | virtual void tryStartService(const std::string& name); |
| 49 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 50 | private: |
| 51 | struct Service { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 52 | sp<IBinder> binder; // not null |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 53 | bool allowIsolated; |
| 54 | int32_t dumpPriority; |
| 55 | }; |
| 56 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 57 | using CallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>; |
| 58 | using ServiceMap = std::map<std::string, Service>; |
| 59 | |
| 60 | // removes a callback from mNameToCallback, removing it if the vector is empty |
| 61 | // this updates iterator to the next location |
| 62 | void removeCallback(const wp<IBinder>& who, |
| 63 | CallbackMap::iterator* it, |
| 64 | bool* found); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 65 | sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 66 | |
| 67 | CallbackMap mNameToCallback; |
| 68 | ServiceMap mNameToService; |
| 69 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 70 | std::unique_ptr<Access> mAccess; |
| 71 | }; |
| 72 | |
| 73 | } // namespace android |