hs-proxy: Keep track of clientCtx and client when starting application
[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     struct closure_data *cdata = static_cast<struct closure_data *>(closure);
47
48     if (!cdata->hs_instance) {
49            return;
50     }
51
52     struct HS_ClientManager *clientManager = cdata->hs_instance->client_manager;
53     if (!clientManager) {
54            return;
55     }
56
57     /* if we have an error then we couldn't start the application so we remove it */
58     if (error) {
59            clientManager->removeClientCtxt(cdata->clientCtx);
60            clientManager->removeClient(cdata->appid);
61     }
62
63     free(cdata);
64 }
65
66 /**
67  * call api asynchronous
68  *
69  * #### Parameters
70  *  - api : the api serving the request
71  *  - service : the api name of service
72  *  - verb : the verb of service
73  *  - args : parameter
74  *
75  * #### Return
76  *  None
77  *
78  */
79 static void api_call(afb_api_t api, const char *service, const char *verb, struct json_object *args, struct closure_data *cdata)
80 {
81     AFB_INFO("service=%s verb=%s, args=%s.", service, verb, json_object_get_string(args));
82     afb_api_call(api, service, verb, args, api_callback, cdata);
83 }
84
85 /**
86  * call api synchronous
87  *
88  * #### Parameters
89  *  - api : the api serving the request
90  *  - service : the api name of service
91  *  - verb : the verb of afm-main
92  *  - args : parameter
93  *  - object : return the details of application
94  *
95  * #### Return
96  *  0 : success
97  *  1 : fail
98  *
99  */
100 static int api_call_sync(afb_api_t api, const char *service, const char *verb, struct json_object *args, struct json_object **object)
101 {
102     char *error = nullptr, *info = nullptr;
103     int ret = afb_api_call_sync(api, service, verb, args, object, &error, &info);
104     AFB_INFO("synchronous call, error=%s, info=%s.", error, info);
105     return ret;
106 }
107
108 /**
109  * get runnables application list
110  *
111  * #### Parameters
112  *  - api : the api serving the request
113  *  - object : return the details of appid
114  *
115  * #### Return
116  *  0 : success
117  *  1 : fail
118  *
119  */
120 int HS_AfmMainProxy::runnables(afb_api_t api, struct json_object **object)
121 {
122     return api_call_sync(api, _afm_main, __FUNCTION__, nullptr, object);
123 }
124
125 /**
126  * get details of application
127  *
128  * #### Parameters
129  *  - api : the api serving the request
130  *  - id : the id to get details,liked "dashboard@0.1"
131  *  - object : return the details of application
132  *
133  * #### Return
134  *  0 : success
135  *  1 : fail
136  *
137  */
138 int HS_AfmMainProxy::detail(afb_api_t api, const std::string &id, struct json_object **object)
139 {
140     struct json_object *args = json_object_new_string(id.c_str());
141     return api_call_sync(api, _afm_main, __FUNCTION__, args, object);
142 }
143
144 /**
145  * start application
146  *
147  * #### Parameters
148  *  - request : the request
149  *  - id : the application id liked "dashboard@0.1"
150  *
151  * #### Return
152  *  None
153  *
154  */
155 void HS_AfmMainProxy::start(struct hs_instance *instance, afb_req_t request, const std::string &id)
156 {
157     struct json_object *args = json_object_new_string(id.c_str());
158     struct closure_data *cdata;
159
160     /* tentatively store the client and client context, as the afb_req_t
161      * request will no longer be available in the async callback handler. This
162      * is similar to that is done showWindow(), handleRequest() in
163      * homescreen.cpp, but allows to fake the subscription here as well to
164      * avoid clients create/install dummy event handlers as to 'register' (or
165      * to keep track of applications started).
166      *
167      * In case api_callback() does return an error we'll remove then the client
168      * and client context there. We pass the closure_data with the client context
169      * and the application id to remove it.
170      */
171     if (!instance)
172             return;
173
174     cdata = static_cast<struct closure_data *>(calloc(1, sizeof(*cdata)));
175     cdata->hs_instance = instance;
176     cdata->appid = id;
177
178     struct HS_ClientManager *clientManager = instance->client_manager;
179     if (!clientManager) {
180             return;
181     }
182
183     cdata->clientCtx = clientManager->createClientCtxt(request, id);
184     HS_Client *client = clientManager->addClient(request, id);
185     if (client) {
186             if (client->handleRequest(request, "subscribe"))
187                     AFB_WARNING("Failed to handle subcribe\n");
188     }
189
190     api_call(request->api, _afm_main, __FUNCTION__, args, cdata);
191 }