1 /* 2 * Copyright (c) 2021-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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H 18 19 #include <string> 20 #include <unordered_map> 21 22 #include "base/memory/ace_type.h" 23 #include "base/utils/noncopyable.h" 24 #include "base/utils/string_utils.h" 25 #include "bridge/declarative_frontend/ng/page_router_manager.h" 26 #include "core/common/ace_page.h" 27 #include "core/common/container.h" 28 #include "core/common/frontend.h" 29 #include "core/common/js_message_dispatcher.h" 30 #include "core/pipeline_ng/ui_task_scheduler.h" 31 #include "frameworks/bridge/declarative_frontend/frontend_delegate_declarative.h" 32 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" 33 34 namespace OHOS::Ace { 35 36 // DeclarativeFrontend is the unique entrance from ACE backend to frontend. 37 // The relationship between AceActivity, AceContainer and DeclarativeFrontend is 1:1:1. 38 // So DeclarativeFrontend would be responsible for below things: 39 // - Create and initialize JS engine. 40 // - Load pages of a JS app, and parse the manifest.json before loading main page. 41 // - Maintain the page stack of JS app by FrontendDelegateDeclarative. 42 // - Lifecycle of JS app (also AceActivity). 43 class ACE_EXPORT DeclarativeFrontend : public Frontend { 44 DECLARE_ACE_TYPE(DeclarativeFrontend, Frontend); 45 46 public: 47 DeclarativeFrontend() = default; 48 ~DeclarativeFrontend() override; 49 50 bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) override; 51 52 void Destroy() override; 53 54 void AttachPipelineContext(const RefPtr<PipelineBase>& context) override; 55 56 void SetAssetManager(const RefPtr<AssetManager>& assetManager) override; 57 58 void RunPage(int32_t pageId, const std::string& url, const std::string& params) override; 59 60 void ReplacePage(const std::string& url, const std::string& params) override; 61 62 void PushPage(const std::string& url, const std::string& params) override; 63 64 // Js frontend manages all pages self. AddPage(const RefPtr<AcePage> & page)65 void AddPage(const RefPtr<AcePage>& page) override {} 66 GetPage(int32_t)67 RefPtr<AcePage> GetPage(int32_t /*pageId*/) const override 68 { 69 return nullptr; 70 } 71 72 std::string GetCurrentPageUrl() const override; 73 74 // Get the currently running JS page information in NG structure. 75 RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap() const override; 76 77 // Get the currently running JS page information in NG structure. 78 RefPtr<Framework::RevSourceMap> GetFaAppSourceMap() const override; 79 80 void GetStageSourceMap( 81 std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const override; 82 83 RefPtr<NG::PageRouterManager> GetPageRouterManager() const; 84 85 void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override; 86 // platform channel. 87 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override; 88 void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 89 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 90 NativeValue* GetContextValue() override; 91 #if defined(PREVIEW) SetPagePath(const std::string & pagePath)92 void SetPagePath(const std::string& pagePath) 93 { 94 if (delegate_) { 95 delegate_->SetPagePath(pagePath); 96 } 97 } RunNativeEngineLoop()98 void RunNativeEngineLoop() override 99 { 100 if (jsEngine_) { 101 jsEngine_->RunNativeEngineLoop(); 102 } 103 } 104 105 void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; 106 RefPtr<Component> GetNewComponentWithJsCode(const std::string& jsCode, const std::string& viewID); InitializeModuleSearcher(const std::string & bundleName,const std::string & moduleName,const std::string & assetPath,bool isBundle)107 void InitializeModuleSearcher(const std::string& bundleName, const std::string& moduleName, 108 const std::string& assetPath, bool isBundle) 109 { 110 if (jsEngine_) { 111 jsEngine_->InitializeModuleSearcher(bundleName, moduleName, assetPath, isBundle); 112 } 113 } 114 #endif 115 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; 116 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 117 void LoadPluginJsCode(std::string&& jsCode) const override; 118 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override; 119 120 // application lifecycle. 121 void UpdateState(Frontend::State state) override; 122 123 // page lifecycle. 124 bool OnBackPressed() override; 125 void OnShow() override; 126 void OnHide() override; 127 void OnConfigurationUpdated(const std::string& data) override; 128 void OnSaveAbilityState(std::string& data) override; 129 void OnRestoreAbilityState(const std::string& data) override; 130 void OnNewWant(const std::string& data) override; 131 void OnActive() override; 132 void OnInactive() override; 133 bool OnStartContinuation() override; 134 void OnCompleteContinuation(int32_t code) override; 135 void OnSaveData(std::string& data) override; 136 void GetPluginsUsed(std::string& data) override; 137 bool OnRestoreData(const std::string& data) override; 138 void OnRemoteTerminated() override; 139 void OnNewRequest(const std::string& data) override; 140 void OnMemoryLevel(const int32_t level) override; 141 void CallRouterBack() override; 142 void OnSurfaceChanged(int32_t width, int32_t height) override; 143 void OnLayoutCompleted(const std::string& componentId) override; 144 void OnDrawCompleted(const std::string& componentId) override; 145 146 void DumpFrontend() const override; 147 std::string GetPagePath() const override; 148 void TriggerGarbageCollection() override; 149 void DumpHeapSnapshot(bool isPrivate) override; 150 void SetColorMode(ColorMode colorMode) override; 151 void RebuildAllPages() override; 152 void NotifyAppStorage(const std::string& key, const std::string& value) override; GetEventHandler()153 RefPtr<AceEventHandler> GetEventHandler() override 154 { 155 return handler_; 156 } 157 158 // judge frontend is foreground frontend. IsForeground()159 bool IsForeground() override 160 { 161 return foregroundFrontend_; 162 } 163 164 RefPtr<AccessibilityManager> GetAccessibilityManager() const override; 165 WindowConfig& GetWindowConfig() override; 166 167 // navigator component call router 168 void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override; 169 170 // distribute 171 std::string RestoreRouterStack(const std::string& contentInfo) override; 172 std::string GetContentInfo() const override; 173 int32_t GetRouterSize() const override; 174 175 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 176 SetJsEngine(const RefPtr<Framework::JsEngine> & jsEngine)177 void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine) 178 { 179 jsEngine_ = jsEngine; 180 } 181 SetNeedDebugBreakPoint(bool value)182 void SetNeedDebugBreakPoint(bool value) 183 { 184 if (jsEngine_) { 185 jsEngine_->SetNeedDebugBreakPoint(value); 186 } 187 } 188 SetDebugVersion(bool value)189 void SetDebugVersion(bool value) 190 { 191 if (jsEngine_) { 192 jsEngine_->SetDebugVersion(value); 193 } 194 } 195 SetInstanceName(const std::string & name)196 void SetInstanceName(const std::string& name) 197 { 198 if (jsEngine_) { 199 jsEngine_->SetInstanceName(name); 200 } 201 } 202 SetPageProfile(const std::string & pageProfile)203 void SetPageProfile(const std::string& pageProfile) 204 { 205 pageProfile_ = pageProfile; 206 } 207 MarkIsSubWindow(bool isSubWindow)208 void MarkIsSubWindow(bool isSubWindow) 209 { 210 isSubWindow_ = isSubWindow; 211 } 212 GetJsEngine()213 RefPtr<Framework::JsEngine> GetJsEngine() 214 { 215 return jsEngine_; 216 } 217 218 void AttachSubPipelineContext(const RefPtr<PipelineBase>& context); 219 220 void FlushReload() override; 221 void HotReload() override; 222 GetDelegate()223 RefPtr<Framework::FrontendDelegate> GetDelegate() const 224 { 225 return AceType::DynamicCast<Framework::FrontendDelegate>(delegate_); 226 } 227 228 protected: 229 bool isFormRender_ = false; 230 RefPtr<Framework::FrontendDelegateDeclarative> delegate_; 231 232 private: 233 void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor); 234 235 RefPtr<AceEventHandler> handler_; 236 RefPtr<Framework::JsEngine> jsEngine_; 237 RefPtr<AccessibilityManager> accessibilityManager_; 238 std::string pageProfile_; 239 bool foregroundFrontend_ = false; 240 bool isSubWindow_ = false; 241 242 ACE_DISALLOW_COPY_AND_MOVE(DeclarativeFrontend); 243 }; 244 245 class DeclarativeEventHandler : public AceEventHandler { 246 public: DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative> & delegate)247 explicit DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative>& delegate) 248 : delegate_(delegate) 249 { 250 ACE_DCHECK(delegate_); 251 } DeclarativeEventHandler()252 DeclarativeEventHandler() {} 253 254 ~DeclarativeEventHandler() override = default; 255 256 void HandleAsyncEvent(const EventMarker& eventMarker) override; 257 258 void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override; 259 260 void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override; 261 262 void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 263 264 void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override; 265 266 void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override; 267 268 void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override; 269 270 void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override; 271 272 void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override; 273 274 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override; 275 276 void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 277 278 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, 279 const bool isDestroy) override; 280 281 private: 282 RefPtr<Framework::FrontendDelegateDeclarative> delegate_; 283 }; 284 285 } // namespace OHOS::Ace 286 287 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H 288