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 void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 89 90 private: 91 class RemoteObjHash { 92 public: operator()93 size_t operator() (const sptr<IRemoteObject> remoteObj) const 94 { 95 return reinterpret_cast<size_t>(remoteObj.GetRefPtr()); 96 } 97 }; 98 using IRemoteObjectSet = std::unordered_set<sptr<IRemoteObject>, RemoteObjHash>; 99 100 bool CreateEventHandler(const std::string &bundleName); 101 102 bool CreateRuntime(const FormJsInfo &formJsInfo); 103 104 std::shared_ptr<AbilityRuntime::Context> GetContext(const FormJsInfo &formJsInfo, const Want &want); 105 106 std::shared_ptr<AbilityRuntime::Context> CreateContext(const FormJsInfo &formJsInfo, const Want &want); 107 108 std::shared_ptr<Ace::FormRendererGroup> GetFormRendererGroup(const FormJsInfo &formJsInfo, 109 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 110 111 std::shared_ptr<Ace::FormRendererGroup> CreateFormRendererGroupLock(const FormJsInfo &formJsInfo, 112 const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime); 113 114 void HandleUpdateInJsThread(const FormJsInfo &formJsInfo, const Want &want); 115 116 bool HandleDeleteInJsThread(int64_t formId, const std::string &compId); 117 118 void HandleDestroyInJsThread(); 119 120 std::string GenerateContextKey(const FormJsInfo &formJsInfo); 121 122 void HandleUpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config); 123 124 std::string bundleName_; 125 std::string uid_; 126 std::shared_ptr<EventRunner> eventRunner_; 127 std::shared_ptr<EventHandler> eventHandler_; 128 std::shared_ptr<AbilityRuntime::Runtime> runtime_; 129 130 // <formId, hostRemoteObj> 131 std::mutex hostsMapMutex_; 132 std::unordered_map<int64_t, IRemoteObjectSet> hostsMapForFormId_; 133 // <moduleName, Context> 134 std::mutex contextsMapMutex_; 135 std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::Context>> contextsMapForModuleName_; 136 // <formId, formRendererGroup> 137 std::mutex formRendererGroupMutex_; 138 std::unordered_map<int64_t, std::shared_ptr<Ace::FormRendererGroup>> formRendererGroupMap_; 139 std::shared_ptr<OHOS::AppExecFwk::Configuration> configuration_; 140 }; 141 } // namespace FormRender 142 } // namespace AppExecFwk 143 } // namespace OHOS 144 #endif // OHOS_FORM_FWK_FORM_RENDER_RECORD_H 145