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 
24 namespace OHOS {
25 namespace NetManagerStandard {
GetBundleMgrProxy()26 sptr<AppExecFwk::BundleMgrProxy> GetBundleMgrProxy()
27 {
28     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
29     if (!systemAbilityManager) {
30         NETMGR_LOG_E("fail to get system ability mgr.");
31         return nullptr;
32     }
33 
34     auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
35     if (!remoteObject) {
36         NETMGR_LOG_E("fail to get bundle manager proxy.");
37         return nullptr;
38     }
39     return iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
40 }
41 
GetJsonFromBundle(std::string & jsonProfile)42 int32_t NetBundleImpl::GetJsonFromBundle(std::string &jsonProfile)
43 {
44     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
45     if (bundleMgrProxy == nullptr) {
46         NETMGR_LOG_E("Failed to get bundle manager proxy.");
47         return NETMANAGER_ERR_INTERNAL;
48     }
49     AppExecFwk::BundleInfo bundleInfo;
50     auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo);
51     if (ret != ERR_OK) {
52         NETMGR_LOG_E("GetSelfBundleName: bundleName get fail.");
53         return NETMANAGER_ERR_INTERNAL;
54     }
55     ret = bundleMgrProxy->GetJsonProfile(AppExecFwk::ProfileType::NETWORK_PROFILE,
56         bundleInfo.name, bundleInfo.entryModuleName, jsonProfile);
57     if (ret != ERR_OK) {
58         NETMGR_LOG_D("No network_config profile configured in bundle manager.[%{public}d]", ret);
59         return NETMANAGER_SUCCESS;
60     }
61     return NETMANAGER_SUCCESS;
62 }
63 
IsAtomicService(std::string & bundleName)64 bool NetBundleImpl::IsAtomicService(std::string &bundleName)
65 {
66     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
67     if (bundleMgrProxy == nullptr) {
68         NETMGR_LOG_E("Failed to get bundle manager proxy.");
69         return false;
70     }
71     AppExecFwk::BundleInfo bundleInfo;
72     auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
73     auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
74     if (ret != ERR_OK) {
75         NETMGR_LOG_E("GetSelfBundleName: bundleName get fail.");
76         return false;
77     }
78     bundleName = bundleInfo.applicationInfo.bundleName;
79     return bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::ATOMIC_SERVICE;
80 }
81 
ObtainBundleNameForSelf()82 std::optional<std::string> NetBundleImpl::ObtainBundleNameForSelf()
83 {
84     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
85     if (bundleMgrProxy == nullptr) {
86         NETMGR_LOG_E("Failed to get bundle manager proxy.");
87         return std::nullopt;
88     }
89     AppExecFwk::BundleInfo bundleInfo;
90     auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
91     auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
92     if (ret != ERR_OK) {
93         NETMGR_LOG_E("bundleName get failed %{public}d.", ret);
94         return std::nullopt;
95     }
96     return bundleInfo.applicationInfo.bundleName;
97 }
98 
ObtainTargetApiVersionForSelf()99 std::optional<int32_t> NetBundleImpl::ObtainTargetApiVersionForSelf()
100 {
101     static constexpr int32_t API_VERSION_MOD = 1000;
102     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
103     if (bundleMgrProxy == nullptr) {
104         NETMGR_LOG_E("Failed to get bundle manager proxy.");
105         return std::nullopt;
106     }
107     AppExecFwk::BundleInfo bundleInfo;
108     auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
109     auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
110     if (ret != ERR_OK) {
111         NETMGR_LOG_E("GetBundleInfoForSelf: bundleName get failed %{public}d.", ret);
112         return std::nullopt;
113     }
114     auto targetApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD;
115     NETMGR_LOG_I("Got target API version %{public}d.", targetApiVersion);
116     return targetApiVersion;
117 }
118 
GetNetBundle()119 INetBundle *GetNetBundle()
120 {
121     static NetBundleImpl impl;
122     return &impl;
123 }
124 
IsAtomicService(std::string & bundleName)125 bool IsAtomicService(std::string &bundleName)
126 {
127     NetBundleImpl impl;
128     return impl.IsAtomicService(bundleName);
129 }
130 } // namespace NetManagerStandard
131 } // namespace OHOS
132