2 * Copyright (c) 2011 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "shortcut_private.h"
31 #include "shortcut_internal.h"
33 #define SHORTCUT_PKGNAME_LEN 512
34 #define REQUEST_ID_LEN 40
37 #define PROVIDER_BUS_NAME "org.tizen.data_provider_service"
38 #define PROVIDER_OBJECT_PATH "/org/tizen/data_provider_service"
39 #define PROVIDER_SHORTCUT_INTERFACE_NAME "org.tizen.data_provider_shortcut_service"
41 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
42 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
43 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
45 static GDBusConnection *_gdbus_conn = NULL;
46 static int monitor_id = 0;
47 static int provider_monitor_id = 0;
49 typedef struct _shortcut_request_cb_info {
50 int (*request_cb)(const char *appid, const char *name, int type, const char *content, const char *icon, pid_t pid, double period, int allow_duplicate, void *data);
52 } shortcut_request_cb_info;
54 typedef struct _shortcut_remove_cb_info {
55 int (*remove_cb)(const char *package_name, const char *name, int sender_pid, void *user_data);
57 } shortcut_remove_cb_info;
59 static shortcut_request_cb_info _request_callback_info;
60 static shortcut_remove_cb_info _remove_callback_info;
62 static void _shortcut_send_return(int ret_val, const char *request_id)
65 GDBusMessage *reply = NULL;
68 if (ret != SHORTCUT_ERROR_NONE) {
69 SHORTCUT_ERR("Can't init dbus %d", ret);
73 _send_sync_shortcut(g_variant_new("(is)", ret_val, request_id),
74 &reply, "send_return_value");
76 g_object_unref(reply);
80 static void _add_shortcut_notify(GVariant *parameters)
82 int ret = SHORTCUT_ERROR_NONE;
90 const char *request_id;
92 g_variant_get(parameters, "(&si&s&si&s&si)", &request_id, &sender_pid, &appid, &name, &type, &content, &icon, &allow_duplicate);
93 SHORTCUT_DBG("_add_shortcut_notify sender pid : [%d] appid : [%s]", sender_pid, appid);
95 if (_request_callback_info.request_cb != NULL)
96 ret = _request_callback_info.request_cb(appid, name, type, content, icon, sender_pid, -1.0f, allow_duplicate, _request_callback_info.data);
98 SHORTCUT_DBG("request_cb is null.");
100 _shortcut_send_return(ret, request_id);
104 /* LCOV_EXCL_START */
105 static void _add_shortcut_widget_notify(GVariant *parameters)
107 int ret = SHORTCUT_ERROR_NONE;
116 const char *request_id;
118 g_variant_get(parameters, "(&si&s&si&s&sdi)", &request_id, &sender_pid, &appid, &name, &type, &content, &icon, &period, &allow_duplicate);
119 SHORTCUT_DBG("_add_shortcut_widget_notify sender pid : [%d] appid : [%s]", sender_pid, appid);
121 if (_request_callback_info.request_cb != NULL)
122 ret = _request_callback_info.request_cb(appid, name, type, content, icon, sender_pid, period, allow_duplicate, _request_callback_info.data);
124 SHORTCUT_DBG("request_cb is null.");
126 _shortcut_send_return(ret, request_id);
130 static void _remove_shortcut_notify(GVariant *parameters)
132 int ret = SHORTCUT_ERROR_NONE;
136 const char *request_id;
138 g_variant_get(parameters, "(&si&s&s)", &request_id, &sender_pid, &appid, &name);
139 SHORTCUT_DBG("_remove_shortcut_notify sender pid : [%d] appid : [%s]", sender_pid, appid);
141 if (_remove_callback_info.remove_cb != NULL)
142 ret = _remove_callback_info.remove_cb(appid, name, sender_pid, _remove_callback_info.data);
144 SHORTCUT_DBG("remove_cb is null.");
146 _shortcut_send_return(ret, request_id);
149 /* LCOV_EXCL_START */
150 static void _handle_shortcut_notify(GDBusConnection *connection,
151 const gchar *sender_name,
152 const gchar *object_path,
153 const gchar *interface_name,
154 const gchar *signal_name,
155 GVariant *parameters,
158 SHORTCUT_DBG("signal_name : [%s]", signal_name);
159 if (g_strcmp0(signal_name, "add_shortcut_notify") == 0)
160 _add_shortcut_notify(parameters);
161 else if (g_strcmp0(signal_name, "add_shortcut_widget_notify") == 0)
162 _add_shortcut_widget_notify(parameters);
163 else if (g_strcmp0(signal_name, "remove_shortcut_notify") == 0)
164 _remove_shortcut_notify(parameters);
170 GError *error = NULL;
172 if (_gdbus_conn == NULL) {
173 _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
175 if (_gdbus_conn == NULL) {
176 /* LCOV_EXCL_START */
179 SHORTCUT_ERR("Failed to get dbus [%s]", error->message);
183 return SHORTCUT_ERROR_IO_ERROR;
186 shortcut_error_quark();
189 return SHORTCUT_ERROR_NONE;
192 int _dbus_signal_init(void)
194 int ret = SHORTCUT_ERROR_NONE;
197 if (monitor_id == 0) {
198 id = g_dbus_connection_signal_subscribe(
201 PROVIDER_SHORTCUT_INTERFACE_NAME, /* interface */
203 PROVIDER_OBJECT_PATH, /* path */
205 G_DBUS_SIGNAL_FLAGS_NONE,
206 _handle_shortcut_notify,
209 SHORTCUT_DBG("subscribe id : %d", id);
211 /* LCOV_EXCL_START */
212 ret = SHORTCUT_ERROR_IO_ERROR;
213 SHORTCUT_ERR("Failed to _register_noti_dbus_interface");
216 SHORTCUT_INFO("get dbus connection success");
224 char *_shortcut_get_pkgname_by_pid(void)
226 char pkgname[SHORTCUT_PKGNAME_LEN + 1] = { 0, };
227 char buf[SHORTCUT_PKGNAME_LEN + 1] = { 0, };
234 ret = aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname));
236 /* LCOV_EXCL_START */
237 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
239 fd = open(buf, O_RDONLY);
243 ret = read(fd, pkgname, sizeof(pkgname) - 1);
253 * "ret" is not able to be larger than "sizeof(pkgname) - 1",
254 * if the system is not going wrong.
257 if (strlen(pkgname) <= 0)
261 dup_pkgname = strdup(pkgname);
263 SHORTCUT_ERR("Heap: %d\n", errno); /* LCOV_EXCL_LINE */
269 * implement user request
271 int _send_sync_shortcut(GVariant *body, GDBusMessage **reply, char *cmd)
275 int ret = SHORTCUT_ERROR_NONE;
277 msg = g_dbus_message_new_method_call(
279 PROVIDER_OBJECT_PATH,
280 PROVIDER_SHORTCUT_INTERFACE_NAME,
283 /* LCOV_EXCL_START */
284 SHORTCUT_ERR("Can't allocate new method call");
286 g_variant_unref(body);
287 return SHORTCUT_ERROR_OUT_OF_MEMORY;
292 g_dbus_message_set_body(msg, body);
294 *reply = g_dbus_connection_send_message_with_reply_sync(
297 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
306 /* LCOV_EXCL_START */
307 ret = SHORTCUT_ERROR_COMM;
309 SHORTCUT_ERR("No reply. cmd = %s, error = %s", cmd, err->message);
310 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
311 ret = SHORTCUT_ERROR_PERMISSION_DENIED;
318 if (g_dbus_message_to_gerror(*reply, &err)) {
319 /* LCOV_EXCL_START */
320 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
321 ret = SHORTCUT_ERROR_PERMISSION_DENIED;
325 SHORTCUT_ERR("_send_sync_shortcut error %s err code: %d", err->message, ret);
331 SHORTCUT_DBG("_send_sync_shortcut done !!");
332 return SHORTCUT_ERROR_NONE;
335 int _send_service_register(void)
337 GDBusMessage *reply = NULL;
340 result = _send_sync_shortcut(g_variant_new("(i)", getuid()), &reply, "shortcut_service_register");
341 SHORTCUT_DBG("_send_service_register done");
345 static void _send_message_with_reply_sync_cb(GDBusConnection *connection,
351 GDBusMessage *reply = NULL;
353 struct result_cb_item *cb_item = (struct result_cb_item *)user_data;
355 if (cb_item == NULL) {
356 /* LCOV_EXCL_START */
357 SHORTCUT_ERR("Failed to get a callback item");
362 reply = g_dbus_connection_send_message_with_reply_finish(
368 /* LCOV_EXCL_START */
370 SHORTCUT_ERR("No reply. error = %s", err->message);
373 result = SHORTCUT_ERROR_COMM;
377 if (g_dbus_message_to_gerror(reply, &err)) {
378 /* LCOV_EXCL_START */
379 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
380 result = SHORTCUT_ERROR_PERMISSION_DENIED;
384 SHORTCUT_ERR("_send_message_with_reply_sync_cb error %s err code: %d", err->message, result);
388 body = g_dbus_message_get_body(reply);
389 g_variant_get(body, "(i)", &result);
392 if (cb_item->result_internal_cb)
393 cb_item->result_internal_cb(result, getpid(), cb_item->data); /* LCOV_EXCL_LINE */
394 else if (cb_item->result_cb)
395 cb_item->result_cb(result, cb_item->data);
398 g_object_unref(reply);
403 int _send_async_shortcut(GVariant *body, struct result_cb_item *cb_item, char *cmd)
407 msg = g_dbus_message_new_method_call(
409 PROVIDER_OBJECT_PATH,
410 PROVIDER_SHORTCUT_INTERFACE_NAME,
413 /* LCOV_EXCL_START */
414 SHORTCUT_ERR("Can't allocate new method call");
415 return SHORTCUT_ERROR_OUT_OF_MEMORY;
419 if (g_variant_is_floating(body))
423 g_dbus_message_set_body(msg, body);
425 g_dbus_connection_send_message_with_reply(
428 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
432 (GAsyncReadyCallback)_send_message_with_reply_sync_cb,
438 SHORTCUT_DBG("_send_async_shortcut done !!");
439 return SHORTCUT_ERROR_NONE;
442 int _check_privilege(void)
444 GDBusMessage *reply = NULL;
445 int ret = SHORTCUT_ERROR_NONE;
447 ret = _send_sync_shortcut(NULL, &reply, "check_privilege");
450 g_object_unref(reply);
456 /* LCOV_EXCL_START */
457 static void _on_name_appeared(GDBusConnection *connection,
459 const gchar *name_owner,
462 SHORTCUT_DBG("name appeared : %s", name);
463 _send_service_register();
467 /* LCOV_EXCL_START */
468 static void _on_name_vanished(GDBusConnection *connection,
472 SHORTCUT_DBG("name vanished : %s", name);
476 void _ipc_monitor_fini(void)
478 if (provider_monitor_id) {
479 g_bus_unwatch_name(provider_monitor_id);
480 provider_monitor_id = 0;
484 g_dbus_connection_signal_unsubscribe(_gdbus_conn, monitor_id);
489 g_object_unref(_gdbus_conn);
495 void _set_request_cb(shortcut_request_cb request_cb, void *data)
497 _request_callback_info.request_cb = request_cb;
498 _request_callback_info.data = data;
501 void _unset_request_cb(void)
503 if (_remove_callback_info.remove_cb == NULL &&
504 _remove_callback_info.data == NULL)
507 _request_callback_info.request_cb = NULL;
508 _request_callback_info.data = NULL;
511 void _set_remove_cb(shortcut_remove_cb remove_cb, void *data)
513 _remove_callback_info.remove_cb = remove_cb;
514 _remove_callback_info.data = data;
517 void _unset_remove_cb(void)
519 if (_request_callback_info.request_cb == NULL &&
520 _request_callback_info.data == NULL)
523 _remove_callback_info.remove_cb = NULL;
524 _remove_callback_info.data = NULL;
527 int _dbus_set_watch_name(void)
529 if (provider_monitor_id == 0) {
530 provider_monitor_id = g_bus_watch_name_on_connection(
533 G_BUS_NAME_WATCHER_FLAGS_NONE,
539 if (provider_monitor_id == 0) {
540 /* LCOV_EXCL_START */
541 SHORTCUT_ERR("watch on name fail");
542 return SHORTCUT_ERROR_IO_ERROR;
547 return SHORTCUT_ERROR_NONE;
550 char *_make_request_id(void)
553 char request_id[REQUEST_ID_LEN];
555 g_atomic_int_inc(&id);
556 snprintf(request_id, sizeof(request_id), "%d@%d", getpid(), id);
558 SHORTCUT_DBG("The request_id of shortcut is [%s]", request_id);
560 return strdup(request_id);
563 int _ready_to_send(char **appid, char **request_id)
568 if (ret != SHORTCUT_ERROR_NONE) {
569 /* LCOV_EXCL_START */
570 SHORTCUT_ERR("Can't init dbus %d", ret);
575 ret = _check_privilege();
576 if (ret != SHORTCUT_ERROR_NONE)
579 *appid = _shortcut_get_pkgname_by_pid();
580 if (*appid == NULL) {
581 /* LCOV_EXCL_START */
582 SHORTCUT_ERR("Can't get appid");
583 return SHORTCUT_ERROR_IO_ERROR;
587 *request_id = _make_request_id();
588 if (*request_id == NULL) {
589 SHORTCUT_ERR("Can't get request_id");
592 return SHORTCUT_ERROR_OUT_OF_MEMORY;
595 return SHORTCUT_ERROR_NONE;
598 EAPI int shortcut_add_to_home_sync(const char *name, shortcut_type type,
599 const char *uri, const char *icon, int allow_duplicate)
603 char *request_id = NULL;
605 GVariant *reply_body;
606 GDBusMessage *reply = NULL;
608 CHECK_SHORTCUT_FEATURE();
610 if (ADD_TO_HOME_IS_DYNAMICBOX(type)) {
611 /* LCOV_EXCL_START */
612 SHORTCUT_ERR("Invalid type used for adding a shortcut\n");
613 return SHORTCUT_ERROR_INVALID_PARAMETER;
617 ret = _ready_to_send(&appid, &request_id);
618 if (ret != SHORTCUT_ERROR_NONE) {
619 /* LCOV_EXCL_START */
620 SHORTCUT_ERR("ready to send error [%d]", ret);
634 body = g_variant_new("(sississi)", request_id, getpid(), appid, name,
635 type, uri, icon, allow_duplicate);
637 ret = _send_sync_shortcut(body, &reply, "add_shortcut");
638 if (ret == SHORTCUT_ERROR_NONE) {
639 reply_body = g_dbus_message_get_body(reply);
640 g_variant_get(reply_body, "(i)", &ret);
646 g_variant_unref(body);
650 g_object_unref(reply);
652 SHORTCUT_DBG("result[%d]", ret);
657 EAPI int shortcut_add_to_home_widget_sync(const char *name,
658 shortcut_widget_size_e size, const char *widget_id,
659 const char *icon, double period, int allow_duplicate)
663 char *request_id = NULL;
665 GVariant *reply_body;
666 GDBusMessage *reply = NULL;
668 CHECK_SHORTCUT_FEATURE();
671 SHORTCUT_ERR("AppID is null\n");
672 return SHORTCUT_ERROR_INVALID_PARAMETER;
675 if (!SHORTCUT_IS_WIDGET_SIZE(size)) {
676 /* LCOV_EXCL_START */
677 SHORTCUT_ERR("Invalid type used for adding a widget\n");
678 return SHORTCUT_ERROR_INVALID_PARAMETER;
682 ret = _ready_to_send(&appid, &request_id);
683 if (ret != SHORTCUT_ERROR_NONE) {
684 /* LCOV_EXCL_START */
685 SHORTCUT_ERR("ready to send error [%d]", ret);
690 body = g_variant_new("(sississdi)", request_id, getpid(), widget_id,
691 name, size, NULL, icon, period, allow_duplicate);
693 ret = _send_sync_shortcut(body, &reply, "add_shortcut_widget");
694 if (ret == SHORTCUT_ERROR_NONE) {
695 reply_body = g_dbus_message_get_body(reply);
696 g_variant_get(reply_body, "(i)", &ret);
702 g_variant_unref(body);
706 g_object_unref(reply);
708 SHORTCUT_DBG("result[%d]", ret);
713 EAPI int shortcut_remove_from_home_sync(const char *name)
717 char *request_id = NULL;
719 GVariant *reply_body;
720 GDBusMessage *reply = NULL;
722 CHECK_SHORTCUT_FEATURE();
725 SHORTCUT_ERR("name is NULL.");
726 return SHORTCUT_ERROR_INVALID_PARAMETER;
729 ret = _ready_to_send(&appid, &request_id);
730 if (ret != SHORTCUT_ERROR_NONE) {
731 /* LCOV_EXCL_START */
732 SHORTCUT_ERR("ready to send error [%d]", ret);
737 body = g_variant_new("(siss)", request_id, getpid(), appid, name);
739 ret = _send_sync_shortcut(body, &reply, "remove_shortcut");
740 if (ret == SHORTCUT_ERROR_NONE) {
741 reply_body = g_dbus_message_get_body(reply);
742 g_variant_get(reply_body, "(i)", &ret);
748 g_variant_unref(body);
752 g_object_unref(reply);
754 SHORTCUT_DBG("result[%d]", ret);