1 /*
2 * Copyright (c) 2021 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 const std::string ACCOUNT_LESS = "non_account";
31 static constexpr int DEFAULT_ACCOUNT = 100;
32 } // namespace
33
OsAccountObserver(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)34 OsAccountObserver::OsAccountObserver(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
35 : EventFwk::CommonEventSubscriber(subscribeInfo)
36 {
37 LOGI("init first to create network of default user");
38 lock_guard<mutex> lock(serializer_);
39 curUsrId = DEFAULT_ACCOUNT;
40 AddMPInfo(curUsrId, SAME_ACCOUNT);
41 AddMPInfo(curUsrId, ACCOUNT_LESS);
42 auto dm = DeviceManagerAgent::GetInstance();
43 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent>>(&DeviceManagerAgent::InitDeviceInfos));
44 LOGI("init first to create network of user %{public}d, done", DEFAULT_ACCOUNT);
45 }
46
~OsAccountObserver()47 OsAccountObserver::~OsAccountObserver()
48 {
49 }
50
AddMPInfo(const int id,const std::string & relativePath)51 void OsAccountObserver::AddMPInfo(const int id, const std::string &relativePath)
52 {
53 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(id, relativePath));
54 auto dm = DeviceManagerAgent::GetInstance();
55 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent, weak_ptr<MountPoint>>>(&DeviceManagerAgent::JoinGroup, smp));
56 mountPoints_[id].emplace_back(smp);
57 }
58
OnReceiveEvent(const EventFwk::CommonEventData & eventData)59 void OsAccountObserver::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
60 {
61 const AAFwk::Want& want = eventData.GetWant();
62 std::string action = want.GetAction();
63 LOGI("AccountSubscriber: OnReceiveEvent action:%{public}s.", action.c_str());
64 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
65 int32_t id = eventData.GetCode();
66 LOGI("user id changed to %{public}d", id);
67 lock_guard<mutex> lock(serializer_);
68 if (curUsrId != -1 && curUsrId != id) {
69 // first stop curUsrId network
70 RemoveMPInfo(curUsrId);
71 } else if (curUsrId != -1 && curUsrId == id) {
72 return;
73 }
74
75 // then start new network
76 curUsrId = id;
77 AddMPInfo(id, SAME_ACCOUNT);
78 AddMPInfo(id, ACCOUNT_LESS);
79 LOGI("user id %{public}d, add network done", curUsrId);
80 }
81 }
82
RemoveMPInfo(const int id)83 void OsAccountObserver::RemoveMPInfo(const int id)
84 {
85 auto iter = mountPoints_.find(id);
86 if (iter == mountPoints_.end()) {
87 LOGE("user id %{public}d not find in map", curUsrId);
88 return;
89 }
90
91 auto dm = DeviceManagerAgent::GetInstance();
92 for (auto smp : iter->second) {
93 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent, weak_ptr<MountPoint>>>(&DeviceManagerAgent::QuitGroup, smp));
94 }
95 mountPoints_.erase(iter);
96
97 LOGE("remove mount info of user id %{public}d", id);
98 }
99 } // namespace DistributedFile
100 } // namespace Storage
101 } // namespace OHOS
102