lib: made API multi thread safe(untested)
[platform/core/appfw/message-port-dbus.git] / lib / message-port.c
1 #include "message-port.h"
2 #include "msgport-factory.h"
3 #include "msgport-manager.h"
4 #include "msgport-utils.h"
5 #include "common/log.h"
6
7 static int
8 _messageport_register_port (const char *name, gboolean is_trusted, messageport_message_cb cb)
9 {
10     int port_id = 0; /* id of the port created */
11     messageport_error_e res;
12     MsgPortManager *manager = msgport_factory_get_manager ();
13
14     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
15
16     res = msgport_manager_register_service (manager, name, is_trusted, cb, &port_id);
17
18     return port_id > 0 ? port_id : (int)res;
19 }
20
21 static messageport_error_e
22 _messageport_check_remote_port (const char *app_id, const char *port, gboolean is_trusted, gboolean *exists)
23 {
24     messageport_error_e res;
25     MsgPortManager *manager = msgport_factory_get_manager ();
26
27     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
28
29     res = msgport_manager_check_remote_service (manager, app_id, port, is_trusted, NULL);
30
31     if (exists) *exists = (res == MESSAGEPORT_ERROR_NONE);
32
33     return res;
34 }
35
36 static messageport_error_e
37 _messageport_send_message (const char *app_id, const char *port, gboolean is_trusted, bundle *message)
38 {
39     MsgPortManager *manager = msgport_factory_get_manager ();
40
41     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
42
43     GVariant *v_data = bundle_to_variant_map (message);
44
45     return msgport_manager_send_message (manager, app_id, port, is_trusted, v_data);
46 }
47
48 messageport_error_e
49 _messageport_send_bidirectional_message (int id, const gchar *remote_app_id, const gchar *remote_port, gboolean is_trusted, bundle *message)
50 {
51     MsgPortManager *manager = msgport_factory_get_manager ();
52
53     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
54
55     GVariant *v_data = bundle_to_variant_map (message);
56
57     return msgport_manager_send_bidirectional_message (manager, id, remote_app_id, remote_port, is_trusted, v_data);
58 }
59
60 /*
61  * API
62  */
63
64 int
65 messageport_register_local_port(const char* local_port, messageport_message_cb callback)
66 {
67     return _messageport_register_port (local_port, FALSE, callback);
68 }
69
70 messageport_error_e
71 messageport_register_trusted_local_port (const char *local_port, messageport_message_cb callback)
72 {
73     return _messageport_register_port (local_port, TRUE, callback);
74 }
75
76 messageport_error_e
77 messageport_check_remote_port (const char *remote_app_id, const char *port_name, gboolean *exists)
78 {
79     return _messageport_check_remote_port (remote_app_id, port_name, FALSE, exists);
80 }
81
82 messageport_error_e
83 messageport_check_trusted_remote_port (const char *remote_app_id, const char *port_name, gboolean *exists)
84 {
85     return _messageport_check_remote_port (remote_app_id, port_name, TRUE, exists);
86 }
87
88 messageport_error_e
89 messageport_send_message (const char* remote_app_id, const char* remote_port, bundle* message)
90 {
91     return _messageport_send_message (remote_app_id, remote_port, FALSE, message);
92 }
93
94 messageport_error_e
95 messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message)
96 {
97     return _messageport_send_message (remote_app_id, remote_port, TRUE, message);
98 }
99
100 messageport_error_e
101 messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* data)
102 {
103     return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, FALSE, data);
104 }
105
106 messageport_error_e
107 messageport_send_bidirectional_trusted_message (int id, const char *remote_app_id, const char *remote_port, bundle *data)
108 {
109     return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, TRUE, data);
110 }
111
112 messageport_error_e
113 messageport_get_local_port_name(int id, char **name_out)
114 {
115     MsgPortManager *manager = msgport_factory_get_manager ();
116
117     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
118
119     return msgport_manager_get_service_name (manager, id, name_out);
120 }
121
122 messageport_error_e
123 messageport_check_trusted_local_port (int id, gboolean *is_trusted_out)
124 {
125     MsgPortManager *manager = msgport_factory_get_manager ();
126
127     if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
128
129     return msgport_manager_get_service_is_trusted (manager, id, is_trusted_out);
130 }
131