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