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 #include "os_account_manager.h" 25 26 namespace OHOS { 27 namespace EDM { GetAppIdByCallingUid(std::string & appId)28ErrCode BundleManagerUtils::GetAppIdByCallingUid(std::string &appId) 29 { 30 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); 31 sptr<AppExecFwk::IBundleMgr> bundleManager = iface_cast<AppExecFwk::IBundleMgr>(remoteObject); 32 if (bundleManager == nullptr) { 33 EDMLOGE("BundleManagerUtils::GetAppIdByCallingUid get bundleManager failed."); 34 return EdmReturnErrCode::SYSTEM_ABNORMALLY; 35 } 36 std::string callingBundleName; 37 int32_t userId = 0; 38 bundleManager->GetNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName); 39 if (FAILED(AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId))) { 40 EDMLOGE("BundleManagerUtils::GetAppIdByCallingUid get userId failed."); 41 return EdmReturnErrCode::SYSTEM_ABNORMALLY; 42 } 43 appId = bundleManager->GetAppIdByBundleName(callingBundleName, userId); 44 return ERR_OK; 45 } 46 } // namespace EDM 47 } // namespace OHOS