• 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 
19 #include <mutex>
20 #include <set>
21 #include <string>
22 #include <vector>
23 #include <refbase.h>
24 
25 #include "interfaces/include/ws_common.h"
26 #include "session/container/include/zidl/session_stage_interface.h"
27 #include "session/host/include/zidl/session_stub.h"
28 #include "session/host/include/scene_persistence.h"
29 #include "wm_common.h"
30 #include "occupied_area_change_info.h"
31 
32 namespace OHOS::MMI {
33 class PointerEvent;
34 class KeyEvent;
35 class AxisEvent;
36 } // namespace OHOS::MMI
37 
38 namespace OHOS::Media {
39 class PixelMap;
40 } // namespace OHOS::Media
41 
42 namespace OHOS::Rosen {
43 class RSSurfaceNode;
44 using NotifyPendingSessionActivationFunc = std::function<void(SessionInfo& info)>;
45 using NotifySessionStateChangeFunc = std::function<void(const SessionState& state)>;
46 using NotifySessionStateChangeNotifyManagerFunc = std::function<void(int32_t persistentId, const SessionState& state)>;
47 using NotifyBackPressedFunc = std::function<void()>;
48 using NotifySessionFocusableChangeFunc = std::function<void(const bool isFocusable)>;
49 using NotifySessionTouchableChangeFunc = std::function<void(const bool touchable)>;
50 using NotifyClickFunc = std::function<void()>;
51 using NotifyTerminateSessionFunc = std::function<void(const SessionInfo& info)>;
52 using NotifyTerminateSessionFuncNew = std::function<void(const SessionInfo& info, bool needStartCaller)>;
53 using NotifyTerminateSessionFuncTotal = std::function<void(const SessionInfo& info, TerminateType terminateType)>;
54 using NotifySessionExceptionFunc = std::function<void(const SessionInfo& info)>;
55 using NotifyPendingSessionToForegroundFunc = std::function<void(const SessionInfo& info)>;
56 using NotifyPendingSessionToBackgroundForDelegatorFunc = std::function<void(const SessionInfo& info)>;
57 using NotifyCallingSessionForegroundFunc = std::function<void(const int32_t& persistentId)>;
58 using NotifyCallingSessionBackgroundFunc = std::function<void()>;
59 
60 class ILifecycleListener {
61 public:
62     virtual void OnConnect() = 0;
63     virtual void OnForeground() = 0;
64     virtual void OnBackground() = 0;
65     virtual void OnDisconnect() = 0;
66     virtual void OnExtensionDied() = 0;
67 };
68 
69 class Session : public SessionStub, public virtual RefBase {
70 public:
Session(const SessionInfo & info)71     explicit Session(const SessionInfo& info) : sessionInfo_(info) {}
72     virtual ~Session() = default;
73 
74     int32_t GetPersistentId() const;
75     int32_t GetParentPersistentId() const;
76     void SetParentPersistentId(int32_t parentId);
77     void SetSessionRect(const WSRect& rect);
78 
79     std::shared_ptr<RSSurfaceNode> GetSurfaceNode() const;
80     std::shared_ptr<Media::PixelMap> GetSnapshot() const;
81     std::shared_ptr<Media::PixelMap> Snapshot();
82     SessionState GetSessionState() const;
83     SessionInfo& GetSessionInfo();
84     sptr<WindowSessionProperty> GetSessionProperty() const;
85     WSRect GetSessionRect() const;
86     WindowType GetWindowType() const;
87     float GetAspectRatio() const;
88     WSError SetAspectRatio(float ratio) override;
89 
90     void SetWindowSessionProperty(const sptr<WindowSessionProperty>& property);
91     sptr<WindowSessionProperty> GetWindowSessionProperty() const;
92     void SetSessionRequestRect(const WSRect& rect);
93     WSRect GetSessionRequestRect() const;
94 
95     virtual WSError SetActive(bool active);
96     virtual WSError UpdateRect(const WSRect& rect, SizeChangeReason reason);
97 
98     void SetShowRecent(bool showRecent);
99     bool GetShowRecent() const;
100     void SetBufferAvailable(bool bufferAvailable);
101     bool GetBufferAvailable() const;
102 
103     WSError Connect(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
104         const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
105         sptr<WindowSessionProperty> property = nullptr, sptr<IRemoteObject> token = nullptr) override;
106     WSError ConnectImpl(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
107         const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
108         sptr<WindowSessionProperty> property = nullptr, sptr<IRemoteObject> token = nullptr);
109     WSError Foreground(sptr<WindowSessionProperty> property) override;
110     WSError Background() override;
111     WSError Disconnect() override;
112 
113     WSError OnSessionEvent(SessionEvent event) override;
114     WSError UpdateWindowSessionProperty(sptr<WindowSessionProperty> property) override;
115     WSError OnNeedAvoid(bool status) override;
116     WSError TransferAbilityResult(uint32_t resultCode, const AAFwk::Want& want) override;
117     WSError TransferExtensionData(const AAFwk::WantParams& wantParams) override;
118     void NotifyRemoteReady() override;
119     void NotifyExtensionDied() override;
120     void NotifyConnect();
121     void NotifyForeground();
122     void NotifyBackground();
123     void NotifyDisconnect();
124 
125     virtual WSError TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
126     virtual WSError TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent);
127     WSError TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed);
128     WSError TransferFocusActiveEvent(bool isFocusActive);
129     WSError TransferFocusStateEvent(bool focusState);
130 
131     bool RegisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener);
132     bool UnregisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener);
133     void SetPendingSessionActivationEventListener(const NotifyPendingSessionActivationFunc& func);
134 
135     WSError PendingSessionActivation(const sptr<AAFwk::SessionInfo> info) override;
136 
137     WSError TerminateSession(const sptr<AAFwk::SessionInfo> info) override;
138     void SetTerminateSessionListener(const NotifyTerminateSessionFunc& func);
139     WSError TerminateSessionNew(const sptr<AAFwk::SessionInfo> info, bool needStartCaller);
140     void SetTerminateSessionListenerNew(const NotifyTerminateSessionFuncNew& func);
141     void SetSessionExceptionListener(const NotifySessionExceptionFunc& func);
142     WSError NotifySessionException(const sptr<AAFwk::SessionInfo> info) override;
143     WSError TerminateSessionTotal(const sptr<AAFwk::SessionInfo> info, TerminateType terminateType);
144     void SetTerminateSessionListenerTotal(const NotifyTerminateSessionFuncTotal& func);
145     WSError Clear();
146     void SetSessionStateChangeListenser(const NotifySessionStateChangeFunc& func);
147     void SetSessionStateChangeNotifyManagerListener(const NotifySessionStateChangeNotifyManagerFunc& func);
148     void NotifySessionStateChange(const SessionState& state);
149     WSError UpdateActiveStatus(bool isActive) override; // update active status from session_stage
150     WSError RaiseToAppTop() override;
151     WSError UpdateSessionRect(const WSRect& rect, const SizeChangeReason& reason) override;
152     WSError CreateAndConnectSpecificSession(const sptr<ISessionStage>& sessionStage,
153         const sptr<IWindowEventChannel>& eventChannel, const std::shared_ptr<RSSurfaceNode>& surfaceNode,
154         sptr<WindowSessionProperty> property, int32_t& persistentId, sptr<ISession>& session,
155         sptr<IRemoteObject> token = nullptr) override;
156     WSError DestroyAndDisconnectSpecificSession(const int32_t& persistentId) override;
157     void SetSystemConfig(const SystemSessionConfig& systemConfig);
158     void SetBackPressedListenser(const NotifyBackPressedFunc& func);
159     WSError ProcessBackEvent(); // send back event to session_stage
160     WSError RequestSessionBack() override; // receive back request from session_stage
161     WSError MarkProcessed(int32_t eventId) override;
162 
163     sptr<ScenePersistence> GetScenePersistence() const;
164     void SetSessionContinueState(const ContinueState& continueState);
165     void SetParentSession(const sptr<Session>& session);
166     void BindDialogToParentSession(const sptr<Session>& session);
167     void RemoveDialogToParentSession(const sptr<Session>& session);
168     std::vector<sptr<Session>> GetDialogVector() const;
169     void NotifyTouchDialogTarget();
170     WSError NotifyDestroy();
171 
172     void SetPendingSessionToForegroundListener(const NotifyPendingSessionToForegroundFunc& func);
173     WSError PendingSessionToForeground();
174     void SetPendingSessionToBackgroundForDelegatorListener(const NotifyPendingSessionToBackgroundForDelegatorFunc& func);
175     WSError PendingSessionToBackgroundForDelegator();
176 
177     void SetSessionFocusableChangeListener(const NotifySessionFocusableChangeFunc& func);
178     void SetSessionTouchableChangeListener(const NotifySessionTouchableChangeFunc& func);
179     void SetClickListener(const NotifyClickFunc& func);
180     void NotifySessionFocusableChange(bool isFocusable);
181     void NotifySessionTouchableChange(bool touchable);
182     void NotifyClick();
183     WSError UpdateFocus(bool isFocused);
184     WSError UpdateWindowMode(WindowMode mode);
185     WSError SetFocusable(bool isFocusable);
186     bool NeedNotify() const;
187     void SetNeedNotify(bool needNotify);
188     bool GetFocusable() const;
189     WSError SetTouchable(bool touchable);
190     bool GetTouchable() const;
191     WSError SetVisible(bool isVisible);
192     bool GetVisible() const;
193     WSError SetGlobalMaximizeMode(MaximizeMode mode) override;
194     WSError GetGlobalMaximizeMode(MaximizeMode& mode) override;
195     AvoidArea GetAvoidAreaByType(AvoidAreaType type) override;
196     WSError SetBrightness(float brightness);
197     float GetBrightness() const;
198     void NotifyOccupiedAreaChangeInfo(sptr<OccupiedAreaChangeInfo> info);
199 
200     bool IsSessionValid() const;
201     bool IsActive() const;
202 
203     sptr<IRemoteObject> dialogTargetToken_ = nullptr;
204     int32_t GetWindowId() const;
205     void SetAppIndex(const int32_t appIndex);
206     int32_t GetAppIndex() const;
207     void SetCallingPid(int32_t id);
208     void SetCallingUid(int32_t id);
209     int32_t GetCallingPid() const;
210     int32_t GetCallingUid() const;
211     void SetAbilityToken(sptr<IRemoteObject> token);
212     sptr<IRemoteObject> GetAbilityToken() const;
213     WindowMode GetWindowMode();
214     virtual void SetZOrder(uint32_t zOrder);
215     uint32_t GetZOrder() const;
216     void SetUINodeId(uint32_t uiNodeId);
217     uint32_t GetUINodeId() const;
218     WSError UpdateWindowAnimationFlag(bool needDefaultAnimationFlag) override;
219     WSError UpdateWindowSceneAfterCustomAnimation(bool isAdd) override;
220 
221     void SetNotifyCallingSessionForegroundFunc(const NotifyCallingSessionForegroundFunc& func);
222     void NotifyCallingSessionForeground();
223     void SetNotifyCallingSessionBackgroundFunc(const NotifyCallingSessionBackgroundFunc& func);
224     void NotifyCallingSessionBackground();
225     void NotifyScreenshot();
GetTouchHotAreas()226     virtual std::vector<Rect> GetTouchHotAreas() const
227     {
228         return std::vector<Rect>();
229     }
230     WSError RaiseAboveTarget(int32_t subWindowId) override;
231 
232 protected:
233     void GeneratePersistentId(const bool isExtension, const SessionInfo& sessionInfo);
234     void UpdateSessionState(SessionState state);
235     void UpdateSessionFocusable(bool isFocusable);
236     void UpdateSessionTouchable(bool touchable);
237 
238     int32_t persistentId_ = INVALID_SESSION_ID;
239     SessionState state_ = SessionState::STATE_DISCONNECT;
240     SessionInfo sessionInfo_;
241     sptr<WindowSessionProperty> property_;
242     std::shared_ptr<RSSurfaceNode> surfaceNode_;
243     std::shared_ptr<Media::PixelMap> snapshot_;
244     sptr<ISessionStage> sessionStage_;
245     bool isActive_ = false;
246     WSRect winRect_;
247 
248     NotifyPendingSessionActivationFunc pendingSessionActivationFunc_;
249     NotifySessionStateChangeFunc sessionStateChangeFunc_;
250     NotifySessionStateChangeNotifyManagerFunc sessionStateChangeNotifyManagerFunc_;
251     NotifyBackPressedFunc backPressedFunc_;
252     NotifySessionFocusableChangeFunc sessionFocusableChangeFunc_;
253     NotifySessionTouchableChangeFunc sessionTouchableChangeFunc_;
254     NotifyClickFunc clickFunc_;
255     NotifyTerminateSessionFunc terminateSessionFunc_;
256     NotifyTerminateSessionFuncNew terminateSessionFuncNew_;
257     NotifyTerminateSessionFuncTotal terminateSessionFuncTotal_;
258     std::vector<std::shared_ptr<NotifySessionExceptionFunc>> sessionExceptionFuncs_;
259     NotifyPendingSessionToForegroundFunc pendingSessionToForegroundFunc_;
260     NotifyPendingSessionToBackgroundForDelegatorFunc pendingSessionToBackgroundForDelegatorFunc_;
261     NotifyCallingSessionForegroundFunc notifyCallingSessionForegroundFunc_;
262     NotifyCallingSessionBackgroundFunc notifyCallingSessionBackgroundFunc_;
263     SystemSessionConfig systemConfig_;
264     sptr<ScenePersistence> scenePersistence_ = nullptr;
265     uint32_t zOrder_ = 0;
266     uint32_t uiNodeId_ = 0;
267     bool isFocused_ = false;
268     float aspectRatio_ = 0.0f;
269 
270 private:
271     bool CheckDialogOnForeground();
272     void HandleDialogForeground();
273     void HandleDialogBackground();
274 
275     template<typename T>
276     bool RegisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
277     template<typename T>
278     bool UnregisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
279 
280     template<typename T1, typename T2, typename Ret>
281     using EnableIfSame = typename std::enable_if<std::is_same_v<T1, T2>, Ret>::type;
282     template<typename T>
GetListeners()283     inline EnableIfSame<T, ILifecycleListener, std::vector<std::weak_ptr<ILifecycleListener>>> GetListeners()
284     {
285         std::vector<std::weak_ptr<ILifecycleListener>> lifecycleListeners;
286         {
287             std::lock_guard<std::recursive_mutex> lock(mutex_);
288             for (auto& listener : lifecycleListeners_) {
289                 lifecycleListeners.push_back(listener);
290             }
291         }
292         return lifecycleListeners;
293     }
294 
295     std::recursive_mutex mutex_;
296     std::vector<std::shared_ptr<ILifecycleListener>> lifecycleListeners_;
297     sptr<IWindowEventChannel> windowEventChannel_ = nullptr;
298 
299     bool showRecent_ = false;
300     bool bufferAvailable_ = false;
301 
302     std::vector<sptr<Session>> dialogVec_;
303     sptr<Session> parentSession_;
304 
305     int32_t callingPid_ = { 0 };
306     int32_t callingUid_ = { 0 };
307     int32_t appIndex_ = { 0 };
308     std::string callingBundleName_ { "unknow" };
309     bool isVisible_ {false};
310     bool needNotify_ {true};
311     sptr<IRemoteObject> abilityToken_ = nullptr;
312 };
313 } // namespace OHOS::Rosen
314 
315 #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_H
316