From ba16a6312d43810fe4537105cca33df687b9fa18 Mon Sep 17 00:00:00 2001 From: jusung son Date: Fri, 18 Aug 2023 16:13:30 +0900 Subject: [PATCH] Support RemoteException for C# Generator Change-Id: I2026724c180361dda912e8642476b55ff84dda89 Signed-off-by: jusung son --- idlc/gen/version2/cs_generator_base.cc | 25 ++++++++-- idlc/gen/version2/cs_generator_base.h | 1 + idlc/gen/version2/cs_generator_base_cb.h | 54 +++++++++++++++++++++ idlc/gen/version2/cs_group_generator.cc | 1 + idlc/gen/version2/cs_proxy_generator.cc | 3 ++ idlc/gen/version2/cs_proxy_generator_cb.h | 8 ++++ idlc/gen/version2/cs_stub_generator.cc | 80 ++++++++++++++++++++----------- idlc/gen/version2/cs_stub_generator.h | 2 + idlc/gen/version2/cs_stub_generator_cb.h | 28 ++++++++--- 9 files changed, 161 insertions(+), 41 deletions(-) diff --git a/idlc/gen/version2/cs_generator_base.cc b/idlc/gen/version2/cs_generator_base.cc index ef5b5ab..90027f7 100644 --- a/idlc/gen/version2/cs_generator_base.cc +++ b/idlc/gen/version2/cs_generator_base.cc @@ -53,6 +53,10 @@ void CsGeneratorBase::GenStructures(std::ofstream& stream) { } } +void CsGeneratorBase::GenRemoteException(std::ofstream& stream) { + stream << CB_REMOTE_EXCEPTION_BASE; +} + void CsGeneratorBase::GenUsingNamespace(std::ofstream& stream) { stream << CB_USING_NAMESPACE; } @@ -158,6 +162,9 @@ std::string CsGeneratorBase::GetSerializer(const Structure& st) { void CsGeneratorBase::GenSerializer(std::ofstream& stream) { stream << GetSerializer(); + + if (GetChannelType() != ChannelType::TYPE_GROUP) + stream << CB_REMOTE_EXCEPTION_SERIALIZE; } std::string CsGeneratorBase::GetSerializer() { @@ -542,8 +549,11 @@ std::string CsGeneratorBase::GenUnitMapParamType(const BaseType& base_type, std::string ret; if (base_type.GetUserDefinedType() == BaseType::UserType::DELEGATE) ret = "CallbackBase"; + else if (base_type.GetFullName() == "[REMOTE_EXCEPTION]") + ret = "RemoteException"; else ret = type_str; + return ret; } @@ -556,6 +566,8 @@ std::string CsGeneratorBase::GenUnitMapParamInit(const Interface& iface, ret = "value = new CallbackBase(1, false);"; } else if (type_str == "string") { ret = "value = new string(\"\");"; + } else if (type_str == "[REMOTE_EXCEPTION]") { + ret = "value = new RemoteException();"; } else { ret = "value = new " + type_str + "();"; } @@ -626,11 +638,9 @@ std::string CsGeneratorBase::GenUnitMaUintRead(const Interface& iface, ret = "int enum_int = unit.parcel.ReadInt();" + NLine(1) + Tab(1) + "value = (" + type_str + ")enum_int;"; } else if (base_type.GetUserDefinedType() == BaseType::UserType::DELEGATE) { - ret = "value = new CallbackBase(1, false);" + NLine(1) + Tab(1) + - "CallbackBase.Deserialize(unit.parcel, value);"; + ret = "CallbackBase.Deserialize(unit.parcel, value);"; } else { - ret = "value = new " + type_str + "();" + NLine(1) + Tab(1) + - iface.GetID() + ".Deserialize(unit.parcel, value);"; + ret = iface.GetID() + ".Deserialize(unit.parcel, value);"; } return ret; @@ -660,6 +670,11 @@ std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) { BaseType bS("string", "string"); BaseType bB("bool", "bool"); + if (GetChannelType() != ChannelType::TYPE_GROUP) { + BaseType bE("[REMOTE_EXCEPTION]", "", BaseType::UserType::STRUCTURE); + types[bE.GetFullName()] = bE; + } + types[bI.GetFullName()] = bI; types[bS.GetFullName()] = bS; types[bB.GetFullName()] = bB; @@ -706,7 +721,7 @@ std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) { .Change("", GenUnitMaUintWrite(iface, base_type)); } - return code; + return AddIndent(TAB_SIZE, code); } void CsGeneratorBase::GenUnitMap(std::ofstream& stream, const Interface& iface, diff --git a/idlc/gen/version2/cs_generator_base.h b/idlc/gen/version2/cs_generator_base.h index 6754960..58df4bc 100644 --- a/idlc/gen/version2/cs_generator_base.h +++ b/idlc/gen/version2/cs_generator_base.h @@ -77,6 +77,7 @@ class CsGeneratorBase : public Generator { void GenEnum(std::ofstream& stream, const Block& block, int tab); std::string GenEnum(const Block& block, int tab_size); bool HasDelegate(const Interface& iface); + void GenRemoteException(std::ofstream& stream); protected: const int TAB_SIZE = 4; diff --git a/idlc/gen/version2/cs_generator_base_cb.h b/idlc/gen/version2/cs_generator_base_cb.h index 6c6d980..432fcbd 100644 --- a/idlc/gen/version2/cs_generator_base_cb.h +++ b/idlc/gen/version2/cs_generator_base_cb.h @@ -26,6 +26,55 @@ using Tizen.Applications.RPCPort; )__cs_cb"; + +const char CB_REMOTE_EXCEPTION_BASE[] = +R"__cs_cb( + public class RemoteException : Exception + { + public RemoteException() + { + } + + public RemoteException(string message) + { + this.Message = message; + } + + public RemoteException(string message, int cause) + { + this.Message = message; + this.Cause = cause; + } + + public new string Message { get; set; } + public int Cause { get; set; } + } + +)__cs_cb"; + +const char CB_REMOTE_EXCEPTION_SERIALIZE[] = +R"__cs_cb( + private static void Serialize(Parcel h, RemoteException param) + { + UnitMap map = new UnitMap(); + map.Write("cause", "int", param.Cause); + map.Write("message", "string", param.Message); + + map.Serialize(h); + } + + private static void Deserialize(Parcel h, RemoteException param) + { + UnitMap map = new UnitMap(); + map.Deserialize(h); + map.Read("cause", "int", out int cause); + param.Cause = cause; + map.Read("message", "string", out string message); + param.Message = message; + } +)__cs_cb"; + + const char CB_UNIT_BASE[] = R"__cs_cb( internal class Unit @@ -123,6 +172,11 @@ internal class UnitMap map[unit.name] = unit; } } + + internal bool ContainsName(string name) + { + return map.ContainsKey(name); + } } )__cs_cb"; diff --git a/idlc/gen/version2/cs_group_generator.cc b/idlc/gen/version2/cs_group_generator.cc index 8af7287..7da9e85 100644 --- a/idlc/gen/version2/cs_group_generator.cc +++ b/idlc/gen/version2/cs_group_generator.cc @@ -26,6 +26,7 @@ namespace version2 { CsGroupGen::CsGroupGen(std::shared_ptr doc) : CsGeneratorBase(doc) {} void CsGroupGen::OnInitGen(std::ofstream& stream) { + SetChannelType(Generator::ChannelType::TYPE_GROUP); GenVersion(stream); GenUsingNamespace(stream); diff --git a/idlc/gen/version2/cs_proxy_generator.cc b/idlc/gen/version2/cs_proxy_generator.cc index ce7a10c..960eee5 100644 --- a/idlc/gen/version2/cs_proxy_generator.cc +++ b/idlc/gen/version2/cs_proxy_generator.cc @@ -26,6 +26,7 @@ namespace version2 { CsProxyGen::CsProxyGen(std::shared_ptr doc) : CsGeneratorBase(doc) {} void CsProxyGen::OnInitGen(std::ofstream& stream) { + SetChannelType(Generator::ChannelType::TYPE_PROXY); GenVersion(stream); GenUsingNamespace(stream); GenNamespace(stream); @@ -38,6 +39,7 @@ void CsProxyGen::GenNamespace(std::ofstream& stream) { GenBrace(stream, 0, [&]() { stream << "namespace " << GetFileNamespace() << NLine(1); GenBrace(stream, 0, [&]() { + GenRemoteException(stream); GenStructures(stream); stream << Tab(1) << "namespace Proxy" << NLine(1); GenBrace(stream, TAB_SIZE, [&]() { @@ -202,6 +204,7 @@ void CsProxyGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { } if (decl.GetType().ToString() != "void") { + st += AddIndent(TAB_SIZE * 5, CB_INVOCATION_REMOTE_EXCEPTION); st += AddIndent(TAB_SIZE * 6, NLine(1) + ConvertTypeToDeserializer( decl.GetType(), "ret", "[RESULT]", "mapReceived")); diff --git a/idlc/gen/version2/cs_proxy_generator_cb.h b/idlc/gen/version2/cs_proxy_generator_cb.h index b69f87c..31c76c7 100644 --- a/idlc/gen/version2/cs_proxy_generator_cb.h +++ b/idlc/gen/version2/cs_proxy_generator_cb.h @@ -260,4 +260,12 @@ R"__cs_cb( UnitMap mapReceived; } )__cs_cb"; +const char CB_INVOCATION_REMOTE_EXCEPTION[] = +R"__cs_cb( if (mapReceived.ContainsName("[REMOTE_EXCEPTION]")) + { + mapReceived.Read("[REMOTE_EXCEPTION]", "remote_exception", out RemoteException exception); + throw exception; + } +)__cs_cb"; + #endif // IDLC_CS_GEN_VERSION2_CS_PROXY_GENRATOR_CB_H_ \ No newline at end of file diff --git a/idlc/gen/version2/cs_stub_generator.cc b/idlc/gen/version2/cs_stub_generator.cc index 73a8e25..31dbd8b 100644 --- a/idlc/gen/version2/cs_stub_generator.cc +++ b/idlc/gen/version2/cs_stub_generator.cc @@ -26,6 +26,7 @@ namespace version2 { CsStubGen::CsStubGen(std::shared_ptr doc) : CsGeneratorBase(doc) {} void CsStubGen::OnInitGen(std::ofstream& stream) { + SetChannelType(Generator::ChannelType::TYPE_STUB); GenVersion(stream); GenUsingNamespace(stream); GenNamespace(stream); @@ -38,6 +39,7 @@ void CsStubGen::GenNamespace(std::ofstream& stream) { GenBrace(stream, 0, [&]() { stream << "namespace " << GetFileNamespace() << NLine(1); GenBrace(stream, 0, [&]() { + GenRemoteException(stream); GenStructures(stream); stream << Tab(1) << "namespace Stub" << NLine(1); GenBrace(stream, TAB_SIZE, [&]() { GenInterfaces(stream); }); @@ -114,6 +116,51 @@ void CsStubGen::GenReceivedEvent(std::ofstream& stream, stream << CB_ON_RECEIVED_EVENT_BACK; } +std::string CsStubGen::GetInvocationMethod(const Declaration& decl) { + std::string code; + if (decl.GetType().ToString() != "void") { + code = ReplaceAll(CB_INVOCATION_REMOTE_EXCEPTION) + .Change("", GetInvocationMethodParam(decl, true)) + .Change("", + ConvertTypeToSerializer("retVal", "[RESULT]", + GetTypeString(decl.GetType()), + "result_map")); + } else { + code = NLine(1) + GetInvocationMethodParam(decl, false); + } + + return code; +} + +std::string CsStubGen::GetInvocationMethodParam(const Declaration& decl, + bool hasRet) { + int cnt = 1; + std::string code; + + if (hasRet) + code = "var retVal = "; + + code += "b." + decl.GetID() + "("; + for (const auto& i : decl.GetParameters()) { + if (cnt != 1) { + code += ", "; + } + + std::string v = "param" + std::to_string(cnt); + auto& pt = i->GetParameterType(); + if (pt.GetDirection() == ParameterType::Direction::OUT) { + code += "out " + ConvertTypeToString(pt.GetBaseType()) + " "; + } else if (pt.GetDirection() == ParameterType::Direction::REF) { + code += "ref "; + } + code += v; + cnt++; + } + + code += ");\n"; + + return code; +} void CsStubGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { int cnt = 1; @@ -132,34 +179,14 @@ void CsStubGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { } // Invoke - cnt = 1; std::string m; - bool hasRet = false; - m += NLine(1); - if (decl.GetType().ToString() != "void") { - m += "var retVal = "; - hasRet = true; - } - - m += "b." + decl.GetID() + "("; - for (const auto& i : decl.GetParameters()) { - if (cnt != 1) { - m += ", "; - } + m += NLine(1) + "UnitMap result_map = new UnitMap();"; - std::string v = "param" + std::to_string(cnt); - auto& pt = i->GetParameterType(); - if (pt.GetDirection() == ParameterType::Direction::OUT) { - m += "out " + ConvertTypeToString(pt.GetBaseType()) + " "; - } else if (pt.GetDirection() == ParameterType::Direction::REF) { - m += "ref "; - } - m += v; - cnt++; - } + m += ReplaceAll(CB_INVOCATION_CHECK_PRIVILEGE) + .Change("", + AddIndent(TAB_SIZE, GetInvocationMethod(decl))); - m += ");\n"; stream << AddIndent(TAB_SIZE * 9, m); // Serialize @@ -178,11 +205,6 @@ void CsStubGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { GetTypeString(pt.GetBaseType()), "result_map"); } - if (hasRet) { - m += ConvertTypeToSerializer("retVal", "[RESULT]", - GetTypeString(decl.GetType()), "result_map"); - } - m += "result_map.Serialize(result);\n"; m += "result.Send(port);\n"; stream << AddIndent(TAB_SIZE * 9, m); diff --git a/idlc/gen/version2/cs_stub_generator.h b/idlc/gen/version2/cs_stub_generator.h index 35d5f2b..b6c9f6a 100644 --- a/idlc/gen/version2/cs_stub_generator.h +++ b/idlc/gen/version2/cs_stub_generator.h @@ -48,6 +48,8 @@ class CsStubGen : public CsGeneratorBase { void GenDeclarations(std::ofstream& stream, const Declarations& decls); void GenInvocation(std::ofstream& stream, const Declaration& decl); void GenDisposable(std::ofstream& stream, const Interface& iface); + std::string GetInvocationMethodParam(const Declaration& decl, bool hasRet); + std::string GetInvocationMethod(const Declaration& decl); }; } // namespace version2 diff --git a/idlc/gen/version2/cs_stub_generator_cb.h b/idlc/gen/version2/cs_stub_generator_cb.h index 0340399..5172901 100644 --- a/idlc/gen/version2/cs_stub_generator_cb.h +++ b/idlc/gen/version2/cs_stub_generator_cb.h @@ -165,12 +165,6 @@ R"__cs_cb( map.Read("[METHOD]", "int", out cmd); - if (b._is_app && CheckPrivilege((MethodId)cmd, b) == false) - { - //send exception - return false; - } - switch ((MethodId)cmd) { )__cs_cb"; @@ -272,6 +266,27 @@ R"__cs_cb( } )__cs_cb"; +constexpr const char CB_INVOCATION_CHECK_PRIVILEGE[] = +R"__cs_cb( +if (b._is_app && CheckPrivilege((MethodId)cmd, b) == false) +{ + RemoteException ex = new RemoteException("Permission denied", (int)Tizen.Internals.Errors.ErrorCode.PermissionDenied); + result_map.Write("[REMOTE_EXCEPTION]", "remote_exception", ex); +} else { + +} +)__cs_cb"; + +constexpr const char CB_INVOCATION_REMOTE_EXCEPTION[] = +R"__cs_cb(try +{ + + +} catch (RemoteException ex) +{ + result_map.Write("[REMOTE_EXCEPTION]", "remote_exception", ex); +})__cs_cb"; + constexpr const char CB_INVOCATION_RESULT_PRE[] = R"__cs_cb( ParcelHeader header = p.GetHeader(); @@ -279,7 +294,6 @@ ParcelHeader resultHeader = result.GetHeader(); resultHeader.SetTag(_tidlVersion); resultHeader.SetSequenceNumber(header.GetSequenceNumber()); -UnitMap result_map = new UnitMap(); result_map.Write("[METHOD]", "int", (int)MethodId.__Result); )__cs_cb"; -- 2.7.4