1 /* 2 * Copyright (c) 2021-2022 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_FRONTEND_DELEGATE_DECLARATIVE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_H 18 19 #include <future> 20 #include <mutex> 21 #include <unordered_map> 22 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "base/thread/cancelable_callback.h" 26 #include "base/utils/measure_util.h" 27 #include "bridge/declarative_frontend/ng/page_router_manager.h" 28 #include "core/common/js_message_dispatcher.h" 29 #include "core/pipeline/pipeline_context.h" 30 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h" 31 #include "frameworks/bridge/common/manifest/manifest_parser.h" 32 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h" 33 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" 34 #include "frameworks/bridge/js_frontend/frontend_delegate.h" 35 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h" 36 #include "frameworks/bridge/js_frontend/js_ace_page.h" 37 38 namespace OHOS::Ace::Framework { 39 40 using ExternalEventCallback = std::function<void(const std::string&, const uint32_t&, const bool&)>; 41 42 enum class RouterAction { 43 PUSH = 0, 44 REPLACE, 45 BACK, 46 CLEAR, 47 }; 48 49 struct RouterTask { 50 RouterAction action = RouterAction::PUSH; 51 PageTarget target = PageTarget(""); 52 std::string params; 53 std::function<void(const std::string&, int32_t)> errorCallback; 54 }; 55 56 class FrontendDelegateDeclarative : public FrontendDelegate { 57 DECLARE_ACE_TYPE(FrontendDelegateDeclarative, FrontendDelegate); 58 59 public: 60 FrontendDelegateDeclarative(const RefPtr<TaskExecutor>& taskExecutor, const LoadJsCallback& loadCallback, 61 const JsMessageDispatcherSetterCallback& transferCallback, const EventCallback& asyncEventCallback, 62 const EventCallback& syncEventCallback, const UpdatePageCallback& updatePageCallback, 63 const ResetStagingPageCallback& resetLoadingPageCallback, const DestroyPageCallback& destroyPageCallback, 64 const DestroyApplicationCallback& destroyApplicationCallback, 65 const UpdateApplicationStateCallback& updateApplicationStateCallback, const TimerCallback& timerCallback, 66 const MediaQueryCallback& mediaQueryCallback, const RequestAnimationCallback& requestAnimationCallback, 67 const JsCallback& jsCallback, const OnWindowDisplayModeChangedCallBack& onWindowDisplayModeChangedCallBack, 68 const OnConfigurationUpdatedCallBack& onConfigurationUpdatedCallBack, 69 const OnSaveAbilityStateCallBack& onSaveAbilityStateCallBack, 70 const OnRestoreAbilityStateCallBack& onRestoreAbilityStateCallBack, const OnNewWantCallBack& onNewWantCallBack, 71 const OnMemoryLevelCallBack& onMemoryLevelCallBack, 72 const OnStartContinuationCallBack& onStartContinuationCallBack, 73 const OnCompleteContinuationCallBack& onCompleteContinuationCallBack, 74 const OnRemoteTerminatedCallBack& onRemoteTerminatedCallBack, const OnSaveDataCallBack& onSaveDataCallBack, 75 const OnRestoreDataCallBack& onRestoreDataCallBack, const ExternalEventCallback& externalEventCallback); 76 ~FrontendDelegateDeclarative() override; 77 78 void AttachPipelineContext(const RefPtr<PipelineBase>& context) override; 79 80 // JSFrontend delegate functions. 81 void RunPage(const std::string& url, const std::string& params, const std::string& profile); 82 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const; 83 void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data); 84 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const; 85 #if defined(PREVIEW) 86 void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; 87 #endif 88 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const; 89 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const; 90 void LoadPluginJsCode(std::string&& jsCode) const; 91 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const; 92 void OnJSCallback(const std::string& callbackId, const std::string& data); 93 bool OnPageBackPress(); 94 void OnBackGround(); 95 void OnForeground(); 96 void OnConfigurationUpdated(const std::string& data); 97 void OnSaveAbilityState(std::string& data); 98 void OnRestoreAbilityState(const std::string& data); 99 void OnNewWant(const std::string& data); 100 bool OnStartContinuation(); 101 void OnCompleteContinuation(int32_t code); 102 void OnMemoryLevel(int32_t level); 103 void OnSaveData(std::string& data); 104 void GetPluginsUsed(std::string& data); 105 bool OnRestoreData(const std::string& data); 106 void OnRemoteTerminated(); 107 void OnNewRequest(const std::string& data); 108 void SetColorMode(ColorMode colorMode); 109 void CallPopPage(); 110 void OnApplicationDestroy(const std::string& packageName); 111 void UpdateApplicationState(const std::string& packageName, Frontend::State state); 112 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 113 void NotifyAppStorage( 114 const WeakPtr<Framework::JsEngine>& jsEngineWeak, const std::string& key, const std::string& value); 115 116 // distribute 117 std::string RestoreRouterStack(const std::string& contentInfo) override; 118 std::string GetContentInfo() override; 119 120 // Accessibility delegate functions. GetJSAccessibilityManager()121 RefPtr<Framework::AccessibilityNodeManager> GetJSAccessibilityManager() const 122 { 123 return jsAccessibilityManager_; 124 } 125 void FireAccessibilityEvent(const AccessibilityEvent& accessibilityEvent); 126 void InitializeAccessibilityCallback(); 127 128 void OnMediaQueryUpdate() override; 129 void OnSurfaceChanged(); 130 // JSEventHandler delegate functions. 131 void FireAsyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs); 132 bool FireSyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs); 133 void FireSyncEvent( 134 const std::string& eventId, const std::string& param, const std::string& jsonArgs, std::string& result); 135 void FireExternalEvent(const std::string& eventId, const std::string& componentId, uint32_t nodeId, bool isDestroy); 136 137 // FrontendDelegate overrides. 138 void Push(const PageTarget& target, const std::string& params, 139 const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr); 140 void Replace(const PageTarget& target, const std::string& params, 141 const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr); 142 void BackWithTarget(const PageTarget& target, const std::string& params); 143 144 void Push(const std::string& uri, const std::string& params) override; 145 void PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override; 146 void PushWithCallback(const std::string& uri, const std::string& params, 147 const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override; 148 void Replace(const std::string& uri, const std::string& params) override; 149 void ReplaceWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override; 150 void ReplaceWithCallback(const std::string& uri, const std::string& params, 151 const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override; 152 void Back(const std::string& uri, const std::string& params) override; 153 void Clear() override; 154 int32_t GetStackSize() const override; 155 void GetState(int32_t& index, std::string& name, std::string& path) override; 156 std::string GetParams() override; 157 158 void PostponePageTransition() override; 159 void LaunchPageTransition() override; 160 size_t GetComponentsCount() override; 161 void TriggerPageUpdate(int32_t pageId, bool directExecute = false) override; 162 163 void PostJsTask(std::function<void()>&& task) override; 164 165 const std::string& GetAppID() const override; 166 const std::string& GetAppName() const override; 167 const std::string& GetVersionName() const override; 168 int32_t GetVersionCode() const override; GetWindowConfig()169 WindowConfig& GetWindowConfig() 170 { 171 return manifestParser_->GetWindowConfig(); 172 } 173 174 double MeasureText(const MeasureContext& context) override; 175 Size MeasureTextSize(const MeasureContext& context) override; 176 177 void ShowToast(const std::string& message, int32_t duration, const std::string& bottom) override; 178 void SetToastStopListenerCallback(std::function<void()>&& stopCallback) override; 179 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 180 bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 181 const std::set<std::string>& callbacks) override; 182 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 183 bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 184 const std::set<std::string>& callbacks, std::function<void(bool)>&& onStatusChanged) override; 185 void ShowDialogInner(DialogProperties& dialogProperties, std::function<void(int32_t, int32_t)>&& callback, 186 const std::set<std::string>& callbacks); 187 188 void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) override; 189 190 void DisableAlertBeforeBackPage() override; 191 192 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 193 std::function<void(int32_t, int32_t)>&& callback) override; 194 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 195 std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged) override; 196 void ShowActionMenuInner(DialogProperties& dialogProperties, const std::vector<ButtonInfo>& button, 197 std::function<void(int32_t, int32_t)>&& callback); 198 199 Rect GetBoundingRectData(NodeId nodeId) override; 200 201 std::string GetInspector(NodeId nodeId) override; 202 203 void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) override; 204 void RemoveVisibleChangeNode(NodeId id) override; 205 // For async event. 206 void SetCallBackResult(const std::string& callBackId, const std::string& result) override; 207 208 void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) override; 209 void ClearTimer(const std::string& callbackId) override; 210 211 void PostSyncTaskToPage(std::function<void()>&& task) override; 212 void AddTaskObserver(std::function<void()>&& task) override; 213 void RemoveTaskObserver() override; 214 215 bool GetAssetContent(const std::string& url, std::string& content) override; 216 bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) override; 217 std::string GetAssetPath(const std::string& url) override; 218 219 // i18n 220 void GetI18nData(std::unique_ptr<JsonValue>& json) override; 221 222 void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) override; 223 224 void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) override; 225 226 int32_t GetMinPlatformVersion() override; 227 228 void LoadResourceConfiguration(std::map<std::string, std::string>& mediaResourceFileMap, 229 std::unique_ptr<JsonValue>& currentResourceData) override; 230 231 void ChangeLocale(const std::string& language, const std::string& countryOrRegion) override; 232 233 void RegisterFont(const std::string& familyName, const std::string& familySrc) override; 234 235 void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) override; 236 237 void RequestAnimationFrame(const std::string& callbackId) override; 238 239 void CancelAnimationFrame(const std::string& callbackId) override; 240 241 SingleTaskExecutor GetAnimationJsTask() override; 242 243 SingleTaskExecutor GetUiTask() override; 244 GetMediaQueryInfoInstance()245 const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() override 246 { 247 return mediaQueryInfo_; 248 } 249 GetGroupJsBridge()250 const RefPtr<GroupJsBridge>& GetGroupJsBridge() override 251 { 252 return groupJsBridge_; 253 } 254 255 #if defined(PREVIEW) SetPagePath(const std::string & pagePath)256 void SetPagePath(const std::string& pagePath) 257 { 258 if (manifestParser_) { 259 manifestParser_->SetPagePath(pagePath); 260 } 261 } 262 #endif 263 264 RefPtr<PipelineBase> GetPipelineContext() override; 265 SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)266 void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge) 267 { 268 groupJsBridge_ = groupJsBridge; 269 } 270 271 RefPtr<JsAcePage> GetPage(int32_t pageId) const override; 272 273 void RebuildAllPages(); 274 275 std::string GetCurrentPageUrl(); 276 277 // Get the currently running JS page information in NG structure. 278 RefPtr<RevSourceMap> GetCurrentPageSourceMap(); 279 280 // Get the currently running JS page information in NG structure. 281 RefPtr<RevSourceMap> GetFaAppSourceMap(); 282 283 void GetStageSourceMap( 284 std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap); 285 286 void InitializeRouterManager(NG::LoadPageCallback&& loadPageCallback); 287 GetPageRouterManager()288 const RefPtr<NG::PageRouterManager>& GetPageRouterManager() const 289 { 290 return pageRouterManager_; 291 } 292 GetTaskExecutor()293 const RefPtr<TaskExecutor>& GetTaskExecutor() const 294 { 295 return taskExecutor_; 296 } 297 GetManifestParser()298 const RefPtr<Framework::ManifestParser>& GetManifestParser() const 299 { 300 return manifestParser_; 301 } 302 303 protected: 304 bool isCardDelegate_ = false; 305 306 private: 307 int32_t GenerateNextPageId(); 308 void RecyclePageId(int32_t pageId); 309 310 void LoadPage( 311 int32_t pageId, const PageTarget& target, bool isMainPage, const std::string& params, bool isRestore = false); 312 void OnPageReady(const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage, bool isRestore); 313 void FlushPageCommand( 314 const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage, bool isRestore); 315 void AddPageLocked(const RefPtr<JsAcePage>& page); 316 void OnPrePageChange(const RefPtr<JsAcePage>& page); 317 void SetCurrentPage(int32_t pageId); 318 319 void OnPushPageSuccess(const RefPtr<JsAcePage>& page, const std::string& url); 320 void OnPopToPageSuccess(const std::string& url); 321 void PopToPage(const std::string& url); 322 int32_t OnPopPageSuccess(); 323 void PopPage(); 324 void RestorePopPage(const RefPtr<JsAcePage>& page, const std::string& url); 325 326 void PushPageTransitionListener(const TransitionEvent& event, const RefPtr<JsAcePage>& page); 327 328 void PopPageTransitionListener(const TransitionEvent& event, int32_t destroyPageId); 329 330 void PopToPageTransitionListener(const TransitionEvent& event, const std::string& url, int32_t pageId); 331 332 void RestorePageTransitionListener( 333 const TransitionEvent& event, const std::string& url, const RefPtr<JsAcePage>& page); 334 335 int32_t OnClearInvisiblePagesSuccess(); 336 void ClearInvisiblePages(); 337 338 void OnReplacePageSuccess(const RefPtr<JsAcePage>& page, const std::string& url); 339 void ReplacePage(const RefPtr<JsAcePage>& page, const std::string& url); 340 void LoadReplacePage(int32_t pageId, const PageTarget& url, const std::string& params); 341 342 void ReplacePageInSubStage(const RefPtr<JsAcePage>& page, const std::string& url); 343 344 uint64_t GetSystemRealTime(); 345 346 // Page lifecycle 347 void OnPageShow(); 348 void OnPageHide(); 349 void OnPageDestroy(int32_t pageId); 350 351 int32_t GetRunningPageId() const; 352 std::string GetRunningPageUrl() const; 353 int32_t GetPageIdByUrl(const std::string& url, bool& isRestore); GetPageIdByUrl(const std::string & url)354 int32_t GetPageIdByUrl(const std::string& url) 355 { 356 bool tmp = false; 357 return GetPageIdByUrl(url, tmp); 358 } 359 360 void ResetStagingPage(); 361 void FlushAnimationTasks(); 362 363 void StartPush(const PageTarget& target, const std::string& params, 364 const std::function<void(const std::string&, int32_t)>& errorCallback); 365 void StartReplace(const PageTarget& target, const std::string& params, 366 const std::function<void(const std::string&, int32_t)>& errorCallback); 367 void StartBack(const PageTarget& target, const std::string& params); 368 void BackCheckAlert(const PageTarget& target, const std::string& params); 369 void ProcessRouterTask(); 370 void AddRouterTask(const RouterTask& task); 371 bool IsNavigationStage(const PageTarget& target); 372 void RecycleSinglePage(); 373 void ClearAlertCallback(PageInfo pageInfo); 374 375 std::atomic<uint64_t> pageIdPool_ = 0; 376 int32_t callbackCnt_ = 0; 377 int32_t pageId_ = -1; 378 bool isRouteStackFull_ = false; 379 bool isStagingPageExist_ = false; 380 int32_t singlePageId_ = -1; 381 std::string mainPagePath_; 382 PageTarget backUri_ = PageTarget(""); 383 std::string backParam_; 384 std::vector<PageInfo> pageRouteStack_; 385 std::unordered_map<int32_t, RefPtr<JsAcePage>> pageMap_; 386 std::unordered_map<int32_t, std::string> pageParamMap_; 387 std::unordered_map<int32_t, std::string> jsCallBackResult_; 388 389 LoadJsCallback loadJs_; 390 ExternalEventCallback externalEvent_; 391 JsMessageDispatcherSetterCallback dispatcherCallback_; 392 EventCallback asyncEvent_; 393 EventCallback syncEvent_; 394 UpdatePageCallback updatePage_; 395 ResetStagingPageCallback resetStagingPage_; 396 DestroyPageCallback destroyPage_; 397 DestroyApplicationCallback destroyApplication_; 398 UpdateApplicationStateCallback updateApplicationState_; 399 TimerCallback timer_; 400 std::unordered_map<std::string, CancelableCallback<void()>> timeoutTaskMap_; 401 MediaQueryCallback mediaQueryCallback_; 402 RequestAnimationCallback requestAnimationCallback_; 403 JsCallback jsCallback_; 404 OnWindowDisplayModeChangedCallBack onWindowDisplayModeChanged_; 405 OnConfigurationUpdatedCallBack onConfigurationUpdated_; 406 OnSaveAbilityStateCallBack onSaveAbilityState_; 407 OnRestoreAbilityStateCallBack onRestoreAbilityState_; 408 OnNewWantCallBack onNewWant_; 409 OnMemoryLevelCallBack onMemoryLevel_; 410 OnStartContinuationCallBack onStartContinuationCallBack_; 411 OnCompleteContinuationCallBack onCompleteContinuationCallBack_; 412 OnRemoteTerminatedCallBack onRemoteTerminatedCallBack_; 413 OnSaveDataCallBack onSaveDataCallBack_; 414 OnRestoreDataCallBack onRestoreDataCallBack_; 415 RefPtr<Framework::ManifestParser> manifestParser_; 416 RefPtr<Framework::AccessibilityNodeManager> jsAccessibilityManager_; 417 RefPtr<MediaQueryInfo> mediaQueryInfo_; 418 RefPtr<GroupJsBridge> groupJsBridge_; 419 420 RefPtr<TaskExecutor> taskExecutor_; 421 422 PipelineContextHolder pipelineContextHolder_; 423 424 BaseId::IdType pageTransitionListenerId_ = 0L; 425 std::queue<std::string> animationFrameTaskIds_; 426 std::unordered_map<std::string, CancelableCallback<void()>> animationFrameTaskMap_; 427 428 mutable std::mutex mutex_; 429 430 std::queue<RouterTask> routerQueue_; 431 mutable std::mutex routerQueueMutex_; 432 433 RefPtr<RevSourceMap> appSourceMap_; 434 RefPtr<NG::PageRouterManager> pageRouterManager_; 435 }; 436 437 } // namespace OHOS::Ace::Framework 438 439 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_H 440