Check autofill service app id is validity
[platform/core/uifw/autofill-daemon.git] / src / autofill-daemon.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <tizen.h>
18 #include <stdlib.h>
19 #include <service_app.h>
20 #include <dlog.h>
21 #include <glib.h>
22 #include <rpc-port.h>
23 #include <app_manager.h>
24
25 #include "autofill_daemon_dlog.h"
26 #include "autofill_stub.h"
27 #include "autofill_service_proxy.h"
28 #include "autofill_manager_stub.h"
29 #include "autofill_config.h"
30
31 static rpc_port_proxy_AutofillSvcPort_h svc_rpc_h = NULL;
32
33 static int connect_service();
34
35 typedef struct {
36     char *app_id;
37     int context_id;
38
39     rpc_port_AutofillAppPort_autofill_auth_info_received_cb_h auth_info_cb;
40     rpc_port_AutofillAppPort_autofill_fill_response_received_cb_h fill_response_received_cb;
41 } autofill_client_s;
42
43 static GList *__client_list = NULL;
44
45 static autofill_client_s *
46 get_autofill_client(const char *app_id, int context_id)
47 {
48     GList *iter;
49     autofill_client_s *client;
50
51     iter = __client_list;
52     while (iter) {
53         client = iter->data;
54
55         iter = g_list_next(iter);
56
57         if (!client) {
58             LOGW("Warning: value is NULL");
59             continue;
60         }
61
62         if ((client->context_id == context_id) &&
63             client->app_id && strcmp(client->app_id, app_id) == 0) {
64             return client;
65         }
66     }
67
68     return NULL;
69 }
70
71 static autofill_client_s *__create_client(const char *app_id, int context_id,
72         rpc_port_AutofillAppPort_autofill_auth_info_received_cb_h auth_info_cb,
73         rpc_port_AutofillAppPort_autofill_fill_response_received_cb_h fill_response_received_cb)
74 {
75     LOGD("");
76     autofill_client_s *handle;
77
78     handle = calloc(1, sizeof(autofill_client_s));
79     if (!handle) {
80         LOGE("Out of memory");
81         return NULL;
82     }
83
84     handle->app_id = strdup(app_id);
85     if (!handle->app_id) {
86         LOGE("Out of memory");
87         free(handle);
88         return NULL;
89     }
90
91     handle->context_id = context_id;
92
93     rpc_port_AutofillAppPort_autofill_auth_info_received_cb_clone(auth_info_cb, &handle->auth_info_cb);
94     if (!handle->auth_info_cb) {
95         LOGE("Out of memory");
96         free(handle->app_id);
97         free(handle);
98         return NULL;
99     }
100
101     rpc_port_AutofillAppPort_autofill_fill_response_received_cb_clone(fill_response_received_cb, &handle->fill_response_received_cb);
102
103     if (!handle->fill_response_received_cb) {
104         LOGE("Out of memory");
105         free(handle->app_id);
106         rpc_port_AutofillAppPort_autofill_auth_info_received_cb_destroy(handle->auth_info_cb);
107         free(handle);
108         return NULL;
109     }
110
111     return handle;
112 }
113
114 static void __destroy_client(gpointer data)
115 {
116     LOGD("");
117     autofill_client_s *handle = data;
118
119     if (!handle)
120         return;
121
122     if (handle->auth_info_cb) {
123         rpc_port_AutofillAppPort_autofill_auth_info_received_cb_destroy(handle->auth_info_cb);
124         handle->auth_info_cb = NULL;
125     }
126
127     if (handle->fill_response_received_cb) {
128         rpc_port_AutofillAppPort_autofill_fill_response_received_cb_destroy(handle->fill_response_received_cb);
129         handle->fill_response_received_cb = NULL;
130     }
131
132     if (handle->app_id) {
133         free(handle->app_id);
134         handle->app_id = NULL;
135     }
136
137     free(handle);
138 }
139
140 static void __remove_client(rpc_port_stub_AutofillAppPort_context_h context)
141 {
142     autofill_client_s *client = NULL;
143     rpc_port_stub_AutofillAppPort_context_get_tag(context, (void *)&client);
144     if (!client)
145         return;
146
147     rpc_port_stub_AutofillAppPort_context_set_tag(context, NULL);
148
149     LOGI("name(%s)", client->app_id);
150
151     __client_list = g_list_remove(__client_list, client);
152     __destroy_client(client);
153 }
154
155 static void __message_create(rpc_port_stub_AutofillAppPort_context_h context,
156         void *user_data)
157 {
158     LOGD("");
159     char *sender = NULL;
160
161     rpc_port_stub_AutofillAppPort_context_get_sender(context, &sender);
162     if (!sender)
163         return;
164
165     LOGD("sender(%s)", sender);
166     free(sender);
167 }
168
169 static void __message_terminate(rpc_port_stub_AutofillAppPort_context_h context,
170         void *user_data)
171 {
172     LOGD("");
173     char *sender = NULL;
174
175     rpc_port_stub_AutofillAppPort_context_get_sender(context, &sender);
176     if (!sender)
177         return;
178
179     LOGD("[__RPC_PORT__] sender(%s)", sender);
180     free(sender);
181
182     __remove_client(context);
183 }
184
185 static int __message_register(rpc_port_stub_AutofillAppPort_context_h context, int context_id, rpc_port_AutofillAppPort_autofill_auth_info_received_cb_h auth_info_cb, rpc_port_AutofillAppPort_autofill_fill_response_received_cb_h fill_response_received_cb, void *user_data)
186 {
187     LOGD("");
188     char *sender = NULL;
189     autofill_client_s *client = NULL;
190
191     rpc_port_stub_AutofillAppPort_context_get_sender(context, &sender);
192     if (!sender)
193         return -1;
194
195     LOGD("sender(%s)", sender);
196
197     client = __create_client(sender, context_id, auth_info_cb, fill_response_received_cb);
198     free(sender);
199
200     if (!client)
201         return -1;
202
203     __client_list = g_list_append(__client_list, client);
204
205     rpc_port_stub_AutofillAppPort_context_set_tag(context, client);
206
207     return 0;
208 }
209
210 static void __message_unregister(rpc_port_stub_AutofillAppPort_context_h context, int context_id, void *user_data)
211 {
212     __remove_client(context);
213 }
214
215 static void __manager_create(rpc_port_stub_AutofillManagerPort_context_h context,
216         void *user_data)
217 {
218     LOGD("");
219 }
220
221 static void __manager_terminate(rpc_port_stub_AutofillManagerPort_context_h context,
222         void *user_data)
223 {
224     LOGD("");
225 }
226
227 bool __view_info_item_cb(rpc_port_autofill_item_h items, void *user_data)
228 {
229     char *id = NULL;
230     char *label = NULL;
231     char *value = NULL;
232     int autofill_hint;
233     bool sensitive_data;
234
235     rpc_port_autofill_svc_view_info_h svi = (rpc_port_autofill_svc_view_info_h)user_data;
236
237     rpc_port_autofill_svc_item_h svc_item;
238
239     rpc_port_autofill_svc_item_create(&svc_item);
240
241     rpc_port_autofill_item_get_id(items, &id);
242     rpc_port_autofill_svc_item_set_id(svc_item, id);
243     if (id) {
244         free(id);
245     }
246
247     rpc_port_autofill_item_get_label(items, &label);
248     rpc_port_autofill_svc_item_set_label(svc_item, label);
249     if (label) {
250         free(label);
251     }
252
253     rpc_port_autofill_item_get_value(items, &value);
254     rpc_port_autofill_svc_item_set_value(svc_item, value);
255     if (value) {
256         free(value);
257     }
258
259     rpc_port_autofill_item_get_autofill_hint(items, &autofill_hint);
260     rpc_port_autofill_svc_item_set_autofill_hint(svc_item, autofill_hint);
261
262     rpc_port_autofill_item_get_is_sensitive_data(items, &sensitive_data);
263     rpc_port_autofill_svc_item_set_is_sensitive_data(svc_item, sensitive_data);
264
265     rpc_port_autofill_svc_view_info_add_items(svi, svc_item);
266
267     rpc_port_autofill_svc_item_destroy(svc_item);
268
269     return true;
270 }
271
272 bool __save_item_cb(rpc_port_autofill_save_item_h items, void *user_data)
273 {
274     char *id = NULL;
275     char *label = NULL;
276     char *value = NULL;
277     int autofill_hint;
278     bool sensitive_data;
279
280     rpc_port_autofill_svc_save_view_info_h svi = (rpc_port_autofill_svc_save_view_info_h)user_data;
281
282     rpc_port_autofill_svc_save_item_h svc_save_item;
283
284     rpc_port_autofill_svc_save_item_create(&svc_save_item);
285
286     rpc_port_autofill_save_item_get_id(items, &id);
287     rpc_port_autofill_svc_save_item_set_id(svc_save_item, id);
288     if (id) {
289         free(id);
290     }
291
292     rpc_port_autofill_save_item_get_label(items, &label);
293     rpc_port_autofill_svc_save_item_set_label(svc_save_item, label);
294     if (label) {
295         free(label);
296     }
297
298     rpc_port_autofill_save_item_get_value(items, &value);
299     rpc_port_autofill_svc_save_item_set_value(svc_save_item, value);
300     if (value) {
301         free(value);
302     }
303
304     rpc_port_autofill_save_item_get_autofill_hint(items, &autofill_hint);
305     rpc_port_autofill_svc_save_item_set_autofill_hint(svc_save_item, autofill_hint);
306
307     rpc_port_autofill_save_item_get_is_sensitive_data(items, &sensitive_data);
308     rpc_port_autofill_svc_save_item_set_is_sensitive_data(svc_save_item, sensitive_data);
309
310     rpc_port_autofill_svc_save_view_info_add_items(svi, svc_save_item);
311
312     rpc_port_autofill_svc_save_item_destroy(svc_save_item);
313
314     return true;
315 }
316
317 static int __auth_info_request_cb(rpc_port_stub_AutofillAppPort_context_h context, int context_id, rpc_port_autofill_view_info_h vi, void *user_data)
318 {
319     char *sender = NULL;
320     autofill_client_s *sender_client;
321
322     if (!svc_rpc_h) {
323         LOGW("Not initialized");
324         return -1;
325     }
326
327     rpc_port_stub_AutofillAppPort_context_get_sender(context, &sender);
328     char *view_id = NULL;
329
330     rpc_port_autofill_view_info_get_view_id(vi, &view_id);
331     LOGD("app id : %s, view id : %s", sender, view_id);
332
333     rpc_port_stub_AutofillAppPort_context_get_tag(context, (void *)&sender_client);
334
335     /* create view info */
336     rpc_port_autofill_svc_view_info_h svi;
337     rpc_port_autofill_svc_view_info_create(&svi);
338     rpc_port_autofill_svc_view_info_set_app_id(svi, sender);
339     rpc_port_autofill_svc_view_info_set_view_id(svi, view_id);
340
341     rpc_port_autofill_view_info_foreach_items(vi, __view_info_item_cb, svi);
342
343     rpc_port_proxy_AutofillSvcPort_invoke_request_auth_info(svc_rpc_h, context_id, svi);
344
345     if (sender) {
346         free(sender);
347     }
348
349     if (view_id) {
350         free(view_id);
351     }
352
353     rpc_port_autofill_svc_view_info_destroy(svi);
354
355     return 0;
356 }
357
358 static int __autofill_fill_request_cb(rpc_port_stub_AutofillAppPort_context_h context, int context_id, rpc_port_autofill_view_info_h vi, void *user_data)
359 {
360     char *sender = NULL;
361     char *view_id = NULL;
362
363     if (!svc_rpc_h) {
364         LOGW("Not initialized");
365         return -1;
366     }
367
368     rpc_port_stub_AutofillAppPort_context_get_sender(context, &sender);
369     rpc_port_autofill_view_info_get_view_id(vi, &view_id);
370
371     LOGD("app id : %s, view id : %s, context id : %d", sender, view_id, context_id);
372
373     rpc_port_autofill_svc_view_info_h svi;
374     rpc_port_autofill_svc_view_info_create(&svi);
375     rpc_port_autofill_svc_view_info_set_app_id(svi, sender);
376     rpc_port_autofill_svc_view_info_set_view_id(svi, view_id);
377
378     rpc_port_autofill_view_info_foreach_items(vi, __view_info_item_cb, svi);
379
380     rpc_port_proxy_AutofillSvcPort_invoke_send_fill_request(svc_rpc_h, context_id, svi);
381
382     if (sender)
383         free(sender);
384
385     if (view_id)
386         free(view_id);
387
388     rpc_port_autofill_svc_view_info_destroy(svi);
389
390     return 0;
391 }
392
393 static int __commit_cb(rpc_port_stub_AutofillAppPort_context_h context, int context_id, rpc_port_autofill_save_view_info_h vi, void *user_data)
394 {
395     char *sender = NULL;
396     autofill_client_s *sender_client;
397
398     if (!svc_rpc_h) {
399         LOGW("Not initialized");
400         return -1;
401     }
402
403     rpc_port_stub_AutofillAppPort_context_get_sender(context, &sender);
404     if (sender) {
405         LOGD("sender(%s)", sender);
406         free(sender);
407     }
408
409     char *view_id = NULL;
410     rpc_port_autofill_save_view_info_get_view_id(vi, &view_id);
411     if (view_id) {
412         LOGD("view id : %s", view_id);
413     }
414
415     rpc_port_autofill_svc_save_view_info_h svi;
416     rpc_port_autofill_svc_save_view_info_create(&svi);
417     rpc_port_autofill_svc_save_view_info_set_view_id(svi, view_id);
418
419     rpc_port_stub_AutofillAppPort_context_get_tag(context, (void *)&sender_client);
420     rpc_port_autofill_save_view_info_foreach_items(vi, __save_item_cb, svi);
421
422     rpc_port_proxy_AutofillSvcPort_invoke_commit(svc_rpc_h, context_id, svi);
423
424     if (view_id)
425         free(view_id);
426
427     rpc_port_autofill_svc_save_view_info_destroy(svi);
428
429     return 0;
430 }
431
432 bool fill_response_item_cb(rpc_port_autofill_svc_response_item_h response_items, void *user_data)
433 {
434     rpc_port_autofill_response_group_h res_group = (rpc_port_autofill_response_group_h)user_data;
435
436     char *id = NULL;
437     char *presentation_text = NULL;
438     char *value = NULL;
439
440     rpc_port_autofill_response_item_h res_item;
441
442     rpc_port_autofill_response_item_create(&res_item);
443
444     rpc_port_autofill_svc_response_item_get_id(response_items, &id);
445     rpc_port_autofill_response_item_set_id(res_item, id);
446
447     rpc_port_autofill_svc_response_item_get_presentation_text(response_items, &presentation_text);
448     rpc_port_autofill_response_item_set_presentation_text(res_item, presentation_text);
449
450     rpc_port_autofill_svc_response_item_get_value(response_items, &value);
451     rpc_port_autofill_response_item_set_value(res_item, value);
452
453     rpc_port_autofill_response_group_add_response_items(res_group, res_item);
454
455     if (id)
456         free(id);
457
458     if (presentation_text)
459         free(presentation_text);
460
461     if (value)
462         free(value);
463
464     rpc_port_autofill_response_item_destroy(res_item);
465
466     return true;
467 }
468
469 bool fill_response_group_cb(rpc_port_autofill_svc_response_group_h response_groups, void *user_data)
470 {
471     rpc_port_autofill_fill_response_h fr_h = (rpc_port_autofill_fill_response_h)user_data;
472
473     rpc_port_autofill_response_group_h res_group;
474     rpc_port_autofill_response_group_create(&res_group);
475
476     rpc_port_autofill_svc_response_group_foreach_response_items(response_groups, fill_response_item_cb, res_group);
477
478     rpc_port_autofill_fill_response_add_response_groups(fr_h, res_group);
479
480     rpc_port_autofill_response_group_destroy(res_group);
481
482     return true;
483 }
484
485 static void __fill_response_recv_cb(void *user_data, int context_id, rpc_port_autofill_svc_fill_response_h response_h)
486 {
487     // recv fill response from service
488     char *view_id = NULL;
489     char *app_id = NULL;
490
491     rpc_port_autofill_svc_fill_response_get_app_id(response_h, &app_id);
492     rpc_port_autofill_svc_fill_response_get_view_id(response_h, &view_id);
493
494     /* create autofill response */
495     rpc_port_autofill_fill_response_h fill_response_h;
496     rpc_port_autofill_fill_response_create(&fill_response_h);
497     rpc_port_autofill_fill_response_set_view_id(fill_response_h, view_id);
498
499     rpc_port_autofill_svc_fill_response_foreach_response_groups(response_h, fill_response_group_cb, fill_response_h);
500
501     autofill_client_s *sender_client = get_autofill_client(app_id, context_id);
502     if (sender_client)
503         rpc_port_AutofillAppPort_autofill_fill_response_received_cb_invoke(sender_client->fill_response_received_cb, fill_response_h);
504
505     rpc_port_autofill_fill_response_destroy(fill_response_h);
506
507     if (app_id)
508         free(app_id);
509
510     if (view_id)
511         free(view_id);
512 }
513
514 static void __auth_info_recv_cb(void *user_data, int context_id, rpc_port_autofill_svc_auth_info_h svc_auth_info_h)
515 {
516     bool exist_autofill_data;
517     bool need_authentication;
518     char *service_name = NULL;
519     char *service_logo_image_path = NULL;
520     char *service_message = NULL;
521     char *app_id = NULL;
522     char *view_id = NULL;
523
524     rpc_port_autofill_svc_auth_info_get_app_id(svc_auth_info_h, &app_id);
525     rpc_port_autofill_svc_auth_info_get_view_id(svc_auth_info_h, &view_id);
526     rpc_port_autofill_svc_auth_info_get_exist_autofill_data(svc_auth_info_h, &exist_autofill_data);
527     rpc_port_autofill_svc_auth_info_get_need_authentication(svc_auth_info_h, &need_authentication);
528     rpc_port_autofill_svc_auth_info_get_service_name(svc_auth_info_h, &service_name);
529     rpc_port_autofill_svc_auth_info_get_service_logo_image_path(svc_auth_info_h, &service_logo_image_path);
530     rpc_port_autofill_svc_auth_info_get_service_message(svc_auth_info_h, &service_message);
531
532     LOGD("app id : %s, service name : %s, message : %s, logo path : %s", app_id, service_name, service_message, service_logo_image_path);
533
534     /* transfer auth info */
535     rpc_port_autofill_auth_info_h auth_info_h;
536     rpc_port_autofill_auth_info_create(&auth_info_h);
537     rpc_port_autofill_auth_info_set_view_id(auth_info_h, view_id);
538     rpc_port_autofill_auth_info_set_exist_autofill_data(auth_info_h, exist_autofill_data);
539     rpc_port_autofill_auth_info_set_need_authentication(auth_info_h, need_authentication);
540     rpc_port_autofill_auth_info_set_service_name(auth_info_h, service_name);
541     rpc_port_autofill_auth_info_set_service_message(auth_info_h, service_message);
542     rpc_port_autofill_auth_info_set_service_logo_image_path(auth_info_h, service_logo_image_path);
543
544     autofill_client_s *sender_client = get_autofill_client(app_id, context_id);
545     if (sender_client)
546         rpc_port_AutofillAppPort_autofill_auth_info_received_cb_invoke(sender_client->auth_info_cb, auth_info_h);
547
548     rpc_port_autofill_auth_info_destroy(auth_info_h);
549
550     if (app_id)
551         free(app_id);
552
553     if (view_id)
554         free(view_id);
555
556     if (service_name)
557         free(service_name);
558
559     if (service_logo_image_path)
560         free(service_logo_image_path);
561
562     if (service_message)
563         free(service_message);
564 }
565
566 static void __on_connected(rpc_port_proxy_AutofillSvcPort_h h, void *user_data)
567 {
568     LOGI("[__RPC_PORT__] connected");
569
570     rpc_port_AutofillSvcPort_autofill_svc_fill_response_cb_h fill_response_received_cb_h = rpc_port_AutofillSvcPort_autofill_svc_fill_response_cb_create(__fill_response_recv_cb, false, NULL);
571     rpc_port_AutofillSvcPort_autofill_svc_auth_info_cb_h auth_info_cb_h = rpc_port_AutofillSvcPort_autofill_svc_auth_info_cb_create(__auth_info_recv_cb, false, NULL);
572
573     int r = rpc_port_proxy_AutofillSvcPort_invoke_Register(h, auth_info_cb_h, fill_response_received_cb_h);
574     if (r != 0)
575         LOGD("Failed to invoke Register");
576 }
577
578 //LCOV_EXCL_START
579 static void __on_disconnected(rpc_port_proxy_AutofillSvcPort_h h, void *user_data)
580 {
581     LOGD("disconnected");
582
583     svc_rpc_h = NULL;
584 }
585
586 static void __on_rejected(rpc_port_proxy_AutofillSvcPort_h h, void *user_data)
587 {
588     LOGD("rejected");
589 }
590 //LCOV_EXCL_STOP
591
592 static bool __manager_set_autofill_service_cb(rpc_port_stub_AutofillManagerPort_context_h context, const char *app_id, void *user_data)
593 {
594     LOGD("app id : %s", app_id);
595
596     if (app_id)
597         autofill_config_set_autofill_service_app_id(app_id);
598
599     if (svc_rpc_h) {
600         LOGD("send terminate");
601         // terminate service
602         rpc_port_proxy_AutofillSvcPort_invoke_request_terminate(svc_rpc_h);
603
604         int ret = rpc_port_proxy_AutofillSvcPort_destroy(svc_rpc_h);
605         LOGD("ret : %d", ret);
606     }
607
608     svc_rpc_h = NULL;
609
610     connect_service();
611
612     return true;
613 }
614
615 static char * __manager_get_autofill_service_cb(rpc_port_stub_AutofillManagerPort_context_h context, void *user_data)
616 {
617     if (!svc_rpc_h) {
618         LOGW("Not initialized");
619         return false;
620     }
621
622     char *app_id;
623     autofill_config_get_autofill_service_app_id(&app_id);
624
625     LOGD("app id : %s", app_id);
626
627     return app_id;
628 }
629
630 bool add_autofill_service_cb(app_info_h app_info, void *user_data)
631 {
632     char *app_id = NULL;
633     rpc_port_list_string_h service_info_list = (rpc_port_list_string_h)user_data;
634
635     int ret = app_info_get_app_id(app_info, &app_id);
636     if (ret != APP_MANAGER_ERROR_NONE) {
637         LOGW("app_info_get_app_id failed (%d)", ret);
638         return true;
639     }
640
641     LOGD("Find autofill service : %s", app_id);
642
643     rpc_port_list_string_add_list_strings(service_info_list, app_id);
644
645     if (app_id) {
646         free(app_id);
647     }
648
649     return true;
650 }
651
652 static bool __manager_get_autofill_service_list_cb(rpc_port_stub_AutofillManagerPort_context_h context, rpc_port_list_string_h *service_info_list, void *user_data)
653 {
654     int ret;
655     app_info_metadata_filter_h handle = NULL;
656
657     // Get the Autofill service list
658     ret = app_info_metadata_filter_create(&handle);
659     if (ret != APP_MANAGER_ERROR_NONE) {
660         LOGW("app_info_metadata_filter_create failed (%d)", ret);
661         app_info_metadata_filter_destroy(handle);
662         return false;
663     }
664
665     ret = app_info_metadata_filter_add(handle, "autofill-service", "true");
666     if (ret != APP_MANAGER_ERROR_NONE) {
667         LOGW("app_info_metadata_filter_add failed (%d)", ret);
668         app_info_metadata_filter_destroy(handle);
669         return false;
670     }
671
672     rpc_port_list_string_h app_id_list_h;
673     rpc_port_list_string_create(&app_id_list_h);
674
675     ret = app_info_metadata_filter_foreach(handle, add_autofill_service_cb, app_id_list_h);
676     if (ret != APP_MANAGER_ERROR_NONE) {
677         LOGW("app_info_metadata_filter_foreach failed (%d)", ret);
678     }
679
680     *service_info_list = app_id_list_h;
681
682     app_info_metadata_filter_destroy(handle);
683
684     return true;
685 }
686
687 static int connect_service()
688 {
689     int ret;
690     size_t service_id_len;
691
692     rpc_port_proxy_AutofillSvcPort_callback_s rpc_callback = {
693         .connected = __on_connected,
694         .disconnected = __on_disconnected,
695         .rejected = __on_rejected
696     };
697
698     if (svc_rpc_h) {
699         LOGI("already connected\n");
700         return RPC_PORT_ERROR_NONE;
701     }
702
703     char *active_autofill_service_id = NULL;
704     autofill_config_get_autofill_service_app_id(&active_autofill_service_id);
705     LOGD("autofill service : '%s'", active_autofill_service_id);
706
707     if (!active_autofill_service_id) {
708         active_autofill_service_id = strdup(AUTOFILL_SERVICE_APP_ID);
709     }
710
711     if (active_autofill_service_id) {
712         autofill_config_set_autofill_service_app_id(active_autofill_service_id);
713         service_id_len = strlen(active_autofill_service_id);
714
715         if (service_id_len > 0) {
716             ret = rpc_port_proxy_AutofillSvcPort_create(active_autofill_service_id, &rpc_callback, NULL, &svc_rpc_h);
717         }
718         free(active_autofill_service_id);
719
720         if (service_id_len == 0) {
721             LOGD("No Autofill service to connect");
722             return false;
723         }
724
725         if (ret != RPC_PORT_ERROR_NONE) {
726             LOGW("Failed to create rpc port. err = %d", ret);
727             return false;
728         }
729     }
730
731     ret = rpc_port_proxy_AutofillSvcPort_connect(svc_rpc_h);
732     if (ret != RPC_PORT_ERROR_NONE) {
733         LOGW("Failed to connect. err = %d", ret);
734         return false;
735     }
736
737     return ret;
738 }
739
740 bool service_app_create(void *data)
741 {
742     // Todo: add your code here.
743     LOGD("");
744
745     int ret;
746     // register app port
747     rpc_port_stub_AutofillAppPort_callback_s callback = {
748         __message_create,
749         __message_terminate,
750         __message_register,
751         __message_unregister,
752         __auth_info_request_cb,
753         __autofill_fill_request_cb,
754         __commit_cb,
755     };
756
757     ret = rpc_port_stub_AutofillAppPort_register(&callback, NULL);
758     if (ret != 0)
759         LOGI("Failed to register app port");
760     else
761         LOGI("Succeeded to register app port");
762
763     // register manager port
764     rpc_port_stub_AutofillManagerPort_callback_s manager_callback = {
765         __manager_create,
766         __manager_terminate,
767         __manager_set_autofill_service_cb,
768         __manager_get_autofill_service_cb,
769         __manager_get_autofill_service_list_cb,
770     };
771
772     ret = rpc_port_stub_AutofillManagerPort_register(&manager_callback, NULL);
773     if (ret != 0)
774         LOGI("Failed to register manager port");
775     else
776         LOGI("Succeeded to register manager port");
777
778     connect_service();
779
780     return true;
781 }
782
783 void service_app_terminate(void *data)
784 {
785     // Todo: add your code here.
786     LOGD("");
787
788     if (__client_list) {
789         g_list_free_full(__client_list, __destroy_client);
790         __client_list = NULL;
791     }
792
793     rpc_port_stub_AutofillAppPort_unregister();
794
795     return;
796 }
797
798 void service_app_control(app_control_h app_control, void *data)
799 {
800     // Todo: add your code here.
801     return;
802 }
803
804 static void
805 service_app_lang_changed(app_event_info_h event_info, void *user_data)
806 {
807     /*APP_EVENT_LANGUAGE_CHANGED*/
808     return;
809 }
810
811 static void
812 service_app_region_changed(app_event_info_h event_info, void *user_data)
813 {
814     /*APP_EVENT_REGION_FORMAT_CHANGED*/
815 }
816
817 static void
818 service_app_low_battery(app_event_info_h event_info, void *user_data)
819 {
820     /*APP_EVENT_LOW_BATTERY*/
821 }
822
823 static void
824 service_app_low_memory(app_event_info_h event_info, void *user_data)
825 {
826     /*APP_EVENT_LOW_MEMORY*/
827 }
828
829 /**
830  * Entry point for this application.
831  */
832 int main(int argc, char *argv[])
833 {
834     LOGI("BEGIN");
835
836     char ad[50] = {0,};
837     service_app_lifecycle_callback_s event_callback;
838     app_event_handler_h handlers[5] = {NULL, };
839
840     event_callback.create = service_app_create;
841     event_callback.terminate = service_app_terminate;
842     event_callback.app_control = service_app_control;
843
844     service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
845     service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
846     service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
847     service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
848
849     return service_app_main(argc, argv, &event_callback, ad);
850 }