From 4a3a9ab78a982663d010dd1692ef929ce69b7f99 Mon Sep 17 00:00:00 2001 From: Jaemin Ahn Date: Thu, 2 May 2013 15:32:17 +0900 Subject: [PATCH] Update network account database when a default profile is changed. Change-Id: I034e4924ae0fa9a76b2b127bef9daab5dd6f988c Signed-off-by: Jaemin Ahn --- src/FNetNetAccountManager.cpp | 3 +- src/FNetNetAccountManagerPartner.cpp | 15 +- src/FNet_ManagedNetConnectionImpl.cpp | 7 +- src/FNet_NetAccountDatabase.cpp | 102 ++------ src/FNet_NetAccountDatabase.h | 4 - src/FNet_NetAccountManagerImpl.cpp | 435 ++++++++++++++++++++++------------ src/FNet_NetAccountManagerImpl.h | 29 +-- src/FNet_NetConnectionManagerImpl.cpp | 36 ++- src/FNet_SystemNetConnection.cpp | 151 ------------ src/inc/FNet_NetTypes.h | 2 +- 10 files changed, 359 insertions(+), 425 deletions(-) diff --git a/src/FNetNetAccountManager.cpp b/src/FNetNetAccountManager.cpp index 3572b39..a80109a 100644 --- a/src/FNetNetAccountManager.cpp +++ b/src/FNetNetAccountManager.cpp @@ -263,8 +263,7 @@ NetAccountManager::GetNetAccountId(NetBearerType netBearerType) const #endif // !_OSP_EMUL_ netAccountId = __pNetAccountManagerImpl->GetNetAccountId(netBearerType); - r = GetLastResult(); - SysTryReturn(NID_NET, netAccountId != INVALID_HANDLE, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r)); + SysTryReturn(NID_NET, netAccountId != INVALID_HANDLE, INVALID_HANDLE, E_SYSTEM, "[%s] Failed to get a network account.", GetErrorMessage(E_SYSTEM)); return netAccountId; } diff --git a/src/FNetNetAccountManagerPartner.cpp b/src/FNetNetAccountManagerPartner.cpp index 4b23c8d..fbd3eaf 100644 --- a/src/FNetNetAccountManagerPartner.cpp +++ b/src/FNetNetAccountManagerPartner.cpp @@ -49,7 +49,7 @@ NetAccountManager::UpdateSystemNetAccount(const NetAccountInfo& netAccountInfo) SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use."); #ifndef _OSP_EMUL_ - if (netAccountInfo.GetAccountId() == _DEFAULT_PS_ACCOUNT_ID) + if (bearerType == NET_BEARER_PS) { static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony"; @@ -58,7 +58,7 @@ NetAccountManager::UpdateSystemNetAccount(const NetAccountInfo& netAccountInfo) r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported); SysTryReturnResult(NID_NET, r == E_SUCCESS && isTelephonySupported, E_UNSUPPORTED_OPERATION, "Telephony is not supported."); } - else if (netAccountInfo.GetAccountId() == _DEFAULT_MMS_ACCOUNT_ID) + else if (bearerType == NET_BEARER_MMS) { static const wchar_t _MMS[] = L"http://tizen.org/feature/network.telephony.mms"; @@ -132,6 +132,17 @@ NetAccountManager::GetAppNetAccountId(const String& netProfileName) const r = pProxy->GetAppNetAccountId(netProfileName, netAccountId); SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r)); + if (netAccountId == _DEFAULT_PS_ACCOUNT_ID) + { + netAccountId = _NetAccountManagerImpl::GetInternetAccountId(); + } + else if (netAccountId == _DEFAULT_MMS_ACCOUNT_ID) + { + netAccountId = _NetAccountManagerImpl::GetMmsAccountId(); + } + + ClearLastResult(); + return netAccountId; } diff --git a/src/FNet_ManagedNetConnectionImpl.cpp b/src/FNet_ManagedNetConnectionImpl.cpp index d1c01c4..b5004b1 100644 --- a/src/FNet_ManagedNetConnectionImpl.cpp +++ b/src/FNet_ManagedNetConnectionImpl.cpp @@ -45,8 +45,11 @@ _ManagedNetConnectionImpl::~_ManagedNetConnectionImpl(void) if (__pListener != null) { - r = pManager->RemoveManagedNetConnectionEventListener(*__pListener); - __pListener = null; + if (pManager != null) + { + r = pManager->RemoveManagedNetConnectionEventListener(*__pListener); + __pListener = null; + } } if (__isRefAdded) diff --git a/src/FNet_NetAccountDatabase.cpp b/src/FNet_NetAccountDatabase.cpp index eac1966..38c57f4 100644 --- a/src/FNet_NetAccountDatabase.cpp +++ b/src/FNet_NetAccountDatabase.cpp @@ -102,41 +102,27 @@ _NetAccountDatabase::AddAccount(const String& accountName, const String& profile isReadOnly = true; } - if (owner == _NET_ACCOUNT_OWNER_SYSTEM_INTERNET) + pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_MAX_ACCOUNT_ID_STATEMENT))); + r = GetLastResult(); + SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating."); + + pEnum.reset(accountDb.ExecuteStatementN(*pStmt)); + if (pEnum != null) { - netAccountId = _DEFAULT_PS_ACCOUNT_ID; + while (pEnum->MoveNext() == E_SUCCESS) + { + pEnum->GetIntAt(0, netAccountId); + break; + } } - else if (owner == _NET_ACCOUNT_OWNER_SYSTEM_MMS) + + if (netAccountId >= _PS_ACCOUNT_ID_START) { - netAccountId = _DEFAULT_MMS_ACCOUNT_ID; + netAccountId++; } else { - pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_MAX_ACCOUNT_ID_STATEMENT))); - r = GetLastResult(); - SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating."); - - pEnum.reset(accountDb.ExecuteStatementN(*pStmt)); - if (pEnum != null) - { - while (pEnum->MoveNext() == E_SUCCESS) - { - pEnum->GetIntAt(0, netAccountId); - break; - } - } - - SysTryReturnResult(NID_NET, netAccountId >= 0, E_SYSTEM, - "[%s] A system error has been occurred. Failed to allocate a new accountId.", GetErrorMessage(E_SYSTEM)); - - if (netAccountId >= _CUSTOM_ACCOUNT_ID_START) - { - netAccountId++; - } - else - { - netAccountId = _CUSTOM_ACCOUNT_ID_START; - } + netAccountId = _PS_ACCOUNT_ID_START; } pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_ADD_ACCOUNT_STATEMENT))); @@ -416,62 +402,6 @@ _NetAccountDatabase::GetProfileNamesN(void) return pList.release(); } -IList* -_NetAccountDatabase::GetAppProfileNamesN(void) -{ - static const wchar_t _NET_ACCOUNT_DATABASE_GET_PROFILE_NAMES_STATEMENT[] = - L"SELECT profileName FROM NetAccountTable WHERE accountId>=?"; - - result r = E_SUCCESS; - Database accountDb; - unique_ptr pStmt; - unique_ptr pEnum; - unique_ptr pList; - - r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false); - SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_PROFILE_NAMES_STATEMENT))); - r = GetLastResult(); - SysTryReturn(NID_NET, pStmt != null, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - r = pStmt->BindInt(0, _CUSTOM_ACCOUNT_ID_START); - SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - pList.reset(new (std::nothrow) ArrayList()); - r = GetLastResult(); - SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - r = pList->Construct(); - SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - pEnum.reset(accountDb.ExecuteStatementN(*pStmt)); - r = GetLastResult(); - SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - if (pEnum != null) - { - while (pEnum->MoveNext() == E_SUCCESS) - { - unique_ptr pProfileName(new (std::nothrow) String()); - SysTryReturn(NID_NET, pProfileName != null, null, E_OUT_OF_MEMORY, - "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); - - r = pEnum->GetStringAt(0, *pProfileName); - SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - r = pList->Add(*pProfileName); - SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - - pProfileName.release(); - } - } - - ClearLastResult(); // To suppress E_OUT_OF_RANGE - - return pList.release(); -} - result _NetAccountDatabase::GetAccountName(NetAccountId accountId, String& accountName) { @@ -684,7 +614,7 @@ String _NetAccountDatabase::GetDbPath(void) { static const wchar_t _OLD_DATA_PATH[] = L"/Home/"; - static const wchar_t _NET_ACCOUNT_DATABASE_FILE_NAME[] = L"netAccount.db"; + static const wchar_t _NET_ACCOUNT_DATABASE_FILE_NAME[] = L".tizen_netaccount.db"; String dbPath; _ApiVersion apiVersion = _AppInfo::GetApiVersion(); diff --git a/src/FNet_NetAccountDatabase.h b/src/FNet_NetAccountDatabase.h index 21e7e73..f207b43 100644 --- a/src/FNet_NetAccountDatabase.h +++ b/src/FNet_NetAccountDatabase.h @@ -42,9 +42,6 @@ namespace Tizen { namespace Net { enum _NetAccountOwner { - _NET_ACCOUNT_OWNER_SYSTEM_INTERNET, - _NET_ACCOUNT_OWNER_SYSTEM_MMS, - _NET_ACCOUNT_OWNER_SYSTEM, _NET_ACCOUNT_OWNER_OTHER, _NET_ACCOUNT_OWNER_THIS }; @@ -67,7 +64,6 @@ public: static Tizen::Base::Collection::IListT* GetAccountIdsN(void); static Tizen::Base::Collection::IList* GetAccountNamesN(void); static Tizen::Base::Collection::IList* GetProfileNamesN(void); - static Tizen::Base::Collection::IList* GetAppProfileNamesN(void); static result GetAccountName(NetAccountId accountId, Tizen::Base::String& accountName); static result GetProfileName(NetAccountId accountId, Tizen::Base::String& profileName); static result GetAccountIdByAccountName(const Tizen::Base::String& accountName, NetAccountId& accountId); diff --git a/src/FNet_NetAccountManagerImpl.cpp b/src/FNet_NetAccountManagerImpl.cpp index 2cbbf15..b9b2a11 100644 --- a/src/FNet_NetAccountManagerImpl.cpp +++ b/src/FNet_NetAccountManagerImpl.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "FNet_NetTypes.h" #include "FNet_NetAccountInfoImpl.h" @@ -32,42 +33,24 @@ #include "FNet_NetConnectionManagerImpl.h" #include "FNet_SystemNetConnection.h" #include "FNet_NetAccountDatabase.h" +#include "FNet_NetUtility.h" #include "FNetWifi_WifiNetAccountInfoImpl.h" using namespace std; using namespace Tizen::Base; using namespace Tizen::Base::Collection; +using namespace Tizen::Base::Runtime; using namespace Tizen::Net::Wifi; namespace Tizen { namespace Net { static const char _WIFI_LIBRARY_NAME[] = "libosp-wifi.so"; -_NetProfileInfo::_NetProfileInfo(void) -{ -} - -_NetProfileInfo::_NetProfileInfo(const String& profileName, const String& profileDisplayName) - : __profileName(profileName) - , __profileDisplayName(profileDisplayName) -{ -} - -_NetProfileInfo::~_NetProfileInfo(void) -{ -} - -String -_NetProfileInfo::GetProfileName(void) const -{ - return __profileName; -} - -String -_NetProfileInfo::GetProfileDisplayName(void) const -{ - return __profileDisplayName; -} +bool _NetAccountManagerImpl::__isNetAccountDbInitialized = false; +NetAccountId _NetAccountManagerImpl::__internetAccountId = INVALID_HANDLE; +NetAccountId _NetAccountManagerImpl::__mmsAccountId = INVALID_HANDLE; +String* _NetAccountManagerImpl::__pInternetProfileName = null; +String* _NetAccountManagerImpl::__pMmsProfileName = null; _NetAccountManagerImpl::_NetAccountManagerImpl(void) : __pConnectionHandle(null) @@ -89,6 +72,7 @@ _NetAccountManagerImpl::Construct(void) "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class."); _SystemNetConnection::InitializeNetworkFramework(); + _NetAccountManagerImpl::UpdateNetAccountDb(); ret = connection_create(&connectionHandle); SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM, @@ -341,20 +325,24 @@ _NetAccountManagerImpl::GetNetAccountInfoN(NetAccountId netAccountId) const IListT* _NetAccountManagerImpl::GetNetAccountIdsN(void) const { - ClearLastResult(); - SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use."); + _NetAccountManagerImpl::UpdateNetAccountDb(true); + + ClearLastResult(); + return _NetAccountDatabase::GetAccountIdsN(); } IList* _NetAccountManagerImpl::GetNetAccountNamesN(void) const { - ClearLastResult(); - SysAssertf(__pConnectionHandle != null, "Not yet constructed. Construct() should be called before use."); + _NetAccountManagerImpl::UpdateNetAccountDb(true); + + ClearLastResult(); + return _NetAccountDatabase::GetAccountNamesN(); } @@ -389,11 +377,11 @@ _NetAccountManagerImpl::GetNetAccountId(NetBearerType netBearerType) const if (netBearerType == NET_BEARER_PS) { - accountId = _DEFAULT_PS_ACCOUNT_ID; + accountId = _NetAccountManagerImpl::GetInternetAccountId(); } else if (netBearerType == NET_BEARER_MMS) { - accountId = _DEFAULT_MMS_ACCOUNT_ID; + accountId = _NetAccountManagerImpl::GetMmsAccountId(); } else if (netBearerType == NET_BEARER_WIFI) { @@ -574,116 +562,6 @@ _NetAccountManagerImpl::GetWifiProfileHandleN(void) return matchingProfileHandle; } -_NetProfileInfo -_NetAccountManagerImpl::GetInternetProfileInfo(void) -{ - ClearLastResult(); - - _NetProfileInfo profileInfo; - int ret = CONNECTION_ERROR_NONE; - connection_h connectionHandle = null; - connection_profile_h profileHandle = null; - - ret = connection_create(&connectionHandle); - SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, profileInfo, E_SYSTEM, - "[%s] A system error has been occurred. The return value from connection_create() is %d", - GetErrorMessage(E_SYSTEM), ret); - unique_ptr pConnectionHandle(connectionHandle); - - ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET, &profileHandle); - if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null)) - { - SysLog(NID_NET, "Internet profile is not found, so search a prepaid-Internet profile."); - - ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET, &profileHandle); - } - SysTryReturn(NID_NET, profileHandle != null, profileInfo, E_INVALID_ACCOUNT, - "[%s] Internet(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT)); - unique_ptr pProfileHandle(profileHandle); - - char* pProfileName = null; - char* pProfileDisplayName = null; - String profileName; - String profileDisplayName; - - ret = connection_profile_get_id(profileHandle, &pProfileName); - SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, profileInfo, E_SYSTEM, - "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d", - GetErrorMessage(E_SYSTEM), ret); - - profileName = String(pProfileName); - free(pProfileName); - - ret = connection_profile_get_name(profileHandle, &pProfileDisplayName); - SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, profileInfo, E_SYSTEM, - "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d", - GetErrorMessage(E_SYSTEM), ret); - - profileDisplayName = String(pProfileDisplayName); - free(pProfileDisplayName); - - profileInfo = _NetProfileInfo(profileName, profileDisplayName); - - SysLog(NID_NET, "GetInternetProfileInfo() has been succeeded with profile[%ls][%ls]", profileInfo.GetProfileDisplayName().GetPointer(), profileInfo.GetProfileName().GetPointer()); - - return profileInfo; -} - -_NetProfileInfo -_NetAccountManagerImpl::GetMmsProfileInfo(void) -{ - ClearLastResult(); - - _NetProfileInfo profileInfo; - int ret = CONNECTION_ERROR_NONE; - connection_h connectionHandle = null; - connection_profile_h profileHandle = null; - - ret = connection_create(&connectionHandle); - SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, profileInfo, E_SYSTEM, - "[%s] A system error has been occurred. The return value from connection_create() is %d", - GetErrorMessage(E_SYSTEM), ret); - unique_ptr pConnectionHandle(connectionHandle); - - ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profileHandle); - if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null)) - { - SysLog(NID_NET, "MMS profile is not found, so search a prepaid-MMS profile."); - - ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS, &profileHandle); - } - SysTryReturn(NID_NET, profileHandle != null, profileInfo, E_INVALID_ACCOUNT, - "[%s] MMS(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT)); - unique_ptr pProfileHandle(profileHandle); - - char* pProfileName = null; - char* pProfileDisplayName = null; - String profileName; - String profileDisplayName; - - ret = connection_profile_get_id(profileHandle, &pProfileName); - SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, profileInfo, E_SYSTEM, - "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d", - GetErrorMessage(E_SYSTEM), ret); - - profileName = String(pProfileName); - free(pProfileName); - - ret = connection_profile_get_name(profileHandle, &pProfileDisplayName); - SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, profileInfo, E_SYSTEM, - "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d", - GetErrorMessage(E_SYSTEM), ret); - - profileDisplayName = String(pProfileDisplayName); - free(pProfileDisplayName); - - profileInfo = _NetProfileInfo(profileName, profileDisplayName); - - SysLog(NID_NET, "GetMmsProfileInfo() has been succeeded with profile[%ls][%ls]", profileInfo.GetProfileDisplayName().GetPointer(), profileInfo.GetProfileName().GetPointer()); - - return profileInfo; -} - IList* _NetAccountManagerImpl::GetAppProfileNamesN(void) { @@ -770,7 +648,7 @@ _NetAccountManagerImpl::GetAppProfileNamesN(void) } IList* -_NetAccountManagerImpl::GetAllProfileInfosN(void) +_NetAccountManagerImpl::GetAllProfileNamesN(void) { ClearLastResult(); @@ -842,17 +720,17 @@ _NetAccountManagerImpl::GetAllProfileInfosN(void) profileDisplayName = String(pProfileDisplayName); free(pProfileDisplayName); - unique_ptr<_NetProfileInfo> pProfileInfo(new (std::nothrow) _NetProfileInfo(profileName, profileDisplayName)); + unique_ptr pNetProfileName(new (std::nothrow) String(profileName)); SysLog(NID_NET, "Cellular profile is found: [%d][%ls][%ls].", serviceType, profileDisplayName.GetPointer(), profileName.GetPointer()); - r = pList->Add(*pProfileInfo); + r = pList->Add(*pNetProfileName); SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r)); - pProfileInfo.release(); + pNetProfileName.release(); } else { - SysLog(NID_NET, "Ignore non-app profile."); + SysLog(NID_NET, "Ignore unknown profile."); } } else @@ -864,7 +742,7 @@ _NetAccountManagerImpl::GetAllProfileInfosN(void) ret = connection_profile_iterator_next(iterator, &profileHandle); } - SysLog(NID_NET, "GetAllProfileInfosN() has been succeeded with profiles count:%d", pList->GetCount()); + SysLog(NID_NET, "GetAllProfileNamesN() has been succeeded with profiles count:%d", pList->GetCount()); return pList.release(); } @@ -898,4 +776,267 @@ CATCH: return null; } +NetAccountId +_NetAccountManagerImpl::GetInternetAccountId(void) +{ + result r = E_SUCCESS; + NetAccountId accountId = INVALID_HANDLE; + + int ret = CONNECTION_ERROR_NONE; + connection_h connectionHandle = null; + connection_profile_h profileHandle = null; + + _NetAccountManagerImpl::UpdateNetAccountDb(); + + ret = connection_create(&connectionHandle); + SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM, + "[%s] A system error has been occurred. The return value from connection_create() is %d", + GetErrorMessage(E_SYSTEM), ret); + unique_ptr pConnectionHandle(connectionHandle); + + ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET, &profileHandle); + if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null)) + { + SysLog(NID_NET, "Internet profile is not found, so search a prepaid-Internet profile."); + + ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET, &profileHandle); + } + SysTryReturn(NID_NET, profileHandle != null, INVALID_HANDLE, E_INVALID_ACCOUNT, + "[%s] Internet(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT)); + unique_ptr pProfileHandle(profileHandle); + + char* pProfileName = null; + char* pProfileDisplayName = null; + String profileName; + String profileDisplayName; + + ret = connection_profile_get_id(profileHandle, &pProfileName); + SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM, + "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d", + GetErrorMessage(E_SYSTEM), ret); + + profileName = String(pProfileName); + free(pProfileName); + + ret = connection_profile_get_name(profileHandle, &pProfileDisplayName); + SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM, + "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d", + GetErrorMessage(E_SYSTEM), ret); + + profileDisplayName = String(pProfileDisplayName); + free(pProfileDisplayName); + + SysLog(NID_NET, "Internet profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer()); + + Mutex* pLock = _NetUtility::GetLock(); + MutexGuard locked(*pLock); + + if (__pInternetProfileName != null) + { + if (!profileName.Equals(*__pInternetProfileName, false)) + { + // Internet profile is changed. + SysLog(NID_NET, "Internet profile is changed. [%ls]->[%ls]", __pInternetProfileName->GetPointer(), profileName.GetPointer()); + + _NetAccountManagerImpl::UpdateNetAccountDb(true); + r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId); + + delete __pInternetProfileName; + __pInternetProfileName = new (std::nothrow) String(profileName); + __internetAccountId = accountId; + } + else + { + accountId = __internetAccountId; + } + } + else + { + SysLog(NID_NET, "Initialize Internet profile."); + + _NetAccountManagerImpl::UpdateNetAccountDb(true); + r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId); + + __pInternetProfileName = new (std::nothrow) String(profileName); + __internetAccountId = accountId; + } + + locked.Unlock(); + + ClearLastResult(); + + return accountId; +} + +NetAccountId +_NetAccountManagerImpl::GetMmsAccountId(void) +{ + result r = E_SUCCESS; + NetAccountId accountId = INVALID_HANDLE; + + int ret = CONNECTION_ERROR_NONE; + connection_h connectionHandle = null; + connection_profile_h profileHandle = null; + + _NetAccountManagerImpl::UpdateNetAccountDb(); + + ret = connection_create(&connectionHandle); + SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM, + "[%s] A system error has been occurred. The return value from connection_create() is %d", + GetErrorMessage(E_SYSTEM), ret); + unique_ptr pConnectionHandle(connectionHandle); + + ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profileHandle); + if ((ret != CONNECTION_ERROR_NONE) || (profileHandle == null)) + { + SysLog(NID_NET, "MMS profile is not found, so search a prepaid-MMS profile."); + + ret = connection_get_default_cellular_service_profile(connectionHandle, CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS, &profileHandle); + } + SysTryReturn(NID_NET, profileHandle != null, INVALID_HANDLE, E_INVALID_ACCOUNT, + "[%s] MMS(incl. prepaid) profile is not found.", GetErrorMessage(E_INVALID_ACCOUNT)); + unique_ptr pProfileHandle(profileHandle); + + char* pProfileName = null; + char* pProfileDisplayName = null; + String profileName; + String profileDisplayName; + + ret = connection_profile_get_id(profileHandle, &pProfileName); + SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM, + "[%s] A system error has been occurred. The return value from connection_profile_get_id() is %d", + GetErrorMessage(E_SYSTEM), ret); + + profileName = String(pProfileName); + free(pProfileName); + + ret = connection_profile_get_name(profileHandle, &pProfileDisplayName); + SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM, + "[%s] A system error has been occurred. The return value from connection_profile_get_name() is %d", + GetErrorMessage(E_SYSTEM), ret); + + profileDisplayName = String(pProfileDisplayName); + free(pProfileDisplayName); + + SysLog(NID_NET, "MMS profile is found. [%ls][%ls]", profileDisplayName.GetPointer(), profileName.GetPointer()); + + Mutex* pLock = _NetUtility::GetLock(); + MutexGuard locked(*pLock); + + if (__pMmsProfileName != null) + { + if (!profileName.Equals(*__pMmsProfileName, false)) + { + // MMS profile is changed. + SysLog(NID_NET, "MMS profile is changed. [%ls]->[%ls]", __pMmsProfileName->GetPointer(), profileName.GetPointer()); + + _NetAccountManagerImpl::UpdateNetAccountDb(true); + r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId); + + delete __pMmsProfileName; + __pMmsProfileName = new (std::nothrow) String(profileName); + __mmsAccountId = accountId; + } + else + { + accountId = __mmsAccountId; + } + } + else + { + SysLog(NID_NET, "Initialize MMS profile."); + + _NetAccountManagerImpl::UpdateNetAccountDb(true); + r = _NetAccountDatabase::GetAccountIdByProfileName(profileName, accountId); + + __pMmsProfileName = new (std::nothrow) String(profileName); + __mmsAccountId = accountId; + } + + locked.Unlock(); + + ClearLastResult(); + + return accountId; +} + +void +_NetAccountManagerImpl::UpdateNetAccountDb(bool force) +{ + if (__isNetAccountDbInitialized && !force) + { + // Already done + return; + } + + Mutex* pLock = _NetUtility::GetLock(); + MutexGuard locked(*pLock); + + result r = E_SUCCESS; + NetAccountId accountId = INVALID_HANDLE; + unique_ptr pAccountList(_NetAccountDatabase::GetProfileNamesN()); + unique_ptr pProfileList(_NetAccountManagerImpl::GetAllProfileNamesN()); + + if (pProfileList != null) + { + if (pProfileList->GetCount() > 0) + { + SysLog(NID_NET, "[%d] profiles are found.", pProfileList->GetCount()); + + unique_ptr pEnum(pProfileList->GetEnumeratorN()); + if (pEnum != null) + { + String* pProfileName = null; + while (pEnum->MoveNext() == E_SUCCESS) + { + pProfileName = dynamic_cast(pEnum->GetCurrent()); + if (pProfileName != null) + { + if (pAccountList->Contains(*pProfileName)) + { + SysLog(NID_NET, "ProfileName[%ls] is already on DB.", pProfileName->GetPointer()); + pAccountList->Remove(*pProfileName, true); + } + else + { + SysLog(NID_NET, "ProfileName[%ls] is not found on DB, so add it.", pProfileName->GetPointer()); + r = _NetAccountDatabase::AddAccount(*pProfileName, *pProfileName, _NET_ACCOUNT_OWNER_OTHER, accountId); + } + } + } + } + } + } + + if (pAccountList != null) + { + if (pAccountList->GetCount() > 0) + { + SysLog(NID_NET, "[%d] zombie accounts are on DB.", pAccountList->GetCount()); + + unique_ptr pEnum(pAccountList->GetEnumeratorN()); + if (pEnum != null) + { + String* pProfileName = null; + while (pEnum->MoveNext() == E_SUCCESS) + { + pProfileName = dynamic_cast(pEnum->GetCurrent()); + if (pProfileName != null) + { + r = _NetAccountDatabase::RemoveAccountByProfileName(*pProfileName); + } + } + } + } + } + + __isNetAccountDbInitialized = true; + + ClearLastResult(); + + locked.Unlock(); + + SysLog(NID_NET, "Network account database is updated."); +} + } } // Tizen::Net diff --git a/src/FNet_NetAccountManagerImpl.h b/src/FNet_NetAccountManagerImpl.h index bd56ec2..70a9a32 100644 --- a/src/FNet_NetAccountManagerImpl.h +++ b/src/FNet_NetAccountManagerImpl.h @@ -39,22 +39,6 @@ class NetAccountInfo; class NetAccountManager; class _PsSystemNetConnection; -class _NetProfileInfo - : public Tizen::Base::Object -{ -public: - _NetProfileInfo(void); - _NetProfileInfo(const Tizen::Base::String& profileName, const Tizen::Base::String& profileDisplayName); - ~_NetProfileInfo(void); - - Tizen::Base::String GetProfileName(void) const; - Tizen::Base::String GetProfileDisplayName(void) const; - -private: - Tizen::Base::String __profileName; - Tizen::Base::String __profileDisplayName; -}; // _NetProfileInfo - class _NetAccountManagerImpl : public Tizen::Base::Object { @@ -149,11 +133,12 @@ public: public: static void* GetPsProfileHandleN(const Tizen::Base::String& profileName); static void* GetWifiProfileHandleN(void); - static _NetProfileInfo GetInternetProfileInfo(void); - static _NetProfileInfo GetMmsProfileInfo(void); static Tizen::Base::Collection::IList* GetAppProfileNamesN(void); - static Tizen::Base::Collection::IList* GetAllProfileInfosN(void); + static Tizen::Base::Collection::IList* GetAllProfileNamesN(void); static NetAccountInfo* CreateWifiNetAccountInfoN(void* pProfileInfo); + static NetAccountId GetInternetAccountId(void); + static NetAccountId GetMmsAccountId(void); + static void UpdateNetAccountDb(bool force = false); private: /** @@ -172,6 +157,12 @@ private: _NetAccountManagerImpl& operator =(const _NetAccountManagerImpl& rhs); private: + static bool __isNetAccountDbInitialized; + static NetAccountId __internetAccountId; + static NetAccountId __mmsAccountId; + static Tizen::Base::String* __pInternetProfileName; + static Tizen::Base::String* __pMmsProfileName; + std::unique_ptr __pConnectionHandle; friend class NetAccountManager; diff --git a/src/FNet_NetConnectionManagerImpl.cpp b/src/FNet_NetConnectionManagerImpl.cpp index a8ad158..b977d12 100644 --- a/src/FNet_NetConnectionManagerImpl.cpp +++ b/src/FNet_NetConnectionManagerImpl.cpp @@ -148,11 +148,10 @@ _NetConnectionManagerImpl::CreateNetConnectionN(NetAccountId netAccountId) result r = E_SUCCESS; - SysTryReturn(NID_NET, ((netAccountId == _DEFAULT_PS_ACCOUNT_ID) || - (netAccountId == _DEFAULT_WIFI_ACCOUNT_ID) || + SysTryReturn(NID_NET, ((netAccountId == _DEFAULT_WIFI_ACCOUNT_ID) || (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID) || (netAccountId == _DEFAULT_USB_ACCOUNT_ID) || - (netAccountId >= _CUSTOM_ACCOUNT_ID_START)), + (netAccountId >= _PS_ACCOUNT_ID_START)), null, E_INVALID_ACCOUNT, "[%s] Invalid network account. accountId=%d", GetErrorMessage(E_INVALID_ACCOUNT), netAccountId); @@ -192,7 +191,6 @@ result _NetConnectionManagerImpl::SetNetPreference(NetPreferenceType netPreference) { result r = E_SUCCESS; - String accountName; #ifndef _OSP_EMUL_ _SystemNetConnection* pNewConnection = null; #endif // !_OSP_EMUL_ @@ -203,8 +201,14 @@ _NetConnectionManagerImpl::SetNetPreference(NetPreferenceType netPreference) if (netPreference == NET_PS_ONLY) { - _NetAccountDatabase::GetAccountName(__managedNetAccountId, accountName); - __pPsConnection = _SystemNetConnection::GetPsInstance(__managedNetAccountId); + if (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID) + { + __pPsConnection = _SystemNetConnection::GetPsInstance(_NetAccountManagerImpl::GetInternetAccountId()); + } + else + { + __pPsConnection = _SystemNetConnection::GetPsInstance(__managedNetAccountId); + } } Mutex* pLock = _NetUtility::GetLock(); @@ -464,8 +468,7 @@ _NetConnectionManagerImpl::SetManagedNetAccountId(NetAccountId netAccountId) String accountName; SysTryReturnResult(NID_NET, __managedNetRefCount == 0, E_INVALID_OPERATION, "The managed network connection is used."); - SysTryReturnResult(NID_NET, ((netAccountId == _DEFAULT_PS_ACCOUNT_ID) || - (netAccountId >= _CUSTOM_ACCOUNT_ID_START)), + SysTryReturnResult(NID_NET, netAccountId >= _PS_ACCOUNT_ID_START, E_INVALID_ARG, "Invalid argument is used. accountId=%d", netAccountId); r = _NetAccountDatabase::GetAccountName(netAccountId, accountName); @@ -496,7 +499,7 @@ _NetConnectionManagerImpl::SetManagedNetAccountId(NetAccountId netAccountId) NetAccountId _NetConnectionManagerImpl::GetManagedNetAccountId(void) const { - ClearLastResult(); + NetAccountId accountId = INVALID_HANDLE; SysTryReturn(NID_NET, __pManagedSystemNetConnection != null, INVALID_HANDLE, E_INVALID_STATE, "[%s] Managed network connection is inactive.", GetErrorMessage(E_INVALID_STATE)); @@ -506,12 +509,23 @@ _NetConnectionManagerImpl::GetManagedNetAccountId(void) const if (__pManagedSystemNetConnection->GetBearerType() == NET_BEARER_WIFI) { - return _DEFAULT_WIFI_ACCOUNT_ID; + accountId = _DEFAULT_WIFI_ACCOUNT_ID; } else { - return __managedNetAccountId; + if (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID) + { + accountId = _NetAccountManagerImpl::GetInternetAccountId(); + } + else + { + accountId = __managedNetAccountId; + } } + + ClearLastResult(); + + return accountId; } NetConnectionState diff --git a/src/FNet_SystemNetConnection.cpp b/src/FNet_SystemNetConnection.cpp index 753d752..35ec2e5 100644 --- a/src/FNet_SystemNetConnection.cpp +++ b/src/FNet_SystemNetConnection.cpp @@ -397,8 +397,6 @@ void _SystemNetConnection::InitializeNetworkFrameworkOnce(void) { result r = E_SUCCESS; - NetAccountId accountId = INVALID_HANDLE; - String accountName; __pPsSystemConnectionMap = new (std::nothrow) HashMap(); SysTryReturnVoidResult(NID_NET, __pPsSystemConnectionMap != null, E_OUT_OF_MEMORY, @@ -414,155 +412,6 @@ _SystemNetConnection::InitializeNetworkFrameworkOnce(void) SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r, "[%s] A system error has been occurred. Failed to initialize the network database.", GetErrorMessage(r)); - _NetProfileInfo internetProfileInfo = _NetAccountManagerImpl::GetInternetProfileInfo(); - _NetProfileInfo mmsProfileInfo = _NetAccountManagerImpl::GetMmsProfileInfo(); - String profileName; - - r = _NetAccountDatabase::GetProfileName(_DEFAULT_PS_ACCOUNT_ID, profileName); - if (internetProfileInfo.GetProfileName().IsEmpty()) - { - SysLog(NID_NET, "Internet profile is not found."); - - if (!profileName.IsEmpty()) - { - SysLog(NID_NET, "Remove Internet profile from database."); - r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_PS_ACCOUNT_ID); - } - } - else - { - if (internetProfileInfo.GetProfileName() != profileName) - { - SysLog(NID_NET, "Internet profile is changed. [%ls] -> [%ls]", profileName.GetPointer(), internetProfileInfo.GetProfileName().GetPointer()); - - if (!profileName.IsEmpty()) - { - r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_PS_ACCOUNT_ID); - } - - r = _NetAccountDatabase::RemoveAccountByProfileName(internetProfileInfo.GetProfileName()); - - accountName.Clear(); - accountName.Append(internetProfileInfo.GetProfileDisplayName()); - accountName.Append(internetProfileInfo.GetProfileName()); - - r = _NetAccountDatabase::AddAccount(accountName, internetProfileInfo.GetProfileName(), _NET_ACCOUNT_OWNER_SYSTEM_INTERNET, accountId); - } - } - - profileName.Clear(); - - r = _NetAccountDatabase::GetProfileName(_DEFAULT_MMS_ACCOUNT_ID, profileName); - if (mmsProfileInfo.GetProfileName().IsEmpty()) - { - SysLog(NID_NET, "MMS profile is not found."); - - if (!profileName.IsEmpty()) - { - SysLog(NID_NET, "Remove MMS profile from database."); - r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_MMS_ACCOUNT_ID); - } - } - else - { - if (mmsProfileInfo.GetProfileName() != profileName) - { - SysLog(NID_NET, "MMS profile is changed. [%ls] -> [%ls]", profileName.GetPointer(), mmsProfileInfo.GetProfileName().GetPointer()); - - if (!profileName.IsEmpty()) - { - r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_MMS_ACCOUNT_ID); - } - - r = _NetAccountDatabase::RemoveAccountByProfileName(mmsProfileInfo.GetProfileName()); - - accountName.Clear(); - accountName.Append(mmsProfileInfo.GetProfileDisplayName()); - accountName.Append(mmsProfileInfo.GetProfileName()); - - r = _NetAccountDatabase::AddAccount(accountName, mmsProfileInfo.GetProfileName(), _NET_ACCOUNT_OWNER_SYSTEM_MMS, accountId); - } - } - - unique_ptr pAccountList(_NetAccountDatabase::GetProfileNamesN()); - unique_ptr pProfileList(_NetAccountManagerImpl::GetAllProfileInfosN()); - - if (pProfileList != null) - { - if (pProfileList->GetCount() > 0) - { - SysLog(NID_NET, "[%d] profiles are found.", pProfileList->GetCount()); - - unique_ptr pEnum(pProfileList->GetEnumeratorN()); - if (pEnum != null) - { - _NetProfileInfo* pProfileInfo = null; - while (pEnum->MoveNext() == E_SUCCESS) - { - pProfileInfo = dynamic_cast<_NetProfileInfo*>(pEnum->GetCurrent()); - if (pProfileInfo != null) - { - if (pAccountList->Contains(pProfileInfo->GetProfileName())) - { - SysLog(NID_NET, "ProfileName[%ls] is already on DB.", pProfileInfo->GetProfileName().GetPointer()); - pAccountList->Remove(pProfileInfo->GetProfileName(), true); - } - else - { - // OSP or Non-default account - accountName.Clear(); - accountName.Append(pProfileInfo->GetProfileDisplayName()); - accountName.Append(pProfileInfo->GetProfileName()); - - SysLog(NID_NET, "ProfileName[%ls] is not found on DB, so add it.", pProfileInfo->GetProfileName().GetPointer()); - r = _NetAccountDatabase::AddAccount(accountName, pProfileInfo->GetProfileName(), _NET_ACCOUNT_OWNER_OTHER, accountId); - } - } - } - } - } - } - - if (pAccountList != null) - { - if (pAccountList->GetCount() > 0) - { - SysLog(NID_NET, "[%d] zombie accounts are on DB.", pAccountList->GetCount()); - - unique_ptr pEnum(pAccountList->GetEnumeratorN()); - if (pEnum != null) - { - String* pProfileName = null; - while (pEnum->MoveNext() == E_SUCCESS) - { - pProfileName = dynamic_cast(pEnum->GetCurrent()); - if (pProfileName != null) - { - r = _NetAccountDatabase::RemoveAccountByProfileName(*pProfileName); - } - } - } - } - } - -#ifdef _OSP_EMUL_ - static const wchar_t _DEFAULT_PS_ACCOUNT_NAME_FOR_EMULATOR[] = L"Tizen_Internet_1"; - static const wchar_t _DEFAULT_MMS_ACCOUNT_NAME_FOR_EMULATOR[] = L"Tizen_Mms_2"; - - String defaultAccountName; - int defaultAccountId; - - r = _NetAccountDatabase::GetAccountName(_DEFAULT_PS_ACCOUNT_ID, defaultAccountName); - if (r != E_SUCCESS) - { - SysLog(NID_NET, "Default PS account is not found, so add it now."); - r = _NetAccountDatabase::AddAccount(String(_DEFAULT_PS_ACCOUNT_NAME_FOR_EMULATOR), - String(_DEFAULT_PS_ACCOUNT_NAME_FOR_EMULATOR), _NET_ACCOUNT_OWNER_SYSTEM_INTERNET, defaultAccountId); - r = _NetAccountDatabase::AddAccount(String(_DEFAULT_MMS_ACCOUNT_NAME_FOR_EMULATOR), - String(_DEFAULT_MMS_ACCOUNT_NAME_FOR_EMULATOR), _NET_ACCOUNT_OWNER_SYSTEM_MMS, defaultAccountId); - } -#endif // _OSP_EMUL_ - __isInitialized = true; } diff --git a/src/inc/FNet_NetTypes.h b/src/inc/FNet_NetTypes.h index 73c2d78..5c1c90c 100644 --- a/src/inc/FNet_NetTypes.h +++ b/src/inc/FNet_NetTypes.h @@ -40,7 +40,7 @@ static const int _DEFAULT_MMS_ACCOUNT_ID = 2; static const int _DEFAULT_WIFI_ACCOUNT_ID = 11; static const int _DEFAULT_WIFI_DIRECT_ACCOUNT_ID = 12; static const int _DEFAULT_USB_ACCOUNT_ID = 21; -static const int _CUSTOM_ACCOUNT_ID_START = 101; +static const int _PS_ACCOUNT_ID_START = 101; static const int _HASH_CODE_INITIAL_VALUE = 17; static const int _HASH_CODE_COEFFICIENT_VALUE = 37; -- 2.7.4