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 "edm_bundle_manager_impl.h"
17
18 #include "application_info.h"
19 #include "bundle_mgr_proxy.h"
20 #include "bundle_mgr_interface.h"
21 #include "system_ability_definition.h"
22
23 #include "edm_log.h"
24 #include "edm_sys_manager.h"
25
26 namespace OHOS {
27 namespace EDM {
GetNameForUid(int uid,std::string & name)28 ErrCode EdmBundleManagerImpl::GetNameForUid(int uid, std::string &name)
29 {
30 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
31 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
32 if (proxy && !proxy->GetNameForUid(uid, name)) {
33 EDMLOGI("EdmBundleManagerImpl::GetNameForUid success.");
34 return ERR_OK;
35 }
36 EDMLOGE("EdmBundleManagerImpl::GetNameForUid GetBundleMgr failed.");
37 return ERR_BUNDLE_SERVICE_ABNORMALLY;
38 }
39
QueryExtensionAbilityInfos(const AAFwk::Want & want,const AppExecFwk::ExtensionAbilityType & extensionType,int32_t flag,int32_t userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)40 bool EdmBundleManagerImpl::QueryExtensionAbilityInfos(const AAFwk::Want &want,
41 const AppExecFwk::ExtensionAbilityType &extensionType, int32_t flag, int32_t userId,
42 std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
43 {
44 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
45 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
46 if (proxy && proxy->QueryExtensionAbilityInfos(want, extensionType, flag, userId, extensionInfos)) {
47 EDMLOGI("EdmBundleManagerImpl::QueryExtensionAbilityInfos success.");
48 return true;
49 }
50 EDMLOGE("EdmBundleManagerImpl::QueryExtensionAbilityInfos GetBundleMgr failed.");
51 return false;
52 }
53
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)54 bool EdmBundleManagerImpl::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
55 AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
56 {
57 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
58 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
59 if (proxy && proxy->GetBundleInfo(bundleName, flag, bundleInfo, userId)) {
60 EDMLOGI("EdmBundleManagerImpl::GetBundleInfo success.");
61 return true;
62 }
63 EDMLOGE("EdmBundleManagerImpl::GetBundleInfo GetBundleMgr failed.");
64 return false;
65 }
66
IsBundleInstalled(const std::string & bundleName,int32_t userId,int32_t appIndex)67 bool EdmBundleManagerImpl::IsBundleInstalled(const std::string &bundleName, int32_t userId, int32_t appIndex)
68 {
69 bool isInstalled = false;
70 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
71 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
72 if (proxy == nullptr) {
73 EDMLOGE("EdmBundleManagerImpl::GetBundleInfo GetBundleMgr failed.");
74 return false;
75 }
76 if (FAILED(proxy->IsBundleInstalled(bundleName, userId, appIndex, isInstalled))) {
77 EDMLOGW("EdmBundleManagerImpl::GetBundleInfo GetBundleMgr IsBundleInstalled failed.");
78 }
79 return isInstalled;
80 }
81
GetApplicationInfo(const std::string & appName,int userId)82 std::string EdmBundleManagerImpl::GetApplicationInfo(const std::string &appName, int userId)
83 {
84 AppExecFwk::ApplicationInfo appInfo;
85 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
86 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
87 if (proxy == nullptr) {
88 EDMLOGE("EdmBundleManagerImpl::GetApplicationInfo GetBundleMgr failed.");
89 return "";
90 }
91 if (!proxy->GetApplicationInfo(appName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfo)) {
92 return "";
93 }
94 return appInfo.appDistributionType;
95 }
96
GetTokenId(const std::string & appName,int userId)97 int32_t EdmBundleManagerImpl::GetTokenId(const std::string &appName, int userId)
98 {
99 AppExecFwk::ApplicationInfo appInfo;
100 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
101 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
102 if (proxy == nullptr) {
103 EDMLOGE("EdmBundleManagerImpl::GetApplicationInfo GetBundleMgr failed.");
104 return 0;
105 }
106 if (!proxy->GetApplicationInfo(appName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfo)) {
107 return 0;
108 }
109 return appInfo.accessTokenId;
110 }
111
AddAppInstallControlRule(std::vector<std::string> & data,AppExecFwk::AppInstallControlRuleType controlRuleType,int32_t userId)112 ErrCode EdmBundleManagerImpl::AddAppInstallControlRule(std::vector<std::string> &data,
113 AppExecFwk::AppInstallControlRuleType controlRuleType, int32_t userId)
114 {
115 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
116 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
117 if (proxy == nullptr) {
118 EDMLOGE("EdmBundleManagerImpl::AddAppInstallControlRule GetBundleMgr failed.");
119 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
120 }
121 auto appControlProxy = proxy->GetAppControlProxy();
122 if (appControlProxy == nullptr) {
123 EDMLOGE("EdmBundleManagerImpl::AddAppInstallControlRule GetAppControlProxy failed.");
124 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
125 }
126 ErrCode res = appControlProxy->AddAppInstallControlRule(data, controlRuleType, userId);
127 if (res != ERR_OK) {
128 EDMLOGE("EdmBundleManagerImpl AddAppInstallControlRule Faild %{public}d:", res);
129 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
130 }
131 return ERR_OK;
132 }
133
DeleteAppInstallControlRule(AppExecFwk::AppInstallControlRuleType controlRuleType,std::vector<std::string> & data,int32_t userId)134 ErrCode EdmBundleManagerImpl::DeleteAppInstallControlRule(AppExecFwk::AppInstallControlRuleType controlRuleType,
135 std::vector<std::string> &data, int32_t userId)
136 {
137 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
138 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
139 if (proxy == nullptr) {
140 EDMLOGE("EdmBundleManagerImpl::DeleteAppInstallControlRule GetBundleMgr failed.");
141 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
142 }
143 auto appControlProxy = proxy->GetAppControlProxy();
144 if (appControlProxy == nullptr) {
145 EDMLOGE("EdmBundleManagerImpl::AddAppInstallControlRule GetAppControlProxy failed.");
146 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
147 }
148 ErrCode res = appControlProxy->DeleteAppInstallControlRule(controlRuleType, data, userId);
149 if (res != ERR_OK) {
150 EDMLOGE("EdmBundleManagerImpl DeleteAppInstallControlRule Faild %{public}d:", res);
151 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
152 }
153 return ERR_OK;
154 }
155 } // namespace EDM
156 } // namespace OHOS