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 enable True for enable, false for disable.
89 */
SetEnableUpdate(int64_t formId,bool enable)90 void FormHostRecord::SetEnableUpdate(int64_t formId, bool enable)
91 {
92 auto result = forms_.find(formId);
93 if (result == forms_.end()) {
94 HILOG_ERROR("formId:%{public}" PRId64 "not found", formId);
95 return;
96 }
97 enableUpdateMap_[formId] = enable;
98 }
99 /**
100 * @brief update enable or not.
101 * @param formId The Id of the form.
102 * @return true on enable, 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 == enableUpdateMap_.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 formHostClient_.
136 * @return formHostClient_.
137 */
GetFormHostClient() const138 sptr<IRemoteObject> FormHostRecord::GetFormHostClient() const
139 {
140 return formHostClient_;
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 (formHostCallback_ == nullptr) {
152 HILOG_ERROR("null formHostCallback_");
153 return;
154 }
155 formHostCallback_->OnAcquired(id, record, formHostClient_);
156 }
157
158 /**
159 * @brief Update form data to form host.
160 * @param id The Id of the form.
161 * @param record Form record.
162 */
OnUpdate(int64_t id,const FormRecord & record)163 void FormHostRecord::OnUpdate(int64_t id, const FormRecord &record)
164 {
165 HILOG_INFO("start");
166 if (formHostCallback_ == nullptr) {
167 HILOG_ERROR("null formHostCallback_");
168 return;
169 }
170 formHostCallback_->OnUpdate(id, record, formHostClient_);
171 }
172
173 /**
174 * @brief Send form uninstall message to form host.
175 * @param formIds The id list of the form.
176 */
OnFormUninstalled(std::vector<int64_t> & formIds)177 void FormHostRecord::OnFormUninstalled(std::vector<int64_t> &formIds)
178 {
179 HILOG_INFO("start");
180 if (formHostCallback_ == nullptr) {
181 HILOG_ERROR("null formHostCallback_");
182 return;
183 }
184 formHostCallback_->OnUninstall(formIds, formHostClient_);
185 }
186
187 /**
188 * Send form state message to form host.
189 *
190 * @param state The form state.
191 * @param want The want of onAcquireFormState.
192 */
OnAcquireState(AppExecFwk::FormState state,const AAFwk::Want & want)193 void FormHostRecord::OnAcquireState(AppExecFwk::FormState state, const AAFwk::Want &want)
194 {
195 HILOG_INFO("start");
196 if (formHostCallback_ == nullptr) {
197 HILOG_ERROR("null formHostCallback_");
198 return;
199 }
200 formHostCallback_->OnAcquireState(state, want, formHostClient_);
201 }
202
OnAcquireFormData(const AAFwk::WantParams & wantParams,int64_t requestCode)203 void FormHostRecord::OnAcquireFormData(const AAFwk::WantParams &wantParams, int64_t requestCode)
204 {
205 HILOG_INFO("start");
206 if (formHostCallback_ == nullptr) {
207 HILOG_ERROR("null formHostCallback_");
208 return;
209 }
210 formHostCallback_->OnAcquireFormData(wantParams, requestCode, formHostClient_);
211 }
212
213 /**
214 * @brief Release resource.
215 * @param id The Id of the form.
216 * @param record Form record.
217 */
CleanResource()218 void FormHostRecord::CleanResource()
219 {
220 if (formHostClient_ != nullptr && deathRecipient_ != nullptr) {
221 formHostClient_->RemoveDeathRecipient(deathRecipient_);
222 formHostClient_ = nullptr;
223 deathRecipient_ = nullptr;
224 }
225 }
226 /**
227 * @brief Set value of callerUid_.
228 * @param callerUid Caller uid.
229 */
SetCallerUid(const int callerUid)230 void FormHostRecord::SetCallerUid(const int callerUid)
231 {
232 callerUid_ = callerUid;
233 }
234 /**
235 * @brief Set value of formHostClient_.
236 * @param formHostClient remote object.
237 */
SetFormHostClient(const sptr<IRemoteObject> & formHostClient)238 void FormHostRecord::SetFormHostClient(const sptr<IRemoteObject> &formHostClient)
239 {
240 formHostClient_ = formHostClient;
241 }
242 /**
243 * @brief Set value of formHostCallback_.
244 * @param formHostCallback Form host callback object.
245 */
SetCallback(const std::shared_ptr<FormHostCallback> & formHostCallback)246 void FormHostRecord::SetCallback(const std::shared_ptr<FormHostCallback> &formHostCallback)
247 {
248 formHostCallback_ = formHostCallback;
249 }
250 /**
251 * @brief Get deathRecipient_.
252 * @return deathRecipient_.
253 */
GetDeathRecipient() const254 sptr<IRemoteObject::DeathRecipient> FormHostRecord::GetDeathRecipient() const
255 {
256 return deathRecipient_;
257 }
258 /**
259 * @brief Set value of deathRecipient_.
260 * @param formHostCallback DeathRecipient object.
261 */
SetDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & deathRecipient)262 void FormHostRecord::SetDeathRecipient(const sptr<IRemoteObject::DeathRecipient> &deathRecipient)
263 {
264 deathRecipient_ = deathRecipient;
265 }
266 /**
267 * @brief Add deathRecipient object to formHostClient_.
268 * @param deathRecipient DeathRecipient object.
269 */
AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & deathRecipient)270 void FormHostRecord::AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> &deathRecipient)
271 {
272 if (formHostClient_ == nullptr) {
273 return;
274 }
275 formHostClient_->AddDeathRecipient(deathRecipient);
276 }
277
278 /**
279 * @brief Create form host record.
280 * @param info The form item info.
281 * @param callback remote object.
282 * @param callingUid Calling uid.
283 */
CreateRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callback,int callingUid)284 FormHostRecord FormHostRecord::CreateRecord(const FormItemInfo &info,
285 const sptr<IRemoteObject> &callback, int callingUid)
286 {
287 FormHostRecord record;
288 record.SetHostBundleName(info.GetHostBundleName());
289 record.SetCallerUid(callingUid);
290 record.SetFormHostClient(callback);
291 record.SetCallback(std::make_shared<FormHostCallback>());
292 record.SetDeathRecipient(new (std::nothrow) FormHostRecord::ClientDeathRecipient());
293 record.AddDeathRecipient(record.GetDeathRecipient());
294 return record;
295 }
296
297 /**
298 * @brief handle remote object died event.
299 * @param remote remote object.
300 */
OnRemoteDied(const wptr<IRemoteObject> & remote)301 void FormHostRecord::ClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
302 {
303 HILOG_DEBUG("Form remote died");
304 FormTaskMgr::GetInstance().PostHostDiedTask(remote.promote());
305 }
306
307 /**
308 * @brief Get hostBundleName_.
309 * @return hostBundleName_.
310 */
GetHostBundleName() const311 std::string FormHostRecord::GetHostBundleName() const
312 {
313 return hostBundleName_;
314 }
315 /**
316 * @brief Set hostBundleName_.
317 * @param hostBundleName Host bundle name.
318 */
SetHostBundleName(const std::string & hostBundleName)319 void FormHostRecord::SetHostBundleName(const std::string &hostBundleName)
320 {
321 hostBundleName_ = hostBundleName;
322 }
323
GetFormsCount() const324 int32_t FormHostRecord::GetFormsCount() const
325 {
326 return static_cast<int32_t>(forms_.size());
327 }
328
OnRecycleForms(const std::vector<int64_t> & formIds,const Want & want) const329 void FormHostRecord::OnRecycleForms(const std::vector<int64_t> &formIds, const Want &want) const
330 {
331 HILOG_DEBUG("start");
332 if (formIds.empty()) {
333 HILOG_ERROR("empty formIds");
334 return;
335 }
336 if (formHostCallback_ == nullptr) {
337 HILOG_ERROR("null formHostCallback_");
338 return;
339 }
340 formHostCallback_->OnRecycleForms(formIds, want, formHostClient_);
341 }
342
OnEnableForms(const std::vector<int64_t> & formIds,const bool enable)343 void FormHostRecord::OnEnableForms(const std::vector<int64_t> &formIds, const bool enable)
344 {
345 HILOG_DEBUG("start");
346 if (formIds.empty()) {
347 HILOG_ERROR("empty formIds");
348 return;
349 }
350 if (formHostCallback_ == nullptr) {
351 HILOG_ERROR("null formHostCallback_");
352 return;
353 }
354 formHostCallback_->OnEnableForms(formIds, enable, formHostClient_);
355 }
356 } // namespace AppExecFwk
357 } // namespace OHOS
358