!1169 Bugfix: 同时传输want与dtbWant,修复兼容隐患

Merge pull request !1169 from zhx/master
This commit is contained in:
openharmony_ci 2024-11-18 08:53:10 +00:00 committed by Gitee
commit 35ddb727d5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 121 additions and 38 deletions

View File

@ -18,6 +18,8 @@
#include <string> #include <string>
#include "cJSON.h"
#include "ability_info.h" #include "ability_info.h"
#include "caller_info.h" #include "caller_info.h"
#include "distributed_sched_interface.h" #include "distributed_sched_interface.h"
@ -88,9 +90,13 @@ public:
int32_t Unmarshal(const std::string &jsonStr); int32_t Unmarshal(const std::string &jsonStr);
private: private:
bool MarshalInner(cJSON* rootValue);
int32_t MarshalCallerInfo(std::string &jsonStr); int32_t MarshalCallerInfo(std::string &jsonStr);
int32_t MarshalAccountInfo(std::string &jsonStr); int32_t MarshalAccountInfo(std::string &jsonStr);
int32_t UnmarshalParcel(const std::string &jsonStr); int32_t UnmarshalParcel(const std::string &jsonStr);
bool UnmarshalWantParcel(cJSON* rootValue);
int32_t UnmarshalWantStr(const std::string &jsonStr);
int32_t UnmarshalDtbWantStr(const std::string &jsonStr);
int32_t UnmarshalCallerInfo(const std::string &jsonStr); int32_t UnmarshalCallerInfo(const std::string &jsonStr);
int32_t UnmarshalCallerInfoExtra(const std::string &jsonStr); int32_t UnmarshalCallerInfoExtra(const std::string &jsonStr);
int32_t UnmarshalAccountInfo(const std::string &jsonStr); int32_t UnmarshalAccountInfo(const std::string &jsonStr);

View File

@ -139,7 +139,7 @@ DSchedContinue::DSchedContinue(std::shared_ptr<DSchedContinueStartCmd> startCmd,
HILOGE("startCmd is null"); HILOGE("startCmd is null");
return; return;
} }
version_ = startCmd->version_; version_ = DSCHED_CONTINUE_PROTOCOL_VERSION;
subServiceType_ = startCmd->subServiceType_; subServiceType_ = startCmd->subServiceType_;
continueByType_ = startCmd->continueByType_; continueByType_ = startCmd->continueByType_;
direction_ = (startCmd->direction_ == CONTINUE_SOURCE) ? CONTINUE_SINK : CONTINUE_SOURCE; direction_ = (startCmd->direction_ == CONTINUE_SOURCE) ? CONTINUE_SINK : CONTINUE_SOURCE;

View File

