blob: fcc5124fa208b036c8c4019e85c26c1ba5419bb0 [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
33 binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override;
34 binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070035 binder::Status addService(const std::string& name, const sp<IBinder>& binder,
36 bool allowIsolated, int32_t dumpPriority) override;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070037 binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070038 binder::Status registerForNotifications(const std::string& name,
39 const sp<IServiceCallback>& callback) override;
40 binder::Status unregisterForNotifications(const std::string& name,
41 const sp<IServiceCallback>& callback) override;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070042
43 void binderDied(const wp<IBinder>& who) override;
44
45private:
46 struct Service {
Steven Moreland27cfab02019-08-12 14:34:16 -070047 sp<IBinder> binder; // not null
Steven Moreland80e1e6d2019-06-21 12:35:59 -070048 bool allowIsolated;
49 int32_t dumpPriority;
50 };
51
Steven Moreland27cfab02019-08-12 14:34:16 -070052 using CallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>;
53 using ServiceMap = std::map<std::string, Service>;
54
55 // removes a callback from mNameToCallback, removing it if the vector is empty
56 // this updates iterator to the next location
57 void removeCallback(const wp<IBinder>& who,
58 CallbackMap::iterator* it,
59 bool* found);
60
61 CallbackMap mNameToCallback;
62 ServiceMap mNameToService;
63
Steven Moreland80e1e6d2019-06-21 12:35:59 -070064 std::unique_ptr<Access> mAccess;
65};
66
67} // namespace android