blob: 7dcdaa4661e9807f1c81133ae752d3407295a0b2 [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001/*
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 Moreland27cfab02019-08-12 14:34:16 -070020#include <android/os/IServiceCallback.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070021
22#include "Access.h"
23
24namespace android {
25
Steven Moreland27cfab02019-08-12 14:34:16 -070026using os::IServiceCallback;
27
Steven Moreland80e1e6d2019-06-21 12:35:59 -070028class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient {
29public:
30 ServiceManager(std::unique_ptr<Access>&& access);
Steven Moreland130242d2019-08-26 17:41:32 -070031 ~ServiceManager();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070032
Jon Spivack0d844302019-07-22 18:40:34 -070033 // getService will try to start any services it cannot find
Steven Moreland80e1e6d2019-06-21 12:35:59 -070034 binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override;
35 binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070036 binder::Status addService(const std::string& name, const sp<IBinder>& binder,
37 bool allowIsolated, int32_t dumpPriority) override;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070038 binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070039 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 Morelandb82b8f82019-10-28 10:52:34 -070043 binder::Status isDeclared(const std::string& name, bool* outReturn) override;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070044
45 void binderDied(const wp<IBinder>& who) override;
46
Jon Spivack0d844302019-07-22 18:40:34 -070047protected:
48 virtual void tryStartService(const std::string& name);
49
Steven Moreland80e1e6d2019-06-21 12:35:59 -070050private:
51 struct Service {
Steven Moreland27cfab02019-08-12 14:34:16 -070052 sp<IBinder> binder; // not null
Steven Moreland80e1e6d2019-06-21 12:35:59 -070053 bool allowIsolated;
54 int32_t dumpPriority;
55 };
56
Steven Moreland27cfab02019-08-12 14:34:16 -070057 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 Spivack0d844302019-07-22 18:40:34 -070065 sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound);
Steven Moreland27cfab02019-08-12 14:34:16 -070066
67 CallbackMap mNameToCallback;
68 ServiceMap mNameToService;
69
Steven Moreland80e1e6d2019-06-21 12:35:59 -070070 std::unique_ptr<Access> mAccess;
71};
72
73} // namespace android