458c384cbfd585b48c05591e2b10c483acef8ce2
[platform/core/uifw/multi-assistant-service.git] / src / client_manager.cpp
1 /*
2  * Copyright 2020 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include "client_manager.h"
18 #include "service_common.h"
19
20 int CClientManager::set_application_manager(IApplicationManager* manager)
21 {
22         mApplicationManager = manager;
23         return 0;
24 }
25
26 int CClientManager::create_client(pid_t pid, std::string appid, std::string sender_info)
27 {
28         ma_client_s* data = NULL;
29
30         try {
31                 data = new ma_client_s;
32         } catch(const std::bad_alloc& e) {
33                 MAS_LOGE("[ERROR] Fail to allocate memory : %s", e.what());
34                 return -1;// MA_ERROR_OUT_OF_MEMORY;
35         }
36
37         if (data) {
38                 data->pid = pid;
39                 data->appid = appid;
40                 data->sender_info = sender_info;
41                 mClientList = g_slist_append(mClientList, data);
42                 MAS_LOGI("Create client : %s %d", appid.c_str(), pid);
43         } else {
44                 MAS_LOGE("[ERROR] data is NULL");
45                 return -1;// MA_ERROR_OUT_OF_MEMORY;
46         }
47
48         return 0;
49 }
50
51 int CClientManager::destroy_client(ma_client_s* client)
52 {
53         if (nullptr == client) {
54                 MAS_LOGE("Input parameter is NULL");
55                 return -1;// MA_ERROR_OPERATION_FAILED;
56         }
57
58         mClientList =  g_slist_remove(mClientList, client);
59
60         delete client;
61
62         return 0;
63 }
64
65 int CClientManager::destroy_client_by_pid(pid_t pid)
66 {
67         ma_client_s* client = find_client_by_pid(pid);
68         return destroy_client(client);
69 }
70
71 int CClientManager::destroy_client_by_appid(std::string appid)
72 {
73         ma_client_s* client = find_client_by_appid(appid);
74         return destroy_client(client);
75 }
76
77 ma_client_s* CClientManager::find_client_by_appid(std::string appid)
78 {
79         ma_client_s *data = NULL;
80
81         int count = g_slist_length(mClientList);
82         int i;
83
84         for (i = 0; i < count; i++) {
85                 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
86
87                 if (NULL != data) {
88                         if (0 == appid.compare(data->appid)) {
89                                 return data;
90                         }
91                 }
92         }
93
94         MAS_LOGE("[ERROR] client Not found : %s", appid.c_str());
95
96         return NULL;
97 }
98
99 ma_client_s* CClientManager::find_client_by_pid(pid_t pid)
100 {
101         ma_client_s *data = NULL;
102
103         int count = g_slist_length(mClientList);
104         int i;
105
106         for (i = 0; i < count; i++) {
107                 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
108
109                 if (NULL != data) {
110                         if (data->pid == pid) {
111                                 return data;
112                         }
113                 }
114         }
115
116         MAS_LOGE("[ERROR] client Not found");
117
118         return NULL;
119 }
120
121 int CClientManager::get_client_num()
122 {
123         return g_slist_length(mClientList);
124 }
125
126 ma_client_s* CClientManager::get_client_by_index(unsigned int index)
127 {
128         return static_cast<ma_client_s*>(g_slist_nth_data(mClientList, index));
129 }
130
131 pid_t CClientManager::find_client_pid_by_index(unsigned int index)
132 {
133         pid_t pid = -1;
134         ma_client_s* client = get_client_by_index(index);
135         if (client) {
136                 pid = client->pid;
137         }
138         return pid;
139 }
140
141 pid_t CClientManager::find_client_pid_by_appid(std::string appid)
142 {
143         pid_t pid = -1;
144
145         if (nullptr == mApplicationManager) {
146                 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
147                 return pid;
148         }
149
150         ma_client_s* client = find_client_by_appid(appid);
151         if (client) {
152                 bool running = mApplicationManager->is_application_running(client->pid);
153                 if (false == running) {
154                         MAS_LOGE("The PID for %s was %d, but it seems to be terminated",
155                                 appid.c_str(), client->pid);
156                 } else {
157                         pid = client->pid;
158                 }
159         }
160         if (-1 == pid) {
161                 MAS_LOGE("PID lookup failed for : %s", appid.c_str());
162         }
163         return pid;
164 }
165
166 std::string CClientManager::find_client_appid_by_pid(pid_t pid)
167 {
168         std::string appid;
169
170         if (nullptr == mApplicationManager) {
171                 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
172                 return appid;
173         }
174
175         ma_client_s* client = find_client_by_pid(pid);
176         if (client) {
177                 bool running = mApplicationManager->is_application_running(pid);
178                 if (false == running) {
179                         MAS_LOGE("The appid for %d was %s, but it seems to be terminated",
180                                 pid, client->appid.c_str());
181                 } else {
182                         appid = client->appid;
183                 }
184         }
185         return appid;
186 }
187
188 std::string CClientManager::find_sender_info_by_pid(pid_t pid)
189 {
190         std::string ret;
191         ma_client_s* client = find_client_by_pid(pid);
192         if (client) {
193                 ret = client->sender_info;
194         }
195         return ret;
196 }
197
198 bool CClientManager::check_client_validity_by_appid(std::string appid)
199 {
200         pid_t pid = find_client_pid_by_appid(appid);
201         if (0 < pid) return true;
202         return false;
203 }
204
205 bool CClientManager::check_sender_info_by_pid(pid_t pid, std::string sender_info)
206 {
207         bool ret = false;
208         ma_client_s* client = find_client_by_pid(pid);
209         if (client) {
210                 if (client->sender_info.compare(sender_info) == 0) {
211                         ret = true;
212                 }
213         }
214         if (!ret) {
215                 MAS_LOGE("SENDER INFO does not match : [%s] [%s]",
216                         (client ? client->sender_info.c_str() : ""),
217                         sender_info.c_str());
218         }
219         return ret;
220 }