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