1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "multiuser/os_account_observer.h"
17
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "device/device_manager_agent.h"
21 #include "dfsu_mount_argument_descriptors.h"
22 #include "utils_log.h"
23
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 using namespace std;
28 namespace {
29 static const std::string SAME_ACCOUNT = "account";
30 static constexpr int DEFAULT_ACCOUNT = 100;
31 } // namespace
32
OsAccountObserver(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)33 OsAccountObserver::OsAccountObserver(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
34 : EventFwk::CommonEventSubscriber(subscribeInfo)
35 {
36 LOGI("init first to create network of default user");
37 lock_guard<mutex> lock(serializer_);
38 curUsrId = DEFAULT_ACCOUNT;
39 AddMPInfo(curUsrId, SAME_ACCOUNT);
40 auto dm = DeviceManagerAgent::GetInstance();
41 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent>>(&DeviceManagerAgent::InitDeviceInfos));
42 LOGI("init first to create network of user %{public}d, done", DEFAULT_ACCOUNT);
43 }
44
~OsAccountObserver()45 OsAccountObserver::~OsAccountObserver()
46 {
47 }
48
AddMPInfo(const int id,const std::string & relativePath)49 void OsAccountObserver::AddMPInfo(const int id, const std::string &relativePath)
50 {
51 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(id, relativePath));
52 auto dm = DeviceManagerAgent::GetInstance();
53 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent, weak_ptr<MountPoint>>>(&DeviceManagerAgent::JoinGroup, smp));
54 mountPoints_[id].emplace_back(smp);
55 }
56
OnReceiveEvent(const EventFwk::CommonEventData & eventData)57 void OsAccountObserver::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
58 {
59 const AAFwk::Want& want = eventData.GetWant();
60 std::string action = want.GetAction();
61 LOGI("AccountSubscriber: OnReceiveEvent action:%{public}s.", action.c_str());
62 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
63 int32_t id = eventData.GetCode();
64 LOGI("user id changed to %{public}d", id);
65 lock_guard<mutex> lock(serializer_);
66 if (curUsrId != -1 && curUsrId != id) {
67 // first stop curUsrId network
68 RemoveMPInfo(curUsrId);
69 } else if (curUsrId != -1 && curUsrId == id) {
70 return;
71 }
72
73 // then start new network
74 curUsrId = id;
75 AddMPInfo(id, SAME_ACCOUNT);
76 LOGI("user id %{public}d, add network done", curUsrId);
77 }
78 }
79
RemoveMPInfo(const int id)80 void OsAccountObserver::RemoveMPInfo(const int id)
81 {
82 auto iter = mountPoints_.find(id);
83 if (iter == mountPoints_.end()) {
84 LOGE("user id %{public}d not find in map", curUsrId);
85 return;
86 }
87
88 auto dm = DeviceManagerAgent::GetInstance();
89 for (auto smp : iter->second) {
90 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent, weak_ptr<MountPoint>>>(&DeviceManagerAgent::QuitGroup, smp));
91 }
92 mountPoints_.erase(iter);
93
94 LOGE("remove mount info of user id %{public}d", id);
95 }
96 } // namespace DistributedFile
97 } // namespace Storage
98 } // namespace OHOS
99