• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef OHOS_IPC_IREMOTE_BROKER_H
17 #define OHOS_IPC_IREMOTE_BROKER_H
18 
19 #include <unordered_map>
20 #include <functional>
21 #include "iremote_object.h"
22 #include "refbase.h"
23 
24 namespace OHOS {
25 template <typename T> class BrokerCreator {
26 public:
27     BrokerCreator() = default;
28     ~BrokerCreator() = default;
operator()29     sptr<IRemoteBroker> operator () (const sptr<IRemoteObject> &object)
30     {
31         T *proxy = new (std::nothrow) T(object);
32         if (proxy != nullptr) {
33             return static_cast<IRemoteBroker *>(proxy);
34         }
35         return nullptr;
36     };
37 };
38 
39 class IRemoteBroker : public virtual RefBase {
40 public:
41     IRemoteBroker() = default;
42     virtual ~IRemoteBroker() override = default;
43     virtual sptr<IRemoteObject> AsObject() = 0;
AsImplement(const sptr<IRemoteObject> & object)44     static inline sptr<IRemoteBroker> AsImplement(const sptr<IRemoteObject> &object)
45     {
46         return nullptr;
47     }
48 };
49 
50 #define DECLARE_INTERFACE_DESCRIPTOR(DESCRIPTOR)                         \
51     static inline const std::u16string metaDescriptor_ = { DESCRIPTOR }; \
52     static inline const std::u16string &GetDescriptor()                  \
53     {                                                                    \
54         return metaDescriptor_;                                          \
55     }
56 
57 class BrokerRegistration {
58     using Constructor = std::function<sptr<IRemoteBroker>(const sptr<IRemoteObject> &object)>;
59 
60 public:
61     static BrokerRegistration &Get();
62     bool Register(const std::u16string &descriptor, const Constructor &creator);
63     void Unregister(const std::u16string &descriptor);
64     sptr<IRemoteBroker> NewInstance(const std::u16string &descriptor, const sptr<IRemoteObject> &object);
65 
66 protected:
67     BrokerRegistration() = default;
68     ~BrokerRegistration();
69 
70 private:
71     BrokerRegistration(const BrokerRegistration &) = delete;
72     BrokerRegistration(BrokerRegistration &&) = delete;
73     BrokerRegistration &operator = (const BrokerRegistration &) = delete;
74     BrokerRegistration &operator = (BrokerRegistration &&) = delete;
75     std::mutex creatorMutex_;
76     std::unordered_map<std::u16string, Constructor> creators_;
77 };
78 
79 template <typename T> class BrokerDelegator {
80 public:
81     BrokerDelegator();
82     ~BrokerDelegator();
83 
84 private:
85     BrokerDelegator(const BrokerDelegator &) = delete;
86     BrokerDelegator(BrokerDelegator &&) = delete;
87     BrokerDelegator &operator = (const BrokerDelegator &) = delete;
88     BrokerDelegator &operator = (BrokerDelegator &&) = delete;
89 };
90 
BrokerDelegator()91 template <typename T> BrokerDelegator<T>::BrokerDelegator()
92 {
93     const std::u16string descriptor = T::GetDescriptor();
94     BrokerRegistration &registration = BrokerRegistration::Get();
95     registration.Register(descriptor, BrokerCreator<T>());
96 }
97 
~BrokerDelegator()98 template <typename T> BrokerDelegator<T>::~BrokerDelegator()
99 {
100     const std::u16string descriptor = T::GetDescriptor();
101     BrokerRegistration &registration = BrokerRegistration::Get();
102     registration.Unregister(descriptor);
103 }
104 
iface_cast(const sptr<IRemoteObject> & object)105 template <typename INTERFACE> inline sptr<INTERFACE> iface_cast(const sptr<IRemoteObject> &object)
106 {
107     const std::u16string descriptor = INTERFACE::GetDescriptor();
108     BrokerRegistration &registration = BrokerRegistration::Get();
109     sptr<IRemoteBroker> broker = registration.NewInstance(descriptor, object);
110     return static_cast<INTERFACE *>(broker.GetRefPtr());
111 }
112 } // namespace OHOS
113 #endif // OHOS_IPC_IREMOTE_BROKER_H
114