From dc3934a72f01b40208e8166176cc1cba9605b987 Mon Sep 17 00:00:00 2001 From: xiahaiqin Date: Wed, 14 Dec 2022 15:30:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96session=E7=AE=A1=E7=90=86?= =?UTF-8?q?=EF=BC=9A=20-=E6=8B=86=E5=88=86napi=E5=92=8Cnative=20-=E5=AF=B9?= =?UTF-8?q?=E5=A4=96=E6=8E=A5=E5=8F=A3=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiahaiqin Change-Id: I663b2924749efb81c47ea2b994a902b2953485f7 --- bundle.json | 6 +- interfaces/kits/napi/BUILD.gn | 2 +- window_scene/BUILD.gn | 9 +-- .../innerkits/scene_session_interface.h | 4 +- .../innerkits}/scene_session_stage.h | 0 .../scene_session_stage_interface.h | 2 +- .../innerkits}/session.h | 6 +- .../innerkits}/session_stage.h | 2 +- .../innerkits}/session_stage_interface.h | 0 .../window_event_channel_interface.h | 9 ++- .../interfaces/kits/napi/host}/BUILD.gn | 24 +++++++ .../kits/napi/host/js_scene_session.cpp | 67 +++++++++++++++---- .../kits/napi/host/js_scene_session.h | 11 +-- .../napi/host/js_scene_session_manager.cpp | 67 +++++++++---------- .../kits/napi/host/js_scene_utils.cpp | 1 + .../kits/napi/host/js_scene_utils.h | 3 +- .../container/include/window_event_channel.h | 1 + .../include/zidl/scene_session_stage_proxy.h | 2 +- .../include/zidl/scene_session_stage_stub.h | 2 +- .../include/zidl/window_event_channel_proxy.h | 2 +- .../include/zidl/window_event_channel_stub.h | 2 +- .../container/src/window_event_channel.cpp | 6 ++ .../src/zidl/window_event_channel_proxy.cpp | 3 + .../src/zidl/window_event_channel_stub.cpp | 3 + .../session/host/include/scene_session.h | 22 +++--- .../host/include/scene_session_manager.h | 7 +- .../session/host/src/scene_session.cpp | 26 +++---- .../host/src/scene_session_manager.cpp | 30 +-------- window_scene/session/host/src/session.cpp | 2 + 29 files changed, 180 insertions(+), 141 deletions(-) rename window_scene/{session/container/include => interfaces/innerkits}/scene_session_stage.h (100%) rename window_scene/{session/container/include/zidl => interfaces/innerkits}/scene_session_stage_interface.h (90%) rename window_scene/{session/host/include => interfaces/innerkits}/session.h (93%) rename window_scene/{session/container/include => interfaces/innerkits}/session_stage.h (98%) rename window_scene/{session/container/include/zidl => interfaces/innerkits}/session_stage_interface.h (100%) rename window_scene/{session/container/include/zidl => interfaces/innerkits}/window_event_channel_interface.h (90%) rename {interfaces/kits/napi/window_scene => window_scene/interfaces/kits/napi/host}/BUILD.gn (57%) diff --git a/bundle.json b/bundle.json index f3a682826e..4429836231 100644 --- a/bundle.json +++ b/bundle.json @@ -60,7 +60,8 @@ "//foundation/window/window_manager/snapshot:snapshot_display", "//foundation/window/window_manager/interfaces/kits/napi/window_runtime/window_stage_napi:windowstage", "//foundation/window/window_manager/interfaces/kits/napi:napi_packages", - "//foundation/window/window_manager/resources:window_resources" + "//foundation/window/window_manager/resources:window_resources", + "//foundation/window/window_manager/window_scene/interfaces/kits/napi/host:window_scene_napi" ], "fwk_group": [ "//foundation/window/window_manager/interfaces/kits/js/declaration:window", @@ -69,7 +70,8 @@ "//foundation/window/window_manager/extension/window_extension:libwindow_extension", "//foundation/window/window_manager/extension/window_extension:window_extension_module", "//foundation/window/window_manager/wm:libwm", - "//foundation/window/window_manager/utils:libwmutil" + "//foundation/window/window_manager/utils:libwmutil", + "//foundation/window/window_manager/window_scene:window_scene" ], "service_group": [ "//foundation/window/window_manager/sa_profile:wms_sa_profile", diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index 8e0261138d..96ed12a543 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -24,6 +24,6 @@ group("napi_packages") { "window_runtime:window_napi", "window_runtime:window_native_kit", "window_runtime:windowstage_kit", - "window_scene:window_scene_napi", + "//foundation/window/window_manager/window_scene/interfaces/kits/napi/host:window_scene_napi", ] } diff --git a/window_scene/BUILD.gn b/window_scene/BUILD.gn index bc52e5d5a6..9017d34c6f 100644 --- a/window_scene/BUILD.gn +++ b/window_scene/BUILD.gn @@ -25,8 +25,9 @@ config("window_scene_private_config") { config("window_scene_public_config") { include_dirs = [ "interfaces/innerkits", - "interfaces/kits/napi/host", "utils/include", + "session/container/include", + "session/host/include", ] } @@ -43,9 +44,6 @@ ohos_shared_library("window_scene") { "session/host/src/session.cpp", "session/host/src/zidl/scene_session_proxy.cpp", "session/host/src/zidl/scene_session_stub.cpp", - "interfaces/kits/napi/host/js_scene_session_manager.cpp", - "interfaces/kits/napi/host/js_scene_session.cpp", - "interfaces/kits/napi/host/js_scene_utils.cpp", ] cflags_cc = [ "-std=c++17" ] @@ -60,11 +58,8 @@ ohos_shared_library("window_scene") { "hilog_native:libhilog", "input:libmmi-client", "ipc:ipc_core", - "napi:ace_napi", - "ability_runtime:runtime", ] - relative_install_dir = "module" part_name = "window_manager" subsystem_name = "window" } diff --git a/window_scene/interfaces/innerkits/scene_session_interface.h b/window_scene/interfaces/innerkits/scene_session_interface.h index f405502638..4b985e516a 100644 --- a/window_scene/interfaces/innerkits/scene_session_interface.h +++ b/window_scene/interfaces/innerkits/scene_session_interface.h @@ -17,9 +17,9 @@ #define OHOS_SCENE_SESSION_INTERFACE_H #include "zidl/session_interface.h" -#include "zidl/scene_session_stage_interface.h" +#include "scene_session_stage_interface.h" #include "window_scene_common.h" -#include "zidl/window_event_channel_interface.h" +#include "window_event_channel_interface.h" namespace OHOS::Rosen { class ISceneSession : public ISession { diff --git a/window_scene/session/container/include/scene_session_stage.h b/window_scene/interfaces/innerkits/scene_session_stage.h similarity index 100% rename from window_scene/session/container/include/scene_session_stage.h rename to window_scene/interfaces/innerkits/scene_session_stage.h diff --git a/window_scene/session/container/include/zidl/scene_session_stage_interface.h b/window_scene/interfaces/innerkits/scene_session_stage_interface.h similarity index 90% rename from window_scene/session/container/include/zidl/scene_session_stage_interface.h rename to window_scene/interfaces/innerkits/scene_session_stage_interface.h index f34eccac2e..0c387b17ff 100644 --- a/window_scene/session/container/include/zidl/scene_session_stage_interface.h +++ b/window_scene/interfaces/innerkits/scene_session_stage_interface.h @@ -16,7 +16,7 @@ #ifndef OHOS_SCEEN_SESSION_STAGE_INTERFACE_H #define OHOS_SCEEN_SESSION_STAGE_INTERFACE_H -#include "zidl/session_stage_interface.h" +#include "session_stage_interface.h" #include "window_scene_common.h" namespace OHOS::Rosen { diff --git a/window_scene/session/host/include/session.h b/window_scene/interfaces/innerkits/session.h similarity index 93% rename from window_scene/session/host/include/session.h rename to window_scene/interfaces/innerkits/session.h index ab5ed11c69..9a9ecb754b 100644 --- a/window_scene/session/host/include/session.h +++ b/window_scene/interfaces/innerkits/session.h @@ -18,10 +18,12 @@ #include #include -#include + #include "window_scene_common.h" namespace OHOS::Rosen { +class RSSurfaceNode; + class Session { public: Session(const std::string& name); @@ -39,7 +41,7 @@ protected: bool IsSessionValid() const; private: - RSSurfaceNode::SharedPtr CreateSurfaceNode(std::string name); + std::shared_ptr CreateSurfaceNode(std::string name); uint32_t persistentId_ = INVALID_SESSION_ID; std::shared_ptr surfaceNode_ = nullptr; diff --git a/window_scene/session/container/include/session_stage.h b/window_scene/interfaces/innerkits/session_stage.h similarity index 98% rename from window_scene/session/container/include/session_stage.h rename to window_scene/interfaces/innerkits/session_stage.h index 086f0b9fe0..569202ddd6 100644 --- a/window_scene/session/container/include/session_stage.h +++ b/window_scene/interfaces/innerkits/session_stage.h @@ -20,7 +20,7 @@ #include #include #include "window_scene_common.h" -#include "zidl/window_event_channel_interface.h" +#include "window_event_channel_interface.h" namespace OHOS::Rosen { class ISessionStateListener { diff --git a/window_scene/session/container/include/zidl/session_stage_interface.h b/window_scene/interfaces/innerkits/session_stage_interface.h similarity index 100% rename from window_scene/session/container/include/zidl/session_stage_interface.h rename to window_scene/interfaces/innerkits/session_stage_interface.h diff --git a/window_scene/session/container/include/zidl/window_event_channel_interface.h b/window_scene/interfaces/innerkits/window_event_channel_interface.h similarity index 90% rename from window_scene/session/container/include/zidl/window_event_channel_interface.h rename to window_scene/interfaces/innerkits/window_event_channel_interface.h index 4e944066b8..669e139bdc 100644 --- a/window_scene/session/container/include/zidl/window_event_channel_interface.h +++ b/window_scene/interfaces/innerkits/window_event_channel_interface.h @@ -18,9 +18,12 @@ #include #include "window_scene_common.h" -#include "axis_event.h" -#include "key_event.h" -#include "pointer_event.h" + +namespace OHOS::MMI { + class PointerEvent; + class KeyEvent; + class AxisEvent; +} namespace OHOS::Rosen { class IWindowEventChannel : public IRemoteBroker { diff --git a/interfaces/kits/napi/window_scene/BUILD.gn b/window_scene/interfaces/kits/napi/host/BUILD.gn similarity index 57% rename from interfaces/kits/napi/window_scene/BUILD.gn rename to window_scene/interfaces/kits/napi/host/BUILD.gn index dfcc51681b..5b1226a741 100644 --- a/interfaces/kits/napi/window_scene/BUILD.gn +++ b/window_scene/interfaces/kits/napi/host/BUILD.gn @@ -13,9 +13,33 @@ import("//build/ohos.gni") +config("window_scene_napi_private_config") { + visibility = [ ":*" ] + + include_dirs = + [ "//foundation/window/window_manager/window_scene/interfaces/kits/napi/host", ] +} + ohos_shared_library("window_scene_napi") { sources = [ + "js_scene_session.cpp", + "js_scene_session_manager.cpp", + "js_scene_utils.cpp", + ] + cflags_cc = [ "-std=c++17" ] + configs = [ ":window_scene_napi_private_config" ] + + deps = [ + "//foundation/window/window_manager/window_scene:window_scene" + ] + + external_deps = [ + "c_utils:utils", + "ability_runtime:runtime", + "hilog_native:libhilog", + "ipc:ipc_core", + "napi:ace_napi", ] relative_install_dir = "module" diff --git a/window_scene/interfaces/kits/napi/host/js_scene_session.cpp b/window_scene/interfaces/kits/napi/host/js_scene_session.cpp index 7558174bf6..3a124fc9f4 100644 --- a/window_scene/interfaces/kits/napi/host/js_scene_session.cpp +++ b/window_scene/interfaces/kits/napi/host/js_scene_session.cpp @@ -29,15 +29,14 @@ namespace { const std::string START_SCENE_CB = "startScene"; } -NativeValue* CreateJsSceneSessionObject(NativeEngine& engine, const sptr& session, - const std::shared_ptr& jsSceneSession) +NativeValue* CreateJsSceneSessionObject(NativeEngine& engine, const sptr& session) { NativeValue* objValue = engine.CreateObject(); NativeObject* object = ConvertNativeValueTo(objValue); WLOGFI("[NAPI]CreateJsSceneSession"); - - object->SetNativePointer(new std::weak_ptr(jsSceneSession), JsSceneSession::Finalizer, nullptr); + std::unique_ptr jsSceneSession = std::make_unique(&engine, session); + object->SetNativePointer(jsSceneSession.release(), JsSceneSession::Finalizer, nullptr); object->SetProperty("abilityInfo", CreateJsAbilityInfo(engine, session)); object->SetProperty("persistentId", CreateJsValue(engine, session->GetPersistentId())); BindFunctions(engine, object, "JsSceneSession"); @@ -47,7 +46,7 @@ NativeValue* CreateJsSceneSessionObject(NativeEngine& engine, const sptr*>(data); + std::unique_ptr(static_cast(data)); } void BindFunctions(NativeEngine& engine, NativeObject* object, const char *moduleName) @@ -79,39 +78,60 @@ NativeValue* JsSceneSession::OnRegisterCallback(NativeEngine& engine, NativeCall } NativeValue* value = info.argv[1]; if (!value->IsCallable()) { - WLOGFI("[NAPI]Callback(info->argv[1]) is not callable"); + 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(); + } + if (session_ == nullptr) { + WLOGFE("[NAPI]session_ is nullptr"); + engine.Throw(CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_INVALID_PARAM), + "Input parameter is missing or invalid")); + return engine.CreateUndefined(); + } + + std::weak_ptr sessionWptr(shared_from_this()); + NotifyStartSceneFunc func = [sessionWptr](const sptr& session) { + auto jsSceneSession = sessionWptr.lock(); + if (jsSceneSession == nullptr || session == nullptr) { + WLOGFE("[NAPI]this scene session or target session is nullptr"); + return; + } + jsSceneSession->StartScene(session); + }; + session_->RegisterStartSceneEventListener(func); std::shared_ptr callbackRef; callbackRef.reset(engine.CreateReference(value, 1)); - jsCbMap_.insert(std::make_pair(cbType, callbackRef)); + jsCbMap_[cbType] = callbackRef; WLOGFI("[NAPI]Register end, type = %{public}s, callback = %{public}p", cbType.c_str(), value); return engine.CreateUndefined(); } void JsSceneSession::StartScene(const sptr& session) { - if (jsCbMap_.find(START_SCENE_CB) == jsCbMap_.end()) { + auto iter = jsCbMap_.find(START_SCENE_CB); + if (iter == jsCbMap_.end()) { return; } - jsCallBack_ = jsCbMap_[START_SCENE_CB]; + jsCallBack_ = iter->second; WLOGFI("[NAPI]start scene: name = %{public}s, id = %{public}u", session->GetAbilityInfo().abilityName_.c_str(), session->GetPersistentId()); std::weak_ptr sessionWptr(shared_from_this()); auto complete = std::make_unique ( [sessionWptr, session, eng = engine_](NativeEngine& engine, AsyncTask& task, int32_t status) { auto jsSceneSession = sessionWptr.lock(); - if (jsSceneSession == nullptr || eng == nullptr || session) { + if (jsSceneSession == nullptr || eng == nullptr || session == nullptr) { WLOGFE("[NAPI]this scene session or target session or engine is nullptr"); return; } - auto jsSceneSessionRef = session->GetJsSceneSessionRef(); - if (jsSceneSessionRef == nullptr) { + NativeValue* jsSceneSessionObj = CreateJsSceneSessionObject(*eng, session); + if (jsSceneSessionObj == nullptr) { WLOGFE("[NAPI]this target session reference is nullptr"); } - NativeValue* argv[] = { jsSceneSessionRef->Get() }; + NativeValue* argv[] = { jsSceneSessionObj }; jsSceneSession->CallJsMethod(START_SCENE_CB.c_str(), argv, ArraySize(argv)); } ); @@ -136,4 +156,25 @@ void JsSceneSession::CallJsMethod(const char* methodName, NativeValue* const* ar } engine_->CallFunction(engine_->CreateUndefined(), method, argv, argc); } + +bool JsSceneSession::IsCallbackRegistered(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; +} + +sptr JsSceneSession::GetNativeSession() const +{ + return session_; +} } diff --git a/window_scene/interfaces/kits/napi/host/js_scene_session.h b/window_scene/interfaces/kits/napi/host/js_scene_session.h index f9f91a4570..0cd7ce49d9 100644 --- a/window_scene/interfaces/kits/napi/host/js_scene_session.h +++ b/window_scene/interfaces/kits/napi/host/js_scene_session.h @@ -16,6 +16,7 @@ #ifndef OHOS_JS_SCENE_SESSION_H #define OHOS_JS_SCENE_SESSION_H +#include #include #include #include @@ -27,25 +28,27 @@ namespace OHOS::Rosen { class SceneSession; class JsSceneSession final : public std::enable_shared_from_this { public: - explicit JsSceneSession(NativeEngine* engine) : engine_(engine) {} + explicit JsSceneSession(NativeEngine* engine, sptr session) : engine_(engine), session_(session) {} ~JsSceneSession() = default; static void Finalizer(NativeEngine* engine, void* data, void* hint); static NativeValue* RegisterCallback(NativeEngine* engine, NativeCallbackInfo* info); void StartScene(const sptr& session); + sptr GetNativeSession() const; private: NativeValue* OnRegisterCallback(NativeEngine& engine, NativeCallbackInfo& info); void CallJsMethod(const char* methodName, NativeValue* const* argv, size_t argc); + bool IsCallbackRegistered(std::string type, NativeValue* jsListenerObject); - std::map> jsCbMap_; NativeEngine* engine_ = nullptr; + sptr session_ = nullptr; + std::map> jsCbMap_; std::shared_ptr jsCallBack_ = nullptr; }; -NativeValue* CreateJsSceneSessionObject(NativeEngine& engine, const sptr& session, - const std::shared_ptr& jsSceneSession); +NativeValue* CreateJsSceneSessionObject(NativeEngine& engine, const sptr& session); void BindFunctions(NativeEngine& engine, NativeObject* object, const char *moduleName); } #endif // OHOS_JS_SCENE_SESSION_H diff --git a/window_scene/interfaces/kits/napi/host/js_scene_session_manager.cpp b/window_scene/interfaces/kits/napi/host/js_scene_session_manager.cpp index 1fc30b39aa..9668670251 100644 --- a/window_scene/interfaces/kits/napi/host/js_scene_session_manager.cpp +++ b/window_scene/interfaces/kits/napi/host/js_scene_session_manager.cpp @@ -18,6 +18,7 @@ #include #include +#include "js_scene_session.h" #include "window_scene_hilog.h" #include "scene_session_manager.h" #include "js_scene_utils.h" @@ -132,23 +133,23 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSession(NativeEngine& engine, } WLOGFI("[NAPI]Ability name = %{public}s, option = %{public}u, err = %{public}d", abilityInfo.abilityName_.c_str(), static_cast(sessionOption), errCode); - sptr sceneSession = SceneSessionManager::GetInstance().RequestSceneSession(abilityInfo, - sessionOption, engine); + sptr sceneSession = SceneSessionManager::GetInstance().RequestSceneSession(abilityInfo, sessionOption); if (sceneSession == nullptr) { engine.Throw(CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "System is abnormal")); return engine.CreateUndefined(); } else { - auto jsSceneSessionRef = sceneSession->GetJsSceneSessionRef(); - if (jsSceneSessionRef == nullptr) { - WLOGFE("[NAPI]Ability name = %{public}s, jsSceneSessionRef is nullptr", abilityInfo.abilityName_.c_str()); + NativeValue* jsSceneSessionObj = CreateJsSceneSessionObject(engine, sceneSession); + if (jsSceneSessionObj == nullptr) { + WLOGFE("[NAPI]Ability name = %{public}s, jsSceneSessionObj is nullptr", abilityInfo.abilityName_.c_str()); engine.Throw(CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "System is abnormal")); } - return jsSceneSessionRef->Get(); + return jsSceneSessionObj; } } + NativeValue* JsSceneSessionManager::OnRequestSceneSessionActivation(NativeEngine& engine, NativeCallbackInfo& info) { WLOGFI("[NAPI]OnRequestSceneSessionActivation"); @@ -158,15 +159,15 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionActivation(NativeEngine errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; } - sptr sceneSession = nullptr; + SceneSession* sceneSession = nullptr; if (errCode == WSErrorCode::WS_OK) { // find scene session - NativeValue* jsSceneSessionObj = info.argv[0]; + auto jsSceneSessionObj = ConvertNativeValueTo(info.argv[0]); if (jsSceneSessionObj == nullptr) { - WLOGFE("[NAPI]Failed to get js scene session value"); + WLOGFE("[NAPI]Failed to get js scene session object"); errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; } else { - sceneSession = SceneSessionManager::GetInstance().FindSceneSession(jsSceneSessionObj); + sceneSession = static_cast(jsSceneSessionObj->GetNativePointer()); if (sceneSession == nullptr) { WLOGFE("[NAPI]Failed to find scene session From Js Object"); errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; @@ -182,18 +183,16 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionActivation(NativeEngine WLOGFI("[NAPI]RequestSceneSessionActivation: ability name = %{public}s", sceneSession->GetAbilityInfo().abilityName_.c_str()); - wptr sceneSessionToken(sceneSession); AsyncTask::CompleteCallback complete = - [sceneSessionToken](NativeEngine& engine, AsyncTask& task, int32_t status) { - auto weakSceneSession = sceneSessionToken.promote(); - if (weakSceneSession == nullptr) { + [sceneSession](NativeEngine& engine, AsyncTask& task, int32_t status) { + if (sceneSession == nullptr) { task.Reject(engine, CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "Invalidate params.")); return; } WSErrorCode ret = WS_JS_TO_ERROR_CODE_MAP.at( - SceneSessionManager::GetInstance().RequestSceneSessionActivation(weakSceneSession)); + SceneSessionManager::GetInstance().RequestSceneSessionActivation(sceneSession)); if (ret == WSErrorCode::WS_OK) { task.Resolve(engine, engine.CreateUndefined()); } else { @@ -201,7 +200,7 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionActivation(NativeEngine "Request scene session activation failed")); } WLOGFI("[NAPI]Session [%{public}s] request scene session activation end", - weakSceneSession->GetAbilityInfo().abilityName_.c_str()); + sceneSession->GetAbilityInfo().abilityName_.c_str()); }; NativeValue* lastParam = (info.argc <= 1) ? nullptr : @@ -222,15 +221,15 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionBackground(NativeEngine errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; } - sptr sceneSession = nullptr; + SceneSession* sceneSession = nullptr; if (errCode == WSErrorCode::WS_OK) { // find scene session - NativeValue* jsSceneSessionObj = info.argv[0]; + auto jsSceneSessionObj = ConvertNativeValueTo(info.argv[0]); if (jsSceneSessionObj == nullptr) { - WLOGFE("[NAPI]Failed to get js scene session value"); + WLOGFE("[NAPI]Failed to get js scene session object"); errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; } else { - sceneSession = SceneSessionManager::GetInstance().FindSceneSession(jsSceneSessionObj); + sceneSession = static_cast(jsSceneSessionObj->GetNativePointer()); if (sceneSession == nullptr) { WLOGFE("[NAPI]Failed to find scene session From Js Object"); errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; @@ -246,18 +245,16 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionBackground(NativeEngine WLOGFI("[NAPI]RequestSceneSessionBackground: ability name = %{public}s", sceneSession->GetAbilityInfo().abilityName_.c_str()); - wptr sceneSessionToken(sceneSession); AsyncTask::CompleteCallback complete = - [sceneSessionToken](NativeEngine& engine, AsyncTask& task, int32_t status) { - auto weakSceneSession = sceneSessionToken.promote(); - if (weakSceneSession == nullptr) { + [sceneSession](NativeEngine& engine, AsyncTask& task, int32_t status) { + if (sceneSession == nullptr) { task.Reject(engine, CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "Invalidate params.")); return; } WSErrorCode ret = WS_JS_TO_ERROR_CODE_MAP.at( - SceneSessionManager::GetInstance().RequestSceneSessionBackground(weakSceneSession)); + SceneSessionManager::GetInstance().RequestSceneSessionBackground(sceneSession)); if (ret == WSErrorCode::WS_OK) { task.Resolve(engine, engine.CreateUndefined()); } else { @@ -265,7 +262,7 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionBackground(NativeEngine "Request scene session background failed")); } WLOGFI("[NAPI]Session [%{public}s] request scene session background end", - weakSceneSession->GetAbilityInfo().abilityName_.c_str()); + sceneSession->GetAbilityInfo().abilityName_.c_str()); }; NativeValue* lastParam = (info.argc <= 1) ? nullptr : @@ -286,15 +283,15 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionDestruction(NativeEngin errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; } - sptr sceneSession = nullptr; + SceneSession* sceneSession = nullptr; if (errCode == WSErrorCode::WS_OK) { // find scene session - NativeValue* jsSceneSessionObj = info.argv[0]; + auto jsSceneSessionObj = ConvertNativeValueTo(info.argv[0]); if (jsSceneSessionObj == nullptr) { - WLOGFE("[NAPI]Failed to get js scene session value"); + WLOGFE("[NAPI]Failed to get js scene session object"); errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; } else { - sceneSession = SceneSessionManager::GetInstance().FindSceneSession(jsSceneSessionObj); + sceneSession = static_cast(jsSceneSessionObj->GetNativePointer()); if (sceneSession == nullptr) { WLOGFE("[NAPI]Failed to find scene session From Js Object"); errCode = WSErrorCode::WS_ERROR_INVALID_PARAM; @@ -310,18 +307,16 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionDestruction(NativeEngin WLOGFI("[NAPI]OnRequestSceneSessionDestruction: ability name = %{public}s", sceneSession->GetAbilityInfo().abilityName_.c_str()); - wptr sceneSessionToken(sceneSession); AsyncTask::CompleteCallback complete = - [sceneSessionToken](NativeEngine& engine, AsyncTask& task, int32_t status) { - auto weakSceneSession = sceneSessionToken.promote(); - if (weakSceneSession == nullptr) { + [sceneSession](NativeEngine& engine, AsyncTask& task, int32_t status) { + if (sceneSession == nullptr) { task.Reject(engine, CreateJsError(engine, static_cast(WSErrorCode::WS_ERROR_STATE_ABNORMALLY), "Invalidate params.")); return; } WSErrorCode ret = WS_JS_TO_ERROR_CODE_MAP.at( - SceneSessionManager::GetInstance().RequestSceneSessionBackground(weakSceneSession)); + SceneSessionManager::GetInstance().RequestSceneSessionBackground(sceneSession)); if (ret == WSErrorCode::WS_OK) { task.Resolve(engine, engine.CreateUndefined()); } else { @@ -329,7 +324,7 @@ NativeValue* JsSceneSessionManager::OnRequestSceneSessionDestruction(NativeEngin "Request scene session destruction failed")); } WLOGFI("[NAPI]Session [%{public}s] request scene session destruction end", - weakSceneSession->GetAbilityInfo().abilityName_.c_str()); + sceneSession->GetAbilityInfo().abilityName_.c_str()); }; NativeValue* lastParam = (info.argc <= 1) ? nullptr : diff --git a/window_scene/interfaces/kits/napi/host/js_scene_utils.cpp b/window_scene/interfaces/kits/napi/host/js_scene_utils.cpp index 02b122c61c..90c8a65268 100644 --- a/window_scene/interfaces/kits/napi/host/js_scene_utils.cpp +++ b/window_scene/interfaces/kits/napi/host/js_scene_utils.cpp @@ -16,6 +16,7 @@ #include "js_scene_utils.h" #include + #include "window_scene_hilog.h" #include "window_scene_common.h" diff --git a/window_scene/interfaces/kits/napi/host/js_scene_utils.h b/window_scene/interfaces/kits/napi/host/js_scene_utils.h index b670c32dc6..5fea665d39 100644 --- a/window_scene/interfaces/kits/napi/host/js_scene_utils.h +++ b/window_scene/interfaces/kits/napi/host/js_scene_utils.h @@ -19,8 +19,9 @@ #include #include #include -#include "scene_session.h" +#include "scene_session.h" +#include "window_scene_common.h" namespace OHOS::Rosen { bool GetAbilityInfoFromJs(NativeEngine& engine, NativeObject* jsObject, AbilityInfo& abilityInfo); diff --git a/window_scene/session/container/include/window_event_channel.h b/window_scene/session/container/include/window_event_channel.h index e40e3d93ce..1115bc7d56 100644 --- a/window_scene/session/container/include/window_event_channel.h +++ b/window_scene/session/container/include/window_event_channel.h @@ -17,6 +17,7 @@ #define OHOS_WINDOW_EVENT_CHANNEL_H #include "zidl/window_event_channel_stub.h" + #include "window_scene_common.h" namespace OHOS::Rosen { diff --git a/window_scene/session/container/include/zidl/scene_session_stage_proxy.h b/window_scene/session/container/include/zidl/scene_session_stage_proxy.h index b5ea83f15c..9303a740c9 100644 --- a/window_scene/session/container/include/zidl/scene_session_stage_proxy.h +++ b/window_scene/session/container/include/zidl/scene_session_stage_proxy.h @@ -18,7 +18,7 @@ #include -#include "zidl/scene_session_stage_interface.h" +#include "scene_session_stage_interface.h" #include "window_scene_common.h" namespace OHOS::Rosen { diff --git a/window_scene/session/container/include/zidl/scene_session_stage_stub.h b/window_scene/session/container/include/zidl/scene_session_stage_stub.h index 84e525d27e..3dcf282bd9 100644 --- a/window_scene/session/container/include/zidl/scene_session_stage_stub.h +++ b/window_scene/session/container/include/zidl/scene_session_stage_stub.h @@ -18,7 +18,7 @@ #include #include -#include "zidl/scene_session_stage_interface.h" +#include "scene_session_stage_interface.h" namespace OHOS::Rosen { class SceneSessionStageStub; diff --git a/window_scene/session/container/include/zidl/window_event_channel_proxy.h b/window_scene/session/container/include/zidl/window_event_channel_proxy.h index bc2e2c5d56..e4ced590ad 100644 --- a/window_scene/session/container/include/zidl/window_event_channel_proxy.h +++ b/window_scene/session/container/include/zidl/window_event_channel_proxy.h @@ -18,7 +18,7 @@ #include -#include "zidl/window_event_channel_interface.h" +#include "window_event_channel_interface.h" #include "window_scene_common.h" namespace OHOS::Rosen { diff --git a/window_scene/session/container/include/zidl/window_event_channel_stub.h b/window_scene/session/container/include/zidl/window_event_channel_stub.h index 0aef0def9a..6dd03b5f51 100644 --- a/window_scene/session/container/include/zidl/window_event_channel_stub.h +++ b/window_scene/session/container/include/zidl/window_event_channel_stub.h @@ -18,7 +18,7 @@ #include #include -#include "zidl/window_event_channel_interface.h" +#include "window_event_channel_interface.h" namespace OHOS::Rosen { class WindowEventChannelStub; diff --git a/window_scene/session/container/src/window_event_channel.cpp b/window_scene/session/container/src/window_event_channel.cpp index be66567cf4..afb3974c4d 100644 --- a/window_scene/session/container/src/window_event_channel.cpp +++ b/window_scene/session/container/src/window_event_channel.cpp @@ -12,3 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#include "window_event_channel.h" + +#include +#include +#include diff --git a/window_scene/session/container/src/zidl/window_event_channel_proxy.cpp b/window_scene/session/container/src/zidl/window_event_channel_proxy.cpp index 71f7ea1427..1f740aacdd 100644 --- a/window_scene/session/container/src/zidl/window_event_channel_proxy.cpp +++ b/window_scene/session/container/src/zidl/window_event_channel_proxy.cpp @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include #include "window_scene_hilog.h" namespace OHOS::Rosen { diff --git a/window_scene/session/container/src/zidl/window_event_channel_stub.cpp b/window_scene/session/container/src/zidl/window_event_channel_stub.cpp index 7744265710..46a6c45d2b 100644 --- a/window_scene/session/container/src/zidl/window_event_channel_stub.cpp +++ b/window_scene/session/container/src/zidl/window_event_channel_stub.cpp @@ -15,6 +15,9 @@ #include "zidl/window_event_channel_stub.h" #include +#include +#include +#include #include "window_scene_hilog.h" namespace OHOS::Rosen { diff --git a/window_scene/session/host/include/scene_session.h b/window_scene/session/host/include/scene_session.h index efaa242546..e7417b83a8 100644 --- a/window_scene/session/host/include/scene_session.h +++ b/window_scene/session/host/include/scene_session.h @@ -17,18 +17,20 @@ #define OHOS_SCENE_SESSION_H #include "zidl/scene_session_stub.h" -#include "session.h" -#include "js_scene_session.h" -#include "zidl/scene_session_stage_interface.h" +#include "foundation/window/window_manager/window_scene/interfaces/innerkits/session.h" +#include "scene_session_stage_interface.h" #include "window_scene_common.h" namespace OHOS::Rosen { -class SceneSession : public SceneSessionStub, public Session{ +class SceneSession; +using NotifyStartSceneFunc = std::function& session)>; +class SceneSession : public SceneSessionStub, public Session { public: - explicit SceneSession(const AbilityInfo& info, NativeEngine& engine); + explicit SceneSession(const AbilityInfo& info); ~SceneSession() = default; - virtual WSError Connect(const sptr& sessionStage, const sptr& eventChannel) override; + virtual WSError Connect(const sptr& sessionStage, + const sptr& eventChannel) override; virtual WSError Foreground() override; virtual WSError Background() override; virtual WSError Disconnect() override; @@ -41,18 +43,14 @@ public: virtual WSError SetActive(bool active) override; const AbilityInfo& GetAbilityInfo() const; - std::shared_ptr GetJsSceneSessionRef() const; - std::shared_ptr GetJsSceneSession() const; + void RegisterStartSceneEventListener(const NotifyStartSceneFunc& func); private: sptr sceneSessionStage_; sptr windowEventChannel_; AbilityInfo abilityInfo_; std::string sessionType_ = ""; bool isActive_ = false; - - NativeEngine& engine_; - std::shared_ptr jsSceneSessionRef_; - std::shared_ptr jsSceneSession_; + NotifyStartSceneFunc startSceneFunc_; }; } #endif // OHOS_SCENE_SESSION_H \ No newline at end of file diff --git a/window_scene/session/host/include/scene_session_manager.h b/window_scene/session/host/include/scene_session_manager.h index ab446b9ccb..f29d4f8fbc 100644 --- a/window_scene/session/host/include/scene_session_manager.h +++ b/window_scene/session/host/include/scene_session_manager.h @@ -21,7 +21,6 @@ #include #include -#include #include "ws_single_instance.h" #include "window_scene_common.h" #include "scene_session.h" @@ -30,13 +29,11 @@ namespace OHOS::Rosen { class SceneSessionManager { WS_DECLARE_SINGLE_INSTANCE(SceneSessionManager) public: - sptr RequestSceneSession(const AbilityInfo& abilityInfo, SessionOption option, NativeEngine& engine); + sptr RequestSceneSession(const AbilityInfo& abilityInfo, SessionOption option); WSError RequestSceneSessionActivation(const sptr& sceneSession); WSError RequestSceneSessionBackground(const sptr& sceneSession); WSError RequestSceneSessionDestruction(const sptr& sceneSession); - sptr FindSceneSession(NativeValue* jsSceneSessionObj); - private: uint32_t GenSessionId(); @@ -45,8 +42,6 @@ private: std::vector> sessions_; int pid_ = getpid(); std::atomic sessionId_ = INVALID_SESSION_ID; - - std::map, sptr> jsSceneSessionMap_; }; } #endif // OHOS_ROSEN_SCENE_SESSION_MANAGER_H diff --git a/window_scene/session/host/src/scene_session.cpp b/window_scene/session/host/src/scene_session.cpp index 14a1c08f6d..60569dad59 100644 --- a/window_scene/session/host/src/scene_session.cpp +++ b/window_scene/session/host/src/scene_session.cpp @@ -22,24 +22,14 @@ namespace OHOS::Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SceneSession"}; } -SceneSession::SceneSession(const AbilityInfo& info, NativeEngine& engine) : Session(info.bundleName_), - abilityInfo_(info), engine_(engine) + +SceneSession::SceneSession(const AbilityInfo& info) : Session(info.bundleName_), abilityInfo_(info) { - std::shared_ptr jsSceneSession = std::make_shared(&engine); - sptr sceneSession(this); - NativeValue* jsSceneSessionObj = CreateJsSceneSessionObject(engine_, sceneSession, jsSceneSession); - jsSceneSessionRef_ = std::unique_ptr(engine_.CreateReference(jsSceneSessionObj, 1)); - jsSceneSession_ = jsSceneSession; } -std::shared_ptr SceneSession::GetJsSceneSessionRef() const +void SceneSession::RegisterStartSceneEventListener(const NotifyStartSceneFunc& func) { - return jsSceneSessionRef_; -} - -std::shared_ptr SceneSession::GetJsSceneSession() const -{ - return jsSceneSession_; + startSceneFunc_ = func; } WSError SceneSession::Connect(const sptr& sessionStage, const sptr& eventChannel) @@ -157,13 +147,13 @@ WSError SceneSession::Maximum() WSError SceneSession::StartScene(const AbilityInfo& info, SessionOption sessionOption) { - auto sceneSession = SceneSessionManager::GetInstance().RequestSceneSession(info, sessionOption, engine_); + auto sceneSession = SceneSessionManager::GetInstance().RequestSceneSession(info, sessionOption); if (sceneSession == nullptr) { return WSError::WS_ERROR_NULLPTR; } - auto jsSceneSession = sceneSession->GetJsSceneSession(); - // TODO: native->ts startScene - jsSceneSession->StartScene(sceneSession); + if (startSceneFunc_) { + startSceneFunc_(sceneSession); + } return WSError::WS_OK; } diff --git a/window_scene/session/host/src/scene_session_manager.cpp b/window_scene/session/host/src/scene_session_manager.cpp index cde3eb278a..164fede9d0 100644 --- a/window_scene/session/host/src/scene_session_manager.cpp +++ b/window_scene/session/host/src/scene_session_manager.cpp @@ -15,9 +15,6 @@ #include "scene_session_manager.h" -#include -#include - #include "window_scene_hilog.h" namespace OHOS::Rosen { @@ -27,29 +24,15 @@ namespace { WS_IMPLEMENT_SINGLE_INSTANCE(SceneSessionManager) -sptr SceneSessionManager::FindSceneSession(NativeValue* jsSceneSessionObj) -{ - std::lock_guard lock(mutex_); - for (auto iter = jsSceneSessionMap_.begin(); iter != jsSceneSessionMap_.end(); iter++) { - if (jsSceneSessionObj->StrictEquals(iter->first->Get())) { - return iter->second; - } - } - WLOGFE("Cannot find FindSceneSession!"); - return nullptr; -} - -sptr SceneSessionManager::RequestSceneSession(const AbilityInfo& abilityInfo, SessionOption option, - NativeEngine& engine) +sptr SceneSessionManager::RequestSceneSession(const AbilityInfo& abilityInfo, SessionOption option) { WLOGFI("abilityInfo: bundleName: %{public}s, abilityName: %{public}s", abilityInfo.bundleName_.c_str(), abilityInfo.abilityName_.c_str()); - sptr session = new SceneSession(abilityInfo, engine); + sptr session = new SceneSession(abilityInfo); uint32_t persistentId = pid_ + GenSessionId(); session->SetPersistentId(persistentId); std::lock_guard lock(mutex_); sessions_.emplace_back(session); - jsSceneSessionMap_.insert(std::make_pair(session->GetJsSceneSessionRef(), session)); return session; } @@ -87,15 +70,6 @@ WSError SceneSessionManager::RequestSceneSessionDestruction(const sptr, sptr>::value_type& pair) { - return sceneSession == pair.second; - }); - if (jsSceneSessionIter == jsSceneSessionMap_.end()) { - WLOGFI("cannot find jsSceneSession"); - return WSError::WS_DO_NOTHING; - } - jsSceneSessionMap_.erase(jsSceneSessionIter); return WSError::WS_OK; } diff --git a/window_scene/session/host/src/session.cpp b/window_scene/session/host/src/session.cpp index 3f2687e620..e902bace64 100644 --- a/window_scene/session/host/src/session.cpp +++ b/window_scene/session/host/src/session.cpp @@ -15,6 +15,8 @@ #include "session.h" +#include + #include "window_scene_hilog.h" namespace OHOS::Rosen {