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