• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/form_host_record.h"
17 
18 #include <cinttypes>
19 #include "form_host/form_host_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     std::string bundleName = info.GetHostBundleName();
289     record.SetHostBundleName(bundleName);
290     record.SetCallerUid(callingUid);
291     record.SetFormHostClient(callback);
292     record.SetCallback(std::make_shared<FormHostCallback>());
293     record.SetDeathRecipient(new (std::nothrow) FormHostRecord::ClientDeathRecipient(bundleName));
294     record.AddDeathRecipient(record.GetDeathRecipient());
295     return record;
296 }
297 
298 /**
299  * @brief handle remote object died event.
300  * @param remote remote object.
301  */
OnRemoteDied(const wptr<IRemoteObject> & remote)302 void FormHostRecord::ClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
303 {
304     HILOG_INFO("Form remote died hostBundleName: %{public}s", hostBundleName_.c_str());
305     FormHostTaskMgr::GetInstance().PostHostDiedTask(remote.promote());
306 }
307 
308 /**
309  * @brief Get hostBundleName_.
310  * @return hostBundleName_.
311  */
GetHostBundleName() const312 std::string FormHostRecord::GetHostBundleName() const
313 {
314     return hostBundleName_;
315 }
316 /**
317  * @brief Set hostBundleName_.
318  * @param hostBundleName Host bundle name.
319  */
SetHostBundleName(const std::string & hostBundleName)320 void FormHostRecord::SetHostBundleName(const std::string &hostBundleName)
321 {
322     hostBundleName_ = hostBundleName;
323 }
324 
GetFormsCount() const325 int32_t FormHostRecord::GetFormsCount() const
326 {
327     return static_cast<int32_t>(forms_.size());
328 }
329 
OnRecycleForms(const std::vector<int64_t> & formIds,const Want & want) const330 void FormHostRecord::OnRecycleForms(const std::vector<int64_t> &formIds, const Want &want) const
331 {
332     HILOG_DEBUG("start");
333     if (formIds.empty()) {
334         HILOG_ERROR("empty formIds");
335         return;
336     }
337     if (formHostCallback_ == nullptr) {
338         HILOG_ERROR("null formHostCallback_");
339         return;
340     }
341     formHostCallback_->OnRecycleForms(formIds, want, formHostClient_);
342 }
343 
OnEnableForms(const std::vector<int64_t> & formIds,const bool enable)344 void FormHostRecord::OnEnableForms(const std::vector<int64_t> &formIds, const bool enable)
345 {
346     HILOG_DEBUG("start");
347     if (formIds.empty()) {
348         HILOG_ERROR("empty formIds");
349         return;
350     }
351     if (formHostCallback_ == nullptr) {
352         HILOG_ERROR("null formHostCallback_");
353         return;
354     }
355     formHostCallback_->OnEnableForms(formIds, enable, formHostClient_);
356 }
357 
OnLockForms(const std::vector<int64_t> & formIds,const bool lock)358 void FormHostRecord::OnLockForms(const std::vector<int64_t> &formIds, const bool lock)
359 {
360     HILOG_DEBUG("start");
361     if (formIds.empty()) {
362         HILOG_ERROR("empty formIds");
363         return;
364     }
365     if (formHostCallback_ == nullptr) {
366         HILOG_ERROR("null formHostCallback_");
367         return;
368     }
369     formHostCallback_->OnLockForms(formIds, lock, formHostClient_);
370 }
371 }  // namespace AppExecFwk
372 }  // namespace OHOS
373