1 /* 2 * Copyright (c) 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_CARD_FRONTEND_FORM_FRONTEND_DELEGATE_DECLARATIVE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_FORM_FRONTEND_DELEGATE_DECLARATIVE_H 18 19 #include <mutex> 20 21 #include "frameworks/base/memory/ace_type.h" 22 #include "frameworks/base/utils/noncopyable.h" 23 #include "frameworks/base/utils/measure_util.h" 24 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h" 25 #include "frameworks/bridge/declarative_frontend/ng/page_router_manager.h" 26 #include "frameworks/bridge/declarative_frontend/frontend_delegate_declarative.h" 27 28 namespace OHOS::Ace::Framework { 29 // This is the primary class by which the CardFrontend delegates 30 31 using LoadCardCallback = std::function<bool(const std::string&, int64_t cardId, const std::string&)>; 32 using UpdateCardDataCallback = std::function<void(const std::string&)>; 33 34 class ACE_EXPORT FormFrontendDelegateDeclarative : public FrontendDelegateDeclarative { 35 DECLARE_ACE_TYPE(FormFrontendDelegateDeclarative, FrontendDelegateDeclarative); 36 public: FormFrontendDelegateDeclarative(const RefPtr<TaskExecutor> & taskExecutor,const LoadJsCallback & loadCallback,const JsMessageDispatcherSetterCallback & transferCallback,const EventCallback & asyncEventCallback,const EventCallback & syncEventCallback,const UpdatePageCallback & updatePageCallback,const ResetStagingPageCallback & resetLoadingPageCallback,const DestroyPageCallback & destroyPageCallback,const DestroyApplicationCallback & destroyApplicationCallback,const UpdateApplicationStateCallback & updateApplicationStateCallback,const TimerCallback & timerCallback,const MediaQueryCallback & mediaQueryCallback,const LayoutInspectorCallback & layoutInspectorCallback,const DrawInspectorCallback & drawInspectorCallback,const DrawChildrenInspectorCallback & drawChildrenInspectorCallback,const RequestAnimationCallback & requestAnimationCallback,const JsCallback & jsCallback,const OnWindowDisplayModeChangedCallBack & onWindowDisplayModeChangedCallBack,const OnConfigurationUpdatedCallBack & onConfigurationUpdatedCallBack,const OnSaveAbilityStateCallBack & onSaveAbilityStateCallBack,const OnRestoreAbilityStateCallBack & onRestoreAbilityStateCallBack,const OnNewWantCallBack & onNewWantCallBack,const OnMemoryLevelCallBack & onMemoryLevelCallBack,const OnStartContinuationCallBack & onStartContinuationCallBack,const OnCompleteContinuationCallBack & onCompleteContinuationCallBack,const OnRemoteTerminatedCallBack & onRemoteTerminatedCallBack,const OnSaveDataCallBack & onSaveDataCallBack,const OnRestoreDataCallBack & onRestoreDataCallBack,const ExternalEventCallback & externalEventCallback)37 FormFrontendDelegateDeclarative(const RefPtr<TaskExecutor>& taskExecutor, const LoadJsCallback& loadCallback, 38 const JsMessageDispatcherSetterCallback& transferCallback, const EventCallback& asyncEventCallback, 39 const EventCallback& syncEventCallback, const UpdatePageCallback& updatePageCallback, 40 const ResetStagingPageCallback& resetLoadingPageCallback, const DestroyPageCallback& destroyPageCallback, 41 const DestroyApplicationCallback& destroyApplicationCallback, 42 const UpdateApplicationStateCallback& updateApplicationStateCallback, const TimerCallback& timerCallback, 43 const MediaQueryCallback& mediaQueryCallback, const LayoutInspectorCallback& layoutInspectorCallback, 44 const DrawInspectorCallback& drawInspectorCallback, 45 const DrawChildrenInspectorCallback& drawChildrenInspectorCallback, 46 const RequestAnimationCallback& requestAnimationCallback, 47 const JsCallback& jsCallback, const OnWindowDisplayModeChangedCallBack& onWindowDisplayModeChangedCallBack, 48 const OnConfigurationUpdatedCallBack& onConfigurationUpdatedCallBack, 49 const OnSaveAbilityStateCallBack& onSaveAbilityStateCallBack, 50 const OnRestoreAbilityStateCallBack& onRestoreAbilityStateCallBack, const OnNewWantCallBack& onNewWantCallBack, 51 const OnMemoryLevelCallBack& onMemoryLevelCallBack, 52 const OnStartContinuationCallBack& onStartContinuationCallBack, 53 const OnCompleteContinuationCallBack& onCompleteContinuationCallBack, 54 const OnRemoteTerminatedCallBack& onRemoteTerminatedCallBack, const OnSaveDataCallBack& onSaveDataCallBack, 55 const OnRestoreDataCallBack& onRestoreDataCallBack, const ExternalEventCallback& externalEventCallback) : 56 FrontendDelegateDeclarative(taskExecutor, loadCallback, transferCallback, asyncEventCallback, 57 syncEventCallback, updatePageCallback, 58 resetLoadingPageCallback, destroyPageCallback, 59 destroyApplicationCallback, 60 updateApplicationStateCallback, timerCallback, 61 mediaQueryCallback, layoutInspectorCallback, 62 drawInspectorCallback, drawChildrenInspectorCallback, 63 requestAnimationCallback, 64 jsCallback, onWindowDisplayModeChangedCallBack, 65 onConfigurationUpdatedCallBack, 66 onSaveAbilityStateCallBack, 67 onRestoreAbilityStateCallBack, onNewWantCallBack, 68 onMemoryLevelCallBack, 69 onStartContinuationCallBack, 70 onCompleteContinuationCallBack, 71 onRemoteTerminatedCallBack, onSaveDataCallBack, 72 onRestoreDataCallBack, externalEventCallback) 73 {} 74 75 ~FormFrontendDelegateDeclarative() override; 76 77 UIContentErrorCode RunCard(const std::string& url, const std::string& params, const std::string& profile, 78 int64_t cardId, const std::string& entryPoint = ""); 79 80 void FireCardEvent(const EventMarker& eventMarker, const std::string& params = ""); 81 82 void FireCardAction(const std::string& action); 83 SetLoadCardCallBack(const LoadCardCallback & loadCallback)84 void SetLoadCardCallBack(const LoadCardCallback& loadCallback) 85 { 86 auto pageRouterManager = GetPageRouterManager(); 87 CHECK_NULL_VOID(pageRouterManager); 88 pageRouterManager->SetLoadCardCallback(loadCallback); 89 } LoadResourceConfiguration(std::map<std::string,std::string> & mediaResourceFileMap,std::unique_ptr<JsonValue> & currentResourceData)90 void LoadResourceConfiguration(std::map<std::string, std::string>& mediaResourceFileMap, 91 std::unique_ptr<JsonValue>& currentResourceData) override 92 {} UpdatePageData(const std::string & dataList)93 void UpdatePageData(const std::string& dataList) 94 { 95 std::lock_guard<std::mutex> lock(mutex_); 96 if (updateCardData_) { 97 updateCardData_(dataList); 98 } else { 99 cardData_ = dataList; 100 } 101 } 102 SetUpdateCardDataCallback(UpdateCardDataCallback && callback)103 void SetUpdateCardDataCallback(UpdateCardDataCallback&& callback) 104 { 105 std::lock_guard<std::mutex> lock(mutex_); 106 updateCardData_ = std::move(callback); 107 } 108 UpdatePageDataImmediately()109 void UpdatePageDataImmediately() 110 { 111 std::lock_guard<std::mutex> lock(mutex_); 112 if (!cardData_.empty() && updateCardData_) { 113 updateCardData_(cardData_); 114 } 115 } 116 117 void RegisterFont(const std::string& familyName, const std::string& familySrc, const std::string& bundleName = "", 118 const std::string& moduleName = "") override; 119 120 private: 121 UpdateCardDataCallback updateCardData_; 122 std::string cardData_; 123 mutable std::mutex mutex_; 124 }; 125 } // namespace OHOS::Ace::Framework 126 127 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_FORM_FRONTEND_DELEGATE_DECLARATIVE_H 128