diff --git a/services/devicemanagerservice/BUILD.gn b/services/devicemanagerservice/BUILD.gn index 5c0c2ae9625aa3521516b9ab4a3635de136c83f1..4e798afa5d234e01b2d92a5039832fc5f8e136ab 100644 --- a/services/devicemanagerservice/BUILD.gn +++ b/services/devicemanagerservice/BUILD.gn @@ -35,6 +35,7 @@ if (defined(ohos_lite)) { "include/devicestate", "include/discovery", "include/dependency/commonevent", + "include/dependency/multipleuser", "include/dependency/hichain", "include/dependency/softbus", "include/dependency/timer", @@ -46,6 +47,7 @@ if (defined(ohos_lite)) { "${common_path}/include/ipc/model", "//base/security/deviceauth/interfaces/innerkits", "//third_party/json/include", + "//base/account/os_account/interfaces/innerkits/osaccount/native/include", ] } @@ -61,6 +63,7 @@ if (defined(ohos_lite)) { "src/config/dm_config_manager.cpp", "src/dependency/commonevent/dm_common_event_manager.cpp", "src/dependency/hichain/hichain_connector.cpp", + "src/dependency/multipleuser/multiple_user_connector.cpp", "src/dependency/softbus/softbus_connector.cpp", "src/dependency/softbus/softbus_session.cpp", "src/dependency/timer/dm_timer.cpp", @@ -87,6 +90,7 @@ if (defined(ohos_lite)) { "${common_path}/include/dialog_ui/js:dialog_js_files_etc", "${innerkits_path}/native_cpp:devicemanagersdk", "${utils_path}:devicemanagerutils", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/security/deviceauth/services:deviceauth_sdk", "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", "//foundation/aafwk/standard/interfaces/innerkits/want:want", diff --git a/services/devicemanagerservice/include/authentication/dm_auth_manager.h b/services/devicemanagerservice/include/authentication/dm_auth_manager.h index a24f9d45d6a58adf253a7b9b9f47d11f882e3b69..6a7ae6b85aeb7a5a7f90f58e97bf559765801835 100644 --- a/services/devicemanagerservice/include/authentication/dm_auth_manager.h +++ b/services/devicemanagerservice/include/authentication/dm_auth_manager.h @@ -163,7 +163,7 @@ public: void ShowStartAuthDialog(); int32_t GetAuthenticationParam(DmAuthParam &authParam); int32_t OnUserOperation(int32_t action); - void UserSwitchEventCallback(void); + void UserSwitchEventCallback(int32_t userId); void ClosePage(const int32_t &id); private: diff --git a/services/devicemanagerservice/include/dependency/commonevent/dm_common_event_manager.h b/services/devicemanagerservice/include/dependency/commonevent/dm_common_event_manager.h index 7efcbb5f5fcf3573f4dda66b86c46616fce41a1f..81d51bb2a6482ffc0f1d329b4cb260503905db5f 100644 --- a/services/devicemanagerservice/include/dependency/commonevent/dm_common_event_manager.h +++ b/services/devicemanagerservice/include/dependency/commonevent/dm_common_event_manager.h @@ -31,21 +31,24 @@ namespace OHOS { namespace DistributedHardware { -using CommomEventCallback = std::function; +using CommomEventCallback = std::function; + +struct CommomEventCallbackNode { + int32_t input_; + CommomEventCallback callback_; +}; class DmCommonEventManager { DECLARE_SINGLE_INSTANCE_BASE(DmCommonEventManager); public: - bool SubscribeServiceEvent(const std::string &event, const CommomEventCallback &callback); + bool SubscribeServiceEvent(const std::string &event, const CommomEventCallback callback); bool UnsubscribeServiceEvent(const std::string &event); - void Test(const std::string &event); class EventSubscriber : public EventFwk::CommonEventSubscriber { public: EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, const CommomEventCallback &callback, const std::string &event) : EventFwk::CommonEventSubscriber(subscribeInfo), callback_(callback), event_(event) {} void OnReceiveEvent(const EventFwk::CommonEventData &data); - void TestCommonEvent(const std::string &event); private: CommomEventCallback callback_; std::string event_; @@ -59,7 +62,7 @@ private: static std::mutex callbackQueueMutex_; static std::mutex eventSubscriberMutex_; static std::condition_variable notEmpty_; - static std::list callbackQueue_; + static std::list callbackQueue_; std::map> dmEventSubscriber_; }; } // namespace DistributedHardware diff --git a/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h b/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h index 0d514be7a15d6bacf55d0bb89e54664e9850904b..99d534f6ef6ee9d3749494be63c4dde0d2d5c170 100644 --- a/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h +++ b/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h @@ -59,9 +59,11 @@ public: int32_t AddMember(std::string deviceId, std::string &connectInfo); int32_t DelMemberFromGroup(std::string groupId, std::string deviceId); int32_t DeleteGroup(std::string &groupId); + int32_t DeleteGroup(const int32_t userId, std::string &groupId); bool IsDevicesInGroup(std::string hostDevice, std::string peerDevice); int32_t GetRelatedGroups(std::string DeviceId, std::vector &groupList); int32_t GetGroupInfo(std::string queryParams, std::vector &groupList); + int32_t GetGroupInfo(const int32_t userId, std::string queryParams, std::vector &groupList); private: int64_t GenRequestId(); int32_t SyncGroups(std::string deviceId, std::vector &remoteGroupIdList); diff --git a/services/devicemanagerservice/include/dependency/multipleuser/multiple_user_connector.h b/services/devicemanagerservice/include/dependency/multipleuser/multiple_user_connector.h new file mode 100644 index 0000000000000000000000000000000000000000..9bcb63b6ad536a13c3fdd8a6904ed8922af6a059 --- /dev/null +++ b/services/devicemanagerservice/include/dependency/multipleuser/multiple_user_connector.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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_DM_MULTIPLE_USER_CONNECTOR_H +#define OHOS_DM_MULTIPLE_USER_CONNECTOR_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class MultipleUserConnector { +public: + static int32_t GetCurrentAccountUserID(void); + static void SetSwitchOldUserId(int32_t userId); + static int32_t GetSwitchOldUserId(void); +private: + static int32_t oldUserId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_MULTIPLE_USER_CONNECTOR_H \ No newline at end of file diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index 777697438f31f96c294578e5d0fbf338895e2c17..dfe504b5c30c43ce160c5ee2c1543e8abcbbef50 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -22,6 +22,7 @@ #include "dm_constants.h" #include "dm_log.h" #include "dm_random.h" +#include "multiple_user_connector.h" #include "nlohmann/json.hpp" #include "parameter.h" #include "ui_service_mgr_client.h" @@ -750,19 +751,33 @@ int32_t DmAuthManager::OnUserOperation(int32_t action) return DM_OK; } -void DmAuthManager::UserSwitchEventCallback (void) +void DmAuthManager::UserSwitchEventCallback (int32_t userId) { - LOGI("all groups from this device will be deleted"); + LOGI("switch user event happen and this user groups will be deleted with userId: %d", userId); nlohmann::json jsonObj; jsonObj[FIELD_GROUP_TYPE] = GROUP_TYPE_PEER_TO_PEER_GROUP; std::string queryParams = jsonObj.dump(); std::vector groupList; - if (!hiChainConnector_->GetGroupInfo(queryParams, groupList)) { + + int32_t oldUserId = MultipleUserConnector::GetSwitchOldUserId(); + MultipleUserConnector::SetSwitchOldUserId(userId); + if (!hiChainConnector_->GetGroupInfo(oldUserId, queryParams, groupList)) { + LOGE("failed to get device join groups"); + return; + } + for (auto iter = groupList.begin(); iter != groupList.end(); iter++) { + int32_t ret = hiChainConnector_->DeleteGroup(oldUserId, iter->groupId); + if (ret != DM_OK) { + LOGE("fail to delete group"); + } + } + + if (!hiChainConnector_->GetGroupInfo(userId, queryParams, groupList)) { LOGE("failed to get device join groups"); return; } for (auto iter = groupList.begin(); iter != groupList.end(); iter++) { - int32_t ret = hiChainConnector_->DeleteGroup(iter->groupId); + int32_t ret = hiChainConnector_->DeleteGroup(userId, iter->groupId); if (ret != DM_OK) { LOGE("fail to delete group"); } diff --git a/services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp b/services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp index 661404a30575a2b4939af110fad82d5b606977e0..798174abeb67fe7efb59906ce2d3bc18bc7001d5 100644 --- a/services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp +++ b/services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp @@ -26,7 +26,7 @@ namespace DistributedHardware { std::mutex DmCommonEventManager::callbackQueueMutex_; std::mutex DmCommonEventManager::eventSubscriberMutex_; std::condition_variable DmCommonEventManager::notEmpty_; -std::list DmCommonEventManager::callbackQueue_; +std::list DmCommonEventManager::callbackQueue_; DmCommonEventManager &DmCommonEventManager::GetInstance() { @@ -55,13 +55,15 @@ void DmCommonEventManager::DealCallback(void) while (1) { std::unique_lock callbackQueueMutexLock(callbackQueueMutex_); notEmpty_.wait(callbackQueueMutexLock, [] { return !callbackQueue_.empty(); }); - CommomEventCallback funcPrt = callbackQueue_.front(); - funcPrt(); + CommomEventCallbackNode node = callbackQueue_.front(); + int32_t input = node.input_; + CommomEventCallback funcPrt = node.callback_; + funcPrt(input); callbackQueue_.pop_front(); } } -bool DmCommonEventManager::SubscribeServiceEvent(const std::string &event, const CommomEventCallback &callback) +bool DmCommonEventManager::SubscribeServiceEvent(const std::string &event, const CommomEventCallback callback) { LOGI("Subscribe event: %s", event.c_str()); if (dmEventSubscriber_.find(event) != dmEventSubscriber_.end() || callback == nullptr) { @@ -107,30 +109,6 @@ bool DmCommonEventManager::UnsubscribeServiceEvent(const std::string &event) return true; } -void DmCommonEventManager::Test(const std::string &event) -{ - std::unique_lock mutexLock(eventSubscriberMutex_); - if (dmEventSubscriber_.find(event) != dmEventSubscriber_.end()) { - dmEventSubscriber_[event]->TestCommonEvent(event); - } -} - -void DmCommonEventManager::EventSubscriber::TestCommonEvent(const std::string &event) -{ - if (event != event_) { - LOGE("Received event is error"); - return; - } - - std::unique_lock callbackQueueMutexLock(callbackQueueMutex_); - if (callbackQueue_.size() > COMMON_CALLBACK_MAX_SIZE) { - LOGE("event callback Queue is too long"); - return; - } - callbackQueue_.push_back(callback_); - notEmpty_.notify_one(); -} - void DmCommonEventManager::EventSubscriber::OnReceiveEvent(const CommonEventData &data) { std::string receiveEvent = data.GetWant().GetAction(); @@ -140,13 +118,21 @@ void DmCommonEventManager::EventSubscriber::OnReceiveEvent(const CommonEventData return; } + int32_t userId = data.GetCode(); + if (userId <= 0) { + LOGE("userId is less zero"); + return; + } + std::unique_lock callbackQueueMutexLock(callbackQueueMutex_); if (callbackQueue_.size() > COMMON_CALLBACK_MAX_SIZE) { LOGE("event callback Queue is too long"); return; } - callbackQueue_.push_back(callback_); + + CommomEventCallbackNode node {userId, callback_}; + callbackQueue_.push_back(node); notEmpty_.notify_one(); } } // namespace DistributedHardware -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp b/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp index f1ce2b3ce0311acecdd30fd783e7a9c3399d510b..22a50c39d2031d6ae48e308e4d656ebe56fbfa3d 100644 --- a/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp +++ b/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp @@ -26,6 +26,7 @@ #include "dm_log.h" #include "dm_random.h" #include "hichain_connector_callback.h" +#include "multiple_user_connector.h" #include "nlohmann/json.hpp" #include "parameter.h" @@ -111,7 +112,13 @@ int32_t HiChainConnector::CreateGroup(int64_t requestId, const std::string &grou jsonObj[FIELD_USER_TYPE] = 0; jsonObj[FIELD_GROUP_VISIBILITY] = GROUP_VISIBILITY_PUBLIC; jsonObj[FIELD_EXPIRE_TIME] = FIELD_EXPIRE_TIME_VALUE; - int32_t ret = deviceGroupManager_->createGroup(requestId, DM_PKG_NAME.c_str(), jsonObj.dump().c_str()); + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + if (userId < 0) { + LOGE("get current process account user id failed"); + return DM_FAILED; + } + + int32_t ret = deviceGroupManager_->createGroup(userId, requestId, DM_PKG_NAME.c_str(), jsonObj.dump().c_str()); if (ret != 0) { LOGE("Failed to start CreateGroup task, ret: %d, requestId %lld.", ret, requestId); return DM_HICHAIN_GROUP_CREATE_FAILED; @@ -136,8 +143,46 @@ int32_t HiChainConnector::GetGroupInfo(std::string queryParams, std::vectorgetGroupInfo(userId, DM_PKG_NAME.c_str(), queryParams.c_str(), &groupVec, &num); + if (ret != 0) { + LOGE("HiChainConnector::GetGroupInfo failed , ret: %d.", ret); + return false; + } + if (groupVec == nullptr) { + LOGE("HiChainConnector::GetGroupInfo failed , returnGroups is nullptr"); + return false; + } + if (num == 0) { + LOGE("HiChainConnector::GetGroupInfo group failed, groupNum is 0."); + return false; + } + LOGI("HiChainConnector::GetGroupInfo group(%s), groupNum(%d)", groupVec, num); + std::string relatedGroups = std::string(groupVec); + deviceGroupManager_->destroyInfo(&groupVec); + nlohmann::json jsonObject = nlohmann::json::parse(relatedGroups); + if (jsonObject.is_discarded()) { + LOGE("returnGroups parse error"); + return false; + } + std::vector groupInfos = jsonObject.get>(); + if (groupInfos.size() == 0) { + LOGE("HiChainConnector::GetGroupInfo group failed, groupInfos is empty."); + return false; + } + groupList = groupInfos; + return true; +} - int32_t ret = deviceGroupManager_->getGroupInfo(DM_PKG_NAME.c_str(), queryParams.c_str(), &groupVec, &num); +int32_t HiChainConnector::GetGroupInfo(const int32_t userId, std::string queryParams, std::vector &groupList) +{ + char *groupVec = nullptr; + uint32_t num = 0; + int32_t ret = deviceGroupManager_->getGroupInfo(userId, DM_PKG_NAME.c_str(), queryParams.c_str(), &groupVec, &num); if (ret != 0) { LOGE("HiChainConnector::GetGroupInfo failed , ret: %d.", ret); return false; @@ -195,8 +240,12 @@ int32_t HiChainConnector::AddMember(std::string deviceId, std::string &connectIn jsonObj[FIELD_CONNECT_PARAMS] = connectInfomation.c_str(); std::string tmpStr = jsonObj.dump(); int64_t requestId = jsonObject[TAG_REQUEST_ID]; - int32_t ret = deviceGroupManager_->addMemberToGroup(requestId, DM_PKG_NAME.c_str(), tmpStr.c_str()); - LOGI("HiChainConnector::tmpStr ::%s", tmpStr.c_str()); + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + if (userId < 0) { + LOGE("get current process account user id failed"); + return DM_FAILED; + } + int32_t ret = deviceGroupManager_->addMemberToGroup(userId, requestId, DM_PKG_NAME.c_str(), tmpStr.c_str()); LOGI("HiChainConnector::AddMember completed"); return ret; } @@ -293,8 +342,13 @@ int32_t HiChainConnector::GetRelatedGroups(std::string deviceId, std::vectorgetRelatedGroups(DM_PKG_NAME.c_str(), deviceId.c_str(), &returnGroups, &groupNum); + deviceGroupManager_->getRelatedGroups(userId, DM_PKG_NAME.c_str(), deviceId.c_str(), &returnGroups, &groupNum); if (ret != 0) { LOGE("HiChainConnector::GetRelatedGroups faild , ret: %d.", ret); return DM_FAILED; @@ -389,7 +443,13 @@ int32_t HiChainConnector::DelMemberFromGroup(std::string groupId, std::string de jsonObj[FIELD_GROUP_ID] = groupId; jsonObj[FIELD_DELETE_ID] = deviceId; std::string deleteParams = jsonObj.dump(); - int32_t ret = deviceGroupManager_->deleteMemberFromGroup(requestId, DM_PKG_NAME.c_str(), deleteParams.c_str()); + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + if (userId < 0) { + LOGE("get current process account user id failed"); + return DM_FAILED; + } + int32_t ret = deviceGroupManager_->deleteMemberFromGroup(userId, requestId, + DM_PKG_NAME.c_str(), deleteParams.c_str()); if (ret != 0) { LOGE("HiChainConnector::DelMemberFromGroup failed , ret: %d.", ret); return ret; @@ -403,7 +463,27 @@ int32_t HiChainConnector::DeleteGroup(std::string &groupId) nlohmann::json jsonObj; jsonObj[FIELD_GROUP_ID] = groupId; std::string disbandParams = jsonObj.dump(); - int32_t ret = deviceGroupManager_->deleteGroup(requestId, DM_PKG_NAME.c_str(), disbandParams.c_str()); + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + if (userId < 0) { + LOGE("get current process account user id failed"); + return DM_FAILED; + } + + int32_t ret = deviceGroupManager_->deleteGroup(userId, requestId, DM_PKG_NAME.c_str(), disbandParams.c_str()); + if (ret != 0) { + LOGE("HiChainConnector::DeleteGroup failed , ret: %d.", ret); + return DM_FAILED; + } + return DM_OK; +} + +int32_t HiChainConnector::DeleteGroup(const int32_t userId, std::string &groupId) +{ + int64_t requestId = GenRequestId(); + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_ID] = groupId; + std::string disbandParams = jsonObj.dump(); + int32_t ret = deviceGroupManager_->deleteGroup(userId, requestId, DM_PKG_NAME.c_str(), disbandParams.c_str()); if (ret != 0) { LOGE("HiChainConnector::DeleteGroup failed , ret: %d.", ret); return DM_FAILED; diff --git a/services/devicemanagerservice/src/dependency/multipleuser/multiple_user_connector.cpp b/services/devicemanagerservice/src/dependency/multipleuser/multiple_user_connector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4168ce99e76bc27fcbde613d5885c5f965145e1f --- /dev/null +++ b/services/devicemanagerservice/src/dependency/multipleuser/multiple_user_connector.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 "multiple_user_connector.h" + +#include "dm_constants.h" +#include "dm_log.h" +#include "os_account_manager.h" + +using namespace OHOS::AccountSA; + +namespace OHOS { +namespace DistributedHardware { +int32_t MultipleUserConnector::oldUserId_ = -1; + +int32_t MultipleUserConnector::GetCurrentAccountUserID(void) +{ + std::vector ids; + ErrCode ret = OsAccountManager::QueryActiveOsAccountIds(ids); + if (ret != ERR_OK || ids.empty()) { + return -1; + } + return ids[0]; +} + +void MultipleUserConnector::SetSwitchOldUserId(int32_t userId) +{ + oldUserId_ = userId; +} + +int32_t MultipleUserConnector::GetSwitchOldUserId(void) +{ + return oldUserId_; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/devicemanagerservice/src/device_manager_service.cpp b/services/devicemanagerservice/src/device_manager_service.cpp index 7c1fae22cfdd9db05347e96b48eef447ada3bf6d..8a02d72652ff492a4d90a471db1de89b8175c9a4 100644 --- a/services/devicemanagerservice/src/device_manager_service.cpp +++ b/services/devicemanagerservice/src/device_manager_service.cpp @@ -17,12 +17,13 @@ #include +#include "common_event_support.h" #include "device_manager_service_listener.h" +#include "dm_common_event_manager.h" #include "dm_constants.h" #include "dm_device_info_manager.h" #include "dm_log.h" -#include "common_event_support.h" -#include "dm_common_event_manager.h" +#include "multiple_user_connector.h" using namespace OHOS::EventFwk; @@ -99,11 +100,20 @@ int32_t DeviceManagerService::Init() softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authMgr_); hiChainConnector_->RegisterHiChainCallback(authMgr_); } + + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + if (userId > 0) { + LOGI("get current account user id success"); + MultipleUserConnector::SetSwitchOldUserId(userId); + } + DmCommonEventManager &dmCommonEventManager = DmCommonEventManager::GetInstance(); - CommomEventCallback callback = std::bind(&DmAuthManager::UserSwitchEventCallback, *authMgr_.get()); + CommomEventCallback callback = std::bind(&DmAuthManager::UserSwitchEventCallback, *authMgr_.get(), + std::placeholders::_1); if (dmCommonEventManager.SubscribeServiceEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED, callback)) { - LOGI("subscribe service event success"); + LOGI("subscribe service user switch common event success"); } + LOGI("Init success, singleton initialized"); intFlag_ = true; return DM_OK;