1 /* 2 * Copyright (c) 2025 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_SERVICE_MGR_H 17 #define OHOS_FORM_FWK_FORM_RENDER_SERVICE_MGR_H 18 19 #include "form_render_stub.h" 20 21 #include <memory> 22 #include <singleton.h> 23 24 #include "bundle_mgr_interface.h" 25 #include "context_impl.h" 26 #include "event_handler.h" 27 #include "form_supply_proxy.h" 28 #include "form_render_record.h" 29 #include "form_render_serial_queue.h" 30 #include "js_runtime.h" 31 #include "runtime.h" 32 #include "want.h" 33 #include "status_mgr_center/form_status_common.h" 34 35 namespace OHOS { 36 namespace AppExecFwk { 37 namespace FormRender { 38 using OHOS::AAFwk::Want; 39 using namespace AbilityRuntime; 40 41 class FormRenderServiceMgr final : public DelayedRefSingleton<FormRenderServiceMgr> { 42 DECLARE_DELAYED_REF_SINGLETON(FormRenderServiceMgr) 43 public: 44 DISALLOW_COPY_AND_MOVE(FormRenderServiceMgr); 45 46 /** 47 * @brief Render form. This is sync API. 48 * @param formJsInfo The form js info. 49 * @param want Indicates the {@link Want} structure containing form info. 50 * @param callerToken Caller ability token. 51 * @return Returns ERR_OK on success, others on failure. 52 */ 53 int32_t RenderForm(const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> &callerToken); 54 55 /** 56 * @brief Stop rendering form. This is sync API. 57 * @param formJsInfo The form js info. 58 * @param want Indicates the {@link Want} structure containing form info. 59 * @param callerToken Caller ability token. 60 * @return Returns ERR_OK on success, others on failure. 61 */ 62 int32_t StopRenderingForm(const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> &callerToken); 63 64 /** 65 * @brief When host is died, clean resources. This is async API. 66 * @param hostToken Caller ability token. 67 * @return Returns ERR_OK on success, others on failure. 68 */ 69 int32_t CleanFormHost(const sptr<IRemoteObject> &hostToken); 70 71 /** 72 * @brief Reload form When app updated. This is sync API. 73 * @param formJsInfo The form js info. 74 * @param want Indicates the {@link Want} structure containing form info. 75 * @return int32_t Returns ERR_OK on success, others on failure. 76 */ 77 int32_t ReloadForm(const std::vector<FormJsInfo> &&formJsInfos, const Want &want); 78 79 /** 80 * @brief Called when the system configuration is updated. 81 * @param configuration Indicates the updated configuration information. 82 */ 83 void OnConfigurationUpdated(const std::shared_ptr<OHOS::AppExecFwk::Configuration> &configuration); 84 85 /** 86 * @brief Called when the system start. 87 * @param config Indicates the updated configuration information. 88 */ 89 void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration> &config); 90 91 void RunCachedConfigurationUpdated(); 92 93 int32_t ReleaseRenderer(int64_t formId, const std::string &compId, const std::string &uid, const Want &want); 94 95 int32_t OnUnlock(); 96 97 int32_t SetVisibleChange(const int64_t formId, bool isVisible, const Want &want); 98 99 int32_t RecycleForm(const int64_t formId, const Want &want); 100 101 int32_t RecoverForm(const FormJsInfo &formJsInfo, const Want &want); 102 103 int32_t UpdateFormSize(const int64_t formId, float width, float height, float borderWidth, const std::string &uid); 104 105 void SetFormSupplyClient(const sptr<IFormSupply> &formSupplyClient); 106 107 sptr<IFormSupply> GetFormSupplyClient(); 108 109 private: 110 void FormRenderGCTask(const std::string &uid); 111 void FormRenderGC(const std::string &uid); 112 void OnConfigurationUpdatedInner(); 113 void ConfirmUnlockState(Want &renderWant); 114 int32_t UpdateRenderRecordByUid(const std::string &uid, Want &formRenderWant, const FormJsInfo &formJsInfo, 115 const sptr<IFormSupply> &formSupplyClient); 116 bool IsRenderRecordExist(const std::string &uid); 117 void GetRenderRecordById(std::shared_ptr<FormRenderRecord> &search, const std::string &uid); 118 int32_t RecoverFormByUid( 119 const FormJsInfo &formJsInfo, const Want &want, const std::string &uid, const std::string &statusData); 120 int32_t RecycleFormByUid(const std::string &uid, std::string &statusData, const int64_t formId); 121 int32_t DeleteRenderRecordByUid(const std::string &uid, const std::shared_ptr<FormRenderRecord> &search); 122 int32_t ReloadFormRecord(const std::vector<FormJsInfo> &&formJsInfos, const Want &want); 123 124 void SetCriticalFalseOnAllFormInvisible(); 125 void SetCriticalTrueOnFormActivity(); 126 127 std::shared_ptr<OHOS::AppExecFwk::Configuration> GetNeedApplyConfig(); 128 void CacheAppliedConfig(); 129 130 private: 131 std::mutex renderRecordMutex_; 132 // <uid(userId + bundleName), renderRecord> 133 std::unordered_map<std::string, std::shared_ptr<FormRenderRecord>> renderRecordMap_; 134 std::mutex configMutex_; 135 std::shared_ptr<OHOS::AppExecFwk::Configuration> configuration_; 136 // The configuration items have already been applied to ArkUI. 137 std::shared_ptr<OHOS::AppExecFwk::Configuration> appliedConfig_; 138 std::chrono::steady_clock::time_point configUpdateTime_ = std::chrono::steady_clock::now(); 139 std::shared_ptr<FormRenderSerialQueue> serialQueue_ = nullptr; 140 std::mutex formSupplyMutex_; 141 sptr<IFormSupply> formSupplyClient_; 142 bool isVerified_ = false; 143 bool hasCachedConfig_ = false; 144 }; 145 } // namespace FormRender 146 } // namespace AppExecFwk 147 } // namespace OHOS 148 #endif // OHOS_FORM_FWK_FORM_RENDER_SERVICE_MGR_H 149