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 <account_internal.h>
25 #include <pkgmgr-info.h>
26 #include <app_manager.h>
27 #include <tzplatform_config.h>
29 #include "account-dlog.h"
31 /* Define EXPORT_API */
33 #define EXPORT_API __attribute__((visibility("default")))
37 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
39 static const xmlChar _NODE_ACCOUNT_PROVIDER[] = "account-provider";
40 static const xmlChar _NODE_ICON[] = "icon";
41 static const xmlChar _NODE_LABEL[] = "label";
42 static const xmlChar _NODE_CAPABILITY[] = "capability";
44 static const xmlChar _ATTRIBUTE_APP_ID[] = "appid";
45 static const xmlChar _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT[] = "multiple-accounts-support";
46 static const xmlChar _ATTRIBUTE_SERVICE_PROVIDER_ID[] = "providerid";
47 static const xmlChar _ATTRIBUTE_SECTION[] = "section";
48 static const xmlChar _ATTRIBUTE_TYPE[] = "type";
49 static const xmlChar _ATTRIBUTE_XML_LANG[] = "xml:lang";
51 static const xmlChar _VALUE_TRUE[] = "true";
52 static const xmlChar _VALUE_ACCOUNT[] = "account";
53 static const xmlChar _VALUE_ACCOUNT_SMALL[] = "account-small";
54 static const xmlChar _VALUE_XHIGH[] = "Xhigh";
56 static const char _DEFAULT_LOCALE[] = "default";
58 static char __old_account_provider_app_id[1024];
60 bool _on_account_received_cb(account_h account, void* user_data)
63 retvm_if((account == NULL) || (user_data == NULL), false, "A system error has occurred.");
65 char* account_provider_app_id = (char*)user_data;
66 retvm_if(account_provider_app_id == NULL, false, "account_provider_app_id is NULL.");
69 int account_db_id = 0;
70 int ret = account_get_account_id(account, &account_db_id);
71 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_get_account_id().", ret);
73 ret = account_set_package_name(account, account_provider_app_id);
74 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_set_package_name().", ret);
77 ret = account_update_to_db_by_id_without_permission(account, account_db_id);
78 retvm_if(ret == ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, false, "[%d] The application does not register the account provider.", ret);
79 retvm_if(ret == ACCOUNT_ERROR_PERMISSION_DENIED, false, "[%d] The application has no permission to update this account.", ret);
80 retvm_if(ret == ACCOUNT_ERROR_RECORD_NOT_FOUND, false, "[%d] The account does not exist.", ret);
81 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_update_to_db_by_id_without_permssion().", ret);
86 int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
89 _D("Registering the Account Provider.");
91 int ret = ACCOUNT_ERROR_NONE;
93 int ret2 = ACCOUNT_ERROR_NONE;
95 account_type_h account_type_handle = NULL;
96 ret = account_type_create(&account_type_handle);
97 if(ret != ACCOUNT_ERROR_NONE || account_type_handle == NULL) {
98 _E("[%d]Memory allocation failed.", ret);
103 xmlNodePtr cur_ptr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
104 if(cur_ptr == NULL) {
106 _E("Failed to get the element.");
110 _SECURE_D("Node: %s", cur_ptr->name);
112 // Get the children nodes
113 cur_ptr = cur_ptr->xmlChildrenNode;
114 if(cur_ptr == NULL) {
116 _E("Failed to get the child element.");
120 while(cur_ptr != NULL) {
121 _SECURE_D("Node: %s", cur_ptr->name);
123 // Node: <account-provider>
124 if((!xmlStrcmp(cur_ptr->name, _NODE_ACCOUNT_PROVIDER))) {
126 xmlChar* attribute_app_id = xmlGetProp(cur_ptr, _ATTRIBUTE_APP_ID);
127 if(attribute_app_id == NULL) {
129 _E("Failed to get the attribute.");
133 _SECURE_D("Attribute: appid - %s", attribute_app_id);
135 ret = account_type_set_app_id(account_type_handle, (char*)attribute_app_id);
136 if(ret != ACCOUNT_ERROR_NONE) {
137 _E("Failed to set the app ID.");
141 // Attribute: providerid
142 xmlChar* attribute_provider_id = xmlGetProp(cur_ptr, _ATTRIBUTE_SERVICE_PROVIDER_ID);
143 if(attribute_provider_id != NULL) {
145 _E("Failed to get the attribute(providerid).");
147 _SECURE_D("Attribute: appid - %s", attribute_provider_id);
149 ret = account_type_set_service_provider_id(account_type_handle, (char*)attribute_provider_id);
150 if(ret != ACCOUNT_ERROR_NONE) {
151 _E("Failed to set the service provider id.");
155 // Attribute: multiple-accounts-support
156 xmlChar* multiple_accounts_support = xmlGetProp(cur_ptr, _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT);
157 if(multiple_accounts_support == NULL) {
159 _E("Failed to get the attribute.");
163 _SECURE_D("Attribute: multiple-accounts-support - %s", multiple_accounts_support);
165 if((!xmlStrcmp(multiple_accounts_support, _VALUE_TRUE))) {
166 ret = account_type_set_multiple_account_support(account_type_handle, true);
167 if(ret != ACCOUNT_ERROR_NONE) {
168 _E("Failed to set the multiple accounts support.");
172 ret = account_type_set_multiple_account_support(account_type_handle, false);
173 if (ret != ACCOUNT_ERROR_NONE)
175 _E("Failed to set the multiple accounts support.");
180 // Get the children nodes
181 cur_ptr = cur_ptr->xmlChildrenNode;
182 if(cur_ptr == NULL) {
184 _E("Failed to get the child element.");
188 while(cur_ptr != NULL) {
189 _SECURE_D("Node: %s", cur_ptr->name);
192 if((!xmlStrcmp(cur_ptr->name, _NODE_ICON))) {
193 // Attribute: section
194 xmlChar* section = xmlGetProp(cur_ptr, _ATTRIBUTE_SECTION);
195 if(section == NULL) {
197 _E("Failed to get the attribute.");
201 _SECURE_D("Attribute: section - %s", section);
203 char *resource_path = NULL;
204 if((!xmlStrcmp(section, _VALUE_ACCOUNT))) {
205 xmlChar* account_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
206 if(account_icon == NULL) {
208 _E("Failed to get the value.");
212 _SECURE_D("Node: icon - %s", account_icon);
214 if (!strncmp((const char*)account_icon, "/usr/share/icons", 16)) {
215 ret = account_type_set_icon_path(account_type_handle, (char*)account_icon);
216 if(ret != ACCOUNT_ERROR_NONE) {
217 _E("Failed to set the icon path.");
221 if (!strcmp((const char*)attribute_app_id, "com.samsung.samsungaccount")) {
222 char *icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.samsungaccount/shared/res/", (const char*)account_icon);
223 if(icon_path == NULL) {
224 _E("icon_path is NULL.");
229 _D("icon_path[%s]", icon_path);
230 ret = account_type_set_icon_path(account_type_handle, icon_path);
231 if(ret != ACCOUNT_ERROR_NONE) {
232 _E("Failed to set the icon path.");
237 } else if (!strcmp((const char*)attribute_app_id, "com.samsung.tizenaccount")) {
238 char *icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.tizenaccount/shared/res/", (char*)account_icon);
239 if(icon_path == NULL) {
240 _E("icon_path is NULL.");
245 _D("icon_path[%s]", icon_path);
246 ret = account_type_set_icon_path(account_type_handle, icon_path);
247 if(ret != ACCOUNT_ERROR_NONE) {
248 _E("Failed to set the icon path.");
254 ret = app_manager_get_shared_resource_path((char*)attribute_app_id, &resource_path);
255 if(ret != APP_MANAGER_ERROR_NONE) {
256 _E("Failed to get the shared resource path. app_manager ret=[%d]", ret);
260 char *icon_path = g_strdup_printf("%s%s", resource_path, (char*)account_icon);
261 if(icon_path == NULL) {
262 _E("icon_path is NULL.");
269 _D("icon_path[%s]", icon_path);
270 ret = account_type_set_icon_path(account_type_handle, icon_path);
271 if(ret != ACCOUNT_ERROR_NONE) {
272 _E("Failed to set the icon path.");
279 } else if((!xmlStrcmp(section, _VALUE_ACCOUNT_SMALL))) {
280 xmlChar* account_small_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
281 if(account_small_icon == NULL) {
283 _E("Failed to get the value.");
287 _SECURE_D("Node: icon (small) - %s", account_small_icon);
289 if (!strncmp((const char*)account_small_icon, "/usr/share/icons", 16) || !strcmp((const char*)account_small_icon, "/usr/apps/com.samsung.tizenaccount/shared/res/TizenAccount.png")) {
290 ret = account_type_set_small_icon_path(account_type_handle, (char*)account_small_icon);
291 if(ret != ACCOUNT_ERROR_NONE) {
292 _E("Failed to set the small icon path.");
296 if (!strcmp((const char*)attribute_app_id, "com.samsung.samsungaccount")) {
297 char *small_icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.samsungaccount/shared/res/", (char*)account_small_icon);
298 if(small_icon_path == NULL) {
299 _E("small_icon_path is NULL.");
304 _D("small_icon_path[%s]", small_icon_path);
305 ret = account_type_set_small_icon_path(account_type_handle, (char*)small_icon_path);
306 if(ret != ACCOUNT_ERROR_NONE) {
307 _E("Failed to set the small icon path.");
308 g_free(small_icon_path);
311 g_free(small_icon_path);
313 ret = app_manager_get_shared_resource_path((char*)attribute_app_id, &resource_path);
314 if(ret != APP_MANAGER_ERROR_NONE) {
315 _E("Failed to get the shared resource path.");
319 char *small_icon_path = g_strdup_printf("%s%s", resource_path, (char*)account_small_icon);
320 if(small_icon_path == NULL) {
321 _E("small_icon_path is NULL.");
328 _D("small_icon_path[%s]", small_icon_path);
329 ret = account_type_set_small_icon_path(account_type_handle, (char*)small_icon_path);
330 if(ret != ACCOUNT_ERROR_NONE) {
331 _E("Failed to set the small icon path.");
332 g_free(small_icon_path);
335 g_free(small_icon_path);
339 } else if((!xmlStrcmp(cur_ptr->name, _NODE_LABEL))) {
342 _SECURE_D("Node: %s", cur_ptr->name);
344 // Attribute: xml:lang
345 xmlChar* xml_lang = xmlNodeGetLang(cur_ptr);
346 if(xml_lang != NULL) {
347 _SECURE_D("Attribute: xml:lang - %s", xml_lang);
349 char* lang = (char*)xml_lang;
350 char* converted_lang = NULL;
352 gchar** tokens = g_strsplit(lang, "-", 2);
355 _E("Failed to get token.");
359 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
360 if(upper_token == NULL) {
363 _E("Failed to convert to upper case.");
367 converted_lang = g_strdup_printf("%s_%s", tokens[0], upper_token);
370 if(converted_lang == NULL) {
373 _E("Failed to convert to upper case.");
377 _SECURE_D("Attribute: converted lang - %s", converted_lang);
381 xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
382 if(xml_label == NULL) {
384 g_free(converted_lang);
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, converted_lang);
392 if(ret != ACCOUNT_ERROR_NONE) {
393 g_free(converted_lang);
394 _E("[%d]Failed to set the display name.", ret);
398 g_free(converted_lang);
400 xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
401 if(xml_label == NULL) {
403 _E("Failed to get the value.");
407 _SECURE_D("Node: label - %s", xml_label);
409 ret = account_type_set_label(account_type_handle, (char*)xml_label, _DEFAULT_LOCALE);
410 if(ret != ACCOUNT_ERROR_NONE) {
411 _E("[%d]Failed to set the display name.", ret);
415 } else if((!xmlStrcmp(cur_ptr->name, _NODE_CAPABILITY))) {
416 // Node: <capability>
418 _SECURE_D("Node: %s", cur_ptr->name);
420 xmlChar* xml_capability = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
421 if(xml_capability == NULL) {
423 _E("Failed to get the value.");
427 _SECURE_D("Node: capability - %s", xml_capability);
429 ret = account_type_set_provider_feature(account_type_handle, (char*)xml_capability);
430 if(ret != ACCOUNT_ERROR_NONE) {
431 _E("[%d]Failed to set the capability.", ret);
436 cur_ptr = cur_ptr->next;
442 cur_ptr = cur_ptr->next;
445 // Insert the account type to the account DB
447 int account_type_db_id = 0;
449 ret = account_type_insert_to_db_offline(account_type_handle, &account_type_db_id);
451 ret = account_type_insert_to_db(account_type_handle, &account_type_db_id);
454 if(ret != ACCOUNT_ERROR_NONE) {
455 _E("[%d]Failed to perform account_type_insert_to_db().", ret);
460 ret = account_type_destroy(account_type_handle);
461 if(ret != ACCOUNT_ERROR_NONE) {
462 _E("[%d]Failed to perform account_type_destroy().", ret);
469 ret2 = account_type_destroy(account_type_handle);
470 retvm_if(ret2 != ACCOUNT_ERROR_NONE, ret2, "[%d]Failed to perform account_type_destroy().", ret2);
475 int _unregister_account_provider(pkgmgrinfo_appinfo_h package_info_handle, void* user_data)
478 _D("Unregistering the Account Provider.");
481 pkgmgrinfo_appinfo_get_appid(package_info_handle, &app_id);
482 _D("appid : %s", app_id);
484 int ret = ACCOUNT_ERROR_NONE;
486 ret = account_delete_from_db_by_package_name_without_permission((char*)app_id);
487 if((ret != ACCOUNT_ERROR_NONE) && (ret != ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
488 _E("Failed to perform account_delete_from_db_by_package_name_without_permission().");
493 ret = account_type_delete_by_app_id_offline((char*)app_id);
495 ret = account_type_delete_by_app_id((char*)app_id);
498 if(ret != ACCOUNT_ERROR_NONE) {
499 _E("Failed to perform account_type_delete_by_app_id().");
509 int _on_package_app_list_received_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
512 _D("Pkgmgr parser plugin pre upgrade.");
514 int ret = ACCOUNT_ERROR_NONE;
516 pkgmgrinfo_appinfo_get_appid(handle, &app_id);
517 _D("appid : %s", app_id);
520 ret = account_type_delete_by_app_id_offline((char*)app_id);
522 ret = account_type_delete_by_app_id((char*)app_id);
525 if(ret == ACCOUNT_ERROR_NONE) {
526 _D("PKGMGR_PARSER_PLUGIN_PRE_UPGRADE: app ID - %s", app_id);
527 strncpy(__old_account_provider_app_id, app_id, 128);
536 int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId)
539 _D("PKGMGR_PARSER_PLUGIN_INSTALL");
541 char* account_provider_app_id = NULL;
542 int ret = _register_account_provider(docPtr, account_provider_app_id);
543 retvm_if(ret != 0, -1, "Failed to register the account provider.");
549 int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char* packageId)
552 _D("PKGMGR_PARSER_PLUGIN_UNINSTALL");
554 pkgmgrinfo_pkginfo_h package_info_handle = NULL;
559 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
560 ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &package_info_handle);
562 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(packageId, uid, &package_info_handle);
564 retvm_if(ret != PMINFO_R_OK, ret, "[%d]Failed to pkgmgrinfo_pkginfo_get_pkginfo().", ret);
566 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
567 ret = pkgmgrinfo_appinfo_get_list(package_info_handle, PMINFO_ALL_APP, _unregister_account_provider, NULL);
569 ret = pkgmgrinfo_appinfo_get_usr_list(package_info_handle, PMINFO_ALL_APP, _unregister_account_provider, NULL, uid);
572 if(ret != PMINFO_R_OK) {
573 _D("Failed to get the application information list.");
574 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
579 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
584 int PKGMGR_PARSER_PLUGIN_PRE_UPGRADE(const char* packageId)
587 _D("PKGMGR_PARSER_PLUGIN_PRE_UPGRADE");
589 memset(__old_account_provider_app_id, 0x00, sizeof(__old_account_provider_app_id));
591 pkgmgrinfo_pkginfo_h package_info_handle = NULL;
596 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
597 ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &package_info_handle);
599 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(packageId, uid, &package_info_handle);
601 retvm_if(ret != PMINFO_R_OK, ret, "[%d]Failed to pkgmgrinfo_pkginfo_get_pkginfo().", ret);
603 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
604 ret = pkgmgrinfo_appinfo_get_list(package_info_handle, PMINFO_ALL_APP, _on_package_app_list_received_cb, NULL);
606 ret = pkgmgrinfo_appinfo_get_usr_list(package_info_handle, PMINFO_ALL_APP, _on_package_app_list_received_cb, NULL, uid);
609 if(ret != PMINFO_R_OK) {
610 _D("Failed to get the application information list.");
611 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
616 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
622 int PKGMGR_PARSER_PLUGIN_UPGRADE(xmlDocPtr docPtr, const char* packageId)
625 _D("PKGMGR_PARSER_PLUGIN_UPGRADE");
627 char* account_provider_app_id = NULL;
628 int ret = _register_account_provider(docPtr, account_provider_app_id);
629 retvm_if(ret != 0, ret, "[%d]Failed to register the account provider.", ret);
631 ret = account_query_account_by_package_name(_on_account_received_cb, __old_account_provider_app_id, (void*)account_provider_app_id);
632 if((ret != ACCOUNT_ERROR_NONE) && (ret != ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
633 _E("Failed to perform account_query_account_by_package_name().");