add GetDirByBundleNameAndAppIndex

Signed-off-by: ming-yue-liu1 <liumingyue12@huawei.com>
Change-Id: I19b1ff6589e357063dcff60d1e368ff7effc7db6
This commit is contained in:
ming-yue-liu1 2024-11-08 16:38:54 +08:00
parent 671932b8f4
commit 1c14b83ac5
22 changed files with 524 additions and 8 deletions

View File

@ -43,6 +43,7 @@ ohos_shared_library("appexecfwk_base") {
"src/app_running_control_rule_result.cpp",
"src/application_info.cpp",
"src/bms_json_util.cpp",
"src/bundle_dir.cpp",
"src/bundle_info.cpp",
"src/bundle_pack_info.cpp",
"src/bundle_resource/bundle_resource_info.cpp",

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_BASE_INCLUDE_BUNDLE_DIR_H
#define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_BASE_INCLUDE_BUNDLE_DIR_H
#include <string>
#include "parcel.h"
namespace OHOS {
namespace AppExecFwk {
struct BundleDir : public Parcelable {
std::string bundleName;
int32_t appIndex = 0;
std::string dir;
bool ReadFromParcel(Parcel &parcel);
virtual bool Marshalling(Parcel &parcel) const override;
static BundleDir *Unmarshalling(Parcel &parcel);
std::string ToString() const;
};
} // AppExecFwk
} // OHOS
#endif // FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_BASE_INCLUDE_BUNDLE_DIR_H

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bundle_dir.h"
#include "nlohmann/json.hpp"
#include "string_ex.h"
#include "json_util.h"
#include "parcel_macro.h"
#include <new>
namespace OHOS {
namespace AppExecFwk {
bool BundleDir::ReadFromParcel(Parcel &parcel)
{
bundleName = Str16ToStr8(parcel.ReadString16());
appIndex = parcel.ReadInt32();
dir = Str16ToStr8(parcel.ReadString16());
return true;
}
BundleDir *BundleDir::Unmarshalling(Parcel &parcel)
{
BundleDir *bundleDir = new (std::nothrow) BundleDir();
if (bundleDir && !bundleDir->ReadFromParcel(parcel)) {
APP_LOGW("read from parcel failed");
delete bundleDir;
bundleDir = nullptr;
}
return bundleDir;
}
bool BundleDir::Marshalling(Parcel &parcel) const
{
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(dir));
return true;
}
std::string BundleDir::ToString() const
{
return "[ bundleName = " + bundleName
+ ", appIndex = " + std::to_string(appIndex)
+ ", dir = " + dir
+ "]";
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -191,7 +191,9 @@ enum class BundleMgrInterfaceCode : uint32_t {
IS_BUNDLE_INSTALLED = 165,
GET_COMPATIBLED_DEVICE_TYPE_NATIVE = 166,
GET_COMPATIBLED_DEVICE_TYPE = 167,
GET_BUNDLE_NAME_BY_APP_ID_OR_APP_IDENTIFIER = 168
GET_BUNDLE_NAME_BY_APP_ID_OR_APP_IDENTIFIER = 168,
GET_DIR_BY_BUNDLENAME_AND_APPINDEX = 169,
GET_ALL_BUNDLE_DIRS = 170,
};
/* SAID: 401-85 Interface No.85 subservice also provides the following interfaces */

View File

@ -18,6 +18,7 @@
#include "appexecfwk_errors.h"
#include "bundle_constants.h"
#include "bundle_dir.h"
#include "bundle_info.h"
#include "bundle_pack_info.h"
#include "extension_ability_info.h"
@ -111,6 +112,7 @@ public:
ErrCode GetSandboxHapModuleInfo(const AbilityInfo &abilityInfo, int32_t appIndex, int32_t userId,
HapModuleInfo &hapModuleInfo);
ErrCode GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex, std::string &dataDir);
ErrCode GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs);
private:
static std::shared_ptr<BundleMgrClientImpl> impl_;

View File

@ -20,6 +20,7 @@
#include "appexecfwk_errors.h"
#include "application_info.h"
#include "bundle_dir.h"
#include "bundle_info.h"
#include "bundle_pack_info.h"
#include "bundle_mgr_interface.h"
@ -66,6 +67,7 @@ public:
HapModuleInfo &hapModuleInfo);
ErrCode CreateBundleDataDir(int32_t userId);
ErrCode GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex, std::string &dataDir);
ErrCode GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs);
private:
ErrCode Connect();

