Merge changes I94bee8d4,I9dc8f8fb,Id361946a,I1b8aef64 into tizen
[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)
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                 mClientList = g_slist_append(mClientList, data);
41         } else {
42                 MAS_LOGE("[ERROR] data is NULL");
43                 return -1;// MA_ERROR_OUT_OF_MEMORY;
44         }
45
46         return 0;
47 }
48
49 int CClientManager::destroy_client(ma_client_s* client)
50 {
51         if (nullptr == client) {
52                 MAS_LOGE("Input parameter is NULL");
53                 return -1;// MA_ERROR_OPERATION_FAILED;
54         }
55
56         mClientList =  g_slist_remove(mClientList, client);
57
58         delete client;
59
60         return 0;
61 }
62
63 int CClientManager::destroy_client_by_pid(pid_t pid)
64 {
65         ma_client_s* client = find_client_by_pid(pid);
66         return destroy_client(client);
67 }
68
69 int CClientManager::destroy_client_by_appid(std::string appid)
70 {
71         ma_client_s* client = find_client_by_appid(appid);
72         return destroy_client(client);
73 }
74
75 ma_client_s* CClientManager::find_client_by_appid(std::string appid)
76 {
77         ma_client_s *data = NULL;
78
79         int count = g_slist_length(mClientList);
80         int i;
81
82         for (i = 0; i < count; i++) {
83                 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
84
85                 if (NULL != data) {
86                         if (0 == appid.compare(data->appid)) {
87                                 return data;
88                         }
89                 }
90         }
91
92         MAS_LOGE("[ERROR] client Not found");
93
94         return NULL;
95 }
96
97 ma_client_s* CClientManager::find_client_by_pid(pid_t pid)
98 {
99         ma_client_s *data = NULL;
100
101         int count = g_slist_length(mClientList);
102         int i;
103
104         for (i = 0; i < count; i++) {
105                 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
106
107                 if (NULL != data) {
108                         if (data->pid == pid) {
109                                 return data;
110                         }
111                 }
112         }
113
114         MAS_LOGE("[ERROR] client Not found");
115
116         return NULL;
117 }
118
119 int CClientManager::get_client_num()
120 {
121         return g_slist_length(mClientList);
122 }
123
124 ma_client_s* CClientManager::get_client_by_index(unsigned int index)
125 {
126         return static_cast<ma_client_s*>(g_slist_nth_data(mClientList, index));
127 }
128
129 pid_t CClientManager::find_client_pid_by_index(unsigned int index)
130 {
131         pid_t pid = -1;
132         ma_client_s* client = get_client_by_index(index);
133         if (client) {
134                 pid = client->pid;
135         }
136         return pid;
137 }
138
139 pid_t CClientManager::find_client_pid_by_appid(std::string appid)
140 {
141         pid_t pid = -1;
142
143         if (nullptr == mApplicationManager) {
144                 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
145                 return pid;
146         }
147
148         ma_client_s* client = find_client_by_appid(appid);
149         if (client) {
150                 bool running = mApplicationManager->is_application_running(client->pid);
151                 if (false == running) {
152                         MAS_LOGE("The PID for %s was %d, but it seems to be terminated",
153                                 appid.c_str(), client->pid);
154                 } else {
155                         pid = client->pid;
156                 }
157         }
158         return pid;
159 }
160
161 std::string CClientManager::find_client_appid_by_pid(pid_t pid)
162 {
163         std::string appid;
164
165         if (nullptr == mApplicationManager) {
166                 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
167                 return appid;
168         }
169
170         ma_client_s* client = find_client_by_pid(pid);
171         if (client) {
172                 bool running = mApplicationManager->is_application_running(pid);
173                 if (false == running) {
174                         MAS_LOGE("The appid for %d was %s, but it seems to be terminated",
175                                 pid, client->appid.c_str());
176                 } else {
177                         appid = client->appid;
178                 }
179         }
180         return appid;
181 }
182
183 bool CClientManager::check_client_validity_by_appid(std::string appid)
184 {
185         pid_t pid = find_client_pid_by_appid(appid);
186         if (0 < pid) return true;
187         return false;
188 }