1 /* 2 * Copyright (c) 2021 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/thread/cancelable_callback.h" 25 #include "core/common/js_message_dispatcher.h" 26 #include "core/pipeline/pipeline_context.h" 27 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h" 28 #include "frameworks/bridge/common/manifest/manifest_parser.h" 29 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h" 30 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" 31 #include "frameworks/bridge/js_frontend/frontend_delegate.h" 32 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h" 33 #include "frameworks/bridge/js_frontend/js_ace_page.h" 34 35 namespace OHOS::Ace::Framework { 36 37 using ExternalEventCallback = std::function<void(const std::string&, const uint32_t&)>; 38 39 class FrontendDelegateDeclarative : public FrontendDelegate { 40 DECLARE_ACE_TYPE(FrontendDelegateDeclarative, FrontendDelegate); 41 42 public: 43 FrontendDelegateDeclarative(const RefPtr<TaskExecutor>& taskExecutor, const LoadJsCallback& loadCallback, 44 const JsMessageDispatcherSetterCallback& transferCallback, const EventCallback& asyncEventCallback, 45 const EventCallback& syncEventCallback, const UpdatePageCallback& updatePageCallback, 46 const ResetStagingPageCallback& resetLoadingPageCallback, const DestroyPageCallback& destroyPageCallback, 47 const DestroyApplicationCallback& destroyApplicationCallback, 48 const UpdateApplicationStateCallback& updateApplicationStateCallback, const TimerCallback& timerCallback, 49 const MediaQueryCallback& mediaQueryCallback, const RequestAnimationCallback& requestAnimationCallback, 50 const JsCallback& jsCallback, const OnWindowDisplayModeChangedCallBack& onWindowDisplayModeChangedCallBack, 51 const OnConfigurationUpdatedCallBack& onConfigurationUpdatedCallBack, 52 const OnSaveAbilityStateCallBack& onSaveAbilityStateCallBack, 53 const OnRestoreAbilityStateCallBack& onRestoreAbilityStateCallBack, 54 const OnNewWantCallBack& onNewWantCallBack, 55 const OnActiveCallBack& onActiveCallBack, const OnInactiveCallBack& onInactiveCallBack, 56 const OnMemoryLevelCallBack& onMemoryLevelCallBack, 57 const OnStartContinuationCallBack& onStartContinuationCallBack, 58 const OnCompleteContinuationCallBack& onCompleteContinuationCallBack, 59 const OnRemoteTerminatedCallBack& onRemoteTerminatedCallBack, 60 const OnSaveDataCallBack& onSaveDataCallBack, 61 const OnRestoreDataCallBack& onRestoreDataCallBack, 62 const ExternalEventCallback& externalEventCallback); 63 ~FrontendDelegateDeclarative() override; 64 65 void AttachPipelineContext(const RefPtr<PipelineContext>& context) override; 66 67 // JSFrontend delegate functions. 68 void RunPage(const std::string& url, const std::string& params, const std::string& profile); 69 void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const; 70 void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data); 71 void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const; 72 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) 73 void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const; 74 #endif 75 void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const; 76 void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const; 77 void LoadPluginJsCode(std::string&& jsCode) const; 78 void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const; 79 void OnJSCallback(const std::string& callbackId, const std::string& data); 80 bool OnPageBackPress(); 81 void OnBackGround(); 82 void OnForground(); 83 void OnConfigurationUpdated(const std::string& data); 84 void OnSaveAbilityState(std::string& data); 85 void OnRestoreAbilityState(const std::string& data); 86 void OnNewWant(const std::string& data); 87 void OnSuspended(); 88 bool OnStartContinuation(); 89 void OnCompleteContinuation(int32_t code); 90 void OnMemoryLevel(const int32_t level); 91 void OnSaveData(std::string& data); 92 void GetPluginsUsed(std::string& data); 93 bool OnRestoreData(const std::string& data); 94 void OnRemoteTerminated(); 95 void OnActive(); 96 void OnInactive(); 97 void OnNewRequest(const std::string& data); 98 void SetColorMode(ColorMode colorMode); 99 void CallPopPage(); 100 void OnApplicationDestroy(const std::string& packageName); 101 void UpdateApplicationState(const std::string& packageName, Frontend::State state); 102 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 103 void NotifyAppStorage(const WeakPtr<Framework::JsEngine>& jsEngineWeak, 104 const std::string& key, const std::string& value); 105 106 // distribute 107 std::string RestoreRouterStack(const std::string& contentInfo) override; 108 std::string GetContentInfo() override; 109 110 // Accessibility delegate functions. GetJSAccessibilityManager()111 RefPtr<Framework::AccessibilityNodeManager> GetJSAccessibilityManager() const 112 { 113 return jsAccessibilityManager_; 114 } 115 void FireAccessibilityEvent(const AccessibilityEvent& accessibilityEvent); 116 void InitializeAccessibilityCallback(); 117 118 void OnMediaQueryUpdate() override; 119 void OnSurfaceChanged(); 120 // JSEventHandler delegate functions. 121 void FireAsyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs); 122 bool FireSyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs); 123 void FireSyncEvent( 124 const std::string& eventId, const std::string& param, const std::string& jsonArgs, std::string& result); 125 void FireExternalEvent(const std::string& eventId, const std::string& componentId, const uint32_t nodeId); 126 127 // FrontendDelegate overrides. 128 void Push(const PageTarget& target, const std::string& params); 129 void Replace(const PageTarget& target, const std::string& params); 130 void BackWithTarget(const PageTarget& target, const std::string& params); 131 void Push(const std::string& uri, const std::string& params) override; 132 void Replace(const std::string& uri, const std::string& params) override; 133 void Back(const std::string& uri, const std::string& params) override; 134 void PostponePageTransition() override; 135 void LaunchPageTransition() override; 136 void Clear() override; 137 int32_t GetStackSize() const override; 138 void GetState(int32_t& index, std::string& name, std::string& path) override; 139 std::string GetParams() override; 140 void TriggerPageUpdate(int32_t pageId, bool directExecute = false) override; 141 142 void PostJsTask(std::function<void()>&& task) override; 143 144 const std::string& GetAppID() const override; 145 const std::string& GetAppName() const override; 146 const std::string& GetVersionName() const override; 147 int32_t GetVersionCode() const override; GetWindowConfig()148 WindowConfig& GetWindowConfig() 149 { 150 return manifestParser_->GetWindowConfig(); 151 } 152 153 void ShowToast(const std::string& message, int32_t duration, const std::string& bottom) override; 154 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 155 bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 156 const std::set<std::string>& callbacks) override; 157 158 void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) override; 159 160 void DisableAlertBeforeBackPage() override; 161 162 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 163 std::function<void(int32_t, int32_t)>&& callback) override; 164 165 Rect GetBoundingRectData(NodeId nodeId) override; 166 167 std::string GetInspector(NodeId nodeId) override; 168 169 void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) override; 170 void RemoveVisibleChangeNode(NodeId id) override; 171 // For async event. 172 void SetCallBackResult(const std::string& callBackId, const std::string& result) override; 173 174 void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) override; 175 void ClearTimer(const std::string& callbackId) override; 176 177 void PostSyncTaskToPage(std::function<void()>&& task) override; 178 void AddTaskObserver(std::function<void()>&& task) override; 179 void RemoveTaskObserver() override; 180 181 bool GetAssetContent(const std::string& url, std::string& content) override; 182 bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) override; 183 std::string GetAssetPath(const std::string& url) override; 184 185 // i18n 186 void GetI18nData(std::unique_ptr<JsonValue>& json) override; 187 188 void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) override; 189 190 void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) override; 191 192 int32_t GetMinPlatformVersion() override; 193 194 void LoadResourceConfiguration(std::map<std::string, std::string>& mediaResourceFileMap, 195 std::unique_ptr<JsonValue>& currentResourceData) override; 196 197 void ChangeLocale(const std::string& language, const std::string& countryOrRegion) override; 198 199 void RegisterFont(const std::string& familyName, const std::string& familySrc) override; 200 201 void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) override; 202 203 void RequestAnimationFrame(const std::string& callbackId) override; 204 205 void CancelAnimationFrame(const std::string& callbackId) override; 206 207 SingleTaskExecutor GetAnimationJsTask() override; 208 209 SingleTaskExecutor GetUiTask() override; 210 GetMediaQueryInfoInstance()211 const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() override 212 { 213 return mediaQueryInfo_; 214 } 215 GetGroupJsBridge()216 const RefPtr<GroupJsBridge>& GetGroupJsBridge() override 217 { 218 return groupJsBridge_; 219 } 220 221 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM) SetPagePath(const std::string & pagePath)222 void SetPagePath(const std::string& pagePath) 223 { 224 if (manifestParser_) { 225 manifestParser_->SetPagePath(pagePath); 226 } 227 } 228 #endif 229 230 RefPtr<PipelineContext> GetPipelineContext() override; 231 SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)232 void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge) 233 { 234 groupJsBridge_ = groupJsBridge; 235 } 236 237 RefPtr<JsAcePage> GetPage(int32_t pageId) const override; 238 239 void RebuildAllPages(); 240 private: 241 int32_t GenerateNextPageId(); 242 void RecyclePageId(int32_t pageId); 243 244 void LoadPage(int32_t pageId, const PageTarget& target, bool isMainPage, const std::string& params, 245 bool isRestore = false); 246 void OnPageReady( 247 const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage, bool isRestore); 248 void FlushPageCommand( 249 const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage, bool isRestore); 250 void AddPageLocked(const RefPtr<JsAcePage>& page); 251 void OnPrePageChange(const RefPtr<JsAcePage>& page); 252 void SetCurrentPage(int32_t pageId); 253 254 void OnPushPageSuccess(const RefPtr<JsAcePage>& page, const std::string& url); 255 void OnPopToPageSuccess(const std::string& url); 256 void PopToPage(const std::string& url); 257 int32_t OnPopPageSuccess(); 258 void PopPage(); 259 void RestorePopPage(const RefPtr<JsAcePage>& page, const std::string& url); 260 261 void PopPageTransitionListener(const TransitionEvent& event, int32_t destroyPageId); 262 263 void PopToPageTransitionListener( 264 const TransitionEvent& event, const std::string& url, int32_t pageId); 265 266 void RestorePageTransitionListener( 267 const TransitionEvent& event, const std::string& url, const RefPtr<JsAcePage>& page); 268 269 int32_t OnClearInvisiblePagesSuccess(); 270 void ClearInvisiblePages(); 271 272 void OnReplacePageSuccess(const RefPtr<JsAcePage>& page, const std::string& url); 273 void ReplacePage(const RefPtr<JsAcePage>& page, const std::string& url); 274 void LoadReplacePage(int32_t pageId, const PageTarget& url, const std::string& params); 275 276 uint64_t GetSystemRealTime(); 277 278 // Page lifecycle 279 void OnPageShow(); 280 void OnPageHide(); 281 void OnPageDestroy(int32_t pageId); 282 283 int32_t GetRunningPageId() const; 284 std::string GetRunningPageUrl() const; 285 int32_t GetPageIdByUrl(const std::string& url, bool isRestore = false); 286 287 void ResetStagingPage(); 288 void FlushAnimationTasks(); 289 290 void ClearAlertCallback(PageInfo pageInfo); 291 292 std::atomic<uint64_t> pageIdPool_ = 0; 293 int32_t callbackCnt_ = 0; 294 int32_t pageId_ = -1; 295 bool isRouteStackFull_ = false; 296 bool isStagingPageExist_ = false; 297 std::string mainPagePath_; 298 std::string backUri_; 299 std::string backParam_; 300 std::vector<PageInfo> pageRouteStack_; 301 std::unordered_map<int32_t, RefPtr<JsAcePage>> pageMap_; 302 std::unordered_map<int32_t, std::string> pageParamMap_; 303 std::unordered_map<int32_t, std::string> jsCallBackResult_; 304 305 LoadJsCallback loadJs_; 306 ExternalEventCallback externalEvent_; 307 JsMessageDispatcherSetterCallback dispatcherCallback_; 308 EventCallback asyncEvent_; 309 EventCallback syncEvent_; 310 UpdatePageCallback updatePage_; 311 ResetStagingPageCallback resetStagingPage_; 312 DestroyPageCallback destroyPage_; 313 DestroyApplicationCallback destroyApplication_; 314 UpdateApplicationStateCallback updateApplicationState_; 315 TimerCallback timer_; 316 std::unordered_map<std::string, CancelableCallback<void()>> timeoutTaskMap_; 317 MediaQueryCallback mediaQueryCallback_; 318 RequestAnimationCallback requestAnimationCallback_; 319 JsCallback jsCallback_; 320 OnWindowDisplayModeChangedCallBack onWindowDisplayModeChanged_; 321 OnConfigurationUpdatedCallBack onConfigurationUpdated_; 322 OnSaveAbilityStateCallBack onSaveAbilityState_; 323 OnRestoreAbilityStateCallBack onRestoreAbilityState_; 324 OnNewWantCallBack onNewWant_; 325 OnActiveCallBack onActive_; 326 OnInactiveCallBack onInactive_; 327 OnMemoryLevelCallBack onMemoryLevel_; 328 OnStartContinuationCallBack onStartContinuationCallBack_; 329 OnCompleteContinuationCallBack onCompleteContinuationCallBack_; 330 OnRemoteTerminatedCallBack onRemoteTerminatedCallBack_; 331 OnSaveDataCallBack onSaveDataCallBack_; 332 OnRestoreDataCallBack onRestoreDataCallBack_; 333 RefPtr<Framework::ManifestParser> manifestParser_; 334 RefPtr<Framework::AccessibilityNodeManager> jsAccessibilityManager_; 335 RefPtr<MediaQueryInfo> mediaQueryInfo_; 336 RefPtr<GroupJsBridge> groupJsBridge_; 337 338 RefPtr<TaskExecutor> taskExecutor_; 339 340 PipelineContextHolder pipelineContextHolder_; 341 342 BaseId::IdType pageTransitionListenerId_ = 0L; 343 std::queue<std::string> animationFrameTaskIds_; 344 std::unordered_map<std::string, CancelableCallback<void()>> animationFrameTaskMap_; 345 346 mutable std::mutex mutex_; 347 }; 348 349 } // namespace OHOS::Ace::Framework 350 351 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_H 352