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