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 {
28 constexpr int32_t APP_INDEX_ZERO = 0;
GetNameForUid(int uid,std::string & name)29 ErrCode EdmBundleManagerImpl::GetNameForUid(int uid, std::string &name)
30 {
31     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
32     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
33     if (proxy && !proxy->GetNameForUid(uid, name)) {
34         EDMLOGI("EdmBundleManagerImpl::GetNameForUid success.");
35         return ERR_OK;
36     }
37     EDMLOGE("EdmBundleManagerImpl::GetNameForUid GetBundleMgr failed.");
38     return ERR_BUNDLE_SERVICE_ABNORMALLY;
39 }
40 
QueryExtensionAbilityInfos(const AAFwk::Want & want,const AppExecFwk::ExtensionAbilityType & extensionType,int32_t flag,int32_t userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)41 bool EdmBundleManagerImpl::QueryExtensionAbilityInfos(const AAFwk::Want &want,
42     const AppExecFwk::ExtensionAbilityType &extensionType, int32_t flag, int32_t userId,
43     std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
44 {
45     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
46     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
47     if (proxy && proxy->QueryExtensionAbilityInfos(want, extensionType, flag, userId, extensionInfos)) {
48         EDMLOGI("EdmBundleManagerImpl::QueryExtensionAbilityInfos success.");
49         return true;
50     }
51     EDMLOGE("EdmBundleManagerImpl::QueryExtensionAbilityInfos GetBundleMgr failed.");
52     return false;
53 }
54 
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)55 bool EdmBundleManagerImpl::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
56     AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
57 {
58     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
59     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
60     if (proxy && proxy->GetBundleInfo(bundleName, flag, bundleInfo, userId)) {
61         EDMLOGI("EdmBundleManagerImpl::GetBundleInfo success.");
62         return true;
63     }
64     EDMLOGE("EdmBundleManagerImpl::GetBundleInfo GetBundleMgr failed.");
65     return false;
66 }
67 
IsBundleInstalled(const std::string & bundleName,int32_t userId)68 bool EdmBundleManagerImpl::IsBundleInstalled(const std::string &bundleName, int32_t userId)
69 {
70     bool isInstalled = false;
71     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
72     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
73     if (proxy == nullptr) {
74         EDMLOGE("EdmBundleManagerImpl::GetBundleInfo GetBundleMgr failed.");
75         return false;
76     }
77     if (FAILED(proxy->IsBundleInstalled(bundleName, userId, APP_INDEX_ZERO, isInstalled))) {
78         EDMLOGW("EdmBundleManagerImpl::GetBundleInfo GetBundleMgr IsBundleInstalled failed.");
79     }
80     return isInstalled;
81 }
82 
GetApplicationInfo(const std::string & appName,int userId)83 std::string EdmBundleManagerImpl::GetApplicationInfo(const std::string &appName, int userId)
84 {
85     AppExecFwk::ApplicationInfo appInfo;
86     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
87     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
88     if (proxy == nullptr) {
89         EDMLOGE("EdmBundleManagerImpl::GetApplicationInfo GetBundleMgr failed.");
90         return "";
91     }
92     if (!proxy->GetApplicationInfo(appName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfo)) {
93         return "";
94     }
95     return appInfo.appDistributionType;
96 }
97 } // namespace EDM
98 } // namespace OHOS