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_PROVIDER_CALLER_H
17 #define OHOS_FORM_FWK_FORM_PROVIDER_CALLER_H
18 
19 #include <iremote_object.h>
20 
21 #include "form_js_info.h"
22 #include "form_provider_info.h"
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 /**
28  * @class FormProviderCaller
29  * The caller of the form provider.
30  */
31 class FormProviderCaller {
32 public:
FormProviderCaller(const sptr<IRemoteObject> & callerToken)33     explicit FormProviderCaller(const sptr<IRemoteObject> &callerToken) : callerToken_(callerToken) {}
34     ~FormProviderCaller() = default;
35 
36     /**
37      * @brief Is the same caller token.
38      * @param callerToken Caller ability token.
39      */
40     bool IsSameToken(const sptr<IRemoteObject> &callerToken) const;
41 
42     /**
43      * @brief Add form js info to formJsInfoMap_.
44      * @param formJsInfo The form js info.
45      */
46     void AddForm(const FormJsInfo &formJsInfo);
47 
48     /**
49      * @brief Delete form js info form formJsInfoMap_.
50      * @param formJsInfo The form js info.
51      */
52     void DeleteFormJsInfo(const FormJsInfo &formJsInfo);
53 
54     /**
55      * @brief Whether has the specific form.
56      * @param formId The form ID.
57      * @return Returns true if has the specific form; returns false otherwise.
58      */
59     bool HasForm(int64_t formId);
60 
61     /**
62      * @brief Delete the Specific form.
63      * @param formId The form ID.
64      */
65     void DeleteForm(int64_t formId);
66 
67     /**
68      * @brief Whether the form is empty.
69      * @return Returns true if the form is empty; returns false otherwise.
70      */
71     bool IsFormEmpty();
72 
73     /**
74      * @brief Callback acquire.
75      * @param providerFormInfo Form binding data.
76      * @param want Indicates the Want structure containing form info.
77      * @param token Indicates the provider client token.
78      * @return Returns ERR_OK on success, others on failure.
79      */
80     int32_t OnAcquire(const FormProviderInfo &formProviderInfo, const AAFwk::Want &want,
81         const sptr<IRemoteObject> &token);
82 
83     /**
84      * @brief Update form with formId.
85      * @param formId Indicates the Id of the form to update.
86      * @param formProviderData Indicates the form provider data.
87      */
88     void UpdateForm(int64_t formId, const FormProviderData &formProviderData);
89 
90     /**
91      * @brief Add deathRecipient object to formHostClient_.
92      * @param deathRecipient DeathRecipient object.
93      */
94     void AddDeathRecipient(sptr<IRemoteObject::DeathRecipient> deathRecipient);
95 private:
96     bool GetFormJsInfo(int64_t formId, FormJsInfo &formJsInfo);
97     int32_t OnAcquire(const FormJsInfo &formJsInfo, const sptr<IRemoteObject> &token);
98     void UpdateForm(const FormJsInfo &formJsInfo);
99 
100     mutable std::mutex formJsInfoMutex_;
101     std::map<int64_t, FormJsInfo> formJsInfoMap_;
102     std::vector<FormJsInfo> formJsInfos_;
103     sptr<IRemoteObject> callerToken_ = nullptr;
104 };
105 
106 /**
107  * @class FormProviderCallerRecipient
108  * FormProviderCallerRecipient notices IRemoteBroker died.
109  */
110 class FormProviderCallerRecipient : public IRemoteObject::DeathRecipient {
111 public:
112     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
FormProviderCallerRecipient(RemoteDiedHandler handler)113     FormProviderCallerRecipient(RemoteDiedHandler handler) : handler_(handler) {}
114     ~FormProviderCallerRecipient() = default;
115 
116     /**
117      * @brief handle remote object died event.
118      * @param remote remote object.
119      */
120     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
121 private:
122     RemoteDiedHandler handler_;
123 };
124 } // namespace AppExecFwk
125 } // namespace OHOS
126 #endif // OHOS_FORM_FWK_FORM_PROVIDER_CALLER_H