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 UIContentErrorCode RunIntentPage() override; 66 67 UIContentErrorCode SetRouterIntentInfo(const std::string& intentInfoSerialized, bool isColdStart, 68 const std::function<void()>&& loadPageCallback) override; 69 70 std::string GetTopNavDestinationInfo(bool onlyFullScreen, bool needParam) override; 71 72 void ReplacePage(const std::string& url, const std::string& params) override; 73 74 void PushPage(const std::string& url, const std::string& params) override; 75 76 // Js frontend manages all pages self. AddPage(const RefPtr<AcePage> & page)77 void AddPage(const RefPtr<AcePage>& page) override {} 78 GetPage(int32_t)79 RefPtr<AcePage> GetPage(int32_t /*pageId*/) const override 80 { 81 return nullptr; 82 } 83 84 std::string GetCurrentPageUrl() const override; 85 86 // Get the currently running JS page information in NG structure. 87 RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap() const override; 88 89 // Get the currently running JS page information in NG structure. 90 RefPtr<Framework::RevSourceMap> GetFaAppSourceMap() const override; 91 92 void GetStageSourceMap( 93 std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const override; 94 95 RefPtr<NG::PageRouterManager> GetPageRouterManager() const; 96 97 void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override; 98 // platform channel. 99 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override; 100 void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 101 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 102 napi_value GetContextValue() override; 103 bool BuilderNodeFunc(std::string functionName, const std::vector<int32_t>& nodeIds) override; 104 napi_value GetFrameNodeValueByNodeId(int32_t nodeId) override; 105 #if defined(PREVIEW) SetPkgNameList(const std::map<std::string,std::string> & map)106 void SetPkgNameList(const std::map<std::string, std::string>& map) 107 { 108 pkgNameMap_ = map; 109 } 110 SetPkgAliasList(const std::map<std::string,std::string> & map)111 void SetPkgAliasList(const std::map<std::string, std::string>& map) 112 { 113 pkgAliasMap_ = map; 114 } 115 SetpkgContextInfoList(const std::map<std::string,std::vector<std::vector<std::string>>> & map)116 void SetpkgContextInfoList(const std::map<std::string, std::vector<std::vector<std::string>>>& map) 117 { 118 pkgContextInfoMap_ = map; 119 } 120 SetPagePath(const std::string & pagePath)121 void SetPagePath(const std::string& pagePath) 122 { 123 if (delegate_) { 124 delegate_->SetPagePath(pagePath); 125 } 126 } RunNativeEngineLoop()127 void RunNativeEngineLoop() override 128 { 129 if (jsEngine_) { 130 jsEngine_->RunNativeEngineLoop(); 131 } 132 } 133 134 void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; 135 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)136 void InitializeModuleSearcher(const std::string& bundleName, const std::string& moduleName, 137 const std::string& assetPath, bool isBundle) 138 { 139 if (jsEngine_) { 140 jsEngine_->InitializeModuleSearcher(bundleName, moduleName, assetPath, isBundle); 141 } 142 } 143 #endif 144 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override; 145 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override; 146 void LoadPluginJsCode(std::string&& jsCode) const override; 147 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override; 148 149 // application lifecycle. 150 void UpdateState(Frontend::State state) override; 151 152 // page lifecycle. 153 bool OnBackPressed() override; 154 void OnShow() override; 155 void OnHide() override; 156 void OnConfigurationUpdated(const std::string& data) override; 157 void OnSaveAbilityState(std::string& data) override; 158 void OnRestoreAbilityState(const std::string& data) override; 159 void OnNewWant(const std::string& data) override; 160 void OnActive() override; 161 void OnInactive() override; 162 bool OnStartContinuation() override; 163 void OnCompleteContinuation(int32_t code) override; 164 void OnSaveData(std::string& data) override; 165 void GetPluginsUsed(std::string& data) override; 166 bool OnRestoreData(const std::string& data) override; 167 void OnRemoteTerminated() override; 168 void OnNewRequest(const std::string& data) override; 169 void OnMemoryLevel(const int32_t level) override; 170 void CallRouterBack() override; 171 void OnSurfaceChanged(int32_t width, int32_t height) override; 172 void OnLayoutCompleted(const std::string& componentId) override; 173 void OnDrawCompleted(const std::string& componentId) override; 174 void OnDrawChildrenCompleted(const std::string& componentId) override; 175 bool IsDrawChildrenCallbackFuncExist(const std::string& componentId) override; 176 void DumpFrontend() const override; 177 std::string GetPagePath() const override; 178 void TriggerGarbageCollection() override; 179 void DumpHeapSnapshot(bool isPrivate) override; 180 void NotifyUIIdle() override; 181 void SetColorMode(ColorMode colorMode) override; 182 void RebuildAllPages() override; 183 void NotifyAppStorage(const std::string& key, const std::string& value) override; GetEventHandler()184 RefPtr<AceEventHandler> GetEventHandler() override 185 { 186 return handler_; 187 } 188 189 // judge frontend is foreground frontend. IsForeground()190 bool IsForeground() override 191 { 192 return foregroundFrontend_; 193 } 194 195 RefPtr<AccessibilityManager> GetAccessibilityManager() const override; 196 WindowConfig& GetWindowConfig() override; 197 198 // navigator component call router 199 void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override; 200 201 // restore 202 std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack( 203 const std::string& contentInfo, ContentInfoType type) override; 204 std::string GetContentInfo(ContentInfoType type) const override; 205 int32_t GetRouterSize() const override; 206 207 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 208 SetJsEngine(const RefPtr<Framework::JsEngine> & jsEngine)209 void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine) 210 { 211 jsEngine_ = jsEngine; 212 } 213 SetNeedDebugBreakPoint(bool value)214 void SetNeedDebugBreakPoint(bool value) 215 { 216 if (jsEngine_) { 217 jsEngine_->SetNeedDebugBreakPoint(value); 218 } 219 } 220 SetDebugVersion(bool value)221 void SetDebugVersion(bool value) 222 { 223 if (jsEngine_) { 224 jsEngine_->SetDebugVersion(value); 225 } 226 } 227 SetInstanceName(const std::string & name)228 void SetInstanceName(const std::string& name) 229 { 230 if (jsEngine_) { 231 jsEngine_->SetInstanceName(name); 232 } 233 } 234 SetPageProfile(const std::string & pageProfile)235 void SetPageProfile(const std::string& pageProfile) 236 { 237 pageProfile_ = pageProfile; 238 } 239 MarkIsSubWindow(bool isSubWindow)240 void MarkIsSubWindow(bool isSubWindow) 241 { 242 isSubWindow_ = isSubWindow; 243 } 244 GetJsEngine()245 RefPtr<Framework::JsEngine> GetJsEngine() 246 { 247 return jsEngine_; 248 } 249 250 void AttachSubPipelineContext(const RefPtr<PipelineBase>& context); 251 252 void FlushReload() override; 253 void HotReload() override; 254 GetDelegate()255 RefPtr<Framework::FrontendDelegate> GetDelegate() const 256 { 257 return AceType::DynamicCast<Framework::FrontendDelegate>(delegate_); 258 } 259 260 std::string GetPagePathByUrl(const std::string& url) const override; 261 262 protected: 263 bool isFormRender_ = false; 264 RefPtr<Framework::FrontendDelegateDeclarative> delegate_; 265 266 private: 267 void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor); 268 269 RefPtr<AceEventHandler> handler_; 270 RefPtr<Framework::JsEngine> jsEngine_; 271 RefPtr<AccessibilityManager> accessibilityManager_; 272 std::string pageProfile_; 273 #if defined(PREVIEW) 274 std::map<std::string, std::string> pkgNameMap_; 275 std::map<std::string, std::string> pkgAliasMap_; 276 std::map<std::string, std::vector<std::vector<std::string>>> pkgContextInfoMap_; 277 #endif 278 bool foregroundFrontend_ = false; 279 bool isSubWindow_ = false; 280 281 ACE_DISALLOW_COPY_AND_MOVE(DeclarativeFrontend); 282 }; 283 284 class DeclarativeEventHandler : public AceEventHandler { 285 public: DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative> & delegate)286 explicit DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative>& delegate) 287 : delegate_(delegate) 288 { 289 ACE_DCHECK(delegate_); 290 } DeclarativeEventHandler()291 DeclarativeEventHandler() {} 292 293 ~DeclarativeEventHandler() override = default; 294 295 void HandleAsyncEvent(const EventMarker& eventMarker) override; 296 297 void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override; 298 299 void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override; 300 301 void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 302 303 void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override; 304 305 void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override; 306 307 void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override; 308 309 void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override; 310 311 void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override; 312 313 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override; 314 315 void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override; 316 317 void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, 318 const bool isDestroy) override; 319 320 private: 321 RefPtr<Framework::FrontendDelegateDeclarative> delegate_; 322 }; 323 324 } // namespace OHOS::Ace 325 326 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H 327