1 /*
2  * Copyright (c) 2023 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 "js_form_state_observer_proxy.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "ipc_types.h"
22 #include "running_form_info.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
OnAddForm(const std::string & bundleName,const AppExecFwk::RunningFormInfo & runningFormInfo)26 int32_t JsFormStateObserverProxy::OnAddForm(const std::string &bundleName,
27     const AppExecFwk::RunningFormInfo &runningFormInfo)
28 {
29     MessageParcel data;
30     MessageParcel reply;
31     MessageOption option(MessageOption::TF_ASYNC);
32 
33     if (!data.WriteInterfaceToken(AbilityRuntime::IJsFormStateObserver::GetDescriptor())) {
34         HILOG_ERROR("write interface token failed");
35         return ERR_APPEXECFWK_PARCEL_ERROR;
36     }
37 
38     if (!data.WriteString(bundleName)) {
39         HILOG_ERROR("write bundleName failed");
40         return ERR_APPEXECFWK_PARCEL_ERROR;
41     }
42 
43     if (!data.WriteParcelable(&runningFormInfo)) {
44         HILOG_ERROR("write runningFormInfo failed");
45         return ERR_APPEXECFWK_PARCEL_ERROR;
46     }
47 
48     int32_t error = SendTransactCmd(
49         IJsFormStateObserver::Message::FORM_STATE_OBSERVER_ON_ADD_FORM,
50         data,
51         reply,
52         option);
53     if (error != ERR_OK) {
54         HILOG_ERROR("SendRequest:%{public}d failed", error);
55     }
56     return error;
57 }
58 
OnRemoveForm(const std::string & bundleName,const AppExecFwk::RunningFormInfo & runningFormInfo)59 int32_t JsFormStateObserverProxy::OnRemoveForm(const std::string &bundleName,
60     const AppExecFwk::RunningFormInfo &runningFormInfo)
61 {
62     MessageParcel data;
63     MessageParcel reply;
64     MessageOption option(MessageOption::TF_ASYNC);
65 
66     if (!data.WriteInterfaceToken(AbilityRuntime::IJsFormStateObserver::GetDescriptor())) {
67         HILOG_ERROR("write interface token failed");
68         return ERR_APPEXECFWK_PARCEL_ERROR;
69     }
70 
71     if (!data.WriteString(bundleName)) {
72         HILOG_ERROR("write bundleName failed");
73         return ERR_APPEXECFWK_PARCEL_ERROR;
74     }
75 
76     if (!data.WriteParcelable(&runningFormInfo)) {
77         HILOG_ERROR("write runningFormInfo failed");
78         return ERR_APPEXECFWK_PARCEL_ERROR;
79     }
80 
81     int32_t error = SendTransactCmd(
82         IJsFormStateObserver::Message::FORM_STATE_OBSERVER_ON_REMOVE_FORM,
83         data,
84         reply,
85         option);
86     if (error != ERR_OK) {
87         HILOG_ERROR("SendRequest:%{public}d failed", error);
88     }
89     return error;
90 }
91 
NotifyWhetherFormsVisible(const AppExecFwk::FormVisibilityType formVisiblityType,const std::string & bundleName,std::vector<AppExecFwk::FormInstance> & formInstances)92 int32_t JsFormStateObserverProxy::NotifyWhetherFormsVisible(const AppExecFwk::FormVisibilityType formVisiblityType,
93     const std::string &bundleName, std::vector<AppExecFwk::FormInstance> &formInstances)
94 {
95     HILOG_DEBUG("call");
96     MessageParcel data;
97     MessageParcel reply;
98     MessageOption option(MessageOption::TF_ASYNC);
99 
100     if (!data.WriteInterfaceToken(AbilityRuntime::IJsFormStateObserver::GetDescriptor())) {
101         HILOG_ERROR("write interface token failed");
102         return ERR_APPEXECFWK_PARCEL_ERROR;
103     }
104     int32_t formVisiblityTypeInt = static_cast<int32_t>(formVisiblityType);
105     if (!data.WriteInt32(formVisiblityTypeInt)) {
106         HILOG_ERROR("fail write formVisiblityType");
107         return ERR_APPEXECFWK_PARCEL_ERROR;
108     }
109     if (!data.WriteString(bundleName)) {
110         HILOG_ERROR("fail write formVisiblityType");
111         return ERR_APPEXECFWK_PARCEL_ERROR;
112     }
113     HILOG_DEBUG("NotifyWhetherFormsVisible formInstances size %{public}zu size", formInstances.size());
114     if (!data.WriteInt32(formInstances.size())) {
115         HILOG_ERROR("write int32 failed");
116         return false;
117     }
118     for (auto &parcelable: formInstances) {
119         if (!data.WriteParcelable(&parcelable)) {
120             HILOG_ERROR("write parcelable failed");
121             return false;
122         }
123     }
124     int32_t error = SendTransactCmd(
125         IJsFormStateObserver::Message::FORM_STATE_OBSERVER_NOTIFY_WHETHER_FORMS_VISIBLE,
126         data,
127         reply,
128         option);
129     if (error != ERR_OK) {
130         HILOG_ERROR("fail get form instances by filter:%{public}d", error);
131     }
132 
133     return error;
134 }
135 
SendTransactCmd(IJsFormStateObserver::Message code,MessageParcel & data,MessageParcel & reply,MessageOption & option)136 int JsFormStateObserverProxy::SendTransactCmd(IJsFormStateObserver::Message code,
137     MessageParcel &data, MessageParcel &reply, MessageOption &option)
138 {
139     sptr<IRemoteObject> remote = Remote();
140     if (!remote) {
141         HILOG_ERROR("get remoteObject failed, cmd:%{public}d", code);
142         return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED;
143     }
144     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
145     if (result != ERR_OK) {
146         HILOG_ERROR("SendRequest failed:%{public}d, cmd:%{public}d", result, code);
147         return ERR_APPEXECFWK_FORM_SEND_FMS_MSG;
148     }
149     return ERR_OK;
150 }
151 
OnFormClickEvent(const std::string & bundleName,const std::string & callType,const AppExecFwk::RunningFormInfo & runningFormInfo)152 int32_t JsFormStateObserverProxy::OnFormClickEvent(
153     const std::string &bundleName, const std::string &callType, const AppExecFwk::RunningFormInfo &runningFormInfo)
154 {
155     MessageParcel data;
156     MessageParcel reply;
157     MessageOption option(MessageOption::TF_ASYNC);
158 
159     if (!data.WriteInterfaceToken(AbilityRuntime::IJsFormStateObserver::GetDescriptor())) {
160         HILOG_ERROR("write interface token failed");
161         return ERR_APPEXECFWK_PARCEL_ERROR;
162     }
163 
164     if (!data.WriteString(bundleName)) {
165         HILOG_ERROR("write bundleName failed");
166         return ERR_APPEXECFWK_PARCEL_ERROR;
167     }
168 
169     if (!data.WriteString(callType)) {
170         HILOG_ERROR("fail write call type");
171         return ERR_APPEXECFWK_PARCEL_ERROR;
172     }
173 
174     if (!data.WriteParcelable(&runningFormInfo)) {
175         HILOG_ERROR("write runningFormInfo failed");
176         return ERR_APPEXECFWK_PARCEL_ERROR;
177     }
178 
179     sptr<IRemoteObject> remote = Remote();
180     if (!remote) {
181         HILOG_ERROR("get remoteObject failed");
182         return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED;
183     }
184     int32_t error = remote->SendRequest(
185         static_cast<uint32_t>(IJsFormStateObserver::Message::FORM_STATE_OBSERVER_ON_FORM_CLICK), data, reply, option);
186     if (error != ERR_OK) {
187         HILOG_ERROR("SendRequest:%{public}d failed", error);
188     }
189     return error;
190 }
191 } // namespace AbilityRuntime
192 } // namespace OHOS
193