change version to 1.2.2.0
[platform/framework/native/net.git] / src / FNet_NetAccountManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FNetNetAccountManager.cpp
19  * @brief               This is the implementation for the _NetAccountManagerImpl class.
20  */
21
22 #include <dlfcn.h>
23 #include <net_connection.h>
24 #include <FNetNetAccountInfo.h>
25 #include <FNetNetAccountManager.h>
26 #include <FNetWifiWifiNetAccountInfo.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseRtMutexGuard.h>
29 #include <FBase_StringConverter.h>
30 #include "FNet_NetTypes.h"
31 #include "FNet_NetAccountInfoImpl.h"
32 #include "FNet_NetAccountManagerImpl.h"
33 #include "FNet_NetConnectionManagerImpl.h"
34 #include "FNet_SystemNetConnection.h"
35 #include "FNet_NetAccountDatabase.h"
36 #include "FNet_NetUtility.h"
37 #include "FNetWifi_WifiNetAccountInfoImpl.h"
38
39 using namespace std;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Runtime;
43 using namespace Tizen::Net::Wifi;
44
45 namespace Tizen { namespace Net {
46
47 static const char _WIFI_LIBRARY_NAME[] = "libosp-wifi.so";
48
49 bool _NetAccountManagerImpl::__isNetAccountDbInitialized = false;
50 NetAccountId _NetAccountManagerImpl::__internetAccountId = INVALID_HANDLE;
51 NetAccountId _NetAccountManagerImpl::__mmsAccountId = INVALID_HANDLE;
52 String* _NetAccountManagerImpl::__pInternetProfileName = null;
53 String* _NetAccountManagerImpl::__pMmsProfileName = null;
54
55 _NetAccountManagerImpl::_NetAccountManagerImpl(void)
56         : __pConnectionHandle(null)
57 {
58 }
59
60 _NetAccountManagerImpl::~_NetAccountManagerImpl(void)
61 {
62 }
63
64 result
65 _NetAccountManagerImpl::Construct(void)
66 {
67         result r = E_SUCCESS;
68         int ret = CONNECTION_ERROR_NONE;
69         connection_h connectionHandle = null;
70
71         SysAssertf(__pConnectionHandle == null,
72                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
73
74         _SystemNetConnection::InitializeNetworkFramework();
75         _NetAccountManagerImpl::UpdateNetAccountDb();
76
77         ret = connection_create(&connectionHandle);
78         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
79                         "A system error has been occurred. The return value from connection_create() is %d", GetErrorMessage(E_SYSTEM), ret);
80
81         __pConnectionHandle.reset(connectionHandle);
82
83         return r;
84 }
85
86 NetAccountId
87 _NetAccountManagerImpl::CreateNetAccount(NetAccountInfo& netAccountInfo)
88 {
89         result r = E_SUCCESS;
90         _NetAccountInfoImpl* pInfoImpl = _NetAccountInfoImpl::GetInstance(netAccountInfo);
91         NetAccountId accountId = INVALID_HANDLE;
92         String profileName;
93         bool isCreated = false;
94         int ret = CONNECTION_ERROR_NONE;
95         connection_h connectionHandle = __pConnectionHandle.get();
96         connection_profile_h profileHandle = null;
97
98         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
99
100         SysTryReturn(NID_NET, pInfoImpl != null, INVALID_HANDLE, E_INVALID_ARG,
101                         "[%s] Invalid argument is used. Network account info is invalid.", GetErrorMessage(E_INVALID_ARG));
102         SysTryReturn(NID_NET, !netAccountInfo.GetAccountName().IsEmpty(),
103                         INVALID_HANDLE, E_INVALID_ARG,
104                         "[%s] Invalid argument is used. accountName is an empty string.", GetErrorMessage(E_INVALID_ARG));
105
106         unique_ptr<IList, _CollectionDeleter> pProfileNamesBefore(_NetAccountManagerImpl::GetAppProfileNamesN());
107         SysTryReturn(NID_NET, pProfileNamesBefore != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
108                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
109
110         unique_ptr<char[]> pAccountName(_StringConverter::CopyToCharArrayN(netAccountInfo.GetAccountName()));
111         SysTryReturn(NID_NET, pAccountName != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
112                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
113
114         ret = connection_profile_create(CONNECTION_PROFILE_TYPE_CELLULAR, pAccountName.get(), &profileHandle);
115         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE,     E_SYSTEM,
116                         "[%s] A system error has been occurred. The return value from connection_profile_create() is %d",
117                         GetErrorMessage(E_SYSTEM), ret);
118
119         unique_ptr<void, _ProfileDeleter> pProfileHandle(profileHandle);
120         r = pInfoImpl->ConvertToProfileInfo(profileHandle);
121         SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
122
123         ret = connection_add_profile(connectionHandle, profileHandle);
124         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
125                         "[%s] A system error has been occurred. The return value from connection_add_profile() is %d",
126                         GetErrorMessage(E_SYSTEM), ret);
127
128         unique_ptr<IList, _CollectionDeleter> pProfileNamesAfter(_NetAccountManagerImpl::GetAppProfileNamesN());
129         SysTryReturn(NID_NET, pProfileNamesAfter != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
130                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
131
132         unique_ptr<IEnumerator> pEnum(pProfileNamesAfter->GetEnumeratorN());
133         SysTryReturn(NID_NET, pEnum != null, INVALID_HANDLE, E_OUT_OF_MEMORY,
134                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
135
136         while (pEnum->MoveNext() == E_SUCCESS)
137         {
138                 String* pProfileName = null;
139
140                 pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
141                 if ((pProfileName != null) && (!pProfileName->IsEmpty()))
142                 {
143                         if (!pProfileNamesBefore->Contains(*pProfileName))
144                         {
145                                 SysLog(NID_NET, "ProfileName[%ls] is not found before, so it is a new profile name.", pProfileName->GetPointer());
146                                 isCreated = true;
147                                 profileName = *pProfileName;
148
149                                 break;
150                         }
151                 }
152         }
153
154         SysTryReturn(NID_NET, isCreated, INVALID_HANDLE, E_SYSTEM,
155                         "[%s] A system error has been occurred. New account is not found.", GetErrorMessage(E_SYSTEM));
156         SysTryReturn(NID_NET, !profileName.IsEmpty(), INVALID_HANDLE, E_SYSTEM,
157                         "[%s] A system error has been occurred. The profileName of new account is an empty string.", GetErrorMessage(E_SYSTEM));
158
159         r = _NetAccountDatabase::AddAccount(netAccountInfo.GetAccountName(), profileName, _NET_ACCOUNT_OWNER_THIS, accountId);
160         SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
161
162         r = pInfoImpl->SetAccountId(accountId);
163         SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
164
165         ClearLastResult();
166
167         SysSecureLog( NID_NET, "CreateNetAccount() has been succeeded with accountId:%d, accountName:%ls, profileName:%ls",
168                         netAccountInfo.GetAccountId(), netAccountInfo.GetAccountName().GetPointer(), profileName.GetPointer());
169
170         return accountId;
171 }
172
173 result
174 _NetAccountManagerImpl::DeleteNetAccount(NetAccountId netAccountId)
175 {
176         result r = E_SUCCESS;
177         String profileName;
178         bool isReadOnly = true;
179         int ret = CONNECTION_ERROR_NONE;
180         connection_h connectionHandle = __pConnectionHandle.get();
181
182         SysLog(NID_NET, "DeleteNetAccount() has been called with accountId:%d", netAccountId);
183
184         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
185
186         SysTryReturnResult(NID_NET, netAccountId > 0, E_INVALID_ACCOUNT,
187                         "Invalid network account. accountId=%d", netAccountId);
188
189         isReadOnly = _NetAccountDatabase::IsReadOnly(netAccountId);
190         r = GetLastResult();
191         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
192         SysTryReturnResult(NID_NET, !isReadOnly, E_INVALID_OPERATION, "Not allowed on the specified network account.");
193
194         r = _NetAccountDatabase::GetProfileName(netAccountId, profileName);
195         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
196         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ACCOUNT, "Invalid network account. accountId=%d", netAccountId);
197
198         unique_ptr<void, _ProfileDeleter> pProfileHandle(_NetAccountManagerImpl::GetPsProfileHandleN(profileName));
199         SysTryReturnResult(NID_NET, pProfileHandle != null, E_INVALID_ACCOUNT,
200                         "Invalid network account. accountId=%d", netAccountId);
201
202         ret = connection_remove_profile(connectionHandle, pProfileHandle.get());
203         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
204                         "A system error has been occurred. The return value from connection_remove_profile() is %d", ret);
205
206         r = _NetAccountDatabase::RemoveAccountByAccountId(netAccountId);
207         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
208         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
209                         "A system error has been occurred. Failed to remove the account.");
210
211         return r;
212 }
213
214 result
215 _NetAccountManagerImpl::UpdateNetAccount(const NetAccountInfo& netAccountInfo)
216 {
217         result r = E_SUCCESS;
218         String profileName;
219         bool isReadOnly = true;
220         const _NetAccountInfoImpl* pInfoImpl = _NetAccountInfoImpl::GetInstance(netAccountInfo);
221         int ret = CONNECTION_ERROR_NONE;
222         connection_h connectionHandle = (connection_h) __pConnectionHandle.get();
223
224         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
225
226         SysTryReturnResult(NID_NET, pInfoImpl != null, E_INVALID_ARG,
227                         "Invalid argument is used. Network account info is invalid.");
228         SysTryReturnResult(NID_NET, netAccountInfo.GetAccountId() > 0, E_INVALID_ARG,
229                         "Invalid argument is used. accountId=%d", netAccountInfo.GetAccountId());
230         SysTryReturnResult(NID_NET, !netAccountInfo.GetAccountName().IsEmpty(), E_INVALID_ARG,
231                         "Invalid argument is used. accountName is an empty string.");
232
233         isReadOnly = _NetAccountDatabase::IsReadOnly(netAccountInfo.GetAccountId());
234         r = GetLastResult();
235         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
236         SysTryReturnResult(NID_NET, !isReadOnly, E_INVALID_OPERATION,
237                         "Not allowed on the specified network account.");
238
239         r = _NetAccountDatabase::GetProfileName(netAccountInfo.GetAccountId(), profileName);
240         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
241         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ARG,
242                         "Invalid argument is used. accountId=%d.", netAccountInfo.GetAccountId());
243
244         unique_ptr<void, _ProfileDeleter> pProfileHandle(_NetAccountManagerImpl::GetPsProfileHandleN(profileName));
245         SysTryReturnResult(NID_NET, pProfileHandle != null, E_INVALID_ACCOUNT,
246                         "Invalid network account. accountId=%d", netAccountInfo.GetAccountId());
247
248         r = pInfoImpl->ConvertToProfileInfo(pProfileHandle.get());
249         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
250
251         ret = connection_update_profile(connectionHandle, pProfileHandle.get());
252         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
253                         "A system error has been occurred. The return value from connection_update_profile() is %d",
254                         GetErrorMessage(E_SYSTEM), ret);
255
256         r = _NetAccountDatabase::UpdateAccountName(netAccountInfo.GetAccountId(), netAccountInfo.GetAccountName());
257         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
258         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
259                         "A system error has been occurred. Failed to update accountName.");
260
261         return r;
262 }
263
264 NetAccountInfo*
265 _NetAccountManagerImpl::GetNetAccountInfoN(NetAccountId netAccountId) const
266 {
267         result r = E_SUCCESS;
268         NetAccountInfo* pInfo = null;
269         connection_profile_h profileHandle = null;
270         unique_ptr<void, _ProfileDeleter> pProfileHandle;
271
272         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
273
274         SysTryReturn(NID_NET, netAccountId > 0, null, E_INVALID_ACCOUNT,
275                         "[%s] Invalid network account. accountId=%d.", GetErrorMessage(E_INVALID_ACCOUNT), netAccountId);
276
277         if (netAccountId == _DEFAULT_WIFI_ACCOUNT_ID)
278         {
279                 profileHandle = _NetAccountManagerImpl::GetWifiProfileHandleN();
280                 SysTryReturn(NID_NET, profileHandle != null, null, E_INVALID_ACCOUNT,
281                                 "[%s] WiFi account is NOT found.",GetErrorMessage(E_INVALID_ACCOUNT));
282                 pProfileHandle.reset(profileHandle);
283
284                 pInfo = _NetAccountManagerImpl::CreateWifiNetAccountInfoN(
285                                 profileHandle);
286                 r = GetLastResult();
287                 SysTryReturn(NID_NET, pInfo != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
288         }
289         else if (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
290         {
291                 SysLogException(NID_NET, E_INVALID_ACCOUNT,
292                                 "[%s] Wifi direct account information is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
293
294                 return null;
295         }
296         else if (netAccountId == _DEFAULT_USB_ACCOUNT_ID)
297         {
298                 SysLogException(NID_NET, E_INVALID_ACCOUNT,
299                                 "[%s] USB account information is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
300
301                 return null;
302         }
303         else
304         {
305                 String profileName;
306
307                 r = _NetAccountDatabase::GetProfileName(netAccountId, profileName);
308                 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
309
310                 profileHandle = _NetAccountManagerImpl::GetPsProfileHandleN(profileName);
311                 SysTryReturn(NID_NET, profileHandle != null, null, E_INVALID_ACCOUNT,
312                                 "[%s] PS account is NOT found.", GetErrorMessage(E_INVALID_ACCOUNT));
313                 pProfileHandle.reset(profileHandle);
314
315                 pInfo = _NetAccountInfoImpl::CreateNetAccountInfoN(profileHandle);
316                 r = GetLastResult();
317                 SysTryReturn(NID_NET, pInfo != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
318         }
319
320         ClearLastResult();
321
322         return pInfo;
323 }
324
325 IListT<NetAccountId>*
326 _NetAccountManagerImpl::GetNetAccountIdsN(void) const
327 {
328         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
329
330         _NetAccountManagerImpl::UpdateNetAccountDb(true);
331
332         ClearLastResult();
333
334         return _NetAccountDatabase::GetAccountIdsN();
335 }
336
337 IList*
338 _NetAccountManagerImpl::GetNetAccountNamesN(void) const
339 {
340         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
341
342         _NetAccountManagerImpl::UpdateNetAccountDb(true);
343
344         ClearLastResult();
345
346         return _NetAccountDatabase::GetAccountNamesN();
347 }
348
349 NetAccountId
350 _NetAccountManagerImpl::GetNetAccountId(const String& netAccountName) const
351 {
352         ClearLastResult();
353
354         result r = E_SUCCESS;
355         NetAccountId accountId = INVALID_HANDLE;
356
357         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
358
359         r = _NetAccountDatabase::GetAccountIdByAccountName(netAccountName, accountId);
360         SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, E_INVALID_ARG,
361                         "[%s] Invalid argument is used. AccountName[%ls] is not found.",
362                         GetErrorMessage(E_INVALID_ARG), netAccountName.GetPointer());
363
364         SysSecureLog(NID_NET, "GetNetAccountId() has been succeeded with accountId:%d", accountId);
365
366         return accountId;
367 }
368
369 NetAccountId
370 _NetAccountManagerImpl::GetNetAccountId(NetBearerType netBearerType) const
371 {
372         ClearLastResult();
373
374         NetAccountId accountId = INVALID_HANDLE;
375
376         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
377
378         if (netBearerType == NET_BEARER_PS)
379         {
380                 accountId = _NetAccountManagerImpl::GetInternetAccountId();
381         }
382         else if (netBearerType == NET_BEARER_MMS)
383         {
384                 accountId = _NetAccountManagerImpl::GetMmsAccountId();
385         }
386         else if (netBearerType == NET_BEARER_WIFI)
387         {
388                 accountId = _DEFAULT_WIFI_ACCOUNT_ID;
389         }
390         else if (netBearerType == NET_BEARER_WIFI_DIRECT)
391         {
392                 accountId = _DEFAULT_WIFI_DIRECT_ACCOUNT_ID;
393         }
394         else if (netBearerType == NET_BEARER_USB)
395         {
396                 accountId = _DEFAULT_USB_ACCOUNT_ID;
397         }
398         else
399         {
400                 SysLogException(NID_NET, E_INVALID_ARG,
401                                 "[%s] Invalid argument is used. bearerType=%d", GetErrorMessage(E_INVALID_ARG), netBearerType);
402         }
403
404         return accountId;
405 }
406
407 NetPreferenceType
408 _NetAccountManagerImpl::GetNetPreference(void) const
409 {
410         _NetConnectionManagerImpl* pConnectionManager = _NetConnectionManagerImpl::GetInstance();
411
412         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
413
414         SysTryReturn(NID_NET, pConnectionManager != null, NET_WIFI_FIRST, E_SYSTEM,
415                         "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
416
417         return pConnectionManager->GetNetPreference();
418 }
419
420 result
421 _NetAccountManagerImpl::SetNetPreference(NetPreferenceType netPreference)
422 {
423         result r = E_SUCCESS;
424
425         _NetConnectionManagerImpl* pConnectionManager = _NetConnectionManagerImpl::GetInstance();
426
427         SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
428
429         SysTryReturnResult(NID_NET, pConnectionManager != null, E_SYSTEM, "A system error has been occurred.");
430
431         r = pConnectionManager->SetNetPreference(netPreference);
432         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
433
434         return r;
435 }
436
437 _NetAccountManagerImpl*
438 _NetAccountManagerImpl::GetInstance(NetAccountManager& netAccountManager)
439 {
440         return netAccountManager.__pNetAccountManagerImpl;
441 }
442
443 const _NetAccountManagerImpl*
444 _NetAccountManagerImpl::GetInstance(const NetAccountManager& netAccountManager)
445 {
446         return netAccountManager.__pNetAccountManagerImpl;
447 }
448
449 void*
450 _NetAccountManagerImpl::GetPsProfileHandleN(const String& profileName)
451 {
452         ClearLastResult();
453
454         int ret = CONNECTION_ERROR_NONE;
455         connection_h connectionHandle = null;
456         connection_profile_iterator_h iterator = null;
457         connection_profile_h profileHandle = null;
458         connection_profile_h matchingProfileHandle = null;
459
460         SysLog(NID_NET, "GetPsProfileHandleN() has been called with profileName:%ls", profileName.GetPointer());
461
462         SysTryReturn(NID_NET, !profileName.IsEmpty(), null, E_INVALID_ARG,
463                         "[%s] Invalid argument is used. profileName is an empty string.", GetErrorMessage(E_INVALID_ARG));
464
465         ret = connection_create(&connectionHandle);
466         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
467                         "[%s] A system error has been occurred. The return value from connection_create() is %d",
468                         GetErrorMessage(E_SYSTEM), ret);
469         unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
470
471         ret = connection_get_profile_iterator(connectionHandle, CONNECTION_ITERATOR_TYPE_REGISTERED, &iterator);
472         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
473                         "[%s] A system error has been occurred. The return value from connection_get_profile_iterator() is %d",
474                         GetErrorMessage(E_SYSTEM), ret);
475         unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
476
477         ret = connection_profile_iterator_next(iterator, &profileHandle);
478         while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
479         {
480                 char* pTempProfileName = null;
481                 String tempProfileName;
482
483                 ret = connection_profile_get_id(profileHandle, &pTempProfileName);
484                 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
485                                 "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
486                                 GetErrorMessage(E_SYSTEM), ret);
487
488                 tempProfileName = String(pTempProfileName);
489                 free(pTempProfileName);
490
491                 if (profileName.Equals(tempProfileName))
492                 {
493                         ret = connection_profile_clone(&matchingProfileHandle, profileHandle);
494                         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
495                                         "[%s] A system error has been occurred. The return value from connection_profile_clone() is %d",
496                                         GetErrorMessage(E_SYSTEM), ret);
497
498                         break;
499                 }
500
501                 profileHandle = null;
502                 ret = connection_profile_iterator_next(iterator, &profileHandle);
503         }
504
505         SysTryReturn(NID_NET, matchingProfileHandle != null, null, E_INVALID_ACCOUNT,
506                         "[%s] Matching profileName is not found. profileName=%ls", GetErrorMessage(E_INVALID_ACCOUNT), profileName.GetPointer());
507
508         return matchingProfileHandle;
509 }
510
511 void*
512 _NetAccountManagerImpl::GetWifiProfileHandleN(void)
513 {
514         ClearLastResult();
515
516         int ret = CONNECTION_ERROR_NONE;
517         connection_h connectionHandle = null;
518         connection_profile_iterator_h iterator = null;
519         connection_profile_h profileHandle = null;
520         connection_profile_h matchingProfileHandle = null;
521         connection_profile_type_e profileType = CONNECTION_PROFILE_TYPE_CELLULAR;
522
523         ret = connection_create(&connectionHandle);
524         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
525                         "[%s] A system error has been occurred. The return value from connection_create() is %d",
526                         GetErrorMessage(E_SYSTEM), ret);
527         unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
528
529         ret = connection_get_profile_iterator(connectionHandle, CONNECTION_ITERATOR_TYPE_CONNECTED, &iterator);
530         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
531                         "[%s] A system error has been occurred. The return value from connection_get_profile_iterator() is %d",
532                         GetErrorMessage(E_SYSTEM), ret);
533         unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
534
535         ret = connection_profile_iterator_next(iterator, &profileHandle);
536         while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
537         {
538                 ret = connection_profile_get_type(profileHandle, &profileType);
539                 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
540                                 "[%s] A system error has been occurred. The return value from connection_profile_get_type() is %d",
541                                 GetErrorMessage(E_SYSTEM), ret);
542
543                 if (profileType == CONNECTION_PROFILE_TYPE_WIFI)
544                 {
545                         SysLog(NID_NET, "Connected WiFi profile is found.");
546                         ret = connection_profile_clone(&matchingProfileHandle,
547                                         profileHandle);
548                         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
549                                         "[%s] A system error has been occurred. The return value from connection_profile_clone() is %d",
550                                         GetErrorMessage(E_SYSTEM), ret);
551
552                         break;
553                 }
554
555                 profileHandle = null;
556                 ret = connection_profile_iterator_next(iterator, &profileHandle);
557         }
558
559         SysTryReturn(NID_NET, matchingProfileHandle != null, null, E_INVALID_ACCOUNT,
560                         "[%s] Active Wi-Fi profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
561
562         return matchingProfileHandle;
563 }
564
565 IList*
566 _NetAccountManagerImpl::GetAppProfileNamesN(void)
567 {
568         ClearLastResult();
569
570         result r = E_SUCCESS;
571         int ret = CONNECTION_ERROR_NONE;
572         connection_h connectionHandle = null;
573         connection_profile_iterator_h iterator = null;
574         connection_profile_h profileHandle = null;
575         connection_profile_type_e profileType = CONNECTION_PROFILE_TYPE_WIFI;
576
577         ret = connection_create(&connectionHandle);
578         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
579                         "[%s] A system error has been occurred. The return value from connection_create() is %d",
580                         GetErrorMessage(E_SYSTEM), ret);
581         unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
582
583         unique_ptr<ArrayList, _CollectionDeleter> pList(new (std::nothrow) ArrayList());
584         SysTryReturn(NID_NET, pList != null, null, E_OUT_OF_MEMORY,
585                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
586
587         r = pList->Construct();
588         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
589
590         ret = connection_get_profile_iterator(connectionHandle, CONNECTION_ITERATOR_TYPE_REGISTERED, &iterator);
591         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
592                         "[%s] A system error has been occurred. The return value from connection_get_profile_iterator() is %d",
593                         GetErrorMessage(E_SYSTEM), ret);
594         unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
595
596         ret = connection_profile_iterator_next(iterator, &profileHandle);
597         while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
598         {
599                 char* pProfileName = null;
600
601                 ret = connection_profile_get_type(profileHandle, &profileType);
602                 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
603                                 "[%s] A system error has been occurred. The return value from connection_profile_get_type() is %d",
604                         GetErrorMessage(E_SYSTEM), ret);
605
606                 if (profileType == CONNECTION_PROFILE_TYPE_CELLULAR)
607                 {
608                         connection_cellular_service_type_e serviceType = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
609
610                         ret = connection_profile_get_cellular_service_type(profileHandle, &serviceType);
611                         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
612                                         "[%s] A system error has been occurred. The return value from connection_profile_get_cellular_service_type() is %d",
613                                         GetErrorMessage(E_SYSTEM), ret);
614
615                         if (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION)
616                         {
617                                 ret = connection_profile_get_id(profileHandle, &pProfileName);
618                                 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
619                                                 "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
620                                                 GetErrorMessage(E_SYSTEM), ret);
621
622                                 unique_ptr<String> pProfileNameString(new (std::nothrow) String(pProfileName));
623                                 free(pProfileName);
624                                 SysTryReturn(NID_NET, pProfileNameString != null, null, E_OUT_OF_MEMORY,
625                                                 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
626
627                                 r = pList->Add(*pProfileNameString);
628                                 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
629                                 pProfileNameString.release();
630                         }
631                         else
632                         {
633                                 SysLog(NID_NET, "Ignore non-app profile.");
634                         }
635                 }
636                 else
637                 {
638                         SysLog(NID_NET, "Ignore non-cellular profile.");
639                 }
640
641                 profileHandle = null;
642                 ret = connection_profile_iterator_next(iterator, &profileHandle);
643         }
644
645         SysLog(NID_NET, "GetAppProfileNamesN() has been succeeded with profiles count:%d", pList->GetCount());
646
647         return pList.release();
648 }
649
650 IList*
651 _NetAccountManagerImpl::GetAllProfileNamesN(void)
652 {
653         ClearLastResult();
654
655         result r = E_SUCCESS;
656         int ret = CONNECTION_ERROR_NONE;
657         connection_h connectionHandle = null;
658         connection_profile_iterator_h iterator = null;
659         connection_profile_h profileHandle = null;
660         connection_profile_type_e profileType = CONNECTION_PROFILE_TYPE_WIFI;
661
662         ret = connection_create(&connectionHandle);
663         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
664                         "[%s] A system error has been occurred. The return value from connection_create() is %d",
665                         GetErrorMessage(E_SYSTEM), ret);
666         unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
667
668         unique_ptr<ArrayList, _CollectionDeleter> pList(new (std::nothrow) ArrayList());
669         SysTryReturn(NID_NET, pList != null, null, E_OUT_OF_MEMORY,
670                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
671
672         r = pList->Construct();
673         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
674
675         ret = connection_get_profile_iterator(connectionHandle, CONNECTION_ITERATOR_TYPE_REGISTERED, &iterator);
676         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
677                         "[%s] A system error has been occurred. The return value from connection_get_profile_iterator() is %d",
678                         GetErrorMessage(E_SYSTEM), ret);
679         unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
680
681         ret = connection_profile_iterator_next(iterator, &profileHandle);
682         while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
683         {
684                 ret = connection_profile_get_type(profileHandle, &profileType);
685                 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
686                                 "[%s] A system error has been occurred. The return value from connection_profile_get_type() is %d",
687                         GetErrorMessage(E_SYSTEM), ret);
688
689                 if (profileType == CONNECTION_PROFILE_TYPE_CELLULAR)
690                 {
691                         connection_cellular_service_type_e serviceType = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
692
693                         ret = connection_profile_get_cellular_service_type(profileHandle, &serviceType);
694                         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
695                                         "[%s] A system error has been occurred. The return value from connection_profile_get_cellular_service_type() is %d",
696                                         GetErrorMessage(E_SYSTEM), ret);
697
698                         if ((serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET)
699                                         || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
700                                         || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_MMS)
701                                         || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS)
702                                         || (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION))
703                         {
704                                 char* pProfileName = null;
705                                 char* pProfileDisplayName = null;
706                                 String profileName;
707                                 String profileDisplayName;
708
709                                 ret = connection_profile_get_id(profileHandle, &pProfileName);
710                                 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
711                                                 "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
712                                                 GetErrorMessage(E_SYSTEM), ret);
713                                 profileName = String(pProfileName);
714                                 free(pProfileName);
715
716                                 ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
717                                 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
718                                                 "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d",
719                                                 GetErrorMessage(E_SYSTEM), ret);
720                                 profileDisplayName = String(pProfileDisplayName);
721                                 free(pProfileDisplayName);
722
723                                 unique_ptr<String> pNetProfileName(new (std::nothrow) String(profileName));
724
725                                 SysLog(NID_NET, "Cellular profile is found: [%d][%ls][%ls].", serviceType, profileDisplayName.GetPointer(), profileName.GetPointer());
726
727                                 r = pList->Add(*pNetProfileName);
728                                 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
729                                 pNetProfileName.release();
730                         }
731                         else
732                         {
733                                 SysLog(NID_NET, "Ignore unknown profile.");
734                         }
735                 }
736                 else
737                 {
738                         SysLog(NID_NET, "Ignore non-cellular profile.");
739                 }
740
741                 profileHandle = null;
742                 ret = connection_profile_iterator_next(iterator, &profileHandle);
743         }
744
745         SysLog(NID_NET, "GetAllProfileNamesN() has been succeeded with profiles count:%d", pList->GetCount());
746
747         return pList.release();
748 }
749
750 NetAccountInfo*
751 _NetAccountManagerImpl::CreateWifiNetAccountInfoN(void* pProfileInfo)
752 {
753         void* pHandle = null;
754         NetAccountInfo* pWifiNetAccountInfo = null;
755         WifiNetAccountInfo*(*pFunction)(void* pProfileInfo) = null;
756
757         pHandle = dlopen(_WIFI_LIBRARY_NAME, RTLD_LAZY);
758         SysTryReturn(NID_NET, pHandle != null, null, E_SYSTEM,
759                         "[%s] A system error has been occurred. Failed to open wifi library.");
760
761         pFunction = reinterpret_cast<WifiNetAccountInfo*(*)(void* pProfileInfo)>(dlsym(pHandle, "_WifiNetAccountInfoImpl_CreateWifiNetAccountInfoN"));
762         SysTryCatch(NID_NET, pFunction != null, , E_SYSTEM,
763                         "[%s] A system error has been occurred. Failed to find a symbol.", GetErrorMessage(E_SYSTEM));
764
765         pWifiNetAccountInfo = pFunction(pProfileInfo);
766         SysTryCatch(NID_NET, pWifiNetAccountInfo != null, , E_SYSTEM,
767                         "[%s] A system error has been occurred. Failed to create the instance.", GetErrorMessage(E_SYSTEM));
768
769         dlclose(pHandle);
770
771         return pWifiNetAccountInfo;
772
773 CATCH:
774         dlclose(pHandle);
775
776         return null;
777 }
778
779 NetAccountId
780 _NetAccountManagerImpl::GetInternetAccountId(void)
781 {
782         result r = E_SUCCESS;
783         NetAccountId accountId = INVALID_HANDLE;
784
785         int ret = CONNECTION_ERROR_NONE;
786         connection_h connectionHandle = null;
787         connection_profile_h profileHandle = null;
788
789         _NetAccountManagerImpl::UpdateNetAccountDb();
790
791         ret = connection_create(&connectionHandle);
792         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
793                         "[%s] A system error has been occurred. The return value from connection_create() is %d",
794                         GetErrorMessage(E_SYSTEM), ret);
795         unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
796
797         ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET, &profileHandle);
798         if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null))
799         {
800                 SysLog(NID_NET, "Internet profile is not found, so search a prepaid-Internet profile.");
801
802                 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET, &profileHandle);
803         }
804         SysTryReturn(NID_NET, profileHandle != null, INVALID_HANDLE, E_INVALID_ACCOUNT,
805                         "[%s] Internet(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
806         unique_ptr<void, _ProfileDeleter> pProfileHandle(profileHandle);
807
808         char* pProfileName = null;
809         char* pProfileDisplayName = null;
810         String profileName;
811         String profileDisplayName;
812
813         ret = connection_profile_get_id(profileHandle, &pProfileName);
814         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
815                         "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
816                         GetErrorMessage(E_SYSTEM), ret);
817
818         profileName = String(pProfileName);
819         free(pProfileName);
820
821         ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
822         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
823                         "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d",
824                         GetErrorMessage(E_SYSTEM), ret);
825
826         profileDisplayName = String(pProfileDisplayName);
827         free(pProfileDisplayName);
828
829         SysLog(NID_NET, "Internet profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer());
830
831         Mutex* pLock = _NetUtility::GetLock();
832         MutexGuard locked(*pLock);
833
834         if (__pInternetProfileName != null)
835         {
836                 if (!profileName.Equals(*__pInternetProfileName, false))
837                 {
838                         // Internet profile is changed.
839                         SysLog(NID_NET, "Internet profile is changed. [%ls]->[%ls]", __pInternetProfileName->GetPointer(), profileName.GetPointer());
840
841                         _NetAccountManagerImpl::UpdateNetAccountDb(true);
842                         r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
843
844                         delete __pInternetProfileName;
845                         __pInternetProfileName = new (std::nothrow) String(profileName);
846                         __internetAccountId = accountId;
847                 }
848                 else
849                 {
850                         accountId = __internetAccountId;
851                 }
852         }
853         else
854         {
855                 SysLog(NID_NET, "Initialize Internet profile.");
856
857                 _NetAccountManagerImpl::UpdateNetAccountDb(true);
858                 r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
859
860                 __pInternetProfileName = new (std::nothrow) String(profileName);
861                 __internetAccountId = accountId;
862         }
863
864         locked.Unlock();
865
866         ClearLastResult();
867
868         return accountId;
869 }
870
871 NetAccountId
872 _NetAccountManagerImpl::GetMmsAccountId(void)
873 {
874         result r = E_SUCCESS;
875         NetAccountId accountId = INVALID_HANDLE;
876
877         int ret = CONNECTION_ERROR_NONE;
878         connection_h connectionHandle = null;
879         connection_profile_h profileHandle = null;
880
881         _NetAccountManagerImpl::UpdateNetAccountDb();
882
883         ret = connection_create(&connectionHandle);
884         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
885                         "[%s] A system error has been occurred. The return value from connection_create() is %d",
886                         GetErrorMessage(E_SYSTEM), ret);
887         unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
888
889         ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profileHandle);
890         if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null))
891         {
892                 SysLog(NID_NET, "MMS profile is not found, so search a prepaid-MMS profile.");
893
894                 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS, &profileHandle);
895         }
896         SysTryReturn(NID_NET, profileHandle != null, INVALID_HANDLE, E_INVALID_ACCOUNT,
897                         "[%s] MMS(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
898         unique_ptr<void, _ProfileDeleter> pProfileHandle(profileHandle);
899
900         char* pProfileName = null;
901         char* pProfileDisplayName = null;
902         String profileName;
903         String profileDisplayName;
904
905         ret = connection_profile_get_id(profileHandle, &pProfileName);
906         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
907                         "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d",
908                         GetErrorMessage(E_SYSTEM), ret);
909
910         profileName = String(pProfileName);
911         free(pProfileName);
912
913         ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
914         SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
915                         "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d",
916                         GetErrorMessage(E_SYSTEM), ret);
917
918         profileDisplayName = String(pProfileDisplayName);
919         free(pProfileDisplayName);
920
921         SysLog(NID_NET, "MMS profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer());
922
923         Mutex* pLock = _NetUtility::GetLock();
924         MutexGuard locked(*pLock);
925
926         if (__pMmsProfileName != null)
927         {
928                 if (!profileName.Equals(*__pMmsProfileName, false))
929                 {
930                         // MMS profile is changed.
931                         SysLog(NID_NET, "MMS profile is changed. [%ls]->[%ls]", __pMmsProfileName->GetPointer(), profileName.GetPointer());
932
933                         _NetAccountManagerImpl::UpdateNetAccountDb(true);
934                         r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
935
936                         delete __pMmsProfileName;
937                         __pMmsProfileName = new (std::nothrow) String(profileName);
938                         __mmsAccountId = accountId;
939                 }
940                 else
941                 {
942                         accountId = __mmsAccountId;
943                 }
944         }
945         else
946         {
947                 SysLog(NID_NET, "Initialize MMS profile.");
948
949                 _NetAccountManagerImpl::UpdateNetAccountDb(true);
950                 r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
951
952                 __pMmsProfileName = new (std::nothrow) String(profileName);
953                 __mmsAccountId = accountId;
954         }
955
956         locked.Unlock();
957
958         ClearLastResult();
959
960         return accountId;
961 }
962
963 void
964 _NetAccountManagerImpl::UpdateNetAccountDb(bool force)
965 {
966         if (__isNetAccountDbInitialized && !force)
967         {
968                 // Already done
969                 return;
970         }
971
972         Mutex* pLock = _NetUtility::GetLock();
973         MutexGuard locked(*pLock);
974
975         result r = E_SUCCESS;
976         NetAccountId accountId = INVALID_HANDLE;
977         unique_ptr<IList, _CollectionDeleter> pAccountList(_NetAccountDatabase::GetProfileNamesN());
978         unique_ptr<IList, _CollectionDeleter> pProfileList(_NetAccountManagerImpl::GetAllProfileNamesN());
979
980         if (pProfileList != null)
981         {
982                 if (pProfileList->GetCount() > 0)
983                 {
984                         SysLog(NID_NET, "[%d] profiles are found.", pProfileList->GetCount());
985
986                         unique_ptr<IEnumerator> pEnum(pProfileList->GetEnumeratorN());
987                         if (pEnum != null)
988                         {
989                                 String* pProfileName = null;
990                                 while (pEnum->MoveNext() == E_SUCCESS)
991                                 {
992                                         pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
993                                         if (pProfileName != null)
994                                         {
995                                                 if (pAccountList->Contains(*pProfileName))
996                                                 {
997                                                         SysLog(NID_NET, "ProfileName[%ls] is already on DB.", pProfileName->GetPointer());
998                                                         pAccountList->Remove(*pProfileName, true);
999                                                 }
1000                                                 else
1001                                                 {
1002                                                         SysLog(NID_NET, "ProfileName[%ls] is not found on DB, so add it.", pProfileName->GetPointer());
1003                                                         r = _NetAccountDatabase::AddAccount(*pProfileName, *pProfileName, _NET_ACCOUNT_OWNER_OTHER, accountId);
1004                                                 }
1005                                         }
1006                                 }
1007                         }
1008                 }
1009         }
1010
1011         if (pAccountList != null)
1012         {
1013                 if (pAccountList->GetCount() > 0)
1014                 {
1015                         SysLog(NID_NET, "[%d] zombie accounts are on DB.", pAccountList->GetCount());
1016
1017                         unique_ptr<IEnumerator> pEnum(pAccountList->GetEnumeratorN());
1018                         if (pEnum != null)
1019                         {
1020                                 String* pProfileName = null;
1021                                 while (pEnum->MoveNext() == E_SUCCESS)
1022                                 {
1023                                         pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
1024                                         if (pProfileName != null)
1025                                         {
1026                                                 r = _NetAccountDatabase::RemoveAccountByProfileName(*pProfileName);
1027                                         }
1028                                 }
1029                         }
1030                 }
1031         }
1032
1033         __isNetAccountDbInitialized = true;
1034
1035         ClearLastResult();
1036
1037         locked.Unlock();
1038
1039         SysLog(NID_NET, "Network account database is updated.");
1040 }
1041
1042 } } // Tizen::Net