add implment for session

Signed-off-by: chyyy0213 <chenhaiying3@huawei.com>
Change-Id: I2a96a9b28b3fc40fa7cf02067d9313db39155282
Signed-off-by: chyyy0213 <chenhaiying3@huawei.com>
This commit is contained in:
chyyy0213 2023-02-28 18:42:15 +08:00
parent 60f1ef4aa2
commit 9300f5211a
24 changed files with 897 additions and 83 deletions

View File

@ -74,7 +74,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/common:window_scene_common"
],
"service_group": [
"//foundation/window/window_manager/sa_profile:wms_sa_profile",

View File

@ -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.
import("//build/ohos.gni")
import("//foundation/window/window_manager/windowmanager_aafwk.gni")
config("libwscommon_public_config") {
include_dirs = [
"${window_base_path}/window_scene",
# for window_manager_hilog
"${window_base_path}/utils/include",
]
}
## Build libwindow_scene_common.so
ohos_shared_library("window_scene_common") {
sources = [ "src/message_scheduler.cpp" ]
public_configs = [ ":libwscommon_public_config" ]
external_deps = [
"eventhandler:libeventhandler",
"hilog_native:libhilog",
]
part_name = "window_manager"
subsystem_name = "window"
}

View File

@ -0,0 +1,62 @@
/*
* 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_MESSAGE_SCHEDULER_H
#define OHOS_ROSEN_WINDOW_SCENE_MESSAGE_SCHEDULER_H
#include "event_handler.h"
#include "window_manager_hilog.h"
namespace OHOS::Rosen {
#ifndef WS_CHECK_NULL_SCHE_VOID
#define WS_CHECK_NULL_SCHE_VOID(ptr, task) \
do { \
if (!(ptr)) { \
task(); \
return; \
} \
} while (0)
#endif
#ifndef WS_CHECK_NULL_SCHE_RETURN
#define WS_CHECK_NULL_SCHE_RETURN(ptr, task) \
do { \
if (!(ptr)) { \
return task(); \
} \
} while (0)
#endif
class MessageScheduler {
public:
using Task = std::function<void()>;
MessageScheduler(const std::string& threadName);
MessageScheduler(const std::shared_ptr<AppExecFwk::EventHandler>& handler) : handler_(handler) {};
~MessageScheduler();
void PostVoidSyncTask(Task task);
void PostAsyncTask(Task task, int64_t delayTime = 0);
template<typename SyncTask, typename Return = std::invoke_result_t<SyncTask>>
Return PostSyncTask(SyncTask&& task)
{
Return ret;
std::function<void()> syncTask([&ret, &task]() {ret = task();});
if (handler_) {
handler_->PostSyncTask(syncTask, AppExecFwk::EventQueue::Priority::IMMEDIATE);
}
return ret;
}
private:
std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_WINDOW_SCENE_MESSAGE_SCHEDULER_H

View File

@ -0,0 +1,56 @@
/*
* 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 "common/include/message_scheduler.h"
namespace OHOS::Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WSMessageScheduler"};
}
MessageScheduler::MessageScheduler(const std::string& threadName)
{
auto runner = AppExecFwk::EventRunner::Create(threadName);
handler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
}
MessageScheduler::~MessageScheduler()
{
handler_ = nullptr;
}
void MessageScheduler::PostVoidSyncTask(Task task)
{
if (handler_ == nullptr) {
WLOGFE("Failed to post task, handler is null!");
return;
}
bool ret = handler_->PostSyncTask(std::move(task), AppExecFwk::EventQueue::Priority::IMMEDIATE);
if (!ret) {
WLOGFE("EventHandler PostTask Failed");
}
}
void MessageScheduler::PostAsyncTask(Task task, int64_t delayTime)
{
if (handler_ == nullptr) {
WLOGFE("Failed to post task, handler is null!");
return;
}
bool ret = handler_->PostTask(std::move(task), delayTime, AppExecFwk::EventQueue::Priority::IMMEDIATE);
if (!ret) {
WLOGFE("EventHandler PostTask Failed");
}
}
} // namespace OHOS::Rosen

View File

@ -64,6 +64,7 @@ enum class SessionState : uint32_t {
STATE_ACTIVE,
STATE_INACTIVE,
STATE_BACKGROUND,
STATE_END,
};
struct SessionInfo {

View File

@ -35,10 +35,10 @@ class AxisEvent;
namespace OHOS::Rosen {
class ISessionStageStateListener {
public:
virtual void AfterForeground() = 0;
virtual void AfterBackground() = 0;
virtual void AfterActive() = 0;
virtual void AfterInactive() = 0;
virtual void AfterForeground() {};
virtual void AfterBackground() {};
virtual void AfterActive() {};
virtual void AfterInactive() {};
};
class ISizeChangeListener {
@ -57,6 +57,15 @@ public:
};
class SessionStage : public SessionStageStub, public virtual RefBase {
#define CALL_SESSION_STATE_LISTENER(sessionStateCb, listeners) \
do { \
for (auto& listener : (listeners)) { \
if (!listener.expired()) { \
listener.lock()->sessionStateCb(); \
} \
} \
} while (0)
public:
SessionStage(const sptr<ISession>& session);
virtual ~SessionStage() = default;
@ -68,7 +77,7 @@ public:
virtual WSError PendingSessionActivation(const SessionInfo& info);
// for scene session stage
virtual WSError Recover();
virtual WSError Maximum();
virtual WSError Maximize();
// IPC
WSError SetActive(bool active) override;
@ -90,9 +99,93 @@ public:
protected:
void NotifySizeChange(const WSRect& rect, SizeChangeReason reason);
inline void NotifyAfterForeground()
{
auto sessionStateListeners = GetListeners<ISessionStageStateListener>();
CALL_SESSION_STATE_LISTENER(AfterForeground, sessionStateListeners);
}
inline void NotifyAfterBackground()
{
auto sessionStateListeners = GetListeners<ISessionStageStateListener>();
CALL_SESSION_STATE_LISTENER(AfterBackground, sessionStateListeners);
}
inline void NotifyAfterActive()
{
auto sessionStateListeners = GetListeners<ISessionStageStateListener>();
CALL_SESSION_STATE_LISTENER(AfterActive, sessionStateListeners);
}
inline void NotifyAfterInactive(bool needNotifyUiContent = true)
{
auto sessionStateListeners = GetListeners<ISessionStageStateListener>();
CALL_SESSION_STATE_LISTENER(AfterInactive, sessionStateListeners);
}
sptr<ISession> session_;
private:
template<typename T>
bool RegisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
template<typename T>
bool UnregisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
template<typename T1, typename T2, typename Ret>
using EnableIfSame = typename std::enable_if<std::is_same_v<T1, T2>, Ret>::type;
template<typename T>
inline EnableIfSame<T, ISessionStageStateListener, std::vector<std::weak_ptr<ISessionStageStateListener>>>
GetListeners()
{
std::vector<std::weak_ptr<ISessionStageStateListener>> sessionStageStateListeners;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (auto& listener : sessionStageStateListeners_) {
sessionStageStateListeners.push_back(listener);
}
}
return sessionStageStateListeners;
}
template<typename T>
inline EnableIfSame<T, ISizeChangeListener, std::vector<std::weak_ptr<ISizeChangeListener>>> GetListeners()
{
std::vector<std::weak_ptr<ISizeChangeListener>> sizeChangeListeners;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (auto& listener : sizeChangeListeners_) {
sizeChangeListeners.push_back(listener);
}
}
return sizeChangeListeners;
}
template<typename T>
inline EnableIfSame<T, IPointerEventListener, std::vector<std::weak_ptr<IPointerEventListener>>> GetListeners()
{
std::vector<std::weak_ptr<IPointerEventListener>> pointerEventListeners;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (auto& listener : pointerEventListeners_) {
pointerEventListeners.push_back(listener);
}
}
return pointerEventListeners;
}
template<typename T>
inline EnableIfSame<T, IKeyEventListener, std::vector<std::weak_ptr<IKeyEventListener>>> GetListeners()
{
std::vector<std::weak_ptr<IKeyEventListener>> keyEventListeners;
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (auto& listener : keyEventListeners_) {
keyEventListeners.push_back(listener);
}
}
return keyEventListeners;
}
std::recursive_mutex mutex_;
std::vector<std::shared_ptr<IPointerEventListener>> pointerEventListeners_;
std::vector<std::shared_ptr<IKeyEventListener>> keyEventListeners_;

View File

@ -18,11 +18,21 @@
#include "session/container/include/window_event_channel.h"
#include "window_manager_hilog.h"
namespace OHOS::Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "ExtensionSessionStage" };
}
ExtensionSessionStage::ExtensionSessionStage(const sptr<ISession>& extensionSession) : SessionStage(extensionSession) {}
WSError ExtensionSessionStage::Connect()
{
return WSError::WS_OK;
if (session_ == nullptr) {
WLOGFE("session is invalid");
return WSError::WS_ERROR_NULLPTR;
}
sptr<ExtensionSessionStage> extensionSessionStage(this);
sptr<IWindowEventChannel> eventChannel(new WindowEventChannel(extensionSessionStage));
return session_->Connect(extensionSessionStage, eventChannel);
}
} // namespace OHOS::Rosen

View File

@ -26,83 +26,164 @@ SessionStage::SessionStage(const sptr<ISession>& session) : session_(session) {}
bool SessionStage::RegisterSessionStageStateListener(const std::shared_ptr<ISessionStageStateListener>& listener)
{
return false;
return RegisterListenerLocked(sessionStageStateListeners_, listener);
}
bool SessionStage::UnregisterSessionStageStateListener(const std::shared_ptr<ISessionStageStateListener>& listener)
{
return false;
return UnregisterListenerLocked(sessionStageStateListeners_, listener);
}
bool SessionStage::RegisterSizeChangeListener(const std::shared_ptr<ISizeChangeListener>& listener)
{
return false;
return RegisterListenerLocked(sizeChangeListeners_, listener);
}
bool SessionStage::UnregisterSizeChangeListener(const std::shared_ptr<ISizeChangeListener>& listener)
{
return false;
return UnregisterListenerLocked(sizeChangeListeners_, listener);
}
bool SessionStage::RegisterPointerEventListener(const std::shared_ptr<IPointerEventListener>& listener)
{
return false;
return RegisterListenerLocked(pointerEventListeners_, listener);
}
bool SessionStage::UnregisterPointerEventListener(const std::shared_ptr<IPointerEventListener>& listener)
{
return false;
return UnregisterListenerLocked(pointerEventListeners_, listener);
}
bool SessionStage::RegisterKeyEventListener(const std::shared_ptr<IKeyEventListener>& listener)
{
return false;
return RegisterListenerLocked(keyEventListeners_, listener);
}
bool SessionStage::UnregisterKeyEventListener(const std::shared_ptr<IKeyEventListener>& listener)
{
return false;
return UnregisterListenerLocked(keyEventListeners_, listener);
}
template<typename T>
bool SessionStage::RegisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener)
{
if (listener == nullptr) {
WLOGFE("listener is nullptr");
return false;
}
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (std::find(holder.begin(), holder.end(), listener) != holder.end()) {
WLOGFW("Listener already registered");
return true;
}
holder.emplace_back(listener);
return true;
}
template<typename T>
bool SessionStage::UnregisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener)
{
if (listener == nullptr) {
WLOGFE("listener could not be null");
return false;
}
std::lock_guard<std::recursive_mutex> lock(mutex_);
holder.erase(std::remove_if(holder.begin(), holder.end(),
[listener](std::shared_ptr<T> registeredListener) { return registeredListener == listener; }),
holder.end());
return true;
}
void SessionStage::NotifySizeChange(const WSRect& rect, SizeChangeReason reason)
{
auto sizeChangeListeners = GetListeners<ISizeChangeListener>();
for (auto& listener : sizeChangeListeners) {
if (!listener.expired()) {
listener.lock()->OnSizeChange(rect, reason);
}
}
}
void SessionStage::NotifyPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
{
auto pointerEventListeners = GetListeners<IPointerEventListener>();
for (auto& listener : pointerEventListeners) {
if (!listener.expired()) {
listener.lock()->OnPointerEvent(pointerEvent);
}
}
}
void SessionStage::NotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
{
auto keyEventListeners = GetListeners<IKeyEventListener>();
for (auto& listener : keyEventListeners) {
if (!listener.expired()) {
listener.lock()->OnKeyEvent(keyEvent);
}
}
}
WSError SessionStage::Connect()
{
return WSError::WS_OK;
if (session_ == nullptr) {
WLOGFE("session is invalid");
return WSError::WS_ERROR_NULLPTR;
}
sptr<SessionStage> sessionStage(this);
sptr<IWindowEventChannel> eventChannel(new WindowEventChannel(sessionStage));
return session_->Connect(sessionStage, eventChannel);
}
WSError SessionStage::Foreground()
{
return WSError::WS_OK;
if (session_ == nullptr) {
WLOGFE("session is invalid");
return WSError::WS_ERROR_NULLPTR;
}
WSError res = session_->Foreground();
if (res == WSError::WS_OK) {
NotifyAfterForeground();
}
return res;
}
WSError SessionStage::Background()
{
if (session_ == nullptr) {
WLOGFE("session is invalid");
return WSError::WS_ERROR_NULLPTR;
}
NotifyAfterBackground();
return WSError::WS_OK;
}
WSError SessionStage::Disconnect()
{
if (session_ == nullptr) {
WLOGFE("session is invalid");
return WSError::WS_ERROR_NULLPTR;
}
return WSError::WS_OK;
}
WSError SessionStage::PendingSessionActivation(const SessionInfo& info)
{
return WSError::WS_OK;
if (session_ == nullptr) {
WLOGFE("session is invalid");
return WSError::WS_ERROR_NULLPTR;
}
return session_->PendingSessionActivation(info);
}
WSError SessionStage::SetActive(bool active)
{
WLOGFD("active status: %{public}d", active);
if (active) {
NotifyAfterActive();
} else {
NotifyAfterInactive();
}
return WSError::WS_OK;
}
@ -110,6 +191,7 @@ WSError SessionStage::UpdateRect(const WSRect& rect, SizeChangeReason reason)
{
WLOGFI("update rect [%{public}d, %{public}d, %{public}u, %{public}u], reason:%{public}u", rect.posX_, rect.posY_,
rect.width_, rect.height_, reason);
NotifySizeChange(rect, reason);
return WSError::WS_OK;
}
@ -118,7 +200,7 @@ WSError SessionStage::Recover()
return WSError::WS_OK;
}
WSError SessionStage::Maximum()
WSError SessionStage::Maximize()
{
return WSError::WS_OK;
}

View File

@ -28,13 +28,23 @@ constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "Window
WSError WindowEventChannel::TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
{
WLOGFI("WindowEventChannel receive key event");
WLOGFD("WindowEventChannel receive key event");
if (!sessionStage_) {
WLOGFE("session stage is null!");
return WSError::WS_ERROR_NULLPTR;
}
sessionStage_->NotifyKeyEvent(keyEvent);
return WSError::WS_OK;
}
WSError WindowEventChannel::TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
{
WLOGFI("WindowEventChannel receive pointer event");
WLOGFD("WindowEventChannel receive pointer event");
if (!sessionStage_) {
WLOGFE("session stage is null!");
return WSError::WS_ERROR_NULLPTR;
}
sessionStage_->NotifyPointerEvent(pointerEvent);
return WSError::WS_OK;
}
} // namespace OHOS::Rosen

View File

@ -22,13 +22,61 @@
#include "window_manager_hilog.h"
namespace OHOS::Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionStageProxy"};
}
WSError SessionStageProxy::SetActive(bool active)
{
return WSError::WS_OK;
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 failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionStageMessage::TRANS_ID_SET_ACTIVE),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError SessionStageProxy::UpdateRect(const WSRect& rect, SizeChangeReason reason)
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (!(data.WriteInt32(rect.posX_) && data.WriteInt32(rect.posY_) &&
data.WriteUint32(rect.width_) && data.WriteUint32(rect.height_))) {
WLOGFE("Write WindowRect failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (!data.WriteUint32(static_cast<uint32_t>(reason))) {
WLOGFE("Write SessionSizeChangeReason failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionStageMessage::TRANS_ID_NOTIFY_SIZE_CHANGE),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
} // namespace OHOS::Rosen

View File

@ -51,12 +51,19 @@ int SessionStageStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Messag
int SessionStageStub::HandleSetActive(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("SetActive!");
bool active = data.ReadBool();
WSError errCode = SetActive(active);
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int SessionStageStub::HandleUpdateRect(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("UpdateRect!");
WSRect rect = { data.ReadInt32(), data.ReadInt32(), data.ReadUint32(), data.ReadUint32() };
SizeChangeReason reason = static_cast<SizeChangeReason>(data.ReadUint32());
WSError errCode = UpdateRect(rect, reason);
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
} // namespace OHOS::Rosen

View File

@ -25,13 +25,55 @@
#include "window_manager_hilog.h"
namespace OHOS::Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowEventChannelProxy"};
}
WSError WindowEventChannelProxy::TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (!keyEvent->WriteToParcel(data)) {
WLOGFE("Failed to write key event");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(WindowEventChannelMessage::TRANS_ID_TRANSFER_KEY_EVENT),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError WindowEventChannelProxy::TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (!pointerEvent->WriteToParcel(data)) {
WLOGFE("Failed to write pointer event");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(WindowEventChannelMessage::TRANS_ID_TRANSFER_POINTER_EVENT),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
}

View File

@ -55,12 +55,34 @@ int WindowEventChannelStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
int WindowEventChannelStub::HandleTransferKeyEvent(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("TransferKeyEvent!");
auto keyEvent = MMI::KeyEvent::Create();
if (keyEvent == nullptr) {
WLOGFE("Failed to create key event!");
return ERR_INVALID_DATA;
}
if (!keyEvent->ReadFromParcel(data)) {
WLOGFE("Read Key Event failed");
return ERR_INVALID_DATA;
}
WSError errCode = TransferKeyEvent(keyEvent);
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int WindowEventChannelStub::HandleTransferPointerEvent(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("TransferPointerEvent!");
auto pointerEvent = MMI::PointerEvent::Create();
if (pointerEvent == nullptr) {
WLOGFE("Failed to create pointer event!");
return ERR_INVALID_DATA;
}
if (!pointerEvent->ReadFromParcel(data)) {
WLOGFE("Read Pointer Event failed");
return ERR_INVALID_DATA;
}
WSError errCode = TransferPointerEvent(pointerEvent);
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
}

View File

@ -37,10 +37,9 @@ using NotifyPendingSessionActivationFunc = std::function<void(const SessionInfo&
class ILifecycleListener {
public:
virtual void OnForeground() = 0;
virtual void OnBackground() = 0;
virtual void OnForeground() {};
virtual void OnBackground() {};
};
class Session : public SessionStub, public virtual RefBase {
public:
Session(const SessionInfo& info);
@ -61,18 +60,12 @@ public:
WSError PendingSessionActivation(const SessionInfo& info) override;
WSError Recover() override;
WSError Maximum() override;
void NotifyForeground();
void NotifyBackground();
WSError Maximize() override;
// for window event
WSError TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
WSError TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent);
bool RegisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener);
bool UnregisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener);
const SessionInfo& GetSessionInfo() const;
void SetPendingSessionActivationEventListener(const NotifyPendingSessionActivationFunc& func);
@ -87,19 +80,12 @@ protected:
NotifyPendingSessionActivationFunc pendingSessionActivationFunc_;
private:
template<typename T>
bool RegisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
template<typename T>
bool UnregisterListenerLocked(std::vector<std::shared_ptr<T>>& holder, const std::shared_ptr<T>& listener);
std::shared_ptr<RSSurfaceNode> CreateSurfaceNode(std::string name);
uint64_t persistentId_ = INVALID_SESSION_ID;
std::shared_ptr<RSSurfaceNode> surfaceNode_ = nullptr;
SessionState state_ = SessionState::STATE_DISCONNECT;
std::recursive_mutex mutex_;
std::vector<std::shared_ptr<ILifecycleListener>> lifecycleListeners_;
sptr<IWindowEventChannel> windowEventChannel_ = nullptr;
};
} // namespace OHOS::Rosen

View File

@ -36,7 +36,7 @@ public:
// Scene
TRANS_ID_RECOVER = 100,
TRANS_ID_MAXIMUM,
TRANS_ID_MAXIMIZE,
};
virtual WSError Connect(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel) = 0;
virtual WSError Foreground() = 0;
@ -46,7 +46,7 @@ public:
// scene session
virtual WSError Recover() = 0;
virtual WSError Maximum() = 0;
virtual WSError Maximize() = 0;
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_INTERFACE_H

View File

@ -35,7 +35,7 @@ public:
WSError PendingSessionActivation(const SessionInfo& info) override;
WSError Recover() override;
WSError Maximum() override;
WSError Maximize() override;
private:
static inline BrokerDelegator<SessionProxy> delegator_;
};

View File

@ -42,7 +42,7 @@ private:
int HandlePendingSessionActivation(MessageParcel& data, MessageParcel& reply);
// for scene
int HandleRecover(MessageParcel& data, MessageParcel& reply);
int HandleMaximum(MessageParcel& data, MessageParcel& reply);
int HandleMaximize(MessageParcel& data, MessageParcel& reply);
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_STUB_H

View File

@ -53,24 +53,6 @@ const SessionInfo& Session::GetSessionInfo() const
return sessionInfo_;
}
bool Session::RegisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener)
{
return false;
}
bool Session::UnregisterLifecycleListener(const std::shared_ptr<ILifecycleListener>& listener)
{
return false;
}
void Session::NotifyForeground()
{
}
void Session::NotifyBackground()
{
}
SessionState Session::GetSessionState() const
{
return state_;
@ -83,18 +65,40 @@ void Session::UpdateSessionState(SessionState state)
bool Session::IsSessionValid() const
{
return false;
bool res = state_ > SessionState::STATE_DISCONNECT && state_ < SessionState::STATE_END;
if (!res) {
WLOGFI("session is already destroyed or not created! id: %{public}" PRIu64 " state: %{public}u",
GetPersistentId(), state_);
}
return res;
}
RSSurfaceNode::SharedPtr Session::CreateSurfaceNode(std::string name)
{
return nullptr;
// expect one session with one surfaceNode
if (name.empty()) {
WLOGFI("name is empty");
name = UNDEFINED + std::to_string(persistentId_);
} else {
std::string surfaceNodeName = name + std::to_string(persistentId_);
std::size_t pos = surfaceNodeName.find_last_of('.');
name = (pos == std::string::npos) ? surfaceNodeName : surfaceNodeName.substr(pos + 1); // skip '.'
}
struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
rsSurfaceNodeConfig.SurfaceNodeName = name;
return RSSurfaceNode::Create(rsSurfaceNodeConfig);
}
WSError Session::UpdateRect(const WSRect& rect, SizeChangeReason reason)
{
WLOGFI("session update rect: id: %{public}" PRIu64 ", rect[%{public}d, %{public}d, %{public}u, %{public}u], "\
"reason:%{public}u", GetPersistentId(), rect.posX_, rect.posY_, rect.width_, rect.height_, reason);
if (!IsSessionValid()) {
winRect_ = rect;
return WSError::WS_ERROR_INVALID_SESSION;
}
sessionStage_->UpdateRect(rect, reason);
winRect_ = rect;
return WSError::WS_OK;
}
@ -102,6 +106,20 @@ WSError Session::Connect(const sptr<ISessionStage>& sessionStage, const sptr<IWi
{
WLOGFI("Connect session, id: %{public}" PRIu64 ", state: %{public}u", GetPersistentId(),
static_cast<uint32_t>(GetSessionState()));
if (GetSessionState() != SessionState::STATE_DISCONNECT) {
WLOGFE("state is not disconnect!");
return WSError::WS_ERROR_INVALID_SESSION;
}
if (sessionStage == nullptr || eventChannel == nullptr) {
WLOGFE("session stage or eventChannel is nullptr");
return WSError::WS_ERROR_NULLPTR;
}
sessionStage_ = sessionStage;
windowEventChannel_ = eventChannel;
UpdateSessionState(SessionState::STATE_CONNECT);
// once update rect before connect, update again when connect
UpdateRect(winRect_, SizeChangeReason::SHOW);
return WSError::WS_OK;
}
@ -110,6 +128,17 @@ WSError Session::Foreground()
SessionState state = GetSessionState();
WLOGFI("Foreground session, id: %{public}" PRIu64 ", state: %{public}u", GetPersistentId(),
static_cast<uint32_t>(state));
if (state != SessionState::STATE_CONNECT && state != SessionState::STATE_BACKGROUND) {
WLOGFE("state invalid!");
return WSError::WS_ERROR_INVALID_SESSION;
}
if (!isActive_) {
sessionStage_->SetActive(true);
UpdateSessionState(SessionState::STATE_ACTIVE);
} else {
UpdateSessionState(SessionState::STATE_FOREGROUND);
}
return WSError::WS_OK;
}
@ -118,6 +147,11 @@ WSError Session::Background()
SessionState state = GetSessionState();
WLOGFI("Background session, id: %{public}" PRIu64 ", state: %{public}u", GetPersistentId(),
static_cast<uint32_t>(state));
if (state < SessionState::STATE_INACTIVE) { // only STATE_INACTIVE can transfer to background
WLOGFE("state invalid!");
return WSError::WS_ERROR_INVALID_SESSION;
}
UpdateSessionState(SessionState::STATE_BACKGROUND);
return WSError::WS_OK;
}
@ -126,6 +160,10 @@ WSError Session::Disconnect()
SessionState state = GetSessionState();
WLOGFI("Disconnect session, id: %{public}" PRIu64 ", state: %{public}u", GetPersistentId(),
static_cast<uint32_t>(state));
Background();
if (GetSessionState() == SessionState::STATE_BACKGROUND) {
UpdateSessionState(SessionState::STATE_DISCONNECT);
}
return WSError::WS_OK;
}
@ -134,11 +172,30 @@ WSError Session::SetActive(bool active)
SessionState state = GetSessionState();
WLOGFI("Session update active: %{public}d, id: %{public}" PRIu64 ", state: %{public}u", active, GetPersistentId(),
static_cast<uint32_t>(state));
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) {
sessionStage_->SetActive(true);
UpdateSessionState(SessionState::STATE_ACTIVE);
}
if (!active && GetSessionState() == SessionState::STATE_ACTIVE) {
sessionStage_->SetActive(false);
UpdateSessionState(SessionState::STATE_INACTIVE);
}
return WSError::WS_OK;
}
WSError Session::PendingSessionActivation(const SessionInfo& info)
{
if (pendingSessionActivationFunc_) {
pendingSessionActivationFunc_(info);
}
return WSError::WS_OK;
}
@ -152,7 +209,7 @@ WSError Session::Recover()
return WSError::WS_OK;
}
WSError Session::Maximum()
WSError Session::Maximize()
{
return WSError::WS_OK;
}
@ -161,12 +218,22 @@ WSError Session::Maximum()
WSError Session::TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
{
WLOGFD("Session TransferPointEvent");
if (!windowEventChannel_) {
WLOGFE("windowEventChannel_ is null");
return WSError::WS_ERROR_NULLPTR;
}
windowEventChannel_->TransferPointerEvent(pointerEvent);
return WSError::WS_OK;
}
WSError Session::TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
{
WLOGFD("Session TransferPointEvent");
if (!windowEventChannel_) {
WLOGFE("windowEventChannel_ is null");
return WSError::WS_ERROR_NULLPTR;
}
windowEventChannel_->TransferKeyEvent(keyEvent);
return WSError::WS_OK;
}
} // namespace OHOS::Rosen

View File

@ -22,38 +22,159 @@
#include "window_manager_hilog.h"
namespace OHOS::Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionProxy"};
}
WSError SessionProxy::Foreground()
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionMessage::TRANS_ID_FOREGROUND),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError SessionProxy::Background()
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionMessage::TRANS_ID_BACKGROUND),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError SessionProxy::Disconnect()
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionMessage::TRANS_ID_DISCONNECT),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError SessionProxy::Connect(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel)
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (!data.WriteRemoteObject(sessionStage->AsObject())) {
WLOGFE("Write ISessionStage failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (!data.WriteRemoteObject(eventChannel->AsObject())) {
WLOGFE("Write IWindowEventChannel failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionMessage::TRANS_ID_CONNECT),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError SessionProxy::PendingSessionActivation(const SessionInfo& info)
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (!(data.WriteString(info.bundleName_) && data.WriteString(info.abilityName_))) {
WLOGFE("Write ability info failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (info.callerToken_) {
if (!data.WriteBool(true) || !data.WriteRemoteObject(info.callerToken_)) {
WLOGFE("Write ability info failed");
return WSError::WS_ERROR_IPC_FAILED;
}
} else {
if (!data.WriteBool(false)) {
WLOGFE("Write ability info failed");
return WSError::WS_ERROR_IPC_FAILED;
}
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionMessage::TRANS_ID_ACTIVE_PENDING_SESSION),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError SessionProxy::Recover()
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionMessage::TRANS_ID_RECOVER),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
WSError SessionProxy::Maximum()
WSError SessionProxy::Maximize()
{
return WSError::WS_OK;
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");
return WSError::WS_ERROR_IPC_FAILED;
}
if (Remote()->SendRequest(static_cast<uint32_t>(SessionMessage::TRANS_ID_MAXIMIZE),
data, reply, option) != ERR_NONE) {
WLOGFE("SendRequest failed");
return WSError::WS_ERROR_IPC_FAILED;
}
int32_t ret = reply.ReadUint32();
return static_cast<WSError>(ret);
}
} // namespace OHOS::Rosen

View File

@ -32,7 +32,7 @@ const std::map<uint32_t, SessionStubFunc> SessionStub::stubFuncMap_{
// for scene only
std::make_pair(static_cast<uint32_t>(SessionMessage::TRANS_ID_RECOVER), &SessionStub::HandleRecover),
std::make_pair(static_cast<uint32_t>(SessionMessage::TRANS_ID_MAXIMUM), &SessionStub::HandleMaximum)
std::make_pair(static_cast<uint32_t>(SessionMessage::TRANS_ID_MAXIMIZE), &SessionStub::HandleMaximize)
};
int SessionStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
@ -55,42 +55,70 @@ int SessionStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParc
int SessionStub::HandleForeground(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("Foreground!");
WSError errCode = Foreground();
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int SessionStub::HandleBackground(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("Background!");
WSError errCode = Background();
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int SessionStub::HandleDisconnect(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("Disconnect!");
WSError errCode = Disconnect();
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int SessionStub::HandleConnect(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("Connect!");
sptr<IRemoteObject> sessionStageObject = data.ReadRemoteObject();
sptr<ISessionStage> sessionStageProxy = iface_cast<ISessionStage>(sessionStageObject);
sptr<IRemoteObject> eventChannelObject = data.ReadRemoteObject();
sptr<IWindowEventChannel> eventChannelProxy = iface_cast<IWindowEventChannel>(eventChannelObject);
if (sessionStageProxy == nullptr || eventChannelProxy == nullptr) {
WLOGFE("Failed to read scene session stage object or event channel object!");
return ERR_INVALID_DATA;
}
WSError errCode = Connect(sessionStageProxy, eventChannelProxy);
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int SessionStub::HandleRecover(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("Recover!");
WSError errCode = Recover();
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int SessionStub::HandleMaximum(MessageParcel& data, MessageParcel& reply)
int SessionStub::HandleMaximize(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("Maximum!");
WLOGFD("Maximize!");
WSError errCode = Maximize();
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
int SessionStub::HandlePendingSessionActivation(MessageParcel& data, MessageParcel& reply)
{
WLOGFD("PendingSessionActivation!");
SessionInfo info;
info.bundleName_ = data.ReadString();
info.abilityName_ = data.ReadString();
if (data.ReadBool()) {
info.callerToken_ = data.ReadRemoteObject();
}
WSError errCode = PendingSessionActivation(info);
reply.WriteUint32(static_cast<uint32_t>(errCode));
return ERR_NONE;
}
} // namespace OHOS::Rosen

View File

@ -32,6 +32,7 @@ ohos_shared_library("scene_session_manager") {
deps = [
"${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",
]

View File

@ -25,11 +25,21 @@
#include "interfaces/include/ws_common.h"
#include "session_manager_base.h"
#include "wm_single_instance.h"
namespace OHOS::AAFwk {
class SessionInfo;
}
namespace OHOS::Rosen {
class ExtensionSession;
class ExtensionSessionManager : public SessionManagerBase {
WM_DECLARE_SINGLE_INSTANCE_BASE(ExtensionSessionManager)
public:
/**
* @brief init extension session manager
*
* @return WSError
*/
WSError Init();
/**
* @brief create extension session
*
@ -62,12 +72,12 @@ public:
*/
WSError RequestExtensionSessionDestruction(const sptr<ExtensionSession>& extensionSession);
protected:
sptr<AAFwk::SessionInfo> SetAbilitySessionInfo(const sptr<ExtensionSession>& extensionSession);
ExtensionSessionManager();
virtual ~ExtensionSessionManager() = default;
private:
void Init();
std::map<uint32_t, std::pair<sptr<ExtensionSession>, sptr<IRemoteObject>>> abilityExtensionMap_;
std::map<uint32_t, sptr<ExtensionSession>> extensionMap_;
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_WINDOW_SCENE_EXTENSION_SESSION_MANAGER_H

View File

@ -16,6 +16,8 @@
#ifndef OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_BASE_H
#define OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_BASE_H
#include "common/include/message_scheduler.h"
namespace OHOS::Rosen {
class SessionManagerBase {
public:
@ -26,7 +28,8 @@ public:
return (((uint64_t)pid_ << 32) | (++sessionId_)); // 32: high bit for uint64
}
protected:
std::shared_ptr<MessageScheduler> mmsScheduler_ = nullptr;
std::atomic<bool> mmsSchedulerInit_ = false;
private:
int pid_ = getpid();
// shared by scene session and extension session once in same process

View File

@ -20,9 +20,14 @@
#include <want.h>
#include "session/host/include/extension_session.h"
#include "session_info.h"
#include "window_manager_hilog.h"
namespace OHOS::Rosen {
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "ExtensionSessionManager" };
const std::string EXTENSION_SESSION_MANAGER_THREAD = "ExtensionSessionManager";
}
WM_IMPLEMENT_SINGLE_INSTANCE(ExtensionSessionManager)
@ -30,27 +35,149 @@ ExtensionSessionManager::ExtensionSessionManager()
{
}
void ExtensionSessionManager::Init()
WSError ExtensionSessionManager::Init()
{
WLOGFI("extension session manager init.");
if (mmsSchedulerInit_) {
WLOGFW("mmsScheduler_ already init!");
return WSError::WS_DO_NOTHING;
}
mmsScheduler_ = std::make_shared<MessageScheduler>(EXTENSION_SESSION_MANAGER_THREAD);
if (!mmsScheduler_) {
WLOGFE("new mmsScheduler_ failed!");
return WSError::WS_ERROR_NULLPTR;
}
mmsSchedulerInit_ = true;
return WSError::WS_OK;
}
sptr<AAFwk::SessionInfo> ExtensionSessionManager::SetAbilitySessionInfo(const sptr<ExtensionSession>& extSession)
{
sptr<AAFwk::SessionInfo> abilitySessionInfo = new (std::nothrow) AAFwk::SessionInfo();
if (!abilitySessionInfo) {
WLOGFE("abilitySessionInfo is nullptr");
return nullptr;
}
auto sessionInfo = extSession->GetSessionInfo();
abilitySessionInfo->sessionToken = extSession;
abilitySessionInfo->surfaceNode = extSession->GetSurfaceNode();
abilitySessionInfo->callerToken = sessionInfo.callerToken_;
abilitySessionInfo->persistentId = extSession->GetPersistentId();
return abilitySessionInfo;
}
sptr<ExtensionSession> ExtensionSessionManager::RequestExtensionSession(const SessionInfo& sessionInfo)
{
return nullptr;
if (!mmsSchedulerInit_) {
WLOGFE("mmsScheduler_ not init!");
return nullptr;
}
auto task = [this, sessionInfo]() {
sptr<ExtensionSession> extensionSession = new (std::nothrow) ExtensionSession(sessionInfo);
if (extensionSession == nullptr) {
WLOGFE("extensionSession is nullptr!");
return extensionSession;
}
uint64_t persistentId = GeneratePersistentId();
extensionSession->SetPersistentId(persistentId);
WLOGFI("create session persistentId: %{public}" PRIu64 ", bundleName: %{public}s, abilityName: %{public}s",
persistentId, sessionInfo.bundleName_.c_str(), sessionInfo.abilityName_.c_str());
extensionMap_.insert({ persistentId, extensionSession });
return extensionSession;
};
// once init but mmsScheduler_ is nullptr
WS_CHECK_NULL_SCHE_RETURN(mmsScheduler_, task);
return mmsScheduler_->PostSyncTask(task);
}
WSError ExtensionSessionManager::RequestExtensionSessionActivation(const sptr<ExtensionSession>& extensionSession)
{
wptr<ExtensionSession> weakExtSession(extensionSession);
auto task = [this, weakExtSession]() {
auto extSession = weakExtSession.promote();
if (extSession == nullptr) {
WLOGFE("session is nullptr");
return WSError::WS_ERROR_NULLPTR;
}
auto persistentId = extSession->GetPersistentId();
WLOGFI("active persistentId: %{public}" PRIu64 "", persistentId);
if (extensionMap_.count(persistentId) == 0) {
WLOGFE("session is invalid with %{public}" PRIu64 "", persistentId);
return WSError::WS_ERROR_INVALID_SESSION;
}
AAFwk::Want want;
auto sessionInfo = extSession->GetSessionInfo();
want.SetElementName(sessionInfo.bundleName_, sessionInfo.abilityName_);
AAFwk::StartOptions startOptions;
auto extSessionInfo = SetAbilitySessionInfo(extSession);
if (!extSessionInfo) {
return WSError::WS_ERROR_NULLPTR;
}
AAFwk::AbilityManagerClient::GetInstance()->StartUIExtensionAbility(want, extSessionInfo,
AAFwk::DEFAULT_INVAL_VALUE,
AppExecFwk::ExtensionAbilityType::UI);
return WSError::WS_OK;
};
WS_CHECK_NULL_SCHE_RETURN(mmsScheduler_, task);
mmsScheduler_->PostAsyncTask(task);
return WSError::WS_OK;
}
WSError ExtensionSessionManager::RequestExtensionSessionBackground(const sptr<ExtensionSession>& extensionSession)
{
wptr<ExtensionSession> weakExtSession(extensionSession);
auto task = [this, weakExtSession]() {
auto extSession = weakExtSession.promote();
if (extSession == nullptr) {
WLOGFE("session is nullptr");
return WSError::WS_ERROR_NULLPTR;
}
auto persistentId = extSession->GetPersistentId();
WLOGFI("background session persistentId: %{public}" PRIu64 "", persistentId);
extSession->SetActive(false);
extSession->Background();
if (extensionMap_.count(persistentId) == 0) {
WLOGFE("session is invalid with %{public}" PRIu64 "", persistentId);
return WSError::WS_ERROR_INVALID_SESSION;
}
auto extSessionInfo = SetAbilitySessionInfo(extSession);
if (!extSessionInfo) {
return WSError::WS_ERROR_NULLPTR;
}
AAFwk::AbilityManagerClient::GetInstance()->MinimizeUIExtensionAbility(extSessionInfo);
return WSError::WS_OK;
};
WS_CHECK_NULL_SCHE_RETURN(mmsScheduler_, task);
mmsScheduler_->PostAsyncTask(task);
return WSError::WS_OK;
}
WSError ExtensionSessionManager::RequestExtensionSessionDestruction(const sptr<ExtensionSession>& extensionSession)
{
wptr<ExtensionSession> weakExtSession(extensionSession);
auto task = [this, weakExtSession]() {
auto extSession = weakExtSession.promote();
if (extSession == nullptr) {
WLOGFE("session is nullptr");
return WSError::WS_ERROR_NULLPTR;
}
auto persistentId = extSession->GetPersistentId();
WLOGFI("destroy session persistentId: %{public}" PRIu64 "", persistentId);
extSession->Disconnect();
if (extensionMap_.count(persistentId) == 0) {
WLOGFE("session is invalid with %{public}" PRIu64 "", persistentId);
return WSError::WS_ERROR_INVALID_SESSION;
}
auto extSessionInfo = SetAbilitySessionInfo(extSession);
if (!extSessionInfo) {
return WSError::WS_ERROR_NULLPTR;
}
AAFwk::AbilityManagerClient::GetInstance()->TerminateUIExtensionAbility(extSessionInfo);
extensionMap_.erase(persistentId);
return WSError::WS_OK;
};
WS_CHECK_NULL_SCHE_RETURN(mmsScheduler_, task);
mmsScheduler_->PostAsyncTask(task);
return WSError::WS_OK;
}
} // namespace OHOS::Rosen