blob: 006e51947b05a358a4da2efa596ebb66414b3704 [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 Moreland80e1e6d2019-06-21 12:35:59 -070043
44 void binderDied(const wp<IBinder>& who) override;
45
Jon Spivack0d844302019-07-22 18:40:34 -070046protected:
47 virtual void tryStartService(const std::string& name);
48
Steven Moreland80e1e6d2019-06-21 12:35:59 -070049private:
50 struct Service {
Steven Moreland27cfab02019-08-12 14:34:16 -070051 sp<IBinder> binder; // not null
Steven Moreland80e1e6d2019-06-21 12:35:59 -070052 bool allowIsolated;
53 int32_t dumpPriority;
54 };
55
Steven Moreland27cfab02019-08-12 14:34:16 -070056 using CallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>;
57 using ServiceMap = std::map<std::string, Service>;
58
59 // removes a callback from mNameToCallback, removing it if the vector is empty
60 // this updates iterator to the next location
61 void removeCallback(const wp<IBinder>& who,
62 CallbackMap::iterator* it,
63 bool* found);
Jon Spivack0d844302019-07-22 18:40:34 -070064 sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound);
Steven Moreland27cfab02019-08-12 14:34:16 -070065
66 CallbackMap mNameToCallback;
67 ServiceMap mNameToService;
68
Steven Moreland80e1e6d2019-06-21 12:35:59 -070069 std::unique_ptr<Access> mAccess;
70};
71
72} // namespace android