1 /* 2 * Copyright (c) 2023 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_renderer_group.h" 29 #include "js_runtime.h" 30 #include "want.h" 31 32 namespace OHOS { 33 namespace AppExecFwk { 34 namespace FormRender { 35 using Want = AAFwk::Want; 36 enum class TaskState { 37 NO_RUNNING = 0, 38 RUNNING = 0, 39 BLOCK, 40 }; 41 42 class ThreadState { 43 public: 44 explicit ThreadState(int32_t maxState); 45 void ResetState(); 46 void NextState(); 47 int32_t GetCurrentState(); 48 bool IsMaxState(); 49 50 private: 51 int32_t state_ = 0; 52 int32_t maxState_; 53 }; 54 55 class FormRenderRecord : public std::enable_shared_from_this<FormRenderRecord> { 56 public: 57 /** 58 * @brief Create a FormRenderRecord. 59 * @param bundleName The bundleName of form bundle. 60 * @param uid The uid of form bundle.(userId + bundleName) 61 * @return Returns FormRenderRecord instance. 62 */ 63 static std::shared_ptr<FormRenderRecord> Create( 64 const std::string &bundleName, const std::string &uid, bool needMonitored = true); 65 66 FormRenderRecord(const std::string &bundleName, const std::string &uid); 67 68 ~FormRenderRecord(); 69 70 /** 71 * @brief When the host exits, clean up related resources. 72 * @param hostRemoteObj host token. 73 * @return Returns TRUE: FormRenderRecord is empty, FALSE: FormRenderRecord is not empty. 74 */ 75 bool HandleHostDied(const sptr<IRemoteObject> hostRemoteObj); 76 77 /** 78 * @brief When add a new form, the corresponding FormRenderRecord needs to be updated. 79 * @param formJsInfo formJsInfo. 80 * @param want want. 81 * @param hostRemoteObj host token. 82 * @return Returns ERR_OK on success, others on failure. 83 */ 84 int32_t UpdateRenderRecord(const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> hostRemoteObj); 85 86 /** 87 * @brief When all forms of an bundle are deleted, the corresponding FormRenderRecord-record needs to be removed 88 * @param formId formId. 89 * @param hostRemoteObj host token. 90 * @return Returns ERR_OK on success, others on failure. 91 */ 92 void DeleteRenderRecord(int64_t formId, const std::string &compId, const sptr<IRemoteObject> hostRemoteObj, bool &isRenderGroupEmpty); 93 94 int32_t HandleOnUnlock(); 95 96 int32_t OnUnlock(); 97 98 int32_t ReloadFormRecord(const std::vector<FormJsInfo> &&formJsInfos, const Want &want); 99 100 int32_t HandleReloadFormRecord(const std::vector<FormJsInfo> &&formJsInfos, const Want &want); 101 102 /** 103 * @brief Get the uid of bundle. 104 * @return Returns the uid. 105 */ 106 std::string GetUid() const; 107 108 bool IsEmpty(); 109 110 void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 111 112 void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 113 114 void MarkThreadAlive(); 115 116 void ReleaseRenderer(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty); 117 118 void Release(); 119 120 private: 121 class RemoteObjHash { 122 public: operator()123 size_t operator() (const sptr<IRemoteObject> remoteObj) const 124 { 125 return reinterpret_cast<size_t>(remoteObj.GetRefPtr()); 126 } 127 }; 128 using IRemoteObjectSet = std::unordered_set<sptr<IRemoteObject>, RemoteObjHash>; 129 130 bool CreateEventHandler(const std::string &bundleName, bool needMonitored = true); 131 132 bool CreateRuntime(const FormJsInfo &formJsInfo); 133 134 std::shared_ptr<AbilityRuntime::Context> GetContext(const FormJsInfo &formJsInfo, const Want &want); 135 136 std::shared_ptr<AbilityRuntime::Context> CreateContext(const FormJsInfo &formJsInfo, const Want &want); 137 138 std::shared_ptr<Ace::FormRendererGroup> GetFormRendererGroup(const FormJsInfo &formJsInfo, 139 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 140 141 std::shared_ptr<Ace::FormRendererGroup> CreateFormRendererGroupLock(const FormJsInfo &formJsInfo, 142 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 143 144 void HandleUpdateInJsThread(const FormJsInfo &formJsInfo, const Want &want); 145 146 bool HandleDeleteInJsThread(int64_t formId, const std::string &compId); 147 148 void HandleDestroyInJsThread(); 149 150 bool HandleReleaseRendererInJsThread(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty); 151 152 std::string GenerateContextKey(const FormJsInfo &formJsInfo); 153 154 void ReleaseHapFileHandle(); 155 156 void HandleUpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 157 158 void AddWatchDogThreadMonitor(); 159 160 void Timer(); 161 162 bool BeforeHandleUpdateForm(const FormJsInfo &formJsInfo); 163 164 void HandleUpdateForm(const FormJsInfo &formJsInfo, const Want &want); 165 166 void HandleUpdateDynamicForm(const FormJsInfo &formJsInfo, const Want &want); 167 168 void HandleUpdateStaticForm(const FormJsInfo &formJsInfo, const Want &want); 169 170 void AddRenderer(const FormJsInfo &formJsInfo, const Want &want); 171 172 void UpdateRenderer(const FormJsInfo &formJsInfo); 173 174 TaskState RunTask(); 175 176 void HandleReleaseInJsThread(); 177 178 bool CheckEventHandler(bool createThead = true, bool needMonitored = false); 179 180 void AddStaticFormRequest(const FormJsInfo &formJsInfo, const Want &want); 181 182 void AddStaticFormRequest(int64_t formId, const Ace::FormRequest &formRequest); 183 184 void DeleteStaticFormRequest(int64_t formId, const std::string &compId); 185 186 void UpdateStaticFormRequestReleaseState( 187 int64_t formId, const std::string &compId, bool hasRelease); 188 189 void ReAddAllStaticForms(); 190 191 void ReAddStaticForms(const std::vector<FormJsInfo> &formJsInfos); 192 193 std::string bundleName_; 194 std::string uid_; 195 std::shared_ptr<EventRunner> eventRunner_; 196 std::shared_ptr<EventHandler> eventHandler_; 197 std::mutex eventHandlerMutex_; 198 std::shared_ptr<AbilityRuntime::Runtime> runtime_; 199 200 // <formId, hostRemoteObj> 201 std::mutex hostsMapMutex_; 202 std::unordered_map<int64_t, IRemoteObjectSet> hostsMapForFormId_; 203 // <moduleName, Context> 204 std::mutex contextsMapMutex_; 205 std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::Context>> contextsMapForModuleName_; 206 // <formId, formRendererGroup> 207 std::mutex formRendererGroupMutex_; 208 std::unordered_map<int64_t, std::shared_ptr<Ace::FormRendererGroup>> formRendererGroupMap_; 209 std::mutex staticFormRequestsMutex_; 210 std::unordered_map<int64_t, std::unordered_map<std::string, Ace::FormRequest>> staticFormRequests_; 211 std::shared_ptr<OHOS::AppExecFwk::Configuration> configuration_; 212 213 std::string hapPath_; 214 std::mutex watchDogMutex_; 215 bool threadIsAlive_ = true; 216 std::atomic_bool hasMonitor_ = false; 217 std::shared_ptr<ThreadState> threadState_; 218 }; 219 } // namespace FormRender 220 } // namespace AppExecFwk 221 } // namespace OHOS 222 #endif // OHOS_FORM_FWK_FORM_RENDER_RECORD_H 223