summaryrefslogtreecommitdiff
path: root/pkgmgr_account/src/account.cpp
blob: 10cbaf964cd9b2c3a73e2d4ff3cb0512b6ed56f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//
// Copyright (c) 2013 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <errno.h>
#include <new>
#include <unique_ptr.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <account.h>
#include <account-types.h>
#include <FApp.h>
#include <FAppPkgPackageInfo.h>
#include <FAppPkgPackageManager.h>
#include <FAppPkg_PackageManagerImpl.h>
#include <FBaseColIEnumerator.h>
#include <FBaseColIList.h>
#include <FBaseDataType.h>
#include <FBaseString.h>
#include <FBaseSysLog.h>
#include <FBase_StringConverter.h>
#include <FBaseUtilStringTokenizer.h>
#include <FBaseUtilStringUtil.h>

#define _SysTryReturn(NID, condition, returnValue, ...)	\
	if (!(condition)) { \
		SysLog(NID, __VA_ARGS__); \
		return returnValue;	\
	} \
	else {;}

#define _SysTryCatch(NID, condition, expr, ...) \
	if (!(condition)) { \
		SysLog(NID, __VA_ARGS__); \
		expr; \
		goto CATCH;	\
	} \
	else {;}

using namespace Tizen::App;
using namespace Tizen::App::Package;
using namespace Tizen::Base;
using namespace Tizen::Base::Collection;
using namespace Tizen::Base::Utility;

static const xmlChar _NODE_ACCOUNT_PROVIDER[]				= "account-provider";
static const xmlChar _NODE_ICON[]							= "icon";
static const xmlChar _NODE_LABEL[]							= "label";
static const xmlChar _NODE_CAPABILITY[]						= "capability";

static const xmlChar _ATTRIBUTE_APP_ID[]					= "appid";
static const xmlChar _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT[]	= "multiple-accounts-support";
static const xmlChar _ATTRIBUTE_SECTION[]					= "section";
static const xmlChar _ATTRIBUTE_TYPE[]						= "type";
static const xmlChar _ATTRIBUTE_XML_LANG[]					= "xml:lang";

static const xmlChar _VALUE_TRUE[]							= "true";
static const xmlChar _VALUE_ACCOUNT[]						= "account";
static const xmlChar _VALUE_ACCOUNT_SMALL[]					= "account-small";
static const xmlChar _VALUE_XHIGH[]							= "Xhigh";

static const char _DEFAULT_LOCALE[]							= "default";

static String __oldAccountProviderAppId;

bool
OnAccountReceived(account_h accountHandle, void* pUserData)
{
	SysTryReturn(NID_SCL, accountHandle != null && pUserData != null, false, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	String* pAccountProviderAppId = static_cast<String*> (pUserData);
	SysTryReturn(NID_SCL, pAccountProviderAppId != null, false, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	// Get the account ID
	int accountDbId = 0;
	int ret = account_get_account_id(accountHandle, &accountDbId);
	SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));

	// Set the new app ID
	std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(*pAccountProviderAppId));
	SysTryReturn(NID_SCL, pCharArrayAppId != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));

	ret = account_set_package_name(accountHandle, pCharArrayAppId.get());
	SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, false, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	// Update the account
	ret = account_update_to_db_by_id_ex(accountHandle, accountDbId);
	SysTryReturn(NID_SCL, ret != ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, false, E_INVALID_OPERATION, "[%s] The application does not register the account provider.", GetErrorMessage(E_INVALID_OPERATION));
	SysTryReturn(NID_SCL, ret != ACCOUNT_ERROR_PERMISSION_DENIED, false, E_INVALID_OPERATION, "[%s] The application has no permission to update this account.", GetErrorMessage(E_INVALID_OPERATION));
	SysTryReturn(NID_SCL, ret != ACCOUNT_ERROR_RECORD_NOT_FOUND, false, E_OBJ_NOT_FOUND, "[%s] The account does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
	SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, false, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	return true;
}

