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 Remove shareForm callback. 171 * @param requestCode The request code of this share form. 172 */ 173 void RemoveShareFormCallback(int64_t requestCode); 174 175 /** 176 * @brief Remove AcquireData callback. 177 * @param requestCode The request code of this acquire form data. 178 */ 179 void RemoveAcquireDataCallback(int64_t requestCode); 180 181 /** 182 * @brief Update form. 183 * @param formJsInfo Form js info. 184 */ 185 void UpdateForm(const FormJsInfo &formJsInfo); 186 187 /** 188 * @brief Recycle dynamic ArkTS form. 189 * @param formId The id of form to be recycled. 190 */ 191 void OnRecycleForm(const int64_t &formId) override; 192 193 /** 194 * @brief enable form style 195 * @param formIds The Id list of the forms. 196 * @param enable True is enableform, false is disableform. 197 */ 198 void OnEnableForm(const std::vector<int64_t> &formIds, const bool enable) override; 199 private: 200 static std::mutex instanceMutex_; 201 static sptr<FormHostClient> instance_; 202 mutable std::mutex callbackMutex_; 203 mutable std::mutex formStateCallbackMutex_; 204 mutable std::mutex uninstallCallbackMutex_; 205 mutable std::mutex shareFormCallbackMutex_; 206 mutable std::mutex AcquireDataCallbackMutex_; 207 std::map<int64_t, std::set<std::shared_ptr<FormCallbackInterface>>> formCallbackMap_; 208 std::map<int64_t, std::shared_ptr<ShareFormCallBack>> shareFormCallbackMap_; 209 std::map<int64_t, std::shared_ptr<FormDataCallbackInterface>> acquireDataCallbackMap_; 210 std::map<std::string, std::set<std::shared_ptr<FormStateCallbackInterface>>> formStateCallbackMap_; 211 UninstallCallback uninstallCallback_ = nullptr; 212 std::unordered_set<int64_t> etsFormIds_; 213 214 DISALLOW_COPY_AND_MOVE(FormHostClient); 215 }; 216 } // namespace AppExecFwk 217 } // namespace OHOS 218 #endif // OHOS_FORM_FWK_FORM_HOST_CLIENT_H 219