fix issue,not free string
[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 #include <algorithm>
17 #include "hs-clientmanager.h"
18 #include "hmi-debug.h"
19
20 static const char _homescreen[] = "homescreen";
21
22 HS_ClientManager* HS_ClientManager::me = nullptr;
23
24 static void cbRemoveClientCtxt(void *data)
25 {
26     HS_ClientManager::instance()->removeClientCtxt(data);
27 }
28
29 /**
30  * HS_ClientManager construction function
31  *
32  * #### Parameters
33  *  - Nothing
34  *
35  * #### Return
36  * None
37  *
38  */
39 HS_ClientManager::HS_ClientManager()
40 {
41 }
42
43 /**
44  * get instance
45  *
46  * #### Parameters
47  *  - Nothing
48  *
49  * #### Return
50  * HS_ClientManager instance pointer
51  *
52  */
53 HS_ClientManager* HS_ClientManager::instance(void)
54 {
55     if(me == nullptr)
56         me = new HS_ClientManager();
57
58     return me;
59 }
60
61 /**
62  * HS_ClientManager init function
63  *
64  * #### Parameters
65  *  - Nothing
66  *
67  * #### Return
68  * init result
69  *
70  */
71 int HS_ClientManager::init(void)
72 {
73     HMI_NOTICE("homescreen-service","called.");
74 }
75
76 /**
77  * create client's afb_req_context
78  *
79  * #### Parameters
80  *  - appid: app's id
81  *
82  * #### Return
83  * HS_ClientCtxt pointer
84  *
85  */
86 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
87 {
88     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
89     if (!ctxt)
90     {
91         HMI_NOTICE("homescreen-service", "create new session for %s", appid.c_str());
92         HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
93         afb_req_session_set_LOA(req, 1);
94         afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
95     }
96     return ctxt;
97 }
98
99 /**
100  * add Client
101  *
102  * #### Parameters
103  *  - ctxt: app's id
104  *
105  * #### Return
106  * HS_Client pointer
107  *
108  */
109 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
110 {
111     return (client_list[appid] = new HS_Client(req, appid));
112 }
113
114 /**
115  * remove Client
116  *
117  * #### Parameters
118  *  - appid: app's id
119  *
120  * #### Return
121  * None
122  *
123  */
124 void HS_ClientManager::removeClient(std::string appid)
125 {
126     delete client_list[appid];
127     client_list.erase(appid);
128 }
129
130 /**
131  * remove Client from list
132  *
133  * #### Parameters
134  *  - data: HS_ClientCtxt pointer
135  *
136  * #### Return
137  * None
138  *
139  */
140 void HS_ClientManager::removeClientCtxt(void *data)
141 {
142     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
143     if(ctxt == nullptr)
144     {
145         HMI_ERROR("homescreen-service", "data is nullptr");
146         return;
147     }
148
149     HMI_NOTICE("homescreen-service", "remove app %s", ctxt->id.c_str());
150     std::lock_guard<std::mutex> lock(this->mtx);
151     removeClient(ctxt->id);
152     delete appid2ctxt[ctxt->id];
153     appid2ctxt.erase(ctxt->id);
154 }
155
156 /**
157  * tap_shortcut
158  *
159  * #### Parameters
160  *  - request: the request to bindings
161  *
162  * #### Return
163  * result
164  *
165  */
166 int HS_ClientManager::tap_shortcut(afb_req_t request)
167 {
168     int ret = 0;
169     const char* value = afb_req_value(request, _application_id);
170     if (value) {
171         HMI_NOTICE("homescreen-service","request params = %s.", value);
172         std::lock_guard<std::mutex> lock(this->mtx);
173         auto ip = client_list.find(std::string(value));
174         if(ip != client_list.end()) {
175             ip->second->tap_shortcut(value);
176         }
177     }
178     else {
179         HMI_NOTICE("homescreen-service","Please input application_id");
180         ret = AFB_EVENT_BAD_REQUEST;
181     }
182     return ret;
183 }
184
185 /**
186  * on_screen_message
187  *
188  * #### Parameters
189  *  - request: the request to bindings
190  *
191  * #### Return
192  * result
193  *
194  */
195 int HS_ClientManager::on_screen_message(afb_req_t request)
196 {
197     int ret = 0;
198     const char* value = afb_req_value(request, _display_message);
199     if (value) {
200         HMI_NOTICE("homescreen-service","request params = %s.", value);
201         std::lock_guard<std::mutex> lock(this->mtx);
202         for(auto m : client_list) {
203             m.second->on_screen_message(request, value);
204         }
205     }
206     else {
207         HMI_NOTICE("homescreen-service","Please input display_message");
208         ret = AFB_EVENT_BAD_REQUEST;
209     }
210     return ret;
211 }
212
213 /**
214  * on_screen_reply
215  *
216  * #### Parameters
217  *  - request: the request to bindings
218  *
219  * #### Return
220  * result
221  *
222  */
223 int HS_ClientManager::on_screen_reply(afb_req_t request)
224 {
225     int ret = 0;
226     const char* value = afb_req_value(request, _reply_message);
227     if (value) {
228       HMI_NOTICE("homescreen-service","request params = %s.", value);
229       std::lock_guard<std::mutex> lock(this->mtx);
230       for(auto m : client_list) {
231         m.second->on_screen_reply(request, value);
232       }
233     }
234     else {
235         HMI_NOTICE("homescreen-service","Please input reply_message");
236         ret = AFB_EVENT_BAD_REQUEST;
237     }
238     return ret;
239 }
240
241 /**
242  * subscribe
243  *
244  * #### Parameters
245  *  - request: the request to bindings
246  *
247  * #### Return
248  * result
249  *
250  */
251 int HS_ClientManager::subscribe(afb_req_t request)
252 {
253     int ret = 0;
254     const char *value = afb_req_value(request, "event");
255     HMI_NOTICE("homescreen-service","value is %s", value);
256     if(value) {
257         std::string appid =std::move(get_application_id(request));
258         if(appid.empty()) {
259             HMI_NOTICE("homescreen-service","can't get application identifier");
260             return AFB_REQ_GETAPPLICATIONID_ERROR;
261         }
262
263         std::lock_guard<std::mutex> lock(this->mtx);
264         HS_Client* client = nullptr;
265         auto ip = client_list.find(appid);
266         if(ip != client_list.end()) {
267             client = client_list[appid];
268         }
269         else {
270             appid2ctxt[appid] = createClientCtxt(request, appid);
271             client = addClient(request, appid);
272         }
273
274         if(client->subscribe(request, value) != 0) {
275             HMI_NOTICE("homescreen-service","subscribe failed");
276             ret = AFB_REQ_SUBSCRIBE_ERROR;
277         }
278     }
279     else {
280         HMI_NOTICE("homescreen-service","Please input event name");
281         ret = AFB_EVENT_BAD_REQUEST;
282     }
283     return ret;
284 }
285
286 /**
287  * unsubscribe
288  *
289  * #### Parameters
290  *  - request: the request to bindings
291  *
292  * #### Return
293  * result
294  *
295  */
296 int HS_ClientManager::unsubscribe(afb_req_t request)
297 {
298     const char *value = afb_req_value(request, "event");
299     HMI_NOTICE("homescreen-service","value is %s", value);
300     int ret = 0;
301     if(value) {
302         std::string appid = std::move(get_application_id(request));
303         if(appid.empty()) {
304             HMI_NOTICE("homescreen-service","can't get application identifier");
305             return AFB_REQ_GETAPPLICATIONID_ERROR;
306         }
307
308         std::lock_guard<std::mutex> lock(this->mtx);
309         auto ip = client_list.find(appid);
310         if(ip != client_list.end()
311         && ip->second->unsubscribe(request, value) != 0) {
312             HMI_NOTICE("homescreen-service","unsubscribe failed");
313             ret = AFB_REQ_UNSUBSCRIBE_ERROR;
314         }
315     }
316     else {
317         HMI_NOTICE("homescreen-service","Please input event name");
318         ret = AFB_EVENT_BAD_REQUEST;
319     }
320     return ret;
321 }
322
323 /**
324  * showWindow event
325  *
326  * #### Parameters
327  *  - request : the request
328  *
329  * #### Return
330  * 0 : success
331  * others : fail
332  *
333  */
334 int HS_ClientManager::showWindow(afb_req_t request)
335 {
336     int ret = 0;
337     const char* value = afb_req_value(request, _application_id);
338     if (value) {
339         HMI_NOTICE("homescreen-service","request params = %s.", value);
340         std::lock_guard<std::mutex> lock(this->mtx);
341         auto ip = client_list.find(std::string(value));
342         if(ip != client_list.end()) {
343             ret = ip->second->showWindow(request, value);
344         }
345     }
346     else {
347         HMI_NOTICE("homescreen-service","Please input application_id");
348         ret = AFB_EVENT_BAD_REQUEST;
349     }
350     return ret;
351 }
352
353 /**
354  * hideWindow event
355  *
356  * #### Parameters
357  *  - request : the request
358  *
359  * #### Return
360  * 0 : success
361  * others : fail
362  *
363  */
364 int HS_ClientManager::hideWindow(afb_req_t request)
365 {
366     int ret = 0;
367     const char* value = afb_req_value(request, _application_id);
368     if (value) {
369         HMI_NOTICE("homescreen-service","request params = %s.", value);
370         std::lock_guard<std::mutex> lock(this->mtx);
371         auto ip = client_list.find(std::string(value));
372         if(ip != client_list.end()) {
373             ret = ip->second->hideWindow(request);
374         }
375     }
376     else {
377         HMI_NOTICE("homescreen-service","Please input application_id");
378         ret = AFB_EVENT_BAD_REQUEST;
379     }
380     return ret;
381 }
382
383 /**
384  * replyShowWindow event
385  *
386  * #### Parameters
387  *  - request : the request
388  *
389  * #### Return
390  * 0 : success
391  * others : fail
392  *
393  */
394 int HS_ClientManager::replyShowWindow(afb_req_t request)
395 {
396     int ret = 0;
397     const char* value = afb_req_value(request, _application_id);
398     if (value) {
399         HMI_NOTICE("homescreen-service","request params = %s.", value);
400         std::lock_guard<std::mutex> lock(this->mtx);
401         auto ip = client_list.find(std::string(value));
402         if(ip != client_list.end()) {
403             ret = ip->second->replyShowWindow(request, value);
404         }
405     }
406     else {
407         HMI_NOTICE("homescreen-service","Please input application_id");
408         ret = AFB_EVENT_BAD_REQUEST;
409     }
410     return ret;
411 }
412
413 /**
414  * showNotification event
415  *
416  * #### Parameters
417  *  - request : the request
418  *
419  * #### Return
420  * 0 : success
421  * others : fail
422  *
423  */
424 int HS_ClientManager::showNotification(afb_req_t request)
425 {
426     int ret = 0;
427     std::lock_guard<std::mutex> lock(this->mtx);
428     auto ip = client_list.find(_homescreen);
429     if(ip != client_list.end()) {
430         ret = ip->second->showNotification(request);
431     }
432     else {
433         HMI_NOTICE("homescreen-service","not exist sessiion with homescreen");
434         ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
435     }
436
437     return ret;
438 }
439
440 /**
441  * showInformation event
442  *
443  * #### Parameters
444  *  - request : the request
445  *
446  * #### Return
447  * 0 : success
448  * others : fail
449  *
450  */
451 int HS_ClientManager::showInformation(afb_req_t request)
452 {
453     int ret = 0;
454     std::lock_guard<std::mutex> lock(this->mtx);
455     auto ip = client_list.find(_homescreen);
456     if(ip != client_list.end()) {
457         ret = ip->second->showInformation(request);
458     }
459     else {
460         HMI_NOTICE("homescreen-service","not exist sessiion with homescreen");
461         ret = AFB_REQ_SHOWINFORMATION_ERROR;
462     }
463
464     return ret;
465 }