1 /* 2 * Copyright (c) 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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_FRONTEDN_COMMON_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_FRONTEDN_COMMON_H 18 19 #include <memory> 20 21 #include "cj_page_router_abstract.h" 22 23 #include "base/memory/ace_type.h" 24 #include "base/utils/measure_util.h" 25 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_collection_ffi.h" 26 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_common_ffi.h" 27 #include "bridge/common/manifest/manifest_parser.h" 28 #include "bridge/common/utils/pipeline_context_holder.h" 29 #include "core/common/frontend.h" 30 #include "core/components_ng/pattern/overlay/overlay_manager.h" 31 32 #if defined(PREVIEW) 33 #include "adapter/preview/osal/response_data.h" 34 #endif 35 36 namespace OHOS::Ace { 37 38 class ACE_EXPORT CJFrontendAbstract : public Frontend { 39 DECLARE_ACE_TYPE(CJFrontendAbstract, Frontend); 40 public: 41 // page lifecycle 42 bool OnBackPressed() override; 43 void OnShow() override; 44 void OnHide() override; 45 46 protected: 47 virtual void InternalInitialize() = 0; 48 49 public: 50 ~CJFrontendAbstract() override; 51 bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) override; 52 void Destroy() override; 53 void FlushReload() override; 54 bool CheckLoadAppLibrary(); 55 56 void AttachPipelineContext(const RefPtr<PipelineBase>& context) override; 57 void SetAssetManager(const RefPtr<AssetManager>& assetManager) override; 58 UIContentErrorCode RunPage(const std::string& url, const std::string& params) override; 59 void ReplacePage(const std::string& url, const std::string& params) override; 60 void PushPage(const std::string& url, const std::string& params) override; 61 void Back(const std::string& uri, const std::string& params); 62 void Replace(const std::string& url, const std::string& params, CJPageRouterAbstract::RouterMode modeValue); 63 void CallRouterBack() override; 64 GetParams()65 std::string GetParams() const 66 { 67 return pageRouterManager_->GetParams(); 68 } 69 GetAccessibilityManager()70 RefPtr<AccessibilityManager> GetAccessibilityManager() const override 71 { 72 return accessibilityManager_; 73 } GetWindowConfig()74 WindowConfig& GetWindowConfig() override 75 { 76 return manifestParser_->GetWindowConfig(); 77 } IsForeground()78 bool IsForeground() override 79 { 80 return foregroundFrontend_; 81 } GetPageRouterManager()82 RefPtr<CJPageRouterAbstract> GetPageRouterManager() const 83 { 84 return pageRouterManager_; 85 } SetStageModel(bool isStageModel)86 void SetStageModel(bool isStageModel) 87 { 88 isStageModel_ = isStageModel; 89 } IsStageModel()90 bool IsStageModel() const 91 { 92 return isStageModel_; 93 } SetRuntimeContext(std::weak_ptr<void> context)94 void SetRuntimeContext(std::weak_ptr<void> context) 95 { 96 runtimeContext_ = context; 97 } GetRuntimeContext()98 std::weak_ptr<void> GetRuntimeContext() const 99 { 100 return runtimeContext_; 101 } SetAceAbility(std::weak_ptr<void> aceAbility)102 void SetAceAbility(std::weak_ptr<void> aceAbility) 103 { 104 aceAbility_ = aceAbility; 105 } GetAceAbility()106 std::weak_ptr<void> GetAceAbility() const 107 { 108 return aceAbility_; 109 } GetPipelineContext()110 const RefPtr<PipelineBase>& GetPipelineContext() 111 { 112 return pipelineContextHolder_.Get(); 113 } 114 115 // ---------------- 116 // Measure 117 // ---------------- 118 double MeasureText(const MeasureContext& context); 119 Size MeasureTextSize(const MeasureContext& context); 120 121 // ---------------- 122 // promptAction 123 // ---------------- 124 void ShowToast(const std::string& message, int32_t duration, const std::string& bottom, 125 const NG::ToastShowMode& showMode = NG::ToastShowMode::DEFAULT); 126 127 void OpenCustomDialog(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback); 128 129 void CloseCustomDialog(int32_t id); 130 131 // todo: support new params 132 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 133 std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks); 134 void ShowDialogInner(DialogProperties& dialogProperties, std::function<void(int32_t, int32_t)>&& callback, 135 const std::set<std::string>& callbacks); 136 137 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 138 std::function<void(int32_t, int32_t)>&& callback); 139 void ShowActionMenuInner(DialogProperties& dialogProperties, const std::vector<ButtonInfo>& button, 140 std::function<void(int32_t, int32_t)>&& callback); 141 142 // ---------------- 143 // Font 144 // ---------------- 145 void RegisterFont(const std::string& familyName, const std::string& familySrc, const std::string& bundleName = "", 146 const std::string& moduleName = ""); 147 148 VectorStringHandle GetSystemFontList(); 149 NativeOptionFontInfo GetSystemFont(const std::string& fontName); 150 void BackIndex(int32_t index, const std::string& params); 151 void Clear(); 152 int32_t GetLength(); 153 void SetShowAlertBeforeBackPage(const char* msg, std::function<void(int32_t)>&& callback); 154 void SetHideAlertBeforeBackPage(); 155 void GetState(CJPageRouterAbstract::RouterState* info); 156 void GetStateByIndex(CJPageRouterAbstract::RouterState* info); 157 CJPageRouterAbstract::RouterStateList GetStateByUrl(const char* url); 158 void PushPageWithCallback(const std::string& url, const std::string& params, CJPageRouterAbstract::RouterMode& mode, 159 std::function<void(int32_t)>&& callback); 160 void ReplacePageWithCallback(const std::string& url, const std::string& params, 161 CJPageRouterAbstract::RouterMode& mode, std::function<void(int32_t)>&& callback); 162 163 #if defined(PREVIEW) 164 void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; 165 #endif 166 167 protected: 168 friend class CJPageRouterAbstract; 169 170 void InternalRunPage(const std::string& url, const std::string& params); 171 172 // internal properties 173 RefPtr<AccessibilityManager> accessibilityManager_; 174 Framework::PipelineContextHolder pipelineContextHolder_; 175 RefPtr<CJPageRouterAbstract> pageRouterManager_; 176 RefPtr<AssetManager> assetManager_; 177 RefPtr<Framework::ManifestParser> manifestParser_; 178 std::weak_ptr<void> runtimeContext_; 179 std::weak_ptr<void> aceAbility_; 180 181 bool foregroundFrontend_ = false; 182 bool isStageModel_ = false; 183 std::string pageProfile_; 184 185 public: 186 // implement later DumpFrontend()187 void DumpFrontend() const override {} GetPagePath()188 std::string GetPagePath() const override 189 { 190 return ""; 191 } TriggerGarbageCollection()192 void TriggerGarbageCollection() override {} DumpHeapSnapshot(bool isPrivate)193 void DumpHeapSnapshot(bool isPrivate) override {} OnSurfaceChanged(int32_t width,int32_t height)194 void OnSurfaceChanged(int32_t width, int32_t height) override {}; SetColorMode(ColorMode colorMode)195 void SetColorMode(ColorMode colorMode) override {}; 196 void RebuildAllPages() override; 197 198 void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override; 199 200 // discarded methods AddPage(const RefPtr<AcePage> & page)201 void AddPage(const RefPtr<AcePage>& page) override {} GetRouterSize()202 int32_t GetRouterSize() const override 203 { 204 return pageRouterManager_->GetStackSize(); 205 } GetPage(int32_t pageId)206 RefPtr<AcePage> GetPage(int32_t pageId) const override 207 { 208 return nullptr; 209 } SendCallbackMessage(const std::string & callbackId,const std::string & data)210 void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override {} SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher> & dispatcher)211 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override {} TransferComponentResponseData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data)212 void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override {} TransferJsResponseData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data)213 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override {} TransferJsPluginGetError(int32_t callbackId,int32_t errorCode,std::string && errorMessage)214 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override {} TransferJsEventData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data)215 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override {} LoadPluginJsCode(std::string && jsCode)216 void LoadPluginJsCode(std::string&& jsCode) const override {} LoadPluginJsByteCode(std::vector<uint8_t> && jsCode,std::vector<int32_t> && jsCodeLen)217 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override {} UpdateState(Frontend::State state)218 void UpdateState(Frontend::State state) override {} OnConfigurationUpdated(const std::string & data)219 void OnConfigurationUpdated(const std::string& data) override {} OnSaveAbilityState(std::string & data)220 void OnSaveAbilityState(std::string& data) override {} OnRestoreAbilityState(const std::string & data)221 void OnRestoreAbilityState(const std::string& data) override {} OnNewWant(const std::string & data)222 void OnNewWant(const std::string& data) override {} OnActive()223 void OnActive() override {} OnInactive()224 void OnInactive() override {} OnStartContinuation()225 bool OnStartContinuation() override 226 { 227 return false; 228 } OnCompleteContinuation(int32_t code)229 void OnCompleteContinuation(int32_t code) override {} OnSaveData(std::string & data)230 void OnSaveData(std::string& data) override {} GetPluginsUsed(std::string & data)231 void GetPluginsUsed(std::string& data) override {} OnRestoreData(const std::string & data)232 bool OnRestoreData(const std::string& data) override 233 { 234 return false; 235 } OnRemoteTerminated()236 void OnRemoteTerminated() override {} OnNewRequest(const std::string & data)237 void OnNewRequest(const std::string& data) override {} OnMemoryLevel(const int32_t level)238 void OnMemoryLevel(const int32_t level) override {} NotifyAppStorage(const std::string & key,const std::string & value)239 void NotifyAppStorage(const std::string& key, const std::string& value) override {} OnLayoutCompleted(const std::string & componentId)240 void OnLayoutCompleted(const std::string& componentId) override {} OnDrawCompleted(const std::string & componentId)241 void OnDrawCompleted(const std::string& componentId) override {} OnDrawChildrenCompleted(const std::string & componentId)242 void OnDrawChildrenCompleted(const std::string& componentId) override {} IsDrawChildrenCallbackFuncExist(const std::string & componentId)243 bool IsDrawChildrenCallbackFuncExist(const std::string& componentId) override 244 { 245 return false; 246 } GetEventHandler()247 RefPtr<AceEventHandler> GetEventHandler() override 248 { 249 return nullptr; 250 }; 251 }; 252 } // namespace OHOS::Ace 253 254 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_FRONTEDN_COMMON_H 255