1 /*
2  * Copyright (c) 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_caller.h"
17 
18 #include "fms_log_wrapper.h"
19 #include "form_constants.h"
20 #include "form_host_client.h"
21 #include "form_mgr_errors.h"
22 #include "form_provider_interface.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
OnRemoteDied(const wptr<IRemoteObject> & remote)26 void FormHostCallerRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
27 {
28     HILOG_DEBUG("On remote died");
29     if (handler_ != nullptr) {
30         handler_(remote);
31     }
32 }
33 
UpdateForm(int64_t formId,const FormProviderData & formProviderData)34 void FormHostCaller::UpdateForm(int64_t formId, const FormProviderData &formProviderData)
35 {
36     HILOG_DEBUG("call");
37     FormJsInfo formJsInfo = formJsInfo_;
38     formJsInfo.formData = formProviderData.GetDataString();
39     formJsInfo.formProviderData = formProviderData;
40     if (!formJsInfo.ConvertRawImageData()) {
41         HILOG_ERROR("convert raw image data failed");
42     }
43     FormHostClient::GetInstance()->UpdateForm(formJsInfo);
44 }
45 
RequestForm(int64_t formId,const sptr<IRemoteObject> & callerToken,const AAFwk::Want & want)46 ErrCode FormHostCaller::RequestForm(int64_t formId, const sptr<IRemoteObject> &callerToken, const AAFwk::Want &want)
47 {
48     sptr<IFormProvider> providerToken = iface_cast<IFormProvider>(callerToken_);
49     if (providerToken == nullptr) {
50         HILOG_ERROR("null callerToken");
51         return ERR_APPEXECFWK_FORM_COMMON_CODE;
52     }
53     return providerToken->NotifyFormUpdate(formId, want, callerToken);
54 }
55 
MessageEvent(int64_t formId,const AAFwk::Want & want,const sptr<IRemoteObject> & callerToken)56 ErrCode FormHostCaller::MessageEvent(int64_t formId, const AAFwk::Want &want, const sptr<IRemoteObject> &callerToken)
57 {
58     std::string message = want.GetStringParam(Constants::PARAM_MESSAGE_KEY);
59     sptr<IFormProvider> providerToken = iface_cast<IFormProvider>(callerToken_);
60     if (providerToken == nullptr) {
61         HILOG_ERROR("null callerToken");
62         return ERR_APPEXECFWK_FORM_COMMON_CODE;
63     }
64     return providerToken->FireFormEvent(formId, message, want, callerToken);
65 }
66 
IsSameToken(const sptr<IRemoteObject> & callerToken) const67 bool FormHostCaller::IsSameToken(const sptr<IRemoteObject> &callerToken) const
68 {
69     return (callerToken == callerToken_);
70 }
71 
AddDeathRecipient(sptr<IRemoteObject::DeathRecipient> deathRecipient)72 void FormHostCaller::AddDeathRecipient(sptr<IRemoteObject::DeathRecipient> deathRecipient)
73 {
74     HILOG_DEBUG("call");
75     callerToken_->AddDeathRecipient(deathRecipient);
76 }
77 } // namespace AppExecFwk
78 } // namespace OHOS
79