2 * Copyright (c) 2018 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.
25 #include "autofill_private.h"
26 #include "autofill_proxy.h"
28 #define AUTOFILL_DAEMON_APP_ID "org.tizen.autofilld"
33 #define LOG_TAG "AUTOFILL"
35 static int _autofill_context_count = 0;
38 static bool fill_response_item_cb(rpc_port_autofill_response_item_h response_items, void *user_data)
41 char *presentation_text = NULL;
43 autofill_fill_response_group_h res_group = (autofill_fill_response_group_h)user_data;
48 rpc_port_autofill_response_item_get_id(response_items, &id);
49 rpc_port_autofill_response_item_get_presentation_text(response_items, &presentation_text);
50 rpc_port_autofill_response_item_get_value(response_items, &value);
52 LOGD("id : %s, presentation text : %s, value : %s", id, presentation_text, value);
54 autofill_fill_response_item_h ritem;
56 autofill_fill_response_item_create(&ritem);
57 autofill_fill_response_item_set_id(ritem, id);
58 autofill_fill_response_item_set_presentation_text(ritem, presentation_text);
59 autofill_fill_response_item_set_value(ritem, value);
61 autofill_fill_response_group_add_item(res_group, ritem);
66 if (presentation_text)
67 free(presentation_text);
72 autofill_fill_response_item_destroy(ritem);
77 static bool fill_response_group_cb(rpc_port_autofill_response_group_h response_groups, void *user_data)
79 autofill_fill_response_h rh = (autofill_fill_response_h)user_data;
80 autofill_fill_response_group_h res_group;
82 autofill_fill_response_group_create(&res_group);
84 rpc_port_autofill_response_group_foreach_response_items(response_groups, fill_response_item_cb, res_group);
86 autofill_fill_response_add_group(rh, res_group);
88 autofill_fill_response_group_destroy(res_group);
93 static void __fill_response_recv_cb(void *user_data, rpc_port_autofill_fill_response_h response_h)
97 autofill_h ah = user_data;
98 autofill_fill_response_h rh;
100 autofill_fill_response_create(&rh);
102 rpc_port_autofill_fill_response_get_app_id(response_h, &app_id);
103 rpc_port_autofill_fill_response_get_view_id(response_h, &view_id);
105 LOGD("app id : %s, view id : %s", app_id, view_id);
108 autofill_fill_response_set_app_id(rh, app_id);
113 autofill_fill_response_set_view_id(rh, view_id);
117 rpc_port_autofill_fill_response_foreach_response_groups(response_h, fill_response_group_cb, rh);
119 if (ah->autofill_fill_response_received_cb)
120 ah->autofill_fill_response_received_cb(ah, rh, ah->autofill_fill_response_data);
122 autofill_fill_response_destroy(rh);
125 static void __auth_info_recv_cb(void *user_data, rpc_port_autofill_auth_info_h auth_info_h)
127 autofill_auth_info_h aih;
128 autofill_h ah = user_data;
130 autofill_auth_info_create(&aih);
132 bool autofill_data_present = false;
133 bool authentication_needed = false;
135 char *view_id = NULL;
136 char *service_name = NULL;
137 char *service_logo_image_path = NULL;
138 char *service_message = NULL;
140 rpc_port_autofill_auth_info_get_app_id(auth_info_h, &app_id);
141 rpc_port_autofill_auth_info_get_view_id(auth_info_h, &view_id);
142 rpc_port_autofill_auth_info_get_exist_autofill_data(auth_info_h, &autofill_data_present);
143 rpc_port_autofill_auth_info_get_need_authentication(auth_info_h, &authentication_needed);
144 rpc_port_autofill_auth_info_get_service_name(auth_info_h, &service_name);
145 rpc_port_autofill_auth_info_get_service_logo_image_path(auth_info_h, &service_logo_image_path);
146 rpc_port_autofill_auth_info_get_service_message(auth_info_h, &service_message);
148 LOGD("app id : %s, view id : %s, service name : %s", app_id, view_id, service_name);
150 autofill_auth_info_set_app_id(aih, app_id);
151 autofill_auth_info_set_view_id(aih, view_id);
152 autofill_auth_info_set_autofill_data_present(aih, autofill_data_present);
153 autofill_auth_info_set_authentication_needed(aih, authentication_needed);
154 autofill_auth_info_set_service_name(aih, service_name);
155 autofill_auth_info_set_service_logo_image_path(aih, service_logo_image_path);
156 autofill_auth_info_set_service_message(aih, service_message);
167 if (service_logo_image_path)
168 free(service_logo_image_path);
171 free(service_message);
174 if (ah->autofill_auth_info_received_cb)
175 ah->autofill_auth_info_received_cb(ah, aih, ah->autofill_auth_info_data);
178 LOGW("no user data");
181 autofill_auth_info_destroy(aih);
184 static void __error_info_recv_cb(void *user_data, rpc_port_autofill_error_info_h error_info_h)
186 autofill_error_info_h eih = NULL;
187 autofill_h ah = user_data;
192 autofill_error_info_create(&eih);
194 rpc_port_autofill_error_info_get_app_id(error_info_h, &app_id);
195 rpc_port_autofill_error_info_get_error_code(error_info_h, &error_code);
197 autofill_error_info_set_app_id(eih, app_id);
198 autofill_error_info_set_error_code(eih, error_code);
200 LOGI("error code : %x", error_code);
206 if (ah->autofill_error_info_received_cb)
207 ah->autofill_error_info_received_cb(ah, eih, ah->autofill_error_info_data);
210 LOGW("no user data");
213 autofill_error_info_destroy(eih);
217 static void __on_connected(rpc_port_proxy_AutofillAppPort_h h, void *user_data)
219 autofill_h ah = user_data;
221 LOGW("no user data");
225 ah->connected = true;
228 rpc_port_AutofillAppPort_autofill_fill_response_received_cb_h fill_response_received_cb = rpc_port_AutofillAppPort_autofill_fill_response_received_cb_create(__fill_response_recv_cb, false, ah);
229 rpc_port_AutofillAppPort_autofill_auth_info_received_cb_h auth_info_received_cb = rpc_port_AutofillAppPort_autofill_auth_info_received_cb_create(__auth_info_recv_cb, false, ah);
230 rpc_port_AutofillAppPort_autofill_error_info_received_cb_h error_info_received_cb = rpc_port_AutofillAppPort_autofill_error_info_received_cb_create(__error_info_recv_cb, false, ah);
232 int r = rpc_port_proxy_AutofillAppPort_invoke_Register(h, ah->context_id, auth_info_received_cb, fill_response_received_cb, error_info_received_cb);
234 LOGW("[ERROR] Failed to invoke Register");
236 if (ah->connection_callback)
237 ah->connection_callback(ah, AUTOFILL_CONNECTION_STATUS_CONNECTED, ah->connection_userdata);
241 static void __on_disconnected(rpc_port_proxy_AutofillAppPort_h h, void *user_data)
243 LOGI("disconnected");
245 autofill_h ah = user_data;
247 ah->connected = false;
249 if (ah && ah->connection_callback)
250 ah->connection_callback(ah, AUTOFILL_CONNECTION_STATUS_DISCONNECTED, ah->connection_userdata);
256 static void __on_rejected(rpc_port_proxy_AutofillAppPort_h h, void *user_data)
260 autofill_h ah = user_data;
262 if (ah && ah->connection_callback)
263 ah->connection_callback(ah, AUTOFILL_CONNECTION_STATUS_REJECTED, ah->connection_userdata);
267 EXPORT_API int autofill_create(autofill_h *ah)
272 return AUTOFILL_ERROR_INVALID_PARAMETER;
274 rpc_port_proxy_AutofillAppPort_callback_s rpc_callback = {
275 .connected = __on_connected,
276 .disconnected = __on_disconnected,
277 .rejected = __on_rejected
280 struct autofill_s *as = (autofill_h)calloc(1, sizeof(struct autofill_s));
282 return AUTOFILL_ERROR_OUT_OF_MEMORY;
284 ret = rpc_port_proxy_AutofillAppPort_create(AUTOFILL_DAEMON_APP_ID, &rpc_callback, as, &as->rpc_h);
285 if (ret != RPC_PORT_ERROR_NONE) {
286 LOGW("[ERROR] Failed to create rpc port. err = %d", ret);
288 return AUTOFILL_ERROR_OUT_OF_MEMORY;
291 as->context_id = _autofill_context_count++;
294 return AUTOFILL_ERROR_NONE;
297 EXPORT_API int autofill_destroy(autofill_h ah)
300 return AUTOFILL_ERROR_INVALID_PARAMETER;
302 ah->connection_callback = NULL;
303 ah->autofill_fill_response_received_cb = NULL;
304 ah->autofill_auth_info_received_cb = NULL;
305 ah->autofill_error_info_received_cb = NULL;
308 rpc_port_proxy_AutofillAppPort_destroy(ah->rpc_h);
314 return AUTOFILL_ERROR_NONE;
317 EXPORT_API int autofill_connect(autofill_h ah, autofill_connection_status_changed_cb callback, void *user_data)
319 LOGD("autofill connect. handle : %p", ah);
323 if (!ah || !callback) {
324 LOGW("[ERROR] Invalid parameter");
325 return AUTOFILL_ERROR_INVALID_PARAMETER;
329 return AUTOFILL_ERROR_OPERATION_FAILED;
332 ah->connection_callback = callback;
333 ah->connection_userdata = user_data;
335 ret = rpc_port_proxy_AutofillAppPort_connect(ah->rpc_h);
336 if (ret != RPC_PORT_ERROR_NONE) {
338 case RPC_PORT_ERROR_IO_ERROR:
339 LOGW("[ERROR] Failed to connect rpc port. I/O Error");
341 case RPC_PORT_ERROR_OUT_OF_MEMORY:
342 LOGW("[ERROR] Failed to connect rpc port. Out of memory");
344 case RPC_PORT_ERROR_INVALID_PARAMETER:
345 LOGW("[ERROR] Failed to connect rpc port. Invalid parameter");
347 case RPC_PORT_ERROR_PERMISSION_DENIED:
348 LOGW("[ERROR] Failed to connect rpc port. Permission denied");
351 LOGW("[ERROR] Failed to connect rpc port. errcode = %d", ret);
354 return AUTOFILL_ERROR_OPERATION_FAILED;
357 return AUTOFILL_ERROR_NONE;
361 EXPORT_API int autofill_commit(autofill_h ah, autofill_save_view_info_h vi)
363 rpc_port_autofill_save_view_info_h vih;
367 autofill_hint_e autofill_hint;
371 LOGW("[ERROR] Invalid parameter");
372 return AUTOFILL_ERROR_INVALID_PARAMETER;
376 return AUTOFILL_ERROR_OPERATION_FAILED;
379 if (!ah->connected) {
380 LOGW("[ERROR] Not connected");
381 return AUTOFILL_ERROR_OPERATION_FAILED;
384 rpc_port_autofill_save_view_info_create(&vih);
385 rpc_port_autofill_save_view_info_set_view_id(vih, vi->view_id);
386 rpc_port_autofill_save_view_info_set_view_title(vih, vi->view_title);
389 autofill_save_item_h it;
390 EINA_LIST_FOREACH(vi->autofill_save_item_list, l, it)
392 autofill_save_item_get_id(it, &id);
393 autofill_save_item_get_label(it, &label);
394 autofill_save_item_get_value(it, &value);
395 autofill_save_item_get_autofill_hint(it, &autofill_hint);
396 autofill_save_item_get_sensitive_data(it, &sensitive_data);
398 LOGD("it : %p, id : %s, label : %s, hint : %d, sensitive : %d", it, id, label, autofill_hint, sensitive_data);
400 rpc_port_autofill_save_item_h aih;
401 rpc_port_autofill_save_item_create(&aih);
402 rpc_port_autofill_save_item_set_id(aih, id);
403 rpc_port_autofill_save_item_set_label(aih, label);
404 rpc_port_autofill_save_item_set_value(aih, value);
405 rpc_port_autofill_save_item_set_autofill_hint(aih, (int)autofill_hint);
406 rpc_port_autofill_save_item_set_is_sensitive_data(aih, sensitive_data);
407 rpc_port_autofill_save_view_info_add_items(vih, aih);
424 rpc_port_autofill_save_item_destroy(aih);
427 LOGD("app id : %s, view id : '%s', view title : '%s'", vi->app_id, vi->view_id, vi->view_title);
429 int ret = rpc_port_proxy_AutofillAppPort_invoke_commit(ah->rpc_h, ah->context_id, vih);
431 rpc_port_autofill_save_view_info_destroy(vih);
433 if (ret != RPC_PORT_ERROR_NONE) {
434 LOGW("[ERROR] Failed to commit. rpc err = %d", ret);
435 return AUTOFILL_ERROR_OPERATION_FAILED;
438 return AUTOFILL_ERROR_NONE;
442 EXPORT_API int autofill_error_info_set_received_cb(autofill_h ah, autofill_error_info_received_cb callback, void *user_data)
444 if (!ah || !callback) {
445 LOGW("[ERROR] Invalid parameter");
446 return AUTOFILL_ERROR_INVALID_PARAMETER;
449 ah->autofill_error_info_received_cb = callback;
450 ah->autofill_error_info_data = user_data;
452 return AUTOFILL_ERROR_NONE;
455 EXPORT_API int autofill_error_info_unset_received_cb(autofill_h ah)
458 LOGW("[ERROR] Invalid parameter");
459 return AUTOFILL_ERROR_INVALID_PARAMETER;
462 ah->autofill_error_info_received_cb = NULL;
463 ah->autofill_error_info_data = NULL;
465 return AUTOFILL_ERROR_NONE;