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 using NotifySendCommandFunction = std::function<void(int32_t id, const std::string& command)>; 42 using NotifySendCommandAsyncFunction = std::function<int32_t(int32_t id, const std::string& command)>; 43 using SendCommandFunction = std::function<void(int32_t value)>; 44 /** 45 * @description: Get ui_manager instance,this object process singleton 46 * @return The return value is ui_manager singleton 47 */ 48 static UiSessionManager* GetInstance(); 49 50 /** 51 * @description: execute click callback when component click event occurs 52 */ ReportClickEvent(const std::string & data)53 virtual void ReportClickEvent(const std::string& data) {}; 54 55 /** 56 * @description: execute search callback when component search event occurs 57 */ ReportSearchEvent(const std::string & data)58 virtual void ReportSearchEvent(const std::string& data) {}; 59 60 /** 61 * @description: execute switch callback when page switch to another page occurs 62 */ ReportRouterChangeEvent(const std::string & data)63 virtual void ReportRouterChangeEvent(const std::string& data) {}; 64 65 /** 66 * @description: execute click callback when page some component change occurs 67 */ ReportComponentChangeEvent(const std::string & key,const std::string & value)68 virtual void ReportComponentChangeEvent(const std::string& key, const std::string& value) {}; 69 70 /** 71 * @description: execute click callback when page some component change occurs 72 */ ReportComponentChangeEvent(int32_t nodeId,const std::string & key,const std::shared_ptr<InspectorJsonValue> & value)73 virtual void ReportComponentChangeEvent( 74 int32_t nodeId, const std::string& key, const std::shared_ptr<InspectorJsonValue>& value) {}; 75 #if !defined(PREVIEW) && !defined(ACE_UNITTEST) && defined(OHOS_PLATFORM) 76 /** 77 * @description: save report communication stub side 78 * @param reportStub report communication stub side 79 */ SaveReportStub(sptr<IRemoteObject> reportStub,int32_t processId)80 virtual void SaveReportStub(sptr<IRemoteObject> reportStub, int32_t processId) {}; 81 #endif 82 /** 83 * @description: get current page inspector tree value 84 */ GetInspectorTree()85 virtual void GetInspectorTree() {}; AddValueForTree(int32_t id,const std::string & value)86 virtual void AddValueForTree(int32_t id, const std::string& value) {}; WebTaskNumsChange(int32_t num)87 virtual void WebTaskNumsChange(int32_t num) {}; ReportInspectorTreeValue(const std::string & value)88 virtual void ReportInspectorTreeValue(const std::string& value) {}; SaveForSendCommandFunction(NotifySendCommandFunction && function)89 virtual void SaveForSendCommandFunction(NotifySendCommandFunction&& function) {}; SaveForSendCommandAsyncFunction(NotifySendCommandAsyncFunction && function)90 virtual void SaveForSendCommandAsyncFunction(NotifySendCommandAsyncFunction&& function) {}; SaveInspectorTreeFunction(InspectorFunction && function)91 virtual void SaveInspectorTreeFunction(InspectorFunction&& function) {}; SaveRegisterForWebFunction(NotifyAllWebFunction && function)92 virtual void SaveRegisterForWebFunction(NotifyAllWebFunction&& function) {}; 93 /** 94 * @description: Report web editing area focus/blur/textChange event 95 * @param type The type of event (focus, blur, or textChange), defaults to empty string 96 */ 97 virtual void ReportWebUnfocusEvent( 98 int64_t accessibilityId, const std::string& data, const std::string& type = "") {}; NotifyAllWebPattern(bool isRegister)99 virtual void NotifyAllWebPattern(bool isRegister) {}; NotifySendCommandPattern(int32_t id,const std::string & command)100 virtual void NotifySendCommandPattern(int32_t id, const std::string& command) {}; NotifySendCommandAsyncPattern(int32_t id,const std::string & command)101 virtual int32_t NotifySendCommandAsyncPattern(int32_t id, const std::string& command) { return 11; }; SetClickEventRegistered(bool status)102 virtual void SetClickEventRegistered(bool status) {}; SetSearchEventRegistered(bool status)103 virtual void SetSearchEventRegistered(bool status) {}; OnRouterChange(const std::string & path,const std::string & event)104 virtual void OnRouterChange(const std::string& path, const std::string& event) {}; SetRouterChangeEventRegistered(bool status)105 virtual void SetRouterChangeEventRegistered(bool status) {}; SetComponentChangeEventRegistered(bool status)106 virtual void SetComponentChangeEventRegistered(bool status) {}; GetClickEventRegistered()107 virtual bool GetClickEventRegistered() 108 { 109 return false; 110 }; GetSearchEventRegistered()111 virtual bool GetSearchEventRegistered() 112 { 113 return false; 114 }; GetRouterChangeEventRegistered()115 virtual bool GetRouterChangeEventRegistered() 116 { 117 return false; 118 }; GetComponentChangeEventRegistered()119 virtual bool GetComponentChangeEventRegistered() 120 { 121 return false; 122 }; GetWebFocusRegistered()123 virtual bool GetWebFocusRegistered() 124 { 125 return false; 126 }; SaveBaseInfo(const std::string & info)127 virtual void SaveBaseInfo(const std::string& info) {}; SendBaseInfo(int32_t processId)128 virtual void SendBaseInfo(int32_t processId) {}; SaveGetPixelMapFunction(GetPixelMapFunction && function)129 virtual void SaveGetPixelMapFunction(GetPixelMapFunction&& function) {}; SaveTranslateManager(std::shared_ptr<UiTranslateManager> uiTranslateManager,int32_t instanceId)130 virtual void SaveTranslateManager(std::shared_ptr<UiTranslateManager> uiTranslateManager, 131 int32_t instanceId) {}; SaveGetCurrentInstanceIdCallback(std::function<int32_t ()> && callback)132 virtual void SaveGetCurrentInstanceIdCallback(std::function<int32_t()>&& callback) {}; RemoveSaveGetCurrentInstanceId(int32_t instanceId)133 virtual void RemoveSaveGetCurrentInstanceId(int32_t instanceId) {}; GetCurrentTranslateManager()134 virtual std::shared_ptr<UiTranslateManager> GetCurrentTranslateManager() { 135 std::shared_ptr<UiTranslateManager> currentTranslateManager = nullptr; 136 return currentTranslateManager; 137 }; GetWebViewLanguage()138 virtual void GetWebViewLanguage() {}; RegisterPipeLineGetCurrentPageName(std::function<std::string ()> && callback)139 virtual void RegisterPipeLineGetCurrentPageName(std::function<std::string()>&& callback) {}; GetCurrentPageName()140 virtual void GetCurrentPageName() {}; SendCurrentPageName(const std::string & result)141 virtual void SendCurrentPageName(const std::string& result) {}; SendCurrentLanguage(std::string result)142 virtual void SendCurrentLanguage(std::string result) {}; SaveProcessId(std::string key,int32_t id)143 virtual void SaveProcessId(std::string key, int32_t id) {}; GetWebTranslateText(std::string extraData,bool isContinued)144 virtual void GetWebTranslateText(std::string extraData, bool isContinued) {}; SendWebTextToAI(int32_t nodeId,std::string res)145 virtual void SendWebTextToAI(int32_t nodeId, std::string res) {}; SendTranslateResult(int32_t nodeId,std::vector<std::string> results,std::vector<int32_t> ids)146 virtual void SendTranslateResult(int32_t nodeId, std::vector<std::string> results, std::vector<int32_t> ids) {}; SendTranslateResult(int32_t nodeId,std::string result)147 virtual void SendTranslateResult(int32_t nodeId, std::string result) {}; 148 virtual void ResetTranslate(int32_t nodeId = -1) {}; GetPixelMap()149 virtual void GetPixelMap() {}; SendCommand(const std::string & command)150 virtual void SendCommand(const std::string& command) {}; SaveSendCommandFunction(SendCommandFunction && function)151 virtual void SaveSendCommandFunction(SendCommandFunction&& function) {}; 152 #if !defined(PREVIEW) && !defined(ACE_UNITTEST) && defined(OHOS_PLATFORM) SendPixelMap(const std::vector<std::pair<int32_t,std::shared_ptr<Media::PixelMap>>> & maps)153 virtual void SendPixelMap(const std::vector<std::pair<int32_t, std::shared_ptr<Media::PixelMap>>>& maps) {}; 154 #endif GetVisibleInspectorTree()155 virtual void GetVisibleInspectorTree() {}; 156 RegisterPipeLineExeAppAIFunction(std::function<uint32_t (const std::string & funcName,const std::string & params)> && callback)157 virtual void RegisterPipeLineExeAppAIFunction( 158 std::function<uint32_t(const std::string& funcName, const std::string& params)>&& callback) {}; ExeAppAIFunction(const std::string & funcName,const std::string & params)159 virtual void ExeAppAIFunction(const std::string& funcName, const std::string& params) {}; SendExeAppAIFunctionResult(uint32_t result)160 virtual void SendExeAppAIFunctionResult(uint32_t result) {}; 161 protected: 162 UiSessionManager() = default; 163 virtual ~UiSessionManager() = default; 164 165 static std::mutex mutex_; 166 static std::shared_mutex reportObjectMutex_; 167 #if !defined(PREVIEW) && !defined(ACE_UNITTEST) && defined(OHOS_PLATFORM) 168 std::map<int32_t, sptr<IRemoteObject>> reportObjectMap_; 169 #endif 170 std::map<std::string, int32_t> processMap_; 171 std::atomic<int32_t> clickEventRegisterProcesses_ = 0; 172 std::atomic<int32_t> searchEventRegisterProcesses_ = 0; 173 std::atomic<int32_t> routerChangeEventRegisterProcesses_ = 0; 174 std::atomic<int32_t> componentChangeEventRegisterProcesses_ = 0; 175 bool webFocusEventRegistered = false; 176 InspectorFunction inspectorFunction_ = 0; 177 NotifyAllWebFunction notifyWebFunction_ = 0; 178 GetPixelMapFunction getPixelMapFunction_ = 0; 179 NotifySendCommandFunction notifySendCommandFunction_ = 0; 180 NotifySendCommandAsyncFunction notifySendCommandAsyncFunction_ = 0; 181 std::shared_ptr<InspectorJsonValue> jsonValue_ = nullptr; 182 std::atomic<int32_t> webTaskNums_ = 0; 183 std::string baseInfo_; 184 std::map<int32_t, std::shared_ptr<UiTranslateManager>> translateManagerMap_; 185 std::shared_mutex translateManagerMutex_; 186 std::function<int32_t()> getInstanceIdCallback_; 187 std::shared_mutex getInstanceIdCallbackMutex_; 188 std::function<std::string()> pipelineContextPageNameCallback_; 189 SendCommandFunction sendCommandFunction_ = 0; 190 std::function<uint32_t(const std::string& funcName, const std::string& params)> pipelineExeAppAIFunctionCallback_; 191 }; 192 } // namespace OHOS::Ace 193 #endif // FOUNDATION_ACE_INTERFACE_UI_SESSION_MANAGER_H 194