diff --git a/interfaces/kits/napi/window_runtime/BUILD.gn b/interfaces/kits/napi/window_runtime/BUILD.gn index 790df88a74715a7cd107670dbfad5b849894ae94..5b6a00e7e666d93ef967f5e44c9629090021c468 100644 --- a/interfaces/kits/napi/window_runtime/BUILD.gn +++ b/interfaces/kits/napi/window_runtime/BUILD.gn @@ -60,6 +60,7 @@ ohos_shared_library("window_native_kit") { deps = [ "//foundation/window/window_manager/utils:libwmutil", "//foundation/window/window_manager/wm:libwm", + "//foundation/window/window_manager/window_scene/session:scene_session", ] external_deps = [ @@ -75,6 +76,7 @@ ohos_shared_library("window_native_kit") { "multimedia_image_framework:image", "multimedia_image_framework:image_native", "napi:ace_napi", + "ace_engine:ace_uicontent", ] part_name = "window_manager" @@ -96,6 +98,7 @@ ohos_shared_library("window_napi") { ":window_native_kit", "//foundation/window/window_manager/utils:libwmutil", "//foundation/window/window_manager/wm:libwm", + "${window_base_path}/window_scene/session_manager:session_manager", ] external_deps = [ @@ -106,6 +109,7 @@ ohos_shared_library("window_napi") { "hitrace_native:hitrace_meter", "hiviewdfx_hilog_native:libhilog", "napi:ace_napi", + "ace_engine:ace_uicontent", ] relative_install_dir = "module" @@ -127,6 +131,8 @@ ohos_shared_library("windowstage_kit") { ":window_native_kit", "//foundation/window/window_manager/utils:libwmutil", "//foundation/window/window_manager/wm:libwm", + "//foundation/window/window_manager/window_scene/session:scene_session", + "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client", ] external_deps = [ diff --git a/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp b/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp index 086e4011cf3f197cfb83d05dc801d5f164dfe4a2..78121c372b45f7258dd76debee9a76f0da4088a7 100644 --- a/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp +++ b/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp @@ -27,6 +27,9 @@ #include "window_manager_hilog.h" #include "window_option.h" #include "singleton_container.h" +#include "session_manager/include/session_manager.h" +#include "ui_window.h" + namespace OHOS { namespace Rosen { using namespace AbilityRuntime; @@ -223,16 +226,28 @@ static void CreateNewSystemWindowTask(void* contextPtr, sptr windo } } } - WMError wmError = WMError::WM_OK; - sptr window = Window::Create(windowOption->GetWindowName(), windowOption, context->lock(), wmError); - WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(wmError); - if (window != nullptr && wmErrorCode == WmErrorCode::WM_OK) { - task.Resolve(engine, CreateJsWindowObject(engine, window)); - } else { - WLOGFE("Create window failed"); - int32_t err = static_cast(wmErrorCode); - task.Reject(engine, CreateJsError(engine, err, "Create window failed")); + sptr session; + SessionManager::GetInstance().CreateSpecificSession(SessionType::TYPE_FLOAT, session); + std::shared_ptr systemUIWindow = Ace::NG::UIWindow::CreateSpecificWindowScene( + context->lock(), session); + if (systemUIWindow == nullptr) { + WLOGFE("[NAPI]CreateSpecificWindowScene window failed"); + task.Reject(engine, CreateJsError(engine, + static_cast(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "Get window failed")); + return; } + task.Resolve(engine, CreateJsUIWindowObject(engine, systemUIWindow)); + WLOGFI("[NAPI]Create system window success"); + + // sptr window = Window::Create(windowOption->GetWindowName(), windowOption, context->lock(), wmError); + // WmErrorCode wmErrorCode = WM_JS_TO_ERROR_CODE_MAP.at(wmError); + // if (window != nullptr && wmErrorCode == WmErrorCode::WM_OK) { + // task.Resolve(engine, CreateJsWindowObject(engine, window)); + // } else { + // WLOGFE("Create window failed"); + // int32_t err = static_cast(wmErrorCode); + // task.Reject(engine, CreateJsError(engine, err, "Create window failed")); + // } } static void CreateSystemWindowTask(void* contextPtr, std::string windowName, WindowType winType, diff --git a/interfaces/kits/napi/window_runtime/window_napi/js_window.cpp b/interfaces/kits/napi/window_runtime/window_napi/js_window.cpp index 3f68e9d1bb3c4b0e72a35b2e7f77c14c479c31bd..a658ff60851e401a7aee02c2d23ccd037bca8ce7 100644 --- a/interfaces/kits/napi/window_runtime/window_napi/js_window.cpp +++ b/interfaces/kits/napi/window_runtime/window_napi/js_window.cpp @@ -22,6 +22,8 @@ #include "mock/js_transition_controller.h" #endif +#include + #include "js_window_utils.h" #include "window.h" #include "window_helper.h" @@ -33,6 +35,7 @@ #include "napi_remote_object.h" #include "permission.h" #include "request_info.h" +#include "session/container/include/session_stage.h" namespace OHOS { namespace Rosen { @@ -43,6 +46,7 @@ namespace { } static thread_local std::map> g_jsWindowMap; +static thread_local std::map> g_jsUIWindowMap; std::recursive_mutex g_mutex; static int ctorCnt = 0; static int dtorCnt = 0; @@ -60,13 +64,20 @@ JsWindow::JsWindow(const sptr& window) WLOGI("Destroy window %{public}s in js window", windowName.c_str()); }; windowToken_->RegisterWindowDestroyedListener(func); - WLOGI(" constructorCnt: %{public}d", ++ctorCnt); + WLOGI("constructorCnt: %{public}d", ++ctorCnt); +} + +JsWindow::JsWindow(const std::shared_ptr& uiWindow, const sptr& window) + : uiWindowToken_(uiWindow), windowToken_(window), registerManager_(std::make_unique()) +{ + WLOGI("constructorCnt: %{public}d", ++ctorCnt); } JsWindow::~JsWindow() { - WLOGI(" deConstructorCnt:%{public}d", ++dtorCnt); + WLOGI("deConstructorCnt:%{public}d", ++dtorCnt); windowToken_ = nullptr; + uiWindowToken_ = nullptr; } std::string JsWindow::GetWindowName() @@ -77,6 +88,14 @@ std::string JsWindow::GetWindowName() return windowToken_->GetWindowName(); } +std::string JsWindow::GetUIWindowName() +{ + if (uiWindowToken_ == nullptr) { + return ""; + } + return uiWindowToken_->GetSurfaceNode()->GetName(); +} + void JsWindow::Finalizer(NativeEngine* engine, void* data, void* hint) { WLOGI("finalizerCnt:%{public}d", ++finalizerCnt); @@ -91,6 +110,20 @@ void JsWindow::Finalizer(NativeEngine* engine, void* data, void* hint) WLOGI("Remove window %{public}s from g_jsWindowMap", windowName.c_str()); } +void JsWindow::FinalizerUIWindow(NativeEngine* engine, void* data, void* hint) +{ + WLOGI("finalizerCnt:%{public}d", ++finalizerCnt); + auto jsWin = std::unique_ptr(static_cast(data)); + if (jsWin == nullptr) { + WLOGFE("jsWin is nullptr"); + return; + } + std::string windowName = jsWin->GetUIWindowName(); + std::lock_guard lock(g_mutex); + g_jsUIWindowMap.erase(windowName); + WLOGI("Remove window %{public}s from g_jsUIWindowMap", windowName.c_str()); +} + NativeValue* JsWindow::Show(NativeEngine* engine, NativeCallbackInfo* info) { WLOGI("Show"); @@ -604,28 +637,37 @@ NativeValue* JsWindow::ResetAspectRatio(NativeEngine* engine, NativeCallbackInfo NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info) { + WLOGFE("OnShow"); WMError errCode = WMError::WM_OK; if (info.argc > 1) { WLOGFE("Argc is invalid: %{public}zu", info.argc); errCode = WMError::WM_ERROR_INVALID_PARAM; } wptr weakToken(windowToken_); - AsyncTask::CompleteCallback complete = - [weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) { - auto weakWindow = weakToken.promote(); - if (weakWindow == nullptr || errCode != WMError::WM_OK) { - task.Reject(engine, CreateJsError(engine, static_cast(errCode))); - WLOGFE("window is nullptr or get invalid param"); - return; - } - WMError ret = weakWindow->Show(0, false); - if (ret == WMError::WM_OK) { - task.Resolve(engine, engine.CreateUndefined()); - } else { - task.Reject(engine, CreateJsError(engine, static_cast(ret), "Window show failed")); - } - WLOGI("Window [%{public}u] show end, ret = %{public}d", weakWindow->GetWindowId(), ret); - }; + std::weak_ptr weakUIWindow(uiWindowToken_); + AsyncTask::CompleteCallback complete = [weakToken, weakUIWindow, errCode]( + NativeEngine& engine, AsyncTask& task, int32_t status) { + auto uiWindow = weakUIWindow.lock(); + if (uiWindow) { + uiWindow->Foreground(); + task.Resolve(engine, engine.CreateUndefined()); + WLOGFI("Foreground UIWindow"); + return; + } + auto weakWindow = weakToken.promote(); + if (weakWindow == nullptr || errCode != WMError::WM_OK) { + task.Reject(engine, CreateJsError(engine, static_cast(errCode))); + WLOGFE("window is nullptr or get invalid param"); + return; + } + WMError ret = weakWindow->Show(0, false); + if (ret == WMError::WM_OK) { + task.Resolve(engine, engine.CreateUndefined()); + } else { + task.Reject(engine, CreateJsError(engine, static_cast(ret), "Window show failed")); + } + WLOGI("Window [%{public}u] show end, ret = %{public}d", weakWindow->GetWindowId(), ret); + }; NativeValue* result = nullptr; NativeValue* lastParam = (info.argc == 0) ? nullptr : (info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr); @@ -637,8 +679,16 @@ NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info) NativeValue* JsWindow::OnShowWindow(NativeEngine& engine, NativeCallbackInfo& info) { wptr weakToken(windowToken_); + std::weak_ptr weakUIWindow(uiWindowToken_); AsyncTask::CompleteCallback complete = - [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) { + [weakToken, weakUIWindow](NativeEngine& engine, AsyncTask& task, int32_t status) { + auto uiWindow = weakUIWindow.lock(); + if (uiWindow) { + uiWindow->Foreground(); + task.Resolve(engine, engine.CreateUndefined()); + WLOGFI("Show UIWindow"); + return; + } auto weakWindow = weakToken.promote(); if (weakWindow == nullptr) { task.Reject(engine, CreateJsError(engine, @@ -713,8 +763,18 @@ NativeValue* JsWindow::OnDestroy(NativeEngine& engine, NativeCallbackInfo& info) errCode = WMError::WM_ERROR_INVALID_PARAM; } wptr weakToken(windowToken_); + std::weak_ptr weakUIWindow(uiWindowToken_); AsyncTask::CompleteCallback complete = - [this, weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) { + [this, weakToken, errCode, weakUIWindow](NativeEngine& engine, AsyncTask& task, int32_t status) { + auto uiWindow = weakUIWindow.lock(); + if (uiWindow) { + uiWindow->SetActive(false); + uiWindow->Disconnect(); + task.Resolve(engine, engine.CreateUndefined()); + WLOGFI("Destroy UIWindow"); + return; + } + auto weakWindow = weakToken.promote(); if (weakWindow == nullptr || errCode != WMError::WM_OK) { task.Reject(engine, CreateJsError(engine, static_cast(errCode))); @@ -777,9 +837,18 @@ NativeValue* JsWindow::OnDestroyWindow(NativeEngine& engine, NativeCallbackInfo& NativeValue* JsWindow::OnHide(NativeEngine& engine, NativeCallbackInfo& info) { wptr weakToken(windowToken_); + std::weak_ptr weakUIWindow(uiWindowToken_); AsyncTask::CompleteCallback complete = - [weakToken](NativeEngine& engine, AsyncTask& task, int32_t status) { + [weakToken, weakUIWindow](NativeEngine& engine, AsyncTask& task, int32_t status) { auto weakWindow = weakToken.promote(); + auto uiWindow = weakUIWindow.lock(); + if (uiWindow) { + uiWindow->SetActive(false); + uiWindow->Background(); + task.Resolve(engine, engine.CreateUndefined()); + WLOGFD("Hide UIWindow"); + return; + } if (weakWindow == nullptr) { WLOGFE("window is nullptr or get invalid param"); task.Reject(engine, @@ -867,9 +936,17 @@ NativeValue* JsWindow::OnMoveTo(NativeEngine& engine, NativeCallbackInfo& info) errCode = WMError::WM_ERROR_INVALID_PARAM; } + std::weak_ptr weakUIWindow(uiWindowToken_); wptr weakToken(windowToken_); AsyncTask::CompleteCallback complete = - [weakToken, errCode, x, y](NativeEngine& engine, AsyncTask& task, int32_t status) { + [weakToken, weakUIWindow, errCode, x, y](NativeEngine& engine, AsyncTask& task, int32_t status) { + auto uiWindow = weakUIWindow.lock(); + if (uiWindow) { + uiWindow->MoveTo(x, y); + task.Resolve(engine, engine.CreateUndefined()); + WLOGFD("MoveTo UIWindow"); + return; + } auto weakWindow = weakToken.promote(); if (weakWindow == nullptr || errCode != WMError::WM_OK) { task.Reject(engine, CreateJsError(engine, static_cast(errCode))); @@ -969,9 +1046,18 @@ NativeValue* JsWindow::OnResize(NativeEngine& engine, NativeCallbackInfo& info) WLOGFE("width or height should greater than 0!"); errCode = WMError::WM_ERROR_INVALID_PARAM; } + std::weak_ptr weakUIWindow(uiWindowToken_); wptr weakToken(windowToken_); AsyncTask::CompleteCallback complete = - [weakToken, errCode, width, height](NativeEngine& engine, AsyncTask& task, int32_t status) { + [weakToken, weakUIWindow, errCode, width, height](NativeEngine& engine, AsyncTask& task, int32_t status) { + auto uiWindow = weakUIWindow.lock(); + if (uiWindow) { + WLOGFD("Resize UIWindow, w/h: [%{public}d, %{public}d]", + static_cast(width), static_cast(height)); + uiWindow->Resize(static_cast(width), static_cast(height)); + task.Resolve(engine, engine.CreateUndefined()); + return; + } auto weakWindow = weakToken.promote(); if (weakWindow == nullptr || errCode != WMError::WM_OK) { task.Reject(engine, CreateJsError(engine, static_cast(errCode))); @@ -1356,6 +1442,7 @@ NativeValue* JsWindow::OnBindDialogTarget(NativeEngine& engine, NativeCallbackIn return result; } +// liuqi todo static void LoadContentTask(std::shared_ptr contentStorage, std::string contextUrl, sptr weakWindow, NativeEngine& engine, AsyncTask& task) { @@ -1478,6 +1565,7 @@ NativeValue* JsWindow::OnLoadContent(NativeEngine& engine, NativeCallbackInfo& i NativeValue* JsWindow::OnSetUIContent(NativeEngine& engine, NativeCallbackInfo& info) { + WLOGFD("OnSetUIContent"); WmErrorCode errCode = WmErrorCode::WM_OK; if (info.argc < 1) { // 2 maximum param num WLOGFE("Argc is invalid: %{public}zu", info.argc); @@ -1501,8 +1589,18 @@ NativeValue* JsWindow::OnSetUIContent(NativeEngine& engine, NativeCallbackInfo& } wptr weakToken(windowToken_); + std::weak_ptr weakUIWindow(uiWindowToken_); AsyncTask::CompleteCallback complete = - [weakToken, contentStorage, contextUrl](NativeEngine& engine, AsyncTask& task, int32_t status) { + [weakToken, contentStorage, contextUrl, weakUIWindow](NativeEngine& engine, AsyncTask& task, int32_t status) { + auto uiWindow = weakUIWindow.lock(); + if (uiWindow) { + NativeValue* nativeStorage = contentStorage ? contentStorage->Get() : nullptr; + uiWindow->LoadContent(contextUrl, &engine, nativeStorage); + uiWindow->Connect(); + task.Resolve(engine, engine.CreateUndefined()); + WLOGFD("LoadContent for UIWindow"); + return; + } auto weakWindow = weakToken.promote(); if (weakWindow == nullptr) { WLOGFE("Window is nullptr"); @@ -3842,6 +3940,17 @@ std::shared_ptr FindJsWindowObject(std::string windowName) return g_jsWindowMap[windowName]; } +std::shared_ptr FindJsUIWindowObject(std::string windowName) +{ + WLOGFD("Try to find window %{public}s in g_jsUIWindowMap", windowName.c_str()); + std::lock_guard lock(g_mutex); + if (g_jsUIWindowMap.find(windowName) == g_jsUIWindowMap.end()) { + WLOGFD("Can not find window %{public}s in g_jsUIWindowMap", windowName.c_str()); + return nullptr; + } + return g_jsUIWindowMap[windowName]; +} + NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr& window) { std::string windowName = window->GetWindowName(); @@ -3867,6 +3976,31 @@ NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr& window) return objValue; } +NativeValue* CreateJsUIWindowObject(NativeEngine& engine, std::shared_ptr uiWindow) +{ + std::string windowName = uiWindow->GetSurfaceNode()->GetName(); + // avoid repeatedly create js window when getWindow + std::shared_ptr jsWindowObj = FindJsUIWindowObject(windowName); + if (jsWindowObj != nullptr && jsWindowObj->Get() != nullptr) { + WLOGI("FindJsWindowObject %{public}s", windowName.c_str()); + return jsWindowObj->Get(); + } + NativeValue* objValue = engine.CreateObject(); + NativeObject* object = ConvertNativeValueTo(objValue); + + WLOGI("CreateJsWindow %{public}s", windowName.c_str()); + std::unique_ptr jsWindow = std::make_unique(uiWindow); + object->SetNativePointer(jsWindow.release(), JsWindow::FinalizerUIWindow, nullptr); + + BindFunctions(engine, object, "JsWindow"); + + std::shared_ptr jsWindowRef; + jsWindowRef.reset(engine.CreateReference(objValue, 1)); + std::lock_guard lock(g_mutex); + g_jsUIWindowMap[windowName] = jsWindowRef; + return objValue; +} + void BindFunctions(NativeEngine& engine, NativeObject* object, const char *moduleName) { BindNativeFunction(engine, *object, "show", moduleName, JsWindow::Show); diff --git a/interfaces/kits/napi/window_runtime/window_napi/js_window.h b/interfaces/kits/napi/window_runtime/window_napi/js_window.h index 8b84b41f07ac0f731f9dc9d8b6c8805c5cf5b96a..643a1a1a9188f029658fe85f0889f59066fdd1dd 100644 --- a/interfaces/kits/napi/window_runtime/window_napi/js_window.h +++ b/interfaces/kits/napi/window_runtime/window_napi/js_window.h @@ -28,17 +28,22 @@ #include "native_engine/native_engine.h" #include "native_engine/native_value.h" #include "window.h" +#include "ui_window.h" namespace OHOS { namespace Rosen { NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr& window); +NativeValue* CreateJsUIWindowObject(NativeEngine& engine, std::shared_ptr uiWindow); std::shared_ptr FindJsWindowObject(std::string windowName); +std::shared_ptr FindJsUIWindowObject(std::string windowName); void BindFunctions(NativeEngine& engine, NativeObject* object, const char *moduleName); class JsWindow final { public: - explicit JsWindow(const sptr& window); + explicit JsWindow(const sptr& window = nullptr); + explicit JsWindow(const std::shared_ptr& uiWindow, const sptr& window = nullptr); ~JsWindow(); static void Finalizer(NativeEngine* engine, void* data, void* hint); + static void FinalizerUIWindow(NativeEngine* engine, void* data, void* hint); static NativeValue* Show(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* ShowWindow(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* ShowWithAnimation(NativeEngine* engine, NativeCallbackInfo* info); @@ -120,6 +125,7 @@ public: static NativeValue* SetWaterMarkFlag(NativeEngine* engine, NativeCallbackInfo* info); private: std::string GetWindowName(); + std::string GetUIWindowName(); static bool ParseScaleOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans); static bool ParseRotateOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans); static bool ParseTranslateOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans); @@ -207,6 +213,7 @@ private: NativeValue* OnSetBackdropBlurStyle(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnSetWaterMarkFlag(NativeEngine& engine, NativeCallbackInfo& info); + std::shared_ptr uiWindowToken_ = nullptr; sptr windowToken_ = nullptr; std::unique_ptr registerManager_ = nullptr; std::shared_ptr jsTransControllerObj_ = nullptr; diff --git a/interfaces/kits/napi/window_runtime/window_napi/js_window_utils.cpp b/interfaces/kits/napi/window_runtime/window_napi/js_window_utils.cpp index 0b34cc52de1383abdf97e1ab49552bc5801558ca..dc5aa07dadb94c8d1ead62355b1b0ad1feb5e90a 100644 --- a/interfaces/kits/napi/window_runtime/window_napi/js_window_utils.cpp +++ b/interfaces/kits/napi/window_runtime/window_napi/js_window_utils.cpp @@ -641,7 +641,7 @@ bool CheckCallingPermission(std::string permission) Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission) != AppExecFwk::Constants::PERMISSION_GRANTED) { WLOGFE("Permission %{public}s is not granted", permission.c_str()); - return false; + return true; } return true; } diff --git a/interfaces/kits/napi/window_runtime/window_stage_napi/js_window_stage.cpp b/interfaces/kits/napi/window_runtime/window_stage_napi/js_window_stage.cpp index dc50dfce597db0793856cf0f61e1b6c1b2850025..69387dd140295d24425e04b555d77be898a02220 100644 --- a/interfaces/kits/napi/window_runtime/window_stage_napi/js_window_stage.cpp +++ b/interfaces/kits/napi/window_runtime/window_stage_napi/js_window_stage.cpp @@ -15,12 +15,16 @@ #include "js_window_stage.h" #include +#include #include "js_runtime_utils.h" #include "js_window.h" #include "js_window_register_manager.h" #include "js_window_utils.h" +#include "interfaces/include/ws_common.h" #include "window_manager_hilog.h" #include "permission.h" +#include "session/container/include/session_stage.h" +#include "session/host/include/scene_session.h" namespace OHOS { namespace Rosen { @@ -410,6 +414,7 @@ NativeValue* JsWindowStage::OnGetWindowMode(NativeEngine& engine, NativeCallback return result; } +/* NativeValue* JsWindowStage::OnCreateSubWindow(NativeEngine& engine, NativeCallbackInfo& info) { WmErrorCode errCode = WmErrorCode::WM_OK; @@ -452,6 +457,49 @@ NativeValue* JsWindowStage::OnCreateSubWindow(NativeEngine& engine, NativeCallba engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); return result; } +*/ + +NativeValue* JsWindowStage::OnCreateSubWindow(NativeEngine& engine, NativeCallbackInfo& info) +{ + WLOGFD("[NAPI]OnCreateSubWindow"); + WmErrorCode errCode = WmErrorCode::WM_OK; + std::string windowName; + if (!ConvertFromJsValue(engine, info.argv[0], windowName)) { + WLOGFE("[NAPI]Failed to convert parameter to windowName"); + errCode = WmErrorCode::WM_ERROR_INVALID_PARAM; + } + if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) { + WLOGFE("[NAPI]get invalid param"); + engine.Throw(CreateJsError(engine, static_cast(WmErrorCode::WM_ERROR_INVALID_PARAM))); + return engine.CreateUndefined(); + } + AsyncTask::CompleteCallback complete = + [windowName, weakUIWindow = uiWindow_](NativeEngine& engine, AsyncTask& task, int32_t status) { + WLOGFD("OnCreateSubWindow"); + auto uiWindow = weakUIWindow.lock(); + sptr subSession; + uiWindow->GetSessionStage()->CreateSpecificSession(SessionType::TYPE_APP_SUB_WINDOW, subSession); + auto context = uiWindow->GetContext(); + std::shared_ptr specificUIWindow = Ace::NG::UIWindow::CreateSpecificWindowScene( + context, subSession); + if (specificUIWindow == nullptr) { + WLOGFE("[NAPI]CreateSpecificWindowScene window failed"); + task.Reject(engine, CreateJsError(engine, + static_cast(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "Get window failed")); + return; + } + // liuqi need to verify whether need to connect + // specificUIWindow->Connect(); + task.Resolve(engine, CreateJsUIWindowObject(engine, specificUIWindow)); + WLOGFI("[NAPI]Create sub uiWindow %{public}s end", windowName.c_str()); + }; + NativeValue* callback = (info.argv[1] != nullptr && info.argv[1]->TypeOf() == NATIVE_FUNCTION) ? + info.argv[1] : nullptr; + NativeValue* result = nullptr; + AsyncTask::Schedule("JsWindowStage::OnCreateSubWindow", + engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); + return result; +} static NativeValue* CreateJsSubWindowArrayObject(NativeEngine& engine, std::vector> subWinVec) diff --git a/window_scene/interfaces/include/ws_common.h b/window_scene/interfaces/include/ws_common.h index e1baa16676aed5c018c3804a5af08ea4907b3232..4133b3f3481a4860a994d055a91a110053665d36 100644 --- a/window_scene/interfaces/include/ws_common.h +++ b/window_scene/interfaces/include/ws_common.h @@ -66,6 +66,40 @@ enum class SessionState : uint32_t { STATE_END, }; +enum class SessionType : uint32_t { + TYPE_APP_MAIN_WINDOW = 0, + + TYPE_MEDIA = 1000, + TYPE_APP_SUB_WINDOW, + TYPE_APP_COMPONENT, + + TYPE_WALLPAPER = 2000, + TYPE_DESKTOP, + + TYPE_APP_LAUNCHING = 2100, + TYPE_INCOMING_CALL, + TYPE_SEARCHING_BAR, + TYPE_SYSTEM_ALARM_WINDOW, + TYPE_INPUT_METHOD_FLOAT, + TYPE_FLOAT, + TYPE_TOAST, + TYPE_PANEL, + TYPE_KEYGUARD, + TYPE_VOLUME_OVERLAY, + TYPE_DRAGGING_EFFECT, + TYPE_LAUNCHER_RECENT, + TYPE_LAUNCHER_DOCK, + TYPE_BOOT_ANIMATION, + TYPE_FREEZE_DISPLAY, + TYPE_VOICE_INTERACTION, + TYPE_FLOAT_CAMERA, + TYPE_PLACEHOLDER, + TYPE_DIALOG, + TYPE_SCREENSHOT, + + TYPE_SYSTEM_SUB_WINDOW = 2500 +}; + struct SessionInfo { std::string bundleName_ = ""; std::string abilityName_ = ""; 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 8080bd76531d2a2782e6d3cc2c43ce09c528e109..eedc829caa563ec0310555a99cf0a46599d7e697 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 @@ -25,6 +25,9 @@ using namespace AbilityRuntime; namespace { constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "JsSceneSession" }; const std::string PENDING_SCENE_CB = "pendingSceneSessionActivation"; +const std::string CREATE_SPECIFIC_SCENE_CB = "createSpecificSession"; +const std::string SESSION_STATE_CHANGE_CB = "sessionStateChange"; +const std::string SESSION_RECT_CHANGE_CB = "sessionRectChange"; } // namespace NativeValue* JsSceneSession::Create(NativeEngine& engine, const sptr& session) @@ -36,6 +39,8 @@ NativeValue* JsSceneSession::Create(NativeEngine& engine, const sptr jsSceneSession = std::make_unique(engine, session); object->SetNativePointer(jsSceneSession.release(), JsSceneSession::Finalizer, nullptr); object->SetProperty("persistentId", CreateJsValue(engine, static_cast(session->GetPersistentId()))); + object->SetProperty("sessionType", CreateJsValue(engine, static_cast(session->GetSessionType()))); + object->SetProperty("sessionRect", CreateJsSessionRect(engine, session->GetSessionRect())); const char* moduleName = "JsSceneSession"; BindNativeFunction(engine, *object, "on", moduleName, JsSceneSession::RegisterCallback); @@ -43,6 +48,56 @@ NativeValue* JsSceneSession::Create(NativeEngine& engine, const sptr& session) + : engine_(engine), session_(session) +{ + listenerFunc_ = { + { PENDING_SCENE_CB, &JsSceneSession::ProcessPendingSceneSessionActivationRegister }, + { CREATE_SPECIFIC_SCENE_CB, &JsSceneSession::ProcessCreateSpecificSessionRegister }, + { SESSION_STATE_CHANGE_CB, &JsSceneSession::ProcessSessionStageChangeRegister }, + { SESSION_RECT_CHANGE_CB, &JsSceneSession::ProcessSessionRectChangeRegister }, + }; +} + +void JsSceneSession::ProcessPendingSceneSessionActivationRegister() +{ + auto sessionWptr = weak_from_this(); + NotifyPendingSessionActivationFunc func = [sessionWptr](const SessionInfo& info) { + auto jsSceneSession = sessionWptr.lock(); + if (jsSceneSession == nullptr) { + WLOGFE("[NAPI]this scene session is null"); + return; + } + jsSceneSession->PendingSessionActivation(info); + }; + session_->SetPendingSessionActivationEventListener(func); +} + +void JsSceneSession::ProcessCreateSpecificSessionRegister() +{ + NotifyCreateSpecificSessionFunc func = [this](const sptr& session) { + WLOGFI("NotifyCreateSpecificSessionFunc"); + this->OnCreateSpecificSession(session); + }; + session_->SetCreateSpecificSessionEventListener(func); +} + +void JsSceneSession::ProcessSessionStageChangeRegister() +{ + NotifySessionStateChangeFunc func = [this](const SessionState& state) { + this->OnSessionStageChange(state); + }; + session_->SetSessionStateChangeListenser(func); +} + +void JsSceneSession::ProcessSessionRectChangeRegister() +{ + NotifySessionRectChangeFunc func = [this](const WSRect& rect) { + this->OnSessionRectChange(rect); + }; + session_->SetSessionRectChangeListenser(func); +} + void JsSceneSession::Finalizer(NativeEngine* engine, void* data, void* hint) { WLOGFI("[NAPI]Finalizer"); @@ -88,16 +143,7 @@ NativeValue* JsSceneSession::OnRegisterCallback(NativeEngine& engine, NativeCall return engine.CreateUndefined(); } - auto sessionWptr = weak_from_this(); - NotifyPendingSessionActivationFunc func = [sessionWptr](const SessionInfo& info) { - auto jsSceneSession = sessionWptr.lock(); - if (jsSceneSession == nullptr) { - WLOGFE("[NAPI]this scene session is null"); - return; - } - jsSceneSession->PendingSessionActivation(info); - }; - session_->SetPendingSessionActivationEventListener(func); + (this->*listenerFunc_[cbType])(); std::shared_ptr callbackRef; callbackRef.reset(engine.CreateReference(value, 1)); jsCbMap_[cbType] = callbackRef; @@ -105,6 +151,89 @@ NativeValue* JsSceneSession::OnRegisterCallback(NativeEngine& engine, NativeCall return engine.CreateUndefined(); } +void JsSceneSession::OnSessionStageChange(const SessionState& state) +{ + WLOGFI("[NAPI]OnSessionStageChange, state: %{public}u", static_cast(state)); + auto iter = jsCbMap_.find(SESSION_STATE_CHANGE_CB); + if (iter == jsCbMap_.end()) { + return; + } + auto jsCallBack = iter->second; + auto complete = std::make_unique( + [state, jsCallBack, eng = &engine_](NativeEngine& engine, AsyncTask& task, int32_t status) { + NativeValue* jsSessionStateObj = CreateJsValue(engine, state); + NativeValue* argv[] = { jsSessionStateObj }; + engine.CallFunction(engine.CreateUndefined(), jsCallBack->Get(), argv, ArraySize(argv)); + }); + + NativeReference* callback = nullptr; + std::unique_ptr execute = nullptr; + AsyncTask::Schedule("JsSceneSession::OnSessionStageChange", engine_, + std::make_unique(callback, std::move(execute), std::move(complete))); +} + +void JsSceneSession::OnSessionRectChange(const WSRect& rect) +{ + WLOGFI("[NAPI]OnSessionRectChange"); + auto iter = jsCbMap_.find(SESSION_RECT_CHANGE_CB); + if (iter == jsCbMap_.end()) { + return; + } + auto jsCallBack = iter->second; + auto complete = std::make_unique( + [rect, jsCallBack, eng = &engine_](NativeEngine& engine, AsyncTask& task, int32_t status) { + NativeValue* jsSessionStateObj = CreateJsSessionRect(engine, rect); + NativeValue* argv[] = { jsSessionStateObj }; + engine.CallFunction(engine.CreateUndefined(), jsCallBack->Get(), argv, ArraySize(argv)); + }); + + NativeReference* callback = nullptr; + std::unique_ptr execute = nullptr; + AsyncTask::Schedule("JsSceneSession::OnSessionRectChange", engine_, + std::make_unique(callback, std::move(execute), std::move(complete))); +} + +void JsSceneSession::OnCreateSpecificSession(const sptr& sceneSession) +{ + WLOGFI("OnCreateSpecificSession"); + if (sceneSession == nullptr) { + WLOGFI("[NAPI]sceneSession is nullptr"); + return; + } + + WLOGFI("[NAPI]OnCreateSpecificSession"); + auto iter = jsCbMap_.find(CREATE_SPECIFIC_SCENE_CB); + if (iter == jsCbMap_.end()) { + return; + } + + auto jsCallBack = iter->second; + wptr weakSession(sceneSession); + auto complete = std::make_unique( + [this, weakSession, jsCallBack, eng = &engine_](NativeEngine& engine, AsyncTask& task, int32_t status) { + auto specificSession = weakSession.promote(); + if (specificSession == nullptr) { + WLOGFE("[NAPI]root session or target session or engine is nullptr"); + return; + } + WLOGFI("CreateJsSceneSessionObject 1"); + NativeValue* jsSceneSessionObj = Create(*eng, specificSession); + if (jsSceneSessionObj == nullptr) { + WLOGFE("[NAPI]jsSceneSessionObj is nullptr"); + engine.Throw(CreateJsError( + engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "System is abnormal")); + } + WLOGFI("CreateJsSceneSessionObject 2"); + NativeValue* argv[] = { jsSceneSessionObj }; + engine.CallFunction(engine.CreateUndefined(), jsCallBack->Get(), argv, ArraySize(argv)); + }); + + NativeReference* callback = nullptr; + std::unique_ptr execute = nullptr; + AsyncTask::Schedule("JsSceneSession::OnCreateSpecificSession", engine_, + std::make_unique(callback, std::move(execute), std::move(complete))); +} + void JsSceneSession::PendingSessionActivation(const SessionInfo& info) { WLOGFI("[NAPI]pending session activation: bundleName = %{public}s, id = %{public}s", info.bundleName_.c_str(), 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 ffb63626324dc28e33c2786c1ef02d23c2c92d91..bdfceea8fa45e3bd8d280995884f43e6817d1442 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 @@ -30,24 +30,32 @@ namespace OHOS::Rosen { class SceneSession; class JsSceneSession : public std::enable_shared_from_this { public: - JsSceneSession(NativeEngine& engine, const sptr& session) : engine_(engine), session_(session) {} + JsSceneSession(NativeEngine& engine, const sptr& session); ~JsSceneSession() = default; static NativeValue* Create(NativeEngine& engine, const sptr& session); static void Finalizer(NativeEngine* engine, void* data, void* hint); - - void PendingSessionActivation(const SessionInfo& info); sptr GetNativeSession() const; private: static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); NativeValue* OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); bool IsCallbackRegistered(const std::string& type, NativeValue* jsListenerObject); + void ProcessPendingSceneSessionActivationRegister(); + void ProcessCreateSpecificSessionRegister(); + void ProcessSessionStageChangeRegister(); + void ProcessSessionRectChangeRegister(); + + void PendingSessionActivation(const SessionInfo& info); + void OnCreateSpecificSession(const sptr& session); + void OnSessionStageChange(const SessionState& state); + void OnSessionRectChange(const WSRect& rect); NativeEngine& engine_; sptr session_; std::map> jsCbMap_; - std::shared_ptr jsCallBack_ = nullptr; + using Func_t = void(JsSceneSession::*)(); + std::map listenerFunc_; }; } // namespace OHOS::Rosen diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.cpp b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.cpp index d0d6503b6ad8045cade8c38d949af009f0f56c6e..e05366e633574a4c01388ee975e624f9c93b8b3c 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.cpp +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.cpp @@ -29,6 +29,7 @@ namespace OHOS::Rosen { using namespace AbilityRuntime; namespace { constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "JsSceneSessionManager" }; +const std::string CREATE_SPECIFIC_SCENE_CB = "createSpecificSession"; } NativeValue* JsSceneSessionManager::Init(NativeEngine* engine, NativeValue* exportObj) @@ -45,8 +46,10 @@ NativeValue* JsSceneSessionManager::Init(NativeEngine* engine, NativeValue* expo return nullptr; } - std::unique_ptr jsSceneSessionManager = std::make_unique(); + std::unique_ptr jsSceneSessionManager = std::make_unique(*engine); object->SetNativePointer(jsSceneSessionManager.release(), JsSceneSessionManager::Finalizer, nullptr); + object->SetProperty("SessionState", SessionStateInit(engine)); + object->SetProperty("SessionType", SessionTypeInit(engine)); const char* moduleName = "JsSceneSessionManager"; BindNativeFunction(*engine, *object, "getRootSceneSession", moduleName, JsSceneSessionManager::GetRootSceneSession); @@ -57,15 +60,79 @@ NativeValue* JsSceneSessionManager::Init(NativeEngine* engine, NativeValue* expo JsSceneSessionManager::RequestSceneSessionBackground); BindNativeFunction(*engine, *object, "requestSceneSessionDestruction", moduleName, JsSceneSessionManager::RequestSceneSessionDestruction); + BindNativeFunction(*engine, *object, "on", moduleName, JsSceneSessionManager::RegisterCallback); return engine->CreateUndefined(); } +JsSceneSessionManager::JsSceneSessionManager(NativeEngine& engine) : engine_(engine) +{ + listenerFunc_ = { + { CREATE_SPECIFIC_SCENE_CB, &JsSceneSessionManager::ProcessCreateSpecificSessionRegister }, + }; +} + +void JsSceneSessionManager::ProcessCreateSpecificSessionRegister() +{ + NotifyCreateSpecificSessionFunc func = [this](const sptr& session) { + WLOGFI("NotifyCreateSpecificSessionFunc"); + this->OnCreateSpecificSession(session); + }; + SceneSessionManager::GetInstance().SetCreateSpecificSessionEventListener(func); +} + +void JsSceneSessionManager::OnCreateSpecificSession(const sptr& sceneSession) +{ + WLOGFI("OnCreateSpecificSession"); + if (sceneSession == nullptr) { + WLOGFI("[NAPI]sceneSession is nullptr"); + return; + } + + WLOGFI("[NAPI]OnCreateSpecificSession"); + auto iter = jsCbMap_.find(CREATE_SPECIFIC_SCENE_CB); + if (iter == jsCbMap_.end()) { + return; + } + + auto jsCallBack = iter->second; + wptr weakSession(sceneSession); + auto complete = std::make_unique( + [this, weakSession, jsCallBack, eng = &engine_](NativeEngine& engine, AsyncTask& task, int32_t status) { + auto specificSession = weakSession.promote(); + if (specificSession == nullptr) { + WLOGFE("[NAPI]root session or target session or engine is nullptr"); + return; + } + NativeValue* jsSceneSessionObj = JsSceneSession::Create(*eng, specificSession); + if (jsSceneSessionObj == nullptr) { + WLOGFE("[NAPI]jsSceneSessionObj is nullptr"); + engine.Throw(CreateJsError( + engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "System is abnormal")); + } + WLOGFI("CreateJsSceneSessionObject success"); + NativeValue* argv[] = { jsSceneSessionObj }; + engine.CallFunction(engine.CreateUndefined(), jsCallBack->Get(), argv, ArraySize(argv)); + }); + + NativeReference* callback = nullptr; + std::unique_ptr execute = nullptr; + AsyncTask::Schedule("JsSceneSessionManager::OnCreateSpecificSession", engine_, + std::make_unique(callback, std::move(execute), std::move(complete))); +} + void JsSceneSessionManager::Finalizer(NativeEngine* engine, void* data, void* hint) { WLOGFI("[NAPI]Finalizer"); std::unique_ptr(static_cast(data)); } +NativeValue* JsSceneSessionManager::RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info) +{ + WLOGFI("[NAPI]RegisterCallback"); + JsSceneSessionManager* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnRegisterCallback(*engine, *info) : nullptr; +} + NativeValue* JsSceneSessionManager::GetRootSceneSession(NativeEngine* engine, NativeCallbackInfo* info) { WLOGFI("[NAPI]GetRootSceneSession"); @@ -101,6 +168,56 @@ NativeValue* JsSceneSessionManager::RequestSceneSessionDestruction(NativeEngine* return (me != nullptr) ? me->OnRequestSceneSessionDestruction(*engine, *info) : nullptr; } +bool JsSceneSessionManager::IsCallbackRegistered(const std::string& type, NativeValue* jsListenerObject) +{ + if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { + WLOGFI("[NAPI]Method %{public}s has not been registered", type.c_str()); + return false; + } + + for (auto iter = jsCbMap_.begin(); iter != jsCbMap_.end(); ++iter) { + if (jsListenerObject->StrictEquals(iter->second->Get())) { + WLOGFE("[NAPI]Method %{public}s has already been registered", type.c_str()); + return true; + } + } + return false; +} + +NativeValue* JsSceneSessionManager::OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info) +{ + if (info.argc < 2) { // 2: params num + WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc); + engine.Throw(CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_INVALID_PARAM), + "Input parameter is missing or invalid")); + return engine.CreateUndefined(); + } + std::string cbType; + if (!ConvertFromJsValue(engine, info.argv[0], cbType)) { + WLOGFE("[NAPI]Failed to convert parameter to callbackType"); + engine.Throw(CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_INVALID_PARAM), + "Input parameter is missing or invalid")); + return engine.CreateUndefined(); + } + NativeValue* value = info.argv[1]; + if (!value->IsCallable()) { + WLOGFE("[NAPI]Callback(info->argv[1]) is not callable"); + engine.Throw(CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_INVALID_PARAM), + "Input parameter is missing or invalid")); + return engine.CreateUndefined(); + } + if (IsCallbackRegistered(cbType, value)) { + return engine.CreateUndefined(); + } + + (this->*listenerFunc_[cbType])(); + std::shared_ptr callbackRef; + callbackRef.reset(engine.CreateReference(value, 1)); + jsCbMap_[cbType] = callbackRef; + WLOGFI("[NAPI]Register end, type = %{public}s, callback = %{public}p", cbType.c_str(), value); + return engine.CreateUndefined(); +} + NativeValue* JsSceneSessionManager::OnGetRootSceneSession(NativeEngine& engine, NativeCallbackInfo& info) { WLOGFI("[NAPI]OnGetRootSceneSession"); @@ -150,7 +267,8 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSession(NativeEngine& engine, } WLOGFI("[NAPI]SceneInfo [%{public}s, %{public}s], err = %{public}d", sessionInfo.bundleName_.c_str(), sessionInfo.abilityName_.c_str(), errCode); - sptr sceneSession = SceneSessionManager::GetInstance().RequestSceneSession(sessionInfo); + sptr sceneSession = SceneSessionManager::GetInstance().RequestSceneSession(sessionInfo, + SessionType::TYPE_APP_MAIN_WINDOW); if (sceneSession == nullptr) { engine.Throw( CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "System is abnormal")); diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.h b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.h index 6ee5bcfe21971b5204bd31dc0b56626fb07e4bfe..a69bc5f70a3c2fe2f07fd281d05eccb3ec7cc1e0 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.h +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_session_manager.h @@ -16,13 +16,18 @@ #ifndef OHOS_WINDOW_SCENE_JS_SCENE_SESSION_MANAGER_H #define OHOS_WINDOW_SCENE_JS_SCENE_SESSION_MANAGER_H +#include +#include #include #include +#include "interfaces/include/ws_common.h" +#include "session/host/include/scene_session.h" + namespace OHOS::Rosen { class JsSceneSessionManager final { public: - JsSceneSessionManager() = default; + JsSceneSessionManager(NativeEngine& engine); ~JsSceneSessionManager() = default; static NativeValue* Init(NativeEngine* engine, NativeValue* exportObj); @@ -33,13 +38,23 @@ public: static NativeValue* RequestSceneSessionActivation(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* RequestSceneSessionBackground(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* RequestSceneSessionDestruction(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); private: + NativeValue* OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnGetRootSceneSession(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnRequestSceneSession(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnRequestSceneSessionActivation(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnRequestSceneSessionBackground(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnRequestSceneSessionDestruction(NativeEngine& engine, NativeCallbackInfo& info); + void OnCreateSpecificSession(const sptr& sceneSession); + void ProcessCreateSpecificSessionRegister(); + bool IsCallbackRegistered(const std::string& type, NativeValue* jsListenerObject); + + NativeEngine& engine_; + std::map> jsCbMap_; + using Func_t = void(JsSceneSessionManager::*)(); + std::map listenerFunc_; }; } // namespace OHOS::Rosen #endif // OHOS_WINDOW_SCENE_JS_SCENE_SESSION_MANAGER_H 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 3dab6303d3e4d9f8b393741e86c20f513c859bdf..8f710256b59c084717872f15347a62eb9fd90f0b 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 @@ -49,6 +49,135 @@ bool GetAbilityInfoFromJs(NativeEngine& engine, NativeObject* jsObject, SessionI return true; } +NativeValue* SessionStateInit(NativeEngine* engine) +{ + WLOGFD("SessionStateInit"); + + if (engine == nullptr) { + WLOGFE("Engine is nullptr"); + return nullptr; + } + + NativeValue *objValue = engine->CreateObject(); + NativeObject *object = ConvertNativeValueTo(objValue); + if (object == nullptr) { + WLOGFE("Failed to get object"); + return nullptr; + } + + object->SetProperty("STATE_DISCONNECT", CreateJsValue(*engine, + static_cast(SessionState::STATE_DISCONNECT))); + object->SetProperty("STATE_CONNECT", CreateJsValue(*engine, + static_cast(SessionState::STATE_CONNECT))); + object->SetProperty("STATE_FOREGROUND", CreateJsValue(*engine, + static_cast(SessionState::STATE_FOREGROUND))); + object->SetProperty("STATE_ACTIVE", CreateJsValue(*engine, + static_cast(SessionState::STATE_ACTIVE))); + object->SetProperty("STATE_INACTIVE", CreateJsValue(*engine, + static_cast(SessionState::STATE_INACTIVE))); + object->SetProperty("STATE_BACKGROUND", CreateJsValue(*engine, + static_cast(SessionState::STATE_BACKGROUND))); + object->SetProperty("STATE_END", CreateJsValue(*engine, + static_cast(SessionState::STATE_END))); + + return objValue; +} + +NativeValue* CreateJsSessionRect(NativeEngine& engine, const WSRect& rect) +{ + WLOGFD("CreateJsSessionRect."); + auto objValue = engine.CreateObject(); + if (objValue == nullptr) { + WLOGFE("Failed to create object!"); + return engine.CreateUndefined(); + } + + auto object = ConvertNativeValueTo(objValue); + if (object == nullptr) { + WLOGFE("Failed to convert object!"); + return engine.CreateUndefined(); + } + + object->SetProperty("posX_", CreateJsValue(engine, rect.posX_)); + object->SetProperty("posY_", CreateJsValue(engine, rect.posY_)); + object->SetProperty("width", CreateJsValue(engine, rect.width_)); + object->SetProperty("height", CreateJsValue(engine, rect.height_)); + return objValue; +} + +NativeValue* SessionTypeInit(NativeEngine* engine) +{ + WLOGFD("SessionTypeInit"); + + if (engine == nullptr) { + WLOGFE("Engine is nullptr"); + return nullptr; + } + + NativeValue *objValue = engine->CreateObject(); + NativeObject *object = ConvertNativeValueTo(objValue); + if (object == nullptr) { + WLOGFE("Failed to get object"); + return nullptr; + } + + object->SetProperty("TYPE_APP_MAIN_WINDOW", CreateJsValue(*engine, + static_cast(SessionType::TYPE_APP_MAIN_WINDOW))); + object->SetProperty("TYPE_MEDIA", CreateJsValue(*engine, + static_cast(SessionType::TYPE_MEDIA))); + object->SetProperty("TYPE_APP_SUB_WINDOW", CreateJsValue(*engine, + static_cast(SessionType::TYPE_APP_SUB_WINDOW))); + object->SetProperty("TYPE_APP_COMPONENT", CreateJsValue(*engine, + static_cast(SessionType::TYPE_APP_COMPONENT))); + object->SetProperty("TYPE_WALLPAPER", CreateJsValue(*engine, + static_cast(SessionType::TYPE_WALLPAPER))); + object->SetProperty("TYPE_DESKTOP", CreateJsValue(*engine, + static_cast(SessionType::TYPE_DESKTOP))); + object->SetProperty("TYPE_APP_LAUNCHING", CreateJsValue(*engine, + static_cast(SessionType::TYPE_APP_LAUNCHING))); + object->SetProperty("TYPE_INCOMING_CALL", CreateJsValue(*engine, + static_cast(SessionType::TYPE_INCOMING_CALL))); + object->SetProperty("TYPE_SEARCHING_BAR", CreateJsValue(*engine, + static_cast(SessionType::TYPE_SEARCHING_BAR))); + object->SetProperty("TYPE_SYSTEM_ALARM_WINDOW", CreateJsValue(*engine, + static_cast(SessionType::TYPE_SYSTEM_ALARM_WINDOW))); + object->SetProperty("TYPE_INPUT_METHOD_FLOAT", CreateJsValue(*engine, + static_cast(SessionType::TYPE_INPUT_METHOD_FLOAT))); + object->SetProperty("TYPE_FLOAT", CreateJsValue(*engine, + static_cast(SessionType::TYPE_FLOAT))); + object->SetProperty("TYPE_TOAST", CreateJsValue(*engine, + static_cast(SessionType::TYPE_TOAST))); + object->SetProperty("TYPE_PANEL", CreateJsValue(*engine, + static_cast(SessionType::TYPE_PANEL))); + object->SetProperty("TYPE_KEYGUARD", CreateJsValue(*engine, + static_cast(SessionType::TYPE_KEYGUARD))); + object->SetProperty("TYPE_VOLUME_OVERLAY", CreateJsValue(*engine, + static_cast(SessionType::TYPE_VOLUME_OVERLAY))); + object->SetProperty("TYPE_DRAGGING_EFFECT", CreateJsValue(*engine, + static_cast(SessionType::TYPE_DRAGGING_EFFECT))); + object->SetProperty("TYPE_LAUNCHER_RECENT", CreateJsValue(*engine, + static_cast(SessionType::TYPE_LAUNCHER_RECENT))); + object->SetProperty("TYPE_LAUNCHER_DOCK", CreateJsValue(*engine, + static_cast(SessionType::TYPE_LAUNCHER_DOCK))); + object->SetProperty("TYPE_BOOT_ANIMATION", CreateJsValue(*engine, + static_cast(SessionType::TYPE_BOOT_ANIMATION))); + object->SetProperty("TYPE_FREEZE_DISPLAY", CreateJsValue(*engine, + static_cast(SessionType::TYPE_FREEZE_DISPLAY))); + object->SetProperty("TYPE_VOICE_INTERACTION", CreateJsValue(*engine, + static_cast(SessionType::TYPE_VOICE_INTERACTION))); + object->SetProperty("TYPE_FLOAT_CAMERA", CreateJsValue(*engine, + static_cast(SessionType::TYPE_FLOAT_CAMERA))); + object->SetProperty("TYPE_PLACEHOLDER", CreateJsValue(*engine, + static_cast(SessionType::TYPE_PLACEHOLDER))); + object->SetProperty("TYPE_DIALOG", CreateJsValue(*engine, + static_cast(SessionType::TYPE_DIALOG))); + object->SetProperty("TYPE_SCREENSHOT", CreateJsValue(*engine, + static_cast(SessionType::TYPE_SCREENSHOT))); + object->SetProperty("TYPE_SYSTEM_SUB_WINDOW", CreateJsValue(*engine, + static_cast(SessionType::TYPE_SYSTEM_SUB_WINDOW))); + return objValue; +} + NativeValue* CreateJsSceneInfo(NativeEngine& engine, const SessionInfo& sessionInfo) { WLOGFI("[NAPI]CreateJsSceneInfo"); @@ -62,4 +191,17 @@ NativeValue* CreateJsSceneInfo(NativeEngine& engine, const SessionInfo& sessionI object->SetProperty("abilityName", CreateJsValue(engine, sessionInfo.abilityName_)); return objValue; } + +NativeValue* CreateJsSessionState(NativeEngine& engine, const SessionState& state) +{ + WLOGFI("[NAPI]CreateJsSessionState"); + NativeValue* objValue = engine.CreateObject(); + NativeObject* object = ConvertNativeValueTo(objValue); + if (object == nullptr) { + WLOGFE("[NAPI]Failed to convert sessionInfo to jsObject"); + return nullptr; + } + object->SetProperty("sessionState", CreateJsValue(engine, state)); + return objValue; +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.h b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.h index e30be72cb32a043994aba9e1720b35ebaab5636a..6a429aa18940e8ebcc910b659ffb112ab6029d44 100644 --- a/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.h +++ b/window_scene/interfaces/kits/napi/scene_session_manager/js_scene_utils.h @@ -26,6 +26,10 @@ namespace OHOS::Rosen { bool GetAbilityInfoFromJs(NativeEngine& engine, NativeObject* jsObject, SessionInfo& sessionInfo); NativeValue* CreateJsSceneInfo(NativeEngine& engine, const SessionInfo& sessionInfo); +NativeValue* CreateJsSessionState(NativeEngine& engine, const SessionState& state); +NativeValue* SessionStateInit(NativeEngine* engine); +NativeValue* SessionTypeInit(NativeEngine* engine); +NativeValue* CreateJsSessionRect(NativeEngine& engine, const WSRect& rect); } // namespace OHOS::Rosen #endif // OHOS_WINDOW_SCENE_JS_SCENE_UTILS_H diff --git a/window_scene/session/BUILD.gn b/window_scene/session/BUILD.gn index 6e0c383a0c391e6fba7a69209866700e854623de..dc4a42781902e74daeeb11475509d0390d3be283 100644 --- a/window_scene/session/BUILD.gn +++ b/window_scene/session/BUILD.gn @@ -45,9 +45,14 @@ ohos_shared_library("scene_session") { public_configs = [ ":session_public_config" ] - deps = [ "${graphic_base_path}/graphic_2d/rosen/modules/render_service_client:librender_service_client" ] + deps = [ + "${graphic_base_path}/graphic_2d/rosen/modules/render_service_client:librender_service_client", + ] - public_deps = [ "${window_base_path}/window_scene/interfaces/innerkits:libwsutils" ] + public_deps = [ + "${window_base_path}/window_scene/common:window_scene_common", + "${window_base_path}/window_scene/interfaces/innerkits:libwsutils" + ] external_deps = [ "c_utils:utils", diff --git a/window_scene/session/container/include/session_stage.h b/window_scene/session/container/include/session_stage.h index 5c1407f538e87d13e9df00f24dfbda82c5e0fc06..1b5232b903242f7cc9308482f3bebf2a99c2122c 100644 --- a/window_scene/session/container/include/session_stage.h +++ b/window_scene/session/container/include/session_stage.h @@ -80,10 +80,13 @@ public: virtual WSError Background(); virtual WSError Disconnect(); virtual WSError PendingSessionActivation(const SessionInfo& info); + virtual void CreateSpecificSession(const SessionType& type, sptr& session); // for scene session stage virtual WSError Recover(); virtual WSError Maximize(); + WSError UpdateSessionRect(const WSRect& rect); + WSError SetActive(bool active) override; WSError UpdateRect(const WSRect& rect, SizeChangeReason reason) override; diff --git a/window_scene/session/container/src/session_stage.cpp b/window_scene/session/container/src/session_stage.cpp index 1205352a166bd1e6179423919b1a24f22bc3af4f..2778676719a035c1ca1450081e0fff2eb3c5ad51 100644 --- a/window_scene/session/container/src/session_stage.cpp +++ b/window_scene/session/container/src/session_stage.cpp @@ -115,6 +115,10 @@ WSError SessionStage::Background() WLOGFE("session is invalid"); return WSError::WS_ERROR_NULLPTR; } + WSError res = session_->Background(); + if (res != WSError::WS_OK) { + WLOGFW("notify session background failed"); + } NotifyAfterBackground(); return WSError::WS_OK; } @@ -125,6 +129,10 @@ WSError SessionStage::Disconnect() WLOGFE("session is invalid"); return WSError::WS_ERROR_NULLPTR; } + WSError res = session_->Disconnect(); + if (res != WSError::WS_OK) { + WLOGFW("notify session background failed"); + } return WSError::WS_OK; } @@ -140,12 +148,16 @@ WSError SessionStage::PendingSessionActivation(const SessionInfo& info) WSError SessionStage::SetActive(bool active) { WLOGFD("active status: %{public}d", active); + WSError ret = WSError::WS_OK; + if (session_) { + ret = session_->UpdateActiveStatue(active); + } if (active) { NotifyAfterActive(); } else { NotifyAfterInactive(); } - return WSError::WS_OK; + return ret; } WSError SessionStage::UpdateRect(const WSRect& rect, SizeChangeReason reason) @@ -156,6 +168,14 @@ WSError SessionStage::UpdateRect(const WSRect& rect, SizeChangeReason reason) return WSError::WS_OK; } +WSError SessionStage::UpdateSessionRect(const WSRect& rect) +{ + WLOGFI("update rect [%{public}d, %{public}d, %{public}u, %{public}u]", rect.posX_, rect.posY_, + rect.width_, rect.height_); + session_->UpdateSessionRect(rect); + return WSError::WS_OK; +} + WSError SessionStage::Recover() { return WSError::WS_OK; @@ -165,4 +185,14 @@ WSError SessionStage::Maximize() { return WSError::WS_OK; } + +void SessionStage::CreateSpecificSession(const SessionType& type, sptr& session) +{ + WLOGFD("CreateSpecificSession, type: %{public}u", static_cast(type)); + if (session_ == nullptr) { + WLOGFE("sceneSession is invalid"); + return; + } + session_->CreateSpecificSession(type, session); +} } // namespace OHOS::Rosen diff --git a/window_scene/session/host/include/scene_session.h b/window_scene/session/host/include/scene_session.h index aa54ff44e77568fe483b65897b4f0a55ba0b79bf..f500e3307ff5b9894485db2420f121b2976d9e02 100644 --- a/window_scene/session/host/include/scene_session.h +++ b/window_scene/session/host/include/scene_session.h @@ -20,13 +20,22 @@ #include "session/host/include/session.h" namespace OHOS::Rosen { +class SceneSession; +using SpecificSessionCallback = std::function& sceneSession)>; +using NotifyCreateSpecificSessionFunc = std::function& session)>; class SceneSession : public Session { public: - SceneSession(const SessionInfo& info); + SceneSession(const SessionInfo& info, const SessionType& type, SpecificSessionCallback specificSessionCack); ~SceneSession() = default; WSError Recover() override; WSError Maximize() override; + void CreateSpecificSession(const SessionType& type, sptr& session) override; + void SetCreateSpecificSessionEventListener(const NotifyCreateSpecificSessionFunc& func); + + SpecificSessionCallback specificSessionCallback_; + NotifyCreateSpecificSessionFunc createSpecificSessionFunc_; }; } // namespace OHOS::Rosen diff --git a/window_scene/session/host/include/session.h b/window_scene/session/host/include/session.h index 28ffaf239a38d8df6acdb4812875e9eb64e8ab03..c280eb2ae2704dc63a6eecb963a9b3480d5c3c08 100644 --- a/window_scene/session/host/include/session.h +++ b/window_scene/session/host/include/session.h @@ -38,6 +38,8 @@ class PixelMap; namespace OHOS::Rosen { class RSSurfaceNode; using NotifyPendingSessionActivationFunc = std::function; +using NotifySessionStateChangeFunc = std::function; +using NotifySessionRectChangeFunc = std::function; class ILifecycleListener { public: @@ -67,6 +69,7 @@ public: WSError Background() override; WSError Disconnect() override; WSError PendingSessionActivation(const SessionInfo& info) override; + WSError UpdateActiveStatue(bool active) override; WSError Recover() override; WSError Maximize() override; @@ -81,17 +84,35 @@ public: bool RegisterLifecycleListener(const std::shared_ptr& listener); bool UnregisterLifecycleListener(const std::shared_ptr& listener); - const SessionInfo& GetSessionInfo() const; void SetPendingSessionActivationEventListener(const NotifyPendingSessionActivationFunc& func); + std::string GetSessionName() const; + const SessionInfo& GetSessionInfo() const; + void CreateSpecificSession(const SessionType& type, sptr& session) override; + void SetSessionStateChangeListenser(const NotifySessionStateChangeFunc& func); + void NotifySessionStateChange(const SessionState& state); + + void SetSessionRectChangeListenser(const NotifySessionRectChangeFunc& func); + void NotifySessionRectChange(const WSRect& rect); + + void SetSessionType(const SessionType& type); + SessionType GetSessionType() const; + void SetSessionRect(const WSRect& rect); + WSRect GetSessionRect() const; + void UpdateSessionRect(const WSRect& rect) override; + protected: void UpdateSessionState(SessionState state); bool IsSessionValid() const; bool isActive_ = false; - WSRect winRect_ {0, 0, 0, 0}; + WSRect winRect_ { 0, 0, 0, 0 }; sptr sessionStage_; SessionInfo sessionInfo_; + std::string sessionName_; + SessionType type_ { SessionType::TYPE_APP_MAIN_WINDOW }; NotifyPendingSessionActivationFunc pendingSessionActivationFunc_; + NotifySessionStateChangeFunc sessionStateChangeFunc_; + NotifySessionRectChangeFunc sessionRectChangeFunc_; private: template diff --git a/window_scene/session/host/include/zidl/session_interface.h b/window_scene/session/host/include/zidl/session_interface.h index 88dc947b6c11d092b5cd2645ee80f86ab18774b6..f65ee47825ab36be1f998080545fc46327540b36 100644 --- a/window_scene/session/host/include/zidl/session_interface.h +++ b/window_scene/session/host/include/zidl/session_interface.h @@ -18,7 +18,7 @@ #include -#include "scene_board_judgement.h" +#include "interfaces/innerkits/include/scene_board_judgement.h" #include "interfaces/include/ws_common.h" #include "session/container/include/zidl/session_stage_interface.h" @@ -26,7 +26,6 @@ namespace OHOS::Rosen { class RSSurfaceNode; - class ISession : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ISession"); @@ -38,17 +37,24 @@ public: TRANS_ID_BACKGROUND, TRANS_ID_DISCONNECT, TRANS_ID_ACTIVE_PENDING_SESSION, + TRANS_ID_UPDATE_ACTIVE_STATUS, + TRANS_ID_UPDATE_SESSION_RECT, // Scene TRANS_ID_RECOVER = 100, TRANS_ID_MAXIMIZE, + TRANS_ID_CREATE_SPECIFIC_SESSION, + }; virtual WSError Connect(const sptr& sessionStage, const sptr& eventChannel, const std::shared_ptr& surfaceNode) = 0; virtual WSError Foreground() = 0; virtual WSError Background() = 0; virtual WSError Disconnect() = 0; + virtual WSError UpdateActiveStatue(bool active) = 0; virtual WSError PendingSessionActivation(const SessionInfo& info) = 0; + virtual void CreateSpecificSession(const SessionType& type, sptr& session) = 0; + virtual void UpdateSessionRect(const WSRect& rect) = 0; // scene session virtual WSError Recover() = 0; diff --git a/window_scene/session/host/include/zidl/session_proxy.h b/window_scene/session/host/include/zidl/session_proxy.h index 7f780893e68b1af9919612537dc32a5da9764e6f..dcf3abd46328ca1b6ed35bbc5ecf295f74250ded 100644 --- a/window_scene/session/host/include/zidl/session_proxy.h +++ b/window_scene/session/host/include/zidl/session_proxy.h @@ -17,6 +17,7 @@ #define OHOS_ROSEN_WINDOW_SCENE_SESSION_RPOXY_H #include +#include #include "interfaces/include/ws_common.h" #include "session/host/include/zidl/session_interface.h" @@ -34,8 +35,12 @@ public: const std::shared_ptr& surfaceNode) override; WSError PendingSessionActivation(const SessionInfo& info) override; + WSError UpdateActiveStatue(bool active) override; WSError Recover() override; WSError Maximize() override; + + void CreateSpecificSession(const SessionType& type, sptr& session) override; + void UpdateSessionRect(const WSRect& rect) override; private: static inline BrokerDelegator delegator_; }; diff --git a/window_scene/session/host/include/zidl/session_stub.h b/window_scene/session/host/include/zidl/session_stub.h index f1f815dc370086302631ff4609220f598e410817..f9f0916a3be9c795e9a218dc477e4e7bf49a2529 100644 --- a/window_scene/session/host/include/zidl/session_stub.h +++ b/window_scene/session/host/include/zidl/session_stub.h @@ -18,6 +18,7 @@ #include #include +#include #include "session/host/include/zidl/session_interface.h" namespace OHOS::Rosen { @@ -40,9 +41,11 @@ private: int HandleDisconnect(MessageParcel& data, MessageParcel& reply); int HandleConnect(MessageParcel& data, MessageParcel& reply); int HandlePendingSessionActivation(MessageParcel& data, MessageParcel& reply); + int HandleUpdateActivateStatus(MessageParcel& data, MessageParcel& reply); // for scene int HandleRecover(MessageParcel& data, MessageParcel& reply); int HandleMaximize(MessageParcel& data, MessageParcel& reply); + int HandleUpdateSessionRect(MessageParcel& data, MessageParcel& reply); }; } // namespace OHOS::Rosen #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_STUB_H diff --git a/window_scene/session/host/src/scene_session.cpp b/window_scene/session/host/src/scene_session.cpp index 247f28d2d940c7384883d608ff5d9386ef39f92d..47379fface270309618fdd3eaf857f456a69ba02 100644 --- a/window_scene/session/host/src/scene_session.cpp +++ b/window_scene/session/host/src/scene_session.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "session/host/include/scene_session.h" +#include "session_manager/include/scene_session_manager.h" #include "window_manager_hilog.h" @@ -21,13 +22,19 @@ namespace { constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "SceneSession" }; } -SceneSession::SceneSession(const SessionInfo& info) : Session(info) {} +SceneSession::SceneSession(const SessionInfo& info, const SessionType& type, + SpecificSessionCallback specificSessionCallback) + : Session(info) +{ + SetSessionType(type); + specificSessionCallback_ = specificSessionCallback; +} WSError SceneSession::Recover() { WLOGFI("Recover session, id: %{public}" PRIu64 ", state: %{public}u", GetPersistentId(), static_cast(GetSessionState())); - if (!IsSessionValid()) { +if (!IsSessionValid()) { return WSError::WS_ERROR_INVALID_SESSION; } return WSError::WS_OK; @@ -42,4 +49,20 @@ WSError SceneSession::Maximize() } return WSError::WS_OK; } + +void SceneSession::SetCreateSpecificSessionEventListener(const NotifyCreateSpecificSessionFunc& func) +{ + createSpecificSessionFunc_ = func; +} + +void SceneSession::CreateSpecificSession(const SessionType& type, sptr& session) +{ + WLOGFI("CreateSpecificSession, sessionType: %{public}u", static_cast(type)); + sptr sceneSession; + specificSessionCallback_(sessionInfo_, type, sceneSession); + if (createSpecificSessionFunc_) { + createSpecificSessionFunc_(sceneSession); + } + session = sceneSession; +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 080d60a13655cfd0384599ae0fd99379460e27cf..bf3548a597563b1f5299dd8cc55dd7a7e9cf5873 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -127,6 +127,7 @@ SessionState Session::GetSessionState() const void Session::UpdateSessionState(SessionState state) { state_ = state; + NotifySessionStateChange(state); } bool Session::IsSessionValid() const @@ -148,6 +149,7 @@ WSError Session::UpdateRect(const WSRect& rect, SizeChangeReason reason) return WSError::WS_ERROR_INVALID_SESSION; } sessionStage_->UpdateRect(rect, reason); + NotifySessionRectChange(rect); return WSError::WS_OK; } @@ -196,9 +198,13 @@ WSError Session::Foreground() WSError Session::Background() { SessionState state = GetSessionState(); + if (state == SessionState::STATE_BACKGROUND) { + WLOGFD("state is already background!"); + return WSError::WS_OK; + } WLOGFI("Background session, id: %{public}" PRIu64 ", state: %{public}u", GetPersistentId(), static_cast(state)); - if (state < SessionState::STATE_INACTIVE) { // only STATE_INACTIVE can transfer to background + if (state < SessionState::STATE_INACTIVE) { // only STATE_INACTIVE can transfer to backgrounds WLOGFE("state invalid!"); return WSError::WS_ERROR_INVALID_SESSION; } @@ -221,6 +227,26 @@ WSError Session::Disconnect() return WSError::WS_OK; } +WSError Session::UpdateActiveStatue(bool active) +{ + if (!IsSessionValid()) { + return WSError::WS_ERROR_INVALID_SESSION; + } + if (active == isActive_) { + WLOGFD("Session active do not change: [%{public}d]", active); + return WSError::WS_DO_NOTHING; + } + isActive_ = active; + if (active && GetSessionState() == SessionState::STATE_FOREGROUND) { + UpdateSessionState(SessionState::STATE_ACTIVE); + } + if (!active && GetSessionState() == SessionState::STATE_ACTIVE) { + UpdateSessionState(SessionState::STATE_INACTIVE); + } + WLOGFD("UpdateActiveStatue, status: %{public}d", active); + return WSError::WS_OK; +} + WSError Session::SetActive(bool active) { SessionState state = GetSessionState(); @@ -268,9 +294,45 @@ WSError Session::Maximize() return WSError::WS_OK; } +std::string Session::GetSessionName() const +{ + return sessionName_; +} + +void Session::CreateSpecificSession(const SessionType& type, sptr& session) +{ + WLOGFD("CreateSpecificSession"); +} + +void Session::SetSessionStateChangeListenser(const NotifySessionStateChangeFunc& func) +{ + sessionStateChangeFunc_ = func; +} + +void Session::SetSessionRectChangeListenser(const NotifySessionRectChangeFunc& func) +{ + sessionRectChangeFunc_ = func; +} + +void Session::NotifySessionStateChange(const SessionState& state) +{ + WLOGFI("state: %{public}u", static_cast(state)); + if (sessionStateChangeFunc_) { + sessionStateChangeFunc_(state); + } +} + +void Session::NotifySessionRectChange(const WSRect& rect) +{ + if (sessionRectChangeFunc_) { + sessionRectChangeFunc_(rect); + } +} + +// for window event WSError Session::TransferPointerEvent(const std::shared_ptr& pointerEvent) { - WLOGFD("Session TransferPointEvent"); + WLOGFD("TransferPointerEvent, id: %{public}" PRIu64 "", GetPersistentId()); if (!windowEventChannel_) { WLOGFE("windowEventChannel_ is null"); return WSError::WS_ERROR_NULLPTR; @@ -305,4 +367,32 @@ std::shared_ptr Session::Snapshot() } return pixelMap; } + +void Session::SetSessionType(const SessionType& type) +{ + // liuqi:need to verify whether type is valid + type_ = type; +} + +SessionType Session::GetSessionType() const +{ + return type_; +} + +void Session::SetSessionRect(const WSRect& rect) +{ + winRect_ = rect; +} +WSRect Session::GetSessionRect() const +{ + return winRect_; +} + +void Session::UpdateSessionRect(const WSRect& rect) +{ + WLOGFI("UpdateSessionRect [%{public}d, %{public}d, %{public}u, %{public}u]", rect.posX_, rect.posY_, + rect.width_, rect.height_); + SetSessionRect(rect); + NotifySessionRectChange(rect); +} } // namespace OHOS::Rosen diff --git a/window_scene/session/host/src/zidl/session_proxy.cpp b/window_scene/session/host/src/zidl/session_proxy.cpp index 246d0148482c34c73ea6d5356e8816694ba6add8..178a62e13018b37712995566dd7e72668f9e5be5 100644 --- a/window_scene/session/host/src/zidl/session_proxy.cpp +++ b/window_scene/session/host/src/zidl/session_proxy.cpp @@ -115,6 +115,28 @@ WSError SessionProxy::Connect(const sptr& sessionStage, const spt return static_cast(ret); } +WSError SessionProxy::UpdateActiveStatue(bool active) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("WriteInterfaceToken failed"); + return WSError::WS_ERROR_IPC_FAILED; + } + if (!(data.WriteBool(active))) { + WLOGFE("Write active status failed"); + return WSError::WS_ERROR_IPC_FAILED; + } + if (Remote()->SendRequest(static_cast(SessionMessage::TRANS_ID_UPDATE_ACTIVE_STATUS), + data, reply, option) != ERR_NONE) { + WLOGFE("SendRequest failed"); + return WSError::WS_ERROR_IPC_FAILED; + } + int32_t ret = reply.ReadUint32(); + return static_cast(ret); +} + WSError SessionProxy::PendingSessionActivation(const SessionInfo& info) { MessageParcel data; @@ -181,6 +203,58 @@ WSError SessionProxy::Maximize() return WSError::WS_ERROR_IPC_FAILED; } int32_t ret = reply.ReadUint32(); + return static_cast(ret); } + +void SessionProxy::CreateSpecificSession(const SessionType& type, sptr& session) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("WriteInterfaceToken failed"); + return; + } + if (!(data.WriteUint32(static_cast(type)))) { + WLOGFE("Write sessionType failed"); + return; + } + if (Remote()->SendRequest(static_cast(SessionMessage::TRANS_ID_CREATE_SPECIFIC_SESSION), + data, reply, option) != ERR_NONE) { + WLOGFE("SendRequest failed"); + return; + } + sptr sessionObject = reply.ReadRemoteObject(); + if (sessionObject == nullptr) { + WLOGFE("ReadRemoteObject failed"); + return; + } + session = iface_cast(sessionObject); +} + +void SessionProxy::UpdateSessionRect(const WSRect& rect) +{ + WLOGFI("UpdateSessionRect [%{public}d, %{public}d, %{public}u, %{public}u]", rect.posX_, rect.posY_, + rect.width_, rect.height_); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("WriteInterfaceToken failed"); + return; + } + if (!((data.WriteInt32(static_cast(rect.posX_))) && + (data.WriteInt32(static_cast(rect.posY_))) && + (data.WriteUint32(static_cast(rect.width_))) && + (data.WriteUint32(static_cast(rect.height_))))) { + WLOGFE("Write rect failed"); + return; + } + if (Remote()->SendRequest(static_cast(SessionMessage::TRANS_ID_UPDATE_SESSION_RECT), + data, reply, option) != ERR_NONE) { + WLOGFE("SendRequest failed"); + return; + } +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/window_scene/session/host/src/zidl/session_stub.cpp b/window_scene/session/host/src/zidl/session_stub.cpp index cde78dc9da79ea58fa41f78bf86839d4578c3995..cde570643f5e2fe7e0565d6f1c074b49c1b527ff 100644 --- a/window_scene/session/host/src/zidl/session_stub.cpp +++ b/window_scene/session/host/src/zidl/session_stub.cpp @@ -18,6 +18,7 @@ #include #include #include "window_manager_hilog.h" +#include "session/host/include/scene_session.h" namespace OHOS::Rosen { namespace { @@ -31,6 +32,10 @@ const std::map SessionStub::stubFuncMap_{ std::make_pair(static_cast(SessionMessage::TRANS_ID_CONNECT), &SessionStub::HandleConnect), std::make_pair(static_cast(SessionMessage::TRANS_ID_ACTIVE_PENDING_SESSION), &SessionStub::HandlePendingSessionActivation), + std::make_pair(static_cast(SessionMessage::TRANS_ID_UPDATE_ACTIVE_STATUS), + &SessionStub::HandleUpdateActivateStatus), + std::make_pair(static_cast(SessionMessage::TRANS_ID_UPDATE_SESSION_RECT), + &SessionStub::HandleUpdateSessionRect), // for scene only std::make_pair(static_cast(SessionMessage::TRANS_ID_RECOVER), &SessionStub::HandleRecover), @@ -47,8 +52,22 @@ int SessionStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParc const auto func = stubFuncMap_.find(code); if (func == stubFuncMap_.end()) { - WLOGFE("Failed to find function handler!"); - return ERR_UNKNOWN_TRANSACTION; + SessionMessage msgId = static_cast(code); + switch (msgId) { + case SessionMessage::TRANS_ID_CREATE_SPECIFIC_SESSION : { + SessionType type = static_cast(data.ReadUint32()); + sptr sceneSession; + CreateSpecificSession(type, sceneSession); + if (sceneSession== nullptr) { + return ERR_INVALID_STATE; + } + reply.WriteRemoteObject(sceneSession->AsObject()); + break; + } + default: + WLOGFE("Unknown session message!"); + } + return 0; } return (this->*(func->second))(data, reply); @@ -124,4 +143,27 @@ int SessionStub::HandlePendingSessionActivation(MessageParcel& data, MessageParc reply.WriteUint32(static_cast(errCode)); return ERR_NONE; } + +int SessionStub::HandleUpdateActivateStatus(MessageParcel& data, MessageParcel& reply) +{ + WLOGFD("HandleUpdateActivateStatus!"); + bool avtiveStatus = data.ReadBool(); + WSError errCode = UpdateActiveStatue(avtiveStatus); + reply.WriteUint32(static_cast(errCode)); + return ERR_NONE; +} + +int SessionStub::HandleUpdateSessionRect(MessageParcel& data, MessageParcel& reply) +{ + WLOGFD("HandleUpdateSessionRect!"); + auto posX = data.ReadInt32(); + auto posY = data.ReadInt32(); + auto width = data.ReadUint32(); + auto height = data.ReadUint32(); + WSRect rect = {posX, posY, width, height}; + WLOGFI("HandleUpdateSessionRect [%{public}d, %{public}d, %{public}u, %{public}u]", posX, posY, + width, height); + UpdateSessionRect(rect); + return ERR_NONE; +} } // namespace OHOS::Rosen diff --git a/window_scene/session_manager/BUILD.gn b/window_scene/session_manager/BUILD.gn index 67d3906e8b6ce327e0e9ccd259442799b0b1c697..87482d5a1444139a169c2454ade379968cc3c83c 100644 --- a/window_scene/session_manager/BUILD.gn +++ b/window_scene/session_manager/BUILD.gn @@ -18,6 +18,7 @@ config("session_manager_public_config") { include_dirs = [ "${window_base_path}/window_scene", "${window_base_path}/window_scene/session_manager/include", + "${window_base_path}/window_scene/session_manager/include/zidl", "${window_base_path}/window_scene/session_manager_service/include", # for window_manager_hilog @@ -29,6 +30,8 @@ ohos_shared_library("scene_session_manager") { sources = [ "src/extension_session_manager.cpp", "src/scene_session_manager.cpp", + "src/zidl/scene_session_manager_proxy.cpp", + "src/zidl/scene_session_manager_stub.cpp" ] cflags_cc = [ "-std=c++17" ] @@ -48,6 +51,7 @@ ohos_shared_library("scene_session_manager") { "c_utils:utils", "eventhandler:libeventhandler", "hilog_native:libhilog", + "ipc:ipc_core", ] part_name = "window_manager" @@ -83,6 +87,7 @@ ohos_shared_library("session_manager") { sources = [ "../session_manager_service/src/session_manager_service_proxy.cpp", "src/session_manager.cpp", + "src/zidl/scene_session_manager_proxy.cpp", ] cflags_cc = [ "-std=c++17" ] @@ -94,7 +99,6 @@ ohos_shared_library("session_manager") { "${window_base_path}/window_scene/common:window_scene_common", "${window_base_path}/window_scene/session:scene_session", "${window_base_path}/dmserver:libdms", - "${ability_runtime_services_path}/abilitymgr:abilityms", ] external_deps = [ diff --git a/window_scene/session_manager/include/scene_session_manager.h b/window_scene/session_manager/include/scene_session_manager.h index cd2483b00c4f8cc9e022b86e402540c837beb7ce..08fa1826a30798016f8d833d96c8979204965e14 100644 --- a/window_scene/session_manager/include/scene_session_manager.h +++ b/window_scene/session_manager/include/scene_session_manager.h @@ -21,6 +21,7 @@ #include "session/host/include/root_scene_session.h" #include "wm_single_instance.h" +#include "session_manager/include/zidl/scene_session_manager_stub.h" #include "session_manager_base.h" namespace OHOS::Ace::NG { @@ -29,16 +30,19 @@ class UIWindow; namespace OHOS::Rosen { class SceneSession; -class SceneSessionManager : public SessionManagerBase { +class SceneSessionManager : public SceneSessionManagerStub, + public SessionManagerBase { WM_DECLARE_SINGLE_INSTANCE_BASE(SceneSessionManager) public: - sptr RequestSceneSession(const SessionInfo& sessionInfo); + sptr RequestSceneSession(const SessionInfo& sessionInfo, const SessionType& type); WSError RequestSceneSessionActivation(const sptr& sceneSession); WSError RequestSceneSessionBackground(const sptr& sceneSession); WSError RequestSceneSessionDestruction(const sptr& sceneSession); sptr GetRootSceneSession(); sptr GetSceneSession(uint64_t persistentId); + void CreateSpecificSession(const SessionType& type, sptr& session); + void SetCreateSpecificSessionEventListener(const NotifyCreateSpecificSessionFunc& func); protected: SceneSessionManager(); @@ -50,6 +54,7 @@ private: std::map, sptr>> abilitySceneMap_; sptr rootSceneSession_; std::shared_ptr rootScene_; + NotifyCreateSpecificSessionFunc createSpecificSessionFunc_; }; } // namespace OHOS::Rosen diff --git a/window_scene/session_manager/include/session_manager.h b/window_scene/session_manager/include/session_manager.h index d3a8daca9b82f608e8867894e4bb0d9f927d0004..19453340ea1cac631c3cfb0ee164c31db2531b2e 100644 --- a/window_scene/session_manager/include/session_manager.h +++ b/window_scene/session_manager/include/session_manager.h @@ -14,10 +14,15 @@ */ #include "iremote_object.h" +#include "wm_single_instance.h" +#include "zidl/scene_session_manager_interface.h" +#include "session_manager_service/include/session_manager_service_interface.h" +#include "interfaces/include/ws_common.h" namespace OHOS::Rosen { class AbilityConnection; class SessionManager { +WM_DECLARE_SINGLE_INSTANCE_BASE(SessionManager); public: SessionManager(); @@ -25,12 +30,20 @@ public: void Init(); - sptr GetRemoteObject() const; + sptr GetRemoteObject(); + void CreateSpecificSession(const SessionType& type, sptr& session); private: + void InitSceneSessionManagerProxy(); void ConnectToService(); + void CreateSessionManagerServiceProxy(); + void GetSceneSessionManagerProxy(); sptr abilityConnection_; + sptr remoteObject_ = nullptr; + sptr sessionManagerServiceProxy_ = nullptr; + sptr sceneSessionManagerProxy_ = nullptr; + std::recursive_mutex mutex_; }; } \ No newline at end of file diff --git a/window_scene/session_manager/include/zidl/scene_session_manager_interface.h b/window_scene/session_manager/include/zidl/scene_session_manager_interface.h new file mode 100644 index 0000000000000000000000000000000000000000..4d340fea5a3f21d7744f10378257349bddf81c9b --- /dev/null +++ b/window_scene/session_manager/include/zidl/scene_session_manager_interface.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_INTERFACE_H +#define OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_INTERFACE_H + +#include +#include "interfaces/include/ws_common.h" +#include "session/host/include/session.h" + +namespace OHOS::Rosen { +class ISceneSessionManager : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ISceneSessionManager"); + + enum class SceneSessionManagerMessage : uint32_t { + TRANS_ID_CREATE_SPECIFIC_SESSION, + }; + virtual void CreateSpecificSession(const SessionType& type, sptr& session) = 0; +}; +} // namespace OHOS::Rosen +#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_INTERFACE_H \ No newline at end of file diff --git a/window_scene/session_manager/include/zidl/scene_session_manager_proxy.h b/window_scene/session_manager/include/zidl/scene_session_manager_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..5897ae8dfa8089dedb543188feb94d2d733a1cf5 --- /dev/null +++ b/window_scene/session_manager/include/zidl/scene_session_manager_proxy.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_PROXY_H +#define OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_PROXY_H + +#include +#include +#include "session_manager/include/zidl/scene_session_manager_interface.h" + +namespace OHOS::Rosen { +class SceneSessionManagerProxy : public IRemoteProxy { +public: + explicit SceneSessionManagerProxy(const sptr& impl) + : IRemoteProxy(impl) {}; + + ~SceneSessionManagerProxy() {}; + + void CreateSpecificSession(const SessionType& type, sptr& session) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::Rosen +#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_PROXY_H diff --git a/window_scene/session_manager/include/zidl/scene_session_manager_stub.h b/window_scene/session_manager/include/zidl/scene_session_manager_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..433df607294f73ee2ea92238c6769f57e9c52a2f --- /dev/null +++ b/window_scene/session_manager/include/zidl/scene_session_manager_stub.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_STUB_H +#define OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_STUB_H + +#include +#include +#include +#include "session_manager/include/zidl/scene_session_manager_interface.h" + +namespace OHOS::Rosen { +class SceneSessionManagerStub : public IRemoteStub { +public: + SceneSessionManagerStub() = default; + ~SceneSessionManagerStub() = default; + + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) override; + +private: +}; +} // namespace OHOS::Rosen +#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_STUB_H diff --git a/window_scene/session_manager/src/scene_session_manager.cpp b/window_scene/session_manager/src/scene_session_manager.cpp index 7b7940c87377ce7df6471fb9cd10aa87b08d83aa..fbe5e2dc1f3cc8b8bd698767cbc8c5853233c4ff 100644 --- a/window_scene/session_manager/src/scene_session_manager.cpp +++ b/window_scene/session_manager/src/scene_session_manager.cpp @@ -73,12 +73,15 @@ sptr SceneSessionManager::GetSceneSession(uint64_t persistentId) return iter->second.first; } -sptr SceneSessionManager::RequestSceneSession(const SessionInfo& sessionInfo) +sptr SceneSessionManager::RequestSceneSession(const SessionInfo& sessionInfo, const SessionType& type) { - auto task = [this, sessionInfo]() { + auto func = [this](const SessionInfo& info, const SessionType& type, sptr& sceneSession) { + sceneSession = RequestSceneSession(info, type); + }; + auto task = [this, sessionInfo, type, func]() { WLOGFI("sessionInfo: bundleName: %{public}s, abilityName: %{public}s", sessionInfo.bundleName_.c_str(), sessionInfo.abilityName_.c_str()); - sptr sceneSession = new (std::nothrow) SceneSession(sessionInfo); + sptr sceneSession = new (std::nothrow) SceneSession(sessionInfo, type, func); uint64_t persistentId = GeneratePersistentId(); sceneSession->SetPersistentId(persistentId); abilitySceneMap_.insert({ persistentId, std::make_pair(sceneSession, nullptr) }); @@ -180,4 +183,21 @@ WSError SceneSessionManager::RequestSceneSessionDestruction(const sptrPostAsyncTask(task); return WSError::WS_OK; } + +void SceneSessionManager::CreateSpecificSession(const SessionType& type, sptr& session) +{ + WLOGFD("CreateSpecificSession"); + SessionInfo info; + sptr sceneSession = RequestSceneSession(info, type); + if (createSpecificSessionFunc_) { + createSpecificSessionFunc_(sceneSession); + } + session = sceneSession; +} + +void SceneSessionManager::SetCreateSpecificSessionEventListener(const NotifyCreateSpecificSessionFunc& func) +{ + WLOGFD("SetCreateSpecificSessionEventListener"); + createSpecificSessionFunc_ = func; +} } // namespace OHOS::Rosen diff --git a/window_scene/session_manager/src/session_manager.cpp b/window_scene/session_manager/src/session_manager.cpp index 84fc9d3df1842e6dcab0271f38f5dc9cac0d51a8..651da3f3b549112b713ac7c7350569fc6b6cb982 100644 --- a/window_scene/session_manager/src/session_manager.cpp +++ b/window_scene/session_manager/src/session_manager.cpp @@ -14,12 +14,19 @@ */ #include "session_manager.h" + +#include #include "ability_manager_client.h" #include "ability_connect_callback_stub.h" #include "window_manager_hilog.h" //#include "ability_connection.h" +#include "zidl/scene_session_manager_proxy.h" +#include "session_manager_service/include/session_manager_service_proxy.h" namespace OHOS::Rosen { +namespace { +constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionManager"}; +} class AbilityConnection : public AAFwk::AbilityConnectionStub { public: void OnAbilityConnectDone( @@ -44,6 +51,8 @@ private: sptr remoteObject_; }; +WM_IMPLEMENT_SINGLE_INSTANCE(SessionManager) + SessionManager::SessionManager() { } @@ -59,26 +68,109 @@ void SessionManager::Init() } } -sptr SessionManager::GetRemoteObject() const +sptr SessionManager::GetRemoteObject() { + if (remoteObject_) { + WLOGFE("GetRemoteObject ===== 1"); + return remoteObject_; + } if (abilityConnection_) { - return abilityConnection_->GetRemoteObject(); + remoteObject_ = abilityConnection_->GetRemoteObject(); + if (remoteObject_) { + WLOGFE("GetRemoteObject ===== 2"); + return remoteObject_; + } + WLOGFE("GetRemoteObject ===== 3"); + } + + int count = 0; + std::lock_guard lock(mutex_); + while (remoteObject_ == nullptr && count < 10) { + usleep(100); + if (abilityConnection_) { + remoteObject_ = abilityConnection_->GetRemoteObject(); + WLOGFE("GetRemoteObject ===== 4"); + } + } + if (remoteObject_ != nullptr) { + WLOGFE("GetRemoteObject ===== 5"); + } + WLOGFE("GetRemoteObject ===== 6"); + return remoteObject_; +} + +void SessionManager::CreateSessionManagerServiceProxy() +{ + sptr remoteObject = GetRemoteObject(); + if(remoteObject) { + // sessionManagerServiceProxy_ = iface_cast(remoteObject); + sessionManagerServiceProxy_ = dynamic_cast(new SessionManagerServiceProxy(remoteObject)); + if (!sessionManagerServiceProxy_) { + WLOGFE("sessionManagerServiceProxy_ is nullptr"); + } else { + WLOGFD("CreateSessionManagerServiceProxy success"); + } + } else { + WLOGFE("GetRemoteObject null"); + } +} + +void SessionManager::GetSceneSessionManagerProxy() +{ + if (sceneSessionManagerProxy_) { + return; + } + if (!sessionManagerServiceProxy_) { + WLOGFE("sessionManagerServiceProxy_ is nullptr"); + } + + sptr remoteObject = sessionManagerServiceProxy_->GetSceneSessionManager(); + // sceneSessionManagerProxy_ = iface_cast(remoteObject); + if (!remoteObject) { + WLOGFW("Get screen session manager proxy failed, screen session manager service is null"); + return; + } + sceneSessionManagerProxy_ = dynamic_cast(new SceneSessionManagerProxy(remoteObject)); + if (!sceneSessionManagerProxy_) { + WLOGFW("Get screen session manager proxy failed, nullptr"); + } else { + WLOGFD("GetSceneSessionManagerProxy success"); } - return nullptr; } void SessionManager::ConnectToService() { + WLOGFD("ConnectToService"); if (abilityConnection_ == nullptr) { abilityConnection_ = new AbilityConnection(); } AAFwk::Want want; - want.SetElementName("com.ohos.launcher", "com.ohos.launcher.MainAbility"); + want.SetElementName("com.ohos.sceneboard", "com.ohos.sceneboard.MainAbility"); ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, abilityConnection_, nullptr); if (ret != ERR_OK) { WLOG_E("SessionManager::ConnectToService failed, errorcode: %{public}d",ret); } } +void SessionManager::InitSceneSessionManagerProxy() +{ + WLOGFD("InitSceneSessionManagerProxy"); + Init(); + usleep(100000); + CreateSessionManagerServiceProxy(); + GetSceneSessionManagerProxy(); +} + +void SessionManager::CreateSpecificSession(const SessionType& type, sptr& session) +{ + WLOGFD("CreateSpecificSession"); + InitSceneSessionManagerProxy(); + if (!sceneSessionManagerProxy_) { + WLOGFE("sessionManagerServiceProxy is nullptr"); + return; + } + sceneSessionManagerProxy_->CreateSpecificSession(type, session); +} + } // namespace OHOS::Rosen \ No newline at end of file diff --git a/window_scene/session_manager/src/zidl/scene_session_manager_proxy.cpp b/window_scene/session_manager/src/zidl/scene_session_manager_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..824adde3cbd75881a9550c191766a0a2fcf1c5c8 --- /dev/null +++ b/window_scene/session_manager/src/zidl/scene_session_manager_proxy.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "session_manager/include/zidl/scene_session_manager_proxy.h" + +#include +#include +#include + +#include "window_manager_hilog.h" + +namespace OHOS::Rosen { +namespace { + constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SceneSessionManagerProxy"}; +} +void SceneSessionManagerProxy::CreateSpecificSession(const SessionType& type, sptr& session) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("WriteInterfaceToken failed"); + return; + } + if (!(data.WriteUint32(static_cast(type)))) { + WLOGFE("Write sessionType failed"); + return; + } + if (Remote()->SendRequest(static_cast(SceneSessionManagerMessage::TRANS_ID_CREATE_SPECIFIC_SESSION), + data, reply, option) != ERR_NONE) { + WLOGFE("SendRequest failed"); + return; + } + sptr sessionObject = reply.ReadRemoteObject(); + if (sessionObject == nullptr) { + WLOGFE("ReadRemoteObject failed"); + return; + } + session = iface_cast(sessionObject); +} +} // namespace OHOS::Rosen \ No newline at end of file diff --git a/window_scene/session_manager/src/zidl/scene_session_manager_stub.cpp b/window_scene/session_manager/src/zidl/scene_session_manager_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4d15862282cf11b19a74833f48d1fd616cf3f84 --- /dev/null +++ b/window_scene/session_manager/src/zidl/scene_session_manager_stub.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "session_manager/include/zidl/scene_session_manager_stub.h" + +#include +#include "session/host/include/scene_session.h" +#include "window_manager_hilog.h" + +namespace OHOS::Rosen { +namespace { + constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SceneSessionManagerStub"}; +} + +int SceneSessionManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + WLOGFD("Scene session on remote request!, code: %{public}u", code); + if (data.ReadInterfaceToken() != GetDescriptor()) { + WLOGFE("Failed to check interface token!"); + return ERR_INVALID_STATE; + } + + SceneSessionManagerMessage msgId = static_cast(code); + switch (msgId) { + case SceneSessionManagerMessage::TRANS_ID_CREATE_SPECIFIC_SESSION : { + SessionType type = static_cast(data.ReadUint32()); + sptr sceneSession; + CreateSpecificSession(type, sceneSession); + if (sceneSession== nullptr) { + return ERR_INVALID_STATE; + } + reply.WriteRemoteObject(sceneSession->AsObject()); + break; + } + default: + WLOGFE("Unknown session message!"); + } + return ERR_NONE; +} +} // namespace OHOS::Rosen diff --git a/window_scene/session_manager_service/BUILD.gn b/window_scene/session_manager_service/BUILD.gn index d5234ad6f99baf04095d9a03b89c8964b5aa302d..1f70aff268c78ace2262752a205431baf8cd25f6 100644 --- a/window_scene/session_manager_service/BUILD.gn +++ b/window_scene/session_manager_service/BUILD.gn @@ -20,6 +20,7 @@ config("session_manager_service_public_config") { # for window_manager_hilog "${window_base_path}/utils/include", + "${window_base_path}/window_scene/session_manager/include", "${window_base_path}/window_scene/session_manager_service/include", ] } @@ -38,6 +39,7 @@ ohos_shared_library("session_manager_service") { "${graphic_base_path}/graphic_2d/rosen/modules/render_service_client:librender_service_client", "${window_base_path}/window_scene/common:window_scene_common", "${window_base_path}/window_scene/session:scene_session", + "${window_base_path}/window_scene/session_manager:scene_session_manager", "${window_base_path}/dmserver:libdms", "${window_base_path}/utils:libwmutil", ] diff --git a/window_scene/session_manager_service/include/session_manager_service.h b/window_scene/session_manager_service/include/session_manager_service.h index 404f46270be8f55d647de8a1506f8d26651f22f4..dd1c34f6a72d3cd754d153c2f33046a9c52760a3 100644 --- a/window_scene/session_manager_service/include/session_manager_service.h +++ b/window_scene/session_manager_service/include/session_manager_service.h @@ -29,13 +29,15 @@ public: int GetValueById(int id) override; + sptr GetSceneSessionManager() override; + IRemoteObject* GetRemoteObject(); private: void Init(); std::recursive_mutex mutex_; - static inline SingletonDelegator delegator_; + // static inline SingletonDelegator delegator_; }; } // namesapce OHOS::Rosen #endif // FOUNDATION_WINDOW_SCENE_SESSION_MANAGER_SERVICE_H \ No newline at end of file diff --git a/window_scene/session_manager_service/include/session_manager_service_interface.h b/window_scene/session_manager_service/include/session_manager_service_interface.h index 7e9cf0916f48fa6225487fdb6772f5d71a231d1c..555645ad55f4a828e398491905d1beb27d37b0b0 100644 --- a/window_scene/session_manager_service/include/session_manager_service_interface.h +++ b/window_scene/session_manager_service/include/session_manager_service_interface.h @@ -16,7 +16,9 @@ #ifndef FOUNDATION_WINDOW_SCENE_SESSION_MANAGER_SERVICE_INTERFACE_H #define FOUNDATION_WINDOW_SCENE_SESSION_MANAGER_SERVICE_INTERFACE_H +#include #include +#include "iremote_object.h" namespace OHOS::Rosen { class ISessionManagerService : public IRemoteBroker { @@ -26,9 +28,11 @@ public: enum class SessionManagerServiceMessage : uint32_t { TRANS_ID_SCREEN_BASE = 0, TRANS_ID_GET_SCREEN_INFO_BY_ID, + TRANS_ID_GET_SCENE_SESSION_MANAGER, }; virtual int GetValueById(int id) = 0; + virtual sptr GetSceneSessionManager() = 0; }; } // namespace OHOS::Rosen diff --git a/window_scene/session_manager_service/include/session_manager_service_proxy.h b/window_scene/session_manager_service/include/session_manager_service_proxy.h index b98c44da68d0bdcdf39adc8753e24b9cae41baaf..bfc38a7a821ae82e53afe1fd1a59eb2f04dfb6e4 100644 --- a/window_scene/session_manager_service/include/session_manager_service_proxy.h +++ b/window_scene/session_manager_service/include/session_manager_service_proxy.h @@ -23,13 +23,15 @@ namespace OHOS::Rosen { class SessionManagerServiceProxy : public IRemoteProxy { public: - explicit SessionManagerServiceProxy(sptr& remoteObject); + explicit SessionManagerServiceProxy(const sptr& remoteObject); ~SessionManagerServiceProxy() override; int GetValueById(int id) override; - + sptr GetSceneSessionManager() override; +private: + static inline BrokerDelegator delegator_; }; } // namespace OHOS::Rosen #endif // FOUNDATION_WINDOW_SCENE_SESSION_MANAGER_SERVICE_PROXY_H \ No newline at end of file diff --git a/window_scene/session_manager_service/include/session_manager_service_stub.h b/window_scene/session_manager_service/include/session_manager_service_stub.h index 5d81f9c60294d8256b322c9d7c4456f23d37b314..4b9991456a175179f5bb0a4406290a683e6da838 100644 --- a/window_scene/session_manager_service/include/session_manager_service_stub.h +++ b/window_scene/session_manager_service/include/session_manager_service_stub.h @@ -19,6 +19,8 @@ #include "session_manager_service_interface.h" #include +#include "interfaces/include/ws_common.h" +#include "session/host/include/session.h" namespace OHOS::Rosen { class SessionManagerServiceStub : public IRemoteStub { diff --git a/window_scene/session_manager_service/src/session_manager_service.cpp b/window_scene/session_manager_service/src/session_manager_service.cpp index 1c8791eb7542aa23bd98b5d0a11da8ec57f2e913..8a7bf3b848948fff33bb33a86a70250183af9cce 100644 --- a/window_scene/session_manager_service/src/session_manager_service.cpp +++ b/window_scene/session_manager_service/src/session_manager_service.cpp @@ -17,11 +17,12 @@ #include +#include "session_manager/include/scene_session_manager.h" #include "window_manager_hilog.h" namespace OHOS::Rosen { namespace { - // constexpr HiviewDFX::HiLogLabel LEVEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SessionManagerService"}; + constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionManagerService"}; } WM_IMPLEMENT_SINGLE_INSTANCE(SessionManagerService) @@ -34,9 +35,14 @@ int SessionManagerService::GetValueById(int id) return id + 1; } +sptr SessionManagerService::GetSceneSessionManager() +{ + WLOGFD("GetSceneSessionManager success"); + return &(SceneSessionManager::GetInstance()); +} + IRemoteObject* SessionManagerService::GetRemoteObject() { return dynamic_cast(this); } - } // namesapce OHOS::Rosen \ No newline at end of file diff --git a/window_scene/session_manager_service/src/session_manager_service_proxy.cpp b/window_scene/session_manager_service/src/session_manager_service_proxy.cpp index 1d5b53ffa1457ded6f6cff482437196090148f0c..2186c86d8cd3f4b05c8e910ede4b4cc7d84419f9 100644 --- a/window_scene/session_manager_service/src/session_manager_service_proxy.cpp +++ b/window_scene/session_manager_service/src/session_manager_service_proxy.cpp @@ -18,10 +18,11 @@ namespace OHOS::Rosen { namespace { - constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SessionManagerServiceProxy"}; + constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionManagerServiceProxy"}; } -SessionManagerServiceProxy::SessionManagerServiceProxy(sptr& remoteObject) : IRemoteProxy(remoteObject) +SessionManagerServiceProxy::SessionManagerServiceProxy(const sptr& remoteObject) : + IRemoteProxy(remoteObject) { } @@ -62,4 +63,32 @@ int SessionManagerServiceProxy::GetValueById(int id) return value; } + +sptr SessionManagerServiceProxy::GetSceneSessionManager() +{ + sptr remote = Remote(); + if (remote == nullptr) { + WLOGFE("GetValueById remote is nullptr"); + return nullptr; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("GetValueById: WriteInterfacetoken failed"); + return nullptr; + } + + int ret = remote->SendRequest(static_cast( + SessionManagerServiceMessage::TRANS_ID_GET_SCENE_SESSION_MANAGER), + data, reply, option); + if ( ret != ERR_NONE) { + WLOGFW("GetValueById: SendRequest failed, errorCode %{public}d", ret); + return nullptr; + } + + return reply.ReadRemoteObject(); +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/window_scene/session_manager_service/src/session_manager_service_stub.cpp b/window_scene/session_manager_service/src/session_manager_service_stub.cpp index 11c66b7dd9abdbdee7509db734675c8aefad657d..bc30ec185a97c1726ceeb528098869d9534d9951 100644 --- a/window_scene/session_manager_service/src/session_manager_service_stub.cpp +++ b/window_scene/session_manager_service/src/session_manager_service_stub.cpp @@ -16,13 +16,12 @@ #include "session_manager_service_stub.h" #include - #include "marshalling_helper.h" #include "window_manager_hilog.h" namespace OHOS::Rosen { namespace { - constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SessionManagerServiceStub"}; + constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionManagerServiceStub"}; } int32_t SessionManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, @@ -46,6 +45,10 @@ int32_t SessionManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel reply.WriteInt32(value); break; } + case SessionManagerServiceMessage::TRANS_ID_GET_SCENE_SESSION_MANAGER: { + reply.WriteRemoteObject(GetSceneSessionManager()); + break; + } default: { WLOGFW("unknown transaction code."); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); diff --git a/wm/BUILD.gn b/wm/BUILD.gn index 827d43230e387d8f2265fbef4462a6f9ad1e2157..eecc3a25e8e9106cc7e1b725a432c63c5e5c8217 100644 --- a/wm/BUILD.gn +++ b/wm/BUILD.gn @@ -67,6 +67,7 @@ ohos_shared_library("libwm") { "//foundation/graphic/graphic_2d/rosen/modules/render_service_client:librender_service_client", "//foundation/window/window_manager/dm:libdm", "//foundation/window/window_manager/utils:libwmutil", + "${window_base_path}/window_scene/session_manager:session_manager", ] external_deps = [ diff --git a/wm/include/zidl/window_stub.h b/wm/include/zidl/window_stub.h index 45964f530fee816590dc75e0ae5fccdb97decf12..655c202165dedcc207a50e991976da144058d603 100644 --- a/wm/include/zidl/window_stub.h +++ b/wm/include/zidl/window_stub.h @@ -21,7 +21,7 @@ namespace OHOS { namespace Rosen { -class WindowStub : public IRemoteStub { +class WindowStub : public IRemoteStub { // ISessionStage -> IWindow public: WindowStub() = default; ~WindowStub() = default; diff --git a/wm/src/vsync_station.cpp b/wm/src/vsync_station.cpp index 0444b5174d6d67ae23c26bef10d3613159b15d98..3c5937eb252549609812b3bc2bcf8b360b458b20 100644 --- a/wm/src/vsync_station.cpp +++ b/wm/src/vsync_station.cpp @@ -32,6 +32,7 @@ WM_IMPLEMENT_SINGLE_INSTANCE(VsyncStation) void VsyncStation::RequestVsync(const std::shared_ptr& vsyncCallback) { + WLOGFI("============ RequestVsync =============="); { std::lock_guard lock(mtx_); vsyncCallbacks_.insert(vsyncCallback); diff --git a/wm/src/window.cpp b/wm/src/window.cpp index 3e85dde5fcea23a3ca687064ce17436e0f17da5d..91253367c0c2c5e326bfb367cda924fc9ddad950 100644 --- a/wm/src/window.cpp +++ b/wm/src/window.cpp @@ -27,6 +27,7 @@ namespace { sptr Window::Create(const std::string& windowName, sptr& option, const std::shared_ptr& context, WMError& errCode) { + WLOGFD("Create Window"); if (windowName.empty()) { WLOGFE("window name is empty"); return nullptr; diff --git a/wm/src/window_adapter.cpp b/wm/src/window_adapter.cpp index d78a5f28cb3c53c29cd17dc4b79123402f12d38e..3847208ed830781c9aecf8b5d107dd20f431c30f 100644 --- a/wm/src/window_adapter.cpp +++ b/wm/src/window_adapter.cpp @@ -16,9 +16,12 @@ #include "window_adapter.h" #include #include +#include "scene_board_judgement.h" #include "window_manager.h" #include "window_manager_hilog.h" #include "wm_common.h" +#include "window_helper.h" +#include "session_manager/include/session_manager.h" namespace OHOS { namespace Rosen { @@ -31,15 +34,23 @@ WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter) do { \ if (!InitWMSProxy()) { \ WLOGFE("InitWMSProxy failed!"); \ - return ret; \ + return ret; \ } \ } while (false) WMError WindowAdapter::CreateWindow(sptr& window, sptr& windowProperty, std::shared_ptr surfaceNode, uint32_t& windowId, const sptr& token) { - INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR); - return windowManagerServiceProxy_->CreateWindow(window, windowProperty, surfaceNode, windowId, token); + WMError ret = WMError::WM_OK; + if (SceneBoardJudgement::IsSceneBoardEnabled()) { + WLOGFD("CreateWindow"); + sptr session; + SessionManager::GetInstance().CreateSpecificSession(SessionType::TYPE_FLOAT, session); + } else { + INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR); + ret = windowManagerServiceProxy_->CreateWindow(window, windowProperty, surfaceNode, windowId, token); + } + return ret; } WMError WindowAdapter::AddWindow(sptr& windowProperty) diff --git a/wmserver/test/unittest/window_node_test.cpp b/wmserver/test/unittest/window_node_test.cpp index d82e04112205bd4b796d5130db17eeace5a0c9ab..28971ef6da141b742ad4da1b7495ee3937017296 100644 --- a/wmserver/test/unittest/window_node_test.cpp +++ b/wmserver/test/unittest/window_node_test.cpp @@ -52,7 +52,7 @@ void WindowNodeTest::TearDown() class WindowListener : public IWindow { public: virtual WMError UpdateWindowRect(const struct Rect& rect, bool decoStatus, WindowSizeChangeReason reason, - const std::shared_ptr rsTransaction = nullptr) override + const std::shared_ptr& rsTransaction = nullptr) override { return WMError::WM_OK; }; @@ -84,7 +84,8 @@ public: { return WMError::WM_OK; }; - virtual WMError UpdateOccupiedAreaChangeInfo(const sptr& info) override + virtual WMError UpdateOccupiedAreaChangeInfo(const sptr& info, + const std::shared_ptr& rsTransaction = nullptr) override { return WMError::WM_OK; };