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