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_callback.h"
17 
18 #include <cinttypes>
19 
20 #include "fms_log_wrapper.h"
21 #include "form_host_interface.h"
22 #include "form_task_mgr.h"
23 #include "form_render_mgr.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 /**
28  * @brief Request to give back a Form.
29  * @param formId The Id of the forms to create.
30  * @param record Form record.
31  * @param callerToken Caller ability token.
32  */
OnAcquired(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & callerToken)33 void FormHostCallback::OnAcquired(const int64_t formId, const FormRecord& record,
34     const sptr<IRemoteObject> &callerToken)
35 {
36     HILOG_DEBUG("FormHostCallback OnAcquired, formId:%{public}" PRId64 "", formId);
37     FormTaskMgr::GetInstance().PostAcquireTaskToHost(formId, record, callerToken);
38 }
39 
40 /**
41 * @brief Form is updated.
42 * @param formId The Id of the form to update.
43 * @param record Form record.
44 * @param callerToken Caller ability token.
45 */
OnUpdate(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & callerToken)46 void FormHostCallback::OnUpdate(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &callerToken)
47 {
48     HILOG_INFO("start");
49 
50     // check formId
51     if (formId < 0) {
52         HILOG_ERROR("OnUpdate invalid param, formId:%{public}" PRId64 ".", formId);
53         return;
54     }
55 
56     if (callerToken == nullptr) {
57         HILOG_ERROR("null callerToken");
58         return;
59     }
60 
61     // post updateTask to host
62     FormTaskMgr::GetInstance().PostUpdateTaskToHost(formId, record, callerToken);
63 }
64 
65 /**
66  * @brief Form provider is uninstalled
67  * @param formIds The Id list of the forms.
68  * @param callerToken Caller ability token.
69  */
OnUninstall(std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken)70 void FormHostCallback::OnUninstall(std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken)
71 {
72     // check formId
73     if (formIds.empty()) {
74         HILOG_ERROR("OnUninstall invalid param, formIds is empty");
75         return;
76     }
77 
78     if (callerToken == nullptr) {
79         HILOG_ERROR("null callerToken");
80         return;
81     }
82     // post updateTask to host
83     FormTaskMgr::GetInstance().PostUninstallTaskToHost(formIds, callerToken);
84 }
85 
OnAcquireFormData(const AAFwk::WantParams & wantParams,int64_t requestCode,const sptr<IRemoteObject> & callerToken)86 void FormHostCallback::OnAcquireFormData(const AAFwk::WantParams &wantParams, int64_t requestCode,
87                                          const sptr<IRemoteObject> &callerToken)
88 {
89     if (callerToken == nullptr) {
90         HILOG_ERROR("null callerToken");
91         return;
92     }
93     // post updateTask to host
94     FormTaskMgr::GetInstance().PostAcquireDataTaskToHost(wantParams, requestCode, callerToken);
95 }
96 
97 /**
98  * @brief Send form state message to form host.
99  * @param state The form state.
100  * @param want The want of onAcquireFormState.
101  * @param callerToken Caller ability token.
102  */
OnAcquireState(AppExecFwk::FormState state,const AAFwk::Want & want,const sptr<IRemoteObject> & callerToken)103 void FormHostCallback::OnAcquireState(AppExecFwk::FormState state, const AAFwk::Want &want,
104                                       const sptr<IRemoteObject> &callerToken)
105 {
106     if (callerToken == nullptr) {
107         HILOG_ERROR("null callerToken");
108         return;
109     }
110     // post updateTask to host
111     FormTaskMgr::GetInstance().PostAcquireStateTaskToHost(state, want, callerToken);
112 }
113 
114 /**
115  * @brief Send recycle form message to form host.
116  * @param formIds The Id list of forms.
117  * @param want The want of forms to be recycled.
118  * @param callerToken Caller ability token.
119  */
OnRecycleForms(const std::vector<int64_t> & formIds,const Want & want,const sptr<IRemoteObject> & callerToken)120 void FormHostCallback::OnRecycleForms(
121     const std::vector<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &callerToken)
122 {
123     HILOG_DEBUG("start");
124     if (formIds.empty()) {
125         HILOG_ERROR("empty formIds");
126         return;
127     }
128     if (callerToken == nullptr) {
129         HILOG_ERROR("null callerToken");
130         return;
131     }
132     FormRenderMgr::GetInstance().RecycleForms(formIds, want, callerToken);
133 }
134 
OnEnableForms(const std::vector<int64_t> & formIds,const bool enable,const sptr<IRemoteObject> & callerToken)135 void FormHostCallback::OnEnableForms(
136     const std::vector<int64_t> &formIds, const bool enable, const sptr<IRemoteObject> &callerToken)
137 {
138     HILOG_INFO("size:%{public}zu, enable:%{public}d", formIds.size(), enable);
139     if (formIds.empty()) {
140         HILOG_ERROR("empty formIds");
141         return;
142     }
143 
144     if (callerToken == nullptr) {
145         HILOG_ERROR("null callerToken");
146         return;
147     }
148     // post enableFormsTask to host
149     FormTaskMgr::GetInstance().PostEnableFormsTaskToHost(formIds, enable, callerToken);
150 }
151 }  // namespace AppExecFwk
152 }  // namespace OHOS
153