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