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 "sys_cap_util.h"
17 #include "string_util.h"
18 #include "window_manager_hilog.h"
19 
20 #include <accesstoken_kit.h>
21 #include <bundle_mgr_interface.h>
22 #include <ipc_skeleton.h>
23 #include <iservice_registry.h>
24 #include <system_ability_definition.h>
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SysCapUtil"};
30 const uint32_t API_VERSION_MOD = 1000;
31 }
32 
GetClientName()33 std::string SysCapUtil::GetClientName()
34 {
35     std::string bn = GetBundleName();
36     if (!bn.empty()) {
37         WLOGFD("bundle name [%{public}s]", bn.c_str());
38         return bn;
39     }
40 
41     std::string pn = GetProcessName();
42     if (!pn.empty()) {
43         WLOGFD("process name [%{public}s]", pn.c_str());
44         return pn;
45     }
46 
47     WLOGFD("unknown name");
48     return "unknown";
49 }
50 
GetBundleName()51 std::string SysCapUtil::GetBundleName()
52 {
53     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
54         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
56         systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
57     sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
58     if (iBundleMgr == nullptr) {
59         WLOGFW("IBundleMgr is null");
60         return "";
61     }
62 
63     std::string bundleName = "";
64     AppExecFwk::BundleInfo bundleInfo;
65     if (iBundleMgr->GetBundleInfoForSelf(0, bundleInfo) == ERR_OK) {
66         bundleName = bundleInfo.name;
67     } else {
68         WLOGFW("Call for GetBundleInfoForSelf failed");
69     }
70     return StringUtil::Trim(bundleName);
71 }
72 
GetApiCompatibleVersion()73 uint32_t SysCapUtil::GetApiCompatibleVersion()
74 {
75     uint32_t apiCompatibleVersion = 0;
76     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
77         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
78     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
79         systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
80     sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
81     if (iBundleMgr == nullptr) {
82         WLOGFW("IBundleMgr is null");
83         return apiCompatibleVersion;
84     }
85     AppExecFwk::BundleInfo bundleInfo;
86     if (iBundleMgr->GetBundleInfoForSelf(0, bundleInfo) == ERR_OK) {
87         apiCompatibleVersion = bundleInfo.targetVersion % API_VERSION_MOD;
88         WLOGFD("targetVersion: [%{public}u], apiCompatibleVersion: [%{public}u]", bundleInfo.targetVersion,
89             apiCompatibleVersion);
90     } else {
91         WLOGFW("Call for GetApiCompatibleVersion failed");
92     }
93     return apiCompatibleVersion;
94 }
95 
GetProcessName()96 std::string SysCapUtil::GetProcessName()
97 {
98     OHOS::Security::AccessToken::NativeTokenInfo info;
99     if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(IPCSkeleton::GetCallingTokenID(), info) != 0) {
100         WLOGFW("get token info failed");
101         return "";
102     }
103     return StringUtil::Trim(info.processName);
104 }
105 } // namespace Rosen
106 } // namespace OHOS