Resolve prevent defects.
[platform/framework/native/appfw.git] / src / io / FIo_MmcStorageManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIo_MmcStorageManagerImpl.cpp
20  * @brief       This is the implementation file for _MmcStorageManagerImpl class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseSysLog.h>
26 #include <FBaseRtIEventArg.h>
27 #include <FBase_NativeError.h>
28 #include <FIoIMmcStorageMountListener.h>
29 #include <FIoIMmcStorageFormatListener.h>
30
31 #include "FIo_MmcStorageManagerImpl.h"
32 #include "FIo_MmcStorageManagerProxy.h"
33
34 using namespace std;
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39
40 namespace Tizen { namespace Io
41 {
42
43 static const int MOUNT_TYPE = 0;
44 static const int UNMOUNT_TYPE = 1;
45
46 class _MountEventArg
47         : public IEventArg
48 {
49 public:
50         _MountEventArg(int type, result r)
51                 : __type(type)
52                 , __r(r)
53         {
54         }
55         int __type;
56         result __r;
57 };
58
59 class _FormatEventArg
60         : public IEventArg
61 {
62 public:
63         _FormatEventArg(result r)
64                 : __r(r)
65         {
66         }
67         result __r;
68 };
69
70 class _MountEvent
71         : public Event
72 {
73 protected:
74         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
75         {
76                 const _MountEventArg* pArg = dynamic_cast<const _MountEventArg*>(&arg);
77                 if (pArg != null)
78                 {
79                         IMmcStorageMountListener* pListener = dynamic_cast<IMmcStorageMountListener*> (&listener);
80                         if (pListener != null)
81                         {
82                                 if (pArg->__type == MOUNT_TYPE) // Mount
83                                 {
84                                         pListener->OnMmcStorageMounted(pArg->__r);
85                                 }
86                                 else    // Unmount
87                                 {
88                                         pListener->OnMmcStorageUnmounted(pArg->__r);
89                                 }
90                         }
91                 }
92         }
93
94 };
95
96 class _FormatEvent
97         : public Event
98 {
99 protected:
100         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
101         {
102                 IMmcStorageFormatListener* pListener = dynamic_cast<IMmcStorageFormatListener*> (&listener);
103                 if (pListener != null)
104                 {
105                         const _FormatEventArg* pArg = dynamic_cast<const _FormatEventArg*>(&arg);
106                         if (pArg != null)
107                         {
108                                 pListener->OnMmcStorageFormatted(pArg->__r);
109                         }
110
111                 }
112         }
113
114 };
115
116 _MmcStorageManagerImpl::_MmcStorageManagerImpl(void)
117         : __pMmcStorageManagerProxy(null)
118         , __pMountEvent(null)
119         , __pFormatEvent(null)
120 {
121 }
122
123 _MmcStorageManagerImpl::~_MmcStorageManagerImpl(void)
124 {
125         delete __pMmcStorageManagerProxy;
126         delete __pMountEvent;
127         delete __pFormatEvent;
128 }
129
130 result
131 _MmcStorageManagerImpl::Construct(void)
132 {
133         result r = E_SUCCESS;
134
135         unique_ptr<_MmcStorageManagerProxy> pMmcStorageManagerProxy(new (std::nothrow) _MmcStorageManagerProxy);
136         SysTryReturnResult(NID_IO, pMmcStorageManagerProxy != null, E_OUT_OF_MEMORY, "The memory is insufficient");
137
138         r = pMmcStorageManagerProxy->Construct(this);
139         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "__pMmcStorageManagerProxy->Construct() failed.");
140
141         unique_ptr<_MountEvent> pMountEvent(new (std::nothrow) _MountEvent);
142         SysTryReturnResult(NID_IO, pMountEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
143
144         unique_ptr<_FormatEvent> pFormatEvent(new (std::nothrow) _FormatEvent);
145         SysTryReturnResult(NID_IO, pFormatEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
146
147         __pMmcStorageManagerProxy = pMmcStorageManagerProxy.release();
148
149         __pMountEvent = pMountEvent.release();
150         __pFormatEvent = pFormatEvent.release();
151
152         return E_SUCCESS;
153 }
154
155 _MmcStorageManagerImpl*
156 _MmcStorageManagerImpl::GetInstance(void)
157 {
158         static _MmcStorageManagerImpl* pImpl = null;
159         if (pImpl == null)
160         {
161                 unique_ptr<_MmcStorageManagerImpl> p(new (std::nothrow) _MmcStorageManagerImpl);
162                 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
163
164                 result r = p->Construct();
165                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
166
167                 pImpl = p.release();
168         }
169
170         return pImpl;
171
172 }
173
174 result
175 _MmcStorageManagerImpl::Mount(void)
176 {
177         return __pMmcStorageManagerProxy->Mount();
178 }
179
180 result
181 _MmcStorageManagerImpl::Unmount(void)
182 {
183         return __pMmcStorageManagerProxy->Unmount();
184 }
185
186 result
187 _MmcStorageManagerImpl::Format(void)
188 {
189         return __pMmcStorageManagerProxy->Format();
190 }
191
192 result
193 _MmcStorageManagerImpl::AddMmcStorageMountListener(IMmcStorageMountListener& listener)
194 {
195         return __pMountEvent->AddListener(listener);
196 }
197
198 result
199 _MmcStorageManagerImpl::RemoveMmcStorageMountListener(IMmcStorageMountListener& listener)
200 {
201         return __pMountEvent->RemoveListener(listener);
202 }
203
204 result
205 _MmcStorageManagerImpl::AddMmcStorageFormatListener(IMmcStorageFormatListener& listener)
206 {
207         return __pFormatEvent->AddListener(listener);
208 }
209
210 result
211 _MmcStorageManagerImpl::RemoveMmcStorageFormatListener(IMmcStorageFormatListener& listener)
212 {
213         return __pFormatEvent->RemoveListener(listener);
214 }
215
216 void
217 _MmcStorageManagerImpl::OnMmcMountResponseReceived(result r)
218 {
219         _MountEventArg* pEventArg= new (std::nothrow) _MountEventArg(MOUNT_TYPE, r);
220         if(pEventArg != null)
221         {
222                 __pMountEvent->Fire(*pEventArg);
223         }
224 }
225
226 void
227 _MmcStorageManagerImpl::OnMmcUnmountResponseReceived(result r)
228 {
229         _MountEventArg* pEventArg= new (std::nothrow) _MountEventArg(UNMOUNT_TYPE, r);
230         if(pEventArg != null)
231         {
232                 __pMountEvent->Fire(*pEventArg);
233         }
234 }
235
236 void
237 _MmcStorageManagerImpl::OnMmcFormatResponseReceived(result r)
238 {
239         _FormatEventArg* pEventArg= new (std::nothrow) _FormatEventArg(r);
240         if(pEventArg != null)
241         {
242                 __pFormatEvent->Fire(*pEventArg);
243         }
244 }
245
246 }} // Tizen::Io
247