Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNetNetConnection.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                FNetNetConnection.cpp
20  * @brief               This is the implementation for the %NetConnection class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FNetNetConnection.h>
25 #include <FBaseSysLog.h>
26 #include <FSec_AccessController.h>
27 #include "FNet_NetConnectionImpl.h"
28
29 using namespace std;
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Security;
33
34 namespace Tizen { namespace Net
35 {
36
37 NetConnection::NetConnection()
38         : __pNetConnectionImpl(null)
39 {
40 }
41
42 NetConnection::~NetConnection(void)
43 {
44         delete __pNetConnectionImpl;
45         __pNetConnectionImpl = null;
46 }
47
48 result
49 NetConnection::Construct(NetAccountId netAccountId)
50 {
51         result r = E_SUCCESS;
52
53         SysAssertf(__pNetConnectionImpl == null,
54                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
55
56         unique_ptr<_NetConnectionImpl> pNetConnectionImpl(new (std::nothrow) _NetConnectionImpl());
57         SysTryReturnResult(NID_NET, pNetConnectionImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
58
59         r = pNetConnectionImpl->Construct(this, netAccountId);
60         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
61
62         __pNetConnectionImpl = pNetConnectionImpl.release();
63
64         return r;
65 }
66
67 result
68 NetConnection::AddNetConnectionListener(INetConnectionEventListener& listener)
69 {
70         result r = E_SUCCESS;
71
72         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
73
74         r = __pNetConnectionImpl->AddNetConnectionListener(listener);
75         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
76
77         return r;
78 }
79
80 result
81 NetConnection::RemoveNetConnectionListener(INetConnectionEventListener& listener)
82 {
83         result r = E_SUCCESS;
84
85         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
86
87         r = __pNetConnectionImpl->RemoveNetConnectionListener(listener);
88         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
89
90         return r;
91 }
92
93 result
94 NetConnection::Start(void)
95 {
96         result r = E_SUCCESS;
97
98         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_CONNECTION);
99         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
100         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "The application is not permitted to call this method.");
101
102         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
103
104         r = __pNetConnectionImpl->Start();
105         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
106
107         return r;
108 }
109
110 result
111 NetConnection::Stop(void)
112 {
113         result r = E_SUCCESS;
114
115         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_CONNECTION);
116         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
117         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "The application is not permitted to call this method.");
118
119         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
120
121         r = __pNetConnectionImpl->Stop();
122         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
123
124         return r;
125 }
126
127 result
128 NetConnection::Close(void)
129 {
130         result r = E_SUCCESS;
131
132         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_CONNECTION);
133         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
134         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "The application is not permitted to call this method.");
135
136         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
137
138         r = __pNetConnectionImpl->Close();
139         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
140
141         return r;
142 }
143
144 NetConnectionInfo*
145 NetConnection::GetNetConnectionInfoN(NetAccountId netAccountId)
146 {
147         return _NetConnectionImpl::GetNetConnectionInfoN(netAccountId);
148 }
149
150 IList*
151 NetConnection::GetAllNetConnectionInfoN(void)
152 {
153         return _NetConnectionImpl::GetAllNetConnectionInfoN();
154 }
155
156 NetAccountId
157 NetConnection::GetNetAccountId(void) const
158 {
159         NetAccountId netAccountId = INVALID_HANDLE;
160
161         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
162
163         netAccountId = __pNetConnectionImpl->GetNetAccountId();
164
165         return netAccountId;
166 }
167
168 const NetConnectionInfo*
169 NetConnection::GetNetConnectionInfo(void) const
170 {
171         const NetConnectionInfo* pInfo = null;
172
173         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
174
175         pInfo = __pNetConnectionImpl->GetNetConnectionInfo();
176
177         return pInfo;
178 }
179
180 NetConnectionState
181 NetConnection::GetConnectionState(void) const
182 {
183         NetConnectionState state = NET_CONNECTION_STATE_NONE;
184
185         SysAssertf(__pNetConnectionImpl != null, "Not yet constructed. Construct() should be called before use.");
186
187         state = __pNetConnectionImpl->GetConnectionState();
188
189         return state;
190 }
191
192 } } // Tizen::Net