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