Remove the constructor 49/278349/6
authorjh9216.park <jh9216.park@samsung.com>
Tue, 19 Jul 2022 10:44:25 +0000 (06:44 -0400)
committerjh9216.park <jh9216.park@samsung.com>
Wed, 20 Jul 2022 06:03:18 +0000 (02:03 -0400)
- Remove the constructor to force it to use the sender apppid

Change-Id: I19bd50e00ff98b37b6b8fb955a6d7dab93e09443
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
idlc/gen/c_group_body_gen_cb.h
idlc/gen/c_group_header_gen_cb.h
idlc/gen/cpp_group_body_gen_cb.h
idlc/gen/cpp_group_header_gen_cb.h
idlc/gen/cs_group_gen_cb.h

index f52d3c4..b40a67e 100644 (file)
@@ -84,6 +84,8 @@ R"__c_cb(
 typedef struct <PREFIX>_<NAME>_s {
   <PREFIX>_<NAME>_callback_s callback;
   event_handler_h event_handler;
+  char *sender_appid;
+  bool is_system;
   GRecMutex mutex;
   void *user_data;
 } <PREFIX>_<NAME>_t;
@@ -156,26 +158,22 @@ static bundle *__get_bundle_from_parcel(rpc_port_parcel_h p, bundle* b)
   return b;
 }
 
-static const char *__get_appid()
+static char *__get_event_name(const char *appid, bool is_system, const char *iface_name)
 {
-  static char *str = nullptr;
-
-  if (str)
-    return str;
-
-  char *id = nullptr;
-  int ret = app_get_id(&id);
-  if (ret != APP_ERROR_NONE)
-    id = strdup("system");
-
-  str = id;
-  return str;
-}
+  size_t size;
+  char *buf;
+
+  if (is_system) {
+    size = strlen("tizen.system.event.tidl_iface_") + strlen(iface_name) + 1;
+    buf = calloc(1, size);
+    if (!buf)
+      return nullptr;
+    snprintf(buf, size, "tizen.system.event.tidl_iface_%s", iface_name);
+    return buf;
+  }
 
-static char *__get_event_name(const char *appid, const char *iface_name)
-{
-  size_t size = strlen(appid) + strlen(iface_name) + 19;
-  char *buf = calloc(1, size);
+  size = strlen(appid) + strlen(iface_name) + 19;
+  buf = calloc(1, size);
   if (!buf)
     return nullptr;
   snprintf(buf, size, "event.%s.tidl_iface_%s", appid, iface_name);
@@ -217,7 +215,7 @@ static void __<NAME>_event_system_cb(const char *event_name, bundle *event_data,
   rpc_port_parcel_destroy(p);
 }
 
-int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, <PREFIX>_<NAME>_h *h)
+int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, bool is_system, <PREFIX>_<NAME>_h *h)
 {
   <PREFIX>_<NAME>_t *handle;
   int ret;
@@ -234,32 +232,44 @@ int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s
     return RPC_PORT_ERROR_OUT_OF_MEMORY;
   }
 
-  g_rec_mutex_init(&handle->mutex);
-  handle->callback = *callback;
-  handle->user_data = user_data;
-
-  ev_name = __get_event_name(sender_appid, "<NAME>");
-  if (ev_name == nullptr)
+  ev_name = __get_event_name(sender_appid, is_system, "<NAME>");
+  if (ev_name == nullptr) {
+    free(handle);
     return RPC_PORT_ERROR_OUT_OF_MEMORY;
+  }
 
   ret = event_add_event_handler(ev_name, __<NAME>_event_system_cb, handle, &handle->event_handler);
   free(ev_name);
 
   if (ret != EVENT_ERROR_NONE) {
     _E("Failed to register events. result(%d)", ret);
+    free(handle);
     return RPC_PORT_ERROR_INVALID_PARAMETER;
   }
 
+  handle->sender_appid = strdup(sender_appid);
+  if (handle->sender_appid == nullptr) {
+    _E("Out of memory");
+    event_remove_event_handler(handle->event_handler);
+    free(handle);
+    return RPC_PORT_ERROR_OUT_OF_MEMORY;
+  }
+
+  g_rec_mutex_init(&handle->mutex);
+  handle->callback = *callback;
+  handle->user_data = user_data;
+  handle->is_system = is_system;
+
   *h = handle;
 
   return RPC_PORT_ERROR_NONE;
 }
 
