Refactor launcher service API 61/283661/1
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 1 Nov 2022 05:43:42 +0000 (05:43 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 1 Nov 2022 05:43:42 +0000 (05:43 +0000)
The launcher service API is implemented using C++ language.

Change-Id: I153e79142f1cda1f6a4eb145ab2f1b47f4df9e74
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_launcher_service.c [deleted file]
src/aul_launcher_service.cc [new file with mode: 0644]

diff --git a/src/aul_launcher_service.c b/src/aul_launcher_service.c
deleted file mode 100644 (file)
index 835c18d..0000000
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <glib.h>
-#include <bundle_internal.h>
-
-#include "aul_api.h"
-#include "aul_app_com.h"
-#include "aul_error.h"
-#include "aul_launcher_service.h"
-#include "aul_sock.h"
-#include "aul_util.h"
-
-struct aul_launcher_service_s {
-       char *name;
-       aul_app_com_connection_h conn;
-       aul_launcher_service_cb callback;
-       void *user_data;
-};
-
-static int __launcher_service_cb(const char *endpoint, aul_app_com_result_e res,
-               bundle *envelope, void *user_data)
-{
-       struct aul_launcher_service_s *service = user_data;
-       char *appid = NULL;
-       char *instance_id = NULL;
-       char *pid_str = NULL;
-       int pid;
-       char *serial_str = NULL;
-       uint32_t serial;
-
-       bundle_get_str(envelope, AUL_K_APPID, &appid);
-       if (!appid) {
-               _E("Failed to get application ID");
-               return -1;
-       }
-
-       bundle_get_str(envelope, AUL_K_INSTANCE_ID, &instance_id);
-       if (!instance_id) {
-               _E("Failed to get instance ID");
-               return -1;
-       }
-
-       bundle_get_str(envelope, AUL_K_PID, &pid_str);
-       if (!pid_str) {
-               _E("Failed to get process ID");
-               return -1;
-       }
-
-       pid = atoi(pid_str);
-
-       bundle_get_str(envelope, AUL_K_LAUNCHER_SERVICE_SERIAL, &serial_str);
-       if (!serial_str) {
-               _E("Failed to get serial");
-               return -1;
-       }
-
-       serial = strtoul(serial_str, NULL, 10);
-
-       service->callback(appid, instance_id, pid, serial, service->user_data);
-
-       return 0;
-}
-
-API int aul_launcher_service_create(const char *name,
-               aul_launcher_service_cb callback,
-               void *user_data,
-               aul_launcher_service_h *handle)
-{
-       struct aul_launcher_service_s *service;
-
-       if (!callback || !name || !handle) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       service = calloc(1, sizeof(struct aul_launcher_service_s));
-       if (!service) {
-               _E("Out of memory");
-               return AUL_R_ENOMEM;
-       }
-
-       service->name = strdup(name);
-       if (!service->name) {
-               _E("Failed to duplicate name");
-               free(service);
-               return AUL_R_ENOMEM;
-       }
-
-       service->callback = callback;
-       service->user_data = user_data;
-
-       *handle = service;
-
-       return AUL_R_OK;
-}
-
-API int aul_launcher_service_listen(aul_launcher_service_h handle)
-{
-       char endpoint[128];
-       int ret;
-
-       if (!handle) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       snprintf(endpoint, sizeof(endpoint), "launcher_service:%s:%d",
-                       handle->name, getpid());
-       ret = aul_app_com_create(endpoint, NULL, __launcher_service_cb,
-                       handle, &handle->conn);
-       if (ret < 0) {
-               _E("Failed to create app com. error(%d)", ret);
-               return AUL_R_ERROR;
-       }
-
-       return AUL_R_OK;
-}
-
-API int aul_launcher_service_destroy(aul_launcher_service_h handle)
-{
-       if (!handle) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       aul_app_com_leave(handle->conn);
-       free(handle->name);
-       free(handle);
-
-       return AUL_R_OK;
-}
-
-static int __send_request(int cmd)
-{
-       int ret;
-
-       ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(), cmd,
-                       NULL, 0, AUL_SOCK_NOREPLY);
-       if (ret != 0) {
-               _E("Failed to send command(%d). error(%d)", cmd, ret);
-               return aul_error_convert(ret);
-       }
-
-       return 0;
-}
-
-API int aul_launcher_service_notify_animation_started(void)
-{
-       int ret;
-
-       ret = __send_request(LAUNCHER_SERVICE_NOTIFY_ANIMATION_STARTED);
-       if (ret < 0)
-               return ret;
-
-       return AUL_R_OK;
-}
-
-API int aul_launcher_service_notify_animation_finished(void)
-{
-       int ret;
-
-       ret = __send_request(LAUNCHER_SERVICE_NOTIFY_ANIMATION_FINISHED);
-       if (ret < 0)
-               return ret;
-
-       return AUL_R_OK;
-}
diff --git a/src/aul_launcher_service.cc b/src/aul_launcher_service.cc
new file mode 100644 (file)
index 0000000..d9b9411
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2019 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "include/aul_launcher_service.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <glib.h>
+#include <bundle_internal.h>
+
+#include "aul_api.h"
+#include "aul_util.h"
+#include "include/aul_app_com.h"
+#include "include/aul_error.h"
+#include "include/aul_sock.h"
+
+#include "app_request.h"
+
+struct aul_launcher_serivce_s {
+  void* dummy;
+};
+
+using namespace aul::internal;
+namespace {
+
+class LauncherService {
+ public:
+  LauncherService(std::string name, aul_launcher_service_cb cb,
+      void* user_data)
+      : name_(std::move(name)), cb_(cb), user_data_(user_data) {
+  }
+
+  ~LauncherService() {
+    if (conn_ != nullptr)
+      aul_app_com_leave(conn_);
+  }
+
+  int Listen() {
+    if (conn_ != nullptr)
+      return AUL_R_OK;
+
+    std::string endpoint = "launcher_service:" + name_ + ":" +
+        std::to_string(getpid());
+    int ret = aul_app_com_create(endpoint.c_str(), nullptr, AppComCb,
+        this, &conn_);
+    if (ret < 0) {
+      _E("Failed to create app com. error(%d)", ret);
+      return AUL_R_ERROR;
+    }
+
+    return AUL_R_OK;
+  }
+
+ private:
+  static int AppComCb(const char* endpoint, aul_app_com_result_e result,
+      bundle* envelope, void* user_data) {
+    const char* appid = bundle_get_val(envelope, AUL_K_APPID);
+    if (appid == nullptr) {
+      _E("Failed to get application ID");
+      return -1;
+    }
+
+    const char* instance_id = bundle_get_val(envelope, AUL_K_INSTANCE_ID);
+    if (instance_id == nullptr) {
+      _E("Failed to get instance ID");
+      return -1;
+    }
+
+    const char* pid_str = bundle_get_val(envelope, AUL_K_PID);
+    if (pid_str == nullptr) {
+      _E("Failed to get process ID");
+      return -1;
+    }
+    int pid = atoi(pid_str);
+
+    const char* serial_str = bundle_get_val(envelope,
+        AUL_K_LAUNCHER_SERVICE_SERIAL);
+    if (serial_str == nullptr) {
+      _E("Failed to get serial");
+      return -1;
+    }
+    uint32_t serial = strtoul(serial_str, NULL, 10);
+
+    auto* launcher_service = static_cast<LauncherService*>(user_data);
+    launcher_service->cb_(appid, instance_id, pid, serial,
+        launcher_service->user_data_);
+    return 0;
+  }
+
+ private:
+  std::string name_;
+  aul_launcher_service_cb cb_;
+  void* user_data_;
+  aul_app_com_connection_h conn_ = nullptr;
+};
+
+}  // namespace
+
+extern "C" API int aul_launcher_service_create(const char* name,
+    aul_launcher_service_cb callback, void* user_data,
+    aul_launcher_service_h* handle) {
+  if (callback == nullptr || name == nullptr || handle == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* service = new (std::nothrow) LauncherService(name, callback, user_data);
+  if (service == nullptr) {
+    _E("Out of memory");
+    return AUL_R_ENOMEM;
+  }
+
+  *handle = reinterpret_cast<aul_launcher_service_h>(service);
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_launcher_service_listen(aul_launcher_service_h handle) {
+  if (handle == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* service = reinterpret_cast<LauncherService*>(handle);
+  return service->Listen();
+}
+
+extern "C" API int aul_launcher_service_destroy(aul_launcher_service_h handle) {
+  if (handle == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* service = reinterpret_cast<LauncherService*>(handle);
+  delete service;
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_launcher_service_notify_animation_started(void) {
+  return AppRequest(LAUNCHER_SERVICE_NOTIFY_ANIMATION_STARTED, getuid())
+      .SendCmdOnly(AUL_SOCK_NOREPLY);
+}
+
+extern "C" API int aul_launcher_service_notify_animation_finished(void) {
+  return AppRequest(LAUNCHER_SERVICE_NOTIFY_ANIMATION_FINISHED, getuid())
+      .SendCmdOnly(AUL_SOCK_NOREPLY);
+}