add set && get sessionManagerService

Signed-off-by: yaodixiong <yaodixiong@huawei.com>
Change-Id: If8c4f77f0957fd965642cdda03bf65a8d42d2f37
This commit is contained in:
yaodixiong 2023-06-25 15:45:33 +08:00
parent 3c4f1c22d0
commit 519b065735
8 changed files with 59 additions and 73 deletions

View File

@ -137,3 +137,31 @@ ohos_executable("demo_water_mark_listener") {
part_name = "window_manager"
subsystem_name = "window"
}
ohos_executable("demo_session_manager") {
sources = [ "demo_session_manager.cpp" ]
include_dirs = [
"${window_base_path}/interfaces/innerkits/wm",
"${window_base_path}/interfaces/innerkits/dm",
"${window_base_path}/test/common/utils/include",
"${window_base_path}/window_scene/session_manager/include",
"${window_base_path}/window_scene/session_manager_service/include",
"${window_base_path}/wm/include",
]
deps = [
"${graphic_base_path}/graphic_2d/rosen/modules/render_service_base:librender_service_base",
"${window_base_path}/dm:libdm",
"${window_base_path}/window_scene/session_manager:session_manager",
"${window_base_path}/wm:libwm",
]
external_deps = [
"c_utils:utils",
"ipc:ipc_core",
]
part_name = "window_manager"
subsystem_name = "window"
}

View File

@ -25,8 +25,6 @@ using namespace OHOS::Rosen;
int main(int argc, char *argv[])
{
SessionManager sessionManager;
sessionManager.Init();
sleep(1);
auto remoteObject = sessionManager.GetRemoteObject();
if (!remoteObject) {

View File

@ -46,6 +46,12 @@ public:
return (me != nullptr) ? me->OnGetRemoteObject(*engine, *info) : nullptr;
}
static NativeValue* InitSessionManagerService(NativeEngine* engine, NativeCallbackInfo* info)
{
JsSessionManagerService* me = CheckParamsAndGetThis<JsSessionManagerService>(engine, info);
return (me != nullptr) ? me->OnInitSessionManagerService(*engine, *info) : nullptr;
}
private:
NativeValue* OnGetRemoteObject(NativeEngine& engine, NativeCallbackInfo& info)
{
@ -57,6 +63,17 @@ private:
napi_value value = NAPI_ohos_rpc_CreateJsRemoteObject(env, remoteObject);
return reinterpret_cast<NativeValue*>(value);
}
NativeValue* OnInitSessionManagerService(NativeEngine& engine, NativeCallbackInfo& info)
{
WLOGI("JsSessionManagerService: OnInitSessionManagerService is called");
auto& sessionManagerService = SingletonContainer::Get<SessionManagerService>();
sessionManagerService.Init();
napi_env env = reinterpret_cast<napi_env>(&engine);
napi_value value = NAPI_ohos_rpc_CreateJsRemoteObject(env, nullptr);
return reinterpret_cast<NativeValue*>(value);
}
};
NativeValue* JsSessionManagerServiceInit(NativeEngine* engine, NativeValue* exportObj)
@ -80,6 +97,8 @@ NativeValue* JsSessionManagerServiceInit(NativeEngine* engine, NativeValue* expo
// bind function
const char* moduleName = "JsSessionManagerService";
BindNativeFunction(*engine, *object, "getRemoteObject", moduleName, JsSessionManagerService::GetRemoteObject);
BindNativeFunction(*engine, *object, "initSessionManagerService", moduleName,
JsSessionManagerService::InitSessionManagerService);
return engine->CreateUndefined();
}
} // namespace OHOS::Rosen

View File

