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 "form_callback_interface.h" 24 #include "form_host_stub.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 /** 29 * @class FormHostClient 30 * The service of the form host. 31 */ 32 class HostForms; 33 class FormHostClient : public FormHostStub { 34 public: 35 FormHostClient(); 36 virtual ~FormHostClient(); 37 38 /** 39 * @brief Get FormHostClient instance. 40 * 41 * @return FormHostClient instance. 42 */ 43 static sptr<FormHostClient> GetInstance(); 44 45 /** 46 * @brief Add form. 47 * 48 * @param formCallback the host's form callback. 49 * @param formId The Id of the form. 50 * @return none. 51 */ 52 void AddForm(std::shared_ptr<FormCallbackInterface> formCallback, const int64_t formId); 53 54 /** 55 * @brief Remove form. 56 * 57 * @param formCallback the host's form callback. 58 * @param formId The Id of the form. 59 * @return none. 60 */ 61 void RemoveForm(std::shared_ptr<FormCallbackInterface> formCallback, const int64_t formId); 62 63 /** 64 * @brief Check whether the form exist in the formhosts. 65 * 66 * @param formId The Id of the form. 67 * @return Returns true if contains form; returns false otherwise. 68 */ 69 bool ContainsForm(int64_t formId); 70 71 /** 72 * @brief Request to give back a form. 73 * 74 * @param formJsInfo Form js info. 75 * @return none. 76 */ 77 virtual void OnAcquired(const FormJsInfo &formJsInfo); 78 79 /** 80 * @brief Update form. 81 * 82 * @param formJsInfo Form js info. 83 * @return none. 84 */ 85 virtual void OnUpdate(const FormJsInfo &formJsInfo); 86 87 /** 88 * @brief UnInstall the forms. 89 * 90 * @param formIds The Id of the forms. 91 * @return none. 92 */ 93 virtual void OnUninstall(const std::vector<int64_t> &formIds); 94 95 private: 96 static sptr<FormHostClient> instance_; 97 static std::mutex instanceMutex_; 98 mutable std::mutex lockMutex_; 99 std::vector<int64_t> keyVector_; 100 std::map<int64_t, std::shared_ptr<FormCallbackInterface>> recordCallback_; 101 std::map<int64_t, HostForms> recordHostForms_; 102 int32_t key_ = 0; 103 104 private: 105 /** 106 * @brief Find callback by formId. 107 * 108 * @param formId The Id of the form. 109 * @return target callback 110 */ 111 std::shared_ptr<FormCallbackInterface> FindTargetCallback(int64_t formId); 112 113 /** 114 * @brief Find Key By form callback. 115 * 116 * @param formCallback The form callback. 117 * @return callback's key 118 */ 119 int32_t FindKeyByCallback(std::shared_ptr<FormCallbackInterface> formCallback); 120 121 /** 122 * @brief Compare callback. 123 * 124 * @param formCallback1 The form callback1. 125 * @param formCallback2 The callback to be compared with form callback1. 126 * @return Returns true if the two callback are equal to each other, returns false otherwise. 127 */ 128 bool Compare(std::shared_ptr<FormCallbackInterface> formCallback1, std::shared_ptr<FormCallbackInterface> formCallback2); 129 130 DISALLOW_COPY_AND_MOVE(FormHostClient); 131 }; 132 133 class HostForms { 134 public: 135 /** 136 * @brief Add form by formId. 137 * 138 * @param formId The Id of the form. 139 */ AddForm(const int64_t formId)140 void AddForm(const int64_t formId) 141 { 142 std::map<int64_t, bool>::iterator it = forms_.find(formId); 143 if (it != forms_.end()) { 144 return; 145 } 146 forms_.insert(std::pair<int64_t, bool>(formId, true)); 147 } 148 149 /** 150 * @brief Delete form by formId. 151 * 152 * @param formId The Id of the form. 153 */ DelForm(const int64_t formId)154 void DelForm(const int64_t formId) 155 { 156 forms_.erase(formId); 157 } 158 159 /** 160 * @brief Check whether the form is empty. 161 */ IsEmpty()162 bool IsEmpty() 163 { 164 return forms_.empty(); 165 } 166 167 /** 168 * @brief Check whether the form exist in the forms. 169 * 170 * @param formId The Id of the form. 171 * @return Returns true if contains form; returns false otherwise. 172 */ Contains(const int64_t formId)173 bool Contains(const int64_t formId) 174 { 175 std::map<int64_t, bool>::iterator it = forms_.find(formId); 176 return (it == forms_.end()) ? false : true; 177 } 178 179 private: 180 std::map<int64_t, bool> forms_; 181 }; 182 183 } // namespace AppExecFwk 184 } // namespace OHOS 185 #endif // FOUNDATION_APPEXECFWK_OHOS_FORM_HOST_CLIENT_H 186