Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNetNetConnectionManager.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 /**
19  * @file                        FNetNetConnectionManager.cpp
20  * @brief               This is the implementation for the %NetConnectionManager class.
21  */
22
23 #include <FNetNetConnectionInfo.h>
24 #include <FNetNetConnectionManager.h>
25 #include <FBaseSysLog.h>
26 #include <FSec_AccessController.h>
27 #include <FSys_SystemInfoImpl.h>
28 #include "FNet_NetConnectionManagerImpl.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Security;
33 using namespace Tizen::System;
34
35 namespace Tizen { namespace Net
36 {
37
38 NetConnectionManager::NetConnectionManager(void)
39         : __pNetConnectionManagerImpl(null)
40 {
41 }
42
43 NetConnectionManager::~NetConnectionManager(void)
44 {
45 }
46
47 result
48 NetConnectionManager::Construct(void)
49 {
50         result r = E_SUCCESS;
51
52         SysAssertf(__pNetConnectionManagerImpl == null,
53                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
54
55         __pNetConnectionManagerImpl = _NetConnectionManagerImpl::GetInstance();
56         SysTryReturnResult(NID_NET, __pNetConnectionManagerImpl != null, E_SYSTEM,
57                         "A system error has been occurred. Failed to initialize the network connection manager instance.");
58
59         return r;
60 }
61
62 NetConnection*
63 NetConnectionManager::CreateNetConnectionN(NetAccountId netAccountId)
64 {
65         result r = E_SUCCESS;
66         NetConnection* pConnection = null;
67
68         SysAssertf(__pNetConnectionManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
69
70         pConnection = __pNetConnectionManagerImpl->CreateNetConnectionN(netAccountId);
71         r = GetLastResult();
72         SysTryReturn(NID_NET, pConnection != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
73
74         return pConnection;
75 }
76
77 ManagedNetConnection*
78 NetConnectionManager::GetManagedNetConnectionN(void) const
79 {
80         result r = E_SUCCESS;
81         ManagedNetConnection* pManagedConnection = null;
82
83         SysAssertf(__pNetConnectionManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
84
85         pManagedConnection = __pNetConnectionManagerImpl->GetManagedNetConnectionN();
86         r = GetLastResult();
87         SysTryReturn(NID_NET, pManagedConnection != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
88
89         return pManagedConnection;
90 }
91
92 NetPreferenceType
93 NetConnectionManager::GetNetPreference(void) const
94 {
95         SysAssertf(__pNetConnectionManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
96
97         return __pNetConnectionManagerImpl->GetNetPreference();
98 }
99
100 result
101 NetConnectionManager::SetNetPreference(NetPreferenceType netPreference)
102 {
103         result r = E_SUCCESS;
104
105         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_CONNECTION);
106         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
107         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "The application is not permitted to call this method.");
108
109 #ifndef _OSP_EMUL_
110         if (netPreference == NET_WIFI_ONLY)
111         {
112                 static const wchar_t _WIFI[] = L"http://tizen.org/feature/network.wifi";
113
114                 bool isWifiSupported = false;
115
116             r = _SystemInfoImpl::GetSysInfo(_WIFI, isWifiSupported);
117             SysTryReturnResult(NID_NET, r == E_SUCCESS && isWifiSupported, E_UNSUPPORTED_OPERATION, "Wi-Fi is not supported.");
118         }
119         else if (netPreference == NET_PS_ONLY)
120         {
121                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
122
123                 bool isTelephonySupported = false;
124
125             r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
126             SysTryReturnResult(NID_NET, r == E_SUCCESS && isTelephonySupported, E_UNSUPPORTED_OPERATION, "Telephony is not supported.");
127         }
128 #endif // !_OSP_EMUL_
129
130         SysAssertf(__pNetConnectionManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
131
132         r = __pNetConnectionManagerImpl->SetNetPreference(netPreference);
133         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
134
135         return r;
136 }
137
138 NetConnectionInfo*
139 NetConnectionManager::GetNetConnectionInfoN(NetAccountId netAccountId)
140 {
141         result r = E_SUCCESS;
142         NetConnectionInfo* pConnectionInfo = null;
143
144         pConnectionInfo = _NetConnectionManagerImpl::GetNetConnectionInfoN(netAccountId);
145         r = GetLastResult();
146         SysTryReturn(NID_NET, pConnectionInfo != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
147
148         return pConnectionInfo;
149 }
150
151 IList*
152 NetConnectionManager::GetAllNetConnectionInfoN(void)
153 {
154         result r = E_SUCCESS;
155         IList* pList = null;
156
157         pList = _NetConnectionManagerImpl::GetAllNetConnectionInfoN();
158         r = GetLastResult();
159         SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
160
161         return pList;
162 }
163
164 } } // Tizen::Net