Fix code for TDIS-5396
[framework/osp/social.git] / src / FSclAccountAccessor.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 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  * @file                FSclAccountAccessor.cpp
19  * @brief               This is the implementation for AccountAccessor class.
20  *
21  * This file contains definitions of @e AccountAccessor class.
22  */
23
24 #include <new>
25 #include <pthread.h>
26 #include <unique_ptr.h>
27 #include <FBaseResult.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseColIList.h>
30 #include <FSec_AccessController.h>
31 #include <FSclAccount.h>
32 #include <FSclAccountAccessor.h>
33 #include <FSclAccountProvider.h>
34 #include <FSclIAccountEventListener.h>
35 #include "FScl_AccountAccessorImpl.h"
36
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Security;
41
42 namespace Tizen { namespace Social
43 {
44
45 AccountAccessor* AccountAccessor::__pInstance = null;
46
47 AccountAccessor::AccountAccessor(void)
48         : __pAccountAccessorImpl(null)
49 {
50         // empty body
51 }
52
53 AccountAccessor::~AccountAccessor(void)
54 {
55         delete __pAccountAccessorImpl;
56 }
57
58 result
59 AccountAccessor::SetEventListener(IAccountEventListener* pListener)
60 {
61         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ);
62         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
63         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
64
65         SysAssertf(__pAccountAccessorImpl != null,
66                         "Not yet constructed. Construct() should be called before use.");
67
68         r = __pAccountAccessorImpl->SetEventListener(pListener);
69         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
70
71         return E_SUCCESS;
72 }
73
74 Account
75 AccountAccessor::GetAccount(AccountId accountId) const
76 {
77         Account account(L"");
78
79         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ);
80         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
81         SysTryReturn(NID_SCL, r == E_SUCCESS, account, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
82
83         SysAssertf(__pAccountAccessorImpl != null,
84                         "Not yet constructed. Construct() should be called before use.");
85
86         account = __pAccountAccessorImpl->GetAccount(accountId);
87         SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), account, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
88
89         return account;
90 }
91
92 IList*
93 AccountAccessor::GetAccountsByAccountProviderN(const AppId& accountProviderAppId) const
94 {
95         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ);
96         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
97         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
98
99         SysAssertf(__pAccountAccessorImpl != null,
100                         "Not yet constructed. Construct() should be called before use.");
101
102         IList* pList = __pAccountAccessorImpl->GetAccountsByAccountProviderN(accountProviderAppId);
103         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
104
105         return pList;
106 }
107
108 IList*
109 AccountAccessor::GetAllAccountsN(void) const
110 {
111         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ);
112         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
113         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
114
115         SysAssertf(__pAccountAccessorImpl != null,
116                         "Not yet constructed. Construct() should be called before use.");
117
118         IList* pList = __pAccountAccessorImpl->GetAllAccountsN();
119         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
120
121         return pList;
122 }
123
124 AccountProvider
125 AccountAccessor::GetAccountProvider(const AppId& accountProviderAppId) const
126 {
127         AccountProvider accountProvider;
128
129         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ);
130         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
131         SysTryReturn(NID_SCL, r == E_SUCCESS, accountProvider, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
132
133         SysAssertf(__pAccountAccessorImpl != null,
134                         "Not yet constructed. Construct() should be called before use.");
135
136         accountProvider = __pAccountAccessorImpl->GetAccountProvider(accountProviderAppId);
137         SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), accountProvider, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
138
139         return accountProvider;
140 }
141
142 IList*
143 AccountAccessor::GetAccountProvidersByCapabilityN(const String& capability) const
144 {
145         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ);
146         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
147         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
148
149         SysAssertf(__pAccountAccessorImpl != null,
150                         "Not yet constructed. Construct() should be called before use.");
151
152         IList* pList = __pAccountAccessorImpl->GetAccountProvidersByCapabilityN(capability);
153         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
154
155         return pList;
156 }
157
158 IList*
159 AccountAccessor::GetAllAccountProvidersN(void) const
160 {
161         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ);
162         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
163         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
164
165         SysAssertf(__pAccountAccessorImpl != null,
166                         "Not yet constructed. Construct() should be called before use.");
167
168         IList* pList = __pAccountAccessorImpl->GetAllAccountProvidersN();
169         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
170
171         return pList;
172 }
173
174 AccountAccessor*
175 AccountAccessor::GetInstance(void)
176 {
177         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
178
179         if (__pInstance == null)
180         {
181                 ClearLastResult();
182
183                 pthread_once(&onceBlock, InitAccountAccessor);
184
185                 result r = GetLastResult();
186
187                 if (IsFailed(r))
188                 {
189                         onceBlock = PTHREAD_ONCE_INIT;
190                 }
191         }
192
193         return __pInstance;
194 }
195
196 result
197 AccountAccessor::Construct(void)
198 {
199         SysAssertf(__pAccountAccessorImpl == null,
200                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
201
202         _AccountAccessorImpl* pAccountAccessorImpl = new (std::nothrow) _AccountAccessorImpl();
203         SysTryReturnResult(NID_SCL, pAccountAccessorImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
204
205         result r = pAccountAccessorImpl->Construct();
206         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Failed to construct pAccountAccessorImpl.", GetErrorMessage(r));
207
208         __pAccountAccessorImpl = pAccountAccessorImpl;
209
210         return E_SUCCESS;
211
212 CATCH:
213         delete pAccountAccessorImpl;
214
215         return r;
216 }
217
218 void
219 AccountAccessor::InitAccountAccessor(void)
220 {
221         std::unique_ptr<AccountAccessor> pInstance(new (std::nothrow) AccountAccessor());
222         SysTryReturnVoidResult(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
223
224         result r = pInstance->Construct();
225         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Failed to construct pInstance.", GetErrorMessage(r));
226
227         __pInstance = pInstance.release();
228
229         std::atexit(DestroyAccountAccessor);
230 }
231
232 void
233 AccountAccessor::DestroyAccountAccessor(void)
234 {
235         delete __pInstance;
236         __pInstance = null;
237 }
238
239 }}  // Tizen::Social