Make single afb_request_subcall
[src/app-framework-binder.git] / src / afb-xreq.c
index 8a05070..8bfb363 100644 (file)
@@ -97,25 +97,26 @@ struct subcall
                struct {
                        union {
                                void (*callback)(void*, int, struct json_object*);
-                               void (*callback2)(void*, int, struct json_object*, struct afb_req);
+                               void (*callback_req)(void*, int, struct json_object*, struct afb_req);
+                               void (*callback_request)(void*, int, struct json_object*, struct afb_request*);
                        };
                        void *closure;
                };
        };
 };
 
-static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
+static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
 {
        struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
 
-       return afb_xreq_subscribe(subcall->xreq.caller, event);
+       return afb_xreq_subscribe(subcall->xreq.caller, eventid);
 }
 
-static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
+static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
 {
        struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
 
-       return afb_xreq_unsubscribe(subcall->xreq.caller, event);
+       return afb_xreq_unsubscribe(subcall->xreq.caller, eventid);
 }
 
 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
@@ -187,7 +188,12 @@ static void subcall_on_reply(struct subcall *subcall, int status, struct json_ob
 
 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
 {
-       subcall->callback2(subcall->closure, status, result, to_req(subcall->xreq.caller));
+       subcall->callback_req(subcall->closure, status, result, to_req(subcall->xreq.caller));
+}
+
+static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result)
+{
+       subcall->callback_request(subcall->closure, status, result, to_request(subcall->xreq.caller));
 }
 
 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
@@ -202,6 +208,12 @@ static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, str
        subcall_req_on_reply(subcall, status, result);
 }
 
+static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
+{
+       afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
+       subcall_request_on_reply(subcall, status, result);
+}
+
 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
 {
        struct afb_xreq *xreq = closure;
@@ -237,11 +249,18 @@ static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct
 
 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
 {
-       subcall->callback2 = callback;
+       subcall->callback_req = callback;
        subcall->closure = cb_closure;
        subcall_process(subcall, subcall_req_on_reply);
 }
 
+static void subcall_request(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
+{
+       subcall->callback_request = callback;
+       subcall->closure = cb_closure;
+       subcall_process(subcall, subcall_request_on_reply);
+}
+
 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
 {
        subcall->callback = callback;
@@ -251,11 +270,18 @@ static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int,
 
 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
 {
-       subcall->callback2 = callback;
+       subcall->callback_req = callback;
        subcall->closure = cb_closure;
        subcall_process(subcall, subcall_req_hooked_on_reply);
 }
 
+static void subcall_request_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
+{
+       subcall->callback_request = callback;
+       subcall->closure = cb_closure;
+       subcall_process(subcall, subcall_request_hooked_on_reply);
+}
+
 static void subcall_sync_leave(struct subcall *subcall)
 {
        struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
@@ -396,10 +422,11 @@ static void xreq_context_set_cb(struct afb_request *closure, void *value, void (
        afb_context_set(&xreq->context, value, free_value);
 }
 
-static void xreq_addref_cb(struct afb_request *closure)
+static struct afb_request *xreq_addref_cb(struct afb_request *closure)
 {
        struct afb_xreq *xreq = from_request(closure);
        afb_xreq_unhooked_addref(xreq);
+       return closure;
 }
 
 static void xreq_unref_cb(struct afb_request *closure)
@@ -420,35 +447,47 @@ static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
        return afb_context_change_loa(&xreq->context, level);
 }
 
+static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
+{
+       return xreq_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
+}
+
+static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
 {
        struct afb_xreq *xreq = from_request(closure);
-       return afb_xreq_subscribe(xreq, event);
+       return afb_xreq_subscribe(xreq, eventid);
 }
 
-int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
+int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
 {
        if (xreq->listener)
-               return afb_evt_add_watch(xreq->listener, event);
+               return afb_evt_add_watch(xreq->listener, eventid);
        if (xreq->queryitf->subscribe)
-               return xreq->queryitf->subscribe(xreq, event);
+               return xreq->queryitf->subscribe(xreq, eventid);
        ERROR("no event listener, subscription impossible");
        errno = EINVAL;
        return -1;
 }
 
+static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
+{
+       return xreq_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
+}
+
+static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
 {
        struct afb_xreq *xreq = from_request(closure);
-       return afb_xreq_unsubscribe(xreq, event);
+       return afb_xreq_unsubscribe(xreq, eventid);
 }
 
-int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
+int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
 {
        if (xreq->listener)
-               return afb_evt_remove_watch(xreq->listener, event);
+               return afb_evt_remove_watch(xreq->listener, eventid);
        if (xreq->queryitf->unsubscribe)
-               return xreq->queryitf->unsubscribe(xreq, event);
+               return xreq->queryitf->unsubscribe(xreq, eventid);
        ERROR("no event listener, unsubscription impossible");
        errno = EINVAL;
        return -1;
@@ -484,6 +523,21 @@ static void xreq_subcall_req_cb(struct afb_request *closure, const char *api, co
        }
 }
 
+static void xreq_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
+{
+       struct afb_xreq *xreq = from_request(closure);
+       struct subcall *sc;
+
+       sc = subcall_alloc(xreq, api, verb, args);
+       if (sc == NULL) {
+               if (callback)
+                       callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq));
+               json_object_put(args);
+       } else {
+               subcall_request(sc, callback, cb_closure);
+       }
+}
+
 
 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
 {
@@ -598,11 +652,11 @@ static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value,
        xreq_context_set_cb(closure, value, free_value);
 }
 
-static void xreq_hooked_addref_cb(struct afb_request *closure)
+static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
 {
        struct afb_xreq *xreq = from_request(closure);
        afb_hook_xreq_addref(xreq);
-       xreq_addref_cb(closure);
+       return xreq_addref_cb(closure);
 }
 
 static void xreq_hooked_unref_cb(struct afb_request *closure)
@@ -626,18 +680,30 @@ static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned
        return afb_hook_xreq_session_set_LOA(xreq, level, r);
 }
 
+static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
 {
-       int r = xreq_subscribe_cb(closure, event);
+       return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
+}
+
+static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
+{
+       int r = xreq_subscribe_eventid_cb(closure, eventid);
        struct afb_xreq *xreq = from_request(closure);
-       return afb_hook_xreq_subscribe(xreq, event, r);
+       return afb_hook_xreq_subscribe(xreq, eventid, r);
 }
 
+static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
 {
-       int r = xreq_unsubscribe_cb(closure, event);
+       return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
+}
+
+static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
+{
+       int r = xreq_unsubscribe_eventid_cb(closure, eventid);
        struct afb_xreq *xreq = from_request(closure);
-       return afb_hook_xreq_unsubscribe(xreq, event, r);
+       return afb_hook_xreq_unsubscribe(xreq, eventid, r);
 }
 
 static void xreq_hooked_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
@@ -672,6 +738,22 @@ static void xreq_hooked_subcall_req_cb(struct afb_request *closure, const char *
        }
 }
 
