1 /*
2  * Copyright (C) 2016 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/hidl/manager/1.2/IServiceManager.h>
20 #include <hidl/Status.h>
21 #include <hidl/MQDescriptor.h>
22 #include <map>
23 
24 #include "AccessControl.h"
25 #include "HidlService.h"
26 
27 namespace android {
28 namespace hidl {
29 namespace manager {
30 namespace implementation {
31 
32 using ::android::hardware::hidl_death_recipient;
33 using ::android::hardware::hidl_vec;
34 using ::android::hardware::hidl_string;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hidl::base::V1_0::IBase;
38 using ::android::hidl::manager::V1_0::IServiceNotification;
39 using ::android::hidl::manager::V1_2::IClientCallback;
40 using ::android::sp;
41 using ::android::wp;
42 
43 struct ServiceManager : public V1_2::IServiceManager, hidl_death_recipient {
44     // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
45     Return<sp<IBase>> get(const hidl_string& fqName,
46                           const hidl_string& name) override;
47     Return<bool> add(const hidl_string& name,
48                      const sp<IBase>& service) override;
49 
50     Return<Transport> getTransport(const hidl_string& fqName,
51                                    const hidl_string& name);
52 
53     Return<void> list(list_cb _hidl_cb) override;
54     Return<void> listByInterface(const hidl_string& fqInstanceName,
55                                  listByInterface_cb _hidl_cb) override;
56 
57     Return<bool> registerForNotifications(const hidl_string& fqName,
58                                           const hidl_string& name,
59                                           const sp<IServiceNotification>& callback) override;
60 
61     Return<void> debugDump(debugDump_cb _cb) override;
62     Return<void> registerPassthroughClient(const hidl_string &fqName,
63             const hidl_string &name) override;
64 
65     // Methods from ::android::hidl::manager::V1_1::IServiceManager follow.
66     Return<bool> unregisterForNotifications(const hidl_string& fqName,
67                                             const hidl_string& name,
68                                             const sp<IServiceNotification>& callback) override;
69 
70     // Methods from ::android::hidl::manager::V1_2::IServiceManager follow.
71     Return<bool> registerClientCallback(const hidl_string& fqName,
72                                         const hidl_string& name,
73                                         const sp<IBase>& server,
74                                         const sp<IClientCallback>& cb) override;
75     Return<bool> unregisterClientCallback(const sp<IBase>& server,
76                                           const sp<IClientCallback>& cb) override;
77     Return<bool> addWithChain(const hidl_string& name,
78                               const sp<IBase>& service,
79                               const hidl_vec<hidl_string>& chain) override;
80     Return<void> listManifestByInterface(const hidl_string& fqInstanceName,
81                                          listManifestByInterface_cb _hidl_cb) override;
82     Return<bool> tryUnregister(const hidl_string& fqName,
83                                const hidl_string& name,
84                                const sp<IBase>& service) override;
85 
86     void handleClientCallbacks();
87 
88     virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
89 private:
90     bool addImpl(const std::string& name,
91                  const sp<IBase>& service,
92                  const hidl_vec<hidl_string>& interfaceChain,
93                  const AccessControl::CallingContext& callingContext);
94 
95     // if restrictToInstanceName is nullptr, remove all, otherwise only those services
96     // which match this instance name. Returns whether all instances were removed.
97     bool removeService(const wp<IBase>& who, const std::string* restrictToInstanceName);
98     bool removePackageListener(const wp<IBase>& who);
99     bool removeServiceListener(const wp<IBase>& who);
100     size_t countExistingService() const;
101 
102     // true = continue, false = break
103     void forEachExistingService(std::function<bool(const HidlService *)> f) const;
104     void forEachExistingService(std::function<bool(HidlService *)> f);
105     void forEachServiceEntry(std::function<bool(const HidlService *)> f) const;
106     void forEachServiceEntry(std::function<bool(HidlService *)> f);
107 
108     HidlService* lookup(const std::string& fqName, const std::string& name);
109 
110     using InstanceMap = std::map<
111             std::string, // instance name e.x. "manager"
112             std::unique_ptr<HidlService>
113         >;
114 
115     struct PackageInterfaceMap {
116         InstanceMap &getInstanceMap();
117         const InstanceMap &getInstanceMap() const;
118 
119         /**
120          * Finds a HidlService with the desired name. If none,
121          * returns nullptr. HidlService::getService() might also be nullptr
122          * if there are registered IServiceNotification objects for it. Return
123          * value should be treated as a temporary reference.
124          */
125         HidlService *lookup(
126             const std::string &name);
127         const HidlService *lookup(
128             const std::string &name) const;
129 
130         void insertService(std::unique_ptr<HidlService> &&service);
131 
132         void addPackageListener(sp<IServiceNotification> listener);
133         bool removePackageListener(const wp<IBase>& who);
134         bool removeServiceListener(const wp<IBase>& who);
135 
136         void sendPackageRegistrationNotification(
137             const hidl_string &fqName,
138             const hidl_string &instanceName);
139 
140     private:
141         InstanceMap mInstanceMap{};
142 
143         std::vector<sp<IServiceNotification>> mPackageListeners{};
144     };
145 
146     AccessControl mAcl;
147 
148     /**
149      * Access to this map doesn't need to be locked, since hwservicemanager
150      * is single-threaded.
151      *
152      * e.x.
153      * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
154      *     -> HidlService object
155      */
156     std::map<
157         std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
158         PackageInterfaceMap
159     > mServiceMap;
160 };
161 
162 }  // namespace implementation
163 }  // namespace manager
164 }  // namespace hidl
165 }  // namespace android
166