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 
16 #ifndef OHOS_FORM_FWK_FORM_HOST_RECORD_H
17 #define OHOS_FORM_FWK_FORM_HOST_RECORD_H
18 
19 #include <unordered_map>
20 #include <vector>
21 #include "fms_log_wrapper.h"
22 #include "form_host_callback.h"
23 #include "form_item_info.h"
24 #include "form_record.h"
25 #include "iremote_object.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 /**
30  * @class FormHostRecord
31  * Form host data.
32  */
33 class FormHostRecord {
34 public:
35     /**
36      * @brief Create form host record.
37      * @param callback remote object.
38      * @param callingUid Calling uid.
39      */
40     static FormHostRecord CreateRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callback, int callingUid);
41     /**
42      * @brief Add form id.
43      * @param formId The Id of the form.
44      */
45     void AddForm(int64_t formId);
46     /**
47      * @brief Delete form id.
48      * @param formId The Id of the form.
49      */
50     void DelForm(int64_t formId);
51     /**
52      * @brief forms_ is empty or not.
53      * @return forms_ is empty or not.
54      */
55     bool IsEmpty() const;
56     /**
57      * @brief formId is in forms_ or not.
58      * @param formId The Id of the form.
59      * @return formId is in forms_ or not.
60      */
61     bool Contains(int64_t formId) const;
62 
63     /**
64      * @brief Set refresh enable flag.
65      * @param formId The Id of the form.
66      * @param flag True for enable, false for disable.
67      */
68     void SetEnableRefresh(int64_t formId, bool flag);
69 
70     /**
71      * @brief Refresh enable or not.
72      * @param formId The Id of the form.
73      * @return true on enable, false on disable.
74      */
75     bool IsEnableRefresh(int64_t formId) const;
76 
77     /**
78      * @brief Set Update enable flag.
79      * @param formId The Id of the form.
80      * @param enable True for enable, false for disable.
81      */
82     void SetEnableUpdate(int64_t formId, bool enable);
83 
84     /**
85      * @brief update enable or not.
86      * @param formId The Id of the form.
87      * @return true on enable, false on disable.
88      */
89     bool IsEnableUpdate(int64_t formId) const;
90 
91     /**
92      * @brief Set need refresh enable flag.
93      * @param formId The Id of the form.
94      * @param flag True for enable, false for disable.
95      */
96     void SetNeedRefresh(int64_t formId, bool flag);
97     /**
98      * @brief Need Refresh enable or not.
99      * @param formId The Id of the form.
100      * @return true on enable, false on disable.
101      */
102     bool IsNeedRefresh(int64_t formId) const;
103 
104     /**
105      * @brief Send form data to form host.
106      * @param id The Id of the form.
107      * @param record Form record.
108      */
109     void OnAcquire(int64_t id, const FormRecord &record);
110 
111     /**
112      * @brief Update form data to form host.
113      * @param id The Id of the form.
114      * @param record Form record.
115      */
116     void OnUpdate(int64_t id, const FormRecord &record);
117     /**
118      * Send form uninstall message to form host.
119      *
120      * @param formIds the uninstalled form id list.
121      */
122     void OnFormUninstalled(std::vector<int64_t> &formIds);
123     /**
124      * Send form state message to form host.
125      *
126      * @param state The form state.
127      * @param want The want of onAcquireFormState.
128      */
129     void OnAcquireState(AppExecFwk::FormState state, const AAFwk::Want &want);
130 
131     /**
132      * Send form data to form host.
133      *
134      * @param wantParams Indicates the data information acquired by the form.
135      * @param requestCode Indicates the requested id.
136      */
137     void OnAcquireFormData(const AAFwk::WantParams &wantParams, int64_t requestCode);
138 
139     /**
140      * @brief Release resource.
141      */
142     void CleanResource();
143     /**
144      * @brief Get caller Uid.
145      * @return the caller Uid.
146      */
GetCallerUid()147     int GetCallerUid() const
148     {
149         return callerUid_;
150     }
151     /**
152      * @brief Get client stub.
153      * @return client stub.
154      */
155     sptr<IRemoteObject> GetFormHostClient() const;
156     /**
157      * @brief Get death recipient.
158      * @return death recipient.
159      */
160     sptr<IRemoteObject::DeathRecipient> GetDeathRecipient() const;
161     /**
162      * @brief Set value of caller uid.
163      * @param callerUid Caller uid.
164      */
165     void SetCallerUid(const int callerUid);
166     /**
167      * @brief Set value of client stub.
168      * @param formHostClient remote object.
169      */
170     void SetFormHostClient(const sptr<IRemoteObject> &formHostClient);
171     /**
172      * @brief Set value of formHostCallback_.
173      * @param formHostCallback Form host callback object.
174      */
175     void SetCallback(const std::shared_ptr<FormHostCallback> &formHostCallback);
176     /**
177      * @brief Set value of deathRecipient_.
178      * @param formHostCallback DeathRecipient object.
179      */
180     void SetDeathRecipient(const sptr<IRemoteObject::DeathRecipient> &deathRecipient);
181     /**
182      * @brief Add deathRecipient object to formHostClient_.
183      * @param deathRecipient DeathRecipient object.
184      */
185     void AddDeathRecipient(const sptr<IRemoteObject::DeathRecipient>& deathRecipient);
186     /**
187      * @brief Get hostBundleName_.
188      * @return hostBundleName_.
189      */
190     std::string GetHostBundleName() const;
191     /**
192      * @brief Set hostBundleName_.
193      * @param hostBundleName Host bundle name.
194      */
195     void SetHostBundleName(const std::string &hostBundleName);
196 
197     /**
198      * @brief get forms count.
199      * @return the number of host form.
200      */
201     int32_t GetFormsCount() const;
202 
203     /**
204      * @brief Send recycle form message to form host.
205      * @param formIds The Id list of forms.
206      * @param want The want of forms to be recycled.
207      */
208     void OnRecycleForms(const std::vector<int64_t> &formIds, const AAFwk::Want &want) const;
209 
210     /**
211      * @brief Enable form or disable form.
212      * @param formIds The Id list of forms.
213      * @param enable True is enable form, false is disable form.
214      */
215     void OnEnableForms(const std::vector<int64_t> &formIds, const bool enable);
216 private:
217     int callerUid_ = 0;
218     sptr<IRemoteObject> formHostClient_ = nullptr;
219     std::shared_ptr<FormHostCallback> formHostCallback_ = nullptr;
220     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
221     std::unordered_map<int64_t, bool> forms_;
222     std::unordered_map<int64_t, bool> enableUpdateMap_;
223     std::unordered_map<int64_t, bool> needRefresh_;
224     std::string hostBundleName_ = "";
225 
226     /**
227      * @class ClientDeathRecipient
228      * notices IRemoteBroker died.
229      */
230     class ClientDeathRecipient : public IRemoteObject::DeathRecipient {
231     public:
232         /**
233          * @brief Constructor
234          */
235         ClientDeathRecipient() = default;
236         ~ClientDeathRecipient() = default;
237         /**
238          * @brief handle remote object died event.
239          * @param remote remote object.
240          */
241         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
242     };
243 };
244 }  // namespace AppExecFwk
245 }  // namespace OHOS
246 
247 #endif // OHOS_FORM_FWK_FORM_HOST_RECORD_H
248