2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file FSclAccountAccessor.cpp
18 * @brief This is the implementation for AccountAccessor class.
20 * This file contains definitions of @e AccountAccessor class.
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"
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Security;
41 namespace Tizen { namespace Social
44 AccountAccessor* AccountAccessor::__pInstance = null;
46 AccountAccessor::AccountAccessor(void)
47 : __pAccountAccessorImpl(null)
52 AccountAccessor::~AccountAccessor(void)
54 delete __pAccountAccessorImpl;
58 AccountAccessor::SetEventListener(IAccountEventListener* pListener)
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));
64 SysAssertf(__pAccountAccessorImpl != null,
65 "Not yet constructed. Construct() should be called before use.");
67 r = __pAccountAccessorImpl->SetEventListener(pListener);
68 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
74 AccountAccessor::GetAccount(AccountId accountId) const
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));
82 SysAssertf(__pAccountAccessorImpl != null,
83 "Not yet constructed. Construct() should be called before use.");
85 account = __pAccountAccessorImpl->GetAccount(accountId);
86 SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), account, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
92 AccountAccessor::GetAccountsByAccountProviderN(const AppId& accountProviderAppId) const
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));
98 SysAssertf(__pAccountAccessorImpl != null,
99 "Not yet constructed. Construct() should be called before use.");
101 IList* pList = __pAccountAccessorImpl->GetAccountsByAccountProviderN(accountProviderAppId);
102 SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
108 AccountAccessor::GetAllAccountsN(void) const
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));
114 SysAssertf(__pAccountAccessorImpl != null,
115 "Not yet constructed. Construct() should be called before use.");
117 IList* pList = __pAccountAccessorImpl->GetAllAccountsN();
118 SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
124 AccountAccessor::GetAccountProvider(const AppId& accountProviderAppId) const
126 AccountProvider accountProvider;
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));
132 SysAssertf(__pAccountAccessorImpl != null,
133 "Not yet constructed. Construct() should be called before use.");
135 accountProvider = __pAccountAccessorImpl->GetAccountProvider(accountProviderAppId);
136 SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), accountProvider, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
138 return accountProvider;
142 AccountAccessor::GetAccountProvidersByCapabilityN(const String& capability) const
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));
148 SysAssertf(__pAccountAccessorImpl != null,
149 "Not yet constructed. Construct() should be called before use.");
151 IList* pList = __pAccountAccessorImpl->GetAccountProvidersByCapabilityN(capability);
152 SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
158 AccountAccessor::GetAllAccountProvidersN(void) const
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));
164 SysAssertf(__pAccountAccessorImpl != null,
165 "Not yet constructed. Construct() should be called before use.");
167 IList* pList = __pAccountAccessorImpl->GetAllAccountProvidersN();
168 SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
174 AccountAccessor::GetInstance(void)
176 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
178 if (__pInstance == null)
182 pthread_once(&onceBlock, InitAccountAccessor);
184 result r = GetLastResult();
188 onceBlock = PTHREAD_ONCE_INIT;
196 AccountAccessor::Construct(void)
198 SysAssertf(__pAccountAccessorImpl == null,
199 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
201 _AccountAccessorImpl* pAccountAccessorImpl = new (std::nothrow) _AccountAccessorImpl();
202 SysTryReturnResult(NID_SCL, pAccountAccessorImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
204 result r = pAccountAccessorImpl->Construct();
205 SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Failed to construct pAccountAccessorImpl.", GetErrorMessage(r));
207 __pAccountAccessorImpl = pAccountAccessorImpl;
212 delete pAccountAccessorImpl;
218 AccountAccessor::InitAccountAccessor(void)
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));
223 result r = pInstance->Construct();
224 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Failed to construct pInstance.", GetErrorMessage(r));
226 __pInstance = pInstance.release();
228 std::atexit(DestroyAccountAccessor);
232 AccountAccessor::DestroyAccountAccessor(void)