• 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 #include "form_provider_stub.h"
16 #include "appexecfwk_errors.h"
17 #include "app_scheduler_interface.h"
18 #include "errors.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
FormProviderStub()27 FormProviderStub::FormProviderStub()
28 {
29     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_ACQUIRE_PROVIDER_FORM_INFO)] =
30         &FormProviderStub::HandleAcquireProviderFormInfo;
31     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_PROVIDER_NOTIFY_FORM_DELETE)] =
32         &FormProviderStub::HandleNotifyFormDelete;
33     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_PROVIDER_NOTIFY_FORMS_DELETE)] =
34         &FormProviderStub::HandleNotifyFormsDelete;
35     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_PROVIDER_NOTIFY_FORM_UPDATE)] =
36         &FormProviderStub::HandleNotifyFormUpdate;
37     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_PROVIDER_EVENT_NOTIFY)] =
38         &FormProviderStub::HandleEventNotify;
39     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_PROVIDER_NOTIFY_TEMP_FORM_CAST)] =
40         &FormProviderStub::HandleNotifyFormCastTempForm;
41     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_PROVIDER_EVENT_MESSAGE)] =
42         &FormProviderStub::HandleFireFormEvent;
43     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_PROVIDER_NOTIFY_STATE_ACQUIRE)] =
44         &FormProviderStub::HandleAcquireState;
45     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_ACQUIRE_PROVIDER_SHARE_FOMR_INFO)] =
46         &FormProviderStub::HandleAcquireShareFormData;
47     memberFuncMap_[static_cast<uint32_t>(IFormProvider::Message::FORM_ACQUIRE_PROVIDER_FOMR_DATA)] =
48         &FormProviderStub::HandleAcquireFormData;
49 }
50 
~FormProviderStub()51 FormProviderStub::~FormProviderStub()
52 {
53     memberFuncMap_.clear();
54 }
55 /**
56  * @brief handle remote request.
57  * @param data input param.
58  * @param reply output param.
59  * @param option message option.
60  * @return Returns ERR_OK on success, others on failure.
61  */
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)62 int FormProviderStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
63 {
64     HILOG_INFO("FormProviderStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
65     std::u16string descriptor = FormProviderStub::GetDescriptor();
66     std::u16string remoteDescriptor = data.ReadInterfaceToken();
67     if (descriptor != remoteDescriptor) {
68         HILOG_ERROR("%{public}s failed, local descriptor is not equal to remote", __func__);
69         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
70     }
71 
72     auto itFunc = memberFuncMap_.find(code);
73     if (itFunc != memberFuncMap_.end()) {
74         auto memberFunc = itFunc->second;
75         if (memberFunc != nullptr) {
76             return (this->*memberFunc)(data, reply);
77         }
78     }
79 
80     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81 }
82 
HandleAcquireProviderFormInfo(MessageParcel & data,MessageParcel & reply)83 int FormProviderStub::HandleAcquireProviderFormInfo(MessageParcel &data, MessageParcel &reply)
84 {
85     std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
86     if (!formJsInfo) {
87         HILOG_ERROR("%{public}s, failed to ReadParcelable<formJsInfo>", __func__);
88         return ERR_APPEXECFWK_PARCEL_ERROR;
89     }
90     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
91     if (!want) {
92         HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
93         return ERR_APPEXECFWK_PARCEL_ERROR;
94     }
95 
96     sptr<IRemoteObject> client = data.ReadRemoteObject();
97     if (client == nullptr) {
98         HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
99         return ERR_APPEXECFWK_PARCEL_ERROR;
100     }
101 
102     int32_t result = AcquireProviderFormInfo(*formJsInfo, *want, client);
103     reply.WriteInt32(result);
104     return result;
105 }
106 /**
107  * @brief handle NotifyFormDelete message.
108  * @param data input param.
109  * @param reply output param.
110  * @return Returns ERR_OK on success, others on failure.
111  */
HandleNotifyFormDelete(MessageParcel & data,MessageParcel & reply)112 int FormProviderStub::HandleNotifyFormDelete(MessageParcel &data, MessageParcel &reply)
113 {
114     int64_t formId = data.ReadInt64();
115     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
116     if (!want) {
117         HILOG_ERROR("%{public}s fail, ReadParcelable<FormReqInfo> failed", __func__);
118         return ERR_APPEXECFWK_PARCEL_ERROR;
119     }
120 
121     sptr<IRemoteObject> client = data.ReadRemoteObject();
122     if (client == nullptr) {
123         HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
124         return ERR_APPEXECFWK_PARCEL_ERROR;
125     }
126 
127     int32_t result = NotifyFormDelete(formId, *want, client);
128     reply.WriteInt32(result);
129     return result;
130 }
131 /**
132  * @brief handle NotifyFormsDelete message.
133  * @param data input param.
134  * @param reply output param.
135  * @return Returns ERR_OK on success, others on failure.
136  */
HandleNotifyFormsDelete(MessageParcel & data,MessageParcel & reply)137 int FormProviderStub::HandleNotifyFormsDelete(MessageParcel &data, MessageParcel &reply)
138 {
139     std::vector<int64_t> formIds;
140     bool ret = data.ReadInt64Vector(&formIds);
141     if (ret) {
142         std::unique_ptr<Want> want(data.ReadParcelable<Want>());
143         if (!want) {
144             HILOG_ERROR("%{public}s fail, ReadParcelable<FormReqInfo> failed", __func__);
145             return ERR_APPEXECFWK_PARCEL_ERROR;
146         }
147 
148         sptr<IRemoteObject> client = data.ReadRemoteObject();
149         if (client == nullptr) {
150             HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
151             return ERR_APPEXECFWK_PARCEL_ERROR;
152         }
153 
154         int32_t result = NotifyFormsDelete(formIds, *want, client);
155         reply.WriteInt32(result);
156         return result;
157     }
158 
159     return ERR_INVALID_DATA;
160 }
161 /**
162  * @brief handle NotifyFormUpdate message.
163  * @param data input param.
164  * @param reply output param.
165  * @return Returns ERR_OK on success, others on failure.
166  */
HandleNotifyFormUpdate(MessageParcel & data,MessageParcel & reply)167 int FormProviderStub::HandleNotifyFormUpdate(MessageParcel &data, MessageParcel &reply)
168 {
169     int64_t formId = data.ReadInt64();
170 
171     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
172     if (!want) {
173         HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
174         return ERR_APPEXECFWK_PARCEL_ERROR;
175     }
176 
177     sptr<IRemoteObject> client = data.ReadRemoteObject();
178     if (client == nullptr) {
179         HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
180         return ERR_APPEXECFWK_PARCEL_ERROR;
181     }
182 
183     int32_t result = NotifyFormUpdate(formId, *want, client);
184     reply.WriteInt32(result);
185     return result;
186 }
187 
188 /**
189  * @brief handle EventNotify message.
190  * @param data input param.
191  * @param reply output param.
192  * @return Returns ERR_OK on success, others on failure.
193  */
HandleEventNotify(MessageParcel & data,MessageParcel & reply)194 int FormProviderStub::HandleEventNotify(MessageParcel &data, MessageParcel &reply)
195 {
196     std::vector<int64_t> formIds;
197     bool ret = data.ReadInt64Vector(&formIds);
198     if (ret) {
199         int32_t formVisibleType = data.ReadInt32();
200 
201         std::unique_ptr<Want> want(data.ReadParcelable<Want>());
202         if (!want) {
203             HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
204             return ERR_APPEXECFWK_PARCEL_ERROR;
205         }
206 
207         sptr<IRemoteObject> client = data.ReadRemoteObject();
208         if (client == nullptr) {
209             HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
210             return ERR_APPEXECFWK_PARCEL_ERROR;
211         }
212 
213         int32_t result = EventNotify(formIds, formVisibleType, *want, client);
214         reply.WriteInt32(result);
215         return result;
216     }
217 
218     return ERR_INVALID_DATA;
219 }
220 
221 /**
222  * @brief handle NotifyFormCastTempForm message.
223  * @param data input param.
224  * @param reply output param.
225  * @return Returns ERR_OK on success, others on failure.
226  */
HandleNotifyFormCastTempForm(MessageParcel & data,MessageParcel & reply)227 int FormProviderStub::HandleNotifyFormCastTempForm(MessageParcel &data, MessageParcel &reply)
228 {
229     int64_t formId = data.ReadInt64();
230 
231     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
232     if (!want) {
233         HILOG_ERROR("%{public}s fail, ReadParcelable<Want> failed", __func__);
234         return ERR_APPEXECFWK_PARCEL_ERROR;
235     }
236 
237     sptr<IRemoteObject> client = data.ReadRemoteObject();
238     if (client == nullptr) {
239         HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
240         return ERR_APPEXECFWK_PARCEL_ERROR;
241     }
242 
243     int32_t result = NotifyFormCastTempForm(formId, *want, client);
244     reply.WriteInt32(result);
245     return result;
246 }
247 /**
248  * @brief handle NotifyFormCastTempForm message.
249  * @param data input param.
250  * @param reply output param.
251  * @return Returns ERR_OK on success, others on failure.
252  */
HandleFireFormEvent(MessageParcel & data,MessageParcel & reply)253 int FormProviderStub::HandleFireFormEvent(MessageParcel &data, MessageParcel &reply)
254 {
255     int64_t formId = data.ReadInt64();
256     std::string message = data.ReadString();
257     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
258     if (!want) {
259         HILOG_ERROR("%{public}s, failed to get want.", __func__);
260         return ERR_APPEXECFWK_PARCEL_ERROR;
261     }
262 
263     sptr<IRemoteObject> client = data.ReadRemoteObject();
264     if (client == nullptr) {
265         HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
266         return ERR_APPEXECFWK_PARCEL_ERROR;
267     }
268 
269     int32_t result = FireFormEvent(formId, message, *want, client);
270     reply.WriteInt32(result);
271     return result;
272 }
273 /**
274  * @brief handle AcquireState message.
275  * @param data input param.
276  * @param reply output param.
277  * @return Returns ERR_OK on success, others on failure.
278  */
HandleAcquireState(MessageParcel & data,MessageParcel & reply)279 int FormProviderStub::HandleAcquireState(MessageParcel &data, MessageParcel &reply)
280 {
281     std::unique_ptr<Want> wantArg(data.ReadParcelable<Want>());
282     if (!wantArg) {
283         HILOG_ERROR("%{public}s fail, ReadParcelable<Want> failed", __func__);
284         return ERR_APPEXECFWK_PARCEL_ERROR;
285     }
286     std::string provider = data.ReadString();
287     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
288     if (!want) {
289         HILOG_ERROR("%{public}s fail, ReadParcelable<Want> failed", __func__);
290         return ERR_APPEXECFWK_PARCEL_ERROR;
291     }
292     sptr<IRemoteObject> client = data.ReadRemoteObject();
293     if (client == nullptr) {
294         HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
295         return ERR_APPEXECFWK_PARCEL_ERROR;
296     }
297     int32_t result = AcquireState(*wantArg, provider, *want, client);
298     reply.WriteInt32(result);
299     return result;
300 }
301 
HandleAcquireShareFormData(MessageParcel & data,MessageParcel & reply)302 int32_t FormProviderStub::HandleAcquireShareFormData(MessageParcel &data, MessageParcel &reply)
303 {
304     auto formId = data.ReadInt64();
305     auto remoteDeviceId = data.ReadString();
306     auto remoteObj = data.ReadRemoteObject();
307     if (remoteObj == nullptr) {
308         HILOG_ERROR("failed to get remote object.");
309         return ERR_APPEXECFWK_PARCEL_ERROR;
310     }
311 
312     auto requestCode = data.ReadInt64();
313     auto result = AcquireShareFormData(formId, remoteDeviceId, remoteObj, requestCode);
314     if (!reply.WriteInt32(result)) {
315         HILOG_ERROR("failed to Write result.");
316         return ERR_APPEXECFWK_PARCEL_ERROR;
317     }
318 
319     return ERR_OK;
320 }
321 
HandleAcquireFormData(MessageParcel & data,MessageParcel & reply)322 int32_t FormProviderStub::HandleAcquireFormData(MessageParcel &data, MessageParcel &reply)
323 {
324     auto formId = data.ReadInt64();
325     auto remoteObj = data.ReadRemoteObject();
326     if (remoteObj == nullptr) {
327         HILOG_ERROR("failed to get remote object.");
328         return ERR_APPEXECFWK_PARCEL_ERROR;
329     }
330 
331     auto requestCode = data.ReadInt64();
332     auto result = AcquireFormData(formId, remoteObj, requestCode);
333     if (!reply.WriteInt32(result)) {
334         HILOG_ERROR("failed to Write result.");
335         return ERR_APPEXECFWK_PARCEL_ERROR;
336     }
337 
338     return ERR_OK;
339 }
340 }  // namespace AppExecFwk
341 }  // namespace OHOS