+static void xreq_hooked_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
+{
+       struct afb_xreq *xreq = from_request(closure);
+       struct subcall *sc;
+
+       afb_hook_xreq_subcall(xreq, api, verb, args);
+       sc = subcall_alloc(xreq, api, verb, args);
+       if (sc == NULL) {
+               if (callback)
+                       callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq));
+               json_object_put(args);
+       } else {
+               subcall_request_hooked(sc, callback, cb_closure);
+       }
+}
+
 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
 {
        int r;
@@ -744,7 +826,10 @@ const struct afb_request_itf xreq_itf = {
        .subcall_req = xreq_subcall_req_cb,
        .has_permission = xreq_has_permission_cb,
        .get_application_id = xreq_get_application_id_cb,
-       .context_make = xreq_context_make_cb
+       .context_make = xreq_context_make_cb,
+       .subscribe_eventid = xreq_subscribe_eventid_cb,
+       .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
+       .subcall_request = xreq_subcall_request_cb,
 };
 
 const struct afb_request_itf xreq_hooked_itf = {
@@ -769,7 +854,10 @@ const struct afb_request_itf xreq_hooked_itf = {
        .subcall_req = xreq_hooked_subcall_req_cb,
        .has_permission = xreq_hooked_has_permission_cb,
        .get_application_id = xreq_hooked_get_application_id_cb,
-       .context_make = xreq_hooked_context_make_cb
+       .context_make = xreq_hooked_context_make_cb,
+       .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
+       .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
+       .subcall_request = xreq_hooked_subcall_request_cb,
 };
 
 /******************************************************************************/
@@ -835,12 +923,12 @@ void afb_xreq_unref(struct afb_xreq *xreq)
        afb_request_unref(to_request(xreq));
 }
 
-void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
+void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
 {
-       xreq_subcall_cb(to_request(xreq), api, verb, args, callback, cb_closure);
+       xreq_subcall_request_cb(to_request(xreq), api, verb, args, callback, cb_closure);
 }
 
-void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
+void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
 {
        afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure);
 }