Fix code for TDIS-5396
[framework/osp/social.git] / src / FSclAccountManager.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                FSclAccountManager.cpp
19  * @brief               This is the implementation for AccountManager class.
20  *
21  * This file contains definitions of @e AccountManager 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 <FSclAccountManager.h>
33 #include "FScl_AccountManagerImpl.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Security;
38
39 namespace Tizen { namespace Social
40 {
41
42 AccountManager* AccountManager::__pInstance = null;
43
44 AccountManager::AccountManager(void)
45         : __pAccountManagerImpl(null)
46 {
47         // empty body
48 }
49
50 AccountManager::~AccountManager(void)
51 {
52         delete __pAccountManagerImpl;
53 }
54
55 result
56 AccountManager::AddAccount(Account& account)
57 {
58         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_WRITE);
59         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
60         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
61
62         SysAssertf(__pAccountManagerImpl != null,
63                         "Not yet constructed. Construct() should be called before use.");
64
65         r = __pAccountManagerImpl->AddAccount(account);
66         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
67
68         return E_SUCCESS;
69 }
70
71 result
72 AccountManager::RemoveAccount(AccountId accountId)
73 {
74         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_WRITE);
75         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
76         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
77
78         SysAssertf(__pAccountManagerImpl != null,
79                         "Not yet constructed. Construct() should be called before use.");
80
81         r = __pAccountManagerImpl->RemoveAccount(accountId);
82         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
83
84         return E_SUCCESS;
85 }
86
87 result
88 AccountManager::UpdateAccount(const Account& account)
89 {
90         result r = _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_WRITE);
91         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
92         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
93
94         SysAssertf(__pAccountManagerImpl != null,
95                         "Not yet constructed. Construct() should be called before use.");
96
97         r = __pAccountManagerImpl->UpdateAccount(account);
98         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
99
100         return E_SUCCESS;
101 }
102
103 AccountManager*
104 AccountManager::GetInstance(void)
105 {
106         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
107
108         if (__pInstance == null)
109         {
110                 ClearLastResult();
111
112                 pthread_once(&onceBlock, InitAccountManager);
113
114                 result r = GetLastResult();
115
116                 if (IsFailed(r))
117                 {
118                         onceBlock = PTHREAD_ONCE_INIT;
119                 }
120         }
121
122         return __pInstance;
123 }
124
125 result
126 AccountManager::Construct(void)
127 {
128         SysAssertf(__pAccountManagerImpl == null,
129                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
130
131         _AccountManagerImpl* pAccountManagerImpl = new (std::nothrow) _AccountManagerImpl();
132         SysTryReturnResult(NID_SCL, pAccountManagerImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
133
134         result r = pAccountManagerImpl->Construct();
135         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Failed to construct pAccountManagerImpl.", GetErrorMessage(r));
136
137         __pAccountManagerImpl = pAccountManagerImpl;
138
139         return E_SUCCESS;
140
141 CATCH:
142         delete pAccountManagerImpl;
143
144         return r;
145 }
146
147 void
148 AccountManager::InitAccountManager(void)
149 {
150         std::unique_ptr<AccountManager> pInstance(new (std::nothrow) AccountManager());
151         SysTryReturnVoidResult(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
152
153         result r = pInstance->Construct();
154         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Failed to construct pInstance.", GetErrorMessage(r));
155
156         __pInstance = pInstance.release();
157
158         std::atexit(DestroyAccountManager);
159 }
160
161 void
162 AccountManager::DestroyAccountManager(void)
163 {
164         delete __pInstance;
165         __pInstance = null;
166 }
167
168 }}  // Tizen::Social