1 /* 2 * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_OHOS_FORM_HOST_CLIENT_H 17 #define FOUNDATION_APPEXECFWK_OHOS_FORM_HOST_CLIENT_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <vector> 23 #include <set> 24 #include "form_callback_interface.h" 25 #include "form_host_stub.h" 26 #include "form_state_info.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 /** 31 * @class FormHostClient 32 * The service of the form host. 33 */ 34 class FormHostClient : public FormHostStub { 35 public: 36 FormHostClient(); 37 virtual ~FormHostClient(); 38 39 /** 40 * @brief Get FormHostClient instance. 41 * 42 * @return FormHostClient instance. 43 */ 44 static sptr<FormHostClient> GetInstance(); 45 46 /** 47 * @brief Add form. 48 * 49 * @param formCallback the host's form callback. 50 * @param formId The Id of the form. 51 * @return none. 52 */ 53 void AddForm(std::shared_ptr<FormCallbackInterface> formCallback, const int64_t formId); 54 55 /** 56 * @brief Remove form. 57 * 58 * @param formCallback the host's form callback. 59 * @param formId The Id of the form. 60 * @return none. 61 */ 62 void RemoveForm(std::shared_ptr<FormCallbackInterface> formCallback, const int64_t formId); 63 64 /** 65 * @brief Check whether the form exist in the formhosts. 66 * 67 * @param formId The Id of the form. 68 * @return Returns true if contains form; returns false otherwise. 69 */ 70 bool ContainsForm(int64_t formId); 71 72 /** 73 * @brief Add form state. 74 * 75 * @param formStateCallback the host's form state callback. 76 * @param want the want of acquiring form state. 77 * @return Returns true if contains form; returns false otherwise. 78 */ 79 bool AddFormState(std::shared_ptr<FormStateCallbackInterface> &formStateCallback, const AAFwk::Want &want); 80 81 using UninstallCallback = void (*)(const std::vector<int64_t> &formIds); 82 /** 83 * @brief register form uninstall function. 84 * 85 * @param callback the form uninstall callback. 86 * @return Returns true if contains form; returns false otherwise. 87 */ 88 bool RegisterUninstallCallback(UninstallCallback callback); 89 90 /** 91 * @brief Request to give back a form. 92 * 93 * @param formJsInfo Form js info. 94 * @return none. 95 */ 96 virtual void OnAcquired(const FormJsInfo &formJsInfo); 97 98 /** 99 * @brief Update form. 100 * 101 * @param formJsInfo Form js info. 102 * @return none. 103 */ 104 virtual void OnUpdate(const FormJsInfo &formJsInfo); 105 106 /** 107 * @brief UnInstall the forms. 108 * 109 * @param formIds The Id of the forms. 110 * @return none. 111 */ 112 virtual void OnUninstall(const std::vector<int64_t> &formIds); 113 114 /** 115 * @brief Acquire the form state 116 * @param state The form state. 117 */ 118 virtual void OnAcquireState(FormState state, const AAFwk::Want &want); 119 120 private: 121 static std::mutex instanceMutex_; 122 static sptr<FormHostClient> instance_; 123 mutable std::mutex callbackMutex_; 124 mutable std::mutex formStateCallbackMutex_; 125 mutable std::mutex uninstallCallbackMutex_; 126 std::map<int64_t, std::set<std::shared_ptr<FormCallbackInterface>>> formCallbackMap_; 127 std::map<std::string, std::set<std::shared_ptr<FormStateCallbackInterface>>> formStateCallbackMap_; 128 UninstallCallback uninstallCallback_ = nullptr; 129 130 DISALLOW_COPY_AND_MOVE(FormHostClient); 131 }; 132 } // namespace AppExecFwk 133 } // namespace OHOS 134 #endif // FOUNDATION_APPEXECFWK_OHOS_FORM_HOST_CLIENT_H 135