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 void 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) override; 152 void ShowDialog(const std::string& title, const std::string& message, 153 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 154 const std::set<std::string>& callbacks) override; 155 EnableAlertBeforeBackPage(const std::string & message,std::function<void (int32_t)> && callback)156 void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) override {} 157 DisableAlertBeforeBackPage()158 void DisableAlertBeforeBackPage() override {} 159 ShowActionMenu(const std::string & title,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback)160 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 161 std::function<void(int32_t, int32_t)>&& callback) override {} 162 163 Rect GetBoundingRectData(NodeId nodeId) override; 164 165 std::string GetInspector(NodeId nodeId) override; 166 167 void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) override; 168 void RemoveVisibleChangeNode(NodeId id) 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) override; 198 199 void GetSystemFontList(std::vector<std::string>& fontList) override; 200 201 bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo) override; 202 203 void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) override; 204 GetSnapshot(const std::string & componentId,std::function<void (std::shared_ptr<Media::PixelMap>,int32_t)> && callback)205 void GetSnapshot(const std::string& componentId, 206 std::function<void(std::shared_ptr<Media::PixelMap>, int32_t)>&& callback) override 207 {} 208 209 void RequestAnimationFrame(const std::string& callbackId) override; 210 211 void CancelAnimationFrame(const std::string& callbackId) override; 212 213 SingleTaskExecutor GetAnimationJsTask() override; 214 215 SingleTaskExecutor GetUiTask() override; 216 GetMediaQueryInfoInstance()217 const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() override 218 { 219 return mediaQueryInfo_; 220 } 221 GetGroupJsBridge()222 const RefPtr<GroupJsBridge>& GetGroupJsBridge() override 223 { 224 return groupJsBridge_; 225 } 226 227 RefPtr<PipelineBase> GetPipelineContext() override; 228 SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)229 void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge) 230 { 231 groupJsBridge_ = groupJsBridge; 232 } 233 234 RefPtr<JsAcePage> GetPage(int32_t pageId) const override; 235 236 void RebuildAllPages(); 237 238 void UpdatePlugin(const std::string& content); 239 SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams && callback)240 void SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams&& callback) 241 { 242 if (callback) { 243 onPluginUpdateWithValueParams_ = callback; 244 } 245 } 246 FireDeclarativeOnUpdateWithValueParamsCallback(const std::string & params)247 void FireDeclarativeOnUpdateWithValueParamsCallback(const std::string& params) const 248 { 249 if (onPluginUpdateWithValueParams_) { 250 onPluginUpdateWithValueParams_(params); 251 } 252 } 253 254 private: 255 int32_t GenerateNextPageId(); 256 void RecyclePageId(int32_t pageId); 257 258 void LoadPage(int32_t pageId, const PageTarget& target, bool isMainPage, const std::string& params); 259 void LoadJS(const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage); 260 void OnPageReady( 261 const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage); 262 void FlushPageCommand( 263 const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage); 264 void AddPageLocked(const RefPtr<JsAcePage>& page); 265 void OnPrePageChange(const RefPtr<JsAcePage>& page); 266 void SetCurrentPage(int32_t pageId); 267 268 void OnPushPageSuccess(const RefPtr<JsAcePage>& page, const std::string& url); 269 void OnPopToPageSuccess(const std::string& url); 270 void PopToPage(const std::string& url); 271 int32_t OnPopPageSuccess(); 272 void PopPage(); 273 274 void PopPageTransitionListener(const TransitionEvent& event, int32_t destroyPageId); 275 276 void PopToPageTransitionListener( 277 const TransitionEvent& event, const std::string& url, int32_t pageId); 278 279 int32_t OnClearInvisiblePagesSuccess(); 280 void ClearInvisiblePages(); 281 282 void OnReplacePageSuccess(const RefPtr<JsAcePage>& page, const std::string& url); 283 void ReplacePage(const RefPtr<JsAcePage>& page, const std::string& url); 284 void LoadReplacePage(int32_t pageId, const PageTarget& url, const std::string& params); 285 286 uint64_t GetSystemRealTime(); 287 288 // Page lifecycle 289 void OnPageShow(); 290 void OnPageHide(); 291 void OnPageDestroy(int32_t pageId); 292 293 int32_t GetRunningPageId() const; 294 std::string GetRunningPageUrl() const; 295 int32_t GetPageIdByUrl(const std::string& url); 296 297 void ResetStagingPage(); 298 void FlushAnimationTasks(); 299 300 std::atomic<uint64_t> pageIdPool_ = 0; 301 int32_t callbackCnt_ = 0; 302 int32_t pageId_ = -1; 303 bool isRouteStackFull_ = false; 304 bool isStagingPageExist_ = false; 305 std::string mainPagePath_; 306 std::vector<PageInfo> pageRouteStack_; 307 std::unordered_map<int32_t, RefPtr<JsAcePage>> pageMap_; 308 std::unordered_map<int32_t, std::string> pageParamMap_; 309 std::unordered_map<int32_t, std::string> jsCallBackResult_; 310 311 LoadJsCallback loadJs_; 312 JsMessageDispatcherSetterCallback dispatcherCallback_; 313 EventCallback asyncEvent_; 314 EventCallback syncEvent_; 315 UpdatePageCallback updatePage_; 316 ResetStagingPageCallback resetStagingPage_; 317 DestroyPageCallback destroyPage_; 318 DestroyApplicationCallback destroyApplication_; 319 UpdateApplicationStateCallback updateApplicationState_; 320 TimerCallback timer_; 321 std::unordered_map<std::string, CancelableCallback<void()>> timeoutTaskMap_; 322 MediaQueryCallback mediaQueryCallback_; 323 RequestAnimationCallback requestAnimationCallback_; 324 JsCallback jsCallback_; 325 OnWindowDisplayModeChangedCallBack onWindowDisplayModeChanged_; 326 OnConfigurationUpdatedCallBack onConfigurationUpdated_; 327 OnSaveAbilityStateCallBack onSaveAbilityState_; 328 OnRestoreAbilityStateCallBack onRestoreAbilityState_; 329 OnNewWantCallBack onNewWant_; 330 OnActiveCallBack onActive_; 331 OnInactiveCallBack onInactive_; 332 OnMemoryLevelCallBack onMemoryLevel_; 333 onPluginUpdateWithValueParams onPluginUpdateWithValueParams_; 334 OnStartContinuationCallBack onStartContinuationCallBack_; 335 OnCompleteContinuationCallBack onCompleteContinuationCallBack_; 336 OnRemoteTerminatedCallBack onRemoteTerminatedCallBack_; 337 OnSaveDataCallBack onSaveDataCallBack_; 338 OnRestoreDataCallBack onRestoreDataCallBack_; 339 RefPtr<Framework::ManifestParser> manifestParser_; 340 RefPtr<Framework::AccessibilityNodeManager> jsAccessibilityManager_; 341 RefPtr<MediaQueryInfo> mediaQueryInfo_; 342 RefPtr<GroupJsBridge> groupJsBridge_; 343 344 RefPtr<TaskExecutor> taskExecutor_; 345 346 PipelineContextHolder pipelineContextHolder_; 347 348 BaseId::IdType pageTransitionListenerId_ = 0L; 349 std::queue<std::string> animationFrameTaskIds_; 350 std::unordered_map<std::string, CancelableCallback<void()>> animationFrameTaskMap_; 351 352 mutable std::mutex mutex_; 353 }; 354 } // namespace OHOS::Ace::Framework 355 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_DELEGATE_H 356