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, const std::string& params) 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 // restore 193 std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack( 194 const std::string& contentInfo, ContentInfoType type) override; 195 std::string GetContentInfo(ContentInfoType type) const override; 196 int32_t GetRouterSize() const override; 197 198 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 199 SetJsEngine(const RefPtr<Framework::JsEngine> & jsEngine)200 void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine) 201 { 202 jsEngine_ = jsEngine; 203 } 204 SetNeedDebugBreakPoint(bool value)205 void SetNeedDebugBreakPoint(bool value) 206 { 207 if (jsEngine_) { 208 jsEngine_->SetNeedDebugBreakPoint(value); 209 } 210 } 211 SetDebugVersion(bool value)212 void SetDebugVersion(bool value) 213 { 214 if (jsEngine_) { 215 jsEngine_->SetDebugVersion(value); 216 } 217 } 218 SetInstanceName(const std::string & name)219 void SetInstanceName(const std::string& name) 220 { 221 if (jsEngine_) { 222 jsEngine_->SetInstanceName(name); 223 } 224 } 225 SetPageProfile(const std::string & pageProfile)226 void SetPageProfile(const std::string& pageProfile) 227 { 228 pageProfile_ = pageProfile; 229 } 230 MarkIsSubWindow(bool isSubWindow)231 void MarkIsSubWindow(bool isSubWindow) 232 { 233 isSubWindow_ = isSubWindow; 234 } 235 GetJsEngine()236 RefPtr<Framework::JsEngine> GetJsEngine() 237 { 238 return jsEngine_; 239 } 240 241 void AttachSubPipelineContext(const RefPtr<PipelineBase>& context); 242 243 void FlushReload() override; 244 void HotReload() override; 245 GetDelegate()246 RefPtr<Framework::FrontendDelegate> GetDelegate() const 247 { 248 return AceType::DynamicCast<Framework::FrontendDelegate>(delegate_); 249 } 250 251 protected: 252 bool isFormRender_ = false; 253 RefPtr<Framework::FrontendDelegateDeclarative> delegate_; 254 255 private: 256 void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor); 257 258 RefPtr<AceEventHandler> handler_; 259 RefPtr<Framework::JsEngine> jsEngine_; 260 RefPtr<AccessibilityManager> accessibilityManager_; 261 std::string pageProfile_; 262 #if defined(PREVIEW) 263 std::map<std::string, std::string> pkgNameMap_; 264 std::map<std::string, std::string> pkgAliasMap_; 265 std::map<std::string, std::vector<std::vector<std::string>>> pkgContextInfoMap_; 266 #endif 267 bool foregroundFrontend_ = false; 268 bool isSubWindow_ = false; 269 270 ACE_DISALLOW_COPY_AND_MOVE(DeclarativeFrontend); 271 }; 272 273 class DeclarativeEventHandler : public AceEventHandler { 274 public: DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative> & delegate)275 explicit DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative>& delegate) 276 : delegate_(delegate) 277 { 278 ACE_DCHECK(delegate_); 279 } DeclarativeEventHandler()280 DeclarativeEventHandler() {} 281 282 ~DeclarativeEventHandler() override = default; 283 284 void HandleAsyncEvent(const EventMarker& eventMarker) override; 285 286 void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override; 287 288 void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override; 289 290 void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 291 292 void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override; 293 294 void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override; 295 296 void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override; 297 298 void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override; 299 300 void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override; 301 302 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override; 303 304 void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 305 306 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, 307 const bool isDestroy) override; 308 309 private: 310 RefPtr<Framework::FrontendDelegateDeclarative> delegate_; 311 }; 312 313 } // namespace OHOS::Ace 314 315 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H 316