新增多用户管理与事件管理

Signed-off-by: chenshuo <chenshuo42@huawei.com>
This commit is contained in:
chenshuo 2024-11-07 16:30:28 +08:00
parent 6206d8ffac
commit 84b807ab72
4 changed files with 394 additions and 0 deletions

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 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_DP_MULTIPLE_USER_CONNECTOR_H
#define OHOS_DP_MULTIPLE_USER_CONNECTOR_H
#include <cstdint>
#include <mutex>
#include <string>
#include <vector>
#include "iremote_object.h"
#include "distributed_device_profile_constants.h"
#include "single_instance.h"
namespace OHOS {
namespace DistributedDeviceProfile {
class MultiUserManager {
DECLARE_SINGLE_INSTANCE(MultiUserManager);
public:
int32_t Init();
int32_t UnInit();
int32_t GetCurrentForegroundUserID();
void SetCurrentForegroundUserID(int32_t userId);
int32_t GetForegroundUserIDFromOs(int32_t& foregroundId);
private:
int32_t foregroundUserId_ = DEFAULT_USER_ID;
std::mutex foregroundUserIdLock_;
};
} // namespace DistributedHardware
} // namespace OHOS
#endif // OHOS_DP_MULTIPLE_USER_CONNECTOR_H

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2024 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_DP_ACCOUNT_COMMON_EVENT_H
#define OHOS_DP_ACCOUNT_COMMON_EVENT_H
#include <functional>
#include <mutex>
#include <string>
#include <vector>
#include "common_event_data.h"
#include "common_event_manager.h"
#include "common_event_subscribe_info.h"
#include "common_event_subscriber.h"
#include "matching_skills.h"
#include "system_ability_status_change_stub.h"
namespace OHOS {
namespace DistributedDeviceProfile {
using OHOS::EventFwk::CommonEventData;
using OHOS::EventFwk::CommonEventSubscriber;
using OHOS::EventFwk::CommonEventSubscribeInfo;
using AccountEventCallback = std::function<void(int32_t, std::string)>;
class DpAccountEventSubscriber : public CommonEventSubscriber {
public:
DpAccountEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const AccountEventCallback &callback,
const std::vector<std::string> &eventNameVec) : CommonEventSubscriber(subscribeInfo),
eventNameVec_(eventNameVec), callback_(callback) {}
~DpAccountEventSubscriber() override = default;
std::vector<std::string> GetSubscriberEventNameVec() const;
void OnReceiveEvent(const CommonEventData &data) override;
private:
std::vector<std::string> eventNameVec_;
AccountEventCallback callback_;
};
class DpAccountCommonEventManager {
public:
DpAccountCommonEventManager() = default;
~DpAccountCommonEventManager();
bool SubscribeAccountCommonEvent(const std::vector<std::string> &eventNameVec,
const AccountEventCallback &callback);
bool UnsubscribeAccountCommonEvent();
private:
std::vector<std::string> eventNameVec_;
bool eventValidFlag_ = false;
std::mutex evenSubscriberMutex_;
std::shared_ptr<DpAccountEventSubscriber> subscriber_ = nullptr;
sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
int32_t counter_ = 0;
private:
class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
public:
explicit SystemAbilityStatusChangeListener(std::shared_ptr<DpAccountEventSubscriber> AccountSubscriber)
: changeSubscriber_(AccountSubscriber) {}
~SystemAbilityStatusChangeListener() = default;
void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
private:
std::shared_ptr<DpAccountEventSubscriber> changeSubscriber_;
};
};
} // namespace DistributedHardware
} // namespace OHOS
#endif // OHOS_DP_ACCOUNT_COMMON_EVENT_H

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2024 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 "multi_user_manager.h"
#include "device_profile_manager.h"
#include "distributed_device_profile_constants.h"
#include "distributed_device_profile_errors.h"
#include "distributed_device_profile_log.h"
#include "profile_utils.h"
#include "account_info.h"
#include "ohos_account_kits.h"
#include "os_account_manager.h"
namespace OHOS {
namespace DistributedDeviceProfile {
IMPLEMENT_SINGLE_INSTANCE(MultiUserManager);
namespace {
const std::string TAG = "MultiUserManager";
}
int32_t MultiUserManager::Init()
{
HILOGI("Init");
int32_t foregroundId = DEFAULT_USER_ID;
int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId);
if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) {
HILOGE("GetForegroundId failed, res: %{public}d", res);
}
HILOGI("current foregroundId = %{public}d", foregroundId);
SetCurrentForegroundUserID(foregroundId);
return DP_SUCCESS;
}
int32_t MultiUserManager::UnInit()
{
return DP_SUCCESS;
}
void MultiUserManager::SetCurrentForegroundUserID(int32_t userId)
{
std::lock_guard<std::mutex> lock(foregroundUserIdLock_);
DeviceProfileManager::GetInstance().OnUserChange(foregroundUserId_, userId);
foregroundUserId_ = userId;
}
int32_t MultiUserManager::GetCurrentForegroundUserID()
{
std::lock_guard<std::mutex> lock(foregroundUserIdLock_);
return foregroundUserId_;
}
int32_t MultiUserManager::GetForegroundUserIDFromOs(int32_t& foregroundId)
{
foregroundId = DEFAULT_USER_ID;
int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundId);
if (res != DP_SUCCESS || foregroundId == DEFAULT_USER_ID) {
HILOGE("GetForegroundId failed, res:%{public}d", res);
return DP_GET_FOREGROUND_ID_FAIL;
}
HILOGI("GetForegroundUserIDFromOs foregroundId = %{public}d", foregroundId);
return DP_SUCCESS;
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -0,0 +1,181 @@
/*
* Copyright (c) 2024 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 "dp_account_common_event.h"
#include <pthread.h>
#include <thread>
#include "common_event_support.h"
#include "distributed_device_profile_errors.h"
#include "distributed_device_profile_log.h"
#include "ffrt.h"
#include "iservice_registry.h"
#include "multi_user_manager.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace DistributedDeviceProfile {
using OHOS::EventFwk::MatchingSkills;
using OHOS::EventFwk::CommonEventManager;
constexpr int32_t MAX_TRY_TIMES = 3;
namespace {
const std::string TAG = "AccountCommonEvent";
}
std::vector<std::string> DpAccountEventSubscriber::GetSubscriberEventNameVec() const
{
return eventNameVec_;
}
DpAccountCommonEventManager::~DpAccountCommonEventManager()
{
DpAccountCommonEventManager::UnsubscribeAccountCommonEvent();
}
bool DpAccountCommonEventManager::SubscribeAccountCommonEvent(const std::vector<std::string> &eventNameVec,
const AccountEventCallback &callback)
{
if (eventNameVec.empty() || callback == nullptr) {
HILOGE("eventNameVec is empty or callback is nullptr.");
return false;
}
std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
if (eventValidFlag_) {
HILOGE("failed to subscribe account commom eventName size: %{public}zu", eventNameVec.size());
return false;
}
MatchingSkills matchingSkills;
for (auto &item : eventNameVec) {
matchingSkills.AddEvent(item);
}
CommonEventSubscribeInfo subscriberInfo(matchingSkills);
subscriber_ = std::make_shared<DpAccountEventSubscriber>(subscriberInfo, callback, eventNameVec);
auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgrProxy == nullptr) {
HILOGE("samgrProxy is nullptr");
subscriber_ = nullptr;
return false;
}
statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
if (statusChangeListener_ == nullptr) {
HILOGE("statusChangeListener_ is nullptr");
subscriber_ = nullptr;
return false;
}
while (counter_ != MAX_TRY_TIMES) {
if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
HILOGI("SubscribeAccountEvent success.");
counter_ = 0;
break;
}
if (++counter_ == MAX_TRY_TIMES) {
HILOGE("SubscribeAccountEvent failed.");
}
sleep(1);
}
eventNameVec_ = eventNameVec;
eventValidFlag_ = true;
HILOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size());
return true;
}
bool DpAccountCommonEventManager::UnsubscribeAccountCommonEvent()
{
std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
if (!eventValidFlag_) {
HILOGE("failed to unsubscribe account commom event name size: %{public}zu because event is invalid.",
eventNameVec_.size());
return false;
}
if (subscriber_ != nullptr) {
HILOGI("start to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
HILOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
return false;
}
HILOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
subscriber_ = nullptr;
}
if (statusChangeListener_ != nullptr) {
auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgrProxy == nullptr) {
HILOGE("samgrProxy is nullptr");
return false;
}
int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
if (ret != ERR_OK) {
HILOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
return false;
}
statusChangeListener_ = nullptr;
}
HILOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
eventValidFlag_ = false;
return true;
}
void DpAccountEventSubscriber::OnReceiveEvent(const CommonEventData &data)
{
std::string receiveEvent = data.GetWant().GetAction();
HILOGI("Received account event: %{public}s", receiveEvent.c_str());
int32_t userId = data.GetCode();
bool accountValidEvent = false;
if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED ||
receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
userId = data.GetCode();
accountValidEvent = true;
}
if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT ||
receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN) {
userId = data.GetWant().GetIntParam("userId", 0);
accountValidEvent = true;
}
if (userId <= 0 || !accountValidEvent) {
HILOGE("Invalid account type event.");
return;
}
ffrt::submit([=]() { callback_(userId, receiveEvent); });
}
void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
int32_t systemAbilityId, const std::string& deviceId)
{
HILOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
return;
}
if (changeSubscriber_ == nullptr) {
HILOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr.");
return;
}
std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
HILOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size());
if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
HILOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size());
}
}
void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
int32_t systemAbilityId, const std::string& deviceId)
{
HILOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
}
} // namespace DistributedHardware
} // namespace OHOS