1 /*
2  * Copyright (c) 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 #ifndef OHOS_FORM_FWK_FORM_ROUTER_PROXY_MGR_H
17 #define OHOS_FORM_FWK_FORM_ROUTER_PROXY_MGR_H
18 
19 #include <singleton.h>
20 #include <vector>
21 
22 #include "iremote_object.h"
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 using Want = OHOS::AAFwk::Want;
28 /**
29  * @class FormRouterProxyMgr
30  * Form router proxy manager.
31  */
32 class FormRouterProxyMgr : public DelayedRefSingleton<FormRouterProxyMgr> {
33 public:
34 
35     /**
36      * @brief Set value of router proxies.
37      * @param formIds the form ids of Router proxy.
38      * @param callerToken Router proxy caller token.
39      */
40     ErrCode SetFormRouterProxy(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken);
41 
42     /**
43      * @brief Remove value of router proxies.
44      * @param formIds the form ids of Router proxy.
45      */
46     ErrCode RemoveFormRouterProxy(const std::vector<int64_t> &formIds);
47 
48     /**
49      * @brief Get the router proxy of the current form id.
50      * @param formId the form id of router proxy.
51      */
52     bool HasRouterProxy(int64_t formId);
53 
54     /**
55      * @brief Trigger the router event info of this form id.
56      * @param formId the form id of router proxy.
57      * @param want the want info for router event.
58      */
59     void OnFormRouterEvent(int64_t formId, const Want &want);
60 
61     /**
62      * @brief when form proxy died clean the resource.
63      * @param remote remote object.
64      */
65     void CleanResource(const wptr<IRemoteObject> &remote);
66 
67 private:
68     /**
69      * @brief Set value of deathRecipient_.
70      * @param callerToken Caller ability token.
71      * @param deathRecipient DeathRecipient object.
72      */
73     void SetDeathRecipient(const sptr<IRemoteObject> &callerToken,
74         const sptr<IRemoteObject::DeathRecipient> &deathRecipient);
75 
76     mutable std::mutex formRouterProxyMutex_;
77     mutable std::mutex deathRecipientsMutex_;
78     std::map<int64_t, sptr<IRemoteObject>> formRouterProxyMap_;
79     std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> deathRecipients_;
80 
81     /**
82      * @class ClientDeathRecipient
83      * notices IRemoteBroker died.
84      */
85     class ClientDeathRecipient : public IRemoteObject::DeathRecipient {
86     public:
87         /**
88          * @brief Constructor
89          */
90         ClientDeathRecipient() = default;
91         virtual ~ClientDeathRecipient() = default;
92         /**
93          * @brief handle remote object died event.
94          * @param remote remote object.
95          */
96         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
97     };
98 };
99 }  // namespace AppExecFwk
100 }  // namespace OHOS
101 
102 #endif // OHOS_FORM_FWK_FORM_ROUTER_PROXY_MGR_H
103