1 /*
2 * Copyright (c) 2021-2022 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_host_record.h"
17
18 #include <cinttypes>
19 #include "form_task_mgr.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 /**
24 * @brief Add form id.
25 * @param formId The Id of the form.
26 */
AddForm(int64_t formId)27 void FormHostRecord::AddForm(int64_t formId)
28 {
29 if (forms_.find(formId) != forms_.end()) {
30 return;
31 }
32 forms_[formId] = true;
33 }
34 /**
35 * @brief Delete form id.
36 * @param formId The Id of the form.
37 */
DelForm(int64_t formId)38 void FormHostRecord::DelForm(int64_t formId)
39 {
40 forms_.erase(formId);
41 }
42 /**
43 * @brief forms_ is empty or not.
44 * @return forms_ is empty or not.
45 */
IsEmpty() const46 bool FormHostRecord::IsEmpty() const
47 {
48 return forms_.empty();
49 }
50 /**
51 * @brief formId is in forms_ or not.
52 * @param formId The Id of the form.
53 * @return formId is in forms_ or not.
54 */
Contains(int64_t formId) const55 bool FormHostRecord::Contains(int64_t formId) const
56 {
57 return forms_.find(formId) != forms_.end();
58 }
59
60 /**
61 * @brief Set refresh enable flag.
62 * @param formId The Id of the form.
63 * @param flag True for enable, false for disable.
64 */
SetEnableRefresh(int64_t formId,bool flag)65 void FormHostRecord::SetEnableRefresh(int64_t formId, bool flag)
66 {
67 if (forms_.find(formId) == forms_.end()) {
68 return;
69 }
70 forms_[formId] = flag;
71 }
72 /**
73 * @brief Refresh enable or not.
74 * @param formId The Id of the form.
75 * @return true on enable, false on disable..
76 */
IsEnableRefresh(int64_t formId) const77 bool FormHostRecord::IsEnableRefresh(int64_t formId) const
78 {
79 auto result = forms_.find(formId);
80 if (result != forms_.end()) {
81 return result->second;
82 }
83 return false;
84 }
85 /**
86 * @brief Set Update enable flag.
87 * @param formId The Id of the form.
88 * @param flag True for enbale, false for disable.
89 */
SetEnableUpdate(int64_t formId,bool flag)90 void FormHostRecord::SetEnableUpdate(int64_t formId, bool flag)
91 {
92 auto result = forms_.find(formId);
93 if (result == forms_.end()) {
94 HILOG_ERROR("%{public}s: formId: %{public}" PRId64 "not found", __func__, formId);
95 return;
96 }
97 enableUpdateMap_[formId] = flag;
98 }
99 /**
100 * @brief update enable or not.
101 * @param formId The Id of the form.
102 * @return true on enbale, false on disable..
103 */
IsEnableUpdate(int64_t formId) const104 bool FormHostRecord::IsEnableUpdate(int64_t formId) const
105 {
106 auto result = enableUpdateMap_.find(formId);
107 if (result == forms_.end()) {
108 return false;
109 }
110 return result->second;
111 }
112 /**
113 * @brief Set need refresh enable flag.
114 * @param formId The Id of the form.
115 * @param flag True for enable, false for disable.
116 */
SetNeedRefresh(int64_t formId,bool flag)117 void FormHostRecord::SetNeedRefresh(int64_t formId, bool flag)
118 {
119 needRefresh_[formId] = flag;
120 }
121 /**
122 * @brief Need Refresh enable or not.
123 * @param formId The Id of the form.
124 * @return true on enable, false on disable..
125 */
IsNeedRefresh(int64_t formId) const126 bool FormHostRecord::IsNeedRefresh(int64_t formId) const
127 {
128 auto result = needRefresh_.find(formId);
129 if (result != needRefresh_.end()) {
130 return result->second;
131 }
132 return false;
133 }
134 /**
135 * @brief Get clientStub_.
136 * @return clientStub_.
137 */
GetClientStub() const138 sptr<IRemoteObject> FormHostRecord::GetClientStub() const
139 {
140 return clientStub_;
141 }
142
143 /**
144 * @brief Send form data to form host.
145 * @param id The Id of the form.
146 * @param record Form record.
147 */
OnAcquire(int64_t id,const FormRecord & record)148 void FormHostRecord::OnAcquire(int64_t id, const FormRecord &record)
149 {
150 HILOG_DEBUG("FormHostRecord OnAcquire");
151 if (clientImpl_ == nullptr) {
152 HILOG_ERROR("%{public}s: clientImpl_ can not be NULL", __func__);
153 return;
154 }
155
156 clientImpl_->OnAcquired(id, record, clientStub_);
157 }
158
159 /**
160 * @brief Update form data to form host.
161 * @param id The Id of the form.
162 * @param record Form record.
163 */
OnUpdate(int64_t id,const FormRecord & record)164 void FormHostRecord::OnUpdate(int64_t id, const FormRecord &record)
165 {
166 HILOG_INFO("%{public}s start.", __func__);
167
168 if (clientImpl_ == nullptr) {
169 HILOG_ERROR("%{public}s: clientImpl_ can not be null.", __func__);
170 return;
171 }
172
173 clientImpl_->OnUpdate(id, record, clientStub_);
174 }
175
176 /**
177 * @brief Send form uninstall message to form host.
178 * @param id The Id of the form.
179 * @param record Form record.
180 */
OnFormUninstalled(std::vector<int64_t> & formIds)181 void FormHostRecord::OnFormUninstalled(std::vector<int64_t> &formIds)
182 {
183 HILOG_INFO("%{public}s start.", __func__);
184
185 if (clientImpl_ == nullptr) {
186 HILOG_ERROR("%{public}s: clientImpl_ can not be null.", __func__);
187 return;
188 }
189 clientImpl_->OnUninstall(formIds, clientStub_);
190 }
191
192 /**
193 * Send form state message to form host.
194 *
195 * @param state The form state.
196 * @param want The want of onAcquireFormState.
197 */
OnAcquireState(AppExecFwk::FormState state,const AAFwk::Want & want)198 void FormHostRecord::OnAcquireState(AppExecFwk::FormState state, const AAFwk::Want &want)
199 {
200 HILOG_INFO("%{public}s start.", __func__);
201
202 if (clientImpl_ == nullptr) {
203 HILOG_ERROR("%{public}s: clientImpl_ can not be null.", __func__);
204 return;
205 }
206 clientImpl_->OnAcquireState(state, want, clientStub_);
207 }
208
209 /**
210 * @brief Release resource.
211 * @param id The Id of the form.
212 * @param record Form record.
213 */
CleanResource()214 void FormHostRecord::CleanResource()
215 {
216 if (clientStub_ != nullptr && deathRecipient_ != nullptr) {
217 clientStub_->RemoveDeathRecipient(deathRecipient_);
218 clientStub_ = nullptr;
219 deathRecipient_ = nullptr;
220 }
221 }
222 /**
223 * @brief Set value of callerUid_.
224 * @param callerUid Caller uid.
225 */
SetCallerUid(const int callerUid)226 void FormHostRecord::SetCallerUid(const int callerUid)
227 {
228 callerUid_ = callerUid;
229 }
230 /**
231 * @brief Set value of clientStub_.
232 * @param clientStub remote object.
233 */
SetClientStub(const sptr<IRemoteObject> & clientStub)234 void FormHostRecord::SetClientStub(const sptr<IRemoteObject> &clientStub)
235 {
236 clientStub_ = clientStub;
237 }
238 /**
239 * @brief Set value of clientImpl_.
240 * @param clientImpl Form host callback object.
241 */
SetClientImpl(const std::shared_ptr<FormHostCallback> & clientImpl)242 void FormHostRecord::SetClientImpl(const std::shared_ptr<FormHostCallback> &clientImpl)
243 {
244 clientImpl_ = clientImpl;
245 }
246 /**
247 * @brief Get deathRecipient_.
248 * @return deathRecipient_.
249 */
GetDeathRecipient() const250 sptr<IRemoteObject::DeathRecipient> FormHostRecord::GetDeathRecipient() const
251 {
252 return deathRecipient_;
253 }
254 /**
255 * @brief Set value of deathRecipient_.
256 * @param clientImpl DeathRecipient object.
257 */
SetDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & deathRecipient)258 void FormHostRecord::SetDeathRecipient(const sptr<IRemoteObject::DeathRecipient> &deathRecipient)
259 {
260 deathRecipient_ = deathRecipient;
261 }
262 /**
263 * @brief Add deathRecipient object to clientStub_.
264 * @param deathRecipient DeathRecipient object.
265 */
AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & deathRecipient)266 void FormHostRecord::AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> &deathRecipient)
267 {
268 clientStub_->AddDeathRecipient(deathRecipient);
269 }
270
271 /**
272 * @brief Create form host record.
273 * @param info The form item info.
274 * @param callback remote object.
275 * @param callingUid Calling uid.
276 */
CreateRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callback,int callingUid)277 FormHostRecord FormHostRecord::CreateRecord(const FormItemInfo &info,
278 const sptr<IRemoteObject> &callback, int callingUid)
279 {
280 FormHostRecord record;
281 record.SetHostBundleName(info.GetHostBundleName());
282 record.SetCallerUid(callingUid);
283 record.SetClientStub(callback);
284 record.SetClientImpl(std::make_shared<FormHostCallback>());
285 record.SetDeathRecipient(new FormHostRecord::ClientDeathRecipient());
286 record.AddDeathRecipient(record.GetDeathRecipient());
287
288 return record;
289 }
290
291 /**
292 * @brief handle remote object died event.
293 * @param remote remote object.
294 */
OnRemoteDied(const wptr<IRemoteObject> & remote)295 void FormHostRecord::ClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
296 {
297 HILOG_DEBUG("Form remote died");
298 FormTaskMgr::GetInstance().PostHostDiedTask(remote.promote());
299 }
300
301 /**
302 * @brief Get hostBundleName_.
303 * @return hostBundleName_.
304 */
GetHostBundleName() const305 std::string FormHostRecord::GetHostBundleName() const
306 {
307 return hostBundleName_;
308 }
309 /**
310 * @brief Set hostBundleName_.
311 * @param hostBandleName Host bundle name.
312 */
SetHostBundleName(const std::string & hostBundleName)313 void FormHostRecord::SetHostBundleName(const std::string &hostBundleName)
314 {
315 hostBundleName_ = hostBundleName;
316 }
317 } // namespace AppExecFwk
318 } // namespace OHOS
319