-int <PREFIX>_<NAME>_create_for_sender(<PREFIX>_<NAME>_h *h)
+int <PREFIX>_<NAME>_create_for_sender(const char *sender_appid, bool is_system, <PREFIX>_<NAME>_h *h)
 {
   <PREFIX>_<NAME>_t *handle;
 
-  if (h == nullptr) {
+  if (h == nullptr || sender_appid == nullptr) {
     _E("Invalid parameter");
     return RPC_PORT_ERROR_INVALID_PARAMETER;
   }
@@ -270,7 +280,15 @@ int <PREFIX>_<NAME>_create_for_sender(<PREFIX>_<NAME>_h *h)
     return RPC_PORT_ERROR_OUT_OF_MEMORY;
   }
 
+  handle->sender_appid = strdup(sender_appid);
+  if (handle->sender_appid == nullptr) {
+    _E("Out of memory");
+    free(handle);
+    return RPC_PORT_ERROR_OUT_OF_MEMORY;
+  }
+
   g_rec_mutex_init(&handle->mutex);
+  handle->is_system = is_system;
   *h = handle;
 
   return RPC_PORT_ERROR_NONE;
@@ -289,6 +307,8 @@ int <PREFIX>_<NAME>_destroy(<PREFIX>_<NAME>_h h)
 
   if (h->event_handler)
     event_remove_event_handler(h->event_handler);
+  if (h->sender_appid)
+    free(h->sender_appid);
 
   free(h);
 
@@ -349,7 +369,7 @@ void <PREFIX>_<NAME>_invoke_<METHOD_NAME>(<PREFIX>_<NAME>_h h<METHOD_PARAMS>)
     return;
   }
 
-  int ret = event_publish_app_event(__get_event_name(__get_appid(), "<NAME>"), b);
+  int ret = event_publish_app_event(__get_event_name(h->sender_appid, h->is_system, "<NAME>"), b);
   if (ret != EVENT_ERROR_NONE) {
     _E("Failed to publish event. result(%d)", ret);
     bundle_free(b);
@@ -555,7 +575,6 @@ R"__c_cb(
 
 constexpr const char CB_INTERFACE_EXTRA_HEADER[] =
 R"__c_cb(
-#include <app_common.h>
 #include <app_event.h>
 )__c_cb";
 
index 8305e95..d0211eb 100644 (file)
@@ -54,6 +54,7 @@ typedef struct {
  * @param[in] sender_appid ID of app sending events
  * @param[in] callback The set of callback functions to handle events
  * @param[in] user_data The user data to be passed to the callback function
+ * @param[in] is_system True on system events
  * @param[out] h The <PREFIX>_<NAME> handle
  * @return @c 0 on success,
  *         otherwise a negative error value
@@ -65,13 +66,15 @@ typedef struct {
  * @see <PREFIX>_<NAME>_destroy()
  * @see #<PREFIX>_<NAME>_callback_s
  */
-int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, <PREFIX>_<NAME>_h *h);
+int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, bool is_system, <PREFIX>_<NAME>_h *h);
 
 /**
  * @brief Creates a <PREFIX>_<NAME> handle for sending events.
  * @remarks The @a h handle should be released using
  *          the <PREFIX>_<NAME>_destroy() if it's no longer needed.
  *
+ * @param[in] sender_appid ID of app sending events
+ * @param[in] is_system True on system events
  * @param[out] h The <PREFIX>_<NAME> handle
  * @return @c 0 on success,
  *         otherwise a negative error value
@@ -82,7 +85,7 @@ int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s
 
  * @see <PREFIX>_<NAME>_destroy()
  */
