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 #include "form_router_proxy_mgr.h"
17 
18 #include "fms_log_wrapper.h"
19 #include "form_mgr_errors.h"
20 #include "form_task_mgr.h"
21 #include "running_form_info.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 
SetDeathRecipient(const sptr<IRemoteObject> & callerToken,const sptr<IRemoteObject::DeathRecipient> & deathRecipient)26 void FormRouterProxyMgr::SetDeathRecipient(const sptr<IRemoteObject> &callerToken,
27     const sptr<IRemoteObject::DeathRecipient> &deathRecipient)
28 {
29     HILOG_DEBUG("Start");
30     std::lock_guard<std::mutex> lock(deathRecipientsMutex_);
31     auto iter = deathRecipients_.find(callerToken);
32     if (iter == deathRecipients_.end()) {
33         deathRecipients_.emplace(callerToken, deathRecipient);
34         callerToken->AddDeathRecipient(deathRecipient);
35     } else {
36         HILOG_DEBUG("The deathRecipient has been added");
37     }
38 }
39 
SetFormRouterProxy(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken)40 ErrCode FormRouterProxyMgr::SetFormRouterProxy(const std::vector<int64_t> &formIds,
41     const sptr<IRemoteObject> &callerToken)
42 {
43     HILOG_DEBUG("call");
44     std::lock_guard<std::mutex> lock(formRouterProxyMutex_);
45     for (const auto &formId : formIds) {
46         auto iter = formRouterProxyMap_.find(formId);
47         if (iter != formRouterProxyMap_.end()) {
48             iter->second = callerToken;
49             continue;
50         }
51         formRouterProxyMap_.emplace(formId, callerToken);
52     }
53     auto dealthRecipient = new (std::nothrow) FormRouterProxyMgr::ClientDeathRecipient();
54     if (dealthRecipient == nullptr) {
55         HILOG_ERROR("create ClientDealthRecipient failed");
56         return ERR_APPEXECFWK_FORM_COMMON_CODE;
57     }
58     SetDeathRecipient(callerToken, dealthRecipient);
59     return ERR_OK;
60 }
61 
RemoveFormRouterProxy(const std::vector<int64_t> & formIds)62 ErrCode FormRouterProxyMgr::RemoveFormRouterProxy(const std::vector<int64_t> &formIds)
63 {
64     HILOG_DEBUG("call");
65     for (int64_t formId : formIds) {
66         std::lock_guard<std::mutex> lock(formRouterProxyMutex_);
67         auto formRouterProxys = formRouterProxyMap_.find(formId);
68         if (formRouterProxys == formRouterProxyMap_.end()) {
69             HILOG_INFO("no formRouterProxy has been register");
70         } else {
71             formRouterProxyMap_.erase(formId);
72         }
73     }
74     return ERR_OK;
75 }
76 
HasRouterProxy(int64_t formId)77 bool FormRouterProxyMgr::HasRouterProxy(int64_t formId)
78 {
79     HILOG_DEBUG("call");
80     std::lock_guard<std::mutex> lock(formRouterProxyMutex_);
81     return formRouterProxyMap_.find(formId) != formRouterProxyMap_.end();
82 }
83 
OnFormRouterEvent(int64_t formId,const Want & want)84 void FormRouterProxyMgr::OnFormRouterEvent(int64_t formId, const Want &want)
85 {
86     HILOG_DEBUG("call");
87     if (!HasRouterProxy(formId)) {
88         HILOG_WARN("This form no formRouterProxy has been register");
89         return;
90     }
91     std::lock_guard<std::mutex> lock(formRouterProxyMutex_);
92     auto routerProxy = formRouterProxyMap_[formId];
93     if (routerProxy == nullptr) {
94         return;
95     }
96     FormTaskMgr::GetInstance().PostRouterProxyToHost(formId, routerProxy, want);
97 }
98 
CleanResource(const wptr<IRemoteObject> & remote)99 void FormRouterProxyMgr::CleanResource(const wptr<IRemoteObject> &remote)
100 {
101     HILOG_DEBUG("Start");
102 
103     // Clean the formRouterProxyMap_.
104     auto object = remote.promote();
105     if (object == nullptr) {
106         HILOG_ERROR("null remoteObject");
107         return;
108     }
109     std::lock_guard<std::mutex> lock(formRouterProxyMutex_);
110     for (auto it = formRouterProxyMap_.begin(); it != formRouterProxyMap_.end();) {
111         if (it->second == object) {
112             formRouterProxyMap_.erase(it++);
113         } else {
114             it++;
115         }
116     }
117 
118     std::lock_guard<std::mutex> deathLock(deathRecipientsMutex_);
119     auto iter = deathRecipients_.find(object);
120     if (iter != deathRecipients_.end()) {
121         auto deathRecipient = iter->second;
122         deathRecipients_.erase(iter);
123         object->RemoveDeathRecipient(deathRecipient);
124     }
125     HILOG_DEBUG("End");
126 }
127 
OnRemoteDied(const wptr<IRemoteObject> & remote)128 void FormRouterProxyMgr::ClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
129 {
130     HILOG_DEBUG("Remote died");
131     FormRouterProxyMgr::GetInstance().CleanResource(remote);
132 }
133 
134 }  // namespace AppExecFwk
135 }  // namespace OHOS
136