Modify the spec file for secure log
[framework/osp/social.git] / src / FScl_AccountDbMonitor.cpp
1 //
2 // Copyright (c) 2012 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                FScl_AccountDbMonitor.cpp
18  * @brief               This is the implementation for _AccountDbMonitor class.
19  *
20  * This file contains definitions of @e _AccountDbMonitor class.
21  */
22
23 #include <new>
24 #include <FBaseRtMutex.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseColArrayListT.h>
27 #include "FScl_IAccountDbChangeEventListener.h"
28 #include "FScl_AccountDbChangeEvent.h"
29 #include "FScl_AccountDbChangeEventArg.h"
30 #include "FScl_AccountDbConnector.h"
31 #include "FScl_AccountDbMonitor.h"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Runtime;
35
36 namespace Tizen { namespace Social
37 {
38
39 _AccountDbMonitor* _AccountDbMonitor::__pTheInstance = null;
40
41 _AccountDbMonitor::__Callback::__Callback(account_subscribe_h accountSubscribeHandle, account_event_cb callback, void* pUserData)
42         : __accountSubscribeHandle(accountSubscribeHandle)
43         , __callback(callback)
44         , __pUserData(pUserData)
45 {
46         // empty body
47 }
48
49 result
50 _AccountDbMonitor::__Callback::Register(void)
51 {
52         int ret = account_subscribe_notification(__accountSubscribeHandle, __callback, __pUserData);
53         SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
54
55         return E_SUCCESS;
56 }
57
58 _AccountDbMonitor::__Callback::~__Callback(void)
59 {
60         if (__accountSubscribeHandle)
61         {
62                 account_unsubscribe_notification(__accountSubscribeHandle);
63         }
64 }
65
66 _AccountDbMonitor::_AccountDbMonitor(void)
67         : __pEvent(null)
68         , __pMutex(null)
69         , __pAccountChangeCallback(null)
70 {
71         // empty body
72 }
73
74 _AccountDbMonitor::~_AccountDbMonitor(void)
75 {
76         // empty body
77 }
78
79 result
80 _AccountDbMonitor::Construct(void)
81 {
82         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
83
84         std::unique_ptr<Mutex> pMutex(new (std::nothrow) Mutex());
85         SysTryReturn(NID_SCL, pMutex != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
86
87         result r = pMutex->Create();
88         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
89
90         std::unique_ptr<_AccountDbChangeEvent> pEvent(new (std::nothrow) _AccountDbChangeEvent());
91         SysTryReturnResult(NID_SCL, pEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
92
93         r = pEvent->Construct();
94         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
95
96         account_subscribe_h accountSubscribeHandle = null;
97         int ret = account_subscribe_create(&accountSubscribeHandle);
98         SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
99
100         std::unique_ptr<__Callback> pAccountChangeCallback(new (std::nothrow) __Callback(accountSubscribeHandle, OnAccountChangeEventReceived, this));
101         SysTryCatch(NID_SCL, pAccountChangeCallback != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
102
103         r = pAccountChangeCallback->Register();
104         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
105
106         __pMutex = move(pMutex);
107         __pEvent = move(pEvent);
108         __pAccountChangeCallback = move(pAccountChangeCallback);
109
110         return E_SUCCESS;
111
112 CATCH:
113         account_unsubscribe_notification(accountSubscribeHandle);
114
115         return r;
116 }
117
118 result
119 _AccountDbMonitor::AddListener(const _IAccountDbChangeEventListener& listener)
120 {
121         result r = __pMutex->Acquire();
122         SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
123
124         r = __pEvent->AddListener(listener);
125         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
126
127         r = __pMutex->Release();
128         SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
129
130         return E_SUCCESS;
131
132 CATCH:
133         r = __pMutex->Release();
134         SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
135
136         return r;
137 }
138
139 result
140 _AccountDbMonitor::RemoveListener(const _IAccountDbChangeEventListener& listener)
141 {
142         result r = __pMutex->Acquire();
143         SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
144
145         r = __pEvent->RemoveListener(listener);
146         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
147
148         r = __pMutex->Release();
149         SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
150
151         return E_SUCCESS;
152
153 CATCH:
154         r = __pMutex->Release();
155         SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
156
157         return r;
158 }
159
160 _AccountDbMonitor*
161 _AccountDbMonitor::GetInstance(void)
162 {
163         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
164         if (__pTheInstance == null)
165         {
166                 ClearLastResult();
167
168                 pthread_once(&onceBlock, InitSingleton);
169
170                 result r = GetLastResult();
171                 if (IsFailed(r))
172                 {
173                         onceBlock = PTHREAD_ONCE_INIT;
174                 }
175         }
176
177         return __pTheInstance;
178 }
179
180 bool
181 _AccountDbMonitor::OnAccountChangeEventReceived(const char* pEventType, int accountId, void* pUserData)
182 {
183         SysTryReturn(NID_SCL, pEventType != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
184
185         std::unique_ptr<String> pAccountChangeEventType(new (std::nothrow) String(pEventType));
186         SysTryReturn(NID_SCL, pAccountChangeEventType != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
187
188         _AccountDbMonitor* pAccountDbMonitor = static_cast<_AccountDbMonitor*>(pUserData);
189         _AccountDbChangeEvent* pEvent = pAccountDbMonitor->__pEvent.get();
190
191         std::unique_ptr<_AccountDbChangeEventArg> pArg(null);
192
193         if (pAccountChangeEventType->Equals(String(ACCOUNT_NOTI_NAME_INSERT)))
194         {
195                 pArg.reset(new (std::nothrow) _AccountDbChangeEventArg(accountId, _ACCOUNT_DB_CHANGE_TYPE_ADD));
196                 SysTryReturn(NID_SCL, pArg != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
197         }
198         else if (pAccountChangeEventType->Equals(String(ACCOUNT_NOTI_NAME_UPDATE)))
199         {
200                 pArg.reset(new (std::nothrow) _AccountDbChangeEventArg(accountId, _ACCOUNT_DB_CHANGE_TYPE_UPDATE));
201                 SysTryReturn(NID_SCL, pArg != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
202         }
203         else if (pAccountChangeEventType->Equals(String(ACCOUNT_NOTI_NAME_DELETE)))
204         {
205                 pArg.reset(new (std::nothrow) _AccountDbChangeEventArg(accountId, _ACCOUNT_DB_CHANGE_TYPE_REMOVE));
206                 SysTryReturn(NID_SCL, pArg != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
207         }
208         else
209         {
210                 SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
211                 return false;
212         }
213
214         result r = pEvent->Fire(*pArg);
215         SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
216
217         pArg.release();
218
219         return true;
220 }
221
222 void
223 _AccountDbMonitor::InitSingleton(void)
224 {
225         std::unique_ptr<_AccountDbMonitor> pInst(new (std::nothrow) _AccountDbMonitor());
226         SysTryReturnVoidResult(NID_SCL, pInst, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
227
228         result r = pInst->Construct();
229         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
230
231         __pTheInstance = pInst.release();
232
233         std::atexit(DestroySingleton);
234 }
235
236 void
237 _AccountDbMonitor::DestroySingleton(void)
238 {
239         delete __pTheInstance;
240 }
241
242 }}  // Tizen::Social