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 class FormRenderRecord : public std::enable_shared_from_this<FormRenderRecord> { 37 public: 38 /** 39 * @brief Create a FormRenderRecord. 40 * @param bundleName The bundleName of form bundle. 41 * @param uid The uid of form bundle.(userId + bundleName) 42 * @return Returns FormRenderRecord instance. 43 */ 44 static std::shared_ptr<FormRenderRecord> Create(const std::string &bundleName, const std::string &uid); 45 46 FormRenderRecord(const std::string &bundleName, const std::string &uid); 47 48 ~FormRenderRecord(); 49 50 /** 51 * @brief When the host exits, clean up related resources. 52 * @param hostRemoteObj host token. 53 * @return Returns TRUE: FormRenderRecord is empty, FALSE: FormRenderRecord is not empty. 54 */ 55 bool HandleHostDied(const sptr<IRemoteObject> hostRemoteObj); 56 57 /** 58 * @brief When add a new form, the corresponding FormRenderRecord needs to be updated. 59 * @param formJsInfo formJsInfo. 60 * @param want want. 61 * @param hostRemoteObj host token. 62 * @return Returns ERR_OK on success, others on failure. 63 */ 64 int32_t UpdateRenderRecord(const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> hostRemoteObj); 65 66 /** 67 * @brief When all forms of an bundle are deleted, the corresponding FormRenderRecord-record needs to be removed 68 * @param formId formId. 69 * @param hostRemoteObj host token. 70 * @return Returns ERR_OK on success, others on failure. 71 */ 72 void DeleteRenderRecord(int64_t formId, const std::string &compId, const sptr<IRemoteObject> hostRemoteObj, bool &isRenderGroupEmpty); 73 74 int32_t ReloadFormRecord(const std::vector<int64_t> &&formIds, const Want &want); 75 76 int32_t HandleReloadFormRecord(const std::vector<int64_t> &&formIds, const Want &want); 77 78 /** 79 * @brief Get the uid of bundle. 80 * @return Returns the uid. 81 */ 82 std::string GetUid() const; 83 84 bool IsEmpty(); 85 86 void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 87 88 private: 89 class RemoteObjHash { 90 public: operator()91 size_t operator() (const sptr<IRemoteObject> remoteObj) const 92 { 93 return reinterpret_cast<size_t>(remoteObj.GetRefPtr()); 94 } 95 }; 96 using IRemoteObjectSet = std::unordered_set<sptr<IRemoteObject>, RemoteObjHash>; 97 98 bool CreateEventHandler(const std::string &bundleName); 99 100 bool CreateRuntime(const FormJsInfo &formJsInfo); 101 102 std::shared_ptr<AbilityRuntime::Context> GetContext(const FormJsInfo &formJsInfo, const Want &want); 103 104 std::shared_ptr<AbilityRuntime::Context> CreateContext(const FormJsInfo &formJsInfo, const Want &want); 105 106 std::shared_ptr<Ace::FormRendererGroup> GetFormRendererGroup(const FormJsInfo &formJsInfo, 107 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 108 109 std::shared_ptr<Ace::FormRendererGroup> CreateFormRendererGroupLock(const FormJsInfo &formJsInfo, 110 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 111 112 void HandleUpdateInJsThread(const FormJsInfo &formJsInfo, const Want &want); 113 114 bool HandleDeleteInJsThread(int64_t formId, const std::string &compId); 115 116 void HandleDestroyInJsThread(); 117 118 std::string GenerateContextKey(const FormJsInfo &formJsInfo); 119 120 void HandleUpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 121 122 std::string bundleName_; 123 std::string uid_; 124 std::shared_ptr<EventRunner> eventRunner_; 125 std::shared_ptr<EventHandler> eventHandler_; 126 std::shared_ptr<AbilityRuntime::Runtime> runtime_; 127 128 // <formId, hostRemoteObj> 129 std::mutex hostsMapMutex_; 130 std::unordered_map<int64_t, IRemoteObjectSet> hostsMapForFormId_; 131 // <moduleName, Context> 132 std::mutex contextsMapMutex_; 133 std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::Context>> contextsMapForModuleName_; 134 // <formId, formRendererGroup> 135 std::mutex formRendererGroupMutex_; 136 std::unordered_map<int64_t, std::shared_ptr<Ace::FormRendererGroup>> formRendererGroupMap_; 137 }; 138 } // namespace FormRender 139 } // namespace AppExecFwk 140 } // namespace OHOS 141 #endif // OHOS_FORM_FWK_FORM_RENDER_RECORD_H 142