• 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 #include "session/host/include/main_session.h"
17 
18 #include <ipc_skeleton.h>
19 
20 #include "common/include/fold_screen_state_internel.h"
21 #include "common/include/session_permission.h"
22 #include "window_helper.h"
23 #include "session_helper.h"
24 #include "session/host/include/scene_persistent_storage.h"
25 
26 namespace OHOS::Rosen {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "MainSession" };
29 constexpr int32_t MAX_LABEL_SIZE = 1024;
30 } // namespace
31 
MainSession(const SessionInfo & info,const sptr<SpecificSessionCallback> & specificCallback)32 MainSession::MainSession(const SessionInfo& info, const sptr<SpecificSessionCallback>& specificCallback)
33     : SceneSession(info, specificCallback)
34 {
35     scenePersistence_ = sptr<ScenePersistence>::MakeSptr(info.bundleName_, GetPersistentId(), capacity_);
36     if (info.persistentId_ != 0 && info.persistentId_ != GetPersistentId()) {
37         // persistentId changed due to id conflicts. Need to rename the old snapshot if exists
38         scenePersistence_->RenameSnapshotFromOldPersistentId(info.persistentId_);
39     }
40     pcFoldScreenController_ = sptr<PcFoldScreenController>::MakeSptr(wptr(this), GetPersistentId());
41     moveDragController_ = sptr<MoveDragController>::MakeSptr(GetPersistentId(), GetWindowType());
42     if (specificCallback != nullptr &&
43         specificCallback->onWindowInputPidChangeCallback_ != nullptr) {
44         moveDragController_->SetNotifyWindowPidChangeCallback(specificCallback->onWindowInputPidChangeCallback_);
45     }
46     SetMoveDragCallback();
47     std::string key = GetRatioPreferenceKey();
48     if (!key.empty()) {
49         if (ScenePersistentStorage::HasKey(key, ScenePersistentStorageType::ASPECT_RATIO)) {
50             float aspectRatio = 0.f;
51             ScenePersistentStorage::Get(key, aspectRatio, ScenePersistentStorageType::ASPECT_RATIO);
52             TLOGI(WmsLogTag::WMS_LAYOUT, "init aspectRatio, bundleName:%{public}s, key:%{public}s, value:%{public}f",
53                 info.bundleName_.c_str(), key.c_str(), aspectRatio);
54             Session::SetAspectRatio(aspectRatio);
55             moveDragController_->SetAspectRatio(aspectRatio);
56         }
57     }
58     auto isPersistentImageFit = ScenePersistentStorage::HasKey(
59         "SetImageForRecent_" + std::to_string(GetPersistentId()), ScenePersistentStorageType::MAXIMIZE_STATE);
60     if (isPersistentImageFit) {
61         scenePersistence_->SetHasSnapshot(true);
62     }
63 
64     WLOGFD("Create MainSession");
65 }
66 
~MainSession()67 MainSession::~MainSession()
68 {
69     WLOGD("~MainSession, id: %{public}d", GetPersistentId());
70 }
71 
Reconnect(const sptr<ISessionStage> & sessionStage,const sptr<IWindowEventChannel> & eventChannel,const std::shared_ptr<RSSurfaceNode> & surfaceNode,sptr<WindowSessionProperty> property,sptr<IRemoteObject> token,int32_t pid,int32_t uid)72 WSError MainSession::Reconnect(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
73     const std::shared_ptr<RSSurfaceNode>& surfaceNode, sptr<WindowSessionProperty> property, sptr<IRemoteObject> token,
74     int32_t pid, int32_t uid)
75 {
76     return PostSyncTask([weakThis = wptr(this), sessionStage, eventChannel, surfaceNode, property, token, pid, uid]() {
77         auto session = weakThis.promote();
78         if (!session) {
79             WLOGFE("session is null");
80             return WSError::WS_ERROR_DESTROYED_OBJECT;
81         }
82         WSError ret = LOCK_GUARD_EXPR(SCENE_GUARD,
83             session->Session::Reconnect(sessionStage, eventChannel, surfaceNode, property, token, pid, uid));
84         if (ret != WSError::WS_OK) {
85             return ret;
86         }
87         WindowState windowState = property->GetWindowState();
88         if (windowState == WindowState::STATE_SHOWN) {
89             session->isActive_ = true;
90             session->UpdateSessionState(SessionState::STATE_ACTIVE);
91         } else {
92             session->isActive_ = false;
93             session->UpdateSessionState(SessionState::STATE_BACKGROUND);
94         }
95         if (session->pcFoldScreenController_) {
96             session->pcFoldScreenController_->OnConnect();
97         }
98         return ret;
99     });
100 }
101 
ProcessPointDownSession(int32_t posX,int32_t posY)102 WSError MainSession::ProcessPointDownSession(int32_t posX, int32_t posY)
103 {
104     const auto& id = GetPersistentId();
105     TLOGD(WmsLogTag::WMS_INPUT_KEY_FLOW, "id:%{public}d, type:%{public}d", id, GetWindowType());
106     auto isModal = IsModal();
107     if (!isModal && CheckDialogOnForeground()) {
108         HandlePointDownDialog();
109         return WSError::WS_OK;
110     }
111     if (isModal) {
112         Session::ProcessClickModalWindowOutside(posX, posY);
113     }
114     auto ret = SceneSession::ProcessPointDownSession(posX, posY);
115     PresentFocusIfPointDown();
116     return ret;
117 }
118 
NotifyForegroundInteractiveStatus(bool interactive)119 void MainSession::NotifyForegroundInteractiveStatus(bool interactive)
120 {
121     SetForegroundInteractiveStatus(interactive);
122     if (!IsSessionValid() || !sessionStage_) {
123         TLOGW(WmsLogTag::WMS_MAIN, "Session or sessionStage is invalid, id: %{public}d state: %{public}u",
124             GetPersistentId(), GetSessionState());
125         return;
126     }
127     const auto& state = GetSessionState();
128     if (isVisible_ || state == SessionState::STATE_ACTIVE || state == SessionState::STATE_FOREGROUND) {
129         WLOGFI("NotifyForegroundInteractiveStatus %{public}d", interactive);
130         sessionStage_->NotifyForegroundInteractiveStatus(interactive);
131     }
132 }
133 
TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)134 WSError MainSession::TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
135 {
136     if (!IsSessionValid()) {
137         return WSError::WS_ERROR_INVALID_SESSION;
138     }
139     if (keyEvent == nullptr) {
140         WLOGFE("KeyEvent is nullptr");
141         return WSError::WS_ERROR_NULLPTR;
142     }
143     if (CheckDialogOnForeground()) {
144         TLOGD(WmsLogTag::WMS_DIALOG, "Has dialog on foreground, not transfer pointer event");
145         return WSError::WS_ERROR_INVALID_PERMISSION;
146     }
147 
148     WSError ret = Session::TransferKeyEvent(keyEvent);
149     return ret;
150 }
151 
UpdatePointerArea(const WSRect & rect)152 void MainSession::UpdatePointerArea(const WSRect& rect)
153 {
154     if (GetWindowMode() != WindowMode::WINDOW_MODE_FLOATING) {
155         return;
156     }
157     Session::UpdatePointerArea(rect);
158 }
159 
CheckPointerEventDispatch(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const160 bool MainSession::CheckPointerEventDispatch(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
161 {
162     auto sessionState = GetSessionState();
163     int32_t action = pointerEvent->GetPointerAction();
164     if (sessionState != SessionState::STATE_FOREGROUND &&
165         sessionState != SessionState::STATE_ACTIVE &&
166         action != MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
167         WLOGFW("Current Session Info: [persistentId: %{public}d, "
168             "state: %{public}d, action:%{public}d]", GetPersistentId(), GetSessionState(), action);
169         return false;
170     }
171     return true;
172 }
173 
SetTopmost(bool topmost)174 WSError MainSession::SetTopmost(bool topmost)
175 {
176     TLOGI(WmsLogTag::WMS_HIERARCHY, "SetTopmost id: %{public}d, topmost: %{public}d", GetPersistentId(), topmost);
177     PostTask([weakThis = wptr(this), topmost]() {
178         auto session = weakThis.promote();
179         if (!session) {
180             TLOGE(WmsLogTag::WMS_HIERARCHY, "session is null");
181             return;
182         }
183         auto property = session->GetSessionProperty();
184         if (property) {
185             TLOGI(WmsLogTag::WMS_HIERARCHY, "Notify session topmost change, id: %{public}d, topmost: %{public}u",
186                 session->GetPersistentId(), topmost);
187             property->SetTopmost(topmost);
188             if (session->onSessionTopmostChange_) {
189                 session->onSessionTopmostChange_(topmost);
190             }
191         }
192     }, "SetTopmost");
193     return WSError::WS_OK;
194 }
195 
IsTopmost() const196 bool MainSession::IsTopmost() const
197 {
198     return GetSessionProperty()->IsTopmost();
199 }
200 
SetMainWindowTopmost(bool isTopmost)201 WSError MainSession::SetMainWindowTopmost(bool isTopmost)
202 {
203     GetSessionProperty()->SetMainWindowTopmost(isTopmost);
204     TLOGD(WmsLogTag::WMS_HIERARCHY, "id: %{public}d, isTopmost: %{public}u",
205         GetPersistentId(), isTopmost);
206     if (mainWindowTopmostChangeFunc_) {
207         mainWindowTopmostChangeFunc_(isTopmost);
208     }
209     return WSError::WS_OK;
210 }
211 
IsMainWindowTopmost() const212 bool MainSession::IsMainWindowTopmost() const
213 {
214     return GetSessionProperty()->IsMainWindowTopmost();
215 }
216 
RectCheck(uint32_t curWidth,uint32_t curHeight)217 void MainSession::RectCheck(uint32_t curWidth, uint32_t curHeight)
218 {
219     uint32_t minWidth = GetSystemConfig().miniWidthOfMainWindow_;
220     uint32_t minHeight = GetSystemConfig().miniHeightOfMainWindow_;
221     uint32_t maxFloatingWindowSize = GetSystemConfig().maxFloatingWindowSize_;
222     RectSizeCheckProcess(curWidth, curHeight, minWidth, minHeight, maxFloatingWindowSize);
223 }
224 
SetExitSplitOnBackground(bool isExitSplitOnBackground)225 void MainSession::SetExitSplitOnBackground(bool isExitSplitOnBackground)
226 {
227     TLOGD(WmsLogTag::WMS_MULTI_WINDOW, "id:%{public}d, isExitSplitOnBackground:%{public}d", persistentId_,
228         isExitSplitOnBackground);
229     isExitSplitOnBackground_ = isExitSplitOnBackground;
230 }
231 
IsExitSplitOnBackground() const232 bool MainSession::IsExitSplitOnBackground() const
233 {
234     return isExitSplitOnBackground_;
235 }
236 
NotifyClientToUpdateInteractive(bool interactive)237 void MainSession::NotifyClientToUpdateInteractive(bool interactive)
238 {
239     if (!sessionStage_) {
240         return;
241     }
242     const auto state = GetSessionState();
243     TLOGI(WmsLogTag::WMS_LIFE, "state: %{public}d", state);
244     if (state == SessionState::STATE_ACTIVE || state == SessionState::STATE_FOREGROUND) {
245         TLOGI(WmsLogTag::WMS_LIFE, "interactive: %{public}d", interactive);
246         sessionStage_->NotifyForegroundInteractiveStatus(interactive);
247         isClientInteractive_ = interactive;
248     }
249 }
250 
251 /*
252  * Notify when updating highlight instead after hightlight functionality enabled
253  */
UpdateFocus(bool isFocused)254 WSError MainSession::UpdateFocus(bool isFocused)
255 {
256     if (FoldScreenStateInternel::IsSuperFoldDisplayDevice() && !IsFocused() && isFocused && pcFoldScreenController_) {
257         pcFoldScreenController_->UpdateSupportEnterWaterfallMode();
258     }
259     return Session::UpdateFocus(isFocused);
260 }
261 
OnTitleAndDockHoverShowChange(bool isTitleHoverShown,bool isDockHoverShown)262 WSError MainSession::OnTitleAndDockHoverShowChange(bool isTitleHoverShown, bool isDockHoverShown)
263 {
264     const char* const funcName = __func__;
265     PostTask([weakThis = wptr(this), isTitleHoverShown, isDockHoverShown, funcName] {
266         auto session = weakThis.promote();
267         if (!session) {
268             TLOGNE(WmsLogTag::WMS_LAYOUT_PC, "%{public}s session is null", funcName);
269             return;
270         }
271         TLOGND(WmsLogTag::WMS_LAYOUT_PC, "%{public}s isTitleHoverShown: %{public}d, isDockHoverShown: %{public}d",
272             funcName, isTitleHoverShown, isDockHoverShown);
273         if (session->onTitleAndDockHoverShowChangeFunc_) {
274             session->onTitleAndDockHoverShowChangeFunc_(isTitleHoverShown, isDockHoverShown);
275         }
276     }, funcName);
277     return WSError::WS_OK;
278 }
279 
OnRestoreMainWindow()280 WSError MainSession::OnRestoreMainWindow()
281 {
282     PostTask([weakThis = wptr(this)] {
283         auto session = weakThis.promote();
284         if (!session) {
285             TLOGNE(WmsLogTag::WMS_LAYOUT_PC, "session is null");
286             return;
287         }
288         if (session->onRestoreMainWindowFunc_) {
289             session->onRestoreMainWindowFunc_();
290         }
291     }, __func__);
292     return WSError::WS_OK;
293 }
294 
OnSetWindowRectAutoSave(bool enabled,bool isSaveBySpecifiedFlag)295 WSError MainSession::OnSetWindowRectAutoSave(bool enabled, bool isSaveBySpecifiedFlag)
296 {
297     const char* const where = __func__;
298     PostTask([weakThis = wptr(this), enabled, isSaveBySpecifiedFlag, where] {
299         auto session = weakThis.promote();
300         if (!session) {
301             TLOGNE(WmsLogTag::WMS_MAIN, "session is null");
302             return;
303         }
304         if (session->onSetWindowRectAutoSaveFunc_) {
305             session->onSetWindowRectAutoSaveFunc_(enabled, isSaveBySpecifiedFlag);
306             TLOGNI(WmsLogTag::WMS_MAIN, "%{public}s id %{public}d isSaveBySpecifiedFlag: %{public}d "
307                 "enable:%{public}d", where, session->GetPersistentId(), isSaveBySpecifiedFlag, enabled);
308         }
309     }, __func__);
310     return WSError::WS_OK;
311 }
312 
NotifySupportWindowModesChange(const std::vector<AppExecFwk::SupportWindowMode> & supportedWindowModes)313 WSError MainSession::NotifySupportWindowModesChange(
314     const std::vector<AppExecFwk::SupportWindowMode>& supportedWindowModes)
315 {
316     const char* const where = __func__;
317     PostTask([weakThis = wptr(this), supportedWindowModes = supportedWindowModes, where]() mutable {
318         auto session = weakThis.promote();
319         if (!session) {
320             TLOGNE(WmsLogTag::WMS_LAYOUT_PC, "%{public}s session is null", where);
321             return;
322         }
323         if (session->onSetSupportedWindowModesFunc_) {
324             session->onSetSupportedWindowModesFunc_(std::move(supportedWindowModes));
325         }
326     }, __func__);
327     return WSError::WS_OK;
328 }
329 
NotifyMainModalTypeChange(bool isModal)330 WSError MainSession::NotifyMainModalTypeChange(bool isModal)
331 {
332     const char* const where = __func__;
333     PostTask([weakThis = wptr(this), isModal, where] {
334         auto session = weakThis.promote();
335         if (!session) {
336             TLOGNE(WmsLogTag::WMS_LIFE, "%{public}s session is null", where);
337             return;
338         }
339         TLOGNI(WmsLogTag::WMS_HIERARCHY, "%{public}s main window isModal:%{public}d", where, isModal);
340         if (session->onMainModalTypeChange_) {
341             session->onMainModalTypeChange_(isModal);
342         }
343     }, __func__);
344     return WSError::WS_OK;
345 }
346 
IsModal() const347 bool MainSession::IsModal() const
348 {
349     return WindowHelper::IsModalMainWindow(GetSessionProperty()->GetWindowType(),
350                                            GetSessionProperty()->GetWindowFlags());
351 }
352 
IsApplicationModal() const353 bool MainSession::IsApplicationModal() const
354 {
355     return IsModal();
356 }
357 
RegisterSessionLockStateChangeCallback(NotifySessionLockStateChangeCallback && callback)358 void MainSession::RegisterSessionLockStateChangeCallback(NotifySessionLockStateChangeCallback&& callback)
359 {
360     PostTask([weakThis = wptr(this), callback = std::move(callback)] {
361         auto session = weakThis.promote();
362         if (!session) {
363             TLOGNE(WmsLogTag::WMS_MAIN, "session is null");
364             return;
365         }
366         session->onSessionLockStateChangeCallback_ = std::move(callback);
367         if (session->onSessionLockStateChangeCallback_ && session->GetSessionLockState()) {
368             session->onSessionLockStateChangeCallback_(session->GetSessionLockState());
369         }
370     }, __func__);
371 }
372 
NotifySubAndDialogFollowRectChange(const WSRect & rect,bool isGlobal,bool needFlush)373 void MainSession::NotifySubAndDialogFollowRectChange(const WSRect& rect, bool isGlobal, bool needFlush)
374 {
375     std::unordered_map<int32_t, NotifySurfaceBoundsChangeFunc> funcMap;
376     {
377         std::lock_guard lock(registerNotifySurfaceBoundsChangeMutex_);
378         funcMap = notifySurfaceBoundsChangeFuncMap_;
379     }
380     WSRect newRect;
381     for (const auto& [sessionId, func] : funcMap) {
382         auto subSession = GetSceneSessionById(sessionId);
383         if (subSession && subSession->GetIsFollowParentLayout() && func) {
384             if (newRect.IsEmpty()) {
385                 HookStartMoveRect(newRect, rect);
386             }
387             func(newRect, isGlobal, needFlush);
388         }
389     }
390 }
391 
SetSubWindowBoundsDuringCross(const WSRect & parentRect,bool isGlobal,bool needFlush)392 void MainSession::SetSubWindowBoundsDuringCross(const WSRect& parentRect, bool isGlobal, bool needFlush)
393 {
394     for (const auto& subSession : GetSubSession()) {
395         if (subSession && subSession->GetWindowAnchorInfo().isAnchorEnabled_ && subSession->IsSessionForeground()) {
396             WSRect subRect = subSession->GetSessionRect();
397             subSession->CalcSubWindowRectByAnchor(parentRect, subRect);
398             subSession->UpdateSizeChangeReason(SizeChangeReason::UNDEFINED);
399             subSession->SetSurfaceBounds(subRect, isGlobal, needFlush);
400         }
401     }
402 }
403 
NotifySubSessionRectChangeByAnchor(const WSRect & parentRect,SizeChangeReason reason,DisplayId displayId)404 void MainSession::NotifySubSessionRectChangeByAnchor(const WSRect& parentRect,
405     SizeChangeReason reason, DisplayId displayId)
406 {
407     for (const auto& subSession : GetSubSession()) {
408         if (subSession && subSession->GetWindowAnchorInfo().isAnchorEnabled_) {
409             WSRect subRect = subSession->GetSessionRect();
410             subSession->CalcSubWindowRectByAnchor(parentRect, subRect);
411             subSession->NotifySessionRectChange(subRect, reason, displayId);
412         }
413     }
414 }
415 
HandleSubSessionSurfaceNodeByWindowAnchor(SizeChangeReason reason,const sptr<ScreenSession> & screenSession)416 void MainSession::HandleSubSessionSurfaceNodeByWindowAnchor(SizeChangeReason reason,
417     const sptr<ScreenSession>& screenSession)
418 {
419     for (const auto& subSession : GetSubSession()) {
420         if (subSession && subSession->GetWindowAnchorInfo().isAnchorEnabled_ && subSession->IsSessionForeground()) {
421             subSession->HandleCrossSurfaceNodeByWindowAnchor(reason, screenSession);
422         }
423     }
424 }
425 
NotifySessionLockStateChange(bool isLockedState)426 void MainSession::NotifySessionLockStateChange(bool isLockedState)
427 {
428     PostTask([weakThis = wptr(this), isLockedState] {
429         auto session = weakThis.promote();
430         if (!session) {
431             TLOGNE(WmsLogTag::WMS_MAIN, "session is null");
432             return;
433         }
434         if (session->GetSessionLockState() == isLockedState) {
435             TLOGNW(WmsLogTag::WMS_MAIN, "isLockedState is already %{public}d", isLockedState);
436             return;
437         }
438         session->SetSessionLockState(isLockedState);
439         if (session->onSessionLockStateChangeCallback_) {
440             TLOGNI(WmsLogTag::WMS_MAIN, "onSessionLockStageChange to:%{public}d", isLockedState);
441             session->onSessionLockStateChangeCallback_(isLockedState);
442         }
443     }, __func__);
444 }
445 
SetSessionLockState(bool isLockedState)446 void MainSession::SetSessionLockState(bool isLockedState)
447 {
448     isLockedState_ = isLockedState;
449 }
450 
GetSessionLockState() const451 bool MainSession::GetSessionLockState() const
452 {
453     return isLockedState_;
454 }
455 
SetSessionLabelAndIcon(const std::string & label,const std::shared_ptr<Media::PixelMap> & icon)456 WSError MainSession::SetSessionLabelAndIcon(const std::string& label,
457     const std::shared_ptr<Media::PixelMap>& icon)
458 {
459     TLOGI(WmsLogTag::WMS_MAIN, "id: %{public}d", persistentId_);
460     int32_t callingPid = IPCSkeleton::GetCallingPid();
461     const bool pidCheck = (callingPid != -1) && (callingPid == GetCallingPid());
462     if (!pidCheck ||
463         !SessionPermission::VerifyCallingPermission(PermissionConstants::PERMISSION_SET_ABILITY_INSTANCE_INFO)) {
464         TLOGE(WmsLogTag::WMS_MAIN,
465             "The caller has not permission granted or not the same processs, "
466             "callingPid_: %{public}d, callingPid: %{public}d, bundleName: %{public}s",
467             GetCallingPid(), callingPid, GetSessionInfo().bundleName_.c_str());
468         return WSError::WS_ERROR_INVALID_PERMISSION;
469     }
470     if (!systemConfig_.IsPcWindow()) {
471         TLOGE(WmsLogTag::WMS_MAIN, "device not support");
472         return WSError::WS_ERROR_DEVICE_NOT_SUPPORT;
473     }
474     if (label.empty() || label.length() > MAX_LABEL_SIZE) {
475         TLOGE(WmsLogTag::WMS_MAIN, "invalid label");
476         return WSError::WS_ERROR_SET_SESSION_LABEL_FAILED;
477     }
478     return SetSessionLabelAndIconInner(label, icon);
479 }
480 
SetSessionLabelAndIconInner(const std::string & label,const std::shared_ptr<Media::PixelMap> & icon)481 WSError MainSession::SetSessionLabelAndIconInner(const std::string& label,
482     const std::shared_ptr<Media::PixelMap>& icon)
483 {
484     const char* const where = __func__;
485     PostTask([weakThis = wptr(this), where, label, icon] {
486         auto session = weakThis.promote();
487         if (session == nullptr) {
488             TLOGNE(WmsLogTag::WMS_MAIN, "%{public}s session is nullptr", where);
489             return WSError::WS_ERROR_NULLPTR;
490         }
491         if (session->updateSessionLabelAndIconFunc_) {
492             session->updateSessionLabelAndIconFunc_(label, icon);
493         }
494         return WSError::WS_OK;
495     }, __func__);
496     return WSError::WS_OK;
497 }
498 
SetUpdateSessionLabelAndIconListener(NofitySessionLabelAndIconUpdatedFunc && func)499 void MainSession::SetUpdateSessionLabelAndIconListener(NofitySessionLabelAndIconUpdatedFunc&& func)
500 {
501     const char* const where = __func__;
502     PostTask([weakThis = wptr(this), func = std::move(func), where] {
503         auto session = weakThis.promote();
504         if (!session) {
505             TLOGNE(WmsLogTag::WMS_MAIN, "%{public}s session is null", where);
506             return;
507         }
508         session->updateSessionLabelAndIconFunc_ = std::move(func);
509     }, __func__);
510 }
511 
UpdateFlag(const std::string & flag)512 WSError MainSession::UpdateFlag(const std::string& flag)
513 {
514     const char* const where = __func__;
515     PostTask([weakThis = wptr(this), flag, where] {
516         auto session = weakThis.promote();
517         if (!session) {
518             TLOGNE(WmsLogTag::WMS_MAIN, "session is null");
519             return;
520         }
521         session->sessionInfo_.specifiedFlag_ = flag;
522         if (session->onUpdateFlagFunc_) {
523             session->onUpdateFlagFunc_(flag);
524             TLOGND(WmsLogTag::WMS_MAIN, "%{public}s id %{public}d flag: %{public}s",
525                 where, session->GetPersistentId(), flag.c_str());
526         }
527     }, __func__);
528     return WSError::WS_OK;
529 }
530 
GetRouterStackInfo(std::string & routerStackInfo) const531 WMError MainSession::GetRouterStackInfo(std::string& routerStackInfo) const
532 {
533     if (!sessionStage_) {
534         TLOGE(WmsLogTag::WMS_LIFE, "sessionStage is nullptr");
535         return WMError::WM_ERROR_NULLPTR;
536     }
537     return sessionStage_->GetRouterStackInfo(routerStackInfo);
538 }
539 
SetRecentSessionState(RecentSessionInfo & info,const SessionState & state)540 void MainSession::SetRecentSessionState(RecentSessionInfo& info, const SessionState& state)
541 {
542     switch (state) {
543         case SessionState::STATE_DISCONNECT: {
544             info.sessionState = RecentSessionState::DISCONNECT;
545             break;
546         }
547         case SessionState::STATE_CONNECT: {
548             info.sessionState = RecentSessionState::CONNECT;
549             break;
550         }
551         case SessionState::STATE_FOREGROUND: {
552             info.sessionState = RecentSessionState::FOREGROUND;
553             break;
554         }
555         case SessionState::STATE_BACKGROUND: {
556             info.sessionState = RecentSessionState::BACKGROUND;
557             break;
558         }
559         case SessionState::STATE_ACTIVE: {
560             info.sessionState = RecentSessionState::ACTIVE;
561             break;
562         }
563         case SessionState::STATE_INACTIVE: {
564             info.sessionState = RecentSessionState::INACTIVE;
565             break;
566         }
567         default: {
568             info.sessionState = RecentSessionState::END;
569             break;
570         }
571     }
572 }
573 } // namespace OHOS::Rosen
574