emit event to one application
[apps/agl-service-homescreen.git] / src / hs-clientmanager.cpp
1 /*
2  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
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 #include "hs-clientmanager.h"
18 #include "hmi-debug.h"
19
20 HS_ClientManager* HS_ClientManager::me = nullptr;
21
22 static void cbRemoveClientCtxt(void *data)
23 {
24     HS_ClientManager::instance()->removeClientCtxt(data);
25 }
26
27 /**
28  * HS_ClientManager construction function
29  *
30  * #### Parameters
31  *  - Nothing
32  *
33  * #### Return
34  * None
35  *
36  */
37 HS_ClientManager::HS_ClientManager()
38 {
39 }
40
41 /**
42  * get instance
43  *
44  * #### Parameters
45  *  - Nothing
46  *
47  * #### Return
48  * HS_ClientManager instance pointer
49  *
50  */
51 HS_ClientManager* HS_ClientManager::instance(void)
52 {
53     if(me == nullptr)
54         me = new HS_ClientManager();
55
56     return me;
57 }
58
59 /**
60  * HS_ClientManager init function
61  *
62  * #### Parameters
63  *  - Nothing
64  *
65  * #### Return
66  * init result
67  *
68  */
69 int HS_ClientManager::init(void)
70 {
71     HMI_NOTICE("homescreen-service","called.");
72     // TODO : connect to windowmanger
73     // get applist from appfw
74 }
75
76 /**
77  * find HS_Client in client_list
78  *
79  * #### Parameters
80  *  - appid: app's id
81  *
82  * #### Return
83  * found HS_Client pointer
84  *
85  */
86 HS_Client* HS_ClientManager::find(std::string appid)
87 {
88     std::lock_guard<std::mutex> lock(this->mtx);
89     HS_Client* p = nullptr;
90     auto ip = client_list.find(appid);
91     if(ip != client_list.end()) {
92         p = client_list[appid];
93     }
94     return p;
95 }
96
97 /**
98  * get HS_Client
99  *
100  * #### Parameters
101  *  - appid: app's id
102  *
103  * #### Return
104  * found HS_Client pointer
105  *
106  */
107 HS_Client* HS_ClientManager::getClient(afb_req req, std::string appid)
108 {
109     std::lock_guard<std::mutex> lock(this->mtx);
110     HS_Client* p = nullptr;
111     auto ip = client_list.find(appid);
112     if(ip != client_list.end()) {
113         p = client_list[appid];
114     }
115     else {
116         appid2ctxt[appid] = createClientCtxt(req, appid);
117         p = addClient(req, appid);
118     }
119     return p;
120 }
121
122 /**
123  * get HS_Client pointers set
124  *
125  * #### Parameters
126  *  - Nothing
127  *
128  * #### Return
129  * HS_Client pointers set
130  *
131  */
132 std::vector<HS_Client*> HS_ClientManager::getAllClient(void)
133 {
134     std::lock_guard<std::mutex> lock(this->mtx);
135     std::vector<HS_Client*> v;
136     for(auto a : client_list)
137         v.push_back(a.second);
138     return v;
139 }
140
141 /**
142  * create client's afb_req_context
143  *
144  * #### Parameters
145  *  - appid: app's id
146  *
147  * #### Return
148  * HS_ClientCtxt pointer
149  *
150  */
151 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req req, std::string appid)
152 {
153     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
154     if (!ctxt)
155     {
156         HMI_NOTICE("homescreen-service", "create new session for %s", appid.c_str());
157         HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
158         afb_req_session_set_LOA(req, 1);
159         afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
160     }
161     return ctxt;
162 }
163
164 /**
165  * add Client
166  *
167  * #### Parameters
168  *  - ctxt: app's id
169  *
170  * #### Return
171  * HS_Client pointer
172  *
173  */
174 HS_Client* HS_ClientManager::addClient(afb_req req, std::string appid)
175 {
176     return (client_list[appid] = new HS_Client(req, appid));
177 }
178
179 /**
180  * remove Client
181  *
182  * #### Parameters
183  *  - appid: app's id
184  *
185  * #### Return
186  * None
187  *
188  */
189 void HS_ClientManager::removeClient(std::string appid)
190 {
191     delete client_list[appid];
192     client_list.erase(appid);
193 }
194
195 /**
196  * remove Client from list
197  *
198  * #### Parameters
199  *  - data: HS_ClientCtxt pointer
200  *
201  * #### Return
202  * None
203  *
204  */
205 void HS_ClientManager::removeClientCtxt(void *data)
206 {
207     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
208     if(ctxt == nullptr)
209     {
210         HMI_ERROR("homescreen-service", "data is nullptr");
211         return;
212     }
213
214     HMI_NOTICE("homescreen-service", "remove app %s", ctxt->id.c_str());
215     std::lock_guard<std::mutex> lock(this->mtx);
216     removeClient(ctxt->id);
217     delete appid2ctxt[ctxt->id];
218     appid2ctxt.erase(ctxt->id);
219 }