@ -27,7 +27,6 @@
namespace OHOS::Rosen {
using namespace ScreenLock;
class AbilityConnection;
class SessionManager {
WM_DECLARE_SINGLE_INSTANCE_BASE(SessionManager);
public:
@ -57,8 +56,6 @@ private:
void InitScreenSessionManagerProxy();
void InitScreenLockManagerProxy();
bool serviceConnected_ = false;
sptr<AbilityConnection> abilityConnection_;
sptr<IRemoteObject> remoteObject_ = nullptr;
sptr<ISessionManagerService> sessionManagerServiceProxy_ = nullptr;
sptr<ISceneSessionManager> sceneSessionManagerProxy_ = nullptr;

View File

@ -29,46 +29,10 @@
namespace OHOS::Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SessionManager"};
const int CONNECT_COUNTS = 10;
const int SLEEP_10MS = 10000;
}
WM_IMPLEMENT_SINGLE_INSTANCE(SessionManager)
class AbilityConnection : public AAFwk::AbilityConnectionStub {
public:
void OnAbilityConnectDone(
const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode) override
{
remoteObject_ = remoteObject;
if (remoteObject_ == nullptr) {
WLOGFW("RemoteObject_ is nullptr");
}
cv_.notify_all();
}
void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) override
{
remoteObject_ = nullptr;
}
sptr<IRemoteObject> GetRemoteObject()
{
if (!remoteObject_) {
std::unique_lock<std::mutex> lock(mutex_);
if (cv_.wait_for(lock, std::chrono::seconds(1)) == std::cv_status::timeout) {
WLOGFW("Get remote object timeout.");
}
}
return remoteObject_;
}
private:
sptr<IRemoteObject> remoteObject_;
std::mutex mutex_;
std::condition_variable cv_;
};
SessionManager::SessionManager()
{
}
@ -79,23 +43,17 @@ SessionManager::~SessionManager()
void SessionManager::Init()
{
if (!serviceConnected_) {
ConnectToService();
}
GetRemoteObject();
}
sptr<IRemoteObject> SessionManager::GetRemoteObject()
{
if (remoteObject_) {
WLOGFE("Get session manager service remote object ok");
return remoteObject_;
}
if (abilityConnection_) {
remoteObject_ = abilityConnection_->GetRemoteObject();
} else {
Init();
}
remoteObject_ = AAFwk::AbilityManagerClient::GetInstance()->GetSessionManagerService();
if (!remoteObject_) {
WLOGFE("Get session manager service remote object nullptr");
}
@ -128,29 +86,6 @@ sptr<ISceneSessionManager> SessionManager::GetSceneSessionManagerProxy()
return sceneSessionManagerProxy_;
}
void SessionManager::ConnectToService()
{
if (!abilityConnection_) {
abilityConnection_ = new(std::nothrow) AbilityConnection();
}
AAFwk::Want want;
want.SetElementName("com.ohos.sceneboard", "com.ohos.sceneboard.MainAbility");
ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, abilityConnection_, nullptr);
for (uint8_t count = 0; count < CONNECT_COUNTS; count++) {
if (ret == ERR_OK) {
break;
} else {
WLOGFE("ConnectToService failed, errorcode: %{public}d", ret);
ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, abilityConnection_, nullptr);
}
usleep(SLEEP_10MS);
}
serviceConnected_ = (ret == ERR_OK);
}
void SessionManager::InitSessionManagerServiceProxy()
{
if (sessionManagerServiceProxy_) {

View File

@ -36,6 +36,7 @@ ohos_shared_library("session_manager_service") {
]
external_deps = [
"ability_runtime:ability_manager",
"graphic_standard:librender_service_client",
"hilog_native:libhilog",
"ipc:ipc_single",

View File

@ -30,6 +30,7 @@ public:
sptr<IRemoteObject> GetSceneSessionManager() override;
sptr<IRemoteObject> GetScreenSessionManagerService() override;
sptr<IRemoteObject> GetScreenLockManagerService() override;
void Init();
private:
sptr<IRemoteObject> sessionManagerServiceObj_;

View File

@ -20,6 +20,8 @@
#include "session_manager/include/scene_session_manager.h"
#include "session_manager/include/screen_session_manager.h"
#include "ability_manager_client.h"
namespace OHOS::Rosen {
WM_IMPLEMENT_SINGLE_INSTANCE(SessionManagerService)
@ -62,4 +64,9 @@ sptr<IRemoteObject> SessionManagerService::GetScreenSessionManagerService()
screenSessionManagerObj_ = ScreenSessionManager::GetInstance().AsObject();
return screenSessionManagerObj_;
}
void SessionManagerService::Init()
{
AAFwk::AbilityManagerClient::GetInstance()->SetSessionManagerService(this->AsObject());
}
} // namesapce OHOS::Rosen