1 /*
2 * Copyright (c) 2022-2024 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 "os_account_manager_wrapper.h"
17
18 #include "hilog_tag_wrapper.h"
19 #ifdef OS_ACCOUNT_PART_ENABLED
20 #include "os_account_manager.h"
21 #endif // OS_ACCOUNT_PART_ENABLED
22
23 namespace OHOS {
24 namespace AppExecFwk {
25 #ifndef OS_ACCOUNT_PART_ENABLED
26 namespace {
27 const int32_t DEFAULT_OS_ACCOUNT_ID = 0; // default id when there is no os_account part
28 const int32_t USER_ID_U100 = 100;
29 const int32_t UID_TRANSFORM_DIVISOR = 200000;
30 }
31 #endif // OS_ACCOUNT_PART_ENABLED
32
QueryActiveOsAccountIds(std::vector<int32_t> & ids)33 ErrCode OsAccountManagerWrapper::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
34 {
35 #ifndef OS_ACCOUNT_PART_ENABLED
36 TAG_LOGD(AAFwkTag::DEFAULT, "Without os account subsystem");
37 ids.emplace_back(DEFAULT_OS_ACCOUNT_ID);
38 return ERR_OK;
39 #else
40 TAG_LOGD(AAFwkTag::DEFAULT, "os account subsystem");
41 return AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
42 #endif // OS_ACCOUNT_PART_ENABLED
43 }
44
GetOsAccountLocalIdFromUid(const int32_t uid,int32_t & id)45 ErrCode OsAccountManagerWrapper::GetOsAccountLocalIdFromUid(const int32_t uid, int32_t &id)
46 {
47 #ifndef OS_ACCOUNT_PART_ENABLED
48 TAG_LOGD(AAFwkTag::DEFAULT, "Without os account subsystem");
49 id = uid / UID_TRANSFORM_DIVISOR;
50 return ERR_OK;
51 #else
52 TAG_LOGD(AAFwkTag::DEFAULT, "os account subsystem");
53 return AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, id);
54 #endif // OS_ACCOUNT_PART_ENABLED
55 }
56
GetOsAccountLocalIdFromProcess(int & id)57 ErrCode OsAccountManagerWrapper::GetOsAccountLocalIdFromProcess(int &id)
58 {
59 #ifndef OS_ACCOUNT_PART_ENABLED
60 TAG_LOGD(AAFwkTag::DEFAULT, "Without os account subsystem");
61 id = DEFAULT_OS_ACCOUNT_ID;
62 return ERR_OK;
63 #else
64 TAG_LOGD(AAFwkTag::DEFAULT, "os account subsystem");
65 return AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(id);
66 #endif // OS_ACCOUNT_PART_ENABLED
67 }
68
IsOsAccountExists(const int id,bool & isOsAccountExists)69 ErrCode OsAccountManagerWrapper::IsOsAccountExists(const int id, bool &isOsAccountExists)
70 {
71 #ifndef OS_ACCOUNT_PART_ENABLED
72 TAG_LOGD(AAFwkTag::DEFAULT, "Without os account subsystem");
73 isOsAccountExists = (id == DEFAULT_OS_ACCOUNT_ID);
74 return ERR_OK;
75 #else // OS_ACCOUNT_PART_ENABLED
76 TAG_LOGD(AAFwkTag::DEFAULT, "os account subsystem");
77 return AccountSA::OsAccountManager::IsOsAccountExists(id, isOsAccountExists);
78 #endif // OS_ACCOUNT_PART_ENABLED
79 }
80
CreateOsAccount(const std::string & name,int32_t & osAccountUserId)81 ErrCode OsAccountManagerWrapper::CreateOsAccount(const std::string &name, int32_t &osAccountUserId)
82 {
83 #ifndef OS_ACCOUNT_PART_ENABLED
84 TAG_LOGD(AAFwkTag::DEFAULT, "Without os account subsystem");
85 osAccountUserId = USER_ID_U100;
86 return ERR_OK;
87 #else // OS_ACCOUNT_PART_ENABLED
88 TAG_LOGD(AAFwkTag::DEFAULT, "os account subsystem");
89 AccountSA::OsAccountInfo osAccountInfo;
90 ErrCode errCode = AccountSA::OsAccountManager::CreateOsAccount(name,
91 AccountSA::OsAccountType::NORMAL, osAccountInfo);
92 osAccountUserId = osAccountInfo.GetLocalId();
93 return errCode;
94 #endif // OS_ACCOUNT_PART_ENABLED
95 }
96
RemoveOsAccount(const int id)97 ErrCode OsAccountManagerWrapper::RemoveOsAccount(const int id)
98 {
99 #ifndef OS_ACCOUNT_PART_ENABLED
100 TAG_LOGD(AAFwkTag::DEFAULT, "Without os account subsystem");
101 return ERR_OK;
102 #else // OS_ACCOUNT_PART_ENABLED
103 TAG_LOGD(AAFwkTag::DEFAULT, "os account subsystem");
104 return AccountSA::OsAccountManager::RemoveOsAccount(id);
105 #endif // OS_ACCOUNT_PART_ENABLED
106 }
107
GetCurrentActiveAccountId()108 int32_t OsAccountManagerWrapper::GetCurrentActiveAccountId()
109 {
110 std::vector<int32_t> accountIds;
111 auto instance = DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance();
112 if (instance == nullptr) {
113 TAG_LOGE(AAFwkTag::DEFAULT, "Get OsAccountManager Failed");
114 return 0;
115 }
116
117 ErrCode ret = instance->QueryActiveOsAccountIds(accountIds);
118 if (ret != ERR_OK) {
119 TAG_LOGE(AAFwkTag::DEFAULT, "Query active id failed");
120 return 0;
121 }
122
123 if (accountIds.empty()) {
124 TAG_LOGE(AAFwkTag::DEFAULT, "account empty");
125 return 0;
126 }
127
128 return accountIds[0];
129 }
130 } // namespace AppExecFwk
131 } // namespace OHOS