@ -15,9 +15,9 @@
#include "dsched_continue_event.h" #include "dsched_continue_event.h"
#include "cJSON.h"
#include "parcel.h" #include "parcel.h"
#include "distributedWant/distributed_want.h"
#include "distributed_sched_utils.h" #include "distributed_sched_utils.h"
#include "dms_constant.h" #include "dms_constant.h"
#include "dtbschedmgr_log.h" #include "dtbschedmgr_log.h"
@ -217,21 +217,49 @@ int32_t DSchedContinueDataCmd::Marshal(std::string &jsonStr)
cJSON_Delete(rootValue); cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR; return INVALID_PARAMETERS_ERR;
} }
cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str()); cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
if (!MarshalInner(rootValue)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
char *data = cJSON_Print(rootValue);
if (data == nullptr) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
jsonStr = std::string(data);
cJSON_Delete(rootValue);
cJSON_free(data);
return ERR_OK;
}
bool DSchedContinueDataCmd::MarshalInner(cJSON* rootValue)
{
if (rootValue == nullptr) {
return false;
}
Parcel wantParcel; Parcel wantParcel;
if (!want_.Marshalling(wantParcel)) { if (!want_.Marshalling(wantParcel)) {
cJSON_Delete(rootValue); return false;
return INVALID_PARAMETERS_ERR;
} }
std::string wantStr = ParcelToBase64Str(wantParcel); std::string wantStr = ParcelToBase64Str(wantParcel);
cJSON_AddStringToObject(rootValue, "Want", wantStr.c_str()); cJSON_AddStringToObject(rootValue, "Want", wantStr.c_str());
DistributedWant dtbWant(want_);
Parcel dtbWantParcel;
if (!dtbWant.Marshalling(dtbWantParcel)) {
return false;
}
std::string dtbWantStr = ParcelToBase64Str(dtbWantParcel);
cJSON_AddStringToObject(rootValue, "DtbWant", dtbWantStr.c_str());
Parcel abilityParcel; Parcel abilityParcel;
if (!abilityInfo_.Marshalling(abilityParcel)) { if (!abilityInfo_.Marshalling(abilityParcel)) {
cJSON_Delete(rootValue); return false;
return INVALID_PARAMETERS_ERR;
} }
std::string abilityInfoStr = ParcelToBase64Str(abilityParcel); std::string abilityInfoStr = ParcelToBase64Str(abilityParcel);
cJSON_AddStringToObject(rootValue, "AbilityInfo", abilityInfoStr.c_str()); cJSON_AddStringToObject(rootValue, "AbilityInfo", abilityInfoStr.c_str());
@ -240,29 +268,20 @@ int32_t DSchedContinueDataCmd::Marshal(std::string &jsonStr)
std::string callerInfoStr; std::string callerInfoStr;
if (MarshalCallerInfo(callerInfoStr) != ERR_OK) { if (MarshalCallerInfo(callerInfoStr) != ERR_OK) {
cJSON_Delete(rootValue); return false;
return INVALID_PARAMETERS_ERR;
} }
cJSON_AddStringToObject(rootValue, "CallerInfo", callerInfoStr.c_str()); cJSON_AddStringToObject(rootValue, "CallerInfo", callerInfoStr.c_str());
std::string accountInfoStr; std::string accountInfoStr;
if (MarshalAccountInfo(accountInfoStr) != ERR_OK) { if (MarshalAccountInfo(accountInfoStr) != ERR_OK) {
cJSON_Delete(rootValue); return false;
return INVALID_PARAMETERS_ERR;
} }
cJSON_AddStringToObject(rootValue, "AccountInfo", accountInfoStr.c_str()); cJSON_AddStringToObject(rootValue, "AccountInfo", accountInfoStr.c_str());
char *data = cJSON_Print(rootValue); return true;
if (data == nullptr) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
jsonStr = std::string(data);
cJSON_Delete(rootValue);
cJSON_free(data);
return ERR_OK;
} }
int32_t DSchedContinueDataCmd::MarshalCallerInfo(std::string &jsonStr) int32_t DSchedContinueDataCmd::MarshalCallerInfo(std::string &jsonStr)
{ {
cJSON *callerInfoJson = cJSON_CreateObject(); cJSON *callerInfoJson = cJSON_CreateObject();
@ -412,26 +431,10 @@ int32_t DSchedContinueDataCmd::UnmarshalParcel(const std::string &jsonStr)
return INVALID_PARAMETERS_ERR; return INVALID_PARAMETERS_ERR;
} }
cJSON *wantStr = cJSON_GetObjectItemCaseSensitive(rootValue, "Want"); if (!UnmarshalWantParcel(rootValue)) {
if (wantStr == nullptr || !cJSON_IsString(wantStr) || (wantStr->valuestring == nullptr)) {
cJSON_Delete(rootValue); cJSON_Delete(rootValue);
HILOGE("Want term is null or not string.");
return INVALID_PARAMETERS_ERR; return INVALID_PARAMETERS_ERR;
} }
Parcel wantParcel;
int32_t ret = Base64StrToParcel(wantStr->valuestring, wantParcel);
if (ret != ERR_OK) {
cJSON_Delete(rootValue);
HILOGE("Want parcel Base64Str unmarshal fail, ret %{public}d.", ret);
return INVALID_PARAMETERS_ERR;
}
auto wantPtr = AAFwk::Want::Unmarshalling(wantParcel);
if (wantPtr == nullptr) {
cJSON_Delete(rootValue);
HILOGE("AAFwk Want unmarshalling fail, check return null.");
return INVALID_PARAMETERS_ERR;
}
want_ = *wantPtr;
cJSON *abilityInfoStr = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityInfo"); cJSON *abilityInfoStr = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityInfo");
if (abilityInfoStr == nullptr || !cJSON_IsString(abilityInfoStr) || (abilityInfoStr->valuestring == nullptr)) { if (abilityInfoStr == nullptr || !cJSON_IsString(abilityInfoStr) || (abilityInfoStr->valuestring == nullptr)) {
@ -440,7 +443,7 @@ int32_t DSchedContinueDataCmd::UnmarshalParcel(const std::string &jsonStr)
return INVALID_PARAMETERS_ERR; return INVALID_PARAMETERS_ERR;
} }
Parcel abilityParcel; Parcel abilityParcel;
ret = Base64StrToParcel(abilityInfoStr->valuestring, abilityParcel); int32_t ret = Base64StrToParcel(abilityInfoStr->valuestring, abilityParcel);
if (ret != ERR_OK) { if (ret != ERR_OK) {
cJSON_Delete(rootValue); cJSON_Delete(rootValue);
HILOGE("AbilityInfo parcel Base64Str unmarshal fail, ret %{public}d.", ret); HILOGE("AbilityInfo parcel Base64Str unmarshal fail, ret %{public}d.", ret);
@ -458,6 +461,79 @@ int32_t DSchedContinueDataCmd::UnmarshalParcel(const std::string &jsonStr)
return ERR_OK; return ERR_OK;
} }
bool DSchedContinueDataCmd::UnmarshalWantParcel(cJSON* rootValue)
{
if (rootValue == nullptr) {
return false;
}
cJSON *dtbWantStr = cJSON_GetObjectItemCaseSensitive(rootValue, "DtbWant");
if (dtbWantStr != nullptr) {
if (!cJSON_IsString(dtbWantStr) || (dtbWantStr->valuestring == nullptr)) {
HILOGE("DtbWant term is null or not string.");
return false;
}
if (UnmarshalDtbWantStr(dtbWantStr->valuestring) != ERR_OK) {
HILOGE("UnmarshalDtbWantStr failed!");
return false;
}
} else {
cJSON *wantStr = cJSON_GetObjectItemCaseSensitive(rootValue, "Want");
if (wantStr == nullptr|| !cJSON_IsString(wantStr) || (wantStr->valuestring == nullptr)) {
HILOGE("Want term is null or not string.");
return false;
}
if (UnmarshalWantStr(wantStr->valuestring) != ERR_OK) {
HILOGE("UnmarshalWantStr failed!");
return false;
}
}
return true;
}
int32_t DSchedContinueDataCmd::UnmarshalDtbWantStr(const std::string &jsonStr)
{
Parcel wantParcel;
int32_t ret = Base64StrToParcel(jsonStr, wantParcel);
if (ret != ERR_OK) {
HILOGE("Want parcel Base64Str unmarshal fail, ret %{public}d.", ret);
return INVALID_PARAMETERS_ERR;
}
auto dtbWantPtr = DistributedWant::Unmarshalling(wantParcel);
if (dtbWantPtr == nullptr) {
HILOGE("Distributed want unmarshalling fail, check return null.");
return INVALID_PARAMETERS_ERR;
}
DistributedWant dtbWant = *dtbWantPtr;
auto wantPtr = dtbWant.ToWant();
if (wantPtr == nullptr) {
HILOGE("Convert distributedWant to want failed.");
return INVALID_PARAMETERS_ERR;
}
want_ = *wantPtr;
return ERR_OK;
}
int32_t DSchedContinueDataCmd::UnmarshalWantStr(const std::string &jsonStr)
{
Parcel wantParcel;
int32_t ret = Base64StrToParcel(jsonStr, wantParcel);
if (ret != ERR_OK) {
HILOGE("Want parcel Base64Str unmarshal fail, ret %{public}d.", ret);
return INVALID_PARAMETERS_ERR;
}
auto wantPtr = AAFwk::Want::Unmarshalling(wantParcel);
if (wantPtr == nullptr) {
HILOGE("Want unmarshalling fail, check return null.");
return INVALID_PARAMETERS_ERR;
}
want_ = *wantPtr;
return ERR_OK;
}
int32_t DSchedContinueDataCmd::UnmarshalCallerInfo(const std::string &jsonStr) int32_t DSchedContinueDataCmd::UnmarshalCallerInfo(const std::string &jsonStr)
{ {
cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); cJSON *rootValue = cJSON_Parse(jsonStr.c_str());

View File

@ -143,6 +143,7 @@ sptr<IDistributedWantParams> DistributedWantParamWrapper::Parse(const std::strin
strKey = ""; strKey = "";
typeId = 0; typeId = 0;
strnum = num + 1; strnum = num + 1;
continue;
} else if (str[strnum] != '"') { } else if (str[strnum] != '"') {
continue; continue;
} }