View File

@ -845,6 +845,8 @@ private:
ErrCode HandleGetBundleNameByAppId(MessageParcel &data, MessageParcel &reply);
ErrCode HandleGetDirByBundleNameAndAppIndex(MessageParcel &data, MessageParcel &reply);
ErrCode HandleGetAllBundleDirs(MessageParcel &data, MessageParcel &reply);
private:
/**
* @brief Write a parcelabe vector objects to the proxy node.

View File

@ -21,6 +21,7 @@
#include "application_info.h"
#include "app_provision_info.h"
#include "bundle_constants.h"
#include "bundle_dir.h"
#include "bundle_event_callback_interface.h"
#include "bundle_info.h"
#include "bundle_pack_info.h"
@ -1614,6 +1615,17 @@ public:
{
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
virtual ErrCode GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex,
std::string &dataDir)
{
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
virtual ErrCode GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs)
{
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
};
#define WRITE_PARCEL(func) \

View File

@ -1119,6 +1119,10 @@ public:
virtual ErrCode GetBundleNameByAppId(const std::string &appId, std::string &bundleName) override;
virtual ErrCode GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex,
std::string &dataDir) override;
virtual ErrCode GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs) override;
private:
/**
* @brief Send a command message from the proxy object.

View File

@ -211,5 +211,14 @@ ErrCode BundleMgrClient::GetDirByBundleNameAndAppIndex(const std::string &bundle
}
return impl_->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dataDir);
}
ErrCode BundleMgrClient::GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs)
{
if (impl_ == nullptr) {
APP_LOGE("Bundle mgr client impl is nullptr");
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
return impl_->GetAllBundleDirs(userId, bundleDirs);
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -534,14 +534,25 @@ ErrCode BundleMgrClientImpl::GetDirByBundleNameAndAppIndex(const std::string &bu
std::string &dataDir)
{
APP_LOGD("GetDir begin");
if (appIndex < 0) {
return ERR_BUNDLE_MANAGER_GET_DIR_INVALID_APP_INDEX;
} else if (appIndex == 0) {
dataDir = bundleName;
} else {
dataDir = "+clone-" + std::to_string(appIndex) + "+" + bundleName;
ErrCode result = Connect();
if (result != ERR_OK) {
APP_LOGE("connect fail");
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
return ERR_OK;
std::shared_lock<std::shared_mutex> lock(mutex_);
return bundleMgr_->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dataDir);
}
ErrCode BundleMgrClientImpl::GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs)
{
APP_LOGD("GetAllBundleDirs begin");
ErrCode result = Connect();
if (result != ERR_OK) {
APP_LOGE("connect fail");
return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
}
std::shared_lock<std::shared_mutex> lock(mutex_);
return bundleMgr_->GetAllBundleDirs(userId, bundleDirs);
}
ErrCode BundleMgrClientImpl::Connect()

View File

@ -614,6 +614,12 @@ int BundleMgrHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessagePa
case static_cast<uint32_t>(BundleMgrInterfaceCode::GET_BUNDLE_NAME_BY_APP_ID_OR_APP_IDENTIFIER):
errCode = HandleGetBundleNameByAppId(data, reply);
break;
case static_cast<uint32_t>(BundleMgrInterfaceCode::GET_DIR_BY_BUNDLENAME_AND_APPINDEX):
errCode = HandleGetDirByBundleNameAndAppIndex(data, reply);
break;
case static_cast<uint32_t>(BundleMgrInterfaceCode::GET_ALL_BUNDLE_DIRS):
errCode = HandleGetAllBundleDirs(data, reply);
break;
default :
APP_LOGW("bundleMgr host receives unknown code %{public}u", code);
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
@ -4179,5 +4185,42 @@ ErrCode BundleMgrHost::HandleGetBundleNameByAppId(MessageParcel &data, MessagePa
}
return ERR_OK;
}
ErrCode BundleMgrHost::HandleGetDirByBundleNameAndAppIndex(MessageParcel &data, MessageParcel &reply)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
std::string bundleName = data.ReadString();
int32_t appIndex = data.ReadInt32();
std::string dataDir;
auto ret = GetDirByBundleNameAndAppIndex(bundleName, appIndex, dataDir);
if (!reply.WriteInt32(ret)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (!reply.WriteString(dataDir)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
return ERR_OK;
}
ErrCode BundleMgrHost::HandleGetAllBundleDirs(MessageParcel &data, MessageParcel &reply)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
int32_t userId = data.ReadInt32();
std::vector<BundleDir> bundleDirs;
auto ret = GetAllBundleDirs(userId, bundleDirs);
if (!reply.WriteInt32(ret)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (ret == ERR_OK) {
if (!WriteVectorToParcelIntelligent(bundleDirs, reply)) {
APP_LOGE("write failed");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
}
return ERR_OK;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -5397,5 +5397,52 @@ ErrCode BundleMgrProxy::GetBundleNameByAppId(const std::string &appId, std::stri
APP_LOGD("GetBundleNameByAppId: ret: %{public}d, bundleName: %{public}s", ret, bundleName.c_str());
return ret;
}
ErrCode BundleMgrProxy::GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex,
std::string &dataDir)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
MessageParcel data;
if (!data.WriteInterfaceToken(GetDescriptor())) {
APP_LOGE("Write interface token fail");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (!data.WriteString(bundleName)) {
APP_LOGE("Write bundle name fail");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (!data.WriteInt32(appIndex)) {
APP_LOGE("Write appIndex id fail");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
MessageParcel reply;
if (!SendTransactCmd(BundleMgrInterfaceCode::GET_DIR_BY_BUNDLENAME_AND_APPINDEX, data, reply)) {
APP_LOGE("Fail to GetDir from server");
return ERR_BUNDLE_MANAGER_IPC_TRANSACTION;
}
auto ret = reply.ReadInt32();
if (ret == ERR_OK) {
dataDir = reply.ReadString();
}
APP_LOGD("GetDirByBundleNameAndAppIndex: ret: %{public}d, dataDir: %{public}s", ret, dataDir.c_str());
return ret;
}
ErrCode BundleMgrProxy::GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs)
{
HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
MessageParcel data;
if (!data.WriteInterfaceToken(GetDescriptor())) {
APP_LOGE("Write interface token fail");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
if (!data.WriteInt32(userId)) {
APP_LOGE("Write user id fail");
return ERR_APPEXECFWK_PARCEL_ERROR;
}
return GetVectorFromParcelIntelligentWithErrCode<BundleDir>(
BundleMgrInterfaceCode::GET_ALL_BUNDLE_DIRS, data, bundleDirs);
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -282,6 +282,7 @@ ohos_shared_library("libbms") {
"hitrace:hitrace_meter",
"init:libbegetutil",
"ipc:ipc_single",
"os_account:libaccountkits",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"syscap_codec:syscap_interface_shared",

View File

@ -157,6 +157,7 @@ bundle_install_deps = [
bundle_install_external_deps = [
"ffrt:libffrt",
"os_account:libaccountkits",
"storage_service:storage_manager_acl",
"zlib:shared_libz",
]

View File

@ -25,6 +25,7 @@
#include <shared_mutex>
#include <string>
#include "bundle_dir.h"
#include "want.h"
#include "ability_info.h"
@ -54,6 +55,7 @@
#ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
#include "element.h"
#endif
#include "ohos_account_kits.h"
namespace OHOS {
namespace AppExecFwk {
@ -998,6 +1000,15 @@ public:
void CreateEl5Dir(const std::vector<CreateDirParam> &el5Params, bool needSaveStorage = false);
int32_t GetUidByBundleName(const std::string &bundleName, int32_t userId, int32_t appIndex) const;
ErrCode GetBundleNameByAppId(const std::string &appId, std::string &bundleName);
ErrCode GetDirForAtomicService(const std::string &bundleName, AccountSA::OhosAccountInfo &accountInfo,
std::string &dataDir) const;
ErrCode GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex,
std::string &dataDir) const;
ErrCode GetDirByBundleNameAndAppIndexAndType(const std::string &bundleName, const int32_t appIndex,
const BundleType type, AccountSA::OhosAccountInfo &accountInfo, std::string &dataDir) const;
std::vector<int32_t> GetCloneAppIndexesByInnerBundleInfo(const InnerBundleInfo &innerBundleInfo,
int32_t userId) const;
ErrCode GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs) const;
private:
/**

View File

@ -1039,6 +1039,9 @@ public:
virtual ErrCode GetCompatibleDeviceTypeNative(std::string &deviceType) override;
virtual ErrCode GetCompatibleDeviceType(const std::string &bundleName, std::string &deviceType) override;
virtual ErrCode GetBundleNameByAppId(const std::string &appId, std::string &bundleName) override;
virtual ErrCode GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex,
std::string &dataDir) override;
virtual ErrCode GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs) override;
private:
const std::shared_ptr<BundleDataMgr> GetDataMgrFromService();

View File

@ -62,6 +62,7 @@
#include "router_data_storage_rdb.h"
#include "shortcut_data_storage_rdb.h"
#include "ohos_account_kits.h"
namespace OHOS {
namespace AppExecFwk {
@ -107,6 +108,9 @@ const std::map<ProfileType, const char*> PROFILE_TYPE_MAP = {
};
const std::string SCHEME_END = "://";
const std::string LINK_FEATURE = "linkFeature";
const std::string ATOMIC_SERVICE_DIR_PREFIX = "+auid-";
const std::string CLONE_APP_DIR_PREFIX = "+clone-";
const std::string PLUS = "+";
constexpr const char* PARAM_URI_SEPARATOR = ":///";
constexpr const char* URI_SEPARATOR = "://";
constexpr uint8_t PARAM_URI_SEPARATOR_LEN = 4;
@ -8890,5 +8894,109 @@ ErrCode BundleDataMgr::GetBundleNameByAppId(const std::string &appId, std::strin
APP_LOGI("get bundleName failed %{private}s", appId.c_str());
return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
}
ErrCode BundleDataMgr::GetDirForAtomicService(const std::string &bundleName, AccountSA::OhosAccountInfo &accountInfo,
std::string &dataDir) const
{
if (accountInfo.uid_.empty()) {
auto ret = AccountSA::OhosAccountKits::GetInstance().GetOhosAccountInfo(accountInfo);
if (ret != ERR_OK) {
APP_LOGE("GetOhosAccountInfo failed, errCode: %{public}d", ret);
return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
}
}
dataDir = ATOMIC_SERVICE_DIR_PREFIX + accountInfo.uid_ + PLUS + bundleName;
return ERR_OK;
}
ErrCode BundleDataMgr::GetDirByBundleNameAndAppIndexAndType(const std::string &bundleName, const int32_t appIndex,
const BundleType type, AccountSA::OhosAccountInfo &accountInfo, std::string &dataDir) const
{
if (type == BundleType::ATOMIC_SERVICE) {
return GetDirForAtomicService(bundleName, accountInfo, dataDir);
}
if (appIndex == 0) {
dataDir = bundleName;
} else {
dataDir = CLONE_APP_DIR_PREFIX + std::to_string(appIndex) + PLUS + bundleName;
}
return ERR_OK;
}
ErrCode BundleDataMgr::GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex,
std::string &dataDir) const
{
APP_LOGD("start GetDir bundleName : %{public}s appIndex : %{public}d", bundleName.c_str(), appIndex);
if (appIndex < 0) {
return ERR_BUNDLE_MANAGER_GET_DIR_INVALID_APP_INDEX;
}
AccountSA::OhosAccountInfo accountInfo;
BundleType type;
GetBundleType(bundleName, type);
return GetDirByBundleNameAndAppIndexAndType(bundleName, appIndex, type, accountInfo, dataDir);
}
std::vector<int32_t> BundleDataMgr::GetCloneAppIndexesByInnerBundleInfo(const InnerBundleInfo &innerBundleInfo,
int32_t userId) const
{
std::vector<int32_t> cloneAppIndexes;
InnerBundleUserInfo innerBundleUserInfo;
if (!innerBundleInfo.GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
return cloneAppIndexes;
}
const std::map<std::string, InnerBundleCloneInfo> &cloneInfos = innerBundleUserInfo.cloneInfos;
if (cloneInfos.empty()) {
return cloneAppIndexes;
}
for (const auto &cloneInfo : cloneInfos) {
LOG_D(BMS_TAG_QUERY, "get cloneAppIndexes by inner bundle info: %{public}d", cloneInfo.second.appIndex);
cloneAppIndexes.emplace_back(cloneInfo.second.appIndex);
}
return cloneAppIndexes;
}
ErrCode BundleDataMgr::GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs) const
{
APP_LOGD("start GetAllBundleDirs");
int32_t requestUserId = GetUserId(userId);
if (requestUserId == Constants::INVALID_USERID) {
APP_LOGE("invalid userid :%{public}d", userId);
return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
}
AccountSA::OhosAccountInfo accountInfo;
std::shared_lock<std::shared_mutex> lock(bundleInfoMutex_);
for (const auto &item : bundleInfos_) {
const InnerBundleInfo &info = item.second;
std::string bundleName = info.GetBundleName();
int32_t responseUserId = info.GetResponseUserId(requestUserId);
if (responseUserId == Constants::INVALID_USERID) {
APP_LOGD("bundle %{public}s is not installed in user %{public}d or 0", bundleName.c_str(), userId);
continue;
}
BundleType type = info.GetApplicationBundleType();
if (type != BundleType::ATOMIC_SERVICE && type != BundleType::APP) {
continue;
}
std::vector<int32_t> allAppIndexes = {0};
if (type == BundleType::APP) {
std::vector<int32_t> cloneAppIndexes = GetCloneAppIndexesByInnerBundleInfo(info, responseUserId);
allAppIndexes.insert(allAppIndexes.end(), cloneAppIndexes.begin(), cloneAppIndexes.end());
}
for (int32_t appIndex: allAppIndexes) {
std::string dataDir;
if (GetDirByBundleNameAndAppIndexAndType(bundleName, appIndex, type, accountInfo, dataDir) != ERR_OK) {
return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
}
BundleDir bundleDir;
bundleDir.bundleName = bundleName;
bundleDir.appIndex = appIndex;
bundleDir.dir = dataDir;
bundleDirs.emplace_back(bundleDir);
}
}
return ERR_OK;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -4569,5 +4569,43 @@ std::string BundleMgrHostImpl::GetCallerName()
}
return callerName;
}
ErrCode BundleMgrHostImpl::GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex,
std::string &dataDir)
{
APP_LOGD("start GetDirByBundleNameAndAppIndex");
if (!BundlePermissionMgr::IsSystemApp()) {
APP_LOGE("non-system app calling system api");
return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
}
if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
APP_LOGE("Verify permission failed");
return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
}
auto dataMgr = GetDataMgrFromService();
if (dataMgr == nullptr) {
APP_LOGE("DataMgr is nullptr");
return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
}
return dataMgr->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dataDir);
}
ErrCode BundleMgrHostImpl::GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs)
{
if (!BundlePermissionMgr::IsSystemApp()) {
APP_LOGE("non-system app calling system api");
return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
}
if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
APP_LOGE("Verify permission failed");
return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
}
auto dataMgr = GetDataMgrFromService();
if (dataMgr == nullptr) {
APP_LOGE("DataMgr is nullptr");
return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
}
return dataMgr->GetAllBundleDirs(userId, bundleDirs);
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -7589,4 +7589,57 @@ HWTEST_F(BmsBundleDataMgrTest, IsBundleInstalled_0005, Function | SmallTest | Le
EXPECT_TRUE(isInstalled);
}
}
/**
* @tc.number: GetAllBundleDirs_0001
* @tc.name: GetAllBundleDirs
* @tc.desc: test GetAllBundleDirs
*/
HWTEST_F(BmsBundleDataMgrTest, GetAllBundleDirs_0001, Function | SmallTest | Level1)
{
ResetDataMgr();
auto bundleDataMgr = GetBundleDataMgr();
EXPECT_NE(bundleDataMgr, nullptr);
if (bundleDataMgr != nullptr) {
bundleDataMgr->AddUserId(100);
InnerBundleInfo innerBundleInfo;
innerBundleInfo.baseApplicationInfo_->bundleType = BundleType::APP;
innerBundleInfo.baseApplicationInfo_->bundleName = BUNDLE_NAME_TEST;
InnerBundleUserInfo innerBundleUserInfo;
innerBundleInfo.innerBundleUserInfos_.emplace(BUNDLE_NAME_TEST + "_100", innerBundleUserInfo);
bundleDataMgr->bundleInfos_.emplace(BUNDLE_NAME_TEST, innerBundleInfo);
std::vector<BundleDir> bundleDirs;
auto ret = bundleDataMgr->GetAllBundleDirs(100, bundleDirs);
EXPECT_EQ(ret, ERR_OK);
EXPECT_EQ(bundleDirs.size(), 1);
}
}
/**
* @tc.number: GetAllBundleDirs_0002
* @tc.name: GetAllBundleDirs
* @tc.desc: test GetAllBundleDirs
*/
HWTEST_F(BmsBundleDataMgrTest, GetAllBundleDirs_0002, Function | SmallTest | Level1)
{
ResetDataMgr();
auto bundleDataMgr = GetBundleDataMgr();
EXPECT_NE(bundleDataMgr, nullptr);
if (bundleDataMgr != nullptr) {
bundleDataMgr->AddUserId(100);
InnerBundleInfo innerBundleInfo;
innerBundleInfo.baseApplicationInfo_->bundleType = BundleType::SHARED;
innerBundleInfo.baseApplicationInfo_->bundleName = BUNDLE_NAME_TEST;
InnerBundleUserInfo innerBundleUserInfo;
innerBundleInfo.innerBundleUserInfos_.emplace(BUNDLE_NAME_TEST + "_100", innerBundleUserInfo);
bundleDataMgr->bundleInfos_.emplace(BUNDLE_NAME_TEST, innerBundleInfo);
std::vector<BundleDir> bundleDirs;
auto ret = bundleDataMgr->GetAllBundleDirs(100, bundleDirs);
EXPECT_EQ(ret, ERR_OK);
EXPECT_EQ(bundleDirs.size(), 0);
}
}
} // OHOS

