2 * Copyright 2020 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
17 #include "client_manager.h"
18 #include "service_common.h"
20 int CClientManager::set_application_manager(IApplicationManager* manager)
22 mApplicationManager = manager;
26 int CClientManager::create_client(pid_t pid, std::string appid)
28 ma_client_s* data = NULL;
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;
40 mClientList = g_slist_append(mClientList, data);
42 MAS_LOGE("[ERROR] data is NULL");
43 return -1;// MA_ERROR_OUT_OF_MEMORY;
49 int CClientManager::destroy_client(ma_client_s* client)
51 if (nullptr == client) {
52 MAS_LOGE("Input parameter is NULL");
53 return -1;// MA_ERROR_OPERATION_FAILED;
56 mClientList = g_slist_remove(mClientList, client);
63 int CClientManager::destroy_client_by_pid(pid_t pid)
65 ma_client_s* client = find_client_by_pid(pid);
66 return destroy_client(client);
69 int CClientManager::destroy_client_by_appid(std::string appid)
71 ma_client_s* client = find_client_by_appid(appid);
72 return destroy_client(client);
75 ma_client_s* CClientManager::find_client_by_appid(std::string appid)
77 ma_client_s *data = NULL;
79 int count = g_slist_length(mClientList);
82 for (i = 0; i < count; i++) {
83 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
86 if (0 == appid.compare(data->appid)) {
92 MAS_LOGE("[ERROR] client Not found");
97 ma_client_s* CClientManager::find_client_by_pid(pid_t pid)
99 ma_client_s *data = NULL;
101 int count = g_slist_length(mClientList);
104 for (i = 0; i < count; i++) {
105 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
108 if (data->pid == pid) {
114 MAS_LOGE("[ERROR] client Not found");
119 int CClientManager::get_client_num()
121 return g_slist_length(mClientList);
124 ma_client_s* CClientManager::get_client_by_index(unsigned int index)
126 return static_cast<ma_client_s*>(g_slist_nth_data(mClientList, index));
129 pid_t CClientManager::find_client_pid_by_index(unsigned int index)
132 ma_client_s* client = get_client_by_index(index);
139 pid_t CClientManager::find_client_pid_by_appid(std::string appid)
143 if (nullptr == mApplicationManager) {
144 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
148 ma_client_s* client = find_client_by_appid(appid);
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);
161 std::string CClientManager::find_client_appid_by_pid(pid_t pid)
165 if (nullptr == mApplicationManager) {
166 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
170 ma_client_s* client = find_client_by_pid(pid);
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());
177 appid = client->appid;
183 bool CClientManager::check_client_validity_by_appid(std::string appid)
185 pid_t pid = find_client_pid_by_appid(appid);
186 if (0 < pid) return true;