Atomic context initialisation for bindings
[src/app-framework-binder.git] / include / afb / afb-req-common.h
index d6d0258..adb0acf 100644 (file)
@@ -74,6 +74,11 @@ struct afb_req_itf
        void (*vverbose)(void *closure, int level, const char *file, int line, const char * func, const char *fmt, va_list args);
        struct afb_stored_req *(*store)(void *closure);
        void (*subcall_req)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure);
+
+       int (*has_permission)(void *closure, const char *permission);
+       char *(*get_application_id)(void *closure);
+
+       void *(*context_make)(void *closure, int replace, void *(*create_value)(void *creation_closure), void (*free_value)(void*), void *creation_closure);
 };
 
 /*
@@ -86,7 +91,7 @@ struct afb_req
 };
 
 /*
- * Checks wether the request 'req' is valid or not.
+ * Checks whether the request 'req' is valid or not.
  *
  * Returns 0 if not valid or 1 if valid.
  */
@@ -254,12 +259,22 @@ static inline void afb_req_context_set(struct afb_req req, void *context, void (
  */
 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
 {
-       void *result = afb_req_context_get(req);
-       if (!result) {
-               result = create_context();
-               afb_req_context_set(req, result, free_context);
-       }
-       return result;
+       return req.itf->context_make(req.closure, 0, (void *(*)(void*))(void*)create_context, free_context, 0);
+}
+
+/*
+ * Gets the pointer stored by the binding for the session of 'request'.
+ * If no previous pointer is stored or if 'replace' is not zero, a new value
+ * is generated using the function 'create_context' called with the 'closure'.
+ * If 'create_context' is NULL the generated value is 'closure'.
+ * When a value is created, the function 'free_context' is recorded and will
+ * be called (with the created value as argument) to free the created value when
+ * it is not more used.
+ * This function is atomic: it ensures that 2 threads will not race together.
+ */
+static inline void *afb_req_context_make(struct afb_req req, int replace, void *(*create_context)(void *closure), void (*free_context)(void*), void *closure)
+{
+       return req.itf->context_make(req.closure, replace, create_context, free_context, closure);
 }
 
 /*
@@ -431,3 +446,29 @@ static inline void afb_req_verbose(struct afb_req req, int level, const char *fi
 #else
 #define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,NULL,0,NULL,__VA_ARGS__)
 #endif
+
+/*
+ * Check whether the 'permission' is granted or not to the client
+ * identified by 'req'.
+ *
+ * Returns 1 if the permission is granted or 0 otherwise.
+ */
+static inline int afb_req_has_permission(struct afb_req req, const char *permission)
+{
+       return req.itf->has_permission(req.closure, permission);
+}
+
+/*
+ * Get the application identifier of the client application for the
+ * request 'req'.
+ *
+ * Returns the application identifier or NULL when the application
+ * can not be identified.
+ *
+ * The returned value if not NULL must be freed by the caller
+ */
+static inline char *afb_req_get_application_id(struct afb_req req)
+{
+       return req.itf->get_application_id(req.closure);
+}
+