1 /* 2 * Copyright (c) 2024 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_INTERFACE_UI_SESSION_MANAGER_H 17 #define FOUNDATION_ACE_INTERFACE_UI_SESSION_MANAGER_H 18 #include <atomic> 19 #include <cstdint> 20 #include <functional> 21 #if !defined(PREVIEW) && !defined(ACE_UNITTEST) && defined(OHOS_PLATFORM) 22 #include <iremote_object.h> 23 24 #include "ui_report_stub.h" 25 #endif 26 27 #include <map> 28 #include <mutex> 29 #include <shared_mutex> 30 31 #include "ui_session_json_util.h" 32 #include "ui_translate_manager.h" 33 34 #include "base/utils/macros.h" 35 namespace OHOS::Ace { 36 class ACE_FORCE_EXPORT UiSessionManager { 37 public: 38 using InspectorFunction = std::function<void(bool onlyNeedVisible)>; 39 using NotifyAllWebFunction = std::function<void(bool isRegister)>; 40 using GetPixelMapFunction = std::function<void()>; 41 /** 42 * @description: Get ui_manager instance,this object process singleton 43 * @return The return value is ui_manager singleton 44 */ 45 static UiSessionManager* GetInstance(); 46 47 /** 48 * @description: execute click callback when component click event occurs 49 */ ReportClickEvent(const std::string & data)50 virtual void ReportClickEvent(const std::string& data) {}; 51 52 /** 53 * @description: execute search callback when component search event occurs 54 */ ReportSearchEvent(const std::string & data)55 virtual void ReportSearchEvent(const std::string& data) {}; 56 57 /** 58 * @description: execute switch callback when page switch to another page occurs 59 */ ReportRouterChangeEvent(const std::string & data)60 virtual void ReportRouterChangeEvent(const std::string& data) {}; 61 62 /** 63 * @description: execute click callback when page some component change occurs 64 */ ReportComponentChangeEvent(const std::string & key,const std::string & value)65 virtual void ReportComponentChangeEvent(const std::string& key, const std::string& value) {}; 66 67 /** 68 * @description: execute click callback when page some component change occurs 69 */ ReportComponentChangeEvent(int32_t nodeId,const std::string & key,const std::shared_ptr<InspectorJsonValue> & value)70 virtual void ReportComponentChangeEvent( 71 int32_t nodeId, const std::string& key, const std::shared_ptr<InspectorJsonValue>& value) {}; 72 #if !defined(PREVIEW) && !defined(ACE_UNITTEST) && defined(OHOS_PLATFORM) 73 /** 74 * @description: save report communication stub side 75 * @param reportStub report communication stub side 76 */ SaveReportStub(sptr<IRemoteObject> reportStub,int32_t processId)77 virtual void SaveReportStub(sptr<IRemoteObject> reportStub, int32_t processId) {}; 78 #endif 79 /** 80 * @description: get current page inspector tree value 81 */ GetInspectorTree()82 virtual void GetInspectorTree() {}; AddValueForTree(int32_t id,const std::string & value)83 virtual void AddValueForTree(int32_t id, const std::string& value) {}; WebTaskNumsChange(int32_t num)84 virtual void WebTaskNumsChange(int32_t num) {}; ReportInspectorTreeValue(const std::string & value)85 virtual void ReportInspectorTreeValue(const std::string& value) {}; SaveInspectorTreeFunction(InspectorFunction && function)86 virtual void SaveInspectorTreeFunction(InspectorFunction&& function) {}; SaveRegisterForWebFunction(NotifyAllWebFunction && function)87 virtual void SaveRegisterForWebFunction(NotifyAllWebFunction&& function) {}; ReportWebUnfocusEvent(int64_t accessibilityId,const std::string & data)88 virtual void ReportWebUnfocusEvent(int64_t accessibilityId, const std::string& data) {}; NotifyAllWebPattern(bool isRegister)89 virtual void NotifyAllWebPattern(bool isRegister) {}; SetClickEventRegistered(bool status)90 virtual void SetClickEventRegistered(bool status) {}; SetSearchEventRegistered(bool status)91 virtual void SetSearchEventRegistered(bool status) {}; OnRouterChange(const std::string & path,const std::string & event)92 virtual void OnRouterChange(const std::string& path, const std::string& event) {}; SetRouterChangeEventRegistered(bool status)93 virtual void SetRouterChangeEventRegistered(bool status) {}; SetComponentChangeEventRegistered(bool status)94 virtual void SetComponentChangeEventRegistered(bool status) {}; GetClickEventRegistered()95 virtual bool GetClickEventRegistered() 96 { 97 return false; 98 }; GetSearchEventRegistered()99 virtual bool GetSearchEventRegistered() 100 { 101 return false; 102 }; GetRouterChangeEventRegistered()103 virtual bool GetRouterChangeEventRegistered() 104 { 105 return false; 106 }; GetComponentChangeEventRegistered()107 virtual bool GetComponentChangeEventRegistered() 108 { 109 return false; 110 }; GetWebFocusRegistered()111 virtual bool GetWebFocusRegistered() 112 { 113 return false; 114 }; SaveBaseInfo(const std::string & info)115 virtual void SaveBaseInfo(const std::string& info) {}; SendBaseInfo(int32_t processId)116 virtual void SendBaseInfo(int32_t processId) {}; SaveGetPixelMapFunction(GetPixelMapFunction && function)117 virtual void SaveGetPixelMapFunction(GetPixelMapFunction&& function) {}; SaveTranslateManager(std::shared_ptr<UiTranslateManager> uiTranslateManager)118 virtual void SaveTranslateManager(std::shared_ptr<UiTranslateManager> uiTranslateManager) {}; GetWebViewLanguage()119 virtual void GetWebViewLanguage() {}; RegisterPipeLineGetCurrentPageName(const std::function<std::string ()> && callback)120 virtual void RegisterPipeLineGetCurrentPageName(const std::function<std::string()>&& callback) {}; GetCurrentPageName()121 virtual void GetCurrentPageName() {}; SendCurrentPageName(const std::string result)122 virtual void SendCurrentPageName(const std::string result) {}; SendCurrentLanguage(std::string result)123 virtual void SendCurrentLanguage(std::string result) {}; SaveProcessId(std::string key,int32_t id)124 virtual void SaveProcessId(std::string key, int32_t id) {}; GetWebTranslateText(std::string extraData,bool isContinued)125 virtual void GetWebTranslateText(std::string extraData, bool isContinued) {}; SendWebTextToAI(int32_t nodeId,std::string res)126 virtual void SendWebTextToAI(int32_t nodeId, std::string res) {}; SendTranslateResult(int32_t nodeId,std::vector<std::string> results,std::vector<int32_t> ids)127 virtual void SendTranslateResult(int32_t nodeId, std::vector<std::string> results, std::vector<int32_t> ids) {}; SendTranslateResult(int32_t nodeId,std::string result)128 virtual void SendTranslateResult(int32_t nodeId, std::string result) {}; 129 virtual void ResetTranslate(int32_t nodeId = -1) {}; GetPixelMap()130 virtual void GetPixelMap() {}; 131 #if !defined(PREVIEW) && !defined(ACE_UNITTEST) && defined(OHOS_PLATFORM) SendPixelMap(std::vector<std::pair<int32_t,std::shared_ptr<Media::PixelMap>>> maps)132 virtual void SendPixelMap(std::vector<std::pair<int32_t, std::shared_ptr<Media::PixelMap>>> maps) {}; 133 #endif GetVisibleInspectorTree()134 virtual void GetVisibleInspectorTree() {}; 135 136 protected: 137 static std::mutex mutex_; 138 static std::shared_mutex reportObjectMutex_; 139 #if !defined(PREVIEW) && !defined(ACE_UNITTEST) && defined(OHOS_PLATFORM) 140 std::map<int32_t, sptr<IRemoteObject>> reportObjectMap_; 141 #endif 142 std::map<std::string, int32_t> processMap_; 143 std::atomic<int32_t> clickEventRegisterProcesses_ = 0; 144 std::atomic<int32_t> searchEventRegisterProcesses_ = 0; 145 std::atomic<int32_t> routerChangeEventRegisterProcesses_ = 0; 146 std::atomic<int32_t> componentChangeEventRegisterProcesses_ = 0; 147 bool webFocusEventRegistered = false; 148 InspectorFunction inspectorFunction_ = 0; 149 NotifyAllWebFunction notifyWebFunction_ = 0; 150 GetPixelMapFunction getPixelMapFunction_ = 0; 151 std::shared_ptr<InspectorJsonValue> jsonValue_ = nullptr; 152 std::atomic<int32_t> webTaskNums_ = 0; 153 std::string baseInfo_; 154 std::shared_ptr<UiTranslateManager> translateManager_ = nullptr; 155 static std::shared_mutex translateManagerMutex_; 156 std::function<std::string()> pipelineContextPageNameCallback_; 157 }; 158 } // namespace OHOS::Ace 159 #endif // FOUNDATION_ACE_INTERFACE_UI_SESSION_MANAGER_H 160