-int <PREFIX>_<NAME>_create_for_sender(<PREFIX>_<NAME>_h *h);
+int <PREFIX>_<NAME>_create_for_sender(const char *sender_appid, bool is_system, <PREFIX>_<NAME>_h *h);
 
 /**
  * @brief Destroys the <PREFIX>_<NAME> handle.
index 4ae3efe..3526914 100644 (file)
@@ -28,11 +28,9 @@ R"__cpp_cb(
 
 const char CB_INTERFACE_CTOR[] =
 R"__cpp_cb(
-<CLS_NAME>::<CLS_NAME>() {
-}
-
-<CLS_NAME>::<CLS_NAME>(const std::string& sender_appid) {
-  Subscribe(sender_appid);
+<CLS_NAME>::<CLS_NAME>(std::string sender_appid, bool is_system)
+    : sender_appid_(std::move(sender_appid)), is_system_(is_system) {
+  Subscribe();
 }
 
 )__cpp_cb";
@@ -78,9 +76,9 @@ R"__cpp_cb(
 
 const char CB_GROUP_HELPER_METHODS[] =
 R"__cpp_cb(
-void <CLS_NAME>::Subscribe(const std::string& sender_appid) {
+void <CLS_NAME>::Subscribe() {
   std::lock_guard<std::recursive_mutex> lock(mutex_);
-  int ret = event_add_event_handler(GetEventName(sender_appid).c_str(), EventCb, this, &event_handler_);
+  int ret = event_add_event_handler(GetEventName().c_str(), EventCb, this, &event_handler_);
   if (ret != EVENT_ERROR_NONE) {
     _E("Failed to register events. result(%d)", ret);
     throw InvalidIOException();
@@ -126,28 +124,10 @@ rpc_port_parcel_h <CLS_NAME>::GetParcelFromBundle(bundle* b) {
   return p;
 }
 
-std::string <CLS_NAME>::GetAppId() {
-  static std::string str;
-
-  if (!str.empty())
-    return str;
-
-  char* id;
-  int ret = app_get_id(&id);
-  if (ret != APP_ERROR_NONE)
-    return "system";
-
-  str = id;
-  free(id);
-  return str;
-}
-
 std::string <CLS_NAME>::GetEventName() {
-  return "event." + GetAppId() + ".tidl_iface_<CLS_NAME>";
-}
-
-std::string <CLS_NAME>::GetEventName(const std::string& sender_appid) {
-  return "event." + sender_appid + ".tidl_iface_<CLS_NAME>";
+  if (is_system_)
+    return "tizen.system.event.tidl_iface_<CLS_NAME>";
+  return "event." + sender_appid_ + ".tidl_iface_<CLS_NAME>";
 }
 
 void <CLS_NAME>::EventCb(const char* event_name, bundle* event_data, void* user_data) {
index d1f3045..f685077 100644 (file)
@@ -46,8 +46,7 @@ const char CB_CLASS_FULL[] =
 R"__cpp_cb(
 class <CLS_NAME> {
  public:
-  <CLS_NAME>();
-  <CLS_NAME>(const std::string& sender_appid);
+  <CLS_NAME>(std::string sender_appid, bool is_system = false);
   virtual ~<CLS_NAME>();
 
   <PUBLIC_MEMBERS>
@@ -57,16 +56,16 @@ class <CLS_NAME> {
 
   bundle* GetBundleFromParcel(rpc_port_parcel_h p, bundle* b);
   rpc_port_parcel_h GetParcelFromBundle(bundle* b);
-  std::string GetAppId();
   std::string GetEventName();
-  std::string GetEventName(const std::string& sender_appid);
-  void Subscribe(const std::string& sender_appid);
+  void Subscribe();
   void Unsubscribe();
   static void EventCb(const char* event_name, bundle* event_data, void* user_data);
 
  private:
   event_handler_h event_handler_ = nullptr;
+  std::string sender_appid_;
   std::recursive_mutex mutex_;
+  bool is_system_;
 };
 )__cpp_cb";
 
@@ -82,7 +81,6 @@ const char CB_HEADER[] =
 R"__cpp_cb(
 #pragma once
 
-#include <app_common.h>
 #include <app_event.h>
 #include <bundle.h>
 #include <rpc-port-parcel.h>
index d5aaada..caaa1fc 100644 (file)
@@ -33,6 +33,7 @@ R"__cs_cb(
             private EventReceiver _eventReceiver;
             private Object _lock = new Object();
             private bool _disposedValue = false;
+            private string _senderAppid;
 
             private Bundle GetBundleFromParcel(Parcel p)
             {
@@ -71,6 +72,7 @@ R"__cs_cb(
             public <CLS_NAME>(string senderAppid)
             {
                 string eventName = GetEventName(senderAppid, "<CLS_NAME>");
+                _senderAppid = senderAppid;
                 _eventReceiver = new EventReceiver(eventName);
                 _eventReceiver.Received += (obj, args) =>
                 {
@@ -82,10 +84,6 @@ R"__cs_cb(
                 };
             }
 
-            public <CLS_NAME>()
-            {
-            }
-
             ~<CLS_NAME>()
             {
                 Dispose(false);
@@ -131,7 +129,7 @@ R"__cs_cb(
                     p.WriteInt((int)MethodId.<METHOD_ID>);
 <PARAM_SERIALIZER>
                     var bundle = GetBundleFromParcel(p);
-                    string eventName = GetEventName(Application.Current.ApplicationInfo.ApplicationId, "<CLS_NAME>");
+                    string eventName = GetEventName(_senderAppid, "<CLS_NAME>");
                     ApplicationEventManager.Publish(eventName, bundle);
                 }
             }