/* * 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 "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" namespace OHOS::MMI { class PointerEvent; class KeyEvent; class AxisEvent; } // namespace OHOS::MMI namespace OHOS::Media { class PixelMap; } // namespace OHOS::Media namespace OHOS::Rosen { class RSSurfaceNode; using NotifyPendingSessionActivationFunc = std::function; using NotifySessionStateChangeFunc = std::function; using NotifySessionStateChangeNotifyManagerFunc = 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 NotifySessionExceptionFunc = std::function; using NotifyPendingSessionToForegroundFunc = std::function; using NotifyPendingSessionToBackgroundForDelegatorFunc = std::function; using NotifyCallingSessionForegroundFunc = std::function; using NotifyCallingSessionBackgroundFunc = std::function; class ILifecycleListener { public: virtual void OnConnect() = 0; virtual void OnForeground() = 0; virtual void OnBackground() = 0; virtual void OnDisconnect() = 0; virtual void OnExtensionDied() = 0; }; class Session : public SessionStub, public virtual RefBase { public: explicit Session(const SessionInfo& info) : sessionInfo_(info) {} virtual ~Session() = default; int32_t GetPersistentId() const; int32_t GetParentPersistentId() const; void SetParentPersistentId(int32_t parentId); void SetSessionRect(const WSRect& rect); std::shared_ptr GetSurfaceNode() const; std::shared_ptr GetSnapshot() const; std::shared_ptr Snapshot(); SessionState GetSessionState() const; SessionInfo& GetSessionInfo(); sptr GetSessionProperty() const; WSRect GetSessionRect() const; WindowType GetWindowType() const; float GetAspectRatio() const; WSError SetAspectRatio(float ratio) override; void SetWindowSessionProperty(const sptr& property); sptr GetWindowSessionProperty() const; void SetSessionRequestRect(const WSRect& rect); WSRect GetSessionRequestRect() const; virtual WSError SetActive(bool active); virtual WSError UpdateRect(const WSRect& rect, SizeChangeReason reason); void SetShowRecent(bool showRecent); bool GetShowRecent() const; void SetBufferAvailable(bool bufferAvailable); bool GetBufferAvailable() const; WSError Connect(const sptr& sessionStage, const sptr& eventChannel, const std::shared_ptr& surfaceNode, SystemSessionConfig& systemConfig, sptr property = nullptr, sptr token = nullptr) override; WSError ConnectImpl(const sptr& sessionStage, const sptr& eventChannel, const std::shared_ptr& surfaceNode, SystemSessionConfig& systemConfig, sptr property = nullptr, sptr token = nullptr); WSError Foreground(sptr property) override; WSError Background() override; WSError Disconnect() override; WSError OnSessionEvent(SessionEvent event) override; WSError UpdateWindowSessionProperty(sptr property) override; WSError OnNeedAvoid(bool status) override; WSError TransferAbilityResult(uint32_t resultCode, const AAFwk::Want& want) override; WSError TransferExtensionData(const AAFwk::WantParams& wantParams) override; void NotifyRemoteReady() override; void NotifyExtensionDied() override; void NotifyConnect(); void NotifyForeground(); void NotifyBackground(); void NotifyDisconnect(); virtual WSError TransferPointerEvent(const std::shared_ptr& pointerEvent); virtual WSError TransferKeyEvent(const std::shared_ptr& keyEvent); WSError TransferKeyEventForConsumed(const std::shared_ptr& keyEvent, bool& isConsumed); WSError TransferFocusActiveEvent(bool isFocusActive); WSError TransferFocusStateEvent(bool focusState); bool RegisterLifecycleListener(const std::shared_ptr& listener); bool UnregisterLifecycleListener(const std::shared_ptr& listener); void SetPendingSessionActivationEventListener(const NotifyPendingSessionActivationFunc& func); WSError PendingSessionActivation(const sptr info) override; WSError TerminateSession(const sptr info) override; void SetTerminateSessionListener(const NotifyTerminateSessionFunc& func); WSError TerminateSessionNew(const sptr info, bool needStartCaller); void SetTerminateSessionListenerNew(const NotifyTerminateSessionFuncNew& func); void SetSessionExceptionListener(const NotifySessionExceptionFunc& func); WSError NotifySessionException(const sptr info) override; WSError TerminateSessionTotal(const sptr info, TerminateType terminateType); void SetTerminateSessionListenerTotal(const NotifyTerminateSessionFuncTotal& func); WSError Clear(); void SetSessionStateChangeListenser(const NotifySessionStateChangeFunc& func); void SetSessionStateChangeNotifyManagerListener(const NotifySessionStateChangeNotifyManagerFunc& func); void NotifySessionStateChange(const SessionState& state); WSError UpdateActiveStatus(bool isActive) override; // update active status from session_stage WSError RaiseToAppTop() override; WSError UpdateSessionRect(const WSRect& rect, const SizeChangeReason& reason) override; WSError CreateAndConnectSpecificSession(const sptr& sessionStage, const sptr& eventChannel, const std::shared_ptr& surfaceNode, sptr property, int32_t& persistentId, sptr& session, sptr token = nullptr) override; WSError DestroyAndDisconnectSpecificSession(const int32_t& persistentId) override; void SetSystemConfig(const SystemSessionConfig& systemConfig); void SetBackPressedListenser(const NotifyBackPressedFunc& func); WSError ProcessBackEvent(); // send back event to session_stage WSError RequestSessionBack() override; // receive back request from session_stage WSError MarkProcessed(int32_t eventId) override; sptr GetScenePersistence() const; void SetSessionContinueState(const ContinueState& continueState); void SetParentSession(const sptr& session); void BindDialogToParentSession(const sptr& session); void RemoveDialogToParentSession(const sptr& session); std::vector> GetDialogVector() const; void NotifyTouchDialogTarget(); WSError NotifyDestroy(); 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(); WSError UpdateFocus(bool isFocused); WSError UpdateWindowMode(WindowMode mode); WSError SetFocusable(bool isFocusable); bool NeedNotify() const; void SetNeedNotify(bool needNotify); bool GetFocusable() const; WSError SetTouchable(bool touchable); bool GetTouchable() const; WSError SetVisible(bool isVisible); bool GetVisible() const; WSError SetGlobalMaximizeMode(MaximizeMode mode) override; WSError GetGlobalMaximizeMode(MaximizeMode& mode) override; AvoidArea GetAvoidAreaByType(AvoidAreaType type) override; WSError SetBrightness(float brightness); float GetBrightness() const; void NotifyOccupiedAreaChangeInfo(sptr info); bool IsSessionValid() const; bool IsActive() 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; WSError UpdateWindowAnimationFlag(bool needDefaultAnimationFlag) override; WSError UpdateWindowSceneAfterCustomAnimation(bool isAdd) override; void SetNotifyCallingSessionForegroundFunc(const NotifyCallingSessionForegroundFunc& func); void NotifyCallingSessionForeground(); void SetNotifyCallingSessionBackgroundFunc(const NotifyCallingSessionBackgroundFunc& func); void NotifyCallingSessionBackground(); void NotifyScreenshot(); virtual std::vector GetTouchHotAreas() const { return std::vector(); } WSError RaiseAboveTarget(int32_t subWindowId) override; protected: void GeneratePersistentId(const bool isExtension, const SessionInfo& sessionInfo); void UpdateSessionState(SessionState state); void UpdateSessionFocusable(bool isFocusable); void UpdateSessionTouchable(bool touchable); int32_t persistentId_ = INVALID_SESSION_ID; SessionState state_ = SessionState::STATE_DISCONNECT; SessionInfo sessionInfo_; sptr property_; std::shared_ptr surfaceNode_; std::shared_ptr snapshot_; sptr sessionStage_; bool isActive_ = false; WSRect winRect_; NotifyPendingSessionActivationFunc pendingSessionActivationFunc_; NotifySessionStateChangeFunc sessionStateChangeFunc_; NotifySessionStateChangeNotifyManagerFunc sessionStateChangeNotifyManagerFunc_; NotifyBackPressedFunc backPressedFunc_; NotifySessionFocusableChangeFunc sessionFocusableChangeFunc_; NotifySessionTouchableChangeFunc sessionTouchableChangeFunc_; NotifyClickFunc clickFunc_; NotifyTerminateSessionFunc terminateSessionFunc_; NotifyTerminateSessionFuncNew terminateSessionFuncNew_; NotifyTerminateSessionFuncTotal terminateSessionFuncTotal_; std::vector> sessionExceptionFuncs_; NotifyPendingSessionToForegroundFunc pendingSessionToForegroundFunc_; NotifyPendingSessionToBackgroundForDelegatorFunc pendingSessionToBackgroundForDelegatorFunc_; NotifyCallingSessionForegroundFunc notifyCallingSessionForegroundFunc_; NotifyCallingSessionBackgroundFunc notifyCallingSessionBackgroundFunc_; SystemSessionConfig systemConfig_; sptr scenePersistence_ = nullptr; uint32_t zOrder_ = 0; uint32_t uiNodeId_ = 0; bool isFocused_ = false; float aspectRatio_ = 0.0f; private: bool CheckDialogOnForeground(); void HandleDialogForeground(); void HandleDialogBackground(); 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(mutex_); for (auto& listener : lifecycleListeners_) { lifecycleListeners.push_back(listener); } } return lifecycleListeners; } std::recursive_mutex mutex_; std::vector> lifecycleListeners_; sptr windowEventChannel_ = nullptr; bool showRecent_ = false; bool bufferAvailable_ = false; std::vector> dialogVec_; sptr parentSession_; int32_t callingPid_ = { 0 }; int32_t callingUid_ = { 0 }; int32_t appIndex_ = { 0 }; std::string callingBundleName_ { "unknow" }; bool isVisible_ {false}; bool needNotify_ {true}; sptr abilityToken_ = nullptr; }; } // namespace OHOS::Rosen #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_H