apply dynamic singleton pattern to internal classes and add retry code to AppWidgetMa...
[framework/osp/shell.git] / src / core / FShellLockManager.cpp
1 //
2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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        FShellAppWidgetManager.cpp
19  * @brief       This is the implementation for the LockManager class.
20  */
21
22 #include <cstdlib>
23 #include <unique_ptr.h>
24 #include <FBaseLog.h>
25 #include <FBaseSysLog.h>
26 #include <FShellLockManager.h>
27 #include <FShell_LockManagerImpl.h>
28
29 namespace Tizen { namespace Shell
30 {
31
32 LockManager* LockManager::__pTheInstance = null;
33
34 LockManager::LockManager()
35  :__pLockManagerImpl(null)
36 {
37 }
38
39 LockManager::~LockManager()
40 {
41 }
42
43 void
44 LockManager::InitSingleton(void)
45 {
46         SysLog(NID_SHELL, "Enter.");
47
48         std::unique_ptr<LockManager> pInst(new (std::nothrow) LockManager());
49         SysTryReturnVoidResult(NID_SHELL, pInst, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
50
51         result r = pInst->Construct();
52         SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
53
54         __pTheInstance = pInst.release();
55         std::atexit(DestroySingleton);
56
57         SysLog(NID_SHELL, "Exit.");
58 }
59
60 void
61 LockManager::DestroySingleton(void)
62 {
63         delete __pTheInstance;
64 }
65
66 LockManager*
67 LockManager::GetInstance(void)
68 {
69         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
70         if (__pTheInstance == null)
71         {
72                 SysLog(NID_SHELL, "Enter.");
73                 ClearLastResult();
74                 pthread_once(&onceBlock, InitSingleton);
75                 result r = GetLastResult();
76                 if (IsFailed(r))
77                 {
78                         onceBlock = PTHREAD_ONCE_INIT;
79                 }
80                 SysLog(NID_SHELL, "Exit.");
81         }
82         return __pTheInstance;
83 }
84
85 result
86 LockManager::Construct(void)
87 {
88         SysAssert(__pLockManagerImpl == null);
89
90         __pLockManagerImpl = _LockManagerImpl::GetInstance();
91         SysTryReturnResult(NID_SHELL, __pLockManagerImpl, E_SYSTEM, "[%s] Failed to construct _LockManagerImpl!!.");
92         SysLog(NID_SHELL, "LockManager is constructed.");
93         return E_SUCCESS;
94 }
95
96 bool
97 LockManager::IsLocked(void) const
98 {
99         SysAssertf(__pLockManagerImpl != null, "[E_SYSTEM] Not yet constructed.");
100
101         return __pLockManagerImpl->IsLocked();
102 }
103
104 bool
105 LockManager::IsSecureMode(void) const
106 {
107         SysAssertf(__pLockManagerImpl != null, "[E_SYSTEM] Not yet constructed.");
108
109         return __pLockManagerImpl->IsSecureMode();
110 }
111
112 result
113 LockManager::Unlock(void)
114 {
115         SysAssertf(__pLockManagerImpl != null, "[E_SYSTEM] Not yet constructed.");
116
117         return __pLockManagerImpl->Unlock();
118 }
119
120 }} // Tizen::Shell