• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_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     HILOG_DEBUG("execute %{public}s without os account subsystem.", __func__);
37     ids.emplace_back(DEFAULT_OS_ACCOUNT_ID);
38     return ERR_OK;
39 #else
40     HILOG_DEBUG("execute %{public}s with os account subsystem.", __func__);
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     HILOG_DEBUG("execute %{public}s without os account subsystem.", __func__);
49     id = uid / UID_TRANSFORM_DIVISOR;
50     return ERR_OK;
51 #else
52     HILOG_DEBUG("execute %{public}s with os account subsystem.", __func__);
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     HILOG_DEBUG("execute %{public}s without os account subsystem.", __func__);
61     id = DEFAULT_OS_ACCOUNT_ID;
62     return ERR_OK;
63 #else
64     HILOG_DEBUG("execute %{public}s with os account subsystem.", __func__);
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     HILOG_DEBUG("execute %{public}s without os account subsystem.", __func__);
73     isOsAccountExists = (id == DEFAULT_OS_ACCOUNT_ID);
74     return ERR_OK;
75 #else // OS_ACCOUNT_PART_ENABLED
76     HILOG_DEBUG("execute %{public}s with os account subsystem.", __func__);
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     HILOG_INFO("execute %{public}s without os account subsystem.", __func__);
85     osAccountUserId = USER_ID_U100;
86     return ERR_OK;
87 #else // OS_ACCOUNT_PART_ENABLED
88     HILOG_INFO("execute %{public}s with os account subsystem.", __func__);
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     HILOG_INFO("execute %{public}s without os account subsystem.", __func__);
101     return ERR_OK;
102 #else // OS_ACCOUNT_PART_ENABLED
103     HILOG_INFO("execute %{public}s with os account subsystem.", __func__);
104     return AccountSA::OsAccountManager::RemoveOsAccount(id);
105 #endif // OS_ACCOUNT_PART_ENABLED
106 }
107 }  // namespace AppExecFwk
108 }  // namespace OHOS