!846 Feature: 自建链路ContinueEvent实现

Merge pull request !846 from zhx/master
This commit is contained in:
openharmony_ci 2024-04-18 07:47:24 +00:00 committed by Gitee
commit 5b1f240f5f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 819 additions and 27 deletions

View File

@ -18,15 +18,23 @@
#include <string>
#include "parcel.h"
namespace OHOS {
namespace DistributedSchedule {
constexpr static int32_t INVALID_MISSION_ID = -1;
const std::string BASE_64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
bool IsValidPath(const std::string &inFilePath, std::string &realFilePath);
bool UpdateAllowAppList(const std::string &cfgJsonStr);
int32_t LoadContinueConfig();
bool CheckBundleContinueConfig(const std::string &bundleName);
int32_t GetCurrentMissionId();
std::string ParcelToBase64Str(const Parcel& parcel);
int32_t Base64StrToParcel(const std::string& rawStr, Parcel& parcel);
std::string Base64Encode(const unsigned char *toEncode, unsigned int len);
std::string Base64Decode(const std::string& basicString);
bool IsBase64(unsigned char c);
} // namespace DistributedSchedule
} // namespace OHOS
#endif // OHOS_DISTRIBUTED_SCHED_SERVICE_H
#endif // OHOS_DISTRIBUTED_SCHED_SERVICE_H

View File

