homescreen/hs-proxy: Guard against empty appids
[apps/agl-service-homescreen.git] / src / hs-proxy.cpp
1 /*
2  * Copyright (c) 2019 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 "homescreen.h"
18 #include "hs-proxy.h"
19
20 struct closure_data {
21         std::string appid;
22         HS_ClientCtxt *clientCtx;
23         struct hs_instance *hs_instance;
24 };
25
26 const char _afm_main[] = "afm-main";
27
28
29 /**
30  * the callback function
31  *
32  * #### Parameters
33  *  - closure : the user defined closure pointer 'closure'
34  *  - object : a JSON object returned (can be NULL)
35  *  - error : a string not NULL in case of error but NULL on success
36  *  - info : a string handling some info (can be NULL)
37  *  - api : the api
38  *
39  * #### Return
40  *  None
41  *
42  */
43 static void api_callback(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api)
44 {
45     AFB_INFO("asynchronous call, error=%s, info=%s, object=%s.", error, info, json_object_get_string(object));
46     (void) api;
47     struct closure_data *cdata = static_cast<struct closure_data *>(closure);
48
49     if (!cdata->hs_instance) {
50            return;
51     }
52
53     struct HS_ClientManager *clientManager = cdata->hs_instance->client_manager;
54     if (!clientManager) {
55            return;
56     }
57
58     /* if we have an error then we couldn't start the application so we remove it */
59     if (error) {
60            AFB_INFO("asynchronous call, removing client %s", cdata->appid.c_str());
61            clientManager->removeClient(cdata->appid);
62     }
63
64     free(cdata);
65 }
66
67 /**
68  * call api asynchronous
69  *
70  * #### Parameters
71  *  - api : the api serving the request
72  *  - service : the api name of service
73  *  - verb : the verb of service
74  *  - args : parameter
75  *
76  * #### Return
77  *  None
78  *
79  */
80 static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args, struct closure_data *cdata)
81 {
82     AFB_INFO("service=%s verb=%s, args=%s.", service, verb, json_object_get_string(args));
83     afb_api_call(api, service, verb, args, api_callback, cdata);
84 }
85
86 /**
87  * call api synchronous
88  *
89  * #### Parameters
90  *  - api : the api serving the request
91  *  - service : the api name of service
92  *  - verb : the verb of afm-main
93  *  - args : parameter
94  *  - object : return the details of application
95  *
96  * #### Return
97  *  0 : success
98  *  1 : fail
99  *
100  */
101 static int api_call_sync(afb_api_t api, const char *service, const char *verb, struct json_object *args, struct json_object **object)
102 {
103     char *error = nullptr, *info = nullptr;
104     int ret = afb_api_call_sync(api, service, verb, args, object, &error, &info);
105     AFB_INFO("synchronous call, error=%s, info=%s.", error, info);
106     return ret;
107 }
108
109 /**
110  * get runnables application list
111  *
112  * #### Parameters
113  *  - api : the api serving the request
114  *  - object : return the details of appid
115  *
116  * #### Return
117  *  0 : success
118  *  1 : fail
119  *
120  */
121 int HS_AfmMainProxy::runnables(afb_api_t api, struct json_object **object)
122 {
123     return api_call_sync(api, _afm_main, __FUNCTION__, nullptr, object);
124 }
125
126 /**
127  * get running application list
128  *
129  * #### Parameters
130  *  - api : the api serving the request
131  *  - object : return the details of appid
132  *
133  * #### Return
134  *  0 : success
135  *  1 : fail
136  *
137  */
138 int HS_AfmMainProxy::ps(afb_api_t api, struct json_object **object)
139 {
140     return api_call_sync(api, _afm_main, "runners", nullptr, object);
141 }
142
143 /**
144  * get details of application
145  *
146  * #### Parameters
147  *  - api : the api serving the request
148  *  - id : the id to get details,liked "dashboard@0.1"
149  *  - object : return the details of application
150  *
151  * #### Return
152  *  0 : success
153  *  1 : fail
154  *
155  */
156 int HS_AfmMainProxy::detail(afb_api_t api, const std::string &id, struct json_object **object)
157 {
158     struct json_object *args = json_object_new_string(id.c_str());
159     return api_call_sync(api, _afm_main, __FUNCTION__, args, object);
160 }
161
162 /**
163  * start application
164  *
165  * #### Parameters
166  *  - request : the request
167  *  - id : the application id liked "dashboard@0.1"
168  *
169  * #### Return
170  *  None
171  *
172  */
173 void HS_AfmMainProxy::start(struct hs_instance *instance, afb_req_t request, const std::string &id)
174 {
175     struct closure_data *cdata;
176
177     /* tentatively store the client and client context, as the afb_req_t
178      * request will no longer be available in the async callback handler. This
179      * is similar to that is done showWindow(), handleRequest() in
180      * homescreen.cpp, but allows to fake the subscription here as well to
181      * avoid clients create/install dummy event handlers as to 'register' (or
182      * to keep track of applications started).
183      *
184      * In case api_callback() does return an error we'll remove then the client
185      * and client context there. We pass the closure_data with the client context
186      * and the application id to remove it.
187      */
188     if (!instance || id.empty())
189             return;
190
191     cdata = static_cast<struct closure_data *>(calloc(1, sizeof(*cdata)));
192     cdata->hs_instance = instance;
193     cdata->appid = id;
194
195     struct HS_ClientManager *clientManager = instance->client_manager;
196     if (!clientManager) {
197             return;
198     }
199
200     clientManager->addClient(request, id);
201     api_call(request->api, _afm_main, __FUNCTION__, json_object_new_string(id.c_str()), cdata);
202 }