1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /**
17 * @addtogroup DriverHdi
18 * @{
19 *
20 * @brief Provides APIs for a system ability to obtain hardware device interface (HDI) services,
21 * load or unload a device, and listen for service status, and capabilities for the hdi-gen tool to
22 * automatically generate code in the interface description language (IDL).
23 *
24 * The HDF and the IDL code generated allow system abilities to access HDI driver services.
25 *
26 * @since 1.0
27 */
28
29 /**
30 * @file object_collector.h
31 *
32 * @brief Defines an object collector for the hdi-gen tool to manage constructors and objects.
33 * When generating a class, the hdi-gen tool defines a static inline object of the <b>ObjectDelegator</b> type
34 * and registers the interface name and constructor with the object collector.
35 * When the service module driver is initialized, the registered constructor (obtained based on the interface name) is
36 * used to create and register the IPC stub object.
37 *
38 * @since 1.0
39 */
40
41 #ifndef HDI_OBJECT_COLLECTOR_H
42 #define HDI_OBJECT_COLLECTOR_H
43
44 #include <string>
45 #include <hdi_base.h>
46 #include <iremote_object.h>
47 #include <map>
48 #include <mutex>
49 #include <refbase.h>
50
51 namespace OHOS {
52 namespace HDI {
53 /**
54 * @brief Defines the <b>ObjectCollector</b> class for the hdi-gen tool to manage constructors and objects.
55 */
56 class ObjectCollector {
57 public:
58 /** Define the constructor interface type. */
59 using Constructor = std::function<sptr<IRemoteObject>(const sptr<HdiBase> &interface)>;
60 /** Obtain the object instance managed by the ObjectCollector. */
61 static ObjectCollector &GetInstance();
62 /** Register a constructor based on the interface name. */
63 bool ConstructorRegister(const std::u16string &interfaceName, const Constructor &constructor);
64 /** Unregister a constructor based on the interface name. */
65 void ConstructorUnRegister(const std::u16string &interfaceName);
66 /** Create an object based on the interface name and implementation. */
67 sptr<IRemoteObject> NewObject(const sptr<HdiBase> &interface, const std::u16string &interfaceName);
68 /** Create or obtain an object based on the interface name and implementation. */
69 sptr<IRemoteObject> GetOrNewObject(const sptr<HdiBase> &interface, const std::u16string &interfaceName);
70 /** Remove an object based on the interface implementation. */
71 bool RemoveObject(const sptr<HdiBase> &interface);
72
73 private:
74 ObjectCollector() = default;
75 /** Create an object based on the interface name and implementation. */
76 sptr<IRemoteObject> NewObjectLocked(const sptr<HdiBase> &interface, const std::u16string &interfaceName);
77 /** Instance of the object managed by the ObjectCollector. */
78 static ObjectCollector *instance_;
79 /** Define the mapping between the interface name and the constructor. */
80 std::map<const std::u16string, const Constructor> constructorMapper_;
81 /** Define the mapping between the interface implementation and the object. */
82 std::map<HdiBase *, IRemoteObject *> interfaceObjectCollector_;
83 /** Define a mutex to support concurrent access to the ObjectCollector. */
84 std::mutex mutex_;
85 };
86
87 /**
88 * @brief Defines a <b>ObjectDelegator</b> template, which is used to register and
89 * unregister a template object constructor.
90 */
91 template <typename OBJECT, typename INTERFACE>
92 class ObjectDelegator {
93 public:
94 /** Constructor, which registers a template object constructor through the ObjectCollector. */
95 ObjectDelegator();
96 /** Destructor, which unregisters a template object constructor through the ObjectCollector. */
97 ~ObjectDelegator();
98
99 private:
100 ObjectDelegator(const ObjectDelegator &) = delete;
101 ObjectDelegator(ObjectDelegator &&) = delete;
102 ObjectDelegator &operator=(const ObjectDelegator &) = delete;
103 ObjectDelegator &operator=(ObjectDelegator &&) = delete;
104 std::u16string descriptor_;
105 };
106
107 /**
108 * @brief A constructor used to register an object constructor of the <b>ObjectDelegator</b> template
109 * through the <b>ObjectCollector</b>.
110 *
111 * @param OBJECT Indicates the type of the object to create.
112 * @param INTERFACE Indicates the HDI interface of the service module.
113 */
114 template <typename OBJECT, typename INTERFACE>
ObjectDelegator()115 ObjectDelegator<OBJECT, INTERFACE>::ObjectDelegator()
116 {
117 auto creator = [](const sptr<HdiBase> &interface) -> sptr<IRemoteObject> {
118 return new OBJECT(static_cast<INTERFACE *>(interface.GetRefPtr()));
119 };
120 if (ObjectCollector::GetInstance().ConstructorRegister(INTERFACE::GetDescriptor(), creator)) {
121 descriptor_ = INTERFACE::GetDescriptor();
122 }
123 }
124
125 /**
126 * @brief A destructor used to unregister an object constructor of the <b>ObjectDelegator</b> template
127 * through the <b>ObjectCollector</b>.
128 *
129 * @param OBJECT Indicates the type of the object.
130 * @param INTERFACE Indicates the HDI interface of the service module.
131 */
132 template <typename OBJECT, typename INTERFACE>
~ObjectDelegator()133 ObjectDelegator<OBJECT, INTERFACE>::~ObjectDelegator()
134 {
135 ObjectCollector::GetInstance().ConstructorUnRegister(descriptor_);
136 }
137 } // namespace HDI
138 } // namespace OHOS
139
140 #endif // HDI_OBJECT_COLLECTOR_H
141