!1066 依据设备系统参数,优化应用接续业务

Merge pull request !1066 from m30043719/master
This commit is contained in:
openharmony_ci 2024-09-02 14:44:32 +00:00 committed by Gitee
commit 5993af7fb5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
10 changed files with 232 additions and 27 deletions

View File

@ -230,7 +230,10 @@ ohos_shared_library("distributedschedsvr") {
"src/mission/wifi_state_adapter.cpp",
"src/mission/wifi_state_listener.cpp",
]
external_deps += [ "image_framework:image_native" ]
external_deps += [
"config_policy:configpolicy_util",
"image_framework:image_native",
]
}
if (dmsfwk_report_memmgr) {

View File

@ -44,11 +44,16 @@ public:
void SetSyncRecord(const std::string &networkId);
void ClearSyncRecord(const std::string &networkId);
bool IsSynchronized(const std::string &networkId);
bool CheckCtrlRule();
bool CheckBundleContinueConfig(const std::string &bundleName);
private:
void TryTwice(const std::function<DistributedKv::Status()> &func) const;
bool CheckKvStore();
DistributedKv::Status GetKvStore();
bool IsValidPath(const std::string &inFilePath, std::string &realFilePath);
bool UpdateWhiteList(const std::string &cfgJsonStr);
int32_t LoadContinueConfig();
static std::mutex mutex_;
static std::shared_ptr<DmsKvSyncE2E> instance_;
@ -59,6 +64,9 @@ private:
mutable std::mutex kvStorePtrMutex_;
std::atomic<bool> isCfgDevices_ = false;
std::map<std::string, bool> deviceSyncRecord_;
std::atomic<bool> isForbidSendAndRecv_ = false;
std::string continueCfgFullPath_ = "";
std::vector<std::string> whiteList_;
};
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -36,6 +36,7 @@
#include "dtbschedmgr_device_info_storage.h"
#include "dtbschedmgr_log.h"
#include "mission/distributed_bm_storage.h"
#include "mission/dsched_sync_e2e.h"
#include "ipc_skeleton.h"
#include "parcel_helper.h"
#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
@ -700,7 +701,7 @@ int32_t DSchedContinue::GetMissionIdByBundleName()
int32_t DSchedContinue::CheckContinueAbilityPermission()
{
if (!CheckBundleContinueConfig(continueInfo_.sourceBundleName_)) {
if (!DmsKvSyncE2E::GetInstance()->CheckBundleContinueConfig(continueInfo_.sourceBundleName_)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s",
continueInfo_.sourceBundleName_.c_str());
return REMOTE_DEVICE_BIND_ABILITY_ERR;

View File

@ -545,11 +545,6 @@ bool DistributedSchedPermission::CheckMigrateStartCtrlPer(const AppExecFwk::Abil
const CallerInfo& callerInfo, const AAFwk::Want& want) const
{
std::string bundleName = want.GetBundle();
if (!CheckBundleContinueConfig(bundleName)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s", bundleName.c_str());
return false;
}
// check if continuation with same appid
HILOGI("Check migration start control permission with same appid enter.");
if (!targetAbility.visible &&

View File

@ -279,11 +279,6 @@ bool DistributedSchedService::Init()
{
HILOGD("ready to init.");
InitDeviceCfg();
int32_t ret = LoadContinueConfig();
if (ret != ERR_OK) {
HILOGE("Load continue config fail, ret %{public}d.", ret);
}
DmsContinueTime::GetInstance().Init();
DnetworkAdapter::GetInstance()->Init();
if (!DtbschedmgrDeviceInfoStorage::GetInstance().Init()) {
@ -852,7 +847,7 @@ int32_t DistributedSchedService::ContinueLocalMission(const std::string& dstDevi
return INVALID_PARAMETERS_ERR;
}
std::string bundleName = missionInfo.want.GetBundle();
if (!CheckBundleContinueConfig(bundleName)) {
if (!DmsKvSyncE2E::GetInstance()->CheckBundleContinueConfig(bundleName)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s, missionId: %{public}d",
bundleName.c_str(), missionId);
return REMOTE_DEVICE_BIND_ABILITY_ERR;
@ -933,11 +928,6 @@ int32_t DistributedSchedService::ContinueRemoteMission(const std::string& srcDev
{
HILOGI("ContinueRemoteMission srcDeviceId: %{public}s. dstDeviceId: %{public}s. bundleName: %{public}s.",
GetAnonymStr(srcDeviceId).c_str(), GetAnonymStr(dstDeviceId).c_str(), bundleName.c_str());
if (!CheckBundleContinueConfig(bundleName)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s", bundleName.c_str());
return REMOTE_DEVICE_BIND_ABILITY_ERR;
}
if (DmsContinueTime::GetInstance().GetPull()) {
int64_t begin = GetTickCount();
DmsContinueTime::GetInstance().SetDurationBegin(DMSDURATION_DSTTOSRCRPCTIME, begin);

View File

@ -26,6 +26,7 @@
#include "distributed_sched_adapter.h"
#include "dtbschedmgr_device_info_storage.h"
#include "dtbschedmgr_log.h"
#include "mission/dsched_sync_e2e.h"
#include "mission/wifi_state_adapter.h"
#include "parcel_helper.h"
#include "softbus_adapter/softbus_adapter.h"
@ -88,6 +89,10 @@ void DMSContinueRecvMgr::NotifyDataRecv(std::string& senderNetworkId,
{
HILOGI("NotifyDataRecv start, senderNetworkId: %{public}s, dataLen: %{public}u.",
GetAnonymStr(senderNetworkId).c_str(), dataLen);
if (!DmsKvSyncE2E::GetInstance()->CheckCtrlRule()) {
HILOGE("Forbid sending and receiving");
return;
}
bool IsContinueSwitchOn = SwitchStatusDependency::GetInstance().IsContinueSwitchOn();
if (!IsContinueSwitchOn) {
HILOGE("ContinueSwitch status is off");
@ -263,11 +268,6 @@ int32_t DMSContinueRecvMgr::DealOnBroadcastBusiness(const std::string& senderNet
return RetryPostBroadcast(senderNetworkId, bundleNameId, continueTypeId, state, retry);
}
if (!CheckBundleContinueConfig(bundleName)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s", bundleName.c_str());
return REMOTE_DEVICE_BIND_ABILITY_ERR;
}
HILOGI("get bundleName, bundleName: %{public}s", bundleName.c_str());
AppExecFwk::BundleInfo localBundleInfo;
if (BundleManagerInternal::GetLocalBundleInfoV9(bundleName, localBundleInfo) != ERR_OK) {

View File

@ -28,6 +28,7 @@
#include "dtbschedmgr_device_info_storage.h"
#include "dtbschedmgr_log.h"
#include "mission/dms_continue_recv_manager.h"
#include "mission/dsched_sync_e2e.h"
#include "mission/wifi_state_adapter.h"
#include "parcel_helper.h"
#include "softbus_adapter/softbus_adapter.h"
@ -135,6 +136,10 @@ void DMSContinueSendMgr::PostUnfocusedTaskWithDelay(const int32_t missionId, Unf
void DMSContinueSendMgr::NotifyMissionFocused(const int32_t missionId, FocusedReason reason)
{
HILOGI("NotifyMissionFocused called, missionId: %{public}d, reason: %{public}d", missionId, reason);
if (!DmsKvSyncE2E::GetInstance()->CheckCtrlRule()) {
HILOGE("Forbid sending and receiving");
return;
}
if (reason <= FocusedReason::MIN || reason >= FocusedReason::MAX) {
HILOGI("Unknown focusedReason, no need to deal NotifyMissionFocused");
return;
@ -297,7 +302,7 @@ int32_t DMSContinueSendMgr::DealFocusedBusiness(const int32_t missionId, Focused
return REMOTE_DEVICE_BIND_ABILITY_ERR;
}
std::string bundleName = info.want.GetBundle();
if (!CheckBundleContinueConfig(bundleName)) {
if (!DmsKvSyncE2E::GetInstance()->CheckBundleContinueConfig(bundleName)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s, missionId: %{public}d",
bundleName.c_str(), missionId);
return REMOTE_DEVICE_BIND_ABILITY_ERR;
@ -433,7 +438,7 @@ int32_t DMSContinueSendMgr::SendScreenOffEvent(uint8_t type)
HILOGI("start, type: %{public}d, missionId: %{public}d, bundleName: %{public}s, bundleNameId: %{public}u",
type, missionId, bundleName.c_str(), bundleNameId);
if (!CheckBundleContinueConfig(bundleName)) {
if (!DmsKvSyncE2E::GetInstance()->CheckBundleContinueConfig(bundleName)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s, missionId: %{public}d",
bundleName.c_str(), missionId);
return REMOTE_DEVICE_BIND_ABILITY_ERR;
@ -571,7 +576,7 @@ int32_t DMSContinueSendMgr::GetBundleNameIdAndContinueTypeId(const int32_t missi
return ret;
}
HILOGI("get bundleName success, missionId: %{public}d, bundleName: %{public}s", missionId, bundleName.c_str());
if (!CheckBundleContinueConfig(bundleName)) {
if (!DmsKvSyncE2E::GetInstance()->CheckBundleContinueConfig(bundleName)) {
HILOGI("App does not allow continue in config file, bundle name %{public}s, missionId: %{public}d",
bundleName.c_str(), missionId);
return REMOTE_DEVICE_BIND_ABILITY_ERR;

View File

@ -15,8 +15,15 @@
#include "mission/dsched_sync_e2e.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <parameter.h>
#include "config_policy_utils.h"
#include "parameters.h"
#include "securec.h"
namespace OHOS {
namespace DistributedSchedule {
namespace {
@ -26,6 +33,12 @@ const int32_t SLEEP_INTERVAL = 100 * 1000; // 100ms
const int32_t EL1 = 1;
const int32_t MAX_TIMES = 600; // 1min
const char DETERMINE_DEVICE_TYPE_KEY[] = "persist.distributed_scene.sys_settings_data_sync";
static const int32_t FORBID_SEND_FORBID_RECV = 0;
static const int32_t ALLOW_SEND_ALLOW_RECV = 1;
const std::string PARAM_DISTRIBUTED_DATAFILES_TRANS_CTRL = "persist.distributed_scene.datafiles_trans_ctrl";
const std::string CONTINUE_CONFIG_RELATIVE_PATH = "etc/distributedhardware/dms/continue_config.json";
const std::string ALLOW_APP_LIST_KEY = "allow_applist";
constexpr int32_t MAX_CONFIG_PATH_LEN = 1024;
} // namespace
std::shared_ptr<DmsKvSyncE2E> DmsKvSyncE2E::instance_ = nullptr;
@ -68,6 +81,18 @@ void DmsKvSyncE2E::SetDeviceCfg()
HILOGI("Determining the e2e device succeeded.");
isCfgDevices_ = true;
}
auto contralType = OHOS::system::GetIntParameter(PARAM_DISTRIBUTED_DATAFILES_TRANS_CTRL,
ALLOW_SEND_ALLOW_RECV);
HILOGI("contralType=%{public}d", contralType);
if (contralType == FORBID_SEND_FORBID_RECV) {
isForbidSendAndRecv_ = true;
}
int32_t result = LoadContinueConfig();
if (result != ERR_OK) {
HILOGE("Load continue config fail, result %{public}d.", result);
}
}
bool DmsKvSyncE2E::CheckDeviceCfg()
@ -76,6 +101,147 @@ bool DmsKvSyncE2E::CheckDeviceCfg()
return isCfgDevices_;
}
bool DmsKvSyncE2E::CheckCtrlRule()
{
HILOGI("called.");
if (isCfgDevices_ && isForbidSendAndRecv_) {
HILOGI("The device is a special device and checkCtrlRule fail");
return false;
}
return true;
}
bool DmsKvSyncE2E::IsValidPath(const std::string &inFilePath, std::string &realFilePath)
{
char path[PATH_MAX + 1] = { 0 };
if (inFilePath.empty() || inFilePath.length() > PATH_MAX || inFilePath.length() + 1 > MAX_CONFIG_PATH_LEN ||
realpath(inFilePath.c_str(), path) == nullptr) {
HILOGE("Get continue config file real path fail, inFilePath %{public}s.", GetAnonymStr(inFilePath).c_str());
return false;
}
realFilePath = std::string(path);
if (realFilePath.empty()) {
HILOGE("Real file path is empty.");
return false;
}
if (!std::filesystem::exists(realFilePath)) {
HILOGE("The real file path %{public}s does not exist in the file system.", GetAnonymStr(realFilePath).c_str());
realFilePath = "";
return false;
}
HILOGI("The real file path %{public}s exist in the file system.", GetAnonymStr(realFilePath).c_str());
return true;
}
bool DmsKvSyncE2E::UpdateWhiteList(const std::string &cfgJsonStr)
{
cJSON *inJson = nullptr;
cJSON *allowList = nullptr;
bool isSuccess = false;
do {
inJson = cJSON_Parse(cfgJsonStr.c_str());
if (inJson == nullptr) {
HILOGE("parse continue config json file stream to json fail.");
break;
}
allowList = cJSON_GetObjectItem(inJson, ALLOW_APP_LIST_KEY.c_str());
if (allowList == nullptr || !cJSON_IsArray(allowList)) {
HILOGE("allow app list array is not in continue config json file.");
break;
}
std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
for (int32_t i = 0; i < cJSON_GetArraySize(allowList); i++) {
cJSON *iAllowAppJson = cJSON_GetArrayItem(allowList, i);
if (!cJSON_IsString(iAllowAppJson)) {
HILOGE("allow app list [%{public}d] is not string.", i);
continue;
}
std::string iAllowAppStr = std::string(cJSON_GetStringValue(iAllowAppJson));
HILOGI("allow app list show [%{public}d] : [%{public}s].", i, iAllowAppStr.c_str());
whiteList_.push_back(iAllowAppStr);
}
isSuccess = true;
} while (false);
if (inJson != nullptr) {
cJSON_Delete(inJson);
inJson = nullptr;
}
return isSuccess;
}
int32_t DmsKvSyncE2E::LoadContinueConfig()
{
HILOGI("Load continue config, continueCfgFullPath %{public}s.", GetAnonymStr(continueCfgFullPath_).c_str());
std::string tempPath = continueCfgFullPath_;
if ((continueCfgFullPath_.empty() || !IsValidPath(tempPath, continueCfgFullPath_))) {
char cfgPathBuf[MAX_CONFIG_PATH_LEN] = { 0 };
char *filePath = GetOneCfgFile(CONTINUE_CONFIG_RELATIVE_PATH.c_str(), cfgPathBuf, MAX_CONFIG_PATH_LEN);
if (filePath == nullptr || filePath != cfgPathBuf) {
HILOGI("Not find continue config file, relative path %{public}s.",
GetAnonymStr(CONTINUE_CONFIG_RELATIVE_PATH).c_str());
continueCfgFullPath_ = "";
return ERR_OK;
}
continueCfgFullPath_ = std::string(filePath);
HILOGI("Get Continue config file full path success, cfgFullPath %{public}s.",
GetAnonymStr(continueCfgFullPath_).c_str());
}
tempPath = continueCfgFullPath_;
if (!IsValidPath(tempPath, continueCfgFullPath_)) {
HILOGE("Continue config full path is invalid, cfgFullPath %{public}s.",
GetAnonymStr(continueCfgFullPath_).c_str());
return DMS_PERMISSION_DENIED;
}
std::ifstream in;
in.open(continueCfgFullPath_.c_str(), std::ios::binary | std::ios::in);
if (!in.is_open()) {
HILOGE("Open continue config json file fail, cfgFullPath %{public}s.",
GetAnonymStr(continueCfgFullPath_).c_str());
return DMS_PERMISSION_DENIED;
}
std::string cfgFileContent;
in.seekg(0, std::ios::end);
cfgFileContent.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.rdbuf()->sgetn(&cfgFileContent[0], cfgFileContent.size());
in.close();
if (!UpdateWhiteList(cfgFileContent)) {
HILOGE("Update allow app list fail, cfgFullPath %{public}s.",
GetAnonymStr(continueCfgFullPath_).c_str());
return DMS_PERMISSION_DENIED;
}
HILOGI("Load continue config success, cfgFullPath %{public}s.", GetAnonymStr(continueCfgFullPath_).c_str());
return ERR_OK;
}
bool DmsKvSyncE2E::CheckBundleContinueConfig(const std::string &bundleName)
{
if (!isCfgDevices_) {
HILOGI("The device is a normal device");
return true;
}
std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
auto it = std::find(whiteList_.begin(), whiteList_.end(), bundleName);
if (it == whiteList_.end()) {
HILOGE("Current app is not allow to continue in config file, bundleName %{public}s, cfgPath %{public}s.",
bundleName.c_str(), GetAnonymStr(continueCfgFullPath_).c_str());
return false;
}
HILOGI("Current app is allow to continue in config file, bundleName %{public}s, cfgPath %{public}s.",
bundleName.c_str(), GetAnonymStr(continueCfgFullPath_).c_str());
return true;
}
bool DmsKvSyncE2E::PushAndPullData()
{
HILOGI("called.");

View File

@ -753,7 +753,7 @@ HWTEST_F(DMSContinueManagerTest, testGetBundleNameIdAndContinueTypeId_001, TestS
int32_t ret = DMSContinueSendMgr::GetInstance().GetBundleNameIdAndContinueTypeId(MISSIONID_01, state,
bundleNameId, continueTypeId);
EXPECT_EQ(ret, REMOTE_DEVICE_BIND_ABILITY_ERR);
EXPECT_EQ(ret, CAN_NOT_FOUND_ABILITY_ERR);
DTEST_LOG << "DMSContinueManagerTest testGetBundleNameIdAndContinueTypeId_001 end" << std::endl;
}

View File

@ -194,5 +194,42 @@ HWTEST_F(DmsKvSyncE2ETest, IsSynchronizedTest_001, TestSize.Level1)
}
DTEST_LOG << "DmsKvSyncE2ETest IsSynchronizedTest_001 end" << std::endl;
}
/**
* @tc.name: CheckCtrlRuleTest_001
* @tc.desc: test insert DmsKvSyncE2E
* @tc.type: FUNC
*/
HWTEST_F(DmsKvSyncE2ETest, CheckCtrlRuleTest_001, TestSize.Level1)
{
DTEST_LOG << "DmsKvSyncE2ETest CheckCtrlRuleTest_001 start" << std::endl;
ASSERT_NE(dmsKvSyncE2E_, nullptr);
auto dmsKvSyncE2E = GetDmsKvSyncE2E();
EXPECT_NE(dmsKvSyncE2E, nullptr);
if (dmsKvSyncE2E != nullptr) {
dmsKvSyncE2E_->GetInstance()->CheckCtrlRule();
}
DTEST_LOG << "DmsKvSyncE2ETest CheckCtrlRuleTest_001 end" << std::endl;
}
/**
* @tc.name: CheckBundleContinueConfigTest_001
* @tc.desc: test insert DmsKvSyncE2E
* @tc.type: FUNC
*/
HWTEST_F(DmsKvSyncE2ETest, CheckBundleContinueConfigTest_001, TestSize.Level1)
{
DTEST_LOG << "DmsKvSyncE2ETest CheckBundleContinueConfigTest_001 start" << std::endl;
ASSERT_NE(dmsKvSyncE2E_, nullptr);
auto dmsKvSyncE2E = GetDmsKvSyncE2E();
EXPECT_NE(dmsKvSyncE2E, nullptr);
if (dmsKvSyncE2E != nullptr) {
const std::string bundleName = "123";
bool ret = dmsKvSyncE2E_->GetInstance()->CheckBundleContinueConfig(bundleName);
EXPECT_EQ(ret, true);
}
DTEST_LOG << "DmsKvSyncE2ETest CheckBundleContinueConfigTest_001 end" << std::endl;
}
} // namespace DistributedSchedule
} // namespace OHOS