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