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 "net_bundle_impl.h"
17
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 #include "bundle_mgr_proxy.h"
21 #include "net_manager_constants.h"
22 #include "net_mgr_log_wrapper.h"
23 #include "os_account_manager.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
GetBundleMgrProxy()27 sptr<AppExecFwk::BundleMgrProxy> GetBundleMgrProxy()
28 {
29 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
30 if (!systemAbilityManager) {
31 NETMGR_LOG_E("fail to get system ability mgr.");
32 return nullptr;
33 }
34
35 auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
36 if (!remoteObject) {
37 NETMGR_LOG_E("fail to get bundle manager proxy.");
38 return nullptr;
39 }
40 return iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
41 }
42
GetJsonFromBundle(std::string & jsonProfile)43 int32_t NetBundleImpl::GetJsonFromBundle(std::string &jsonProfile)
44 {
45 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
46 if (bundleMgrProxy == nullptr) {
47 NETMGR_LOG_E("Failed to get bundle manager proxy.");
48 return NETMANAGER_ERR_INTERNAL;
49 }
50 AppExecFwk::BundleInfo bundleInfo;
51 auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo);
52 if (ret != ERR_OK) {
53 NETMGR_LOG_I("GetSelfBundleName: bundleName get fail.");
54 return NETMANAGER_ERR_INTERNAL;
55 }
56 ret = bundleMgrProxy->GetJsonProfile(AppExecFwk::ProfileType::NETWORK_PROFILE,
57 bundleInfo.name, bundleInfo.entryModuleName, jsonProfile);
58 if (ret != ERR_OK) {
59 NETMGR_LOG_D("No network_config profile configured in bundle manager.[%{public}d]", ret);
60 return NETMANAGER_SUCCESS;
61 }
62 return NETMANAGER_SUCCESS;
63 }
64
IsAtomicService(std::string & bundleName)65 bool NetBundleImpl::IsAtomicService(std::string &bundleName)
66 {
67 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
68 if (bundleMgrProxy == nullptr) {
69 NETMGR_LOG_E("Failed to get bundle manager proxy.");
70 return false;
71 }
72 AppExecFwk::BundleInfo bundleInfo;
73 auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
74 auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
75 if (ret != ERR_OK) {
76 NETMGR_LOG_E("GetSelfBundleName: bundleName get fail.");
77 return false;
78 }
79 bundleName = bundleInfo.applicationInfo.bundleName;
80 return bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE;
81 }
82
ObtainBundleNameForSelf()83 std::optional<std::string> NetBundleImpl::ObtainBundleNameForSelf()
84 {
85 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
86 if (bundleMgrProxy == nullptr) {
87 NETMGR_LOG_E("Failed to get bundle manager proxy.");
88 return std::nullopt;
89 }
90 AppExecFwk::BundleInfo bundleInfo;
91 auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
92 auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
93 if (ret != ERR_OK) {
94 NETMGR_LOG_E("bundleName get failed %{public}d.", ret);
95 return std::nullopt;
96 }
97 return bundleInfo.applicationInfo.bundleName;
98 }
99
ObtainTargetApiVersionForSelf()100 std::optional<int32_t> NetBundleImpl::ObtainTargetApiVersionForSelf()
101 {
102 static constexpr int32_t API_VERSION_MOD = 1000;
103 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
104 if (bundleMgrProxy == nullptr) {
105 NETMGR_LOG_E("Failed to get bundle manager proxy.");
106 return std::nullopt;
107 }
108 AppExecFwk::BundleInfo bundleInfo;
109 auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
110 auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
111 if (ret != ERR_OK) {
112 NETMGR_LOG_E("GetBundleInfoForSelf: bundleName get failed %{public}d.", ret);
113 return std::nullopt;
114 }
115 auto targetApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD;
116 NETMGR_LOG_I("Got target API version %{public}d.", targetApiVersion);
117 return targetApiVersion;
118 }
119
ObtainBundleInfoForActive()120 std::optional<std::unordered_map<uint32_t, SampleBundleInfo>> NetBundleImpl::ObtainBundleInfoForActive()
121 {
122 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
123 if (bundleMgrProxy == nullptr) {
124 NETMGR_LOG_E("ObtainBundleInfoForActive Failed to get bundle manager proxy.");
125 return std::nullopt;
126 }
127 int32_t userId;
128 if (GetActivatedOsAccountId(userId) != NETMANAGER_SUCCESS) {
129 NETMGR_LOG_E("ObtainBundleInfoForActive Failed to get userid.");
130 return std::nullopt;
131 }
132 auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
133 std::vector<AppExecFwk::BundleInfo> bundleInfos;
134 auto ret = bundleMgrProxy->GetBundleInfosV9(static_cast<int32_t>(flags), bundleInfos, userId);
135 if (ret != ERR_OK) {
136 NETMGR_LOG_E("ObtainBundleInfoForUid Failed GetBundleInfo. ret[%{public}d] userId[%{public}d]", ret,
137 userId);
138 return std::nullopt;
139 }
140 std::unordered_map<uint32_t, SampleBundleInfo> result;
141 for (const auto &bundleInfo : bundleInfos) {
142 result.insert(
143 std::make_pair(static_cast<uint32_t>(bundleInfo.applicationInfo.uid),
144 SampleBundleInfo{static_cast<uint32_t>(bundleInfo.applicationInfo.uid),
145 bundleInfo.applicationInfo.bundleName,
146 bundleInfo.applicationInfo.installSource, bundleInfo.installTime}));
147 }
148 return result;
149 }
150
ObtainBundleInfoForUid(uint32_t uid)151 std::optional<SampleBundleInfo> NetBundleImpl::ObtainBundleInfoForUid(uint32_t uid)
152 {
153 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
154 if (bundleMgrProxy == nullptr) {
155 NETMGR_LOG_E("ObtainBundleInfoForUid Failed to get bundle manager proxy.");
156 return std::nullopt;
157 }
158 std::string bundleName;
159 if (bundleMgrProxy->GetNameForUid(uid, bundleName) != ERR_OK) {
160 NETMGR_LOG_E("ObtainBundleInfoForUid Failed to GetBundleName. uid[%{public}u]", uid);
161 return std::nullopt;
162 }
163 int32_t userId;
164 if (GetActivatedOsAccountId(userId) != NETMANAGER_SUCCESS) {
165 NETMGR_LOG_E("ObtainBundleInfoForUid Failed to GetUserId.");
166 return std::nullopt;
167 }
168 auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
169 AppExecFwk::BundleInfo bundleInfo;
170 auto ret = bundleMgrProxy->GetBundleInfoV9(bundleName, static_cast<int32_t>(flags), bundleInfo, userId);
171 if (ret != ERR_OK) {
172 NETMGR_LOG_E("ObtainBundleInfoForUid Failed. ret[%{public}d] userId[%{public}d], bundleName[%{public}s]", ret,
173 userId, bundleName.c_str());
174 return std::nullopt;
175 }
176 return SampleBundleInfo{bundleInfo.applicationInfo.uid, bundleInfo.applicationInfo.bundleName,
177 bundleInfo.applicationInfo.installSource, bundleInfo.installTime};
178 }
179
GetActivatedOsAccountId(int32_t & userId)180 int32_t NetBundleImpl::GetActivatedOsAccountId(int32_t &userId)
181 {
182 std::vector<int32_t> activatedOsAccountIds;
183 int ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds);
184 if (ret != ERR_OK) {
185 NETMGR_LOG_E("QueryActiveOsAccountIds failed. ret is %{public}d", ret);
186 return NETMANAGER_ERR_INTERNAL;
187 }
188 if (activatedOsAccountIds.empty()) {
189 NETMGR_LOG_E("QueryActiveOsAccountIds is empty");
190 return NETMANAGER_ERR_INTERNAL;
191 }
192 userId = activatedOsAccountIds[0];
193 NETMGR_LOG_I("QueryActiveOsAccountIds is %{public}d", userId);
194 return NETMANAGER_SUCCESS;
195 }
196
GetNetBundle()197 INetBundle *GetNetBundle()
198 {
199 static NetBundleImpl impl;
200 return &impl;
201 }
202
IsAtomicService(std::string & bundleName)203 bool IsAtomicService(std::string &bundleName)
204 {
205 NetBundleImpl impl;
206 return impl.IsAtomicService(bundleName);
207 }
208 } // namespace NetManagerStandard
209 } // namespace OHOS
210