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 #ifndef OHOS_FORM_FWK_FORM_HOST_CALLER_H
17 #define OHOS_FORM_FWK_FORM_HOST_CALLER_H
18 
19 #include <iremote_object.h>
20 
21 #include "form_js_info.h"
22 #include "want.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 /**
27  * @class FormHostCaller
28  * The caller of the form host.
29  */
30 class FormHostCaller {
31 public:
FormHostCaller(const FormJsInfo & formJsInfo,const sptr<IRemoteObject> & callerToken)32     FormHostCaller(const FormJsInfo &formJsInfo, const sptr<IRemoteObject> &callerToken)
33         : formJsInfo_(formJsInfo), callerToken_(callerToken) {}
34     ~FormHostCaller() = default;
35 
36     /**
37      * @brief Update form with formId.
38      * @param formId Indicates the Id of the form to update.
39      * @param formProviderData Indicates the form provider data.
40      */
41     void UpdateForm(int64_t formId, const FormProviderData &formProviderData);
42 
43     /**
44     * @brief Notify the form service that the form user's lifecycle is updated.
45     *
46     * This should be called when form user request form.
47     *
48     * @param formId Indicates the unique id of form.
49     * @param callerToken Indicates the callback remote object of specified form user.
50     * @param want Indicates information passed to supplier.
51     * @return Returns ERR_OK on success, others on failure.
52     */
53     ErrCode RequestForm(int64_t formId, const sptr<IRemoteObject> &callerToken, const AAFwk::Want &want);
54 
55     /**
56      * @brief Process js message event.
57      * @param formId Indicates the unique id of form.
58      * @param want Indicates information passed to supplier.
59      * @param callerToken Caller ability token.
60      * @return Returns ERR_OK on success, others on failure.
61      */
62     ErrCode MessageEvent(int64_t formId, const AAFwk::Want &want, const sptr<IRemoteObject> &callerToken);
63 
64     /**
65      * @brief Add deathRecipient object to formHostClient_.
66      * @param deathRecipient DeathRecipient object.
67      */
68     void AddDeathRecipient(sptr<IRemoteObject::DeathRecipient> deathRecipient);
69 
70     /**
71      * @brief Is the same caller token.
72      * @param callerToken Caller ability token.
73      */
74     bool IsSameToken(const sptr<IRemoteObject> &callerToken) const;
75 private:
76     FormJsInfo formJsInfo_;
77     sptr<IRemoteObject> callerToken_;
78 };
79 
80 /**
81  * @class FormHostCallerRecipient
82  * FormHostCallerRecipient notices IRemoteBroker died.
83  */
84 class FormHostCallerRecipient : public IRemoteObject::DeathRecipient {
85 public:
86     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
FormHostCallerRecipient(RemoteDiedHandler handler)87     FormHostCallerRecipient(RemoteDiedHandler handler) : handler_(handler) {}
88     ~FormHostCallerRecipient() = default;
89 
90     /**
91      * @brief handle remote object died event.
92      * @param remote remote object.
93      */
94     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
95 private:
96     RemoteDiedHandler handler_;
97 };
98 } // namespace AppExecFwk
99 } // namespace OHOS
100 #endif // OHOS_FORM_FWK_FORM_HOST_CALLER_H
101