• 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_SCENE_SESSION_MANAGER_H
17 #define OHOS_ROSEN_WINDOW_SCENE_SCENE_SESSION_MANAGER_H
18 
19 #include <shared_mutex>
20 #include <mutex>
21 
22 #include <transaction/rs_interfaces.h>
23 #include "agent_death_recipient.h"
24 #include "common/include/task_scheduler.h"
25 #include "future.h"
26 #include "interfaces/include/ws_common.h"
27 #include "mission_snapshot.h"
28 #include "session_listener_controller.h"
29 #include "scene_session_converter.h"
30 #include "scb_session_handler.h"
31 #include "session/host/include/root_scene_session.h"
32 #include "session_manager/include/zidl/scene_session_manager_stub.h"
33 #include "wm_single_instance.h"
34 #include "window_scene_config.h"
35 #include "display_info.h"
36 #include "display_change_listener.h"
37 
38 namespace OHOS::AAFwk {
39 class SessionInfo;
40 } // namespace OHOS::AAFwk
41 
42 namespace OHOS::AppExecFwk {
43 class IBundleMgr;
44 struct AbilityInfo;
45 } // namespace OHOS::AppExecFwk
46 
47 namespace OHOS::Global::Resource {
48 class ResourceManager;
49 } // namespace OHOS::Global::Resource
50 
51 namespace OHOS::Rosen {
52 namespace AncoConsts {
53     constexpr const char* ANCO_MISSION_ID = "ohos.anco.param.missionId";
54     constexpr const char* ANCO_SESSION_ID = "ohos.anco.param.sessionId";
55 }
56 
57 class SceneSession;
58 class AccessibilityWindowInfo;
59 using NotifyCreateSpecificSessionFunc = std::function<void(const sptr<SceneSession>& session)>;
60 using ProcessGestureNavigationEnabledChangeFunc = std::function<void(bool enable)>;
61 using ProcessOutsideDownEventFunc = std::function<void(int32_t x, int32_t y)>;
62 using NotifySetFocusSessionFunc = std::function<void(const sptr<SceneSession>& session)>;
63 using DumpRootSceneElementInfoFunc = std::function<void(const std::vector<std::string>& params,
64     std::vector<std::string>& infos)>;
65 using WindowFocusChangedFunc = std::function<void(int32_t persistentId, bool isFocused)>;
66 
67 class DisplayChangeListener : public IDisplayChangeListener {
68 public:
69     virtual void OnDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
70         const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type) override;
71     virtual void OnScreenshot(DisplayId displayId) override;
72 };
73 
74 class SceneSessionManager : public SceneSessionManagerStub {
75 WM_DECLARE_SINGLE_INSTANCE_BASE(SceneSessionManager)
76 public:
77     sptr<SceneSession> RequestSceneSession(const SessionInfo& sessionInfo,
78         sptr<WindowSessionProperty> property = nullptr);
79     std::future<int32_t> RequestSceneSessionActivation(const sptr<SceneSession>& sceneSession, bool isNewActive);
80     WSError RequestSceneSessionBackground(const sptr<SceneSession>& sceneSession, const bool isDelegator = false);
81     WSError RequestSceneSessionDestruction(
82         const sptr<SceneSession>& sceneSession, const bool needRemoveSession = true);
83     WSError RequestSceneSessionByCall(const sptr<SceneSession>& sceneSession);
84     void StartAbilityBySpecified(const SessionInfo& sessionInfo);
85     void SetRootSceneContext(AbilityRuntime::Context* context);
86     sptr<RootSceneSession> GetRootSceneSession();
87     sptr<SceneSession> GetSceneSession(int32_t persistentId);
88     sptr<SceneSession> GetSceneSessionByName(const std::string& bundleName,
89         const std::string& moduleName, const std::string& abilityName);
90     WSError CreateAndConnectSpecificSession(const sptr<ISessionStage>& sessionStage,
91         const sptr<IWindowEventChannel>& eventChannel, const std::shared_ptr<RSSurfaceNode>& surfaceNode,
92         sptr<WindowSessionProperty> property, int32_t& persistentId, sptr<ISession>& session,
93         sptr<IRemoteObject> token = nullptr);
94     WSError DestroyAndDisconnectSpecificSession(const int32_t& persistentId);
95     WMError UpdateProperty(sptr<WindowSessionProperty>& property, WSPropertyChangeAction action);
96     void SetCreateSpecificSessionListener(const NotifyCreateSpecificSessionFunc& func);
97     void SetGestureNavigationEnabledChangeListener(const ProcessGestureNavigationEnabledChangeFunc& func);
98     void SetOutsideDownEventListener(const ProcessOutsideDownEventFunc& func);
99     const AppWindowSceneConfig& GetWindowSceneConfig() const;
100     WSError ProcessBackEvent();
101     WSError BindDialogTarget(uint64_t persistentId, sptr<IRemoteObject> targetToken);
102     void GetStartPage(const SessionInfo& sessionInfo, std::string& path, uint32_t& bgColor);
103     WMError SetGestureNavigaionEnabled(bool enable);
104     WMError RegisterWindowManagerAgent(WindowManagerAgentType type,
105         const sptr<IWindowManagerAgent>& windowManagerAgent);
106     WMError UnregisterWindowManagerAgent(WindowManagerAgentType type,
107         const sptr<IWindowManagerAgent>& windowManagerAgent);
108 
109     WSError SetFocusedSession(int32_t persistentId);
110     int32_t GetFocusedSession() const;
111     WSError GetAllSessionDumpInfo(std::string& info);
112     WSError GetSpecifiedSessionDumpInfo(std::string& dumpInfo, const std::vector<std::string>& params,
113         const std::string& strId);
114     WSError GetSessionDumpInfo(const std::vector<std::string>& params, std::string& info);
115     WSError UpdateFocus(int32_t persistentId, bool isFocused);
116     WSError UpdateWindowMode(int32_t persistentId, int32_t windowMode);
117     WSError SwitchUser(int32_t oldUserId, int32_t newUserId, std::string &fileDir);
118     int32_t GetCurrentUserId() const;
119     void StartWindowInfoReportLoop();
120     void GetFocusWindowInfo(FocusChangeInfo& focusInfo);
121     void NotifyCompleteFirstFrameDrawing(int32_t persistentId);
122     void NotifySessionMovedToFront(int32_t persistentId);
123     WSError SetSessionGravity(int32_t persistentId, SessionGravity gravity, uint32_t percent);
124     WSError SetSessionLabel(const sptr<IRemoteObject> &token, const std::string &label);
125     WSError SetSessionIcon(const sptr<IRemoteObject> &token, const std::shared_ptr<Media::PixelMap> &icon);
126     WSError IsValidSessionIds(const std::vector<int32_t> &sessionIds, std::vector<bool> &results);
127     WSError RegisterSessionListener(const sptr<ISessionChangeListener> sessionListener);
128     void UnregisterSessionListener();
129     void HandleTurnScreenOn(const sptr<SceneSession>& sceneSession);
130     void HandleKeepScreenOn(const sptr<SceneSession>& sceneSession, bool requireLock);
131     void InitWithRenderServiceAdded();
132     WSError PendingSessionToForeground(const sptr<IRemoteObject> &token);
133     WSError PendingSessionToBackgroundForDelegator(const sptr<IRemoteObject> &token);
134     WSError GetFocusSessionToken(sptr<IRemoteObject> &token);
135 
136     WSError RegisterSessionListener(const sptr<ISessionListener>& listener);
137     WSError UnRegisterSessionListener(const sptr<ISessionListener>& listener);
138     WSError GetSessionInfos(const std::string& deviceId, int32_t numMax, std::vector<SessionInfoBean>& sessionInfos);
139     WSError GetSessionInfo(const std::string& deviceId, int32_t persistentId, SessionInfoBean& sessionInfo);
140     WSError GetAllAbilityInfos(const AAFwk::Want &want, int32_t userId,
141         std::vector<AppExecFwk::AbilityInfo> &abilityInfos);
142     WSError PrepareTerminate(int32_t persistentId, bool& isPrepareTerminate);
143 
144     WSError TerminateSessionNew(const sptr<AAFwk::SessionInfo> info, bool needStartCaller);
145     WSError UpdateSessionAvoidAreaListener(int32_t& persistentId, bool haveListener);
146     WSError GetSessionSnapshot(const std::string& deviceId, int32_t persistentId,
147                                std::shared_ptr<Media::PixelMap>& snapshot, bool isLowResolution);
148     WSError SetSessionContinueState(const sptr<IRemoteObject> &token, const ContinueState& continueState);
149     WSError ClearSession(int32_t persistentId);
150     WSError ClearAllSessions();
151 
152     void UpdatePrivateStateAndNotify(bool isAddingPrivateSession);
153     void InitPersistentStorage();
154     std::string GetSessionSnapshotFilePath(int32_t persistentId);
155     void OnOutsideDownEvent(int32_t x, int32_t y);
156     void NotifySessionTouchOutside(int32_t action, int32_t x, int32_t y);
157 
158     WMError GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos);
159     WSError SetWindowFlags(const sptr<SceneSession>& sceneSession, uint32_t flags);
160 
161     void SetWaterMarkSessionCount(int32_t count);
162     int32_t GetWaterMarkSessionCount() const;
163     void NotifyOccupiedAreaChangeInfo(const sptr<SceneSession> callingSession,
164         const WSRect& rect, const WSRect& occupiedArea);
165     void OnScreenshot(DisplayId displayId);
166     void NotifyDumpInfoResult(const std::vector<std::string>& info);
167     void SetDumpRootSceneElementInfoListener(const DumpRootSceneElementInfoFunc& func);
168 
169     RunnableFuture<std::vector<std::string>> dumpInfoFuture_;
170     void RegisterWindowFocusChanged(const WindowFocusChangedFunc& func);
171 
172     WSError RegisterIAbilityManagerCollaborator(int32_t type, const sptr<AAFwk::IAbilityManagerCollaborator> &impl);
173     WSError UnregisterIAbilityManagerCollaborator(int32_t type);
174 protected:
175     SceneSessionManager();
176     virtual ~SceneSessionManager() = default;
177 
178 private:
179     void Init();
180     void InitPrepareTerminateConfig();
181     void LoadWindowSceneXml();
182     void ConfigWindowSceneXml();
183     void ConfigWindowEffect(const WindowSceneConfig::ConfigItem& effectConfig);
184     void ConfigKeyboardAnimation(const WindowSceneConfig::ConfigItem& animationConfig);
185     bool ConfigAppWindowCornerRadius(const WindowSceneConfig::ConfigItem& item, float& out);
186     bool ConfigAppWindowShadow(const WindowSceneConfig::ConfigItem& shadowConfig, WindowShadowConfig& outShadow);
187     void ConfigDecor(const WindowSceneConfig::ConfigItem& decorConfig);
188     void ConfigWindowAnimation(const WindowSceneConfig::ConfigItem& windowAnimationConfig);
189     void ConfigStartingWindowAnimation(const WindowSceneConfig::ConfigItem& startingWindowConfig);
190     void ConfigWindowSizeLimits();
191     void ConfigMainWindowSizeLimits(const WindowSceneConfig::ConfigItem& mainWindowSizeConifg);
192     void ConfigSubWindowSizeLimits(const WindowSceneConfig::ConfigItem& subWindowSizeConifg);
193     sptr<SceneSession::SpecificSessionCallback> CreateSpecificSessionCallback();
194     void FillSessionInfo(SessionInfo& sessionInfo);
195     std::shared_ptr<AppExecFwk::AbilityInfo> QueryAbilityInfoFromBMS(const int32_t uId, const std::string& bundleName,
196         const std::string& abilityName, const std::string& moduleName);
197 
198     void RelayoutKeyBoard(sptr<SceneSession> sceneSession);
199     void RestoreCallingSessionSizeIfNeed();
200     void ResizeSoftInputCallingSessionIfNeed(const sptr<SceneSession>& sceneSession);
201 
202     sptr<AAFwk::SessionInfo> SetAbilitySessionInfo(const sptr<SceneSession>& scnSession);
203     WSError DestroyDialogWithMainWindow(const sptr<SceneSession>& scnSession);
204     sptr<SceneSession> FindMainWindowWithToken(sptr<IRemoteObject> targetToken);
205     WSError UpdateParentSession(const sptr<SceneSession>& sceneSession, sptr<WindowSessionProperty> property);
206     void UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing);
207     void UpdateFocusableProperty(int32_t persistentId);
208     std::vector<sptr<SceneSession>> GetSceneSessionVectorByType(WindowType type);
209     bool UpdateSessionAvoidAreaIfNeed(const int32_t& persistentId,
210         const AvoidArea& avoidArea, AvoidAreaType avoidAreaType);
211     bool UpdateAvoidArea(const int32_t& persistentId);
212 
213     sptr<AppExecFwk::IBundleMgr> GetBundleManager();
214     std::shared_ptr<Global::Resource::ResourceManager> CreateResourceManager(
215         const AppExecFwk::AbilityInfo& abilityInfo);
216     void GetStartPageFromResource(const AppExecFwk::AbilityInfo& abilityInfo, std::string& path, uint32_t& bgColor);
217     std::string CreateCurve(
218         const WindowSceneConfig::ConfigItem& curveConfig, const std::string& nodeName = "keyboardAnimation");
219 
220     bool CheckIsRemote(const std::string& deviceId);
221     bool GetLocalDeviceId(std::string& localDeviceId);
222     std::string AnonymizeDeviceId(const std::string& deviceId);
223     int GetRemoteSessionInfos(const std::string& deviceId, int32_t numMax,
224                               std::vector<SessionInfoBean> &sessionInfos);
225     int GetRemoteSessionInfo(const std::string& deviceId, int32_t persistentId, SessionInfoBean& sessionInfo);
226 
227     WSError SetBrightness(const sptr<SceneSession>& sceneSession, float brightness);
228     WSError UpdateBrightness(int32_t persistentId);
229     void SetDisplayBrightness(float brightness);
230     float GetDisplayBrightness() const;
231     WMError HandleUpdateProperty(const sptr<WindowSessionProperty>& property, WSPropertyChangeAction action,
232         const sptr<SceneSession>& sceneSession);
233     void NotifyWindowInfoChange(int32_t persistentId, WindowUpdateType type);
234     bool FillWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos,
235         const sptr<SceneSession>& sceneSession);
236     std::vector<std::pair<uint64_t, bool>> GetWindowVisibilityChangeInfo(
237         std::shared_ptr<RSOcclusionData> occlusionData);
238     void WindowVisibilityChangeCallback(std::shared_ptr<RSOcclusionData> occlusiontionData);
239     sptr<SceneSession> SelectSesssionFromMap(const uint64_t& surfaceId);
240     void WindowDestroyNotifyVisibility(const sptr<SceneSession>& sceneSession);
241     void RegisterInputMethodShownFunc(const sptr<SceneSession>& sceneSession);
242     void OnInputMethodShown(const int32_t& persistentId);
243     void RegisterInputMethodHideFunc(const sptr<SceneSession>& sceneSession);
244     void RegisterSessionExceptionFunc(const sptr<SceneSession>& sceneSession);
245     bool IsSessionVisible(const sptr<SceneSession>& session);
246     void DumpSessionInfo(const sptr<SceneSession>& session, std::ostringstream& oss);
247     void DumpAllAppSessionInfo(std::ostringstream& oss);
248     void DumpSessionElementInfo(const sptr<SceneSession>& session,
249         const std::vector<std::string>& params, std::string& dumpInfo);
250     void AddClientDeathRecipient(const sptr<ISessionStage>& sessionStage, const sptr<SceneSession>& sceneSession);
251     void DestroySpecificSession(const sptr<IRemoteObject>& remoteObject);
252     void CleanUserMap();
253     WMError UpdatePropertyDragEnabled(const sptr<WindowSessionProperty>& property,
254                                       const sptr<SceneSession>& sceneSession);
255     WMError UpdatePropertyRaiseEnabled(const sptr<WindowSessionProperty>& property,
256                                        const sptr<SceneSession>& sceneSession);
257     WSError DestroyAndDisconnectSpecificSessionInner(sptr<SceneSession> sceneSession);
258     sptr<RootSceneSession> rootSceneSession_;
259     std::shared_ptr<AbilityRuntime::Context> rootSceneContext_;
260     std::shared_mutex sceneSessionMapMutex_;
261     std::map<int32_t, sptr<SceneSession>> sceneSessionMap_;
262     sptr<ScbSessionHandler> scbSessionHandler_;
263     std::shared_ptr<SessionListenerController> listenerController_;
264     std::map<sptr<IRemoteObject>, int32_t> remoteObjectMap_;
265     std::set<sptr<SceneSession>> avoidAreaListenerSessionSet_;
266     std::map<int32_t, std::map<AvoidAreaType, AvoidArea>> lastUpdatedAvoidArea_;
267 
268     NotifyCreateSpecificSessionFunc createSpecificSessionFunc_;
269     ProcessGestureNavigationEnabledChangeFunc gestureNavigationEnabledChangeFunc_;
270     ProcessOutsideDownEventFunc outsideDownEventFunc_;
271     DumpRootSceneElementInfoFunc dumpRootSceneFunc_;
272     AppWindowSceneConfig appWindowSceneConfig_;
273     SystemSessionConfig systemConfig_;
274     int32_t focusedSessionId_ = INVALID_SESSION_ID;
275     int32_t brightnessSessionId_ = INVALID_SESSION_ID;
276     float displayBrightness_ = UNDEFINED_BRIGHTNESS;
277     bool isPrepareTerminateEnable_ {false};
278     WSRect callingWindowRestoringRect_ = {0, 0, 0, 0};
279     int32_t currentUserId_;
280 
281     std::shared_ptr<TaskScheduler> taskScheduler_;
282     sptr<AppExecFwk::IBundleMgr> bundleMgr_;
283 
284     std::shared_ptr<AppExecFwk::EventRunner> eventLoop_;
285     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
286     bool isReportTaskStart_ = false;
287     std::shared_ptr<RSOcclusionData> lastOcclusionData_ = std::make_shared<RSOcclusionData>();
288     RSInterfaces& rsInterface_;
289     void RegisterSessionStateChangeNotifyManagerFunc(sptr<SceneSession>& sceneSession);
290     void OnSessionStateChange(int32_t persistentId, const SessionState& state);
291     sptr<ISessionChangeListener> sessionListener_;
292     sptr<SceneSession> FindSessionByToken(const sptr<IRemoteObject> &token);
293 
294     void CheckAndNotifyWaterMarkChangedResult(bool isAddingWaterMark);
295     WSError NotifyWaterMarkFlagChangedResult(bool hasWaterMark);
296     int32_t waterMarkSessionCount_ { 0 };
297     void ProcessPreload(const AppExecFwk::AbilityInfo& abilityInfo) const;
298     WindowFocusChangedFunc windowFocusChangedFunc_;
299     sptr<SceneSession> callingSession_ = nullptr;
300     sptr<AgentDeathRecipient> windowDeath_ = new AgentDeathRecipient(
301         std::bind(&SceneSessionManager::DestroySpecificSession, this, std::placeholders::_1));
302     WSError ClearSession(sptr<SceneSession> sceneSession);
303     bool IsSessionClearable(sptr<SceneSession> scnSession);
304     void GetAllClearableSessions(std::vector<sptr<SceneSession>>& sessionVector);
305     int GetRemoteSessionSnapshotInfo(const std::string& deviceId, int32_t sessionId,
306                                      AAFwk::MissionSnapshot& sessionSnapshot);
307 
308     const int32_t BROKER_UID = 5528;
309     const int32_t BROKER_RESERVE_UID = 5005;
310     std::shared_mutex collaboratorMapLock_;
311     std::unordered_map<int32_t, sptr<AAFwk::IAbilityManagerCollaborator>> collaboratorMap_;
312 
313     bool CheckCollaboratorType(int32_t type);
314     void QueryAbilityInfoFromBMS(const int32_t uId,
315         const SessionInfo& sessionInfo, AppExecFwk::AbilityInfo& abilityInfo);
316     void NotifyStartAbility(int32_t collaboratorType, const SessionInfo& sessionInfo);
317     void NotifySessionCreate(const sptr<SceneSession> sceneSession, SessionInfo& sessionInfo);
318     void NotifyLoadAbility(int32_t collaboratorType, sptr<AAFwk::SessionInfo> abilitySessionInfo,
319         std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo);
320     void NotifyUpdateSessionInfo(const sptr<SceneSession> sceneSession);
321     void NotifyClearSession(int32_t collaboratorType, int32_t persistentId);
322     void NotifyMoveSessionToForeground(int32_t collaboratorType, int32_t persistendId);
323     void PreHandleCollaborator(sptr<SceneSession> sceneSession);
324 };
325 } // namespace OHOS::Rosen
326 
327 #endif // OHOS_ROSEN_WINDOW_SCENE_SCENE_SESSION_MANAGER_H
328