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
GetNetBundle()64 INetBundle *GetNetBundle()
65 {
66 static NetBundleImpl impl;
67 return &impl;
68 }
69 } // namespace NetManagerStandard
70 } // namespace OHOS
71