• 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 
HandleConfiguration(const int32_t userId,const Configuration & config,std::vector<std::string> & changeKeyV,bool & isNotifyUser0)64 void MultiUserConfigurationMgr::HandleConfiguration(
65     const int32_t userId, const Configuration& config, std::vector<std::string>& changeKeyV, bool &isNotifyUser0)
66 {
67     std::lock_guard<std::mutex> guard(multiUserConfigurationMutex_);
68     isNotifyUser0 = false;
69     if (userId == -1) {
70         if (globalConfiguration_ == nullptr) {
71             TAG_LOGE(AAFwkTag::APPMGR, "globalConfiguration_ null");
72             return;
73         }
74         {
75             HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "globalConfiguration_->CompareDifferent");
76             globalConfiguration_->CompareDifferent(changeKeyV, config);
77         }
78         if (changeKeyV.size() != 0) {
79             HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "globalConfiguration_->Merge");
80             globalConfiguration_->Merge(changeKeyV, config);
81         }
82         UpdateMultiUserConfiguration(config);
83     } else {
84         auto it = multiUserConfiguration_.find(userId);
85         if (it != multiUserConfiguration_.end()) {
86             it->second.CompareDifferent(changeKeyV, config);
87             if (changeKeyV.size() != 0) {
88                 it->second.Merge(changeKeyV, config);
89             }
90         } else {
91             if (globalConfiguration_ == nullptr) {
92                 TAG_LOGE(AAFwkTag::APPMGR, "globalConfiguration_ null");
93                 return;
94             }
95             Configuration userConfig = *globalConfiguration_;
96             userConfig.CompareDifferent(changeKeyV, config);
97             if (changeKeyV.size() != 0) {
98                 userConfig.Merge(changeKeyV, config);
99             }
100             multiUserConfiguration_[userId] = userConfig;
101         }
102         if (userId != USER0 && userId == MultiUserConfigurationMgr::GetForegroundOsAccountLocalId()) {
103             std::vector<std::string> diff;
104             multiUserConfiguration_[USER0].CompareDifferent(diff, multiUserConfiguration_[userId]);
105             if (diff.size() != 0) {
106                 multiUserConfiguration_[USER0].Merge(diff, multiUserConfiguration_[userId]);
107                 isNotifyUser0 = true;
108             }
109         }
110     }
111 }
112 
UpdateMultiUserConfiguration(const Configuration & config)113 void MultiUserConfigurationMgr::UpdateMultiUserConfiguration(const Configuration& config)
114 {
115     for (auto& userConfig : multiUserConfiguration_) {
116         std::vector<std::string> diffVe;
117         userConfig.second.CompareDifferent(diffVe, config);
118         if (diffVe.size() != 0) {
119             userConfig.second.Merge(diffVe, config);
120         }
121     }
122 }
123 
UpdateMultiUserConfigurationForGlobal(const Configuration & globalConfig)124 void MultiUserConfigurationMgr::UpdateMultiUserConfigurationForGlobal(const Configuration& globalConfig)
125 {
126     for (auto& userConfig : multiUserConfiguration_) {
127         Configuration globalCfg = globalConfig;
128         std::vector<std::string> diffVe;
129         globalCfg.CompareDifferent(diffVe, userConfig.second);
130         if (diffVe.size() != 0) {
131             globalCfg.Merge(diffVe, userConfig.second);
132         }
133         userConfig.second = globalCfg;
134     }
135 }
136 
GetForegroundOsAccountLocalId()137 int32_t MultiUserConfigurationMgr::GetForegroundOsAccountLocalId()
138 {
139     int32_t foregroundUserId = USER100;
140     auto errNo = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(foregroundUserId);
141     if (errNo != 0) {
142         TAG_LOGE(AAFwkTag::APPMGR, "GetForegroundOsAccountLocalId failed: %{public}d", errNo);
143         foregroundUserId = USER100;
144     }
145     return foregroundUserId;
146 }
147 } // namespace AppExecFwk
148 } // namespace OHOS
149