diff --git a/window_scene/interfaces/include/ws_common.h b/window_scene/interfaces/include/ws_common.h index fc808aab11875a3469c6183e2148a4a8cd98711a..8c0f64fe46a0b89114abc9ecad963c16038d12d6 100644 --- a/window_scene/interfaces/include/ws_common.h +++ b/window_scene/interfaces/include/ws_common.h @@ -144,6 +144,7 @@ struct SessionInfo { uint32_t callState_ = 0; uint32_t callingTokenId_ = 0; bool reuse = false; + int32_t windowMode = 0; StartMethod startMethod = StartMethod::START_NORMAL; bool lockedState = false; std::string time; diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_root_scene_session.cpp b/window_scene/interfaces/kits/napi/scene_session_manager/js_root_scene_session.cpp index 2c1e0238de19d159a2971e3d567b396c3806caed..e4327e59a4b7b8e3b8710b07d4ff5be70bc72924 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_root_scene_session.cpp +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_root_scene_session.cpp @@ -200,9 +200,14 @@ void JsRootSceneSession::PendingSessionActivation(SessionInfo& info) int32_t realCallerSessionId = SceneSessionManager::GetInstance().GetFocusedSession(); WLOGFI("[NAPI]need to back to other session: %{public}d", realCallerSessionId); if (sceneSession != nullptr) { - sceneSession->GetSessionInfo().persistentId_ = realCallerSessionId; + sceneSession->GetSessionInfo().callerPersistentId_ = realCallerSessionId; } info.callerPersistentId_ = realCallerSessionId; + } else { + info.callerPersistentId_ = 0; + if (sceneSession != nullptr) { + sceneSession->GetSessionInfo().callerPersistentId_ = 0; + } } } auto iter = jsCbMap_.find(PENDING_SCENE_CB); diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp index 79b368b074f707de49faf756584c66018e694ea1..cfb561c873fed1e5e18e79119bb61dad52b128ae 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.cpp @@ -428,8 +428,8 @@ void JsSceneSession::OnSessionEvent(uint32_t eventId) void JsSceneSession::ProcessBackPressedRegister() { - NotifyBackPressedFunc func = [this]() { - this->OnBackPressed(); + NotifyBackPressedFunc func = [this](bool needMoveToBackground) { + this->OnBackPressed(needMoveToBackground); }; auto session = weakSession_.promote(); if (session == nullptr) { @@ -1006,21 +1006,23 @@ void JsSceneSession::PendingSessionActivationInner(SessionInfo& info) std::make_unique(callback, std::move(execute), std::move(complete))); } -void JsSceneSession::OnBackPressed() +void JsSceneSession::OnBackPressed(bool needMoveToBackground) { - WLOGFI("[NAPI]OnBackPressed"); + WLOGFI("[NAPI]OnBackPressed needMoveToBackground %{public}d", needMoveToBackground); auto iter = jsCbMap_.find(BACK_PRESSED_CB); if (iter == jsCbMap_.end()) { return; } auto jsCallBack = iter->second; auto complete = std::make_unique( - [jsCallBack, eng = &engine_](NativeEngine& engine, AsyncTask& task, int32_t status) { + [needMoveToBackground, jsCallBack, eng = &engine_](NativeEngine& engine, AsyncTask& task, int32_t status) { if (!jsCallBack) { WLOGFE("[NAPI]jsCallBack is nullptr"); return; } - engine.CallFunction(engine.CreateUndefined(), jsCallBack->Get(), {}, 0); + NativeValue* jsNeedMoveToBackgroundObj = CreateJsValue(engine, needMoveToBackground); + NativeValue* argv[] = { jsNeedMoveToBackgroundObj }; + engine.CallFunction(engine.CreateUndefined(), jsCallBack->Get(), argv, ArraySize(argv)); }); NativeReference* callback = nullptr; diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h index bd15f7c37baced72326d538b73508df556c6b460..b6ebee4abdbee8a230ffc6ab081cb3a1fd381178 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session.h @@ -89,7 +89,7 @@ private: void OnSessionRectChange(const WSRect& rect, const SizeChangeReason& reason = SizeChangeReason::UNDEFINED); void OnRaiseToTop(); void OnRaiseAboveTarget(int32_t subWindowId); - void OnBackPressed(); + void OnBackPressed(bool needMoveToBackground); void OnSessionFocusableChange(bool isFocusable); void OnSessionTouchableChange(bool touchable); void OnClick(); diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.cpp b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.cpp index caa4e4f65848c44a8e13a9c30ce09728eae7218a..b68f6dd2d226e3165df0c66fb27c6c7ff2c9f116 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.cpp +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.cpp @@ -170,6 +170,7 @@ NativeValue* CreateJsSessionInfo(NativeEngine& engine, const SessionInfo& sessio object->SetProperty("callerPersistentId", CreateJsValue(engine, static_cast(sessionInfo.callerPersistentId_))); object->SetProperty("callState", CreateJsValue(engine, static_cast(sessionInfo.callState_))); + object->SetProperty("windowMode", CreateJsValue(engine, static_cast(sessionInfo.windowMode))); return objValue; } diff --git a/window_scene/session/host/include/session.h b/window_scene/session/host/include/session.h index 115aa2c2d93db0a0d3be8ac3132e7df817f6d336..39e207e974c932885a20465d64e24f44b2869472 100644 --- a/window_scene/session/host/include/session.h +++ b/window_scene/session/host/include/session.h @@ -44,7 +44,7 @@ class RSSurfaceNode; using NotifyPendingSessionActivationFunc = std::function; using NotifySessionStateChangeFunc = std::function; using NotifySessionStateChangeNotifyManagerFunc = std::function; -using NotifyBackPressedFunc = std::function; +using NotifyBackPressedFunc = std::function; using NotifySessionFocusableChangeFunc = std::function; using NotifySessionTouchableChangeFunc = std::function; using NotifyClickFunc = std::function; @@ -160,7 +160,7 @@ public: void SetSystemConfig(const SystemSessionConfig& systemConfig); void SetBackPressedListenser(const NotifyBackPressedFunc& func); WSError ProcessBackEvent(); // send back event to session_stage - WSError RequestSessionBack() override; // receive back request from session_stage + WSError RequestSessionBack(bool needMoveToBackground) override; // receive back request from session_stage WSError MarkProcessed(int32_t eventId) override; sptr GetScenePersistence() const; diff --git a/window_scene/session/host/include/zidl/session_interface.h b/window_scene/session/host/include/zidl/session_interface.h index 4cbb18378f407ae03c780ed0af05caf691d5c95c..d1fd5d69b6b625d0592527d19de1091802072a4f 100644 --- a/window_scene/session/host/include/zidl/session_interface.h +++ b/window_scene/session/host/include/zidl/session_interface.h @@ -89,7 +89,7 @@ public: virtual WSError DestroyAndDisconnectSpecificSession(const int32_t& persistentId) = 0; virtual WSError OnNeedAvoid(bool status) = 0; virtual AvoidArea GetAvoidAreaByType(AvoidAreaType type) = 0; - virtual WSError RequestSessionBack() = 0; + virtual WSError RequestSessionBack(bool needMoveToBackground) = 0; virtual WSError MarkProcessed(int32_t eventId) = 0; virtual WSError SetGlobalMaximizeMode(MaximizeMode mode) = 0; virtual WSError GetGlobalMaximizeMode(MaximizeMode& mode) = 0; diff --git a/window_scene/session/host/include/zidl/session_proxy.h b/window_scene/session/host/include/zidl/session_proxy.h index c3fc956c4623c56a92843bf6a92a18d5da802f4a..476ed5c32e95f9a80569b50bdf40a9f975018c47 100644 --- a/window_scene/session/host/include/zidl/session_proxy.h +++ b/window_scene/session/host/include/zidl/session_proxy.h @@ -48,7 +48,7 @@ public: WSError DestroyAndDisconnectSpecificSession(const int32_t& persistentId) override; WSError OnNeedAvoid(bool status) override; AvoidArea GetAvoidAreaByType(AvoidAreaType type) override; - WSError RequestSessionBack() override; + WSError RequestSessionBack(bool needMoveToBackground) override; WSError MarkProcessed(int32_t eventId) override; WSError SetGlobalMaximizeMode(MaximizeMode mode) override; WSError GetGlobalMaximizeMode(MaximizeMode& mode) override; diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 7d4a43abe05acceb3db546fd15de3108674dd795..81293b8603585dc086ff80dbf6f19de89e6825e9 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -577,11 +577,14 @@ WSError Session::PendingSessionActivation(const sptr ability info.startSetting = abilitySessionInfo->startSetting; info.callingTokenId_ = abilitySessionInfo->callingTokenId; info.reuse = abilitySessionInfo->reuse; + if (info.want != nullptr) { + info.windowMode = info.want->GetIntParam(AAFwk::Want::PARAM_RESV_WINDOW_MODE, 0); + } WLOGFI("PendingSessionActivation:bundleName %{public}s, moduleName:%{public}s, abilityName:%{public}s", info.bundleName_.c_str(), info.moduleName_.c_str(), info.abilityName_.c_str()); - WLOGFI("PendingSessionActivation callState:%{public}d, want persistentId: %{public}d, callingTokenId:%{public}d, \ - uiAbilityId: %{public}" PRIu64 "", info.callState_, info.persistentId_, info.callingTokenId_, - info.uiAbilityId_); + WLOGFI("PendingSessionActivation callState:%{public}d, want persistentId: %{public}d, callingTokenId:%{public}d," \ + "uiAbilityId: %{public}" PRIu64 ", windowMode: %{public}d", + info.callState_, info.persistentId_, info.callingTokenId_, info.uiAbilityId_, info.windowMode); if (pendingSessionActivationFunc_) { pendingSessionActivationFunc_(info); } @@ -1187,13 +1190,13 @@ void Session::SetSystemConfig(const SystemSessionConfig& systemConfig) systemConfig_ = systemConfig; } -WSError Session::RequestSessionBack() +WSError Session::RequestSessionBack(bool needMoveToBackground) { if (!backPressedFunc_) { WLOGFW("Session didn't register back event consumer!"); return WSError::WS_DO_NOTHING; } - backPressedFunc_(); + backPressedFunc_(needMoveToBackground); return WSError::WS_OK; } diff --git a/window_scene/session/host/src/zidl/session_proxy.cpp b/window_scene/session/host/src/zidl/session_proxy.cpp index 708631df395924aebb68058a341df50f4a1d0732..89794d2ccb7a61d97c4e3cb1bb9b5de054d0673f 100644 --- a/window_scene/session/host/src/zidl/session_proxy.cpp +++ b/window_scene/session/host/src/zidl/session_proxy.cpp @@ -542,7 +542,7 @@ AvoidArea SessionProxy::GetAvoidAreaByType(AvoidAreaType type) return *area; } -WSError SessionProxy::RequestSessionBack() +WSError SessionProxy::RequestSessionBack(bool needMoveToBackground) { MessageParcel data; MessageParcel reply; @@ -551,6 +551,10 @@ WSError SessionProxy::RequestSessionBack() WLOGFE("WriteInterfaceToken failed"); return WSError::WS_ERROR_IPC_FAILED; } + if (!data.WriteBool(needMoveToBackground)) { + WLOGFE("Write needMoveToBackground failed"); + return WSError::WS_ERROR_IPC_FAILED; + } if (Remote()->SendRequest(static_cast(SessionMessage::TRANS_ID_BACKPRESSED), data, reply, option) != ERR_NONE) { WLOGFE("SendRequest failed"); diff --git a/window_scene/session/host/src/zidl/session_stub.cpp b/window_scene/session/host/src/zidl/session_stub.cpp index 6a1ff881fa0c9617cab4e09aed41491e1955258c..92e00aea91fe8b101e04e54a6a426f54e8a82c94 100644 --- a/window_scene/session/host/src/zidl/session_stub.cpp +++ b/window_scene/session/host/src/zidl/session_stub.cpp @@ -332,7 +332,12 @@ int SessionStub::HandleRaiseAboveTarget(MessageParcel& data, MessageParcel& repl int SessionStub::HandleBackPressed(MessageParcel& data, MessageParcel& reply) { WLOGFD("HandleBackPressed!"); - WSError errCode = RequestSessionBack(); + bool needMoveToBackground = false; + if (!data.ReadBool(needMoveToBackground)) { + WLOGFE("Read needMoveToBackground from parcel failed!"); + return ERR_INVALID_DATA; + } + WSError errCode = RequestSessionBack(needMoveToBackground); reply.WriteUint32(static_cast(errCode)); return ERR_NONE; } diff --git a/window_scene/test/mock/mock_session.h b/window_scene/test/mock/mock_session.h index c45d128dec987076aede370f4e522e78e11f9667..d0c2b63c5beb7e3ad662193af1b8eea14204a494 100644 --- a/window_scene/test/mock/mock_session.h +++ b/window_scene/test/mock/mock_session.h @@ -43,7 +43,7 @@ public: MOCK_METHOD1(PendingSessionActivation, WSError(const sptr info)); MOCK_METHOD1(UpdateActiveStatus, WSError(bool isActive)); MOCK_METHOD1(OnSessionEvent, WSError(SessionEvent event)); - MOCK_METHOD0(RequestSessionBack, WSError(void)); + MOCK_METHOD1(RequestSessionBack, WSError(bool needMoveToBackground)); MOCK_METHOD0(RaiseToAppTop, WSError(void)); MOCK_METHOD1(GetAvoidAreaByType, AvoidArea(AvoidAreaType type)); MOCK_METHOD1(SetAspectRatio, WSError(float ratio)); diff --git a/window_scene/test/unittest/session_test.cpp b/window_scene/test/unittest/session_test.cpp index 04000dd2b26a69a22858285f80950889b6209fe0..08525fade90a78d25f84bb64d8b5f8789acdb720 100644 --- a/window_scene/test/unittest/session_test.cpp +++ b/window_scene/test/unittest/session_test.cpp @@ -473,12 +473,12 @@ HWTEST_F(WindowSessionTest, RequestSessionBack, Function | SmallTest | Level2) { ASSERT_NE(session_, nullptr); - ASSERT_EQ(WSError::WS_DO_NOTHING, session_->RequestSessionBack()); + ASSERT_EQ(WSError::WS_DO_NOTHING, session_->RequestSessionBack(false)); - NotifyBackPressedFunc callback = []() {}; + NotifyBackPressedFunc callback = [](bool needMoveToBackground) {}; session_->SetBackPressedListenser(callback); - ASSERT_EQ(WSError::WS_OK, session_->RequestSessionBack()); + ASSERT_EQ(WSError::WS_OK, session_->RequestSessionBack(false)); } /** diff --git a/wm/src/window_scene_session_impl.cpp b/wm/src/window_scene_session_impl.cpp index 0271455de5018817d46c09e07093b61531a1a84d..dc4d6df9debee1dcfbf33a646032c76543c74393 100644 --- a/wm/src/window_scene_session_impl.cpp +++ b/wm/src/window_scene_session_impl.cpp @@ -1164,16 +1164,37 @@ WSError WindowSceneSessionImpl::HandleBackEvent() } WLOGFD("report Back"); SingletonContainer::Get().ReportBackButtonInfoImmediately(); + if (handler_ == nullptr) { + WLOGFE("HandleBackEvent handler_ is nullptr!"); + return WSError::WS_ERROR_INVALID_PARAM; + } // notify back event to host session - PerformBack(); + wptr weak = this; + auto task = [weak]() { + auto weakSession = weak.promote(); + if (weakSession == nullptr) { + WLOGFE("HandleBackEvent session wptr is nullptr"); + return; + } + weakSession->PerformBack(); + }; + if (!handler_->PostTask(task)) { + WLOGFE("Failed to post PerformBack"); + return WSError::WS_ERROR_INVALID_OPERATION; + } return WSError::WS_OK; } void WindowSceneSessionImpl::PerformBack() { if (hostSession_) { - WLOGFD("Transfer back event to host session"); - hostSession_->RequestSessionBack(); + bool needMoveToBackground = false; + auto abilityContext = AbilityRuntime::Context::ConvertTo(context_); + if (abilityContext != nullptr) { + abilityContext->OnBackPressedCallBack(needMoveToBackground); + } + WLOGFI("Transfer back event to host session needMoveToBackground %{public}d", needMoveToBackground); + hostSession_->RequestSessionBack(needMoveToBackground); } }