Flush app registry before releasing file lock
[platform/framework/native/appfw.git] / src / app / FApp_AppRegistryImpl.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 /**
18  * @file        FApp_AppRegistryImpl.cpp
19  * @brief       This is the implementation for the _AppRegistryImpl class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include <FBaseRtMutexGuard.h>
24 #include <FIoFile.h>
25 #include <FIoRegistry.h>
26 #include <FAppAppRegistry.h>
27
28 #include "FApp_AppRegistryImpl.h"
29 #include "FApp_AppInfo.h"
30
31 using namespace Tizen::Io;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Runtime;
34
35 namespace Tizen { namespace App
36 {
37
38 static const ReadOnlyTag ReadOnly = {};
39 static const ReadWriteTag ReadWrite = {};
40
41 _AppRegistryImpl::_AppRegistryImpl(void)
42         : __sectionName(L"__ApplicationStates")
43 {
44 }
45
46 _AppRegistryImpl::~_AppRegistryImpl(void)
47 {
48 }
49
50 result
51 _AppRegistryImpl::Construct(void)
52 {
53         const String& packageId = _AppInfo::GetPackageId();
54         SysAssertf(!packageId.IsEmpty(), "Empty package.");
55
56         __regPath = _AppInfo::GetAppRootPath() + L"data/";
57         result r = __regPath.Append(packageId);
58         SysTryReturnResult(NID_APP, !IsFailed(r), r, "String appending has failed.");
59
60         r = __mutex.Create();
61         SysTryReturnResult(NID_APP, !IsFailed(r), r, "Mutex intialization failed.");
62
63         {
64                 MutexGuard lock(__mutex);
65
66                 Registry reg;
67                 r = reg.Construct(__regPath, "a+");
68                 SysAssertf(!IsFailed(r), "[%s] Constructing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
69
70                 FileLock* pReglock = reg.LockN(FILE_LOCK_EXCLUSIVE);
71                 SysTryLog(NID_APP, pReglock != null, "[%s] Locking the app registry file has failed.", GetErrorMessage(GetLastResult()));
72
73                 r = reg.AddSection(__sectionName);
74                 if (r == E_SECTION_ALREADY_EXIST)
75                 {
76                         r = E_SUCCESS;
77                 }
78
79                 reg.Flush();
80                 delete pReglock;
81         }
82         return r;
83 }
84
85 result
86 _AppRegistryImpl::Add(const String& key, const String& value)
87 {
88         MutexGuard lock(__mutex);
89
90         Registry* pReg = LoadN(ReadWrite);
91         if (pReg == null)
92         {
93                 result r = GetLastResult();
94                 SysPropagate(NID_APP, r);
95                 return r;
96         }
97
98         result r = pReg->AddValue(__sectionName, key, value);
99         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
100
101         r = pReg->Flush();
102         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
103
104 CATCH:
105         delete pReg;
106         return r;
107 }
108
109 result
110 _AppRegistryImpl::Add(const String& key, int value)
111 {
112         MutexGuard lock(__mutex);
113
114         Registry* pReg = LoadN(ReadWrite);
115         if (pReg == null)
116         {
117                 result r = GetLastResult();
118                 SysPropagate(NID_APP, r);
119                 return r;
120         }
121
122         result r = pReg->AddValue(__sectionName, key, value);
123         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
124
125         r = pReg->Flush();
126         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
127
128 CATCH:
129         delete pReg;
130         return r;
131 }
132
133 result
134 _AppRegistryImpl::Add(const String& key, double value)
135 {
136         MutexGuard lock(__mutex);
137
138         Registry* pReg = LoadN(ReadWrite);
139         if (pReg == null)
140         {
141                 result r = GetLastResult();
142                 SysPropagate(NID_APP, r);
143                 return r;
144         }
145
146         result r = pReg->AddValue(__sectionName, key, value);
147         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
148
149         r = pReg->Flush();
150         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
151
152 CATCH:
153         delete pReg;
154         return r;
155 }
156
157 result
158 _AppRegistryImpl::Set(const String& key, const String& value)
159 {
160         MutexGuard lock(__mutex);
161
162         Registry* pReg = LoadN(ReadWrite);
163         if (pReg == null)
164         {
165                 result r = GetLastResult();
166                 SysPropagate(NID_APP, r);
167                 return r;
168         }
169
170         result r = pReg->SetValue(__sectionName, key, value);
171         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
172
173         r = pReg->Flush();
174         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
175
176 CATCH:
177         delete pReg;
178         return r;
179 }
180
181 result
182 _AppRegistryImpl::Set(const String& key, int value)
183 {
184         MutexGuard lock(__mutex);
185
186         Registry* pReg = LoadN(ReadWrite);
187         if (pReg == null)
188         {
189                 result r = GetLastResult();
190                 SysPropagate(NID_APP, r);
191                 return r;
192         }
193
194         result r = pReg->SetValue(__sectionName, key, value);
195         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
196
197         r = pReg->Flush();
198         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
199
200 CATCH:
201         delete pReg;
202         return r;
203 }
204
205 result
206 _AppRegistryImpl::Set(const String& key, double value)
207 {
208         MutexGuard lock(__mutex);
209
210         Registry* pReg = LoadN(ReadWrite);
211         if (pReg == null)
212         {
213                 result r = GetLastResult();
214                 SysPropagate(NID_APP, r);
215                 return r;
216         }
217
218         result r = pReg->SetValue(__sectionName, key, value);
219         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
220
221         r = pReg->Flush();
222         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
223
224 CATCH:
225         delete pReg;
226         return r;
227 }
228
229 result
230 _AppRegistryImpl::Save(void)
231 {
232     return E_SUCCESS;
233 }
234
235 result
236 _AppRegistryImpl::Remove(const String& key)
237 {
238         MutexGuard lock(__mutex);
239
240         Registry* pReg = LoadN(ReadWrite);
241         if (pReg == null)
242         {
243                 result r = GetLastResult();
244                 SysPropagate(NID_APP, r);
245                 return r;
246         }
247
248         result r = pReg->RemoveValue(__sectionName, key);
249         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
250
251         delete pReg;
252
253         return r;
254 }
255
256 result
257 _AppRegistryImpl::Get(const String& key, String& value) const
258 {
259         MutexGuard lock(__mutex);
260
261         Registry* pReg = LoadN(ReadOnly);
262         if (pReg == null)
263         {
264                 result r = GetLastResult();
265                 SysPropagate(NID_APP, r);
266                 return r;
267         }
268
269         result r = pReg->GetValue(__sectionName, key, value);
270         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
271
272         delete pReg;
273
274         return r;
275 }
276
277 result
278 _AppRegistryImpl::Get(const String& key, int& value) const
279 {
280         MutexGuard lock(__mutex);
281
282         Registry* pReg = LoadN(ReadOnly);
283         if (pReg == null)
284         {
285                 result r = GetLastResult();
286                 SysPropagate(NID_APP, r);
287                 return r;
288         }
289
290         result r = pReg->GetValue(__sectionName, key, value);
291         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
292
293         delete pReg;
294
295         return r;
296 }
297
298 result
299 _AppRegistryImpl::Get(const String& key, double& value) const
300 {
301         MutexGuard lock(__mutex);
302
303         Registry* pReg = LoadN(ReadOnly);
304         if (pReg == null)
305         {
306                 result r = GetLastResult();
307                 SysPropagate(NID_APP, r);
308                 return r;
309         }
310
311         result r = pReg->GetValue(__sectionName, key, value);
312         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
313
314         delete pReg;
315
316         return r;
317 }
318
319 Registry*
320 _AppRegistryImpl::LoadN(ReadOnlyTag) const
321 {
322         Registry* pReg = new (std::nothrow) Registry();
323         SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
324
325         result r = pReg->Construct(__regPath, "r");
326         SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
327                         __regPath.GetPointer(), GetErrorMessage(r));
328
329         SetLastResult(r);
330         return pReg;
331 }
332
333 Registry*
334 _AppRegistryImpl::LoadN(ReadWriteTag) const
335 {
336         Registry* pReg = new (std::nothrow) Registry();
337         SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
338
339         result r = pReg->Construct(__regPath, "a+");
340         SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
341                         __regPath.GetPointer(), GetErrorMessage(r));
342
343         SetLastResult(r);
344         return pReg;
345 }
346
347 }} // Tizen::App
348