blob: a2fc5a84c5afd04471d64a7d22ff6ee47900436d [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>
Jon Spivack9f503a42019-10-22 16:49:19 -070020#include <android/os/IClientCallback.h>
Steven Moreland27cfab02019-08-12 14:34:16 -070021#include <android/os/IServiceCallback.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070022
23#include "Access.h"
24
25namespace android {
26
Jon Spivack9f503a42019-10-22 16:49:19 -070027using os::IClientCallback;
Steven Moreland27cfab02019-08-12 14:34:16 -070028using os::IServiceCallback;
29
Steven Moreland80e1e6d2019-06-21 12:35:59 -070030class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient {
31public:
32 ServiceManager(std::unique_ptr<Access>&& access);
Steven Moreland130242d2019-08-26 17:41:32 -070033 ~ServiceManager();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070034
Jon Spivack0d844302019-07-22 18:40:34 -070035 // getService will try to start any services it cannot find
Steven Moreland80e1e6d2019-06-21 12:35:59 -070036 binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override;
37 binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070038 binder::Status addService(const std::string& name, const sp<IBinder>& binder,
39 bool allowIsolated, int32_t dumpPriority) override;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070040 binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070041 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 Moreland80e1e6d2019-06-21 12:35:59 -070045
Jon Spivack9f503a42019-10-22 16:49:19 -070046 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 Moreland80e1e6d2019-06-21 12:35:59 -070050 void binderDied(const wp<IBinder>& who) override;
Jon Spivack9f503a42019-10-22 16:49:19 -070051 void handleClientCallbacks();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070052
Jon Spivack0d844302019-07-22 18:40:34 -070053protected:
54 virtual void tryStartService(const std::string& name);
55
Steven Moreland80e1e6d2019-06-21 12:35:59 -070056private:
57 struct Service {
Steven Moreland27cfab02019-08-12 14:34:16 -070058 sp<IBinder> binder; // not null
Steven Moreland80e1e6d2019-06-21 12:35:59 -070059 bool allowIsolated;
60 int32_t dumpPriority;
Jon Spivack9f503a42019-10-22 16:49:19 -070061 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 Moreland80e1e6d2019-06-21 12:35:59 -070067 };
68
Jon Spivackf288b1d2019-12-19 17:15:51 -080069 using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>;
Jon Spivack9f503a42019-10-22 16:49:19 -070070 using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>;
Steven Moreland27cfab02019-08-12 14:34:16 -070071 using ServiceMap = std::map<std::string, Service>;
72
Jon Spivackf288b1d2019-12-19 17:15:51 -080073 // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty
Steven Moreland27cfab02019-08-12 14:34:16 -070074 // this updates iterator to the next location
Jon Spivackf288b1d2019-12-19 17:15:51 -080075 void removeRegistrationCallback(const wp<IBinder>& who,
76 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -070077 bool* found);
Jon Spivackd9533c22020-01-27 22:19:22 +000078 ssize_t handleServiceClientCallback(const std::string& serviceName, bool isCalledOnInterval);
Jon Spivack9f503a42019-10-22 16:49:19 -070079 // 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 Spivack0d844302019-07-22 18:40:34 -070085 sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound);
Steven Moreland27cfab02019-08-12 14:34:16 -070086
Steven Moreland27cfab02019-08-12 14:34:16 -070087 ServiceMap mNameToService;
Jon Spivackf288b1d2019-12-19 17:15:51 -080088 ServiceCallbackMap mNameToRegistrationCallback;
Jon Spivack9f503a42019-10-22 16:49:19 -070089 ClientCallbackMap mNameToClientCallback;
Steven Moreland27cfab02019-08-12 14:34:16 -070090
Steven Moreland80e1e6d2019-06-21 12:35:59 -070091 std::unique_ptr<Access> mAccess;
92};
93
94} // namespace android