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>
28 #include "account-dlog.h"
30 /* Define EXPORT_API */
32 #define EXPORT_API __attribute__((visibility("default")))
36 static const xmlChar _NODE_ACCOUNT_PROVIDER[] = "account-provider";
37 static const xmlChar _NODE_ICON[] = "icon";
38 static const xmlChar _NODE_LABEL[] = "label";
39 static const xmlChar _NODE_CAPABILITY[] = "capability";
41 static const xmlChar _ATTRIBUTE_APP_ID[] = "appid";
42 static const xmlChar _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT[] = "multiple-accounts-support";
43 static const xmlChar _ATTRIBUTE_SERVICE_PROVIDER_ID[] = "providerid";
44 static const xmlChar _ATTRIBUTE_SECTION[] = "section";
45 static const xmlChar _ATTRIBUTE_TYPE[] = "type";
46 static const xmlChar _ATTRIBUTE_XML_LANG[] = "xml:lang";
48 static const xmlChar _VALUE_TRUE[] = "true";
49 static const xmlChar _VALUE_ACCOUNT[] = "account";
50 static const xmlChar _VALUE_ACCOUNT_SMALL[] = "account-small";
51 static const xmlChar _VALUE_XHIGH[] = "Xhigh";
53 static const char _DEFAULT_LOCALE[] = "default";
55 static char __old_account_provider_app_id[1024];
57 bool _on_account_received_cb(account_h account, void* user_data)
60 retvm_if((account == NULL) || (user_data == NULL), false, "A system error has occurred.");
62 char* account_provider_app_id = (char*)user_data;
63 retvm_if(account_provider_app_id == NULL, false, "account_provider_app_id is NULL.");
66 int account_db_id = 0;
67 int ret = account_get_account_id(account, &account_db_id);
68 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_get_account_id().", ret);
70 ret = account_set_package_name(account, account_provider_app_id);
71 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_set_package_name().", ret);
74 ret = account_update_to_db_by_id_without_permission(account, account_db_id);
75 retvm_if(ret == ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, false, "[%d] The application does not register the account provider.", ret);
76 retvm_if(ret == ACCOUNT_ERROR_PERMISSION_DENIED, false, "[%d] The application has no permission to update this account.", ret);
77 retvm_if(ret == ACCOUNT_ERROR_RECORD_NOT_FOUND, false, "[%d] The account does not exist.", ret);
78 retvm_if(ret != ACCOUNT_ERROR_NONE, false, "[%d] Failed to perform account_update_to_db_by_id_without_permssion().", ret);
83 int _register_account_provider(xmlDocPtr docPtr, char* account_provider_app_id)
86 _D("Registering the Account Provider.");
88 int ret = ACCOUNT_ERROR_NONE;
90 int ret2 = ACCOUNT_ERROR_NONE;
92 account_type_h account_type_handle = NULL;
93 ret = account_type_create(&account_type_handle);
94 if(ret != ACCOUNT_ERROR_NONE || account_type_handle == NULL) {
95 _E("[%d]Memory allocation failed.", ret);
100 xmlNodePtr cur_ptr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
101 if(cur_ptr == NULL) {
103 _E("Failed to get the element.");
107 _SECURE_D("Node: %s", cur_ptr->name);
109 // Get the children nodes
110 cur_ptr = cur_ptr->xmlChildrenNode;
111 if(cur_ptr == NULL) {
113 _E("Failed to get the child element.");
117 while(cur_ptr != NULL) {
118 _SECURE_D("Node: %s", cur_ptr->name);
120 // Node: <account-provider>
121 if((!xmlStrcmp(cur_ptr->name, _NODE_ACCOUNT_PROVIDER))) {
123 xmlChar* attribute_app_id = xmlGetProp(cur_ptr, _ATTRIBUTE_APP_ID);
124 if(attribute_app_id == NULL) {
126 _E("Failed to get the attribute.");
130 _SECURE_D("Attribute: appid - %s", attribute_app_id);
132 ret = account_type_set_app_id(account_type_handle, (char*)attribute_app_id);
133 if(ret != ACCOUNT_ERROR_NONE) {
134 _E("Failed to set the app ID.");
138 // Attribute: providerid
139 xmlChar* attribute_provider_id = xmlGetProp(cur_ptr, _ATTRIBUTE_SERVICE_PROVIDER_ID);
140 if(attribute_provider_id != NULL) {
142 _E("Failed to get the attribute(providerid).");
144 _SECURE_D("Attribute: appid - %s", attribute_provider_id);
146 ret = account_type_set_service_provider_id(account_type_handle, (char*)attribute_provider_id);
147 if(ret != ACCOUNT_ERROR_NONE) {
148 _E("Failed to set the service provider id.");
152 // Attribute: multiple-accounts-support
153 xmlChar* multiple_accounts_support = xmlGetProp(cur_ptr, _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT);
154 if(multiple_accounts_support == NULL) {
156 _E("Failed to get the attribute.");
160 _SECURE_D("Attribute: multiple-accounts-support - %s", multiple_accounts_support);
162 if((!xmlStrcmp(multiple_accounts_support, _VALUE_TRUE))) {
163 ret = account_type_set_multiple_account_support(account_type_handle, true);
164 if(ret != ACCOUNT_ERROR_NONE) {
165 _E("Failed to set the multiple accounts support.");
169 ret = account_type_set_multiple_account_support(account_type_handle, false);
170 if (ret != ACCOUNT_ERROR_NONE)
172 _E("Failed to set the multiple accounts support.");
177 // Get the children nodes
178 cur_ptr = cur_ptr->xmlChildrenNode;
179 if(cur_ptr == NULL) {
181 _E("Failed to get the child element.");
185 while(cur_ptr != NULL) {
186 _SECURE_D("Node: %s", cur_ptr->name);
189 if((!xmlStrcmp(cur_ptr->name, _NODE_ICON))) {
190 // Attribute: section
191 xmlChar* section = xmlGetProp(cur_ptr, _ATTRIBUTE_SECTION);
192 if(section == NULL) {
194 _E("Failed to get the attribute.");
198 _SECURE_D("Attribute: section - %s", section);
200 char *resource_path = NULL;
201 if((!xmlStrcmp(section, _VALUE_ACCOUNT))) {
202 xmlChar* account_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
203 if(account_icon == NULL) {
205 _E("Failed to get the value.");
209 _SECURE_D("Node: icon - %s", account_icon);
211 if (!strncmp((const char*)account_icon, "/usr/share/icons", 16)) {
212 ret = account_type_set_icon_path(account_type_handle, (char*)account_icon);
213 if(ret != ACCOUNT_ERROR_NONE) {
214 _E("Failed to set the icon path.");
218 if (!strcmp((const char*)attribute_app_id, "com.samsung.samsungaccount")) {
219 char *icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.samsungaccount/shared/res/", (const char*)account_icon);
220 if(icon_path == NULL) {
221 _E("icon_path is NULL.");
226 _D("icon_path[%s]", icon_path);
227 ret = account_type_set_icon_path(account_type_handle, icon_path);
228 if(ret != ACCOUNT_ERROR_NONE) {
229 _E("Failed to set the icon path.");
234 } else if (!strcmp((const char*)attribute_app_id, "com.samsung.tizenaccount")) {
235 char *icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.tizenaccount/shared/res/", (char*)account_icon);
236 if(icon_path == NULL) {
237 _E("icon_path is NULL.");
242 _D("icon_path[%s]", icon_path);
243 ret = account_type_set_icon_path(account_type_handle, icon_path);
244 if(ret != ACCOUNT_ERROR_NONE) {
245 _E("Failed to set the icon path.");
251 ret = app_manager_get_shared_resource_path((char*)attribute_app_id, &resource_path);
252 if(ret != APP_MANAGER_ERROR_NONE) {
253 _E("Failed to get the shared resource path. app_manager ret=[%d]", ret);
257 char *icon_path = g_strdup_printf("%s%s", resource_path, (char*)account_icon);
258 if(icon_path == NULL) {
259 _E("icon_path is NULL.");
266 _D("icon_path[%s]", icon_path);
267 ret = account_type_set_icon_path(account_type_handle, icon_path);
268 if(ret != ACCOUNT_ERROR_NONE) {
269 _E("Failed to set the icon path.");
276 } else if((!xmlStrcmp(section, _VALUE_ACCOUNT_SMALL))) {
277 xmlChar* account_small_icon = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
278 if(account_small_icon == NULL) {
280 _E("Failed to get the value.");
284 _SECURE_D("Node: icon (small) - %s", account_small_icon);
286 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")) {
287 ret = account_type_set_small_icon_path(account_type_handle, (char*)account_small_icon);
288 if(ret != ACCOUNT_ERROR_NONE) {
289 _E("Failed to set the small icon path.");
293 if (!strcmp((const char*)attribute_app_id, "com.samsung.samsungaccount")) {
294 char *small_icon_path = g_strdup_printf("%s%s", "/usr/apps/com.samsung.samsungaccount/shared/res/", (char*)account_small_icon);
295 if(small_icon_path == NULL) {
296 _E("small_icon_path is NULL.");
301 _D("small_icon_path[%s]", small_icon_path);
302 ret = account_type_set_small_icon_path(account_type_handle, (char*)small_icon_path);
303 if(ret != ACCOUNT_ERROR_NONE) {
304 _E("Failed to set the small icon path.");
305 g_free(small_icon_path);
308 g_free(small_icon_path);
310 ret = app_manager_get_shared_resource_path((char*)attribute_app_id, &resource_path);
311 if(ret != APP_MANAGER_ERROR_NONE) {
312 _E("Failed to get the shared resource path.");
316 char *small_icon_path = g_strdup_printf("%s%s", resource_path, (char*)account_small_icon);
317 if(small_icon_path == NULL) {
318 _E("small_icon_path is NULL.");
325 _D("small_icon_path[%s]", small_icon_path);
326 ret = account_type_set_small_icon_path(account_type_handle, (char*)small_icon_path);
327 if(ret != ACCOUNT_ERROR_NONE) {
328 _E("Failed to set the small icon path.");
329 g_free(small_icon_path);
332 g_free(small_icon_path);
336 } else if((!xmlStrcmp(cur_ptr->name, _NODE_LABEL))) {
339 _SECURE_D("Node: %s", cur_ptr->name);
341 // Attribute: xml:lang
342 xmlChar* xml_lang = xmlNodeGetLang(cur_ptr);
343 if(xml_lang != NULL) {
344 _SECURE_D("Attribute: xml:lang - %s", xml_lang);
346 char* lang = (char*)xml_lang;
347 char* converted_lang = NULL;
349 gchar** tokens = g_strsplit(lang, "-", 2);
352 _E("Failed to get token.");
356 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
357 if(upper_token == NULL) {
360 _E("Failed to convert to upper case.");
364 converted_lang = g_strdup_printf("%s_%s", tokens[0], upper_token);
367 if(converted_lang == NULL) {
370 _E("Failed to convert to upper case.");
374 _SECURE_D("Attribute: converted lang - %s", converted_lang);
378 xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
379 if(xml_label == NULL) {
381 g_free(converted_lang);
382 _E("Failed to get the value.");
386 _SECURE_D("Node: label - %s", xml_label);
388 ret = account_type_set_label(account_type_handle, (char*)xml_label, converted_lang);
389 if(ret != ACCOUNT_ERROR_NONE) {
390 g_free(converted_lang);
391 _E("[%d]Failed to set the display name.", ret);
395 g_free(converted_lang);
397 xmlChar* xml_label = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
398 if(xml_label == NULL) {
400 _E("Failed to get the value.");
404 _SECURE_D("Node: label - %s", xml_label);
406 ret = account_type_set_label(account_type_handle, (char*)xml_label, _DEFAULT_LOCALE);
407 if(ret != ACCOUNT_ERROR_NONE) {
408 _E("[%d]Failed to set the display name.", ret);
412 } else if((!xmlStrcmp(cur_ptr->name, _NODE_CAPABILITY))) {
413 // Node: <capability>
415 _SECURE_D("Node: %s", cur_ptr->name);
417 xmlChar* xml_capability = xmlNodeListGetString(docPtr, cur_ptr->xmlChildrenNode, 1);
418 if(xml_capability == NULL) {
420 _E("Failed to get the value.");
424 _SECURE_D("Node: capability - %s", xml_capability);
426 ret = account_type_set_provider_feature(account_type_handle, (char*)xml_capability);
427 if(ret != ACCOUNT_ERROR_NONE) {
428 _E("[%d]Failed to set the capability.", ret);
433 cur_ptr = cur_ptr->next;
439 cur_ptr = cur_ptr->next;
442 // Insert the account type to the account DB
444 int account_type_db_id = 0;
446 ret = account_type_insert_to_db_offline(account_type_handle, &account_type_db_id);
448 ret = account_type_insert_to_db(account_type_handle, &account_type_db_id);
451 if(ret != ACCOUNT_ERROR_NONE) {
452 _E("[%d]Failed to perform account_type_insert_to_db().", ret);
457 ret = account_type_destroy(account_type_handle);
458 if(ret != ACCOUNT_ERROR_NONE) {
459 _E("[%d]Failed to perform account_type_destroy().", ret);
466 ret2 = account_type_destroy(account_type_handle);
467 retvm_if(ret2 != ACCOUNT_ERROR_NONE, ret2, "[%d]Failed to perform account_type_destroy().", ret2);
472 int _unregister_account_provider(pkgmgrinfo_appinfo_h package_info_handle, void* user_data)
475 _D("Unregistering the Account Provider.");
478 pkgmgrinfo_appinfo_get_appid(package_info_handle, &app_id);
479 _D("appid : %s", app_id);
481 int ret = ACCOUNT_ERROR_NONE;
483 ret = account_delete_from_db_by_package_name_without_permission((char*)app_id);
484 if((ret != ACCOUNT_ERROR_NONE) && (ret != ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
485 _E("Failed to perform account_delete_from_db_by_package_name_without_permission().");
490 ret = account_type_delete_by_app_id_offline((char*)app_id);
492 ret = account_type_delete_by_app_id((char*)app_id);
495 if(ret != ACCOUNT_ERROR_NONE) {
496 _E("Failed to perform account_type_delete_by_app_id().");
506 int _on_package_app_list_received_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
509 _D("Pkgmgr parser plugin pre upgrade.");
511 int ret = ACCOUNT_ERROR_NONE;
513 pkgmgrinfo_appinfo_get_appid(handle, &app_id);
514 _D("appid : %s", app_id);
517 ret = account_type_delete_by_app_id_offline((char*)app_id);
519 ret = account_type_delete_by_app_id((char*)app_id);
522 if(ret == ACCOUNT_ERROR_NONE) {
523 _D("PKGMGR_PARSER_PLUGIN_PRE_UPGRADE: app ID - %s", app_id);
524 strncpy(__old_account_provider_app_id, app_id, 128);
533 int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId)
536 _D("PKGMGR_PARSER_PLUGIN_INSTALL");
538 char* account_provider_app_id = NULL;
539 int ret = _register_account_provider(docPtr, account_provider_app_id);
540 retvm_if(ret != 0, -1, "Failed to register the account provider.");
546 int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char* packageId)
549 _D("PKGMGR_PARSER_PLUGIN_UNINSTALL");
551 pkgmgrinfo_pkginfo_h package_info_handle = NULL;
553 int ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &package_info_handle);
554 retvm_if(ret != PMINFO_R_OK, ret, "[%d]Failed to pkgmgrinfo_pkginfo_get_pkginfo().", ret);
556 ret = pkgmgrinfo_appinfo_get_list(package_info_handle, PMINFO_UI_APP, _unregister_account_provider, NULL);
557 if(ret != PMINFO_R_OK) {
558 _D("Failed to get the application information list.");
559 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
564 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
569 int PKGMGR_PARSER_PLUGIN_PRE_UPGRADE(const char* packageId)
572 _D("PKGMGR_PARSER_PLUGIN_PRE_UPGRADE");
574 memset(__old_account_provider_app_id, 0x00, sizeof(__old_account_provider_app_id));
576 pkgmgrinfo_pkginfo_h package_info_handle = NULL;
578 int ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &package_info_handle);
579 retvm_if(ret != PMINFO_R_OK, ret, "[%d]Failed to pkgmgrinfo_pkginfo_get_pkginfo().", ret);
581 ret = pkgmgrinfo_appinfo_get_list(package_info_handle, PMINFO_UI_APP, _on_package_app_list_received_cb, NULL);
582 if(ret != PMINFO_R_OK) {
583 _D("Failed to get the application information list.");
584 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
589 pkgmgrinfo_pkginfo_destroy_pkginfo(package_info_handle);
595 int PKGMGR_PARSER_PLUGIN_UPGRADE(xmlDocPtr docPtr, const char* packageId)
598 _D("PKGMGR_PARSER_PLUGIN_UPGRADE");
600 char* account_provider_app_id = NULL;
601 int ret = _register_account_provider(docPtr, account_provider_app_id);
602 retvm_if(ret != 0, ret, "[%d]Failed to register the account provider.", ret);
604 ret = account_query_account_by_package_name(_on_account_received_cb, __old_account_provider_app_id, (void*)account_provider_app_id);
605 if((ret != ACCOUNT_ERROR_NONE) && (ret != ACCOUNT_ERROR_RECORD_NOT_FOUND)) {
606 _E("Failed to perform account_query_account_by_package_name().");