1 /* 2 * Copyright (c) 2023-2024 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_RENDER_RECORD_H 17 #define OHOS_FORM_FWK_FORM_RENDER_RECORD_H 18 19 #include <memory> 20 #include <unordered_map> 21 #include <unordered_set> 22 23 #include "configuration.h" 24 #include "context_impl.h" 25 #include "event_handler.h" 26 #include "form_js_info.h" 27 #include "form_mgr_errors.h" 28 #include "form_supply_proxy.h" 29 #include "form_renderer_group.h" 30 #include "js_runtime.h" 31 #include "want.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 namespace FormRender { 36 using Want = AAFwk::Want; 37 enum class TaskState { 38 NO_RUNNING = 0, 39 RUNNING = 0, 40 BLOCK, 41 }; 42 43 class ThreadState { 44 public: 45 explicit ThreadState(int32_t maxState); 46 void ResetState(); 47 void NextState(); 48 int32_t GetCurrentState(); 49 bool IsMaxState(); 50 51 private: 52 int32_t state_ = 0; 53 int32_t maxState_; 54 }; 55 56 class HandlerDumper : public AppExecFwk::Dumper { 57 public: 58 void Dump(const std::string &message) override; 59 std::string GetTag() override; 60 std::string GetDumpInfo(); 61 private: 62 std::string dumpInfo_; 63 }; 64 65 struct FormLocationInfo { 66 std::string formName; 67 uint32_t formLocation; 68 }; 69 70 class FormRenderRecord : public std::enable_shared_from_this<FormRenderRecord> { 71 public: 72 /** 73 * @brief Create a FormRenderRecord. 74 * @param bundleName The bundleName of form bundle. 75 * @param uid The uid of form bundle.(userId + bundleName) 76 * @return Returns FormRenderRecord instance. 77 */ 78 static std::shared_ptr<FormRenderRecord> Create(const std::string &bundleName, const std::string &uid, 79 bool needMonitored = true, sptr<IFormSupply> formSupplyClient = nullptr); 80 81 FormRenderRecord(const std::string &bundleName, const std::string &uid, sptr<IFormSupply> formSupplyClient); 82 83 ~FormRenderRecord(); 84 85 /** 86 * @brief When the host exits, clean up related resources. 87 * @param hostRemoteObj host token. 88 * @return Returns TRUE: FormRenderRecord is empty, FALSE: FormRenderRecord is not empty. 89 */ 90 bool HandleHostDied(const sptr<IRemoteObject> hostRemoteObj); 91 92 /** 93 * @brief When add a new form, the corresponding FormRenderRecord needs to be updated. 94 * @param formJsInfo formJsInfo. 95 * @param want want. 96 * @param hostRemoteObj host token. 97 * @return Returns ERR_OK on success, others on failure. 98 */ 99 int32_t UpdateRenderRecord(const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> hostRemoteObj); 100 101 /** 102 * @brief When all forms of an bundle are deleted, the corresponding FormRenderRecord-record needs to be removed 103 * @param formId formId. 104 * @param hostRemoteObj host token. 105 * @return Returns ERR_OK on success, others on failure. 106 */ 107 void DeleteRenderRecord(int64_t formId, const std::string &compId, const sptr<IRemoteObject> hostRemoteObj, 108 bool &isRenderGroupEmpty); 109 110 int32_t HandleOnUnlock(); 111 112 int32_t OnUnlock(); 113 114 int32_t SetVisibleChange(const int64_t &formId, bool isVisible); 115 116 int32_t HandleSetVisibleChange(const int64_t &formId, bool isVisible); 117 118 int32_t ReloadFormRecord(const std::vector<FormJsInfo> &&formJsInfos, const Want &want); 119 120 int32_t HandleReloadFormRecord(const std::vector<FormJsInfo> &&formJsInfos, const Want &want); 121 122 /** 123 * @brief Get the uid of bundle. 124 * @return Returns the uid. 125 */ 126 std::string GetUid() const; 127 128 bool IsEmpty(); 129 130 bool HasRenderFormTask(); 131 132 void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config, 133 const sptr<IFormSupply> &formSupplyClient); 134 135 void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 136 137 void MarkThreadAlive(); 138 139 void ReleaseRenderer(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty); 140 141 void Release(); 142 143 void FormRenderGC(); 144 145 int32_t RecycleForm(const int64_t &formId, std::string &statusData); 146 147 int32_t RecoverForm(const FormJsInfo &formJsInfo, const std::string &statusData, 148 const bool &isRecoverFormToHandleClickEvent); 149 150 size_t FormCount(); 151 152 void UpdateFormSizeOfGroups(const int64_t &formId, float width, float height, float borderWidth); 153 bool IsAllFormsInvisible(); 154 private: 155 class RemoteObjHash { 156 public: operator()157 size_t operator() (const sptr<IRemoteObject> remoteObj) const 158 { 159 return reinterpret_cast<size_t>(remoteObj.GetRefPtr()); 160 } 161 }; 162 using IRemoteObjectSet = std::unordered_set<sptr<IRemoteObject>, RemoteObjHash>; 163 164 bool CreateEventHandler(const std::string &bundleName, bool needMonitored = true); 165 166 bool CreateRuntime(const FormJsInfo &formJsInfo); 167 168 bool UpdateRuntime(const FormJsInfo &formJsInfo); 169 170 bool SetPkgContextInfoMap(const FormJsInfo &formJsInfo, AbilityRuntime::Runtime::Options &options); 171 172 std::shared_ptr<AbilityRuntime::Context> GetContext(const FormJsInfo &formJsInfo, const Want &want); 173 174 std::shared_ptr<AbilityRuntime::Context> CreateContext(const FormJsInfo &formJsInfo, const Want &want); 175 176 std::shared_ptr<Ace::FormRendererGroup> GetFormRendererGroup(const FormJsInfo &formJsInfo, 177 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 178 179 std::shared_ptr<Ace::FormRendererGroup> CreateFormRendererGroupLock(const FormJsInfo &formJsInfo, 180 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 181 182 void UpdateFormRequest(const FormJsInfo &formJsInfo, const Want &want); 183 184 int32_t HandleUpdateInJsThread(const FormJsInfo &formJsInfo, const Want &want); 185 186 bool HandleDeleteInJsThread(int64_t formId, const std::string &compId); 187 188 void HandleDestroyInJsThread(); 189 190 bool HandleReleaseRendererInJsThread(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty); 191 192 void DeleteRendererGroup(int64_t formId); 193 194 void HandleDeleteRendererGroup(int64_t formId); 195 196 std::string GenerateContextKey(const FormJsInfo &formJsInfo); 197 198 void ReleaseHapFileHandle(); 199 200 void HandleUpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 201 202 void AddWatchDogThreadMonitor(); 203 204 void RemoveWatchDogThreadMonitor(); 205 206 void OnRenderingBlock(const std::string &bundleName); 207 208 void OnNotifyRefreshForm(const int64_t &formId); 209 210 void Timer(); 211 212 bool BeforeHandleUpdateForm(const FormJsInfo &formJsInfo); 213 214 int32_t HandleUpdateForm(const FormJsInfo &formJsInfo, const Want &want); 215 216 void MergeFormData(Ace::FormRequest &formRequest, const FormJsInfo &formJsInfo); 217 218 void AddRenderer(const FormJsInfo &formJsInfo, const Want &want); 219 220 void UpdateRenderer(const FormJsInfo &formJsInfo); 221 222 TaskState RunTask(); 223 224 void DumpEventHandler(); 225 226 void HandleReleaseInJsThread(); 227 228 bool CheckEventHandler(bool createThead = true, bool needMonitored = false); 229 230 void AddFormRequest(const FormJsInfo &formJsInfo, const Want &want); 231 232 void AddFormRequest(int64_t formId, Ace::FormRequest &formRequest); 233 234 void DeleteFormRequest(int64_t formId, const std::string &compId); 235 236 void UpdateFormRequestReleaseState( 237 int64_t formId, const std::string &compId, bool hasRelease); 238 239 void RecoverFormsByConfigUpdate(std::vector<int64_t> &formIds, const sptr<IFormSupply> &formSupplyClient); 240 241 void ReAddAllRecycledForms(const sptr<IFormSupply> &formSupplyClient); 242 243 int32_t ReAddRecycledForms(const std::vector<FormJsInfo> &formJsInfos); 244 245 int32_t HandleRecycleForm(const int64_t &formId, std::string &statusData); 246 247 void HandleRecoverForm(const FormJsInfo &formJsInfo, const std::string &statusData, 248 const bool &isRecoverFormToHandleClickEvent); 249 250 void HandleFormRenderGC(); 251 252 bool GetAndDeleteRecycledCompIds(const int64_t &formId, 253 std::vector<std::string> &orderedCompIds, std::string ¤tCompId); 254 255 bool RecoverFormRequestsInGroup(const FormJsInfo &formJsInfo, const std::string &statusData, 256 const bool &isHandleClickEvent, std::unordered_map<std::string, Ace::FormRequest> &recordFormRequests); 257 bool RecoverRenderer(const std::vector<Ace::FormRequest> &groupRequests, const size_t ¤tRequestIndex); 258 259 bool ReAddIfHapPathChanged(const std::vector<FormJsInfo> &formJsInfos); 260 261 void UpdateAllFormRequest(const std::vector<FormJsInfo> &formJsInfos, bool hasRelease); 262 263 void HandleReleaseAllRendererInJsThread(); 264 265 void UpdateGroupRequestsWhenRecover(const int64_t &formId, const FormJsInfo &formJsInfo, 266 const std::vector<std::string> &orderedCompIds, const std::string ¤tCompId, 267 const std::string &statusData, const bool &isHandleClickEvent, size_t ¤tRequestIndex, 268 std::vector<Ace::FormRequest> &groupRequests, bool ¤tRequestFound, 269 const std::unordered_map<std::string, Ace::FormRequest> &recordFormRequests); 270 271 void MergeMap(std::map<std::string, sptr<FormAshmem>> &dst, 272 const std::map<std::string, sptr<FormAshmem>> &src); 273 274 void MarkRenderFormTaskDone(int32_t renderType); 275 276 bool CheckManagerDelegateValid(const FormJsInfo &formJsInfo, const Want &want); 277 278 void SetFormSupplyClient(const sptr<IFormSupply> &formSupplyClient); 279 280 sptr<IFormSupply> GetFormSupplyClient(); 281 282 std::shared_ptr<EventHandler> GetEventHandler(); 283 284 int32_t AddHostByFormId(int64_t formId, const sptr<IRemoteObject> hostRemoteObj); 285 286 void DeleteHostRemoteObjByFormId(int64_t formId, const sptr<IRemoteObject> hostRemoteObj); 287 288 void DeleteRecycledFormCompIds(int64_t formId); 289 290 void InsertRecycledFormCompIds(int64_t formId, const std::pair<std::vector<std::string>, std::string> &compIds); 291 292 void DeleteHostByFormId(int64_t formId, const sptr<IRemoteObject> hostRemoteObj); 293 294 void RemoveHostByFormId(int64_t formId); 295 296 bool IsFormContextExist(const FormJsInfo &formJsInfo); 297 298 bool GetFormRequestByFormId(int64_t formId, std::unordered_map<std::string, Ace::FormRequest> &formRequests); 299 300 void SetEventHandlerNeedResetFlag(bool needReset); 301 302 bool GetEventHandlerNeedReset(); 303 304 void DeleteAndUpdateRecycledFormCompIds(int64_t formId, 305 const std::pair<std::vector<std::string>, std::string>& compIds, const bool needUpdate); 306 307 void RegisterUncatchableErrorHandler(); 308 void OnJsError(napi_value value); 309 std::string GetNativeStrFromJsTaggedObj(napi_value obj, const char* key); 310 311 void RecordFormVisibility(int64_t formId, bool isVisible); 312 313 void RecordFormLocation(int64_t formId, const FormLocationInfo &formLocation); 314 void DeleteFormLocation(int64_t formId); 315 void ParseFormLocationMap(std::vector<std::string> &formName, std::vector<uint32_t> &formLocation); 316 void RuntimeMemoryMonitor(); 317 void PostReAddRecycledForms(const FormJsInfo &formJsInfo, const Want &want); 318 319 pid_t jsThreadId_ = 0; 320 pid_t processId_ = 0; 321 322 std::string bundleName_; 323 std::string uid_; 324 std::shared_ptr<EventRunner> eventRunner_; 325 std::shared_ptr<EventHandler> eventHandler_; 326 bool eventHandleNeedReset = false; 327 std::shared_mutex eventHandlerReset_; 328 std::recursive_mutex eventHandlerMutex_; 329 std::shared_ptr<AbilityRuntime::Runtime> runtime_; 330 331 // <formId, hostRemoteObj> 332 std::mutex hostsMapMutex_; 333 std::unordered_map<int64_t, IRemoteObjectSet> hostsMapForFormId_; 334 // <moduleName, Context> 335 std::mutex contextsMapMutex_; 336 std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::Context>> contextsMapForModuleName_; 337 // <formId, formRendererGroup> 338 std::mutex formRendererGroupMutex_; 339 std::unordered_map<int64_t, std::shared_ptr<Ace::FormRendererGroup>> formRendererGroupMap_; 340 // <formId, <compId, formRequest>> 341 std::mutex formRequestsMutex_; 342 std::unordered_map<int64_t, std::unordered_map<std::string, Ace::FormRequest>> formRequests_; 343 std::shared_ptr<OHOS::AppExecFwk::Configuration> configuration_; 344 // <formId, <orderedCompIds, currentCompId>> 345 std::mutex recycledFormCompIdsMutex_; 346 std::unordered_map<int64_t, std::pair<std::vector<std::string>, std::string>> recycledFormCompIds_; 347 348 std::string hapPath_; 349 std::mutex watchDogMutex_; 350 bool threadIsAlive_ = true; 351 std::atomic_bool hasMonitor_ = false; 352 std::unique_ptr<ThreadState> threadState_; 353 std::mutex formSupplyMutex_; 354 sptr<IFormSupply> formSupplyClient_; 355 std::atomic<int> renderFormTasksNum = 0; 356 std::mutex visibilityMapMutex_; 357 std::unordered_map<int64_t, bool> visibilityMap_; 358 std::mutex formLocationMutex_; 359 std::unordered_map<int64_t, FormLocationInfo> formLocationMap_; 360 }; 361 } // namespace FormRender 362 } // namespace AppExecFwk 363 } // namespace OHOS 364 #endif // OHOS_FORM_FWK_FORM_RENDER_RECORD_H 365