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