/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ROSEN_WINDOW_SCENE_SESSION_H #define OHOS_ROSEN_WINDOW_SCENE_SESSION_H #include #include #include #include #include #include "accessibility_element_info.h" #include "interfaces/include/ws_common.h" #include "session/container/include/zidl/session_stage_interface.h" #include "session/host/include/zidl/session_stub.h" #include "session/host/include/scene_persistence.h" #include "wm_common.h" #include "occupied_area_change_info.h" #include "window_visibility_info.h" namespace OHOS::MMI { class PointerEvent; class KeyEvent; class AxisEvent; enum class WindowArea; } // namespace OHOS::MMI namespace OHOS::Media { class PixelMap; } // namespace OHOS::Media namespace OHOS::Rosen { class RSSurfaceNode; class RSTransaction; class RSSyncTransactionController; using NotifyPendingSessionActivationFunc = std::function; using NotifySessionStateChangeFunc = std::function; using NotifyBufferAvailableChangeFunc = std::function; using NotifySessionStateChangeNotifyManagerFunc = std::function; using NotifyRequestFocusStatusNotifyManagerFunc = std::function; using NotifyBackPressedFunc = std::function; using NotifySessionFocusableChangeFunc = std::function; using NotifySessionTouchableChangeFunc = std::function; using NotifyClickFunc = std::function; using NotifyTerminateSessionFunc = std::function; using NotifyTerminateSessionFuncNew = std::function; using NotifyTerminateSessionFuncTotal = std::function; using NofitySessionLabelUpdatedFunc = std::function; using NofitySessionIconUpdatedFunc = std::function; using NotifySessionExceptionFunc = std::function; using NotifySessionSnapshotFunc = std::function; using NotifyPendingSessionToForegroundFunc = std::function; using NotifyPendingSessionToBackgroundForDelegatorFunc = std::function; using NotifyCallingSessionUpdateRectFunc = std::function; using NotifyCallingSessionForegroundFunc = std::function; using NotifyCallingSessionBackgroundFunc = std::function; using NotifyRaiseToTopForPointDownFunc = std::function; using NotifyUIRequestFocusFunc = std::function; using NotifyUILostFocusFunc = std::function; using GetStateFromManagerFunc = std::function; using NotifySessionInfoLockedStateChangeFunc = std::function; using NotifySystemSessionPointerEventFunc = std::function pointerEvent)>; using NotifySessionInfoChangeNotifyManagerFunc = std::function; using NotifySystemSessionKeyEventFunc = std::function keyEvent)>; class ILifecycleListener { public: virtual void OnActivation() = 0; virtual void OnConnect() = 0; virtual void OnForeground() = 0; virtual void OnBackground() = 0; virtual void OnDisconnect() = 0; virtual void OnExtensionDied() = 0; virtual void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info, int64_t uiExtensionIdLevel) = 0; }; enum class LifeCycleTaskType : uint32_t { START, STOP }; class Session : public SessionStub { public: using Task = std::function; explicit Session(const SessionInfo& info); virtual ~Session() = default; void SetEventHandler(const std::shared_ptr& handler, const std::shared_ptr& exportHandler = nullptr); WSError Connect(const sptr& sessionStage, const sptr& eventChannel, const std::shared_ptr& surfaceNode, SystemSessionConfig& systemConfig, sptr property = nullptr, sptr token = nullptr, int32_t pid = -1, int32_t uid = -1) override; WSError Reconnect(const sptr& sessionStage, const sptr& eventChannel, const std::shared_ptr& surfaceNode, sptr property = nullptr, sptr token = nullptr, int32_t pid = -1, int32_t uid = -1); WSError Foreground(sptr property) override; WSError Background() override; WSError Disconnect(bool isFromClient = false) override; WSError Show(sptr property) override; WSError Hide() override; bool RegisterLifecycleListener(const std::shared_ptr& listener); bool UnregisterLifecycleListener(const std::shared_ptr& listener); void NotifyActivation(); void NotifyConnect(); void NotifyForeground(); void NotifyBackground(); void NotifyDisconnect(); void NotifyExtensionDied() override; void NotifyTransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info, int64_t uiExtensionIdLevel) override; virtual WSError TransferPointerEvent(const std::shared_ptr& pointerEvent, bool needNotifyClient = true); virtual WSError TransferKeyEvent(const std::shared_ptr& keyEvent); virtual WSError TransferSearchElementInfo(int64_t elementId, int32_t mode, int64_t baseParent, std::list& infos); virtual WSError TransferSearchElementInfosByText(int64_t elementId, const std::string& text, int64_t baseParent, std::list& infos); virtual WSError TransferFindFocusedElementInfo(int64_t elementId, int32_t focusType, int64_t baseParent, Accessibility::AccessibilityElementInfo& info); virtual WSError TransferFocusMoveSearch(int64_t elementId, int32_t direction, int64_t baseParent, Accessibility::AccessibilityElementInfo& info); virtual WSError NotifyClientToUpdateRect(std::shared_ptr rsTransaction) { return WSError::WS_OK; } WSError TransferBackPressedEventForConsumed(bool& isConsumed); WSError TransferKeyEventForConsumed(const std::shared_ptr& keyEvent, bool& isConsumed); WSError TransferFocusActiveEvent(bool isFocusActive); WSError TransferFocusStateEvent(bool focusState); virtual WSError TransferExecuteAction(int64_t elementId, const std::map& actionArguments, int32_t action, int64_t baseParent); virtual WSError UpdateAvoidArea(const sptr& avoidArea, AvoidAreaType type) { return WSError::WS_OK; } int32_t GetPersistentId() const; std::shared_ptr GetSurfaceNode() const; void SetLeashWinSurfaceNode(std::shared_ptr leashWinSurfaceNode); std::shared_ptr GetLeashWinSurfaceNode() const; std::shared_ptr GetSnapshot() const; std::shared_ptr Snapshot(const float scaleParam = 0.0f) const; SessionState GetSessionState() const; virtual void SetSessionState(SessionState state); void SetSessionInfoAncoSceneState(int32_t ancoSceneState); void SetSessionInfoTime(const std::string& time); void SetSessionInfoAbilityInfo(const std::shared_ptr& abilityInfo); void SetSessionInfoWant(const std::shared_ptr& want); void SetSessionInfoPersistentId(int32_t persistentId); void SetSessionInfoCallerPersistentId(int32_t callerPersistentId); void SetSessionInfoContinueState(ContinueState state); void SetSessionInfoLockedState(bool lockedState); void SetSessionInfoIsClearSession(bool isClearSession); void SetSessionInfoAffinity(std::string affinity); void GetCloseAbilityWantAndClean(AAFwk::Want& outWant); void SetSessionInfo(const SessionInfo& info); const SessionInfo& GetSessionInfo() const; void SetScreenId(uint64_t screenId); WindowType GetWindowType() const; float GetAspectRatio() const; WSError SetAspectRatio(float ratio) override; WSError SetSessionProperty(const sptr& property) override; sptr GetSessionProperty() const; void SetSessionRect(const WSRect& rect); WSRect GetSessionRect() const; void SetSessionRequestRect(const WSRect& rect); WSRect GetSessionRequestRect() const; virtual WSError SetActive(bool active); virtual WSError UpdateRect(const WSRect& rect, SizeChangeReason reason, const std::shared_ptr& rsTransaction = nullptr); WSError UpdateDensity(); void SetShowRecent(bool showRecent); void SetSystemActive(bool systemActive); bool GetShowRecent() const; void SetOffset(float x, float y); float GetOffsetX() const; float GetOffsetY() const; void SetBounds(const WSRectF& bounds); WSRectF GetBounds(); void SetBufferAvailable(bool bufferAvailable); bool GetBufferAvailable() const; void SetNeedSnapshot(bool needSnapshot); void SetPendingSessionActivationEventListener(const NotifyPendingSessionActivationFunc& func); void SetTerminateSessionListener(const NotifyTerminateSessionFunc& func); WSError TerminateSessionNew(const sptr info, bool needStartCaller); void SetTerminateSessionListenerNew(const NotifyTerminateSessionFuncNew& func); void SetSessionExceptionListener(const NotifySessionExceptionFunc& func, bool fromJsScene); void SetSessionSnapshotListener(const NotifySessionSnapshotFunc& func); WSError TerminateSessionTotal(const sptr info, TerminateType terminateType); void SetTerminateSessionListenerTotal(const NotifyTerminateSessionFuncTotal& func); WSError Clear(); WSError SetSessionLabel(const std::string &label); void SetUpdateSessionLabelListener(const NofitySessionLabelUpdatedFunc& func); WSError SetSessionIcon(const std::shared_ptr &icon); void SetUpdateSessionIconListener(const NofitySessionIconUpdatedFunc& func); void SetSessionStateChangeListenser(const NotifySessionStateChangeFunc& func); void SetBufferAvailableChangeListener(const NotifyBufferAvailableChangeFunc& func); void UnregisterSessionChangeListeners(); void SetSessionStateChangeNotifyManagerListener(const NotifySessionStateChangeNotifyManagerFunc& func); void SetSessionInfoChangeNotifyManagerListener(const NotifySessionInfoChangeNotifyManagerFunc& func); void SetRequestFocusStatusNotifyManagerListener(const NotifyRequestFocusStatusNotifyManagerFunc& func); void SetNotifyUIRequestFocusFunc(const NotifyUIRequestFocusFunc& func); void SetNotifyUILostFocusFunc(const NotifyUILostFocusFunc& func); void SetGetStateFromManagerListener(const GetStateFromManagerFunc& func); void SetSystemConfig(const SystemSessionConfig& systemConfig); void SetSnapshotScale(const float snapshotScale); void SetBackPressedListenser(const NotifyBackPressedFunc& func); WSError ProcessBackEvent(); // send back event to session_stage WSError MarkProcessed(int32_t eventId) override; sptr GetScenePersistence() const; void SetParentSession(const sptr& session); sptr GetParentSession() const; void BindDialogToParentSession(const sptr& session); void RemoveDialogToParentSession(const sptr& session); std::vector> GetDialogVector() const; void ClearDialogVector(); WSError NotifyDestroy(); WSError NotifyCloseExistPipWindow(); void SetPendingSessionToForegroundListener(const NotifyPendingSessionToForegroundFunc& func); WSError PendingSessionToForeground(); void SetPendingSessionToBackgroundForDelegatorListener(const NotifyPendingSessionToBackgroundForDelegatorFunc& func); WSError PendingSessionToBackgroundForDelegator(); void SetSessionFocusableChangeListener(const NotifySessionFocusableChangeFunc& func); void SetSessionTouchableChangeListener(const NotifySessionTouchableChangeFunc& func); void SetClickListener(const NotifyClickFunc& func); void NotifySessionFocusableChange(bool isFocusable); void NotifySessionTouchableChange(bool touchable); void NotifyClick(); void NotifyRequestFocusStatusNotifyManager(bool isFocused, bool byForeground = true); void NotifyUIRequestFocus(); virtual void NotifyUILostFocus(); bool GetStateFromManager(const ManagerState key); void PresentFoucusIfNeed(int32_t pointerAcrion); WSError UpdateFocus(bool isFocused); WSError NotifyFocusStatus(bool isFocused); WSError UpdateWindowMode(WindowMode mode); WSError SetSystemSceneBlockingFocus(bool blocking); bool GetBlockingFocus() const; WSError SetFocusable(bool isFocusable); bool NeedNotify() const; void SetNeedNotify(bool needNotify); bool GetFocusable() const; WSError SetTouchable(bool touchable); bool GetTouchable() const; virtual void SetSystemTouchable(bool touchable); bool GetSystemTouchable() const; virtual WSError SetVisible(bool isVisible); bool GetVisible() const; WSError SetVisibilityState(WindowVisibilityState state); WindowVisibilityState GetVisibilityState() const; WSError SetDrawingContentState(bool isRSDrawing); bool GetDrawingContentState() const; WSError SetBrightness(float brightness); float GetBrightness() const; void NotifyOccupiedAreaChangeInfo(sptr info); void SetSessionInfoLockedStateChangeListener(const NotifySessionInfoLockedStateChangeFunc& func); void NotifySessionInfoLockedStateChange(bool lockedState); bool IsSessionValid() const; bool IsActive() const; bool IsSystemActive() const; bool IsSystemSession() const; bool IsTerminated() const; bool IsSessionForeground() const; sptr dialogTargetToken_ = nullptr; int32_t GetWindowId() const; void SetAppIndex(const int32_t appIndex); int32_t GetAppIndex() const; void SetCallingPid(int32_t id); void SetCallingUid(int32_t id); int32_t GetCallingPid() const; int32_t GetCallingUid() const; void SetAbilityToken(sptr token); sptr GetAbilityToken() const; WindowMode GetWindowMode(); virtual void SetZOrder(uint32_t zOrder); uint32_t GetZOrder() const; void SetUINodeId(uint32_t uiNodeId); uint32_t GetUINodeId() const; virtual void SetFloatingScale(float floatingScale); float GetFloatingScale() const; void SetSCBKeepKeyboard(bool scbKeepKeyboardFlag); bool GetSCBKeepKeyboardFlag() const; virtual void SetScale(float scaleX, float scaleY, float pivotX, float pivotY); float GetScaleX() const; float GetScaleY() const; float GetPivotX() const; float GetPivotY() const; void SetNotifyCallingSessionUpdateRectFunc(const NotifyCallingSessionUpdateRectFunc& func); void NotifyCallingSessionUpdateRect(); void SetNotifyCallingSessionForegroundFunc(const NotifyCallingSessionForegroundFunc& func); void NotifyCallingSessionForeground(); void SetNotifyCallingSessionBackgroundFunc(const NotifyCallingSessionBackgroundFunc& func); void SetRaiseToAppTopForPointDownFunc(const NotifyRaiseToTopForPointDownFunc& func); void NotifyCallingSessionBackground(); void NotifyScreenshot(); void RemoveLifeCycleTask(const LifeCycleTaskType &taskType); void PostLifeCycleTask(Task &&task, const std::string &name, const LifeCycleTaskType &taskType); WSError UpdateMaximizeMode(bool isMaximize); void NotifySessionForeground(uint32_t reason, bool withAnimation); void NotifySessionBackground(uint32_t reason, bool withAnimation, bool isFromInnerkits); void HandlePointDownDialog(); bool CheckDialogOnForeground(); void PresentFocusIfPointDown(); void ResetSnapshot(); std::shared_ptr GetSnapshotPixelMap(const float oriScale = 1.0f, const float newScale = 1.0f); virtual std::vector GetTouchHotAreas() const { return std::vector(); } virtual void SetTouchHotAreas(const std::vector& touchHotAreas); void SetVpr(float vpr) { vpr_ = vpr; } bool operator==(const Session* session) const { if (session == nullptr) { return false; } return (persistentId_ == session->persistentId_ && callingPid_ == session->callingPid_); } bool operator!=(const Session* session) const { return !this->operator==(session); } virtual void HandleStyleEvent(MMI::WindowArea area) {}; WSError SetPointerStyle(MMI::WindowArea area); const char* DumpPointerWindowArea(MMI::WindowArea area) const; WSRectF UpdateHotRect(const WSRect& rect); WSError RaiseToAppTopForPointDown(); void NotifyForegroundInteractiveStatus(bool interactive); WSError UpdateTitleInTargetPos(bool isShow, int32_t height); void SetNotifySystemSessionPointerEventFunc(const NotifySystemSessionPointerEventFunc& func); void SetNotifySystemSessionKeyEventFunc(const NotifySystemSessionKeyEventFunc& func); bool IsSystemInput(); bool GetForegroundInteractiveStatus() const; virtual void SetForegroundInteractiveStatus(bool interactive); protected: class SessionLifeCycleTask : public virtual RefBase { public: SessionLifeCycleTask(const Task &task, const std::string &name, const LifeCycleTaskType &type) : task(task), name(name), type(type) {} Task task; const std::string name; LifeCycleTaskType type; std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now(); bool running = false; }; void StartLifeCycleTask(sptr lifeCycleTask); void GeneratePersistentId(bool isExtension, int32_t persistentId); virtual void UpdateSessionState(SessionState state); void NotifySessionStateChange(const SessionState& state); void UpdateSessionTouchable(bool touchable); WSRectF UpdateTopBottomArea(const WSRectF& rect, MMI::WindowArea area); WSRectF UpdateLeftRightArea(const WSRectF& rect, MMI::WindowArea area); WSRectF UpdateInnerAngleArea(const WSRectF& rect, MMI::WindowArea area); void UpdatePointerArea(const WSRect& rect); bool CheckPointerEventDispatch(const std::shared_ptr& pointerEvent) const; bool CheckKeyEventDispatch(const std::shared_ptr& keyEvent) const; bool IsTopDialog() const; bool NeedSystemPermission(WindowType type); void HandlePointDownDialog(int32_t pointAction); void PostTask(Task&& task, const std::string& name = "sessionTask", int64_t delayTime = 0); void PostExportTask(Task&& task, const std::string& name = "sessionExportTask", int64_t delayTime = 0); template> Return PostSyncTask(SyncTask&& task, const std::string& name = "sessionTask") { Return ret; if (!handler_ || handler_->GetEventRunner()->IsCurrentRunnerThread()) { StartTraceForSyncTask(name); ret = task(); FinishTraceForSyncTask(); return ret; } auto syncTask = [&ret, &task, name]() { StartTraceForSyncTask(name); ret = task(); FinishTraceForSyncTask(); }; handler_->PostSyncTask(std::move(syncTask), name, AppExecFwk::EventQueue::Priority::IMMEDIATE); return ret; } int32_t persistentId_ = INVALID_SESSION_ID; SessionState state_ = SessionState::STATE_DISCONNECT; SessionInfo sessionInfo_; std::recursive_mutex sessionInfoMutex_; std::shared_ptr surfaceNode_; std::shared_ptr leashWinSurfaceNode_; std::shared_ptr snapshot_; sptr sessionStage_; std::mutex lifeCycleTaskQueueMutex_; std::list> lifeCycleTaskQueue_; bool isActive_ = false; bool isSystemActive_ = false; WSRect winRect_; WSRectF bounds_; float offsetX_ = 0.0f; float offsetY_ = 0.0f; bool isVisible_ = false; NotifyPendingSessionActivationFunc pendingSessionActivationFunc_; NotifySessionStateChangeFunc sessionStateChangeFunc_; NotifyBufferAvailableChangeFunc bufferAvailableChangeFunc_; NotifySessionInfoChangeNotifyManagerFunc sessionInfoChangeNotifyManagerFunc_; NotifySessionStateChangeNotifyManagerFunc sessionStateChangeNotifyManagerFunc_; NotifyRequestFocusStatusNotifyManagerFunc requestFocusStatusNotifyManagerFunc_; NotifyUIRequestFocusFunc requestFocusFunc_; NotifyUILostFocusFunc lostFocusFunc_; GetStateFromManagerFunc getStateFromManagerFunc_; NotifyBackPressedFunc backPressedFunc_; NotifySessionFocusableChangeFunc sessionFocusableChangeFunc_; NotifySessionTouchableChangeFunc sessionTouchableChangeFunc_; NotifyClickFunc clickFunc_; NotifyTerminateSessionFunc terminateSessionFunc_; NotifyTerminateSessionFuncNew terminateSessionFuncNew_; NotifyTerminateSessionFuncTotal terminateSessionFuncTotal_; NofitySessionLabelUpdatedFunc updateSessionLabelFunc_; NofitySessionIconUpdatedFunc updateSessionIconFunc_; std::shared_ptr sessionExceptionFunc_; std::shared_ptr jsSceneSessionExceptionFunc_; NotifySessionSnapshotFunc notifySessionSnapshotFunc_; NotifyPendingSessionToForegroundFunc pendingSessionToForegroundFunc_; NotifyPendingSessionToBackgroundForDelegatorFunc pendingSessionToBackgroundForDelegatorFunc_; NotifyCallingSessionUpdateRectFunc notifyCallingSessionUpdateRectFunc_; NotifyCallingSessionForegroundFunc notifyCallingSessionForegroundFunc_; NotifyCallingSessionBackgroundFunc notifyCallingSessionBackgroundFunc_; NotifyRaiseToTopForPointDownFunc raiseToTopForPointDownFunc_; NotifySessionInfoLockedStateChangeFunc sessionInfoLockedStateChangeFunc_; NotifySystemSessionPointerEventFunc systemSessionPointerEventFunc_; NotifySystemSessionKeyEventFunc systemSessionKeyEventFunc_; SystemSessionConfig systemConfig_; bool needSnapshot_ = false; float snapshotScale_ = 0.5; sptr scenePersistence_ = nullptr; uint32_t zOrder_ = 0; uint32_t uiNodeId_ = 0; bool isFocused_ = false; bool blockingFocus_ {false}; float aspectRatio_ = 0.0f; std::map windowAreas_; bool isTerminating = false; float floatingScale_ = 1.0f; bool scbKeepKeyboardFlag_ = false; bool isDirty_ = false; float scaleX_ = 1.0f; float scaleY_ = 1.0f; float pivotX_ = 0.0f; float pivotY_ = 0.0f; mutable std::mutex dialogVecMutex_; std::vector> dialogVec_; sptr parentSession_; mutable std::mutex pointerEventMutex_; mutable std::mutex keyEventMutex_; private: void HandleDialogForeground(); void HandleDialogBackground(); void NotifyPointerEventToRs(int32_t pointAction); void NotifySessionInfoChange(); WSError HandleSubWindowClick(int32_t action); template bool RegisterListenerLocked(std::vector>& holder, const std::shared_ptr& listener); template bool UnregisterListenerLocked(std::vector>& holder, const std::shared_ptr& listener); template using EnableIfSame = typename std::enable_if, Ret>::type; template inline EnableIfSame>> GetListeners() { std::vector> lifecycleListeners; { std::lock_guard lock(lifecycleListenersMutex_); for (auto& listener : lifecycleListeners_) { lifecycleListeners.push_back(listener); } } return lifecycleListeners; } std::recursive_mutex lifecycleListenersMutex_; std::vector> lifecycleListeners_; sptr windowEventChannel_; std::shared_ptr handler_; std::shared_ptr mainHandler_; std::shared_ptr exportHandler_; mutable std::shared_mutex propertyMutex_; sptr property_; bool showRecent_ = false; bool bufferAvailable_ = false; WSRect preRect_; int32_t callingPid_ = -1; int32_t callingUid_ = -1; int32_t appIndex_ = { 0 }; std::string callingBundleName_ { "unknow" }; bool isRSVisible_ {false}; WindowVisibilityState visibilityState_ { WINDOW_LAYER_STATE_MAX}; bool needNotify_ {true}; bool isRSDrawing_ {false}; sptr abilityToken_ = nullptr; float vpr_ { 1.5f }; bool systemTouchable_ { true }; std::atomic_bool foregroundInteractiveStatus_ { true }; }; } // namespace OHOS::Rosen #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_H