Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNetNetAccountManager.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 %NetAccountManagerclass.
20  */
21
22 #include <unique_ptr.h>
23 #include <FNetNetAccountManager.h>
24 #include <FBaseSysLog.h>
25 #include <FSec_AccessController.h>
26 #include <FSys_SystemInfoImpl.h>
27 #include "FNet_NetAccountManagerImpl.h"
28
29 using namespace std;
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 NetAccountManager::NetAccountManager(void)
39         : __pNetAccountManagerImpl(null)
40 {
41 }
42
43 NetAccountManager::~NetAccountManager(void)
44 {
45         delete __pNetAccountManagerImpl;
46         __pNetAccountManagerImpl = null;
47 }
48
49 result
50 NetAccountManager::Construct(void)
51 {
52         result r = E_SUCCESS;
53
54         SysAssertf(__pNetAccountManagerImpl == null,
55                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
56
57         unique_ptr<_NetAccountManagerImpl> pNetAccountManagerImpl(new (std::nothrow) _NetAccountManagerImpl());
58         SysTryReturnResult(NID_NET, pNetAccountManagerImpl != null, E_OUT_OF_MEMORY,
59                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
60
61         r = pNetAccountManagerImpl->Construct();
62         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
63
64         __pNetAccountManagerImpl = pNetAccountManagerImpl.release();
65
66         return r;
67 }
68
69 NetAccountId
70 NetAccountManager::CreateNetAccount(NetAccountInfo& netAccountInfo)
71 {
72         result r = E_SUCCESS;
73         NetAccountId netAccountId = INVALID_HANDLE;
74
75         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_ACCOUNT);
76         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
77         SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r,
78                         "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
79
80 #ifndef _OSP_EMUL_
81         static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
82
83         bool isTelephonySupported = false;
84
85     r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
86     SysTryReturn(NID_NET, r == E_SUCCESS && isTelephonySupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
87                 "[%s] Telephony is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
88 #endif // !_OSP_EMUL_
89
90         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
91
92         netAccountId = __pNetAccountManagerImpl->CreateNetAccount(netAccountInfo);
93         r = GetLastResult();
94         SysTryReturn(NID_NET, netAccountId != INVALID_HANDLE, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
95
96         return netAccountId;
97 }
98
99 result
100 NetAccountManager::DeleteNetAccount(NetAccountId netAccountId)
101 {
102         result r = E_SUCCESS;
103
104         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_ACCOUNT);
105         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
106         SysTryReturnResult(NID_NET, r == E_SUCCESS, r,
107                         "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
108
109         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
110
111         r = __pNetAccountManagerImpl->DeleteNetAccount(netAccountId);
112         SysTryReturn(NID_NET, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
113
114         return r;
115 }
116
117 result
118 NetAccountManager::UpdateNetAccount(const NetAccountInfo& netAccountInfo)
119 {
120         result r = E_SUCCESS;
121
122         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_ACCOUNT);
123         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
124         SysTryReturnResult(NID_NET, r == E_SUCCESS, r,
125                         "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
126
127 #ifndef _OSP_EMUL_
128         static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
129
130         bool isTelephonySupported = false;
131
132     r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
133     SysTryReturnResult(NID_NET, r == E_SUCCESS && isTelephonySupported, E_UNSUPPORTED_OPERATION, "Telephony is not supported.");
134 #endif // !_OSP_EMUL_
135
136         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
137
138         r = __pNetAccountManagerImpl->UpdateNetAccount(netAccountInfo);
139         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
140
141         return r;
142 }
143
144 NetAccountInfo*
145 NetAccountManager::GetNetAccountInfoN(NetAccountId netAccountId) const
146 {
147         result r = E_SUCCESS;
148         NetAccountInfo* pInfo = null;
149
150         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
151
152         pInfo = __pNetAccountManagerImpl->GetNetAccountInfoN(netAccountId);
153         r = GetLastResult();
154         SysTryReturn(NID_NET, pInfo != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
155
156         return pInfo;
157 }
158
159 IListT<NetAccountId>*
160 NetAccountManager::GetNetAccountIdsN(void) const
161 {
162         result r = E_SUCCESS;
163         IListT<NetAccountId>* pList = null;
164
165         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
166
167         pList = __pNetAccountManagerImpl->GetNetAccountIdsN();
168         r = GetLastResult();
169         SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
170
171         return pList;
172 }
173
174 IList*
175 NetAccountManager::GetNetAccountNamesN(void) const
176 {
177         result r = E_SUCCESS;
178         IList* pList = null;
179
180         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
181
182         pList = __pNetAccountManagerImpl->GetNetAccountNamesN();
183         r = GetLastResult();
184         SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
185
186         return pList;
187 }
188
189 NetAccountId
190 NetAccountManager::GetNetAccountId(const String& netAccountName) const
191 {
192         result r = E_SUCCESS;
193         NetAccountId netAccountId = INVALID_HANDLE;
194
195         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
196
197         netAccountId = __pNetAccountManagerImpl->GetNetAccountId(netAccountName);
198         r = GetLastResult();
199         SysTryReturn(NID_NET, netAccountId != INVALID_HANDLE, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
200
201         return netAccountId;
202 }
203
204 NetAccountId
205 NetAccountManager::GetNetAccountId(NetBearerType netBearerType) const
206 {
207         result r = E_SUCCESS;
208         NetAccountId netAccountId = INVALID_HANDLE;
209
210         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
211
212 #ifndef _OSP_EMUL_
213         if (netBearerType == NET_BEARER_PS)
214         {
215                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
216
217                 bool isTelephonySupported = false;
218
219             r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
220             SysTryReturn(NID_NET, r == E_SUCCESS && isTelephonySupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
221                         "[%s] Telephony is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
222         }
223         else if (netBearerType == NET_BEARER_WIFI)
224         {
225             static const wchar_t _WIFI[] = L"http://tizen.org/feature/network.wifi";
226
227                 bool isWifiSupported = false;
228
229             r = _SystemInfoImpl::GetSysInfo(_WIFI, isWifiSupported);
230             SysTryReturn(NID_NET, r == E_SUCCESS && isWifiSupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
231                         "[%s] Wi-Fi is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
232         }
233         else if (netBearerType == NET_BEARER_WIFI_DIRECT)
234         {
235             static const wchar_t _WIFI_DIRECT[] = L"http://tizen.org/feature/network.wifi.direct";
236
237                 bool isWifiDirectSupported = false;
238
239             r = _SystemInfoImpl::GetSysInfo(_WIFI_DIRECT, isWifiDirectSupported);
240             SysTryReturn(NID_NET, r == E_SUCCESS && isWifiDirectSupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
241                         "[%s] Wi-Fi direct is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
242         }
243         else if (netBearerType == NET_BEARER_USB)
244         {
245             static const wchar_t _USB[] = L"http://tizen.org/feature/usb.host";
246
247                 bool isUsbSupported = false;
248
249             r = _SystemInfoImpl::GetSysInfo(_USB, isUsbSupported);
250             SysTryReturn(NID_NET, r == E_SUCCESS && isUsbSupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
251                         "[%s] USB is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
252         }
253         else if (netBearerType == NET_BEARER_MMS)
254         {
255                 static const wchar_t _MMS[] = L"http://tizen.org/feature/network.telephony.mms";
256
257                 bool isMmsSupported = false;
258
259             r = _SystemInfoImpl::GetSysInfo(_MMS, isMmsSupported);
260             SysTryReturn(NID_NET, r == E_SUCCESS && isMmsSupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
261                         "[%s] MMS is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
262         }
263 #endif // !_OSP_EMUL_
264
265         netAccountId = __pNetAccountManagerImpl->GetNetAccountId(netBearerType);
266         SysTryReturn(NID_NET, netAccountId != INVALID_HANDLE, INVALID_HANDLE, E_SYSTEM, "[%s] Failed to get a network account.", GetErrorMessage(E_SYSTEM));
267
268         return netAccountId;
269 }
270
271 NetPreferenceType
272 NetAccountManager::GetNetPreference(void) const
273 {
274         NetPreferenceType netPreference = NET_WIFI_FIRST;
275
276         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
277
278         netPreference = __pNetAccountManagerImpl->GetNetPreference();
279
280         return netPreference;
281 }
282
283 result
284 NetAccountManager::SetNetPreference(NetPreferenceType netPreference)
285 {
286         result r = E_SUCCESS;
287
288         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_CONNECTION);
289         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
290         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "The application is not permitted to call this method.");
291
292 #ifndef _OSP_EMUL_
293         if (netPreference == NET_WIFI_ONLY)
294         {
295                 static const wchar_t _WIFI[] = L"http://tizen.org/feature/network.wifi";
296
297                 bool isWifiSupported = false;
298
299             r = _SystemInfoImpl::GetSysInfo(_WIFI, isWifiSupported);
300             SysTryReturnResult(NID_NET, r == E_SUCCESS && isWifiSupported, E_UNSUPPORTED_OPERATION, "Wi-Fi is not supported.");
301         }
302         else if (netPreference == NET_PS_ONLY)
303         {
304                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
305
306                 bool isTelephonySupported = false;
307
308             r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
309             SysTryReturnResult(NID_NET, r == E_SUCCESS && isTelephonySupported, E_UNSUPPORTED_OPERATION, "Telephony is not supported.");
310         }
311 #endif // !_OSP_EMUL_
312
313         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
314
315         r = __pNetAccountManagerImpl->SetNetPreference(netPreference);
316         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
317
318         return r;
319 }
320
321 }}  // Tizen::Net