@ -35,6 +35,20 @@ const std::string CONTINUE_CONFIG_RELATIVE_PATH = "etc/distributedhardware/dms/c
const std::string ALLOW_APP_LIST_KEY = "allow_applist";
constexpr int32_t MAX_CONFIG_PATH_LEN = 1024;
const uint32_t OFFSET2 = 2;
const uint32_t OFFSET4 = 4;
const uint32_t OFFSET6 = 6;
const uint8_t PARAM_FC = 0xfc;
const uint8_t PARAM_03 = 0x03;
const uint8_t PARAM_F0 = 0xf0;
const uint8_t PARAM_0F = 0x0f;
const uint8_t PARAM_C0 = 0xc0;
const uint8_t PARAM_3F = 0x3f;
const int INDEX_FIRST = 0;
const int INDEX_SECOND = 1;
const int INDEX_THIRD = 2;
const int INDEX_FORTH = 3;
static std::atomic<bool> g_isMissContinueCfg = false;
static std::string g_continueCfgFullPath = "";
static std::vector<std::string> g_allowAppList;
@ -185,5 +199,132 @@ int32_t GetCurrentMissionId()
AAFwk::AbilityManagerClient::GetInstance()->GetMissionIdByToken(token, missionId);
return missionId;
}
std::string ParcelToBase64Str(const Parcel& parcel)
{
auto parcelSize = parcel.GetDataSize();
return Base64Encode(reinterpret_cast<const unsigned char *>(parcel.GetData()), parcelSize);
}
int32_t Base64StrToParcel(const std::string& rawStr, Parcel& parcel)
{
std::string str = Base64Decode(rawStr);
auto parcelSize = str.size();
if (!parcel.SetDataCapacity(parcelSize)) {
return INVALID_PARAMETERS_ERR;
}
auto ret = memcpy_s((void *)parcel.GetData(), parcel.GetMaxCapacity(), &str[0], parcelSize);
if (ret != ERR_OK || !parcel.SetDataSize(parcelSize)) {
return INVALID_PARAMETERS_ERR;
}
return ERR_OK;
}
std::string Base64Encode(const unsigned char *toEncode, unsigned int len)
{
std::string ret = "";
if (len == 0 || toEncode == nullptr) {
HILOGE("toEncode is null or len is zero.");
return ret;
}
uint32_t length = len;
uint32_t i = 0;
unsigned char charArray3[3];
unsigned char charArray4[4];
while (length--) {
charArray3[i++] = *(toEncode++);
if (i == sizeof(charArray3)) {
charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
for (i = 0; i < sizeof(charArray4); i++) {
ret += BASE_64_CHARS[charArray4[i]];
}
i = 0;
}
}
if (i > 0) {
uint32_t j = 0;
for (j = i; j < sizeof(charArray3); j++) {
charArray3[j] = '\0';
}
charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
for (j = 0; j < i + 1; j++) {
ret += BASE_64_CHARS[charArray4[j]];
}
while (i++ < sizeof(charArray3)) {
ret += '=';
}
}
return ret;
}
std::string Base64Decode(const std::string& basicString)
{
std::string ret = "";
if (basicString.empty()) {
HILOGE("basicString is empty.");
return ret;
}
uint32_t i = 0;
int index = 0;
int len = static_cast<int>(basicString.size());
unsigned char charArray3[3];
unsigned char charArray4[4];
while (len-- && (basicString[index] != '=') && IsBase64(basicString[index])) {
charArray4[i++] = basicString[index];
index++;
if (i == sizeof(charArray4)) {
for (i = 0; i < sizeof(charArray4); i++) {
charArray4[i] = BASE_64_CHARS.find(charArray4[i]);
}
charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
for (i = 0; i < sizeof(charArray3); i++) {
ret += charArray3[i];
}
i = 0;
}
}
if (i > 0) {
uint32_t j = 0;
for (j = i; j < sizeof(charArray4); j++) {
charArray4[j] = 0;
}
for (j = 0; j < sizeof(charArray4); j++) {
charArray4[j] = BASE_64_CHARS.find(charArray4[j]);
}
charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
for (j = 0; j < i - 1; j++) {
ret += charArray3[j];
}
}
return ret;
}
bool IsBase64(unsigned char c)
{
return (isalnum(c) || (c == '+') || (c == '/'));
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -21,6 +21,7 @@
#include "ability_info.h"
#include "caller_info.h"
#include "distributed_sched_interface.h"
#include "distributedWant/distributed_want_params.h"
#include "want.h"
namespace OHOS {
@ -39,43 +40,62 @@ typedef enum {
DSCHED_CONTINUE_END_EVENT = 7
} DSchedContinueEventType;
typedef enum {
DSCHED_CONTINUE_CMD_MIN = 0,
DSCHED_CONTINUE_CMD_START = 1,
DSCHED_CONTINUE_CMD_DATA = 2,
DSCHED_CONTINUE_CMD_REPLY = 3,
DSCHED_CONTINUE_CMD_END = 4,
} DSchedContinueCommand;
class DSchedContinueCmdBase {
public:
int32_t version_;
int32_t serviceType_;
int32_t subServiceType_;
int32_t command_;
DSchedContinueCmdBase() = default;
virtual ~DSchedContinueCmdBase() = default;
virtual int32_t Marshal(std::string &jsonStr);
virtual int32_t Unmarshal(const std::string &jsonStr);
public:
int32_t version_ = 0;
int32_t serviceType_ = 0;
int32_t subServiceType_ = 0;
int32_t command_ = 0;
std::string srcDeviceId_;
std::string srcBundleName_;
std::string dstDeviceId_;
std::string dstBundleName_;
std::string continueType_;
int32_t continueByType_;
int32_t sourceMissionId_;
int32_t dmsVersion_;
int32_t continueByType_ = 0;
int32_t sourceMissionId_ = 0;
int32_t dmsVersion_ = 0;
};
class DSchedContinueStartCmd {
class DSchedContinueStartCmd : public DSchedContinueCmdBase {
public:
int32_t Marshal(std::string &jsonStr);
int32_t Unmarshal(const std::string &jsonStr);
public:
DSchedContinueCmdBase cmdBase_;
int32_t direction_;
int32_t appVersion_;
OHOS::AAFwk::WantParams wantParams_;
int32_t direction_ = 0;
int32_t appVersion_ = 0;
DistributedWantParams wantParams_;
};
class DSchedContinueDataCmd {
class DSchedContinueDataCmd : public DSchedContinueCmdBase {
public:
int32_t Marshal(std::string &jsonStr);
int32_t Unmarshal(const std::string &jsonStr);
private:
int32_t MarshalCallerInfo(std::string &jsonStr);
int32_t MarshalAccountInfo(std::string &jsonStr);
int32_t UnmarshalParcel(const std::string &jsonStr);
int32_t UnmarshalCallerInfo(std::string &jsonStr);
int32_t UnmarshalCallerInfoExtra(std::string &jsonStr);
int32_t UnmarshalAccountInfo(std::string &jsonStr);
public:
using AccountInfo = IDistributedSched::AccountInfo;
DSchedContinueCmdBase cmdBase_;
OHOS::AAFwk::Want want_;
AppExecFwk::CompatibleAbilityInfo abilityInfo_;
int32_t requestCode_;
@ -83,28 +103,26 @@ public:
AccountInfo accountInfo_;
};
class DSchedContinueReplyCmd {
class DSchedContinueReplyCmd : public DSchedContinueCmdBase {
public:
int32_t Marshal(std::string &jsonStr);
int32_t Unmarshal(const std::string &jsonStr);
public:
DSchedContinueCmdBase cmdBase_;
int32_t replyCmd_;
int32_t appVersion_;
int32_t result_;
int32_t replyCmd_ = 0;
int32_t appVersion_ = 0;
int32_t result_ = 0;
std::string reason_;
};
class DSchedContinueEndCmd {
class DSchedContinueEndCmd : public DSchedContinueCmdBase {
public:
int32_t Marshal(std::string &jsonStr);
int32_t Unmarshal(const std::string &jsonStr);
public:
DSchedContinueCmdBase cmdBase_;
int32_t result_;
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif // OHOS_DSCHED_CONTINUE_EVENT_H
#endif // OHOS_DSCHED_CONTINUE_EVENT_H

View File

@ -26,7 +26,7 @@ class DSchedContinue;
class DSchedContinueEventHandler : public AppExecFwk::EventHandler {
public:
explicit DSchedContinueEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
const std::shared_ptr<DSchedContinue> dschedContinue);
const std::shared_ptr<DSchedContinue>& dschedContinue);
~DSchedContinueEventHandler() override;
void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;

View File

@ -15,48 +15,672 @@
#include "dsched_continue_event.h"
#include "cJSON.h"
#include "parcel.h"
#include "distributed_sched_utils.h"
#include "dtbschedmgr_log.h"
namespace OHOS {
namespace DistributedSchedule {
namespace {
const std::string TAG = "DSchedContinueCmd";
const char* EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
const char* DMS_VERSION_ID = "dmsVersion";
}
int32_t DSchedContinueCmdBase::Marshal(std::string &jsonStr)
{
cJSON *rootValue = cJSON_CreateObject();
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON_AddNumberToObject(rootValue, "Version", version_);
cJSON_AddNumberToObject(rootValue, "ServiceType", serviceType_);
cJSON_AddNumberToObject(rootValue, "SubServiceType", subServiceType_);
cJSON_AddNumberToObject(rootValue, "Command", command_);
cJSON_AddStringToObject(rootValue, "SrcDeviceId", srcDeviceId_.c_str());
cJSON_AddStringToObject(rootValue, "SrcBundleName", srcBundleName_.c_str());
cJSON_AddStringToObject(rootValue, "DstDeviceId", dstDeviceId_.c_str());
cJSON_AddStringToObject(rootValue, "DstBundleName", dstBundleName_.c_str());
cJSON_AddStringToObject(rootValue, "ContinueType", continueType_.c_str());
cJSON_AddNumberToObject(rootValue, "ContinueByType", continueByType_);
cJSON_AddNumberToObject(rootValue, "SourceMissionId", sourceMissionId_);
cJSON_AddNumberToObject(rootValue, "DmsVersion", dmsVersion_);
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;
}
int32_t DSchedContinueCmdBase::Unmarshal(const std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
const char *numKeys[] = { "Version", "ServiceType", "SubServiceType", "ContinueByType", "SourceMissionId",
"DmsVersion" };
int32_t *numValues[] = { &version_, &serviceType_, &subServiceType_, &continueByType_, &sourceMissionId_,
&dmsVersion_ };
int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
for (int32_t i = 0; i < numLength; i++) {
cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
if (item == nullptr || !cJSON_IsNumber(item)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
*numValues[i] = item->valueint;
}
const char *strKeys[] = { "SrcDeviceId", "SrcBundleName", "DstDeviceId", "DstBundleName", "ContinueType" };
std::string *strValues[] = { &srcDeviceId_, &srcBundleName_, &dstDeviceId_, &dstBundleName_, &continueType_ };
int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
for (int32_t i = 0; i < strLength; i++) {
cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
*strValues[i] = item->valuestring;
}
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueStartCmd::Marshal(std::string &jsonStr)
{
cJSON *rootValue = cJSON_CreateObject();
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
std::string baseJsonStr;
if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
cJSON_AddNumberToObject(rootValue, "Direction", direction_);
cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_);
Parcel parcel;
if (!wantParams_.Marshalling(parcel)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string wantParamsStr = ParcelToBase64Str(parcel);
cJSON_AddStringToObject(rootValue, "WantParams", wantParamsStr.c_str());
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;
}
int32_t DSchedContinueStartCmd::Unmarshal(const std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string baseCmdStr = baseCmd->valuestring;
if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON *direction = cJSON_GetObjectItemCaseSensitive(rootValue, "Direction");
if (direction == nullptr || !cJSON_IsNumber(direction)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
direction_ = direction->valueint;
cJSON *appVersion = cJSON_GetObjectItemCaseSensitive(rootValue, "AppVersion");
if (appVersion == nullptr || !cJSON_IsNumber(appVersion)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
appVersion_ = appVersion->valueint;
cJSON *wantParams = cJSON_GetObjectItemCaseSensitive(rootValue, "WantParams");
if (wantParams == nullptr || !cJSON_IsString(wantParams) || (wantParams->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
Parcel parcel;
int32_t ret = Base64StrToParcel(wantParams->valuestring, parcel);
if (ret != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
auto wantParamsPtr = DistributedWantParams::Unmarshalling(parcel);
if (wantParamsPtr == nullptr) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
wantParams_ = *wantParamsPtr;
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueDataCmd::Marshal(std::string &jsonStr)
{
cJSON *rootValue = cJSON_CreateObject();
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
std::string baseJsonStr;
if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
Parcel wantParcel;
if (!want_.Marshalling(wantParcel)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string wantStr = ParcelToBase64Str(wantParcel);
cJSON_AddStringToObject(rootValue, "Want", wantStr.c_str());
Parcel abilityParcel;
if (!abilityInfo_.Marshalling(abilityParcel)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string abilityInfoStr = ParcelToBase64Str(abilityParcel);
cJSON_AddStringToObject(rootValue, "AbilityInfo", abilityInfoStr.c_str());
cJSON_AddNumberToObject(rootValue, "RequestCode", requestCode_);
std::string callerInfoStr;
if (MarshalCallerInfo(callerInfoStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddStringToObject(rootValue, "CallerInfo", callerInfoStr.c_str());
std::string accountInfoStr;
if (MarshalAccountInfo(accountInfoStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddStringToObject(rootValue, "AccountInfo", accountInfoStr.c_str());
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;
}
int32_t DSchedContinueDataCmd::MarshalCallerInfo(std::string &jsonStr)
{
cJSON *callerInfoJson = cJSON_CreateObject();
if (callerInfoJson == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON_AddNumberToObject(callerInfoJson, "Uid", callerInfo_.uid);
cJSON_AddNumberToObject(callerInfoJson, "Pid", callerInfo_.pid);
cJSON_AddNumberToObject(callerInfoJson, "CallerType", callerInfo_.callerType);
cJSON_AddStringToObject(callerInfoJson, "SourceDeviceId", callerInfo_.sourceDeviceId.c_str());
cJSON_AddNumberToObject(callerInfoJson, "Duid", callerInfo_.duid);
cJSON_AddStringToObject(callerInfoJson, "CallerAppId", callerInfo_.callerAppId.c_str());
const auto bundleNamesSize = static_cast<int32_t>(callerInfo_.bundleNames.size());
cJSON *bundleNames = cJSON_CreateArray();
if (bundleNames == nullptr) {
cJSON_Delete(callerInfoJson);
return INVALID_PARAMETERS_ERR;
}
for (auto i = 0; i < bundleNamesSize; i++) {
cJSON *bundleName = cJSON_CreateString(callerInfo_.bundleNames[i].c_str());
if (bundleName == nullptr) {
cJSON_Delete(callerInfoJson);
cJSON_Delete(bundleNames);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddItemToArray(bundleNames, bundleName);
}
cJSON_AddItemToObject(callerInfoJson, "BundleNames", bundleNames);
std::string extraInfo = callerInfo_.extraInfoJson.dump();
cJSON_AddStringToObject(callerInfoJson, "ExtraInfo", extraInfo.c_str());
char *data = cJSON_Print(callerInfoJson);
if (data == nullptr) {
cJSON_Delete(callerInfoJson);
return INVALID_PARAMETERS_ERR;
}
jsonStr = std::string(data);
cJSON_Delete(callerInfoJson);
cJSON_free(data);
return ERR_OK;
}
int32_t DSchedContinueDataCmd::MarshalAccountInfo(std::string &jsonStr)
{
cJSON *accountInfoJson = cJSON_CreateObject();
if (accountInfoJson == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON_AddNumberToObject(accountInfoJson, "AccountType", accountInfo_.accountType);
const auto groupIdListSize = static_cast<int32_t>(accountInfo_.groupIdList.size());
cJSON *groupIdList = cJSON_CreateArray();
if (groupIdList == nullptr) {
cJSON_Delete(accountInfoJson);
return INVALID_PARAMETERS_ERR;
}
for (auto i = 0; i < groupIdListSize; i++) {
cJSON *groupId = cJSON_CreateString(accountInfo_.groupIdList[i].c_str());
if (groupId == nullptr) {
cJSON_Delete(accountInfoJson);
cJSON_Delete(groupIdList);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddItemToArray(groupIdList, groupId);
}
cJSON_AddItemToObject(accountInfoJson, "GroupIdList", groupIdList);
char *data = cJSON_Print(accountInfoJson);
if (data == nullptr) {
cJSON_Delete(accountInfoJson);
return INVALID_PARAMETERS_ERR;
}
jsonStr = std::string(data);
cJSON_Delete(accountInfoJson);
cJSON_free(data);
return ERR_OK;
}
int32_t DSchedContinueDataCmd::Unmarshal(const std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string baseCmdStr = baseCmd->valuestring;
if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
if (UnmarshalParcel(jsonStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON *requestCode = cJSON_GetObjectItemCaseSensitive(rootValue, "RequestCode");
if (requestCode == nullptr || !cJSON_IsNumber(requestCode)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
requestCode_ = requestCode->valueint;
cJSON *callerInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "CallerInfo");
if (callerInfoJson == nullptr || !cJSON_IsString(callerInfoJson) || (callerInfoJson->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string callerInfoStr = callerInfoJson->valuestring;
if (UnmarshalCallerInfo(callerInfoStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON *accountInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountInfo");
if (accountInfoJson == nullptr || !cJSON_IsString(accountInfoJson) || (accountInfoJson->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string accountInfoStr = accountInfoJson->valuestring;
if (UnmarshalAccountInfo(accountInfoStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueDataCmd::UnmarshalParcel(const std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON *wantStr = cJSON_GetObjectItemCaseSensitive(rootValue, "Want");
if (wantStr == nullptr || !cJSON_IsString(wantStr) || (wantStr->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
Parcel wantParcel;
int32_t ret = Base64StrToParcel(wantStr->valuestring, wantParcel);
if (ret != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
auto wantPtr = AAFwk::Want::Unmarshalling(wantParcel);
if (wantPtr == nullptr) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
want_ = *wantPtr;
cJSON *abilityInfoStr = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityInfo");
if (abilityInfoStr == nullptr || !cJSON_IsString(abilityInfoStr) || (abilityInfoStr->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
Parcel abilityParcel;
ret = Base64StrToParcel(abilityInfoStr->valuestring, abilityParcel);
if (ret != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
auto abilityInfoPtr = AppExecFwk::CompatibleAbilityInfo::Unmarshalling(abilityParcel);
if (abilityInfoPtr == nullptr) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
abilityInfo_ = *abilityInfoPtr;
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueDataCmd::UnmarshalCallerInfo(std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
const char *strKeys[] = {
"SourceDeviceId", "CallerAppId"
};
std::string *strValues[] = {
&callerInfo_.sourceDeviceId, &callerInfo_.callerAppId
};
int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
for (int32_t i = 0; i < strLength; i++) {
cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
*strValues[i] = item->valuestring;
}
const char *numKeys[] = {
"Uid", "Pid", "CallerType", "Duid"
};
int32_t *numValues[] = {
&callerInfo_.uid, &callerInfo_.pid, &callerInfo_.callerType, &callerInfo_.duid
};
int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
for (int32_t i = 0; i < numLength; i++) {
cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
if (item == nullptr || !cJSON_IsNumber(item)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
*numValues[i] = item->valueint;
}
if (UnmarshalCallerInfoExtra(jsonStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueDataCmd::UnmarshalCallerInfoExtra(std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON *bundleName = nullptr;
std::vector<std::string> bundleNameList;
cJSON *bundleNames = cJSON_GetObjectItemCaseSensitive(rootValue, "BundleNames");
cJSON_ArrayForEach(bundleName, bundleNames) {
if (bundleName == nullptr || !cJSON_IsString(bundleName) || (bundleName->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
bundleNameList.push_back(bundleName->valuestring);
}
callerInfo_.bundleNames = bundleNameList;
cJSON *extraInfo = cJSON_GetObjectItemCaseSensitive(rootValue, "ExtraInfo");
if (extraInfo == nullptr || !cJSON_IsString(extraInfo) || (extraInfo->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON *extraInfoValue = cJSON_Parse(extraInfo->valuestring);
if (extraInfoValue == nullptr) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON *accessToken = cJSON_GetObjectItemCaseSensitive(extraInfoValue, EXTRO_INFO_JSON_KEY_ACCESS_TOKEN);
if (accessToken != nullptr && cJSON_IsNumber(accessToken)) {
callerInfo_.accessToken = accessToken->valueint;
}
cJSON *dmsVersion = cJSON_GetObjectItemCaseSensitive(extraInfoValue, DMS_VERSION_ID);
if (dmsVersion != nullptr && !cJSON_IsString(dmsVersion) && (dmsVersion->valuestring != nullptr)) {
callerInfo_.extraInfoJson[DMS_VERSION_ID] = dmsVersion->valuestring;
}
cJSON_Delete(extraInfoValue);
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueDataCmd::UnmarshalAccountInfo(std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON *accountType = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountType");
if (accountType == nullptr || !cJSON_IsNumber(accountType)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
accountInfo_.accountType = accountType->valueint;
cJSON *groupId = nullptr;
std::vector<std::string> groupIdList;
cJSON *groupIdListStr = cJSON_GetObjectItemCaseSensitive(rootValue, "groupIdList");
cJSON_ArrayForEach(groupId, groupIdListStr) {
if (groupId == nullptr || !cJSON_IsString(groupId) || (groupId->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
groupIdList.push_back(groupId->valuestring);
}
accountInfo_.groupIdList = groupIdList;
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueReplyCmd::Marshal(std::string &jsonStr)
{
cJSON *rootValue = cJSON_CreateObject();
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
std::string baseJsonStr;
if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
cJSON_AddNumberToObject(rootValue, "ReplyCmd", replyCmd_);
cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_);
cJSON_AddNumberToObject(rootValue, "Result", result_);
cJSON_AddStringToObject(rootValue, "Reason", reason_.c_str());
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;
}
int32_t DSchedContinueReplyCmd::Unmarshal(const std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string baseCmdStr = baseCmd->valuestring;
if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
const char *numKeys[] = {
"ReplyCmd", "AppVersion", "Result"
};
int32_t *numValues[] = {
&replyCmd_, &appVersion_, &result_
};
int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
for (int32_t i = 0; i < numLength; i++) {
cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
if (item == nullptr || !cJSON_IsNumber(item)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
*numValues[i] = item->valueint;
}
cJSON *reason = cJSON_GetObjectItemCaseSensitive(rootValue, "Reason");
if (reason == nullptr || !cJSON_IsString(reason) || (reason->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
reason_ = reason->valuestring;
cJSON_Delete(rootValue);
return ERR_OK;
}
int32_t DSchedContinueEndCmd::Marshal(std::string &jsonStr)
{
cJSON *rootValue = cJSON_CreateObject();
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
std::string baseJsonStr;
if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
cJSON_AddNumberToObject(rootValue, "Result", result_);
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;
}
int32_t DSchedContinueEndCmd::Unmarshal(const std::string &jsonStr)
{
cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
if (rootValue == nullptr) {
return INVALID_PARAMETERS_ERR;
}
cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
std::string baseCmdStr = baseCmd->valuestring;
if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
cJSON *result = cJSON_GetObjectItemCaseSensitive(rootValue, "Result");
if (result == nullptr || !cJSON_IsNumber(result)) {
cJSON_Delete(rootValue);
return INVALID_PARAMETERS_ERR;
}
result_ = result->valueint;
cJSON_Delete(rootValue);
return ERR_OK;
}
} // namespace DistributedSchedule
} // namespace OHOS
} // namespace OHOS

View File

@ -25,7 +25,8 @@ const std::string TAG = "DSchedContinueEventHandler";
}
DSchedContinueEventHandler::DSchedContinueEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
const std::shared_ptr<DSchedContinue> dschedContinue)
const std::shared_ptr<DSchedContinue>& dschedContinue) : AppExecFwk::EventHandler(runner),
dschedContinue_(dschedContinue)
{
}
@ -48,4 +49,4 @@ void DSchedContinueEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Poin
dContinue->ProcessEvent(event);
}
} // namespace DistributedSchedule
} // namespace OHOS
} // namespace OHOS