2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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.
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
23 #include <account-types.h>
24 #include <pkgmgr-info.h>
25 #include <app_manager.h>
27 #include "account-dlog.h"
29 /* Define EXPORT_API */
31 #define EXPORT_API __attribute__((visibility("default")))
35 static const xmlChar _NODE_ACCOUNT_PROVIDER[] = "account-provider";
36 static const xmlChar _NODE_ICON[] = "icon";
37 static const xmlChar _NODE_LABEL[] = "label";
38 static const xmlChar _NODE_CAPABILITY[] = "capability";
40 static const xmlChar _ATTRIBUTE_APP_ID[] = "appid";
41 static const xmlChar _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT[] = "multiple-accounts-support";
42 static const xmlChar _ATTRIBUTE_SECTION[] = "section";
43 static const xmlChar _ATTRIBUTE_TYPE[] = "type";
44 static const xmlChar _ATTRIBUTE_XML_LANG[] = "xml:lang";
46 static const xmlChar _VALUE_TRUE[] = "true";
47 static const xmlChar _VALUE_ACCOUNT[] = "account";
48 static const xmlChar _VALUE_ACCOUNT_SMALL[] = "account-small";
49 static const xmlChar _VALUE_XHIGH[] = "Xhigh";
51 static const char _DEFAULT_LOCALE[] = "default";
53 static char __old_account_provider_app_id[1024];
55 bool _on_account_received_cb(account_h account, void* user_data)
58 retvm_if((account == NULL) || (user_data == NULL), false, "A system error has occurred.");
60 char* account_provider_app_id = (char*)user_data;
61 retvm_if(account_provider_app_id == NULL, false, "account_provider_app_id is NULL.");
64 int account_db_id = 0;
65 int ret = account_get_account_id(account, &account_db_id);
66 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_get_account_id().", ret);
68 ret = account_set_package_name(account, account_provider_app_id);
69 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_set_package_name().", ret);
72 ret = account_update_to_db_by_id_ex(account, account_db_id);
73 retvm_if(ret == ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, false, "[%d] The application does not register the account provider.", ret);
74 retvm_if(ret == ACCOUNT_ERROR_PERMISSION_DENIED, false, "[%d] The application has no permission to update this account.", ret);
75 retvm_if(ret == ACCOUNT_ERROR_RECORD_NOT_FOUND, false, "[%d] The account does not exist.", ret);
76 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_update_to_db_by_id_ex().", ret);
81 int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
84 _D("Registering the Account Provider.");
86 int ret = account_connect();
87 retvm_if(ret != ACCOUNT_ERROR_NONE, ret, "[%d]A system error has occurred.", ret);
91 account_type_h account_type_handle = NULL;
92 ret = account_type_create(&account_type_handle);
93 if(ret != ACCOUNT_ERROR_NONE || account_type_handle == NULL) {
94 _E("[%d]Memory allocation failed.", ret);
99 xmlNodePtr cur_ptr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
100 if(cur_ptr == NULL) {
102 _E("Failed to get the element.");
106 _SECURE_D("Node: %s", cur_ptr->name);
108 // Get the children nodes
109 cur_ptr = cur_ptr->xmlChildrenNode;
110 if(cur_ptr == NULL) {
112 _E("Failed to get the child element.");
116 while(cur_ptr != NULL) {
117 _SECURE_D("Node: %s", cur_ptr->name);
119 // Node: <account-provider>
120 if((!xmlStrcmp(cur_ptr->name, _NODE_ACCOUNT_PROVIDER))) {
122 xmlChar* attribute_app_id = xmlGetProp(cur_ptr, _ATTRIBUTE_APP_ID);
123 if(attribute_app_id == NULL) {
125 _E("Failed to get the attribute.");
129 _SECURE_D("Attribute: appid - %s", attribute_app_id);
131 ret = account_type_set_app_id(account_type_handle, (char*)attribute_app_id);
132 if(ret != ACCOUNT_ERROR_NONE) {
133 _E("Failed to set the app ID.");
137 // Attribute: multiple-accounts-support
138 xmlChar* multiple_accounts_support = xmlGetProp(cur_ptr, _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT);
139 if(multiple_accounts_support == NULL) {
141 _E("Failed to get the attribute.");
145 _SECURE_D("Attribute: multiple-accounts-support - %s", multiple_accounts_support);
147 if((!xmlStrcmp(multiple_accounts_support, _VALUE_TRUE))) {
148 ret = account_type_set_multiple_account_support(account_type_handle, true);
149 if(ret != ACCOUNT_ERROR_NONE) {
150 _E("Failed to set the multiple accounts support.");
154 ret = account_type_set_multiple_account_support(account_type_handle, false);
155 if (ret != ACCOUNT_ERROR_NONE)
157 _E("Failed to set the multiple accounts support.");
162 // Get the children nodes
163 cur_ptr = cur_ptr->xmlChildrenNode;
164 if(cur_ptr == NULL) {
166 _E("Failed to get the child element.");
170 while(cur_ptr != NULL) {
171 _SECURE_D("Node: %s", cur_ptr->name);
174 if((!xmlStrcmp(cur_ptr->name, _NODE_ICON))) {
175 // Attribute: section
176 xmlChar* section = xmlGetProp(cur_ptr, _ATTRIBUTE_SECTION);
177 if(section == NULL) {
179 _E("Failed to get the attribute.");
183 _SECURE_D("Attribute: section - %s", section);
185 char *resource_path = NULL;
186 if((!xmlStrcmp(section, _VALUE_ACCOUNT))) {
187 xmlChar* account_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
188 if(account_icon == NULL) {
190 _E("Failed to get the value.");
194 _SECURE_D("Node: icon - %s", account_icon);
196 if (!strncmp(account_icon, "/usr/share/icons", 16)) {
197 ret = account_type_set_icon_path(account_type_handle, (char*)account_icon);
198 if(ret != ACCOUNT_ERROR_NONE) {
199 _E("Failed to set the icon path.");
203 if (!strcmp(attribute_app_id, "com.samsung.samsungaccount")) {
204 char *icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.samsungaccount/shared/res/", (char*)account_icon);
205 if(icon_path == NULL) {
206 _E("icon_path is NULL.");
211 _D("icon_path[%s]", icon_path);
212 ret = account_type_set_icon_path(account_type_handle, icon_path);
213 if(ret != ACCOUNT_ERROR_NONE) {
214 _E("Failed to set the icon path.");
219 } else if (!strcmp(attribute_app_id, "com.samsung.tizenaccount")) {
220 char *icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.tizenaccount/shared/res/", (char*)account_icon);
221 if(icon_path == NULL) {
222 _E("icon_path is NULL.");
227 _D("icon_path[%s]", icon_path);
228 ret = account_type_set_icon_path(account_type_handle, icon_path);
229 if(ret != ACCOUNT_ERROR_NONE) {
230 _E("Failed to set the icon path.");
236 ret = app_manager_get_shared_resource_path((char*)attribute_app_id, &resource_path);
237 if(ret != APP_MANAGER_ERROR_NONE) {
238 _E("Failed to get the shared resource path.");
242 char *icon_path = g_strdup_printf("%s%s", resource_path, (char*)account_icon);
243 if(icon_path == NULL) {
244 _E("icon_path is NULL.");
251 _D("icon_path[%s]", icon_path);
252 ret = account_type_set_icon_path(account_type_handle, icon_path);
253 if(ret != ACCOUNT_ERROR_NONE) {
254 _E("Failed to set the icon path.");
261 } else if((!xmlStrcmp(section, _VALUE_ACCOUNT_SMALL))) {
262 xmlChar* account_small_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
263 if(account_small_icon == NULL) {
265 _E("Failed to get the value.");
269 _SECURE_D("Node: icon (small) - %s", account_small_icon);
271 if (!strncmp(account_small_icon, "/usr/share/icons", 16) || !strcmp(account_small_icon, "/usr/apps/com.samsung.tizenaccount/shared/res/TizenAccount.png")) {
272 ret = account_type_set_small_icon_path(account_type_handle, (char*)account_small_icon);
273 if(ret != ACCOUNT_ERROR_NONE) {
274 _E("Failed to set the small icon path.");
278 if (!strcmp(attribute_app_id, "com.samsung.samsungaccount")) {
279 char *small_icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.samsungaccount/shared/res/", (char*)account_small_icon);
280 if(small_icon_path == NULL) {
281 _E("small_icon_path is NULL.");
286 _D("small_icon_path[%s]", small_icon_path);
287 ret = account_type_set_small_icon_path(account_type_handle, (char*)small_icon_path);
288 if(ret != ACCOUNT_ERROR_NONE) {
289 _E("Failed to set the small icon path.");
290 g_free(small_icon_path);
293 g_free(small_icon_path);
295 ret = app_manager_get_shared_resource_path((char*)attribute_app_id, &resource_path);
296 if(ret != APP_MANAGER_ERROR_NONE) {
297 _E("Failed to get the shared resource path.");
301 char *small_icon_path = g_strdup_printf("%s%s", resource_path, (char*)account_small_icon);
302 if(small_icon_path == NULL) {
303 _E("small_icon_path is NULL.");
310 _D("small_icon_path[%s]", small_icon_path);
311 ret = account_type_set_small_icon_path(account_type_handle, (char*)small_icon_path);
312 if(ret != ACCOUNT_ERROR_NONE) {
313 _E("Failed to set the small icon path.");
314 g_free(small_icon_path);
317 g_free(small_icon_path);
321 } else if((!xmlStrcmp(cur_ptr->name, _NODE_LABEL))) {
324 _SECURE_D("Node: %s", cur_ptr->name);
326 // Attribute: xml:lang
327 xmlChar* xml_lang = xmlNodeGetLang(cur_ptr);
328 if(xml_lang != NULL) {
329 _SECURE_D("Attribute: xml:lang - %s", xml_lang);
331 char* lang = (char*)xml_lang;
332 char* converted_lang = NULL;
334 gchar** tokens = g_strsplit(lang, "-", 2);
337 _E("Failed to get token.");
341 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
342 if(upper_token == NULL) {
345 _E("Failed to convert to upper case.");
349 converted_lang = g_strdup_printf("%s_%s", tokens[0], upper_token);
352 if(converted_lang == NULL) {
355 _E("Failed to convert to upper case.");
359 _SECURE_D("Attribute: converted lang - %s", converted_lang);
363 xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
364 if(xml_label == NULL) {
366 g_free(converted_lang);
367 _E("Failed to get the value.");
371 _SECURE_D("Node: label - %s", xml_label);
373 ret = account_type_set_label(account_type_handle, (char*)xml_label, converted_lang);
374 if(ret != ACCOUNT_ERROR_NONE) {
375 g_free(converted_lang);
376 _E("[%d]Failed to set the display name.", ret);
380 g_free(converted_lang);
382 xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
383 if(xml_label == NULL) {
385 _E("Failed to get the value.");
389 _SECURE_D("Node: label - %s", xml_label);
391 ret = account_type_set_label(account_type_handle, (char*)xml_label, _DEFAULT_LOCALE);
392 if(ret != ACCOUNT_ERROR_NONE) {
393 _E("[%d]Failed to set the display name.", ret);
397 } else if((!xmlStrcmp(cur_ptr->name, _NODE_CAPABILITY))) {
398 // Node: <capability>
400 _SECURE_D("Node: %s", cur_ptr->name);
402 xmlChar* xml_capability = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
403 if(xml_capability == NULL) {
405 _E("Failed to get the value.");
409 _SECURE_D("Node: capability - %s", xml_capability);
411 ret = account_type_set_provider_feature(account_type_handle, (char*)xml_capability);
412 if(ret != ACCOUNT_ERROR_NONE) {
413 _E("[%d]Failed to set the capability.", ret);
418 cur_ptr = cur_ptr->next;
424 cur_ptr = cur_ptr->next;
427 // Insert the account type to the account DB
429 int account_type_db_id = 0;
430 ret = account_type_insert_to_db(account_type_handle, &account_type_db_id);
431 if(ret != ACCOUNT_ERROR_NONE) {
432 _E("[%d]Failed to perform account_type_insert_to_db().", ret);
437 ret = account_type_destroy(account_type_handle);
438 if(ret != ACCOUNT_ERROR_NONE) {
439 _E("[%d]Failed to perform account_type_destroy().", ret);
443 ret = account_disconnect();
444 if(ret != ACCOUNT_ERROR_NONE) {
445 _E("[%d]Failed to perform account_disconnect().", ret);
452 ret2 = account_type_destroy(account_type_handle);
453 retvm_if(ret2 != ACCOUNT_ERROR_NONE, ret2, "[%d]Failed to perform account_type_destroy().", ret2);
455 ret2 = account_disconnect();
456 retvm_if(ret2 != ACCOUNT_ERROR_NONE, ret2, "[%d]Failed to perform account_disconnect().", ret2);
461 int _unregister_account_provider(pkgmgrinfo_appinfo_h package_info_handle, void* user_data)
464 _D("Unregistering the Account Provider.");
467 pkgmgrinfo_appinfo_get_appid(package_info_handle, &app_id);
468 _D("appid : %s", app_id);
470 int ret = account_connect();
471 retvm_if(ret != ACCOUNT_ERROR_NONE, ret, "[%d]Failed to account_connect().", ret);
475 ret = account_delete_from_db_by_package_name((char*)app_id);
476 if((ret != ACCOUNT_ERROR_NONE) && (ret != ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
477 _E("Failed to perform account_delete_from_db_by_package_name().");
481 ret = account_type_delete_by_app_id((char*)app_id);
482 if(ret != ACCOUNT_ERROR_NONE) {
483 _E("Failed to perform account_type_delete_by_app_id().");
487 ret = account_disconnect();
488 retvm_if(ret != ACCOUNT_ERROR_NONE, ret, "[%d]Failed to account_disconnect().", ret);
493 ret2 = account_disconnect();
494 retvm_if(ret2 != ACCOUNT_ERROR_NONE, ret, "[%d]Failed to account_disconnect().", ret2);
499 int _on_package_app_list_received_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
502 _D("Pkgmgr parser plugin pre upgrade.");
505 pkgmgrinfo_appinfo_get_appid(handle, &app_id);
506 _D("appid : %s", app_id);
508 int ret = account_connect();
509 retvm_if(ret != ACCOUNT_ERROR_NONE, ret, "[%d]Failed to account_connect().", ret);
511 ret = account_type_delete_by_app_id((char*)app_id);
512 if(ret == ACCOUNT_ERROR_NONE) {
513 _D("PKGMGR_PARSER_PLUGIN_PRE_UPGRADE: app ID - %s", app_id);
514 strncpy(__old_account_provider_app_id, app_id, 128);
519 ret = account_disconnect();
520 retvm_if(ret != ACCOUNT_ERROR_NONE, ret, "[%d]Failed to account_disconnect().", ret);
526 int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId)
529 _D("PKGMGR_PARSER_PLUGIN_INSTALL");
531 char* account_provider_app_id = NULL;
532 int ret = _register_account_provider(docPtr, account_provider_app_id);
533 retvm_if(ret != 0, -1, "Failed to register the account provider.");
539 int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char* packageId)
542 _D("PKGMGR_PARSER_PLUGIN_UNINSTALL");
544 pkgmgrinfo_pkginfo_h package_info_handle = NULL;
546 int ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &package_info_handle);
547 retvm_if(ret != PMINFO_R_OK, ret, "[%d]Failed to pkgmgrinfo_pkginfo_get_pkginfo().", ret);
549 ret = pkgmgrinfo_appinfo_get_list(package_info_handle, PMINFO_UI_APP, _unregister_account_provider, NULL);
550 if(ret != PMINFO_R_OK) {
551 _D("Failed to get the application information list.");
552 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
557 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
562 int PKGMGR_PARSER_PLUGIN_PRE_UPGRADE(const char* packageId)
565 _D("PKGMGR_PARSER_PLUGIN_PRE_UPGRADE");
567 memset(__old_account_provider_app_id, 0x00, sizeof(__old_account_provider_app_id));
569 pkgmgrinfo_pkginfo_h package_info_handle = NULL;
571 int ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &package_info_handle);
572 retvm_if(ret != PMINFO_R_OK, ret, "[%d]Failed to pkgmgrinfo_pkginfo_get_pkginfo().", ret);
574 ret = pkgmgrinfo_appinfo_get_list(package_info_handle, PMINFO_UI_APP, _on_package_app_list_received_cb, NULL);
575 if(ret != PMINFO_R_OK) {
576 _D("Failed to get the application information list.");
577 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
582 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
588 int PKGMGR_PARSER_PLUGIN_UPGRADE(xmlDocPtr docPtr, const char* packageId)
591 _D("PKGMGR_PARSER_PLUGIN_UPGRADE");
593 char* account_provider_app_id = NULL;
594 int ret = _register_account_provider(docPtr, account_provider_app_id);
595 retvm_if(ret != 0, ret, "[%d]Failed to register the account provider.", ret);
597 ret = account_connect();
598 retvm_if(ret != ACCOUNT_ERROR_NONE, ret, "[%d]Failed to perfrom account_connect().", ret);
602 ret = account_query_account_by_package_name(_on_account_received_cb, __old_account_provider_app_id, (void*)account_provider_app_id);
603 if((ret != ACCOUNT_ERROR_NONE) && (ret != ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
604 _E("Failed to perform account_query_account_by_package_name().");
608 ret = account_disconnect();
609 retvm_if(ret != ACCOUNT_ERROR_NONE, ret, "[%d]Failed to perfrom account_disconnect().", ret);
614 ret2 = account_disconnect();
615 retvm_if(ret2 != ACCOUNT_ERROR_NONE, ret2, "[%d]Failed to perfrom account_disconnect().", ret2);