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 CallRouterBack() override; 63 GetParams()64 std::string GetParams() const 65 { 66 return pageRouterManager_->GetParams(); 67 } 68 GetAccessibilityManager()69 RefPtr<AccessibilityManager> GetAccessibilityManager() const override 70 { 71 return accessibilityManager_; 72 } GetWindowConfig()73 WindowConfig& GetWindowConfig() override 74 { 75 return manifestParser_->GetWindowConfig(); 76 } IsForeground()77 bool IsForeground() override 78 { 79 return foregroundFrontend_; 80 } GetPageRouterManager()81 RefPtr<CJPageRouterAbstract> GetPageRouterManager() const 82 { 83 return pageRouterManager_; 84 } SetStageModel(bool isStageModel)85 void SetStageModel(bool isStageModel) 86 { 87 isStageModel_ = isStageModel; 88 } IsStageModel()89 bool IsStageModel() const 90 { 91 return isStageModel_; 92 } SetRuntimeContext(std::weak_ptr<void> context)93 void SetRuntimeContext(std::weak_ptr<void> context) 94 { 95 runtimeContext_ = context; 96 } GetRuntimeContext()97 std::weak_ptr<void> GetRuntimeContext() const 98 { 99 return runtimeContext_; 100 } SetAceAbility(std::weak_ptr<void> aceAbility)101 void SetAceAbility(std::weak_ptr<void> aceAbility) 102 { 103 aceAbility_ = aceAbility; 104 } GetAceAbility()105 std::weak_ptr<void> GetAceAbility() const 106 { 107 return aceAbility_; 108 } GetPipelineContext()109 const RefPtr<PipelineBase>& GetPipelineContext() 110 { 111 return pipelineContextHolder_.Get(); 112 } 113 114 // ---------------- 115 // Measure 116 // ---------------- 117 double MeasureText(const MeasureContext& context); 118 Size MeasureTextSize(const MeasureContext& context); 119 120 // ---------------- 121 // promptAction 122 // ---------------- 123 void ShowToast(const std::string& message, int32_t duration, const std::string& bottom, 124 const NG::ToastShowMode& showMode = NG::ToastShowMode::DEFAULT); 125 126 void OpenCustomDialog(const PromptDialogAttr& dialogAttr, std::function<void(int32_t)>&& callback); 127 128 void CloseCustomDialog(int32_t id); 129 130 // todo: support new params 131 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 132 std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks); 133 void ShowDialogInner(DialogProperties& dialogProperties, std::function<void(int32_t, int32_t)>&& callback, 134 const std::set<std::string>& callbacks); 135 136 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 137 std::function<void(int32_t, int32_t)>&& callback); 138 void ShowActionMenuInner(DialogProperties& dialogProperties, const std::vector<ButtonInfo>& button, 139 std::function<void(int32_t, int32_t)>&& callback); 140 141 // ---------------- 142 // Font 143 // ---------------- 144 void RegisterFont(const std::string& familyName, const std::string& familySrc, const std::string& bundleName = "", 145 const std::string& moduleName = ""); 146 147 VectorStringHandle GetSystemFontList(); 148 NativeOptionFontInfo GetSystemFont(const std::string& fontName); 149 void BackIndex(int32_t index, const std::string& params); 150 void Clear(); 151 int32_t GetLength(); 152 void SetShowAlertBeforeBackPage(const char* msg, std::function<void(int32_t)>&& callback); 153 void SetHideAlertBeforeBackPage(); 154 void GetState(CJPageRouterAbstract::RouterState* info); 155 void GetStateByIndex(CJPageRouterAbstract::RouterState* info); 156 CJPageRouterAbstract::RouterStateList GetStateByUrl(const char* url); 157 void PushPageWithCallback(const std::string& url, const std::string& params, CJPageRouterAbstract::RouterMode& mode, 158 std::function<void(int32_t)>&& callback); 159 void ReplacePageWithCallback(const std::string& url, const std::string& params, 160 CJPageRouterAbstract::RouterMode& mode, std::function<void(int32_t)>&& callback); 161 162 #if defined(PREVIEW) 163 void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; 164 #endif 165 166 protected: 167 friend class CJPageRouterAbstract; 168 169 void InternalRunPage(const std::string& url, const std::string& params); 170 171 // internal properties 172 RefPtr<AccessibilityManager> accessibilityManager_; 173 Framework::PipelineContextHolder pipelineContextHolder_; 174 RefPtr<CJPageRouterAbstract> pageRouterManager_; 175 RefPtr<AssetManager> assetManager_; 176 RefPtr<Framework::ManifestParser> manifestParser_; 177 std::weak_ptr<void> runtimeContext_; 178 std::weak_ptr<void> aceAbility_; 179 180 bool foregroundFrontend_ = false; 181 bool isStageModel_ = false; 182 std::string pageProfile_; 183 184 public: 185 // implement later DumpFrontend()186 void DumpFrontend() const override {} GetPagePath()187 std::string GetPagePath() const override 188 { 189 return ""; 190 } TriggerGarbageCollection()191 void TriggerGarbageCollection() override {} DumpHeapSnapshot(bool isPrivate)192 void DumpHeapSnapshot(bool isPrivate) override {} OnSurfaceChanged(int32_t width,int32_t height)193 void OnSurfaceChanged(int32_t width, int32_t height) override {}; SetColorMode(ColorMode colorMode)194 void SetColorMode(ColorMode colorMode) override {}; 195 void RebuildAllPages() override; 196 197 void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override; 198 199 // discarded methods AddPage(const RefPtr<AcePage> & page)200 void AddPage(const RefPtr<AcePage>& page) override {} GetRouterSize()201 int32_t GetRouterSize() const override 202 { 203 return pageRouterManager_->GetStackSize(); 204 } GetPage(int32_t pageId)205 RefPtr<AcePage> GetPage(int32_t pageId) const override 206 { 207 return nullptr; 208 } SendCallbackMessage(const std::string & callbackId,const std::string & data)209 void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override {} SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher> & dispatcher)210 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override {} TransferComponentResponseData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data)211 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)212 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)213 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)214 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override {} LoadPluginJsCode(std::string && jsCode)215 void LoadPluginJsCode(std::string&& jsCode) const override {} LoadPluginJsByteCode(std::vector<uint8_t> && jsCode,std::vector<int32_t> && jsCodeLen)216 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override {} UpdateState(Frontend::State state)217 void UpdateState(Frontend::State state) override {} OnConfigurationUpdated(const std::string & data)218 void OnConfigurationUpdated(const std::string& data) override {} OnSaveAbilityState(std::string & data)219 void OnSaveAbilityState(std::string& data) override {} OnRestoreAbilityState(const std::string & data)220 void OnRestoreAbilityState(const std::string& data) override {} OnNewWant(const std::string & data)221 void OnNewWant(const std::string& data) override {} OnActive()222 void OnActive() override {} OnInactive()223 void OnInactive() override {} OnStartContinuation()224 bool OnStartContinuation() override 225 { 226 return false; 227 } OnCompleteContinuation(int32_t code)228 void OnCompleteContinuation(int32_t code) override {} OnSaveData(std::string & data)229 void OnSaveData(std::string& data) override {} GetPluginsUsed(std::string & data)230 void GetPluginsUsed(std::string& data) override {} OnRestoreData(const std::string & data)231 bool OnRestoreData(const std::string& data) override 232 { 233 return false; 234 } OnRemoteTerminated()235 void OnRemoteTerminated() override {} OnNewRequest(const std::string & data)236 void OnNewRequest(const std::string& data) override {} OnMemoryLevel(const int32_t level)237 void OnMemoryLevel(const int32_t level) override {} NotifyAppStorage(const std::string & key,const std::string & value)238 void NotifyAppStorage(const std::string& key, const std::string& value) override {} OnLayoutCompleted(const std::string & componentId)239 void OnLayoutCompleted(const std::string& componentId) override {} OnDrawCompleted(const std::string & componentId)240 void OnDrawCompleted(const std::string& componentId) override {} GetEventHandler()241 RefPtr<AceEventHandler> GetEventHandler() override 242 { 243 return nullptr; 244 }; 245 }; 246 } // namespace OHOS::Ace 247 248 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_FRONTEDN_COMMON_H 249