result
RegisterAccountProvider(xmlDocPtr docPtr, String& accountProviderAppId)
{
	SysLog(NID_SCL, "Registering the Account Provider.");

	int ret = account_connect();
	SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "A system error has occurred.");

	account_type_h accountTypeHandle = null;
	ret = account_type_create(&accountTypeHandle);
	if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
	{
		SysLog(NID_SCL, "Memory allocation failed.");
		return E_OUT_OF_MEMORY;
	}

	result r = E_SUCCESS;

	// Node: <account>
	xmlNodePtr curPtr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
	SysTryCatch(NID_SCL, curPtr != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the element.", GetErrorMessage(E_INVALID_ARG));

	SysSecureLog(NID_SCL, "Node: %s", curPtr->name);

	// Get the children nodes
	curPtr = curPtr->xmlChildrenNode;
	SysTryCatch(NID_SCL, curPtr != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the child element.", GetErrorMessage(E_INVALID_ARG));

	while(curPtr != null)
	{
		SysSecureLog(NID_SCL, "Node: %s", curPtr->name);

		// Node: <account-provider>
		if ((!xmlStrcmp(curPtr->name, _NODE_ACCOUNT_PROVIDER)))
		{
			// Attribute: appid
			xmlChar* pAppIdId = xmlGetProp(curPtr, _ATTRIBUTE_APP_ID);
			SysTryCatch(NID_SCL, pAppIdId != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the attribute.", GetErrorMessage(E_INVALID_ARG));

			SysSecureLog(NID_SCL, "Attribute: appid - %s", pAppIdId);
			ret = account_type_set_app_id(accountTypeHandle, reinterpret_cast<char*> (pAppIdId));
			SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the app ID.", GetErrorMessage(E_SYSTEM));

			accountProviderAppId.Clear();
			accountProviderAppId.Append(String(reinterpret_cast<char*> (pAppIdId)));

			// Attribute: multiple-accounts-support
			xmlChar* pMultipleAccountsSupport = xmlGetProp(curPtr, _ATTRIBUTE_MULTIPLE_ACCOUNTS_SUPPORT);
			SysTryCatch(NID_SCL, pMultipleAccountsSupport != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the attribute.", GetErrorMessage(E_INVALID_ARG));

			SysSecureLog(NID_SCL, "Attribute: multiple-accounts-support - %s", pMultipleAccountsSupport);
			if ((!xmlStrcmp(pMultipleAccountsSupport, _VALUE_TRUE)))
			{
				ret = account_type_set_multiple_account_support(accountTypeHandle, true);
				SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the multiple accounts support.", GetErrorMessage(E_SYSTEM));
			}
			else
			{
				ret = account_type_set_multiple_account_support(accountTypeHandle, false);
				SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the multiple accounts support.", GetErrorMessage(E_SYSTEM));
			}

			// Get the children nodes
			curPtr = curPtr->xmlChildrenNode;
			SysTryCatch(NID_SCL, curPtr != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the child element.", GetErrorMessage(E_INVALID_ARG));

			while (curPtr != NULL)
			{
				SysSecureLog(NID_SCL, "Node: %s", curPtr->name);

				// Node: <icon>
				if ((!xmlStrcmp(curPtr->name, _NODE_ICON)))
				{
					// Attribute: section
					xmlChar* pSection = xmlGetProp(curPtr, _ATTRIBUTE_SECTION);
					SysTryCatch(NID_SCL, pSection != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the attribute.", GetErrorMessage(E_INVALID_ARG));

					SysSecureLog(NID_SCL, "Attribute: section - %s", pSection);

					if ((!xmlStrcmp(pSection, _VALUE_ACCOUNT)))
					{
						xmlChar* pAccountIcon = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
						SysTryCatch(NID_SCL, pAccountIcon != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the value.", GetErrorMessage(E_INVALID_ARG));

						SysSecureLog(NID_SCL, "Node: icon - %s", pAccountIcon);
						ret = account_type_set_icon_path(accountTypeHandle, reinterpret_cast<char*> (pAccountIcon));
						SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the icon path.", GetErrorMessage(E_SYSTEM));
					}
					else if ((!xmlStrcmp(pSection, _VALUE_ACCOUNT_SMALL)))
					{
						xmlChar* pAccountSmallIcon = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
						SysTryCatch(NID_SCL, pAccountSmallIcon != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the value.", GetErrorMessage(E_INVALID_ARG));

						SysSecureLog(NID_SCL, "Node: icon (small) - %s",  pAccountSmallIcon);
						ret = account_type_set_small_icon_path(accountTypeHandle, reinterpret_cast<char*> (pAccountSmallIcon));
						SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the small icon path.", GetErrorMessage(E_SYSTEM));
					}
				}
				// Node: <label>
				else if ((!xmlStrcmp(curPtr->name, _NODE_LABEL)))
				{
					SysSecureLog(NID_SCL, "Node: %s", curPtr->name);

		    		// Attribute: xml:lang
					xmlChar* pLang = xmlNodeGetLang(curPtr);
					if (pLang != null)
					{
						SysSecureLog(NID_SCL, "Attribute: xml:lang - %s", pLang);

						String lang(reinterpret_cast<char*> (pLang));

						StringTokenizer strTok(lang, L"-");
						String token;
						String convertedLang;

						strTok.GetNextToken(convertedLang);
						convertedLang.Append(L"_");

						strTok.GetNextToken(token);
						token.ToUpper();
						convertedLang.Append(token);

						std::unique_ptr<ByteBuffer> pConvertedLangBuf(StringUtil::StringToUtf8N(convertedLang));
						SysTryCatch(NID_SCL, pConvertedLangBuf != null, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to convert String to Utf8N.", GetErrorMessage(E_SYSTEM));

						SysSecureLog(NID_SCL, "Attribute: converted lang - %s", pConvertedLangBuf->GetPointer());

						xmlChar* pLabel = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
						SysTryCatch(NID_SCL, pLabel != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the value.", GetErrorMessage(E_INVALID_ARG));

						SysSecureLog(NID_SCL, "Node: label - %s", pLabel);
						ret = account_type_set_label(accountTypeHandle, reinterpret_cast<char*> (pLabel), reinterpret_cast<char*> (pConvertedLangBuf->GetPointer()));
						SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the display name.", GetErrorMessage(E_SYSTEM));
					}
					else
					{
						xmlChar* pLabel = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
						SysTryCatch(NID_SCL, pLabel != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the value.", GetErrorMessage(E_INVALID_ARG));

						SysSecureLog(NID_SCL, "Node: label - %s",  pLabel);
						ret = account_type_set_label(accountTypeHandle, reinterpret_cast<char*> (pLabel), _DEFAULT_LOCALE);
						SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the display name.", GetErrorMessage(E_SYSTEM));
					}
				}
				// Node: <capability>
				else if ((!xmlStrcmp(curPtr->name, _NODE_CAPABILITY)))
				{
					SysSecureLog(NID_SCL, "Node: %s", curPtr->name);

					xmlChar* pCapability = xmlNodeListGetString(docPtr, curPtr->xmlChildrenNode, 1);
					SysTryCatch(NID_SCL, pCapability != null, r = E_INVALID_ARG, E_INVALID_ARG, "[%s] Failed to get the value.", GetErrorMessage(E_INVALID_ARG));

					SysSecureLog(NID_SCL, "Node: capability - %s",  pCapability);
					ret = account_type_set_provider_feature(accountTypeHandle, reinterpret_cast<char*> (pCapability));
					SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to set the capability.", GetErrorMessage(E_SYSTEM));
				}

				curPtr = curPtr->next;
			}

			break;
		}

		curPtr = curPtr->next;
	}

	// Insert the account type to the account DB
	{
		int accountTypeDbId = 0;
		ret = account_type_insert_to_db(accountTypeHandle, &accountTypeDbId);
		SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));
	}

	account_type_destroy(accountTypeHandle);
	SysTryReturnResult(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, E_SYSTEM, "A system error has occurred.");

	return E_SUCCESS;

CATCH:
	account_type_destroy(accountTypeHandle);
	SysTryReturnResult(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, E_SYSTEM, "A system error has occurred.");

	return r;
}

extern "C"
__attribute__ ((visibility("default")))
int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr docPtr, const char* packageId)
{
	SysLog(NID_SCL, "PKGMGR_PARSER_PLUGIN_INSTALL");

	String accountProviderAppId;
	result r = RegisterAccountProvider(docPtr, accountProviderAppId);
	_SysTryReturn(NID_SCL, r == E_SUCCESS, -1, "Failed to register the account provider.");

	return 0;
}

extern "C"
__attribute__ ((visibility("default")))
int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr docPtr, const char* packageId)
{
	SysLog(NID_SCL, "PKGMGR_PARSER_PLUGIN_UNINSTALL");

	int ret = account_connect();
	_SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");

	// Node: <account>
	xmlNodePtr curPtr = xmlFirstElementChild(xmlDocGetRootElement(docPtr));
	_SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the element.");

	SysSecureLog(NID_SCL, "Node: %s", curPtr->name);

	// Get the children nodes
	curPtr = curPtr->xmlChildrenNode;
	_SysTryCatch(NID_SCL, curPtr != null, ret = -EINVAL, "Failed to get the child element.");

	while(curPtr != null)
	{
		SysSecureLog(NID_SCL, "Node: %s", curPtr->name);

		// Node: <account-provider>
		if ((!xmlStrcmp(curPtr->name, _NODE_ACCOUNT_PROVIDER)))
		{
			// Attribute: appid
			xmlChar* pAppIdId = xmlGetProp(curPtr, _ATTRIBUTE_APP_ID);
			_SysTryCatch(NID_SCL, pAppIdId != null, ret = -EINVAL, "Failed to get the attribute.");

			SysSecureLog(NID_SCL, "Attribute: appid - %s", pAppIdId);

			ret = account_delete_from_db_by_package_name(reinterpret_cast<char*> (pAppIdId));
			_SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "A system error has occurred.");

			ret = account_type_delete_by_app_id(reinterpret_cast<char*> (pAppIdId));
			_SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, ret = -1, "A system error has occurred.");

			break;
		}

		curPtr = curPtr->next;
	}

	SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	return 0;

CATCH:
	SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	return ret;
}

extern "C"
__attribute__ ((visibility("default")))
int PKGMGR_PARSER_PLUGIN_PRE_UPGRADE(const char* packageId)
{
	SysLog(NID_SCL, "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE");

	result r = E_SUCCESS;

	__oldAccountProviderAppId.Clear();

	_PackageManagerImpl* pPackageManagerImpl = _PackageManagerImpl::GetInstance();
	_SysTryReturn(NID_SCL, pPackageManagerImpl != null, -1, "Failed to get the PackageManagerImpl instance.");

	String pkgId(packageId);
	std::unique_ptr<PackageInfo> pPackageInfo(pPackageManagerImpl->GetPackageInfoN(pkgId));
	r = GetLastResult();
	_SysTryReturn(NID_SCL, r != E_PKG_NOT_INSTALLED, -1, "The package is not installed.");
	_SysTryReturn(NID_SCL, pPackageInfo != null && r == E_SUCCESS, -1, "Failed to get the package information.");

	std::unique_ptr<IList, AllElementsDeleter> pPackageAppInfoList(pPackageInfo->GetPackageAppInfoListN());
	_SysTryReturn(NID_SCL, pPackageAppInfoList != null, -1, "Failed to get the application information list.");

	std::unique_ptr<IEnumerator> pEnum(pPackageAppInfoList->GetEnumeratorN());
	_SysTryReturn(NID_SCL, pEnum != null, -1, "A system error has occurred.");

	int ret = account_connect();
	_SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");

	while (pEnum->MoveNext() == E_SUCCESS)
	{
		PackageAppInfo* pPackageAppInfo = dynamic_cast<PackageAppInfo*> (pEnum->GetCurrent());
		_SysTryCatch(NID_SCL, pPackageAppInfo != null, ret = -1, "A system error has occurred.");

		std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(pPackageAppInfo->GetAppId()));
		_SysTryCatch(NID_SCL, pCharArrayAppId != null, ret = -1, "Memory allocation failed.");

		ret = account_type_delete_by_app_id(pCharArrayAppId.get());
		if (ret == ACCOUNT_ERROR_NONE)
		{
			SysLog(NID_SCL, "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE: app ID - %ls", (pPackageAppInfo->GetAppId()).GetPointer());

			__oldAccountProviderAppId = pPackageAppInfo->GetAppId();
			break;
		}
	}

	SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	return 0;

CATCH:
	SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));

	return ret;
}

extern "C"
__attribute__ ((visibility("default")))
int PKGMGR_PARSER_PLUGIN_UPGRADE(xmlDocPtr docPtr, const char* packageId)
{
	SysLog(NID_SCL, "PKGMGR_PARSER_PLUGIN_UPGRADE");

	String accountProviderAppId;
	result r = RegisterAccountProvider(docPtr, accountProviderAppId);
	_SysTryReturn(NID_SCL, r == E_SUCCESS, -1, "Failed to register the account provider.");

	int ret = account_connect();
	_SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");

	std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(__oldAccountProviderAppId));
	_SysTryCatch(NID_SCL, pCharArrayAppId != null, ret = -1, "Memory allocation failed.");

	ret = account_query_account_by_package_name(OnAccountReceived, pCharArrayAppId.get(), (void*) &accountProviderAppId);
	_SysTryCatch(NID_SCL, (ret == ACCOUNT_ERROR_NONE) || (ret == ACCOUNT_ERROR_RECORD_NOT_FOUND), ret = -1, "A system error has occurred.");

	r = GetLastResult();
	_SysTryCatch(NID_SCL, r == E_SUCCESS, ret = -1, "Failed to update accounts.");

	ret = account_disconnect();
	_SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");

	return 0;

CATCH:
	_SysTryReturn(NID_SCL, account_disconnect() == ACCOUNT_ERROR_NONE, -1, "A system error has occurred.");

	return ret;
}