afm-binding: Common code getting application id
[src/app-framework-main.git] / src / afm-binding.c
index b111f09..51316e7 100644 (file)
@@ -27,7 +27,6 @@
 
 #include "verbose.h"
 #include "utils-systemd.h"
-#include "afm.h"
 #include "afm-udb.h"
 #include "afm-urun.h"
 #include "wgt-info.h"
@@ -90,12 +89,42 @@ static void application_list_changed(const char *operation, const char *data)
        afb_event_broadcast(applist_changed_event, e);
 }
 
+/*
+ * retrieves the 'appid' in parameters received with the
+ * request 'req' for the 'method'.
+ *
+ * Returns 1 in case of success.
+ * Otherwise, if the 'appid' can't be retrieved, an error stating
+ * the bad request is replied for 'req' and 0 is returned.
+ */
+static int onappid(struct afb_req req, const char *method, const char **appid)
+{
+       struct json_object *json;
+
+       /* get the paramaters of the request */
+       json = afb_req_json(req);
+
+       /* get the appid if any */
+       if (!wrap_json_unpack(json, "s", appid)
+        || !wrap_json_unpack(json, "{si}", _id_, appid)) {
+               /* found */
+               INFO("method %s called for %s", method, *appid);
+               return 1;
+       }
+
+       /* nothing appropriate */
+       INFO("bad request method %s: %s", method,
+                                       json_object_to_json_string(json));
+       bad_request(req);
+       return 0;
+}
+
 /*
  * retrieves the 'runid' in parameters received with the
  * request 'req' for the 'method'.
  *
  * Returns 1 in case of success.
- * Otherwise, if the 'runid' can't be retrived, an error stating
+ * Otherwise, if the 'runid' can't be retrieved, an error stating
  * the bad request is replied for 'req' and 0 is returned.
  */
 static int onrunid(struct afb_req req, const char *method, int *runid)
@@ -103,8 +132,8 @@ static int onrunid(struct afb_req req, const char *method, int *runid)
        struct json_object *json;
 
        json = afb_req_json(req);
