• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_ROSEN_WINDOW_SCENE_SESSION_H
17 #define OHOS_ROSEN_WINDOW_SCENE_SESSION_H
18 #include <list>
19 #include <mutex>
20 #include <shared_mutex>
21 #include <vector>
22 
23 #include <event_handler.h>
24 
25 #include "accessibility_element_info.h"
26 #include "interfaces/include/ws_common.h"
27 #include "session/container/include/zidl/session_stage_interface.h"
28 #include "session/host/include/zidl/session_stub.h"
29 #include "session/host/include/scene_persistence.h"
30 #include "wm_common.h"
31 #include "occupied_area_change_info.h"
32 #include "window_visibility_info.h"
33 #include "pattern_detach_callback_interface.h"
34 
35 namespace OHOS::MMI {
36 class PointerEvent;
37 class KeyEvent;
38 class AxisEvent;
39 enum class WindowArea;
40 } // namespace OHOS::MMI
41 
42 namespace OHOS::Media {
43 class PixelMap;
44 } // namespace OHOS::Media
45 
46 namespace OHOS::Rosen {
47 class RSSurfaceNode;
48 class RSTransaction;
49 class RSSyncTransactionController;
50 using NotifySessionRectChangeFunc = std::function<void(const WSRect& rect, const SizeChangeReason& reason)>;
51 using NotifyPendingSessionActivationFunc = std::function<void(SessionInfo& info)>;
52 using NotifyChangeSessionVisibilityWithStatusBarFunc = std::function<void(SessionInfo& info, const bool visible)>;
53 using NotifySessionStateChangeFunc = std::function<void(const SessionState& state)>;
54 using NotifyBufferAvailableChangeFunc = std::function<void(const bool isAvailable)>;
55 using NotifySessionStateChangeNotifyManagerFunc = std::function<void(int32_t persistentId, const SessionState& state)>;
56 using NotifyRequestFocusStatusNotifyManagerFunc =
57     std::function<void(int32_t persistentId, const bool isFocused, const bool byForeground, FocusChangeReason reason)>;
58 using NotifyBackPressedFunc = std::function<void(const bool needMoveToBackground)>;
59 using NotifySessionFocusableChangeFunc = std::function<void(const bool isFocusable)>;
60 using NotifySessionTouchableChangeFunc = std::function<void(const bool touchable)>;
61 using NotifyClickFunc = std::function<void()>;
62 using NotifyTerminateSessionFunc = std::function<void(const SessionInfo& info)>;
63 using NotifyTerminateSessionFuncNew =
64     std::function<void(const SessionInfo& info, bool needStartCaller, bool isFromBroker)>;
65 using NotifyTerminateSessionFuncTotal = std::function<void(const SessionInfo& info, TerminateType terminateType)>;
66 using NofitySessionLabelUpdatedFunc = std::function<void(const std::string& label)>;
67 using NofitySessionIconUpdatedFunc = std::function<void(const std::string& iconPath)>;
68 using NotifySessionExceptionFunc = std::function<void(const SessionInfo& info, bool needRemoveSession, bool startFail)>;
69 using NotifySessionSnapshotFunc = std::function<void(const int32_t& persistentId)>;
70 using NotifyPendingSessionToForegroundFunc = std::function<void(const SessionInfo& info)>;
71 using NotifyPendingSessionToBackgroundForDelegatorFunc = std::function<void(const SessionInfo& info,
72     bool shouldBackToCaller)>;
73 using NotifyRaiseToTopForPointDownFunc = std::function<void()>;
74 using NotifyUIRequestFocusFunc = std::function<void()>;
75 using NotifyUILostFocusFunc = std::function<void()>;
76 using NotifySessionInfoLockedStateChangeFunc = std::function<void(const bool lockedState)>;
77 using GetStateFromManagerFunc = std::function<bool(const ManagerState key)>;
78 using NotifySystemSessionPointerEventFunc = std::function<void(std::shared_ptr<MMI::PointerEvent> pointerEvent)>;
79 using NotifySessionInfoChangeNotifyManagerFunc = std::function<void(int32_t persistentid)>;
80 using NotifySystemSessionKeyEventFunc = std::function<bool(std::shared_ptr<MMI::KeyEvent> keyEvent,
81     bool isPreImeEvent)>;
82 using NotifyContextTransparentFunc = std::function<void()>;
83 using NotifyFrameLayoutFinishFunc = std::function<void()>;
84 using AcquireRotateAnimationConfigFunc = std::function<void(RotateAnimationConfig& config)>;
85 
86 class ILifecycleListener {
87 public:
OnActivation()88     virtual void OnActivation() {}
OnConnect()89     virtual void OnConnect() {}
OnForeground()90     virtual void OnForeground() {}
OnBackground()91     virtual void OnBackground() {}
OnDisconnect()92     virtual void OnDisconnect() {}
OnLayoutFinished()93     virtual void OnLayoutFinished() {}
OnRemoveBlank()94     virtual void OnRemoveBlank() {}
OnDrawingCompleted()95     virtual void OnDrawingCompleted() {}
OnExtensionDied()96     virtual void OnExtensionDied() {}
OnExtensionTimeout(int32_t errorCode)97     virtual void OnExtensionTimeout(int32_t errorCode) {}
OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo & info,int64_t uiExtensionIdLevel)98     virtual void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
99         int64_t uiExtensionIdLevel) {}
100 };
101 
102 enum class LifeCycleTaskType : uint32_t {
103     START,
104     STOP
105 };
106 
107 enum class DetectTaskState : uint32_t {
108     NO_TASK,
109     ATTACH_TASK,
110     DETACH_TASK
111 };
112 
113 struct DetectTaskInfo {
114     WindowMode taskWindowMode = WindowMode::WINDOW_MODE_UNDEFINED;
115     DetectTaskState taskState = DetectTaskState::NO_TASK;
116 };
117 
118 class Session : public SessionStub {
119 public:
120     using Task = std::function<void()>;
121     explicit Session(const SessionInfo& info);
122     virtual ~Session();
123     bool isKeyboardPanelEnabled_ = false;
124     void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler>& handler,
125         const std::shared_ptr<AppExecFwk::EventHandler>& exportHandler = nullptr);
126 
127     virtual WSError ConnectInner(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
128         const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
129         sptr<WindowSessionProperty> property = nullptr, sptr<IRemoteObject> token = nullptr,
130         int32_t pid = -1, int32_t uid = -1, const std::string& identityToken = "");
131     WSError Reconnect(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
132         const std::shared_ptr<RSSurfaceNode>& surfaceNode, sptr<WindowSessionProperty> property = nullptr,
133         sptr<IRemoteObject> token = nullptr, int32_t pid = -1, int32_t uid = -1);
134     WSError Foreground(sptr<WindowSessionProperty> property, bool isFromClient = false,
135         const std::string& identityToken = "") override;
136     WSError Background(bool isFromClient = false, const std::string& identityToken = "") override;
137     WSError Disconnect(bool isFromClient = false, const std::string& identityToken = "") override;
138     WSError Show(sptr<WindowSessionProperty> property) override;
139     WSError Hide() override;
140     WSError DrawingCompleted() override;
141     void ResetSessionConnectState();
142 
143     bool RegisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener);
144     bool UnregisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener);
145 
146     /*
147      * Callbacks for ILifecycleListener
148      */
149     void NotifyActivation();
150     void NotifyConnect();
151     void NotifyForeground();
152     void NotifyBackground();
153     void NotifyDisconnect();
154     void NotifyLayoutFinished();
155     void NotifyRemoveBlank();
156     void NotifyExtensionDied() override;
157     void NotifyExtensionTimeout(int32_t errorCode) override;
158     void NotifyTransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
159         int64_t uiExtensionIdLevel) override;
160 
161     virtual WSError TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
162         bool needNotifyClient = true);
163     virtual WSError TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent);
164 
NotifyClientToUpdateRect(const std::string & updateReason,std::shared_ptr<RSTransaction> rsTransaction)165     virtual WSError NotifyClientToUpdateRect(const std::string& updateReason,
166         std::shared_ptr<RSTransaction> rsTransaction) { return WSError::WS_OK; }
167     WSError TransferBackPressedEventForConsumed(bool& isConsumed);
168     WSError TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed,
169         bool isPreImeEvent = false);
170     WSError TransferFocusActiveEvent(bool isFocusActive);
171     WSError TransferFocusStateEvent(bool focusState);
UpdateAvoidArea(const sptr<AvoidArea> & avoidArea,AvoidAreaType type)172     virtual WSError UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type) { return WSError::WS_OK; }
173 
174     int32_t GetPersistentId() const;
175     std::shared_ptr<RSSurfaceNode> GetSurfaceNode() const;
176     void SetLeashWinSurfaceNode(std::shared_ptr<RSSurfaceNode> leashWinSurfaceNode);
177     std::shared_ptr<RSSurfaceNode> GetLeashWinSurfaceNode() const;
178     std::shared_ptr<Media::PixelMap> GetSnapshot() const;
179     std::shared_ptr<Media::PixelMap> Snapshot(bool runInFfrt = false, const float scaleParam = 0.0f) const;
180     void SaveSnapshot(bool useFfrt);
181     SessionState GetSessionState() const;
182     virtual void SetSessionState(SessionState state);
183     void SetSessionInfoAncoSceneState(int32_t ancoSceneState);
184     void SetSessionInfoTime(const std::string& time);
185     void SetSessionInfoAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo>& abilityInfo);
186     void SetSessionInfoWant(const std::shared_ptr<AAFwk::Want>& want);
187     void SetSessionInfoProcessOptions(const std::shared_ptr<AAFwk::ProcessOptions>& processOptions);
188     void ResetSessionInfoResultCode();
189     void SetSessionInfoPersistentId(int32_t persistentId);
190     void SetSessionInfoCallerPersistentId(int32_t callerPersistentId);
191     void SetSessionInfoContinueState(ContinueState state);
192     void SetSessionInfoLockedState(bool lockedState);
193     void SetSessionInfoIsClearSession(bool isClearSession);
194     void SetSessionInfoAffinity(std::string affinity);
195     void GetCloseAbilityWantAndClean(AAFwk::Want& outWant);
196     void SetSessionInfo(const SessionInfo& info);
197     const SessionInfo& GetSessionInfo() const;
198     void SetScreenId(uint64_t screenId);
199     void SetScreenIdOnServer(uint64_t screenId);
200     WindowType GetWindowType() const;
201     float GetAspectRatio() const;
202     WSError SetAspectRatio(float ratio) override;
203     WSError SetSessionProperty(const sptr<WindowSessionProperty>& property);
204     sptr<WindowSessionProperty> GetSessionProperty() const;
205     void SetSessionRect(const WSRect& rect);
206     WSRect GetSessionRect() const;
207     WSRect GetSessionGlobalRect() const;
208     WMError GetGlobalScaledRect(Rect& globalScaledRect) override;
209     void SetSessionGlobalRect(const WSRect& rect);
210     void SetSessionRequestRect(const WSRect& rect);
211     WSRect GetSessionRequestRect() const;
212     std::string GetWindowName() const;
213     WSRect GetLastLayoutRect() const;
214     WSRect GetLayoutRect() const;
215 
216     virtual WSError SetActive(bool active);
217     virtual WSError UpdateSizeChangeReason(SizeChangeReason reason);
GetSizeChangeReason()218     SizeChangeReason GetSizeChangeReason() const { return reason_; }
219     virtual WSError UpdateRect(const WSRect& rect, SizeChangeReason reason,
220         const std::string& updateReason, const std::shared_ptr<RSTransaction>& rsTransaction = nullptr);
221     WSError UpdateDensity();
222     WSError UpdateOrientation();
223 
224     void SetShowRecent(bool showRecent);
225     void SetSystemActive(bool systemActive);
226     bool GetShowRecent() const;
227     void SetOffset(float x, float y);
228     float GetOffsetX() const;
229     float GetOffsetY() const;
230     void SetBounds(const WSRectF& bounds);
231     WSRectF GetBounds();
232     void SetRotation(Rotation rotation);
233     Rotation GetRotation() const;
234     void SetBufferAvailable(bool bufferAvailable);
235     bool GetBufferAvailable() const;
236     void SetNeedSnapshot(bool needSnapshot);
237     virtual void SetExitSplitOnBackground(bool isExitSplitOnBackground);
238     virtual bool IsExitSplitOnBackground() const;
NeedStartingWindowExitAnimation()239     virtual bool NeedStartingWindowExitAnimation() const { return true; }
240 
241     void SetPendingSessionActivationEventListener(const NotifyPendingSessionActivationFunc& func);
242     void SetChangeSessionVisibilityWithStatusBarEventListener(
243         const NotifyChangeSessionVisibilityWithStatusBarFunc& func);
244     void SetTerminateSessionListener(const NotifyTerminateSessionFunc& func);
245     WSError TerminateSessionNew(const sptr<AAFwk::SessionInfo> info, bool needStartCaller, bool isFromBroker);
246     void SetTerminateSessionListenerNew(const NotifyTerminateSessionFuncNew& func);
247     void SetSessionExceptionListener(const NotifySessionExceptionFunc& func, bool fromJsScene);
248     void SetSessionSnapshotListener(const NotifySessionSnapshotFunc& func);
249     WSError TerminateSessionTotal(const sptr<AAFwk::SessionInfo> info, TerminateType terminateType);
250     void SetTerminateSessionListenerTotal(const NotifyTerminateSessionFuncTotal& func);
251     WSError Clear(bool needStartCaller = false);
252     WSError SetSessionLabel(const std::string &label);
253     void SetUpdateSessionLabelListener(const NofitySessionLabelUpdatedFunc& func);
254     WSError SetSessionIcon(const std::shared_ptr<Media::PixelMap>& icon);
255     void SetUpdateSessionIconListener(const NofitySessionIconUpdatedFunc& func);
256     void SetSessionStateChangeListenser(const NotifySessionStateChangeFunc& func);
257     void SetBufferAvailableChangeListener(const NotifyBufferAvailableChangeFunc& func);
258     virtual void UnregisterSessionChangeListeners();
259     void SetSessionStateChangeNotifyManagerListener(const NotifySessionStateChangeNotifyManagerFunc& func);
260     void SetSessionInfoChangeNotifyManagerListener(const NotifySessionInfoChangeNotifyManagerFunc& func);
261     void SetRequestFocusStatusNotifyManagerListener(const NotifyRequestFocusStatusNotifyManagerFunc& func);
262     void SetNotifyUIRequestFocusFunc(const NotifyUIRequestFocusFunc& func);
263     void SetNotifyUILostFocusFunc(const NotifyUILostFocusFunc& func);
264     void SetGetStateFromManagerListener(const GetStateFromManagerFunc& func);
265 
266     void SetSystemConfig(const SystemSessionConfig& systemConfig);
267     void SetSnapshotScale(const float snapshotScale);
268     void SetBackPressedListenser(const NotifyBackPressedFunc& func);
269     virtual WSError ProcessBackEvent(); // send back event to session_stage
270     WSError MarkProcessed(int32_t eventId) override;
271 
272     sptr<ScenePersistence> GetScenePersistence() const;
273     void SetParentSession(const sptr<Session>& session);
274     sptr<Session> GetParentSession() const;
275     sptr<Session> GetMainSession();
276     void BindDialogToParentSession(const sptr<Session>& session);
277     void RemoveDialogToParentSession(const sptr<Session>& session);
278     std::vector<sptr<Session>> GetDialogVector() const;
279     void ClearDialogVector();
280     WSError NotifyDestroy();
281     WSError NotifyCloseExistPipWindow();
282 
283     void SetPendingSessionToForegroundListener(const NotifyPendingSessionToForegroundFunc& func);
284     WSError PendingSessionToForeground();
285     void SetPendingSessionToBackgroundForDelegatorListener(const NotifyPendingSessionToBackgroundForDelegatorFunc&
286         func);
287     WSError PendingSessionToBackgroundForDelegator(bool shouldBackToCaller);
288 
289     void SetSessionFocusableChangeListener(const NotifySessionFocusableChangeFunc& func);
290     void SetSessionTouchableChangeListener(const NotifySessionTouchableChangeFunc& func);
291     void SetClickListener(const NotifyClickFunc& func);
292     void NotifySessionFocusableChange(bool isFocusable);
293     void NotifySessionTouchableChange(bool touchable);
294     void NotifyClick();
295     bool GetStateFromManager(const ManagerState key);
296     virtual void PresentFoucusIfNeed(int32_t pointerAcrion);
297     virtual WSError UpdateWindowMode(WindowMode mode);
298     WSError SetCompatibleModeInPc(bool enable, bool isSupportDragInPcCompatibleMode);
299     WSError SetIsPcAppInPad(bool enable);
300     WSError SetCompatibleWindowSizeInPc(int32_t portraitWidth, int32_t portraitHeight,
301         int32_t landscapeWidth, int32_t landscapeHeight);
302     WSError SetCompatibleModeEnableInPad(bool enable);
303     bool NeedNotify() const;
304     void SetNeedNotify(bool needNotify);
305     void SetStartingBeforeVisible(bool isStartingBeforeVisible);
306     bool GetStartingBeforeVisible() const;
307     WSError SetTouchable(bool touchable);
308     bool GetTouchable() const;
309     void SetForceTouchable(bool touchable);
310     virtual void SetSystemTouchable(bool touchable);
311     bool GetSystemTouchable() const;
312     virtual WSError SetRSVisible(bool isVisible);
313     bool GetRSVisible() const;
314     WSError SetVisibilityState(WindowVisibilityState state);
315     WindowVisibilityState GetVisibilityState() const;
316     WSError SetDrawingContentState(bool isRSDrawing);
317     bool GetDrawingContentState() const;
318     WSError SetBrightness(float brightness);
319     float GetBrightness() const;
320     void NotifyOccupiedAreaChangeInfo(sptr<OccupiedAreaChangeInfo> info,
321                                       const std::shared_ptr<RSTransaction>& rsTransaction = nullptr);
322     void SetSessionInfoLockedStateChangeListener(const NotifySessionInfoLockedStateChangeFunc& func);
323     void NotifySessionInfoLockedStateChange(bool lockedState);
324     void SetContextTransparentFunc(const NotifyContextTransparentFunc& func);
325     void NotifyContextTransparent();
326     bool NeedCheckContextTransparent() const;
327     void SetAcquireRotateAnimationConfigFunc(const AcquireRotateAnimationConfigFunc& func);
328 
329     /*
330      * Window Focus
331      */
332     virtual WSError SetSystemSceneBlockingFocus(bool blocking);
333     bool GetBlockingFocus() const;
334     WSError SetFocusable(bool isFocusable);
335     bool GetFocusable() const;
336     void SetFocusedOnShow(bool focusedOnShow); // Used when creating ability
337     bool IsFocusedOnShow() const;
338     WSError SetFocusableOnShow(bool isFocusableOnShow); // Used when showing window
339     bool IsFocusableOnShow() const;
340     bool IsFocused() const;
341     bool GetFocused() const;
342     virtual WSError UpdateFocus(bool isFocused);
343     virtual void PresentFocusIfPointDown();
344     WSError RequestFocus(bool isFocused) override;
345     void NotifyRequestFocusStatusNotifyManager(bool isFocused, bool byForeground = true,
346         FocusChangeReason reason = FocusChangeReason::DEFAULT);
347     void NotifyUIRequestFocus();
348     virtual void NotifyUILostFocus();
349     WSError NotifyFocusStatus(bool isFocused);
350 
351     /*
352      * Multi Window
353      */
354     void SetIsMidScene(bool isMidScene);
355     bool GetIsMidScene() const;
356 
357     /*
358      * Keyboard Window
359      */
360     bool CheckEmptyKeyboardAvoidAreaIfNeeded() const;
361 
362     bool IsSessionValid() const;
363     bool IsActive() const;
364     bool IsSystemActive() const;
365     bool IsSystemSession() const;
366     bool IsTerminated() const;
367     bool IsSessionForeground() const;
IsAnco()368     virtual bool IsAnco() const { return false; }
SetBlankFlag(bool isAddBlank)369     virtual void SetBlankFlag(bool isAddBlank) {};
GetBlankFlag()370     virtual bool GetBlankFlag() const { return false; }
GetBufferAvailableCallbackEnable()371     virtual bool GetBufferAvailableCallbackEnable() const { return false; }
372 
373     sptr<IRemoteObject> dialogTargetToken_ = nullptr;
374     int32_t GetWindowId() const;
375     void SetAppIndex(const int32_t appIndex);
376     int32_t GetAppIndex() const;
377     void SetCallingPid(int32_t id);
378     void SetCallingUid(int32_t id);
379     int32_t GetCallingPid() const;
380     int32_t GetCallingUid() const;
381     void SetAbilityToken(sptr<IRemoteObject> token);
382     sptr<IRemoteObject> GetAbilityToken() const;
383     WindowMode GetWindowMode() const;
384 
385     /*
386      * Window ZOrder
387      */
388     virtual void SetZOrder(uint32_t zOrder);
389     uint32_t GetZOrder() const;
390     uint32_t GetLastZOrder() const;
391 
392     void SetUINodeId(uint32_t uiNodeId);
393     uint32_t GetUINodeId() const;
394     virtual void SetFloatingScale(float floatingScale);
395     float GetFloatingScale() const;
396     virtual void SetScale(float scaleX, float scaleY, float pivotX, float pivotY);
397     float GetScaleX() const;
398     float GetScaleY() const;
399     float GetPivotX() const;
400     float GetPivotY() const;
401     void SetSCBKeepKeyboard(bool scbKeepKeyboardFlag);
402     bool GetSCBKeepKeyboardFlag() const;
403 
404     void SetRaiseToAppTopForPointDownFunc(const NotifyRaiseToTopForPointDownFunc& func);
405     void SetFrameLayoutFinishListener(const NotifyFrameLayoutFinishFunc& func);
406     void NotifyScreenshot();
407     void RemoveLifeCycleTask(const LifeCycleTaskType& taskType);
408     void PostLifeCycleTask(Task &&task, const std::string& name, const LifeCycleTaskType& taskType);
409     WSError UpdateMaximizeMode(bool isMaximize);
410     void NotifySessionForeground(uint32_t reason, bool withAnimation);
411     void NotifySessionBackground(uint32_t reason, bool withAnimation, bool isFromInnerkits);
412     void HandlePointDownDialog();
413     bool CheckDialogOnForeground();
414     std::shared_ptr<Media::PixelMap> GetSnapshotPixelMap(const float oriScale = 1.0f, const float newScale = 1.0f);
GetTouchHotAreas()415     virtual std::vector<Rect> GetTouchHotAreas() const
416     {
417         return std::vector<Rect>();
418     }
419 
420     virtual void SetTouchHotAreas(const std::vector<Rect>& touchHotAreas);
421 
SetVpr(float vpr)422     void SetVpr(float vpr)
423     {
424         vpr_ = vpr;
425     }
426 
427     bool operator==(const Session* session) const
428     {
429         if (session == nullptr) {
430             return false;
431         }
432         return (persistentId_ == session->persistentId_ && callingPid_ == session->callingPid_);
433     }
434 
435     bool operator!=(const Session* session) const
436     {
437         return !this->operator==(session);
438     }
439 
HandleStyleEvent(MMI::WindowArea area)440     virtual void HandleStyleEvent(MMI::WindowArea area) {};
441     WSError SetPointerStyle(MMI::WindowArea area);
442     const char* DumpPointerWindowArea(MMI::WindowArea area) const;
443     WSRectF UpdateHotRect(const WSRect& rect);
444     WSError RaiseToAppTopForPointDown();
445 
446     virtual void NotifyForegroundInteractiveStatus(bool interactive);
447     WSError UpdateTitleInTargetPos(bool isShow, int32_t height);
448     void SetNotifySystemSessionPointerEventFunc(const NotifySystemSessionPointerEventFunc& func);
449     void SetNotifySystemSessionKeyEventFunc(const NotifySystemSessionKeyEventFunc& func);
450     bool IsSystemInput();
451     // ForegroundInteractiveStatus interface only for event use
452     bool GetForegroundInteractiveStatus() const;
453     virtual void SetForegroundInteractiveStatus(bool interactive);
454 
455     /*
456      * Window Lifecycle
457      */
458     bool IsActivatedAfterScreenLocked() const;
459     void SetIsActivatedAfterScreenLocked(bool isActivatedAfterScreenLocked);
460     void SetAttachState(bool isAttach, WindowMode windowMode = WindowMode::WINDOW_MODE_UNDEFINED);
461     bool GetAttachState() const;
462     void RegisterDetachCallback(const sptr<IPatternDetachCallback>& callback);
463 
464     SystemSessionConfig GetSystemConfig() const;
465     void RectCheckProcess();
RectCheck(uint32_t curWidth,uint32_t curHeight)466     virtual void RectCheck(uint32_t curWidth, uint32_t curHeight) {};
467     void RectSizeCheckProcess(uint32_t curWidth, uint32_t curHeight, uint32_t minWidth,
468         uint32_t minHeight, uint32_t maxFloatingWindowSize);
469     DetectTaskInfo GetDetectTaskInfo() const;
470     void SetDetectTaskInfo(const DetectTaskInfo& detectTaskInfo);
471     WSError GetUIContentRemoteObj(sptr<IRemoteObject>& uiContentRemoteObj);
472     void CreateWindowStateDetectTask(bool isAttach, WindowMode windowMode);
473     void RegisterIsScreenLockedCallback(const std::function<bool()>& callback);
474     std::string GetWindowDetectTaskName() const;
475     void RemoveWindowDetectTask();
476     WSError SwitchFreeMultiWindow(bool enable);
GetCustomDecorHeight()477     virtual int32_t GetCustomDecorHeight()
478     {
479         return 0;
480     };
CheckGetAvoidAreaAvailable(AvoidAreaType type)481     virtual bool CheckGetAvoidAreaAvailable(AvoidAreaType type) { return true; }
482 
483     virtual bool IsVisibleForeground() const;
484     void SetIsStarting(bool isStarting);
485     void SetUIStateDirty(bool dirty);
486     void SetMainSessionUIStateDirty(bool dirty);
487     bool GetUIStateDirty() const;
488     void ResetDirtyFlags();
489     static bool IsScbCoreEnabled();
490     static void SetScbCoreEnabled(bool enabled);
IsNeedSyncScenePanelGlobalPosition()491     virtual bool IsNeedSyncScenePanelGlobalPosition() { return true; }
492 
493     /*
494      * Window Layout
495      */
496     void SetClientRect(const WSRect& rect);
497     WSRect GetClientRect() const;
498 
499     /*
500      * Starting Window
501      */
502     void SetUseStartingWindowAboveLocked(bool useStartingWindowAboveLocked);
503     bool UseStartingWindowAboveLocked() const;
504 
505 protected:
506     class SessionLifeCycleTask : public virtual RefBase {
507     public:
SessionLifeCycleTask(const Task & task,const std::string & name,const LifeCycleTaskType & type)508         SessionLifeCycleTask(const Task& task, const std::string& name, const LifeCycleTaskType& type)
509             : task(task), name(name), type(type) {}
510         Task task;
511         const std::string name;
512         LifeCycleTaskType type;
513         std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();
514         bool running = false;
515     };
516     void StartLifeCycleTask(sptr<SessionLifeCycleTask> lifeCycleTask);
517     void GeneratePersistentId(bool isExtension, int32_t persistentId);
518     virtual void UpdateSessionState(SessionState state);
519     void NotifySessionStateChange(const SessionState& state);
520     void UpdateSessionTouchable(bool touchable);
UpdateActiveStatus(bool isActive)521     virtual WSError UpdateActiveStatus(bool isActive) { return WSError::WS_OK; }
522 
523     /*
524      * Gesture Back
525      */
UpdateGestureBackEnabled()526     virtual void UpdateGestureBackEnabled() {}
527 
528     WSRectF UpdateTopBottomArea(const WSRectF& rect, MMI::WindowArea area);
529     WSRectF UpdateLeftRightArea(const WSRectF& rect, MMI::WindowArea area);
530     WSRectF UpdateInnerAngleArea(const WSRectF& rect, MMI::WindowArea area);
531     virtual void UpdatePointerArea(const WSRect& rect);
532     virtual bool CheckPointerEventDispatch(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const;
533     bool IsTopDialog() const;
534     void HandlePointDownDialog(int32_t pointAction);
535     void NotifySessionInfoChange();
536 
537     void PostTask(Task&& task, const std::string& name = "sessionTask", int64_t delayTime = 0);
538     void PostExportTask(Task&& task, const std::string& name = "sessionExportTask", int64_t delayTime = 0);
539     template<typename SyncTask, typename Return = std::invoke_result_t<SyncTask>>
540     Return PostSyncTask(SyncTask&& task, const std::string& name = "sessionTask")
541     {
542         Return ret;
543         if (!handler_ || handler_->GetEventRunner()->IsCurrentRunnerThread()) {
544             StartTraceForSyncTask(name);
545             ret = task();
546             FinishTraceForSyncTask();
547             return ret;
548         }
549         auto syncTask = [&ret, &task, name]() {
550             StartTraceForSyncTask(name);
551             ret = task();
552             FinishTraceForSyncTask();
553         };
554         handler_->PostSyncTask(std::move(syncTask), name, AppExecFwk::EventQueue::Priority::IMMEDIATE);
555         return ret;
556     }
557 
558     static std::shared_ptr<AppExecFwk::EventHandler> mainHandler_;
559     int32_t persistentId_ = INVALID_SESSION_ID;
560     std::atomic<SessionState> state_ = SessionState::STATE_DISCONNECT;
561     SessionInfo sessionInfo_;
562     std::recursive_mutex sessionInfoMutex_;
563     std::shared_ptr<RSSurfaceNode> surfaceNode_;
564     mutable std::mutex snapshotMutex_;
565     std::shared_ptr<Media::PixelMap> snapshot_;
566     sptr<ISessionStage> sessionStage_;
567     std::mutex lifeCycleTaskQueueMutex_;
568     std::list<sptr<SessionLifeCycleTask>> lifeCycleTaskQueue_;
569     bool isActive_ = false;
570     bool isSystemActive_ = false;
571     WSRectF bounds_;
572     Rotation rotation_;
573     float offsetX_ = 0.0f;
574     float offsetY_ = 0.0f;
575     std::atomic_bool isExitSplitOnBackground_ = false;
576     bool isVisible_ = false;
577 
578     NotifyPendingSessionActivationFunc pendingSessionActivationFunc_;
579     NotifyChangeSessionVisibilityWithStatusBarFunc changeSessionVisibilityWithStatusBarFunc_;
580     NotifySessionStateChangeFunc sessionStateChangeFunc_;
581     NotifyBufferAvailableChangeFunc bufferAvailableChangeFunc_;
582     NotifySessionInfoChangeNotifyManagerFunc sessionInfoChangeNotifyManagerFunc_;
583     NotifySessionStateChangeNotifyManagerFunc sessionStateChangeNotifyManagerFunc_;
584     NotifyRequestFocusStatusNotifyManagerFunc requestFocusStatusNotifyManagerFunc_;
585     NotifyUIRequestFocusFunc requestFocusFunc_;
586     NotifyUILostFocusFunc lostFocusFunc_;
587     GetStateFromManagerFunc getStateFromManagerFunc_;
588     NotifyBackPressedFunc backPressedFunc_;
589     NotifySessionFocusableChangeFunc sessionFocusableChangeFunc_;
590     NotifySessionTouchableChangeFunc sessionTouchableChangeFunc_;
591     NotifyClickFunc clickFunc_;
592     NotifyTerminateSessionFunc terminateSessionFunc_;
593     NotifyTerminateSessionFuncNew terminateSessionFuncNew_;
594     NotifyTerminateSessionFuncTotal terminateSessionFuncTotal_;
595     NofitySessionLabelUpdatedFunc updateSessionLabelFunc_;
596     NofitySessionIconUpdatedFunc updateSessionIconFunc_;
597     std::shared_ptr<NotifySessionExceptionFunc> sessionExceptionFunc_;
598     std::shared_ptr<NotifySessionExceptionFunc> jsSceneSessionExceptionFunc_;
599     NotifySessionSnapshotFunc notifySessionSnapshotFunc_;
600     NotifyPendingSessionToForegroundFunc pendingSessionToForegroundFunc_;
601     NotifyPendingSessionToBackgroundForDelegatorFunc pendingSessionToBackgroundForDelegatorFunc_;
602     NotifyRaiseToTopForPointDownFunc raiseToTopForPointDownFunc_;
603     NotifySessionInfoLockedStateChangeFunc sessionInfoLockedStateChangeFunc_;
604     NotifySystemSessionPointerEventFunc systemSessionPointerEventFunc_;
605     NotifySystemSessionKeyEventFunc systemSessionKeyEventFunc_;
606     NotifyContextTransparentFunc contextTransparentFunc_;
607     NotifyFrameLayoutFinishFunc frameLayoutFinishFunc_;
608     AcquireRotateAnimationConfigFunc acquireRotateAnimationConfigFunc_;
609     SystemSessionConfig systemConfig_;
610     bool needSnapshot_ = false;
611     float snapshotScale_ = 0.5;
612     sptr<ScenePersistence> scenePersistence_ = nullptr;
613 
614     /*
615      * Window Layout
616      */
617     WSRect winRect_;
618     WSRect clientRect_; // rect saved when prelayout or notify client to update rect
619     WSRect lastLayoutRect_; // rect saved when go background
620     WSRect layoutRect_; // rect of root view
621     WSRect globalRect_; // globalRect include translate
622     mutable std::mutex globalRectMutex_;
623     SizeChangeReason reason_ = SizeChangeReason::UNDEFINED;
624     NotifySessionRectChangeFunc sessionRectChangeFunc_;
625     float clientScaleX_ = 1.0f;
626     float clientScaleY_ = 1.0f;
627     float clientPivotX_ = 0.0f;
628     float clientPivotY_ = 0.0f;
629     void SetClientScale(float scaleX, float scaleY, float pivotX, float pivotY);
630 
631     /*
632      * Window ZOrder
633      */
634     uint32_t zOrder_ = 0;
635     uint32_t lastZOrder_ = 0;
636 
637     /*
638      * Window Focus
639      */
640     bool isFocused_ = false;
641     bool blockingFocus_ {false};
642 
643     uint32_t uiNodeId_ = 0;
644     float aspectRatio_ = 0.0f;
645     std::map<MMI::WindowArea, WSRectF> windowAreas_;
646     bool isTerminating_ = false;
647     float floatingScale_ = 1.0f;
648     bool isDirty_ = false;
649     std::recursive_mutex sizeChangeMutex_;
650     float scaleX_ = 1.0f;
651     float scaleY_ = 1.0f;
652     float pivotX_ = 0.0f;
653     float pivotY_ = 0.0f;
654     bool scbKeepKeyboardFlag_ = false;
655     mutable std::shared_mutex dialogVecMutex_;
656     std::vector<sptr<Session>> dialogVec_;
657     mutable std::shared_mutex parentSessionMutex_;
658     sptr<Session> parentSession_;
659     sptr<IWindowEventChannel> windowEventChannel_;
660 
661     mutable std::mutex pointerEventMutex_;
662     mutable std::shared_mutex keyEventMutex_;
663     bool rectChangeListenerRegistered_ = false;
664     // only accessed on SSM thread
665     uint32_t dirtyFlags_ = 0;
666     bool isNeedSyncSessionRect_ { true }; // where need sync to session rect,  currently use in split drag
667 
668     bool isStarting_ = false;   // when start app, session is starting state until foreground
669     std::atomic_bool mainUIStateDirty_ = false;
670     static bool isScbCoreEnabled_;
671 
672 private:
673     void HandleDialogForeground();
674     void HandleDialogBackground();
675     WSError HandleSubWindowClick(int32_t action);
676     void NotifyPointerEventToRs(int32_t pointAction);
677     void SetWindowSessionProperty(const sptr<WindowSessionProperty>& property);
678 
679     template<typename T>
680     bool RegisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
681     template<typename T>
682     bool UnregisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
683     bool IsStateMatch(bool isAttach) const;
684     bool IsSupportDetectWindow(bool isAttach);
685     bool ShouldCreateDetectTask(bool isAttach, WindowMode windowMode) const;
686     bool ShouldCreateDetectTaskInRecent(bool newShowRecent, bool oldShowRecent, bool isAttach) const;
687     void CreateDetectStateTask(bool isAttach, WindowMode windowMode);
688     int32_t GetRotateAnimationDuration();
689 
690     /*
691      * Window Layout
692      */
693     void UpdateGravityWhenUpdateWindowMode(WindowMode mode);
694 
695     template<typename T1, typename T2, typename Ret>
696     using EnableIfSame = typename std::enable_if<std::is_same_v<T1, T2>, Ret>::type;
697     template<typename T>
GetListeners()698     inline EnableIfSame<T, ILifecycleListener, std::vector<std::weak_ptr<ILifecycleListener>>> GetListeners()
699     {
700         std::vector<std::weak_ptr<ILifecycleListener>> lifecycleListeners;
701         {
702             std::lock_guard<std::recursive_mutex> lock(lifecycleListenersMutex_);
703             for (auto& listener : lifecycleListeners_) {
704                 lifecycleListeners.push_back(listener);
705             }
706         }
707         return lifecycleListeners;
708     }
709 
710     std::recursive_mutex lifecycleListenersMutex_;
711     std::vector<std::shared_ptr<ILifecycleListener>> lifecycleListeners_;
712     std::shared_ptr<AppExecFwk::EventHandler> handler_;
713     std::shared_ptr<AppExecFwk::EventHandler> exportHandler_;
714     std::function<bool()> isScreenLockedCallback_;
715 
716     mutable std::shared_mutex propertyMutex_;
717     sptr<WindowSessionProperty> property_;
718 
719     /*
720      * Window Focus
721      */
722     mutable std::shared_mutex uiRequestFocusMutex_;
723     mutable std::shared_mutex uiLostFocusMutex_;
724     bool focusedOnShow_ = true;
725     bool focusableOnShow_ = true; // if false, ignore request focus when session onAttach
726     bool isStartingBeforeVisible_ = false;
727 
728     bool showRecent_ = false;
729     bool bufferAvailable_ = false;
730 
731     /*
732      * Multi Window
733      */
734     bool isMidScene_ = false;
735 
736     WSRect preRect_;
737     int32_t callingPid_ = -1;
738     int32_t callingUid_ = -1;
739     int32_t appIndex_ = { 0 };
740     std::string callingBundleName_ { "unknown" };
741     bool isRSVisible_ {false};
742     WindowVisibilityState visibilityState_ { WINDOW_LAYER_STATE_MAX};
743     bool needNotify_ {true};
744     bool isRSDrawing_ {false};
745     sptr<IRemoteObject> abilityToken_ = nullptr;
746     float vpr_ { 1.5f };
747     bool forceTouchable_ { true };
748     bool systemTouchable_ { true };
749     std::atomic_bool foregroundInteractiveStatus_ { true };
750 
751     /*
752      * Window Lifecycle
753      */
754     std::atomic<bool> isActivatedAfterScreenLocked_ { true };
755     std::atomic<bool> isAttach_{ false };
756     sptr<IPatternDetachCallback> detachCallback_ = nullptr;
757 
758     std::shared_ptr<RSSurfaceNode> leashWinSurfaceNode_;
759     mutable std::mutex leashWinSurfaceNodeMutex_;
760     DetectTaskInfo detectTaskInfo_;
761     mutable std::shared_mutex detectTaskInfoMutex_;
762 
763     /*
764      * Starting Window
765      */
766     bool useStartingWindowAboveLocked_ { false };
767 };
768 } // namespace OHOS::Rosen
769 
770 #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_H
771