!673 同步3.1版本【分布式want】

Merge pull request !673 from xinking129/master
This commit is contained in:
openharmony_ci 2023-07-13 14:22:19 +00:00 committed by Gitee
commit 201fb349ba
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 173 additions and 62 deletions

View File

@ -410,6 +410,23 @@ public:
*/
std::vector<bool> GetBoolArrayParam(const std::string& key) const;
/**
* @description: Sets a parameter value of the IRemoteObject type.
* @param key Indicates the key matching the parameter.
* @param value Indicates the IRemoteObject value of the parameter.
* @return Returns this want object containing the parameter value.
*/
DistributedWant& SetParam(const std::string& key, const sptr<IRemoteObject>& remoteObject);
/**
* @description: Obtains a IRemoteObject-type value matching the given key.
* @param key Indicates the key of WantParams.
* @param defaultValue Indicates the default IRemoteObject-type value.
* @return Returns the IRemoteObject-type value of the parameter matching the given key;
* returns the nullptr if the key does not exist.
*/
sptr<IRemoteObject> GetRemoteObject(const std::string& key) const;
/**
* @description: Sets a parameter value of the boolean type.
* @param key Indicates the key matching the parameter.
@ -816,7 +833,6 @@ private:
static bool ParseContent(const std::string& content, std::string& prop, std::string& value);
static bool ParseUriInternal(
const std::string& content, OHOS::AppExecFwk::ElementName& element, DistributedWant& want);
static bool CheckBeforeParseUri(const std::string& uri);
bool ReadFromParcel(Parcel& parcel);
static bool CheckAndSetParameters(
DistributedWant& want, const std::string& key, std::string& prop, const std::string& value);

View File

@ -114,6 +114,8 @@ private:
VALUE_TYPE_WANTPARAMS = 101,
VALUE_TYPE_ARRAY = 102,
VALUE_TYPE_FD = 103,
VALUE_TYPE_REMOTE_OBJECT = 104
};
bool WriteArrayToParcel(Parcel& parcel, AAFwk::IArray* ao) const;
@ -140,7 +142,9 @@ private:
bool ReadFromParcelArrayLong(Parcel& parcel, sptr<AAFwk::IArray>& ao);
bool ReadFromParcelArrayFloat(Parcel& parcel, sptr<AAFwk::IArray>& ao);
bool ReadFromParcelArrayDouble(Parcel& parcel, sptr<AAFwk::IArray>& ao);
bool ReadFromParcelWantParamWrapper(Parcel& parcel, const std::string& key);
bool ReadFromParcelWantParamWrapper(Parcel& parcel, const std::string& key, int type);
bool ReadFromParcelFD(Parcel& parcel, const std::string& key);
bool ReadFromParcelRemoteObject(Parcel& parcel, const std::string& key);
bool WriteArrayToParcelString(Parcel& parcel, AAFwk::IArray* ao) const;
bool WriteArrayToParcelBool(Parcel& parcel, AAFwk::IArray* ao) const;
@ -163,6 +167,8 @@ private:
bool WriteToParcelFloat(Parcel& parcel, sptr<AAFwk::IInterface>& o) const;
bool WriteToParcelDouble(Parcel& parcel, sptr<AAFwk::IInterface>& o) const;
bool WriteToParcelWantParams(Parcel& parcel, sptr<AAFwk::IInterface>& o) const;
bool WriteToParcelFD(Parcel& parcel, const DistributedWantParams& value) const;
bool WriteToParcelRemoteObject(Parcel& parcel, const DistributedWantParams& value) const;
bool DoMarshalling(Parcel& parcel) const;
bool ReadUnsupportedData(Parcel& parcel, const std::string& key, int type);

View File

@ -33,6 +33,7 @@
#include "int_wrapper.h"
#include "long_wrapper.h"
#include "parcel_macro_base.h"
#include "remote_object_wrapper.h"
#include "short_wrapper.h"
#include "string_ex.h"
#include "string_wrapper.h"
@ -45,6 +46,9 @@ namespace DistributedSchedule {
namespace {
const std::regex NUMBER_REGEX("^[-+]?([0-9]+)([.]([0-9]+))?$");
const std::string TAG = "DistributedWant";
const char* REMOTE_OBJECT = "RemoteObject";
const char* TYPE_PROPERTY = "type";
const char* VALUE_PROPERTY = "value";
}; // namespace
const std::string DistributedWant::ACTION_PLAY("action.system.play");
const std::string DistributedWant::ACTION_HOME("action.system.home");
@ -117,6 +121,7 @@ DistributedWant::DistributedWant(const AAFwk::Want& want)
(tp == DistributedWantParams::VALUE_TYPE_DOUBLE) ||
(tp == DistributedWantParams::VALUE_TYPE_STRING) ||
(tp == DistributedWantParams::VALUE_TYPE_ARRAY) ||
(tp == DistributedWantParams::VALUE_TYPE_REMOTE_OBJECT) ||
(tp == DistributedWantParams::VALUE_TYPE_WANTPARAMS)) {
parameters_.SetParam(it->first, it->second);
}
@ -367,7 +372,42 @@ std::vector<bool> DistributedWant::GetBoolArrayParam(const std::string& key) con
return array;
}
DistributedWant& DistributedWant::SetParam(const std::string&key, bool value)
DistributedWant& DistributedWant::SetParam(const std::string& key, const sptr<IRemoteObject>& remoteObject)
{
DistributedWantParams wp;
wp.SetParam(TYPE_PROPERTY, AAFwk::String::Box(AAFwk::REMOTE_OBJECT));
wp.SetParam(AAFwk::VALUE_PROPERTY, AAFwk::RemoteObjectWrap::Box(remoteObject));
parameters_.SetParam(key, DistributedWantParamWrapper::Box(wp));
return *this;
}
sptr<IRemoteObject> DistributedWant::GetRemoteObject(const std::string& key) const
{
auto value = parameters_.GetParam(key);
IDistributedWantParams* iwp = IDistributedWantParams::Query(value);
if (iwp == nullptr) {
return nullptr;
}
auto wp = DistributedWantParamWrapper::Unbox(iwp);
auto type = wp.GetParam(TYPE_PROPERTY);
AAFwk::IString* iString = AAFwk::IString::Query(type);
if (iString == nullptr) {
return nullptr;
}
if (REMOTE_OBJECT != AAFwk::String::Unbox(iString)) {
return nullptr;
}
auto remoteObjVal = wp.GetParam(VALUE_PROPERTY);
AAFwk::IRemoteObjectWrap* iRemoteObj = AAFwk::IRemoteObjectWrap::Query(remoteObjVal);
if (iRemoteObj == nullptr) {
return nullptr;
}
return AAFwk::RemoteObjectWrap::UnBox(iRemoteObj);
}
DistributedWant& DistributedWant::SetParam(const std::string& key, bool value)
{
parameters_.SetParam(key, AAFwk::Boolean::Box(value));
return *this;
@ -835,31 +875,23 @@ DistributedWant* DistributedWant::CloneOperation()
return want;
}
bool DistributedWant::CheckBeforeParseUri(const std::string& uri)
DistributedWant* DistributedWant::ParseUri(const std::string& uri)
{
if (uri.length() <= 0) {
return false;
return nullptr;
}
std::string head = WANT_HEADER;
std::string end = ";end";
if (uri.find(head) != 0) {
return false;
return nullptr;
}
if (uri.rfind(end) != (uri.length() - end.length())) {
return false;
}
return true;
}
DistributedWant* DistributedWant::ParseUri(const std::string& uri)
{
if (!CheckBeforeParseUri(uri)) {
return nullptr;
}
bool ret = true;
std::string content;
std::size_t pos;
std::size_t begin = WANT_HEADER.length();
std::size_t begin = head.length();
ElementName element;
DistributedWant* want = new (std::nothrow) DistributedWant();
if (want == nullptr) {

View File

@ -32,9 +32,16 @@
#include "string_wrapper.h"
#include "want_params_wrapper.h"
#include "zchar_wrapper.h"
#include "remote_object_wrapper.h"
namespace OHOS {
namespace DistributedSchedule {
namespace {
const char* FD = "FD";
const char* REMOTE_OBJECT = "RemoteObject";
const char* TYPE_PROPERTY = "type";
const char* VALUE_PROPERTY = "value";
}
DistributedUnsupportedData::~DistributedUnsupportedData()
{
if (buffer != nullptr) {
@ -162,6 +169,9 @@ bool DistributedWantParams::NewParams(const DistributedWantParams& source, Distr
dest.params_[it->first] = AAFwk::Float::Box(AAFwk::Float::Unbox(AAFwk::IFloat::Query(o)));
} else if (AAFwk::IDouble::Query(o) != nullptr) {
dest.params_[it->first] = AAFwk::Double::Box(AAFwk::Double::Unbox(AAFwk::IDouble::Query(o)));
} else if (AAFwk::IRemoteObjectWrap::Query(o) != nullptr) {
dest.params_[it->first] =
AAFwk::RemoteObjectWrap::Box(AAFwk::RemoteObjectWrap::UnBox(AAFwk::IRemoteObjectWrap::Query(o)));
} else if (IDistributedWantParams::Query(o) != nullptr) {
DistributedWantParams newDest(DistributedWantParamWrapper::Unbox(IDistributedWantParams::Query(o)));
dest.params_[it->first] = DistributedWantParamWrapper::Box(newDest);
@ -415,6 +425,19 @@ bool DistributedWantParams::WriteToParcelBool(Parcel& parcel, sptr<IInterface>&
bool DistributedWantParams::WriteToParcelWantParams(Parcel& parcel, sptr<IInterface>& o) const
{
DistributedWantParams value = DistributedWantParamWrapper::Unbox(IDistributedWantParams::Query(o));
auto type = value.GetParam(TYPE_PROPERTY);
AAFwk::IString *typeP = AAFwk::IString::Query(type);
if (typeP != nullptr) {
std::string typeValue = AAFwk::String::Unbox(typeP);
if (typeValue == FD) {
return WriteToParcelFD(parcel, value);
}
if (typeValue == REMOTE_OBJECT) {
return WriteToParcelRemoteObject(parcel, value);
}
}
if (!parcel.WriteInt32(VALUE_TYPE_WANTPARAMS)) {
return false;
}
@ -422,6 +445,47 @@ bool DistributedWantParams::WriteToParcelWantParams(Parcel& parcel, sptr<IInterf
static_cast<DistributedWantParamWrapper*>(IDistributedWantParams::Query(o))->ToString()));
}
bool DistributedWantParams::WriteToParcelFD(Parcel& parcel, const DistributedWantParams& value) const
{
if (!parcel.WriteInt32(VALUE_TYPE_FD)) {
return false;
}
auto fdWrap = value.GetParam(VALUE_PROPERTY);
AAFwk::IInteger *fdIWrap = AAFwk::IInteger::Query(fdWrap);
if (fdIWrap != nullptr) {
int fd = AAFwk::Integer::Unbox(fdIWrap);
auto messageParcel = static_cast<MessageParcel*>(&parcel);
if (messageParcel == nullptr) {
return false;
}
bool ret = messageParcel->WriteFileDescriptor(fd);
return ret;
}
return false;
}
bool DistributedWantParams::WriteToParcelRemoteObject(Parcel& parcel, const DistributedWantParams& value) const
{
if (!parcel.WriteInt32(VALUE_TYPE_REMOTE_OBJECT)) {
return false;
}
auto remoteObjectWrap = value.GetParam(VALUE_PROPERTY);
AAFwk::IRemoteObjectWrap *remoteObjectIWrap = AAFwk::IRemoteObjectWrap::Query(remoteObjectWrap);
if (remoteObjectIWrap != nullptr) {
auto remoteObject = AAFwk::RemoteObjectWrap::UnBox(remoteObjectIWrap);
auto messageParcel = static_cast<MessageParcel*>(&parcel);
if (messageParcel == nullptr) {
return false;
}
bool ret = messageParcel->WriteRemoteObject(remoteObject);
return ret;
}
return false;
}
bool DistributedWantParams::WriteToParcelByte(Parcel& parcel, sptr<IInterface>& o) const
{
AAFwk::byte value = AAFwk::Byte::Unbox(AAFwk::IByte::Query(o));
@ -568,29 +632,7 @@ bool DistributedWantParams::DoMarshalling(Parcel& parcel) const
bool DistributedWantParams::Marshalling(Parcel& parcel) const
{
Parcel tempParcel;
if (!DoMarshalling(tempParcel)) {
return false;
}
int size = static_cast<int>(tempParcel.GetDataSize());
if (!parcel.WriteInt32(size)) {
return false;
}
const uint8_t* buffer = tempParcel.ReadUnpadBuffer(size);
if (buffer == nullptr) {
return false;
}
// Corresponding to Parcel#writeByteArray() in Java.
if (!parcel.WriteInt32(size)) {
return false;
}
if (!parcel.WriteBuffer(buffer, size)) {
return false;
}
return true;
return DoMarshalling(parcel);
}
template<typename dataType, typename className>
@ -1026,8 +1068,16 @@ bool DistributedWantParams::ReadFromParcelInt(Parcel& parcel, const std::string&
}
}
bool DistributedWantParams::ReadFromParcelWantParamWrapper(Parcel& parcel, const std::string& key)
bool DistributedWantParams::ReadFromParcelWantParamWrapper(Parcel& parcel, const std::string& key, int type)
{
if (type == VALUE_TYPE_FD) {
return ReadFromParcelFD(parcel, key);
}
if (type == VALUE_TYPE_REMOTE_OBJECT) {
return ReadFromParcelRemoteObject(parcel, key);
}
std::u16string value = parcel.ReadString16();
sptr<IInterface> intf = DistributedWantParamWrapper::Parse(Str16ToStr8(value));
if (intf) {
@ -1036,6 +1086,30 @@ bool DistributedWantParams::ReadFromParcelWantParamWrapper(Parcel& parcel, const
return true;
}
bool DistributedWantParams::ReadFromParcelFD(Parcel& parcel, const std::string& key)
{
auto messageParcel = static_cast<MessageParcel*>(&parcel);
auto fd = messageParcel->ReadFileDescriptor();
DistributedWantParams wp;
wp.SetParam(TYPE_PROPERTY, AAFwk::String::Box(FD));
wp.SetParam(VALUE_PROPERTY, AAFwk::Integer::Box(fd));
sptr<IDistributedWantParams> pWantParams = DistributedWantParamWrapper::Box(wp);
SetParam(key, pWantParams);
return true;
}
bool DistributedWantParams::ReadFromParcelRemoteObject(Parcel& parcel, const std::string& key)
{
auto messageParcel = static_cast<MessageParcel*>(&parcel);
auto remoteObject = messageParcel->ReadRemoteObject();
DistributedWantParams wp;
wp.SetParam(TYPE_PROPERTY, AAFwk::String::Box(REMOTE_OBJECT));
wp.SetParam(VALUE_PROPERTY, AAFwk::RemoteObjectWrap::Box(remoteObject));
sptr<IDistributedWantParams> pWantParams = DistributedWantParamWrapper::Box(wp);
SetParam(key, pWantParams);
return true;
}
bool DistributedWantParams::ReadFromParcelLong(Parcel& parcel, const std::string& key)
{
int64_t value;
@ -1089,7 +1163,8 @@ bool DistributedWantParams::ReadUnsupportedData(Parcel& parcel, const std::strin
if (!parcel.ReadInt32(bufferSize)) {
return false;
}
if (bufferSize < 0) {
static constexpr int32_t maxAllowedSize = 100 * 1024 * 1024;
if (bufferSize < 0 || bufferSize > maxAllowedSize) {
return false;
}
@ -1141,7 +1216,9 @@ bool DistributedWantParams::ReadFromParcelParam(Parcel& parcel, const std::strin
case VALUE_TYPE_DOUBLE:
return ReadFromParcelDouble(parcel, key);
case VALUE_TYPE_WANTPARAMS:
return ReadFromParcelWantParamWrapper(parcel, key);
case VALUE_TYPE_FD:
case VALUE_TYPE_REMOTE_OBJECT:
return ReadFromParcelWantParamWrapper(parcel, key, type);
case VALUE_TYPE_NULL:
break;
case VALUE_TYPE_PARCELABLE:
@ -1188,28 +1265,8 @@ bool DistributedWantParams::ReadFromParcel(Parcel& parcel)
DistributedWantParams* DistributedWantParams::Unmarshalling(Parcel& parcel)
{
int32_t bufferSize;
if (!parcel.ReadInt32(bufferSize)) {
return nullptr;
}
// Corresponding to Parcel#writeByteArray() in Java.
int32_t length;
if (!parcel.ReadInt32(length)) {
return nullptr;
}
const uint8_t* dataInBytes = parcel.ReadUnpadBuffer(bufferSize);
if (dataInBytes == nullptr) {
return nullptr;
}
Parcel tempParcel;
if (!tempParcel.WriteBuffer(dataInBytes, bufferSize)) {
return nullptr;
}
DistributedWantParams* wantParams = new (std::nothrow) DistributedWantParams();
if (wantParams != nullptr && !wantParams->ReadFromParcel(tempParcel)) {
if (wantParams != nullptr && !wantParams->ReadFromParcel(parcel)) {
delete wantParams;
wantParams = nullptr;
}