-       if (wrap_json_unpack(json, "s", runid)
-               || wrap_json_unpack(json, "{ss}", "runid", runid)) {
+       if (wrap_json_unpack(json, "i", runid)
+               && wrap_json_unpack(json, "{si}", _runid_, runid)) {
                INFO("bad request method %s: %s", method,
                                        json_object_to_json_string(json));
                bad_request(req);
@@ -143,7 +172,7 @@ static void runnables(struct afb_req req)
 {
        struct json_object *resp;
        INFO("method runnables called");
-       resp = afm_udb_applications_public(afudb);
+       resp = afm_udb_applications_public(afudb, afb_req_get_uid(req));
        afb_req_success(req, resp, NULL);
 }
 
@@ -153,19 +182,14 @@ static void runnables(struct afb_req req)
 static void detail(struct afb_req req)
 {
        const char *appid;
-       struct json_object *resp, *json;
+       struct json_object *resp;
 
        /* scan the request */
-       json = afb_req_json(req);
-       if (wrap_json_unpack(json, "s", &appid)
-               || wrap_json_unpack(json, "{ss}", _id_, &appid)) {
-               bad_request(req);
+       if (!onappid(req, _detail_, &appid))
                return;
-       }
 
        /* wants details for appid */
-       INFO("method detail called for %s", appid);
-       resp = afm_udb_get_application_public(afudb, appid);
+       resp = afm_udb_get_application_public(afudb, appid, afb_req_get_uid(req));
        if (resp)
                afb_req_success(req, resp, NULL);
        else
@@ -178,27 +202,22 @@ static void detail(struct afb_req req)
 static void start(struct afb_req req)
 {
        const char *appid;
-       struct json_object *appli, *resp, *json;
+       struct json_object *appli, *resp;
        int runid;
 
        /* scan the request */
-       json = afb_req_json(req);
-       if (wrap_json_unpack(json, "s", &appid)
-               || wrap_json_unpack(json, "{ss}", _id_, &appid)) {
-               bad_request(req);
+       if (!onappid(req, _start_, &appid))
                return;
-       }
 
        /* get the application */
-       INFO("method start called for %s", appid);
-       appli = afm_udb_get_application_private(afudb, appid);
+       appli = afm_udb_get_application_private(afudb, appid, afb_req_get_uid(req));
        if (appli == NULL) {
                not_found(req);
                return;
        }
 
        /* launch the application */
-       runid = afm_urun_start(appli);
+       runid = afm_urun_start(appli, afb_req_get_uid(req));
        if (runid <= 0) {
                cant_start(req);
                return;
@@ -206,7 +225,11 @@ static void start(struct afb_req req)
 
        /* returns */
        resp = NULL;
+#if 0
        wrap_json_pack(&resp, "{si}", _runid_, runid);
+#else
+       wrap_json_pack(&resp, "i", runid);
+#endif
        afb_req_success(req, resp, NULL);
 }
 
@@ -216,34 +239,29 @@ static void start(struct afb_req req)
 static void once(struct afb_req req)
 {
        const char *appid;
-       struct json_object *appli, *resp, *json;
+       struct json_object *appli, *resp;
        int runid;
 
        /* scan the request */
-       json = afb_req_json(req);
-       if (wrap_json_unpack(json, "s", &appid)
-               || wrap_json_unpack(json, "{ss}", _id_, &appid)) {
-               bad_request(req);
+       if (!onappid(req, _once_, &appid))
                return;
-       }
 
        /* get the application */
-       INFO("method once called for %s", appid);
-       appli = afm_udb_get_application_private(afudb, appid);
+       appli = afm_udb_get_application_private(afudb, appid, afb_req_get_uid(req));
        if (appli == NULL) {
                not_found(req);
                return;
        }
 
        /* launch the application */
-       runid = afm_urun_once(appli);
+       runid = afm_urun_once(appli, afb_req_get_uid(req));
        if (runid <= 0) {
                cant_start(req);
                return;
        }
 
        /* returns the state */
-       resp = afm_urun_state(afudb, runid);
+       resp = afm_urun_state(afudb, runid, afb_req_get_uid(req));
        afb_req_success(req, resp, NULL);
 }
 
@@ -254,7 +272,7 @@ static void pause(struct afb_req req)
 {
        int runid, status;
        if (onrunid(req, "pause", &runid)) {
-               status = afm_urun_pause(runid);
+               status = afm_urun_pause(runid, afb_req_get_uid(req));
                reply_status(req, status, _not_found_);
        }
 }
@@ -266,7 +284,7 @@ static void resume(struct afb_req req)
 {
        int runid, status;
        if (onrunid(req, "resume", &runid)) {
-               status = afm_urun_resume(runid);
+               status = afm_urun_resume(runid, afb_req_get_uid(req));
                reply_status(req, status, _not_found_);
        }
 }
@@ -278,7 +296,7 @@ static void terminate(struct afb_req req)
 {
        int runid, status;
        if (onrunid(req, "terminate", &runid)) {
-               status = afm_urun_terminate(runid);
+               status = afm_urun_terminate(runid, afb_req_get_uid(req));
                reply_status(req, status, _not_found_);
        }
 }
@@ -290,7 +308,7 @@ static void runners(struct afb_req req)
 {
        struct json_object *resp;
        INFO("method runners called");
-       resp = afm_urun_list(afudb);
+       resp = afm_urun_list(afudb, afb_req_get_uid(req));
        afb_req_success(req, resp, NULL);
 }
 
@@ -302,56 +320,11 @@ static void state(struct afb_req req)
        int runid;
        struct json_object *resp;
        if (onrunid(req, "state", &runid)) {
-               resp = afm_urun_state(afudb, runid);
+               resp = afm_urun_state(afudb, runid, afb_req_get_uid(req));
                reply(req, resp, _not_found_);
        }
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 static void install(struct afb_req req)
 {
        const char *wgtfile;
@@ -370,8 +343,8 @@ static void install(struct afb_req req)
        /* scan the request */
        json = afb_req_json(req);
        if (wrap_json_unpack(json, "s", &wgtfile)
-               || wrap_json_unpack(json, "{ss s?s s?b s?b}",
-                               "widget", &wgtfile,
+               && wrap_json_unpack(json, "{ss s?s s?b s?b}",
+                               "wgt", &wgtfile,
                                "root", &root,
                                "force", &force,
                                "reload", &reload)) {
@@ -383,6 +356,7 @@ static void install(struct afb_req req)
        if (ifo == NULL)
                afb_req_fail_f(req, "failed", "installation failed: %m");
        else {
+               afm_udb_update(afudb);
                /* reload if needed */
                if (reload)
                        do_reloads();
@@ -410,7 +384,7 @@ static void uninstall(struct afb_req req)
        /* scan the request */
        json = afb_req_json(req);
        if (wrap_json_unpack(json, "s", &idaver)
-               || wrap_json_unpack(json, "{ss s?s}",
+               && wrap_json_unpack(json, "{ss s?s}",
                                _id_, &idaver,
                                "root", &root)) {
                return bad_request(req);
@@ -421,6 +395,7 @@ static void uninstall(struct afb_req req)
        if (rc)
                afb_req_fail_f(req, "failed", "uninstallation failed: %m");
        else {
+               afm_udb_update(afudb);
                afb_req_success(req, NULL, NULL);
                application_list_changed(_uninstall_, idaver);
        }
@@ -429,16 +404,12 @@ static void uninstall(struct afb_req req)
 static int init()
 {
        /* init database */
-       afudb = afm_udb_create(1, 1, "afm-appli-");
+       afudb = afm_udb_create(1, 0, "afm-appli-");
        if (!afudb) {
                ERROR("afm_udb_create failed");
                return -1;
        }
 
-       /* set the systemd's buses */
-       systemd_set_bus(0, afb_daemon_get_system_bus());
-       systemd_set_bus(1, afb_daemon_get_user_bus());
-
        /* create TRUE */
        json_true = json_object_new_boolean(1);