2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
18 * @file FNetNetAccountManager.cpp
19 * @brief This is the implementation for the _NetAccountManagerImpl class.
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"
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Runtime;
43 using namespace Tizen::Net::Wifi;
45 namespace Tizen { namespace Net {
47 static const char _WIFI_LIBRARY_NAME[] = "libosp-wifi.so";
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;
55 _NetAccountManagerImpl::_NetAccountManagerImpl(void)
56 : __pConnectionHandle(null)
60 _NetAccountManagerImpl::~_NetAccountManagerImpl(void)
65 _NetAccountManagerImpl::Construct(void)
68 int ret = CONNECTION_ERROR_NONE;
69 connection_h connectionHandle = null;
71 SysAssertf(__pConnectionHandle == null,
72 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
74 _SystemNetConnection::InitializeNetworkFramework();
75 _NetAccountManagerImpl::UpdateNetAccountDb();
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);
81 __pConnectionHandle.reset(connectionHandle);
87 _NetAccountManagerImpl::CreateNetAccount(NetAccountInfo& netAccountInfo)
90 _NetAccountInfoImpl* pInfoImpl = _NetAccountInfoImpl::GetInstance(netAccountInfo);
91 NetAccountId accountId = INVALID_HANDLE;
93 bool isCreated = false;
94 int ret = CONNECTION_ERROR_NONE;
95 connection_h connectionHandle = __pConnectionHandle.get();
96 connection_profile_h profileHandle = null;
98 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
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));
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));
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));
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);
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));
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);
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));
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));
136 while (pEnum->MoveNext() == E_SUCCESS)
138 String* pProfileName = null;
140 pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
141 if ((pProfileName != null) && (!pProfileName->IsEmpty()))
143 if (!pProfileNamesBefore->Contains(*pProfileName))
145 SysLog(NID_NET, "ProfileName[%ls] is not found before, so it is a new profile name.", pProfileName->GetPointer());
147 profileName = *pProfileName;
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));
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));
162 r = pInfoImpl->SetAccountId(accountId);
163 SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
167 SysSecureLog( NID_NET, "CreateNetAccount() has been succeeded with accountId:%d, accountName:%ls, profileName:%ls",
168 netAccountInfo.GetAccountId(), netAccountInfo.GetAccountName().GetPointer(), profileName.GetPointer());
174 _NetAccountManagerImpl::DeleteNetAccount(NetAccountId netAccountId)
176 result r = E_SUCCESS;
178 bool isReadOnly = true;
179 int ret = CONNECTION_ERROR_NONE;
180 connection_h connectionHandle = __pConnectionHandle.get();
182 SysLog(NID_NET, "DeleteNetAccount() has been called with accountId:%d", netAccountId);
184 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
186 SysTryReturnResult(NID_NET, netAccountId > 0, E_INVALID_ACCOUNT,
187 "Invalid network account. accountId=%d", netAccountId);
189 isReadOnly = _NetAccountDatabase::IsReadOnly(netAccountId);
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.");
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);
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);
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);
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.");
215 _NetAccountManagerImpl::UpdateNetAccount(const NetAccountInfo& netAccountInfo)
217 result r = E_SUCCESS;
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();
224 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
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.");
233 isReadOnly = _NetAccountDatabase::IsReadOnly(netAccountInfo.GetAccountId());
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.");
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());
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());
248 r = pInfoImpl->ConvertToProfileInfo(pProfileHandle.get());
249 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
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);
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.");
265 _NetAccountManagerImpl::GetNetAccountInfoN(NetAccountId netAccountId) const
267 result r = E_SUCCESS;
268 NetAccountInfo* pInfo = null;
269 connection_profile_h profileHandle = null;
270 unique_ptr<void, _ProfileDeleter> pProfileHandle;
272 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
274 SysTryReturn(NID_NET, netAccountId > 0, null, E_INVALID_ACCOUNT,
275 "[%s] Invalid network account. accountId=%d.", GetErrorMessage(E_INVALID_ACCOUNT), netAccountId);
277 if (netAccountId == _DEFAULT_WIFI_ACCOUNT_ID)
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);
284 pInfo = _NetAccountManagerImpl::CreateWifiNetAccountInfoN(
287 SysTryReturn(NID_NET, pInfo != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
289 else if (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
291 SysLogException(NID_NET, E_INVALID_ACCOUNT,
292 "[%s] Wifi direct account information is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
296 else if (netAccountId == _DEFAULT_USB_ACCOUNT_ID)
298 SysLogException(NID_NET, E_INVALID_ACCOUNT,
299 "[%s] USB account information is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
307 r = _NetAccountDatabase::GetProfileName(netAccountId, profileName);
308 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
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);
315 pInfo = _NetAccountInfoImpl::CreateNetAccountInfoN(profileHandle);
317 SysTryReturn(NID_NET, pInfo != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
325 IListT<NetAccountId>*
326 _NetAccountManagerImpl::GetNetAccountIdsN(void) const
328 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
330 _NetAccountManagerImpl::UpdateNetAccountDb(true);
334 return _NetAccountDatabase::GetAccountIdsN();
338 _NetAccountManagerImpl::GetNetAccountNamesN(void) const
340 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
342 _NetAccountManagerImpl::UpdateNetAccountDb(true);
346 return _NetAccountDatabase::GetAccountNamesN();
350 _NetAccountManagerImpl::GetNetAccountId(const String& netAccountName) const
354 result r = E_SUCCESS;
355 NetAccountId accountId = INVALID_HANDLE;
357 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
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());
364 SysSecureLog(NID_NET, "GetNetAccountId() has been succeeded with accountId:%d", accountId);
370 _NetAccountManagerImpl::GetNetAccountId(NetBearerType netBearerType) const
374 NetAccountId accountId = INVALID_HANDLE;
376 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
378 if (netBearerType == NET_BEARER_PS)
380 accountId = _NetAccountManagerImpl::GetInternetAccountId();
382 else if (netBearerType == NET_BEARER_MMS)
384 accountId = _NetAccountManagerImpl::GetMmsAccountId();
386 else if (netBearerType == NET_BEARER_WIFI)
388 accountId = _DEFAULT_WIFI_ACCOUNT_ID;
390 else if (netBearerType == NET_BEARER_WIFI_DIRECT)
392 accountId = _DEFAULT_WIFI_DIRECT_ACCOUNT_ID;
394 else if (netBearerType == NET_BEARER_USB)
396 accountId = _DEFAULT_USB_ACCOUNT_ID;
400 SysLogException(NID_NET, E_INVALID_ARG,
401 "[%s] Invalid argument is used. bearerType=%d", GetErrorMessage(E_INVALID_ARG), netBearerType);
408 _NetAccountManagerImpl::GetNetPreference(void) const
410 _NetConnectionManagerImpl* pConnectionManager = _NetConnectionManagerImpl::GetInstance();
412 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
414 SysTryReturn(NID_NET, pConnectionManager != null, NET_WIFI_FIRST, E_SYSTEM,
415 "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
417 return pConnectionManager->GetNetPreference();
421 _NetAccountManagerImpl::SetNetPreference(NetPreferenceType netPreference)
423 result r = E_SUCCESS;
425 _NetConnectionManagerImpl* pConnectionManager = _NetConnectionManagerImpl::GetInstance();
427 SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use.");
429 SysTryReturnResult(NID_NET, pConnectionManager != null, E_SYSTEM, "A system error has been occurred.");
431 r = pConnectionManager->SetNetPreference(netPreference);
432 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
437 _NetAccountManagerImpl*
438 _NetAccountManagerImpl::GetInstance(NetAccountManager& netAccountManager)
440 return netAccountManager.__pNetAccountManagerImpl;
443 const _NetAccountManagerImpl*
444 _NetAccountManagerImpl::GetInstance(const NetAccountManager& netAccountManager)
446 return netAccountManager.__pNetAccountManagerImpl;
450 _NetAccountManagerImpl::GetPsProfileHandleN(const String& profileName)
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;
460 SysLog(NID_NET, "GetPsProfileHandleN() has been called with profileName:%ls", profileName.GetPointer());
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));
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);
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);
477 ret = connection_profile_iterator_next(iterator, &profileHandle);
478 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
480 char* pTempProfileName = null;
481 String tempProfileName;
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);
488 tempProfileName = String(pTempProfileName);
489 free(pTempProfileName);
491 if (profileName.Equals(tempProfileName))
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);
501 profileHandle = null;
502 ret = connection_profile_iterator_next(iterator, &profileHandle);
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());
508 return matchingProfileHandle;
512 _NetAccountManagerImpl::GetWifiProfileHandleN(void)
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;
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);
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);
535 ret = connection_profile_iterator_next(iterator, &profileHandle);
536 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
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);
543 if (profileType == CONNECTION_PROFILE_TYPE_WIFI)
545 SysLog(NID_NET, "Connected WiFi profile is found.");
546 ret = connection_profile_clone(&matchingProfileHandle,
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);
555 profileHandle = null;
556 ret = connection_profile_iterator_next(iterator, &profileHandle);
559 SysTryReturn(NID_NET, matchingProfileHandle != null, null, E_INVALID_ACCOUNT,
560 "[%s] Active Wi-Fi profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT));
562 return matchingProfileHandle;
566 _NetAccountManagerImpl::GetAppProfileNamesN(void)
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;
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);
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));
587 r = pList->Construct();
588 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
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);
596 ret = connection_profile_iterator_next(iterator, &profileHandle);
597 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
599 char* pProfileName = null;
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);
606 if (profileType == CONNECTION_PROFILE_TYPE_CELLULAR)
608 connection_cellular_service_type_e serviceType = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
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);
615 if (serviceType == CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION)
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);
622 unique_ptr<String> pProfileNameString(new (std::nothrow) String(pProfileName));
624 SysTryReturn(NID_NET, pProfileNameString != null, null, E_OUT_OF_MEMORY,
625 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
627 r = pList->Add(*pProfileNameString);
628 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
629 pProfileNameString.release();
633 SysLog(NID_NET, "Ignore non-app profile.");
638 SysLog(NID_NET, "Ignore non-cellular profile.");
641 profileHandle = null;
642 ret = connection_profile_iterator_next(iterator, &profileHandle);
645 SysLog(NID_NET, "GetAppProfileNamesN() has been succeeded with profiles count:%d", pList->GetCount());
647 return pList.release();
651 _NetAccountManagerImpl::GetAllProfileNamesN(void)
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;
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);
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));
672 r = pList->Construct();
673 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
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);
681 ret = connection_profile_iterator_next(iterator, &profileHandle);
682 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
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);
689 if (profileType == CONNECTION_PROFILE_TYPE_CELLULAR)
691 connection_cellular_service_type_e serviceType = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
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);
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))
704 char* pProfileName = null;
705 char* pProfileDisplayName = null;
707 String profileDisplayName;
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);
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);
723 unique_ptr<String> pNetProfileName(new (std::nothrow) String(profileName));
725 SysLog(NID_NET, "Cellular profile is found: [%d][%ls][%ls].", serviceType, profileDisplayName.GetPointer(), profileName.GetPointer());
727 r = pList->Add(*pNetProfileName);
728 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
729 pNetProfileName.release();
733 SysLog(NID_NET, "Ignore unknown profile.");
738 SysLog(NID_NET, "Ignore non-cellular profile.");
741 profileHandle = null;
742 ret = connection_profile_iterator_next(iterator, &profileHandle);
745 SysLog(NID_NET, "GetAllProfileNamesN() has been succeeded with profiles count:%d", pList->GetCount());
747 return pList.release();
751 _NetAccountManagerImpl::CreateWifiNetAccountInfoN(void* pProfileInfo)
753 void* pHandle = null;
754 NetAccountInfo* pWifiNetAccountInfo = null;
755 WifiNetAccountInfo*(*pFunction)(void* pProfileInfo) = null;
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.");
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));
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));
771 return pWifiNetAccountInfo;
780 _NetAccountManagerImpl::GetInternetAccountId(void)
782 result r = E_SUCCESS;
783 NetAccountId accountId = INVALID_HANDLE;
785 int ret = CONNECTION_ERROR_NONE;
786 connection_h connectionHandle = null;
787 connection_profile_h profileHandle = null;
789 _NetAccountManagerImpl::UpdateNetAccountDb();
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);
797 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET, &profileHandle);
798 if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null))
800 SysLog(NID_NET, "Internet profile is not found, so search a prepaid-Internet profile.");
802 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET, &profileHandle);
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);
808 char* pProfileName = null;
809 char* pProfileDisplayName = null;
811 String profileDisplayName;
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);
818 profileName = String(pProfileName);
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);
826 profileDisplayName = String(pProfileDisplayName);
827 free(pProfileDisplayName);
829 SysLog(NID_NET, "Internet profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer());
831 Mutex* pLock = _NetUtility::GetLock();
832 MutexGuard locked(*pLock);
834 if (__pInternetProfileName != null)
836 if (!profileName.Equals(*__pInternetProfileName, false))
838 // Internet profile is changed.
839 SysLog(NID_NET, "Internet profile is changed. [%ls]->[%ls]", __pInternetProfileName->GetPointer(), profileName.GetPointer());
841 _NetAccountManagerImpl::UpdateNetAccountDb(true);
842 r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
844 delete __pInternetProfileName;
845 __pInternetProfileName = new (std::nothrow) String(profileName);
846 __internetAccountId = accountId;
850 accountId = __internetAccountId;
855 SysLog(NID_NET, "Initialize Internet profile.");
857 _NetAccountManagerImpl::UpdateNetAccountDb(true);
858 r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
860 __pInternetProfileName = new (std::nothrow) String(profileName);
861 __internetAccountId = accountId;
872 _NetAccountManagerImpl::GetMmsAccountId(void)
874 result r = E_SUCCESS;
875 NetAccountId accountId = INVALID_HANDLE;
877 int ret = CONNECTION_ERROR_NONE;
878 connection_h connectionHandle = null;
879 connection_profile_h profileHandle = null;
881 _NetAccountManagerImpl::UpdateNetAccountDb();
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);
889 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profileHandle);
890 if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null))
892 SysLog(NID_NET, "MMS profile is not found, so search a prepaid-MMS profile.");
894 ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS, &profileHandle);
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);
900 char* pProfileName = null;
901 char* pProfileDisplayName = null;
903 String profileDisplayName;
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);
910 profileName = String(pProfileName);
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);
918 profileDisplayName = String(pProfileDisplayName);
919 free(pProfileDisplayName);
921 SysLog(NID_NET, "MMS profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer());
923 Mutex* pLock = _NetUtility::GetLock();
924 MutexGuard locked(*pLock);
926 if (__pMmsProfileName != null)
928 if (!profileName.Equals(*__pMmsProfileName, false))
930 // MMS profile is changed.
931 SysLog(NID_NET, "MMS profile is changed. [%ls]->[%ls]", __pMmsProfileName->GetPointer(), profileName.GetPointer());
933 _NetAccountManagerImpl::UpdateNetAccountDb(true);
934 r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
936 delete __pMmsProfileName;
937 __pMmsProfileName = new (std::nothrow) String(profileName);
938 __mmsAccountId = accountId;
942 accountId = __mmsAccountId;
947 SysLog(NID_NET, "Initialize MMS profile.");
949 _NetAccountManagerImpl::UpdateNetAccountDb(true);
950 r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId);
952 __pMmsProfileName = new (std::nothrow) String(profileName);
953 __mmsAccountId = accountId;
964 _NetAccountManagerImpl::UpdateNetAccountDb(bool force)
966 if (__isNetAccountDbInitialized && !force)
972 Mutex* pLock = _NetUtility::GetLock();
973 MutexGuard locked(*pLock);
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());
980 if (pProfileList != null)
982 if (pProfileList->GetCount() > 0)
984 SysLog(NID_NET, "[%d] profiles are found.", pProfileList->GetCount());
986 unique_ptr<IEnumerator> pEnum(pProfileList->GetEnumeratorN());
989 String* pProfileName = null;
990 while (pEnum->MoveNext() == E_SUCCESS)
992 pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
993 if (pProfileName != null)
995 if (pAccountList->Contains(*pProfileName))
997 SysLog(NID_NET, "ProfileName[%ls] is already on DB.", pProfileName->GetPointer());
998 pAccountList->Remove(*pProfileName, true);
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);
1011 if (pAccountList != null)
1013 if (pAccountList->GetCount() > 0)
1015 SysLog(NID_NET, "[%d] zombie accounts are on DB.", pAccountList->GetCount());
1017 unique_ptr<IEnumerator> pEnum(pAccountList->GetEnumeratorN());
1020 String* pProfileName = null;
1021 while (pEnum->MoveNext() == E_SUCCESS)
1023 pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
1024 if (pProfileName != null)
1026 r = _NetAccountDatabase::RemoveAccountByProfileName(*pProfileName);
1033 __isNetAccountDbInitialized = true;
1039 SysLog(NID_NET, "Network account database is updated.");