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 #include "work_sched_utils.h"
16
17 #include "errors.h"
18 #include "ohos_account_kits.h"
19 #include "os_account_manager.h"
20 #include "tokenid_kit.h"
21 #include "ipc_skeleton.h"
22 #include "work_sched_hilog.h"
23
24 namespace OHOS {
25 namespace WorkScheduler {
26 const int32_t INVALID_DATA = -1;
GetCurrentAccountId()27 int32_t WorkSchedUtils::GetCurrentAccountId()
28 {
29 std::vector<int32_t> osAccountIds;
30 ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
31 if (ret != ERR_OK) {
32 WS_HILOGE("QueryActiveOsAccountIds failed.");
33 return -1;
34 }
35
36 if (osAccountIds.empty()) {
37 WS_HILOGE("osAccountInfos is empty, no accounts.");
38 return -1;
39 }
40
41 auto iter = std::find_if(osAccountIds.cbegin(), osAccountIds.cend(),
42 [](const int32_t &accountId) { return accountId >= 0; });
43 if (iter != osAccountIds.end()) {
44 return *iter;
45 }
46 WS_HILOGE("GetCurrentAccountId failed, no osAccountIds now.");
47 return -1;
48 }
49
IsIdActive(int32_t id)50 bool WorkSchedUtils::IsIdActive(int32_t id)
51 {
52 std::vector<int32_t> osAccountIds;
53 ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
54 if (ret != ERR_OK) {
55 WS_HILOGE("QueryActiveOsAccountIds failed.");
56 return false;
57 }
58
59 if (osAccountIds.empty()) {
60 WS_HILOGE("osAccountIds is empty, no accounts.");
61 return false;
62 }
63
64 auto iter = std::find_if(osAccountIds.cbegin(), osAccountIds.cend(),
65 [&id](const int32_t &accountId) { return accountId == id; });
66 if (iter != osAccountIds.end()) {
67 return true;
68 }
69 WS_HILOGE("IsIdActive failed, osAccountIds now.");
70 return false;
71 }
72
GetUserIdByUid(int32_t uid)73 int32_t WorkSchedUtils::GetUserIdByUid(int32_t uid)
74 {
75 if (uid <= INVALID_DATA) {
76 WS_HILOGE("uid is illegal: %{public}d", uid);
77 return INVALID_DATA;
78 }
79 const int32_t baseUserRange = 200000;
80 return uid / baseUserRange;
81 }
82
ConvertFullPath(const std::string & partialPath,std::string & fullPath)83 bool WorkSchedUtils::ConvertFullPath(const std::string& partialPath, std::string& fullPath)
84 {
85 if (partialPath.empty() || partialPath.length() >= PATH_MAX) {
86 return false;
87 }
88 char tmpPath[PATH_MAX] = {0};
89 if (realpath(partialPath.c_str(), tmpPath) == nullptr) {
90 return false;
91 }
92 fullPath = tmpPath;
93 return true;
94 }
95
IsSystemApp()96 bool WorkSchedUtils::IsSystemApp()
97 {
98 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
99 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
100 }
101
GetCurrentTimeMs()102 uint64_t WorkSchedUtils::GetCurrentTimeMs()
103 {
104 using namespace std;
105 auto now = chrono::system_clock::now();
106 chrono::milliseconds currentTimeMs = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch());
107 return currentTimeMs.count();
108 }
109 } // namespace WorkScheduler
110 } // namespace OHOS