• 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_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("%{public}s: formId: %{public}" PRId64 "not found", __func__, 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("%{public}s: formHostCallback_ can not be NULL", __func__);
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("%{public}s start.", __func__);
166     if (formHostCallback_ == nullptr) {
167         HILOG_ERROR("%{public}s: formHostCallback_ can not be null.", __func__);
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("%{public}s start.", __func__);
180     if (formHostCallback_ == nullptr) {
181         HILOG_ERROR("%{public}s: formHostCallback_ can not be null.", __func__);
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("%{public}s start.", __func__);
196     if (formHostCallback_ == nullptr) {
197         HILOG_ERROR("%{public}s: formHostCallback_ can not be null.", __func__);
198         return;
199     }
200     formHostCallback_->OnAcquireState(state, want, formHostClient_);
201 }
202 
203 /**
204  * @brief Release resource.
205  * @param id The Id of the form.
206  * @param record Form record.
207  */
CleanResource()208 void FormHostRecord::CleanResource()
209 {
210     if (formHostClient_ != nullptr && deathRecipient_ != nullptr) {
211         formHostClient_->RemoveDeathRecipient(deathRecipient_);
212         formHostClient_ = nullptr;
213         deathRecipient_ = nullptr;
214     }
215 }
216 /**
217  * @brief Set value of callerUid_.
218  * @param callerUid Caller uid.
219  */
SetCallerUid(const int callerUid)220 void FormHostRecord::SetCallerUid(const int callerUid)
221 {
222     callerUid_ = callerUid;
223 }
224 /**
225  * @brief Set value of formHostClient_.
226  * @param formHostClient remote object.
227  */
SetFormHostClient(const sptr<IRemoteObject> & formHostClient)228 void FormHostRecord::SetFormHostClient(const sptr<IRemoteObject> &formHostClient)
229 {
230     formHostClient_ = formHostClient;
231 }
232 /**
233  * @brief Set value of formHostCallback_.
234  * @param formHostCallback Form host callback object.
235  */
SetCallback(const std::shared_ptr<FormHostCallback> & formHostCallback)236 void FormHostRecord::SetCallback(const std::shared_ptr<FormHostCallback> &formHostCallback)
237 {
238     formHostCallback_ = formHostCallback;
239 }
240 /**
241  * @brief Get deathRecipient_.
242  * @return deathRecipient_.
243  */
GetDeathRecipient() const244 sptr<IRemoteObject::DeathRecipient> FormHostRecord::GetDeathRecipient() const
245 {
246     return deathRecipient_;
247 }
248 /**
249  * @brief Set value of deathRecipient_.
250  * @param formHostCallback DeathRecipient object.
251  */
SetDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & deathRecipient)252 void FormHostRecord::SetDeathRecipient(const sptr<IRemoteObject::DeathRecipient> &deathRecipient)
253 {
254     deathRecipient_ = deathRecipient;
255 }
256 /**
257  * @brief Add deathRecipient object to formHostClient_.
258  * @param deathRecipient DeathRecipient object.
259  */
AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> & deathRecipient)260 void FormHostRecord::AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient> &deathRecipient)
261 {
262     if (formHostClient_ == nullptr) {
263         return;
264     }
265     formHostClient_->AddDeathRecipient(deathRecipient);
266 }
267 
268 /**
269  * @brief Create form host record.
270  * @param info The form item info.
271  * @param callback remote object.
272  * @param callingUid Calling uid.
273  */
CreateRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callback,int callingUid)274 FormHostRecord FormHostRecord::CreateRecord(const FormItemInfo &info,
275     const sptr<IRemoteObject> &callback, int callingUid)
276 {
277     FormHostRecord record;
278     record.SetHostBundleName(info.GetHostBundleName());
279     record.SetCallerUid(callingUid);
280     record.SetFormHostClient(callback);
281     record.SetCallback(std::make_shared<FormHostCallback>());
282     record.SetDeathRecipient(new (std::nothrow) FormHostRecord::ClientDeathRecipient());
283     record.AddDeathRecipient(record.GetDeathRecipient());
284     return record;
285 }
286 
287 /**
288  * @brief handle remote object died event.
289  * @param remote remote object.
290  */
OnRemoteDied(const wptr<IRemoteObject> & remote)291 void FormHostRecord::ClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
292 {
293     HILOG_DEBUG("Form remote died");
294     FormTaskMgr::GetInstance().PostHostDiedTask(remote.promote());
295 }
296 
297 /**
298  * @brief Get hostBundleName_.
299  * @return hostBundleName_.
300  */
GetHostBundleName() const301 std::string FormHostRecord::GetHostBundleName() const
302 {
303     return hostBundleName_;
304 }
305 /**
306  * @brief Set hostBundleName_.
307  * @param hostBundleName Host bundle name.
308  */
SetHostBundleName(const std::string & hostBundleName)309 void FormHostRecord::SetHostBundleName(const std::string &hostBundleName)
310 {
311     hostBundleName_ = hostBundleName;
312 }
313 }  // namespace AppExecFwk
314 }  // namespace OHOS
315