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