1 /* 2 * Copyright (c) 2023 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 "bundle_manager_utils.h" 17 18 #include <bundle_mgr_interface.h> 19 #include <ipc_skeleton.h> 20 #include <system_ability_definition.h> 21 22 #include "edm_log.h" 23 #include "edm_sys_manager.h" 24 #ifdef OS_ACCOUNT_EDM_ENABLE 25 #include "os_account_manager.h" 26 #endif 27 28 namespace OHOS { 29 namespace EDM { GetAppIdByCallingUid(std::string & appId)30ErrCode BundleManagerUtils::GetAppIdByCallingUid(std::string &appId) 31 { 32 #ifdef OS_ACCOUNT_EDM_ENABLE 33 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); 34 sptr<AppExecFwk::IBundleMgr> bundleManager = iface_cast<AppExecFwk::IBundleMgr>(remoteObject); 35 if (bundleManager == nullptr) { 36 EDMLOGE("BundleManagerUtils::GetAppIdByCallingUid get bundleManager failed."); 37 return EdmReturnErrCode::SYSTEM_ABNORMALLY; 38 } 39 std::string callingBundleName; 40 int32_t userId = 0; 41 bundleManager->GetNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName); 42 if (FAILED(AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId))) { 43 EDMLOGE("BundleManagerUtils::GetAppIdByCallingUid get userId failed."); 44 return EdmReturnErrCode::SYSTEM_ABNORMALLY; 45 } 46 appId = bundleManager->GetAppIdByBundleName(callingBundleName, userId); 47 return ERR_OK; 48 #else 49 EDMLOGW("BundleManagerUtils::GetAppIdByCallingUid Unsupported Capabilities."); 50 return EdmReturnErrCode::SYSTEM_ABNORMALLY; 51 #endif 52 } 53 } // namespace EDM 54 } // namespace OHOS