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