Add ClearLastResult()
[platform/framework/native/appfw.git] / src / io / FIoMmcStorageManager.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        FIoMmcStorageManager.cpp
19  * @brief       This is the implementation file for MmcStorageManager class.
20  */
21
22 #include <cstdlib>
23 #include <pthread.h>
24 #include <limits.h>
25
26 #include <FBaseSysLog.h>
27 #include <FIoMmcStorageManager.h>
28
29 #include <FBase_NativeError.h>
30 #include <FIo_MmcStorageManagerImpl.h>
31 #include <FSec_AccessController.h>
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Security;
35
36 namespace Tizen { namespace Io
37 {
38
39 MmcStorageManager* MmcStorageManager::__pMmcStorageManagerInstance = null;
40
41 MmcStorageManager::MmcStorageManager(void)
42         : __pMmcStorageManagerImpl(null)
43 {
44 }
45
46 MmcStorageManager::~MmcStorageManager(void)
47 {
48         delete __pMmcStorageManagerImpl;
49 }
50
51 void
52 MmcStorageManager::InitSingleton(void)
53 {
54         MmcStorageManager* pInst = new (std::nothrow) MmcStorageManager();
55         SysTryReturnVoidResult(NID_IO, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
56
57         pInst->__pMmcStorageManagerImpl = _MmcStorageManagerImpl::GetInstance();
58         SysTryCatch(NID_IO, pInst->__pMmcStorageManagerImpl != null, , GetLastResult(),
59                         "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
60
61         __pMmcStorageManagerInstance = pInst;
62                 
63         std::atexit(DestroySingleton);
64         return;
65
66 CATCH:
67         delete pInst;
68 }
69
70 void
71 MmcStorageManager::DestroySingleton(void)
72 {
73         delete __pMmcStorageManagerInstance;
74 }
75
76 MmcStorageManager*
77 MmcStorageManager::GetInstance(void)
78 {
79         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
80
81         if (__pMmcStorageManagerInstance == null)
82         {
83                 ClearLastResult();
84                 pthread_once(&onceBlock, InitSingleton);
85                 result r = GetLastResult();
86                 if (IsFailed(r))
87                 {
88                         onceBlock = PTHREAD_ONCE_INIT;
89                         SysPropagate(NID_IO, r);
90                 }
91         }
92
93         return __pMmcStorageManagerInstance;
94 }
95
96 result
97 MmcStorageManager::Mount(void)
98 {
99         SysAssertf(__pMmcStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.\n");
100
101         return __pMmcStorageManagerImpl->Mount();
102 }
103
104 result
105 MmcStorageManager::Unmount(void)
106 {
107         SysAssertf(__pMmcStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.\n");
108
109         return __pMmcStorageManagerImpl->Unmount();
110 }
111
112 result
113 MmcStorageManager::Format(void)
114 {
115         SysAssertf(__pMmcStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.\n");
116
117         return __pMmcStorageManagerImpl->Format();
118 }
119
120 result
121 MmcStorageManager::AddMmcStorageMountListener(IMmcStorageMountListener& listener)
122 {
123         SysAssertf(__pMmcStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.\n");
124
125         // Compatibility issue
126         result r = __pMmcStorageManagerImpl->AddMmcStorageMountListener(listener);
127         switch(r)
128         {
129                 case E_OBJ_ALREADY_EXIST:
130                         return E_SUCCESS;
131                 case E_INVALID_OPERATION:
132                         return E_SYSTEM;
133                 default:
134                         break;
135         }
136
137         return r;
138 }
139
140 result
141 MmcStorageManager::RemoveMmcStorageMountListener(IMmcStorageMountListener& listener)
142 {
143         SysAssertf(__pMmcStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.\n");
144
145         // Compatibility issue
146         result r = __pMmcStorageManagerImpl->RemoveMmcStorageMountListener(listener);
147         switch(r)
148         {
149                 case E_OBJ_NOT_FOUND:
150                         return E_SUCCESS;
151                 default:
152                         break;
153         }
154
155         return r;
156 }
157
158 result
159 MmcStorageManager::AddMmcStorageFormatListener(IMmcStorageFormatListener& listener)
160 {
161         SysAssertf(__pMmcStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.\n");
162
163         // Compatibility issue
164         result r = __pMmcStorageManagerImpl->AddMmcStorageFormatListener(listener);
165         switch(r)
166         {
167                 case E_OBJ_ALREADY_EXIST:
168                         return E_SUCCESS;
169                 case E_INVALID_OPERATION:
170                         return E_SYSTEM;
171                 default:
172                         break;
173         }
174
175         return r;
176 }
177
178 result
179 MmcStorageManager::RemoveMmcStorageFormatListener(IMmcStorageFormatListener& listener)
180 {
181         SysAssertf(__pMmcStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.\n");
182
183         result r = __pMmcStorageManagerImpl->RemoveMmcStorageFormatListener(listener);
184         switch(r)
185         {
186                 case E_OBJ_NOT_FOUND:
187                         return E_SUCCESS;
188                 default:
189                         break;
190         }
191
192         return r;
193 }
194
195 }} // Tizen::Io
196