Modify IsAppInstalled() API
[platform/framework/native/appfw.git] / src / io / FIo_MmcStorageManagerProxy.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        FIo_MmcStorageManagerProxy.cpp
19  * @brief       This is the implementation for the _MmcStorageManagerProxy class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <FBaseErrors.h>
25 #include <FBaseSysLog.h>
26 #include "FIo_IpcClient.h"
27
28 #include "FIo_MmcStorageManagerProxy.h"
29 #include "FIo_MmcStorageManagerIpcMessages.h"
30
31 using namespace std;
32 using namespace Tizen::Base;
33
34 namespace Tizen { namespace Io
35 {
36
37 _MmcStorageManagerProxy* _MmcStorageManagerProxy::__pMmcStorageManagerProxyInstance = null;
38
39 _MmcStorageManagerProxy::_MmcStorageManagerProxy(void)
40         : __pIpcClient(null)
41         , __pListener(null)
42 {
43 }
44
45 _MmcStorageManagerProxy::~_MmcStorageManagerProxy(void)
46 {
47         delete __pIpcClient;
48 }
49
50 result
51 _MmcStorageManagerProxy::Construct(void)
52 {
53         unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient);
54         SysTryReturnResult(NID_IO, pIpcClient != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
55
56         result r = pIpcClient->Construct("osp.io.ipcserver.mmcstoragemanager", this);
57         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to connect to IPC server.");
58
59         __pIpcClient = pIpcClient.release();
60
61         return E_SUCCESS;
62 }
63
64 void
65 _MmcStorageManagerProxy::InitSingleton(void)
66 {
67         _MmcStorageManagerProxy* pInst = new (std::nothrow) _MmcStorageManagerProxy();
68         SysTryReturnVoidResult(NID_IO, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
69
70         result r = pInst->Construct();
71         SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
72
73         __pMmcStorageManagerProxyInstance = pInst;
74
75         std::atexit(DestroySingleton);
76         return;
77
78 CATCH:
79         delete pInst;
80 }
81
82 void
83 _MmcStorageManagerProxy::DestroySingleton(void)
84 {
85         delete __pMmcStorageManagerProxyInstance;
86 }
87
88 _MmcStorageManagerProxy*
89 _MmcStorageManagerProxy::GetInstance(void)
90 {
91         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
92
93         if (__pMmcStorageManagerProxyInstance == null)
94         {
95                 ClearLastResult();
96                 pthread_once(&onceBlock, InitSingleton);
97                 result r = GetLastResult();
98                 if (IsFailed(r))
99                 {
100                         onceBlock = PTHREAD_ONCE_INIT;
101                         SysPropagate(NID_IO, r);
102                 }
103         }
104
105         return __pMmcStorageManagerProxyInstance;
106 }
107
108 void
109 _MmcStorageManagerProxy::SetMmcStorageManagerListener(_IMmcStorageManagerListener* pListener)
110 {
111         __pListener = pListener;
112 }
113
114 result
115 _MmcStorageManagerProxy::Mount(void)
116 {
117         SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
118
119         result response = E_SUCCESS;
120         unique_ptr<IPC::Message> pMsg (new (std::nothrow) MmcStorageManager_Mount(&response));
121         SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
122
123         result r = __pIpcClient->SendRequest(pMsg.get());
124         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "SendRequest is failed.");
125
126         SysLog(NID_IO, "Received sync mount response msg from server is: [%s]", GetErrorMessage(response));
127
128         return response;
129 }
130
131 result
132 _MmcStorageManagerProxy::Unmount(void)
133 {
134         SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
135
136         result response = E_SUCCESS;
137         unique_ptr<IPC::Message> pMsg (new (std::nothrow) MmcStorageManager_Unmount(&response));
138         SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
139
140         result r = __pIpcClient->SendRequest(pMsg.get());
141         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "SendRequest is failed.");
142
143         SysLog(NID_IO, "Received sync unmount response msg from server is: [%s]", GetErrorMessage(response));
144
145         return response;
146 }
147
148 result
149 _MmcStorageManagerProxy::Format(void)
150 {
151         SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.");
152
153         result response = E_SUCCESS;
154         unique_ptr<IPC::Message> pMsg (new (std::nothrow) MmcStorageManager_Format(&response));
155         SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
156
157         result r = __pIpcClient->SendRequest(pMsg.get());
158         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "SendRequest is failed.");
159
160         SysLog(NID_IO, "Received sync format response msg from server is: [%s]", GetErrorMessage(response));
161
162         return response;
163 }
164
165 void
166 _MmcStorageManagerProxy::OnIpcResponseReceived(Tizen::Io::_IpcClient& client, const IPC::Message& message)
167 {
168         IPC_BEGIN_MESSAGE_MAP(_MmcStorageManagerProxy, message)
169                 IPC_MESSAGE_HANDLER_EX(MmcStorageManager_MountReceived, &client, MountResponseReceived)
170                 IPC_MESSAGE_HANDLER_EX(MmcStorageManager_UnmountReceived, &client, UnmountResponseReceived)
171                 IPC_MESSAGE_HANDLER_EX(MmcStorageManager_FormatReceived, &client, FormatResponseReceived)
172         IPC_END_MESSAGE_MAP()
173 }
174
175 void
176 _MmcStorageManagerProxy::MountResponseReceived(result r)
177 {
178         SysLog(NID_IO, "Received Async mount response msg from server is : [%s]", GetErrorMessage(r));
179         if (__pListener != null)
180         {
181                 __pListener->OnMmcMountResponseReceived(r);
182         }
183 }
184
185 void
186 _MmcStorageManagerProxy::UnmountResponseReceived(result r)
187 {
188         SysLog(NID_IO, "Received Async unmount response msg from server is : [%s]", GetErrorMessage(r));
189         if (__pListener != null)
190         {
191                 __pListener->OnMmcUnmountResponseReceived(r);
192         }
193 }
194
195 void
196 _MmcStorageManagerProxy::FormatResponseReceived(result r)
197 {
198         SysLog(NID_IO, "Received Async format response msg from server is : [%s]", GetErrorMessage(r));
199         if (__pListener != null)
200         {
201                 __pListener->OnMmcFormatResponseReceived(r);
202         }
203 }
204
205 }} // Tizen::Io
206