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_CLIENT_H
17 #define OHOS_FORM_FWK_FORM_HOST_CLIENT_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 #include <set>
24 #include <unordered_set>
25 #include "form_callback_interface.h"
26 #include "form_host_stub.h"
27 #include "form_state_info.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 /**
32  * @class FormHostClient
33  * The service of the form host.
34  */
35 class FormHostClient : public FormHostStub {
36 public:
37     FormHostClient();
38     virtual ~FormHostClient();
39 
40     /**
41      * @brief Get FormHostClient instance.
42      *
43      * @return FormHostClient instance.
44      */
45     static sptr<FormHostClient> GetInstance();
46 
47     /**
48      * @brief Add form.
49      *
50      * @param formCallback the host's form callback.
51      * @param formJsInfo The info of the form.
52      * @return none.
53      */
54     void AddForm(std::shared_ptr<FormCallbackInterface> formCallback, const FormJsInfo &formJsInfo);
55 
56     /**
57      * @brief Remove form.
58      *
59      * @param formCallback the host's form callback.
60      * @param formId The Id of the form.
61      * @return none.
62      */
63     void RemoveForm(std::shared_ptr<FormCallbackInterface> formCallback, const int64_t formId);
64 
65     /**
66      * @brief Check whether the form exist in the formhosts.
67      *
68      * @param formId The Id of the form.
69      * @return Returns true if contains form; returns false otherwise.
70      */
71     bool ContainsForm(int64_t formId);
72 
73     /**
74      * @brief Add form state.
75      *
76      * @param formStateCallback the host's form state callback.
77      * @param want the want of acquiring form state.
78      * @return Returns true if contains form; returns false otherwise.
79      */
80     bool AddFormState(const std::shared_ptr<FormStateCallbackInterface> &formStateCallback, const AAFwk::Want &want);
81 
82     /**
83      * @brief Remove form state.
84      *
85      * @param want the want of acquiring form state.
86      */
87     void RemoveFormState(const AAFwk::Want &want);
88 
89     using UninstallCallback = void (*)(const std::vector<int64_t> &formIds);
90     /**
91      * @brief register form uninstall function.
92      *
93      * @param callback the form uninstall callback.
94      * @return Returns true if contains form; returns false otherwise.
95      */
96     bool RegisterUninstallCallback(UninstallCallback callback);
97 
98     /**
99      * @brief Request to give back a form.
100      *
101      * @param formJsInfo Form js info.
102      * @param token Provider client token.
103      * @return none.
104      */
105     void OnAcquired(const FormJsInfo &formJsInfo, const sptr<IRemoteObject> &token) override;
106 
107      /**
108      * @brief Update form.
109      *
110      * @param formJsInfo Form js info.
111      * @return none.
112      */
113     void OnUpdate(const FormJsInfo &formJsInfo) override;
114 
115     /**
116      * @brief UnInstall the forms.
117      *
118      * @param formIds The Id of the forms.
119      * @return none.
120      */
121     void OnUninstall(const std::vector<int64_t> &formIds) override;
122 
123     /**
124      * @brief Acquire the form state
125      * @param state The form state.
126      */
127     void OnAcquireState(FormState state, const AAFwk::Want &want) override;
128 
129     /**
130      * @brief Add shareForm callback.
131      *
132      * @param shareFormCallback The host's share form callback.
133      * @param requestCode The request code of this share form.
134      * @return Returns true if contains form; returns false otherwise.
135      */
136     bool AddShareFormCallback(const std::shared_ptr<ShareFormCallBack> &shareFormCallback, int64_t requestCode);
137 
138     /**
139      * @brief Add Acquire form data callback.
140      *
141      * @param acquireFormDataTask The host's acquire form callback.
142      * @param requestCode The request code of this acquire form.
143      * @return Returns true if contains form; returns false otherwise.
144      */
145     bool AddAcqiureFormDataCallback(const std::shared_ptr<FormDataCallbackInterface> &acquireFormDataTask,
146         int64_t requestCode);
147 
148     /**
149      * @brief Responsive form sharing.
150      * @param requestCode The request code of this share form.
151      * @param result Share form result.
152      */
153     void OnShareFormResponse(int64_t requestCode, int32_t result) override;
154 
155     /**
156      * @brief Responsive form acquiring form data.
157      * @param requestCode The request code of this acquire form.
158      */
159     void OnAcquireDataResponse(const AAFwk::WantParams &wantParams, int64_t requestCode) override;
160 
161     /**
162      * @brief Return error to host.
163      *
164      * @param errorCode Indicates error-code of the form.
165      * @param errorMsg Indicates error-message of the form.
166      */
167     void OnError(int32_t errorCode, const std::string &errorMsg) override;
168 
169     /**
170      * @brief Return error to host for forms.
171      *
172      * @param errorCode Indicates error-code of the form.
173      * @param errorMsg Indicates error-message of the form.
174      * @param formIds Indicates ids of the form.
175      */
176     void OnError(int32_t errorCode, const std::string &errorMsg, std::vector<int64_t> &formIds) override;
177 
178     /**
179      * @brief Remove shareForm callback.
180      * @param requestCode The request code of this share form.
181      */
182     void RemoveShareFormCallback(int64_t requestCode);
183 
184     /**
185      * @brief Remove AcquireData callback.
186      * @param requestCode The request code of this acquire form data.
187      */
188     void RemoveAcquireDataCallback(int64_t requestCode);
189 
190     /**
191      * @brief Update form.
192      * @param formJsInfo Form js info.
193      */
194     void UpdateForm(const FormJsInfo &formJsInfo);
195 
196     /**
197      * @brief Recycle dynamic ArkTS form.
198      * @param formId The id of form to be recycled.
199      */
200     void OnRecycleForm(const int64_t &formId) override;
201 
202     /**
203      * @brief enable form style
204      * @param formIds The Id list of the forms.
205      * @param enable True is enableform, false is disableform.
206      */
207     void OnEnableForm(const std::vector<int64_t> &formIds, const bool enable) override;
208 private:
209     static std::mutex instanceMutex_;
210     static sptr<FormHostClient> instance_;
211     mutable std::mutex callbackMutex_;
212     mutable std::mutex formStateCallbackMutex_;
213     mutable std::mutex uninstallCallbackMutex_;
214     mutable std::mutex shareFormCallbackMutex_;
215     mutable std::mutex AcquireDataCallbackMutex_;
216     std::map<int64_t, std::set<std::shared_ptr<FormCallbackInterface>>> formCallbackMap_;
217     std::map<int64_t, std::shared_ptr<ShareFormCallBack>> shareFormCallbackMap_;
218     std::map<int64_t, std::shared_ptr<FormDataCallbackInterface>> acquireDataCallbackMap_;
219     std::map<std::string, std::set<std::shared_ptr<FormStateCallbackInterface>>> formStateCallbackMap_;
220     UninstallCallback uninstallCallback_ = nullptr;
221     std::unordered_set<int64_t> etsFormIds_;
222 
223     DISALLOW_COPY_AND_MOVE(FormHostClient);
224 };
225 }  // namespace AppExecFwk
226 }  // namespace OHOS
227 #endif  // OHOS_FORM_FWK_FORM_HOST_CLIENT_H
228