Move function definition to aul header
[platform/core/appfw/aul-1.git] / src / aul_launcher_service.cc
1 /*
2  * Copyright (c) 2019 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "include/aul_launcher_service.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <bundle_internal.h>
24
25 #include "aul_api.h"
26 #include "aul_util.h"
27 #include "include/aul_app_com.h"
28 #include "include/aul_error.h"
29 #include "include/aul_sock.h"
30
31 #include "app_request.h"
32
33 struct aul_launcher_serivce_s {
34   void* dummy;
35 };
36
37 using namespace aul::internal;
38 namespace {
39
40 class LauncherService {
41  public:
42   LauncherService(std::string name, aul_launcher_service_cb cb,
43       void* user_data)
44       : name_(std::move(name)), cb_(cb), user_data_(user_data) {
45   }
46
47   ~LauncherService() {
48     if (conn_ != nullptr)
49       aul_app_com_leave(conn_);
50   }
51
52   int Listen() {
53     if (conn_ != nullptr)
54       return AUL_R_OK;
55
56     std::string endpoint = "launcher_service:" + name_ + ":" +
57         std::to_string(getpid());
58     int ret = aul_app_com_create(endpoint.c_str(), nullptr, AppComCb,
59         this, &conn_);
60     if (ret < 0) {
61       _E("Failed to create app com. error(%d)", ret);
62       return AUL_R_ERROR;
63     }
64
65     return AUL_R_OK;
66   }
67
68  private:
69   static int AppComCb(const char* endpoint, aul_app_com_result_e result,
70       bundle* envelope, void* user_data) {
71     const char* appid = bundle_get_val(envelope, AUL_K_APPID);
72     if (appid == nullptr) {
73       _E("Failed to get application ID");
74       return -1;
75     }
76
77     const char* instance_id = bundle_get_val(envelope, AUL_K_INSTANCE_ID);
78     if (instance_id == nullptr) {
79       _E("Failed to get instance ID");
80       return -1;
81     }
82
83     const char* pid_str = bundle_get_val(envelope, AUL_K_PID);
84     if (pid_str == nullptr) {
85       _E("Failed to get process ID");
86       return -1;
87     }
88     int pid = atoi(pid_str);
89
90     const char* serial_str = bundle_get_val(envelope,
91         AUL_K_LAUNCHER_SERVICE_SERIAL);
92     if (serial_str == nullptr) {
93       _E("Failed to get serial");
94       return -1;
95     }
96     uint32_t serial = strtoul(serial_str, NULL, 10);
97
98     auto* launcher_service = static_cast<LauncherService*>(user_data);
99     launcher_service->cb_(appid, instance_id, pid, serial,
100         launcher_service->user_data_);
101     return 0;
102   }
103
104  private:
105   std::string name_;
106   aul_launcher_service_cb cb_;
107   void* user_data_;
108   aul_app_com_connection_h conn_ = nullptr;
109 };
110
111 }  // namespace
112
113 extern "C" API int aul_launcher_service_create(const char* name,
114     aul_launcher_service_cb callback, void* user_data,
115     aul_launcher_service_h* handle) {
116   if (callback == nullptr || name == nullptr || handle == nullptr) {
117     _E("Invalid parameter");
118     return AUL_R_EINVAL;
119   }
120
121   auto* service = new (std::nothrow) LauncherService(name, callback, user_data);
122   if (service == nullptr) {
123     _E("Out of memory");
124     return AUL_R_ENOMEM;
125   }
126
127   *handle = reinterpret_cast<aul_launcher_service_h>(service);
128   return AUL_R_OK;
129 }
130
131 extern "C" API int aul_launcher_service_listen(aul_launcher_service_h handle) {
132   if (handle == nullptr) {
133     _E("Invalid parameter");
134     return AUL_R_EINVAL;
135   }
136
137   auto* service = reinterpret_cast<LauncherService*>(handle);
138   return service->Listen();
139 }
140
141 extern "C" API int aul_launcher_service_destroy(aul_launcher_service_h handle) {
142   if (handle == nullptr) {
143     _E("Invalid parameter");
144     return AUL_R_EINVAL;
145   }
146
147   auto* service = reinterpret_cast<LauncherService*>(handle);
148   delete service;
149   return AUL_R_OK;
150 }
151
152 extern "C" API int aul_launcher_service_notify_animation_started(void) {
153   return AppRequest(LAUNCHER_SERVICE_NOTIFY_ANIMATION_STARTED, getuid())
154       .SendCmdOnly(AUL_SOCK_NOREPLY);
155 }
156
157 extern "C" API int aul_launcher_service_notify_animation_finished(void) {
158   return AppRequest(LAUNCHER_SERVICE_NOTIFY_ANIMATION_FINISHED, getuid())
159       .SendCmdOnly(AUL_SOCK_NOREPLY);
160 }