1 /*
2 * Copyright (c) 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 <cstdint>
17 #include <iomanip>
18 #include <sstream>
19 #include "drm_dfx_utils.h"
20 #include "drm_log.h"
21 #include "iservice_registry.h"
22 #include "bundle_mgr_interface.h"
23 #include "bundle_mgr_proxy.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace DrmStandard {
GetClientBundleName(int32_t uid)28 std::string __attribute__((visibility("default"))) GetClientBundleName(int32_t uid)
29 {
30 std::string bundleName = "";
31 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32 if (samgr == nullptr) {
33 DRM_ERR_LOG("Get ability manager failed");
34 return bundleName;
35 }
36
37 sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
38 if (object == nullptr) {
39 DRM_ERR_LOG("object is NULL.");
40 return bundleName;
41 }
42
43 sptr<OHOS::AppExecFwk::IBundleMgr> bms = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
44 if (bms == nullptr) {
45 DRM_ERR_LOG("bundle manager service is NULL.");
46 return bundleName;
47 }
48
49 auto result = bms->GetNameForUid(uid, bundleName);
50 if (result != ERR_OK) {
51 DRM_ERR_LOG("GetBundleNameForUid fail");
52 return "";
53 }
54
55 DRM_INFO_LOG("BundleName:%{public}s", bundleName.c_str());
56
57 return bundleName;
58 }
59
GetClientBundleInfoTargetVersion(const std::string & bundleName,int32_t uid)60 uint8_t __attribute__((visibility("default"))) GetClientBundleInfoTargetVersion(const std::string &bundleName,
61 int32_t uid)
62 {
63 if (bundleName.empty()) {
64 DRM_ERR_LOG("bundleName is empty.");
65 return 0;
66 }
67 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
68 if (samgr == nullptr) {
69 DRM_ERR_LOG("Get ability manager failed");
70 return 0;
71 }
72
73 sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
74 if (object == nullptr) {
75 DRM_ERR_LOG("object is NULL.");
76 return 0;
77 }
78
79 sptr<OHOS::AppExecFwk::IBundleMgr> bms = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
80 if (bms == nullptr) {
81 DRM_ERR_LOG("bundle manager service is NULL.");
82 return 0;
83 }
84
85 auto flags = (
86 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY) |
87 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) |
88 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE));
89
90 AppExecFwk::BundleInfo bundleInfo;
91 auto result = bms->GetBundleInfoV9(bundleName, flags, bundleInfo, uid / AppExecFwk::Constants::BASE_USER_RANGE);
92 if (result != ERR_OK) {
93 DRM_ERR_LOG("GetBundleInfoV9 err:%{public}d, bundleName: %{public}s", result, bundleName.c_str());
94 return 0;
95 }
96 uint8_t targetVersion = static_cast<uint8_t>(bundleInfo.targetVersion % 100); // % 100 to get the real version
97
98 DRM_INFO_LOG("targetVersion: %{public}u", targetVersion);
99
100 return targetVersion;
101 }
102
CastToHexString(std::vector<uint8_t> binaryData)103 std::string __attribute__((visibility("default"))) CastToHexString(std::vector<uint8_t> binaryData)
104 {
105 std::string hexString;
106 for (uint8_t binary : binaryData) {
107 std::stringstream stream;
108 stream << std::hex << std::setw(minimumDigit) << std::setfill('0') << static_cast<int>(binary);
109 hexString += stream.str();
110 }
111 return hexString;
112 }
113
CalculateTimeDiff(std::chrono::system_clock::time_point timeBefore,std::chrono::system_clock::time_point timeAfter)114 uint32_t __attribute__((visibility("default"))) CalculateTimeDiff(std::chrono::system_clock::time_point timeBefore,
115 std::chrono::system_clock::time_point timeAfter)
116 {
117 auto duration = timeAfter - timeBefore;
118 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
119 return static_cast<uint32_t>(milliseconds.count());
120 }
121 } // namespace DrmStandard
122 } // namespace OHOS