fix segmentation fault error
[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 <cstring>
18 #include <algorithm>
19 #include "hs-clientmanager.h"
20 #include "hs-apprecover.h"
21
22 static const char _homescreen[] = "homescreen";
23 static const char _area[] = "area";
24 static const char _parameter[] = "parameter";
25
26 HS_ClientManager* HS_ClientManager::me = nullptr;
27
28 static void cbRemoveClientCtxt(void *data)
29 {
30     HS_ClientManager::instance()->removeClientCtxt(data);
31 }
32
33 /**
34  * HS_ClientManager construction function
35  *
36  * #### Parameters
37  *  - Nothing
38  *
39  * #### Return
40  * None
41  *
42  */
43 HS_ClientManager::HS_ClientManager()
44 {
45 }
46
47 /**
48  * get instance
49  *
50  * #### Parameters
51  *  - Nothing
52  *
53  * #### Return
54  * HS_ClientManager instance pointer
55  *
56  */
57 HS_ClientManager* HS_ClientManager::instance(void)
58 {
59     if(me == nullptr)
60         me = new HS_ClientManager();
61
62     return me;
63 }
64
65 /**
66  * HS_ClientManager init function
67  *
68  * #### Parameters
69  *  - Nothing
70  *
71  * #### Return
72  * init result
73  *
74  */
75 int HS_ClientManager::init(void)
76 {
77     listener_list.clear();
78 }
79
80 /**
81  * create client's afb_req_context
82  *
83  * #### Parameters
84  *  - appid: app's id
85  *
86  * #### Return
87  * HS_ClientCtxt pointer
88  *
89  */
90 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
91 {
92     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
93     if (!ctxt)
94     {
95         AFB_INFO( "create new session for %s", appid.c_str());
96         HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
97         afb_req_session_set_LOA(req, 1);
98         afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
99     }
100     return ctxt;
101 }
102
103 /**
104  * add Client
105  *
106  * #### Parameters
107  *  - ctxt: app's id
108  *
109  * #### Return
110  * HS_Client pointer
111  *
112  */
113 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
114 {
115     return (client_list[appid] = new HS_Client(req, appid));
116 }
117
118 /**
119  * remove Client
120  *
121  * #### Parameters
122  *  - appid: app's id
123  *
124  * #### Return
125  * None
126  *
127  */
128 void HS_ClientManager::removeClient(std::string appid)
129 {
130     delete client_list[appid];
131     client_list.erase(appid);
132 }
133
134 /**
135  * remove Client from list
136  *
137  * #### Parameters
138  *  - data: HS_ClientCtxt pointer
139  *
140  * #### Return
141  * None
142  *
143  */
144 void HS_ClientManager::removeClientCtxt(void *data)
145 {
146     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
147     if(ctxt == nullptr)
148     {
149         AFB_WARNING( "data is nullptr");
150         return;
151     }
152
153     AFB_INFO( "remove app %s", ctxt->id.c_str());
154     std::lock_guard<std::mutex> lock(this->mtx);
155     removeClient(ctxt->id);
156     delete appid2ctxt[ctxt->id];
157     appid2ctxt.erase(ctxt->id);
158 }
159
160 /**
161  * handle homescreen request
162  *
163  * #### Parameters
164  *  - request : the request
165  *  - verb : the verb name
166  *  - appid : to which application
167  *
168  * #### Return
169  * 0 : success
170  * others : fail
171  *
172  */
173 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
174 {
175     AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
176     int ret = 0;
177     bool isRegisterApp = false;
178     if(appid == nullptr) {
179         std::lock_guard<std::mutex> lock(this->mtx);
180         for(auto m : client_list) {
181             m.second->handleRequest(request, verb);
182         }
183     }
184     else {
185         std::lock_guard<std::mutex> lock(this->mtx);
186         auto ip = client_list.find(std::string(appid));
187         if(ip != client_list.end()) {
188             ret = ip->second->handleRequest(request, verb);
189         }
190         else {
191             if(!strcasecmp(verb, "subscribe")) {
192                 appid2ctxt[appid] = createClientCtxt(request, appid);
193                 HS_Client* client = addClient(request, appid);
194                 ret = client->handleRequest(request, "subscribe");
195                 isRegisterApp = true;
196             }
197             else {
198                 AFB_NOTICE("not exist session");
199                 ret = AFB_REQ_NOT_STARTED_APPLICATION;
200             }
201         }
202     }
203     if(isRegisterApp) {
204         notifyListener(request->api, std::string(appid));
205     }
206     return ret;
207 }
208
209 /**
210  * push event
211  *
212  * #### Parameters
213  *  - event : the event want to push
214  *  - param : the parameter contents of event
215  *  - appid : the destination application's id
216  *
217  * #### Return
218  * 0 : success
219  * others : fail
220  *
221  */
222 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
223 {
224     AFB_INFO("event=[%s], appid=[%s].", event, appid.c_str());
225     if(event == nullptr) {
226         AFB_WARNING("event name is null.");
227         return -1;
228     }
229
230     std::lock_guard<std::mutex> lock(this->mtx);
231     if(appid.empty()) { // broadcast event to clients who subscribed this event
232         for(auto m : client_list) {
233             m.second->pushEvent(event, param);
234         }
235     }
236     else {  // push event to specific client
237         auto ip = client_list.find(appid);
238         if(ip != client_list.end()) {
239             ip->second->pushEvent(event, param);
240         }
241     }
242
243     return 0;
244 }
245
246 /**
247  * check whether application was started
248  *
249  * #### Parameters
250  *  - appid : application's id
251  *
252  * #### Return
253  * true : started
254  * false : not start
255  *
256  */
257 bool HS_ClientManager::isAppStarted(const std::string &appid)
258 {
259     auto it = client_list.find(appid);
260     return it != client_list.end() ? true : false;
261 }
262
263 /**
264  * add app register listener
265  *
266  * #### Parameters
267  *  - listener_interface : listener interface
268  *
269  * #### Return
270  * None
271  *
272  */
273 void HS_ClientManager::addListener(listener_interface* listener)
274 {
275     listener_list[listener->myUid()] = listener;
276 }
277
278 /**
279  * remove app register listener
280  *
281  * #### Parameters
282  *  - listener_interface : listener interface
283  *
284  * #### Return
285  * None
286  *
287  */
288 void HS_ClientManager::removeListener(listener_interface* listener)
289 {
290     listener_list.erase(listener->myUid());
291 }
292
293 /**
294  * notify listener
295  *
296  * #### Parameters
297  *  - api : the api
298  *  - appid : register application's id
299  *
300  * #### Return
301  * None
302  *
303  */
304 void HS_ClientManager::notifyListener(afb_api_t api, const std::string &appid)
305 {
306     AFB_INFO("listen %s, notified", appid.c_str());
307     for(auto &it : listener_list) {
308         if(it.second->isListenAppId(appid)) {
309             it.second->notify(api, appid);
310         }
311     }
312
313     if(startup_appid == appid) {
314         struct json_object* json_param = json_object_new_object();
315         json_object_object_add(json_param, _area, json_object_new_string(startup_area.c_str()));
316         startup_area.clear();
317         startup_appid.clear();
318         pushEvent("showWindow", json_param, appid);
319     }
320 }