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 "device/device_manager_agent.h"
19 #include "dfsu_mount_argument_descriptors.h"
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
25 using namespace std;
26 namespace {
27 static const std::string SAME_ACCOUNT = "account";
28 static const std::string ACCOUNT_LESS = "non_account";
29 static constexpr int DEFAULT_ACCOUNT = 100;
30 } // namespace
31
OsAccountObserver(const AccountSA::OsAccountSubscribeInfo & subscribeInfo)32 OsAccountObserver::OsAccountObserver(const AccountSA::OsAccountSubscribeInfo &subscribeInfo)
33 : OsAccountSubscriber(subscribeInfo)
34 {
35 LOGI("init first to create network of default user");
36 lock_guard<mutex> lock(serializer_);
37 curUsrId = DEFAULT_ACCOUNT;
38 AddMPInfo(curUsrId, SAME_ACCOUNT);
39 AddMPInfo(curUsrId, ACCOUNT_LESS);
40 LOGI("init first to create network of user %{public}d, done", DEFAULT_ACCOUNT);
41 }
42
~OsAccountObserver()43 OsAccountObserver::~OsAccountObserver()
44 {
45 }
46
AddMPInfo(const int id,const std::string & relativePath)47 void OsAccountObserver::AddMPInfo(const int id, const std::string &relativePath)
48 {
49 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(id, relativePath));
50 auto dm = DeviceManagerAgent::GetInstance();
51 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent, weak_ptr<MountPoint>>>(&DeviceManagerAgent::JoinGroup, smp));
52 mountPoints_[id].emplace_back(smp);
53 }
54
OnAccountsChanged(const int & id)55 void OsAccountObserver::OnAccountsChanged(const int &id)
56 {
57 LOGI("user id changed to %{public}d", id);
58 lock_guard<mutex> lock(serializer_);
59 if (curUsrId != -1) {
60 // first stop curUsrId network
61 RemoveMPInfo(curUsrId);
62 }
63
64 // then start new network
65 curUsrId = id;
66 AddMPInfo(id, SAME_ACCOUNT);
67 AddMPInfo(id, ACCOUNT_LESS);
68 LOGI("user id %{public}d, add network done", curUsrId);
69 }
70
RemoveMPInfo(const int id)71 void OsAccountObserver::RemoveMPInfo(const int id)
72 {
73 auto iter = mountPoints_.find(id);
74 if (iter == mountPoints_.end()) {
75 LOGE("user id %{public}d not find in map", curUsrId);
76 return;
77 }
78
79 auto dm = DeviceManagerAgent::GetInstance();
80 for (auto smp : iter->second) {
81 dm->Recv(make_unique<DfsuCmd<DeviceManagerAgent, weak_ptr<MountPoint>>>(&DeviceManagerAgent::QuitGroup, smp));
82 }
83 mountPoints_.erase(iter);
84
85 LOGE("remove mount info of user id %{public}d", id);
86 }
87 } // namespace DistributedFile
88 } // namespace Storage
89 } // namespace OHOS