1 /*
2 * Copyright (c) 2025 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 "multi_user_config_mgr.h"
17 #include "hilog_tag_wrapper.h"
18 #include "hitrace_meter.h"
19 #include "i18n_service_ability_client.h"
20 #include "os_account_manager.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 constexpr int32_t USER0 = 0;
26 constexpr int32_t USER100 = 100;
27 }
28
MultiUserConfigurationMgr()29 MultiUserConfigurationMgr::MultiUserConfigurationMgr()
30 : globalConfiguration_(std::make_shared<AppExecFwk::Configuration>())
31 {}
32
InitConfiguration(std::shared_ptr<AppExecFwk::Configuration> config)33 void MultiUserConfigurationMgr::InitConfiguration(std::shared_ptr<AppExecFwk::Configuration> config)
34 {
35 std::lock_guard<std::mutex> guard(multiUserConfigurationMutex_);
36 if (config == nullptr || globalConfiguration_ == nullptr) {
37 TAG_LOGE(AAFwkTag::APPMGR, "config null");
38 return;
39 }
40 std::vector<std::string> diffVe;
41 config->CompareDifferent(diffVe, *globalConfiguration_);
42 if (diffVe.size() != 0) {
43 config->Merge(diffVe, *globalConfiguration_);
44 }
45 globalConfiguration_ = config;
46
47 UpdateMultiUserConfigurationForGlobal(*globalConfiguration_);
48 }
49
GetConfigurationByUserId(const int32_t userId)50 std::shared_ptr<AppExecFwk::Configuration> MultiUserConfigurationMgr::GetConfigurationByUserId(const int32_t userId)
51 {
52 std::lock_guard<std::mutex> guard(multiUserConfigurationMutex_);
53 auto it = multiUserConfiguration_.find(userId);
54 if (it != multiUserConfiguration_.end()) {
55 return std::make_shared<AppExecFwk::Configuration>((it->second));
56 } else {
57 if (globalConfiguration_ == nullptr) {
58 TAG_LOGE(AAFwkTag::APPMGR, "globalConfiguration_ null");
59 return nullptr;
60 }
61 return std::make_shared<AppExecFwk::Configuration>(*globalConfiguration_);
62 }
63 }
64
SetOrUpdateConfigByUserId(const int32_t userId,const Configuration & config,std::vector<std::string> & changeKeyV)65 void MultiUserConfigurationMgr::SetOrUpdateConfigByUserId(
66 const int32_t userId, const Configuration& config, std::vector<std::string>& changeKeyV)
67 {
68 std::lock_guard<std::mutex> guard(multiUserConfigurationMutex_);
69 auto it = multiUserConfiguration_.find(userId);
70 if (it != multiUserConfiguration_.end()) {
71 it->second.CompareDifferent(changeKeyV, config);
72 if (!changeKeyV.empty()) {
73 it->second.Merge(changeKeyV, config);
74 }
75 } else {
76 if (globalConfiguration_ == nullptr) {
77 TAG_LOGE(AAFwkTag::APPMGR, "globalConfiguration_ null");
78 return;
79 }
80 Configuration userConfig = *globalConfiguration_;
81 std::string language = OHOS::Global::I18n::I18nServiceAbilityClient::GetLanguageFromUserId(userId);
82 if (!language.empty()) {
83 userConfig.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language);
84 }
85 userConfig.CompareDifferent(changeKeyV, config);
86 if (!changeKeyV.empty()) {
87 userConfig.Merge(changeKeyV, config);
88 }
89 multiUserConfiguration_[userId] = userConfig;
90 }
91 }
92
HandleConfiguration(const int32_t userId,const Configuration & config,std::vector<std::string> & changeKeyV,bool & isNotifyUser0)93 void MultiUserConfigurationMgr::HandleConfiguration(
94 const int32_t userId, const Configuration& config, std::vector<std::string>& changeKeyV, bool &isNotifyUser0)
95 {
96 isNotifyUser0 = false;
97 if (userId == -1) {
98 std::lock_guard<std::mutex> guard(multiUserConfigurationMutex_);
99 if (globalConfiguration_ == nullptr) {
100 TAG_LOGE(AAFwkTag::APPMGR, "globalConfiguration_ null");
101 return;
102 }
103 {
104 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "globalConfiguration_->CompareDifferent");
105 globalConfiguration_->CompareDifferent(changeKeyV, config);
106 }
107 if (changeKeyV.size() != 0) {
108 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "globalConfiguration_->Merge");
109 globalConfiguration_->Merge(changeKeyV, config);
110 }
111 UpdateMultiUserConfiguration(config);
112 } else {
113 SetOrUpdateConfigByUserId(userId, config, changeKeyV);
114 if (userId != USER0 && userId == MultiUserConfigurationMgr::GetForegroundOsAccountLocalId()) {
115 std::vector<std::string> diff;
116 SetOrUpdateConfigByUserId(USER0, config, diff);
117 if (!diff.empty()) {
118 isNotifyUser0 = true;
119 }
120 }
121 }
122 }
123
UpdateMultiUserConfiguration(const Configuration & config)124 void MultiUserConfigurationMgr::UpdateMultiUserConfiguration(const Configuration& config)
125 {
126 for (auto& userConfig : multiUserConfiguration_) {
127 std::vector<std::string> diffVe;
128 userConfig.second.CompareDifferent(diffVe, config);
129 if (diffVe.size() != 0) {
130 userConfig.second.Merge(diffVe, config);
131 }
132 }
133 }
134
UpdateMultiUserConfigurationForGlobal(const Configuration & globalConfig)135 void MultiUserConfigurationMgr::UpdateMultiUserConfigurationForGlobal(const Configuration& globalConfig)
136 {
137 for (auto& userConfig : multiUserConfiguration_) {
138 Configuration globalCfg = globalConfig;
139 std::vector<std::string> diffVe;
140 globalCfg.CompareDifferent(diffVe, userConfig.second);
141 if (diffVe.size() != 0) {
142 globalCfg.Merge(diffVe, userConfig.second);
143 }
144 userConfig.second = globalCfg;
145 }
146 }
147
GetForegroundOsAccountLocalId()148 int32_t MultiUserConfigurationMgr::GetForegroundOsAccountLocalId()
149 {
150 int32_t foregroundUserId = USER100;
151 auto errNo = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundUserId);
152 if (errNo != 0) {
153 TAG_LOGE(AAFwkTag::APPMGR, "GetForegroundOsAccountLocalId failed: %{public}d", errNo);
154 foregroundUserId = USER100;
155 }
156 return foregroundUserId;
157 }
158 } // namespace AppExecFwk
159 } // namespace OHOS
160