Add E_USER_NOT_CONSENTED error
[platform/framework/native/net.git] / src / http / FNetHttpHttpCredentials.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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                FNetHttpHttpCredentials.cpp
20  * @brief               This is the implementation file for HttpCredentials class.
21  *
22  * This file contains the implementation of HttpCredentials class.
23  */
24
25 #include <FBaseBuffer.h>
26 #include <FBaseString.h>
27 #include <FNetHttpHttpCredentials.h>
28 #include <FBaseSysLog.h>
29 #include <FSec_AccessController.h>
30 #include "FNet_NetTypes.h"
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Security;
34
35 namespace Tizen { namespace Net { namespace Http
36 {
37
38 HttpCredentials::HttpCredentials(void)
39         : __pHttpCredentialsImpl(null)
40 {
41 }
42
43 HttpCredentials::HttpCredentials(const String& username, const String& password)
44 {
45         __userName = username;
46         __password = password;
47         __pHttpCredentialsImpl = null;
48 }
49
50 HttpCredentials::HttpCredentials(const HttpCredentials& rhs)
51 {
52         __userName = rhs.__userName;
53         __password = rhs.__password;
54         __pHttpCredentialsImpl = null;
55 }
56
57 HttpCredentials::~HttpCredentials(void)
58 {
59 }
60
61 HttpCredentials&
62 HttpCredentials::operator =(const HttpCredentials& rhs)
63 {
64         if (this == &rhs)
65         {
66                 return *this;
67         }
68
69         __userName = rhs.__userName;
70         __password = rhs.__password;
71
72         return *this;
73 }
74
75 String
76 HttpCredentials::GetName(void) const
77 {
78         ClearLastResult();
79         result r = E_SUCCESS;
80
81         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
82         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
83         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, L"", r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
84
85         return __userName;
86 }
87
88 String
89 HttpCredentials::GetPassword(void) const
90 {
91         ClearLastResult();
92         result r = E_SUCCESS;
93
94         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
95         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
96         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, L"", r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
97
98         return __password;
99 }
100
101 result
102 HttpCredentials::SetName(const String& userName)
103 {
104         result r = E_SUCCESS;
105
106         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
107         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
108         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "The application is not permitted to call this method.");
109
110         SysTryReturnResult(NID_NET_HTTP, userName.GetLength() > 0, E_INVALID_ARG,
111                                  "Failed to set user name of the credentials.");
112
113         __userName = userName;
114         return r;
115 }
116
117 result
118 HttpCredentials::SetPassword(const String& password)
119 {
120         result r = E_SUCCESS;
121
122         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
123         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
124         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "The application is not permitted to call this method.");
125
126         SysTryReturnResult(NID_NET_HTTP, password.GetLength() > 0, E_INVALID_ARG,
127                                  "Failed to set password of the credentials.");
128
129         __password = password;
130         return r;
131 }
132
133 bool
134 HttpCredentials::Equals(const Tizen::Base::Object& rhs) const
135 {
136         const HttpCredentials* pRhs = dynamic_cast< const HttpCredentials* >(&rhs);
137
138         if (pRhs == null)
139         {
140                 return false;
141         }
142
143         if (__userName != pRhs->__userName ||
144                 __password != pRhs->__password)
145         {
146                 return false;
147         }
148
149         return true;
150 }
151
152 int
153 HttpCredentials::GetHashCode(void) const
154 {
155         int hashCode = _HASH_CODE_INITIAL_VALUE;
156
157         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __userName.GetHashCode();
158         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __password.GetHashCode();
159
160         return hashCode;
161 }
162
163 } } } // Tizen::Net::Http