View File

@ -9791,5 +9791,25 @@ HWTEST_F(ActsBmsKitSystemTest, GetBundleNameByAppId_0002, Function | MediumTest
EXPECT_EQ(uninstallResult, "Success") << "uninstall fail!";
std::cout << "END GetBundleNameByAppId_0002" << std::endl;
}
/**
* @tc.number: GetAllBundleDirs_0001
* @tc.name: test GetAllBundleDirs interface
* @tc.desc: 1. call GetAllBundleDirs
*/
HWTEST_F(ActsBmsKitSystemTest, GetAllBundleDirs_0001, Function | MediumTest | Level1)
{
std::cout << "START GetAllBundleDirs_0001" << std::endl;
sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
EXPECT_NE(bundleMgrProxy, nullptr);
if (bundleMgrProxy != nullptr) {
std::vector<BundleDir> bundleDirs;
ErrCode ret = bundleMgrProxy->GetAllBundleDirs(0, bundleDirs);
EXPECT_EQ(ret, ERR_OK);
ret = bundleMgrProxy->GetAllBundleDirs(200, bundleDirs);
EXPECT_EQ(ret, ERR_BUNDLE_MANAGER_INVALID_USER_ID);
}
std::cout << "END GetAllBundleDirs_0001" << std::endl;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -2442,10 +2442,14 @@ HWTEST_F(BundleMgrClientSystemTest, CreateBundleDataDir_0100, TestSize.Level1)
HWTEST_F(BundleMgrClientSystemTest, GetDirByBundleNameAndAppIndex_0001, TestSize.Level1)
{
BundleMgrClientImpl impl;
sptr<IBundleMgr> proxy = GetBundleMgrProxy();
impl.bundleMgr_ = proxy;
impl.Connect();
std::string dataDir;
std::string bundleName = "com.example.application";
ErrCode queryResult = impl.GetDirByBundleNameAndAppIndex(bundleName, -1, dataDir);
EXPECT_EQ(queryResult, ERR_BUNDLE_MANAGER_GET_DIR_INVALID_APP_INDEX);
impl.OnDeath();
}
/**
@ -2456,11 +2460,15 @@ HWTEST_F(BundleMgrClientSystemTest, GetDirByBundleNameAndAppIndex_0001, TestSize
HWTEST_F(BundleMgrClientSystemTest, GetDirByBundleNameAndAppIndex_0002, TestSize.Level1)
{
BundleMgrClientImpl impl;
sptr<IBundleMgr> proxy = GetBundleMgrProxy();
impl.bundleMgr_ = proxy;
impl.Connect();
std::string dataDir;
std::string bundleName = "com.example.application";
ErrCode queryResult = impl.GetDirByBundleNameAndAppIndex(bundleName, 1, dataDir);
EXPECT_EQ(queryResult, ERR_OK);
EXPECT_EQ(dataDir, "+clone-1+com.example.application");
impl.OnDeath();
}
/**
@ -2471,11 +2479,49 @@ HWTEST_F(BundleMgrClientSystemTest, GetDirByBundleNameAndAppIndex_0002, TestSize
HWTEST_F(BundleMgrClientSystemTest, GetDirByBundleNameAndAppIndex_0003, TestSize.Level1)
{
BundleMgrClientImpl impl;
sptr<IBundleMgr> proxy = GetBundleMgrProxy();
impl.bundleMgr_ = proxy;
impl.Connect();
std::string dataDir;
std::string bundleName = "com.example.application";
ErrCode queryResult = impl.GetDirByBundleNameAndAppIndex(bundleName, 0, dataDir);
EXPECT_EQ(queryResult, ERR_OK);
EXPECT_EQ(dataDir, "com.example.application");
impl.OnDeath();
}
/**
* @tc.number: GetAllBundleDirs_0001
* @tc.name: GetAllBundleDirs
* @tc.desc: Test GetAllBundleDirs.
*/
HWTEST_F(BundleMgrClientSystemTest, GetAllBundleDirs_0001, TestSize.Level1)
{
BundleMgrClientImpl impl;
sptr<IBundleMgr> proxy = GetBundleMgrProxy();
impl.bundleMgr_ = proxy;
impl.Connect();
std::vector<BundleDir> bundleDirs;
ErrCode queryResult = impl.GetAllBundleDirs(DEFAULT_USERID, bundleDirs);
EXPECT_EQ(queryResult, ERR_OK);
impl.OnDeath();
}
/**
* @tc.number: GetAllBundleDirs_0002
* @tc.name: GetAllBundleDirs
* @tc.desc: Test GetAllBundleDirs.
*/
HWTEST_F(BundleMgrClientSystemTest, GetAllBundleDirs_0002, TestSize.Level1)
{
BundleMgrClientImpl impl;
sptr<IBundleMgr> proxy = GetBundleMgrProxy();
impl.bundleMgr_ = proxy;
impl.Connect();
std::vector<BundleDir> bundleDirs;
ErrCode queryResult = impl.GetAllBundleDirs(-100, bundleDirs);
EXPECT_NE(queryResult, ERR_OK);
impl.OnDeath();
}
} // namespace AppExecFwk
} // namespace OHOS