MmcStorageManager refactoring
authorHyunbin Lee <hyunbin.lee@samsung.com>
Fri, 19 Jul 2013 07:46:19 +0000 (16:46 +0900)
committerHyunbin Lee <hyunbin.lee@samsung.com>
Thu, 25 Jul 2013 04:49:23 +0000 (13:49 +0900)
Change-Id: I68f93daa8944bfeafdac8ceb979efa0d5503d7ad
Signed-off-by: Hyunbin Lee <hyunbin.lee@samsung.com>
src/io/FIoMmcStorageManager.cpp
src/io/FIo_MmcStorageManagerImpl.cpp
src/io/FIo_MmcStorageManagerProxy.cpp
src/io/inc/FIo_IMmcStorageManagerListener.h [moved from src/io/inc/FIo_IMmcStorageServiceEventListener.h with 64% similarity]
src/io/inc/FIo_MmcStorageManagerImpl.h
src/io/inc/FIo_MmcStorageManagerProxy.h

index 4e8d74b..45ae5e4 100644 (file)
@@ -26,9 +26,9 @@
 #include <FBaseSysLog.h>
 #include <FIoMmcStorageManager.h>
 
-#include <FSec_AccessController.h>
 #include <FBase_NativeError.h>
 #include <FIo_MmcStorageManagerImpl.h>
+#include <FSec_AccessController.h>
 
 using namespace Tizen::Base;
 using namespace Tizen::Security;
@@ -56,7 +56,7 @@ MmcStorageManager::InitSingleton(void)
 
        pInst->__pMmcStorageManagerImpl = _MmcStorageManagerImpl::GetInstance();
        SysTryCatch(NID_IO, pInst->__pMmcStorageManagerImpl != null, , GetLastResult(),
-                       "[%s] Propagated.", GetErrorMessage(GetLastResult()));
+                       "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
 
        __pMmcStorageManagerInstance = pInst;
                
index e45ffa8..9b30549 100644 (file)
@@ -24,8 +24,8 @@
 #include <FBaseSysLog.h>
 #include <FBaseRtIEventArg.h>
 #include <FBase_NativeError.h>
-#include <FIoIMmcStorageMountListener.h>
-#include <FIoIMmcStorageFormatListener.h>
+#include "FIoIMmcStorageMountListener.h"
+#include "FIoIMmcStorageFormatListener.h"
 
 #include "FIo_MmcStorageManagerImpl.h"
 #include "FIo_MmcStorageManagerProxy.h"
@@ -121,7 +121,6 @@ _MmcStorageManagerImpl::_MmcStorageManagerImpl(void)
 
 _MmcStorageManagerImpl::~_MmcStorageManagerImpl(void)
 {
-       delete __pMmcStorageManagerProxy;
        delete __pMountEvent;
        delete __pFormatEvent;
 }
@@ -129,22 +128,12 @@ _MmcStorageManagerImpl::~_MmcStorageManagerImpl(void)
 result
 _MmcStorageManagerImpl::Construct(void)
 {
-       result r = E_SUCCESS;
-
-       unique_ptr<_MmcStorageManagerProxy> pMmcStorageManagerProxy(new (std::nothrow) _MmcStorageManagerProxy);
-       SysTryReturnResult(NID_IO, pMmcStorageManagerProxy != null, E_OUT_OF_MEMORY, "The memory is insufficient");
-
-       r = pMmcStorageManagerProxy->Construct(this);
-       SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "__pMmcStorageManagerProxy->Construct() failed.");
-
        unique_ptr<_MountEvent> pMountEvent(new (std::nothrow) _MountEvent);
        SysTryReturnResult(NID_IO, pMountEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
 
        unique_ptr<_FormatEvent> pFormatEvent(new (std::nothrow) _FormatEvent);
        SysTryReturnResult(NID_IO, pFormatEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
 
-       __pMmcStorageManagerProxy = pMmcStorageManagerProxy.release();
-
        __pMountEvent = pMountEvent.release();
        __pFormatEvent = pFormatEvent.release();
 
@@ -157,34 +146,51 @@ _MmcStorageManagerImpl::GetInstance(void)
        static _MmcStorageManagerImpl* pImpl = null;
        if (pImpl == null)
        {
-               unique_ptr<_MmcStorageManagerImpl> p(new (std::nothrow) _MmcStorageManagerImpl);
-               SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+               unique_ptr<_MmcStorageManagerImpl> pMmcStorageManagerImpl(new (std::nothrow) _MmcStorageManagerImpl);
+               SysTryReturn(NID_IO, pMmcStorageManagerImpl != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
-               result r = p->Construct();
-               SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
+               result r = pMmcStorageManagerImpl->Construct();
+               SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
-               pImpl = p.release();
+               pImpl = pMmcStorageManagerImpl.release();
        }
 
        return pImpl;
-
 }
 
 result
 _MmcStorageManagerImpl::Mount(void)
 {
+       if (__pMmcStorageManagerProxy == null)
+       {
+               __pMmcStorageManagerProxy = _MmcStorageManagerProxy::GetInstance();
+               SysTryReturnResult(NID_IO, __pMmcStorageManagerProxy != null, E_SERVICE_BUSY, "Failed to get IPC proxy instance.");
+               __pMmcStorageManagerProxy->SetMmcStorageManagerListener(this);
+       }
        return __pMmcStorageManagerProxy->Mount();
 }
 
 result
 _MmcStorageManagerImpl::Unmount(void)
 {
+       if (__pMmcStorageManagerProxy == null)
+       {
+               __pMmcStorageManagerProxy = _MmcStorageManagerProxy::GetInstance();
+               SysTryReturnResult(NID_IO, __pMmcStorageManagerProxy != null, E_SERVICE_BUSY, "Failed to get IPC proxy instance.");
+               __pMmcStorageManagerProxy->SetMmcStorageManagerListener(this);
+       }
        return __pMmcStorageManagerProxy->Unmount();
 }
 
 result
 _MmcStorageManagerImpl::Format(void)
 {
+       if (__pMmcStorageManagerProxy == null)
+       {
+               __pMmcStorageManagerProxy = _MmcStorageManagerProxy::GetInstance();
+               SysTryReturnResult(NID_IO, __pMmcStorageManagerProxy != null, E_SERVICE_BUSY, "Failed to get IPC proxy instance.");
+               __pMmcStorageManagerProxy->SetMmcStorageManagerListener(this);
+       }
        return __pMmcStorageManagerProxy->Format();
 }
 
index 677ad46..e8b80e1 100644 (file)
 
 #include <FBaseErrors.h>
 #include <FBaseSysLog.h>
-#include <FIo_IpcClient.h>
+#include "FIo_IpcClient.h"
 
-#include "FIo_IMmcStorageServiceEventListener.h"
 #include "FIo_MmcStorageManagerProxy.h"
 #include "FIo_MmcStorageManagerIpcMessages.h"
 
 using namespace std;
-
 using namespace Tizen::Base;
 
 namespace Tizen { namespace Io
 {
 
+_MmcStorageManagerProxy* _MmcStorageManagerProxy::__pMmcStorageManagerProxyInstance = null;
+
 _MmcStorageManagerProxy::_MmcStorageManagerProxy(void)
        : __pIpcClient(null)
+       , __pListener(null)
 {
 }
 
@@ -47,7 +48,7 @@ _MmcStorageManagerProxy::~_MmcStorageManagerProxy(void)
 }
 
 result
-_MmcStorageManagerProxy::Construct(_IMmcStorageServiceEventListener* pListener)
+_MmcStorageManagerProxy::Construct(void)
 {
        unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient);
        SysTryReturnResult(NID_IO, pIpcClient != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
@@ -56,11 +57,60 @@ _MmcStorageManagerProxy::Construct(_IMmcStorageServiceEventListener* pListener)
        SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to connect to IPC server.");
 
        __pIpcClient = pIpcClient.release();
-       __pListener = pListener;
 
        return E_SUCCESS;
 }
 
+void
+_MmcStorageManagerProxy::InitSingleton(void)
+{
+       _MmcStorageManagerProxy* pInst = new (std::nothrow) _MmcStorageManagerProxy();
+       SysTryReturnVoidResult(NID_IO, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+
+       result r = pInst->Construct();
+       SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
+
+       __pMmcStorageManagerProxyInstance = pInst;
+
+       std::atexit(DestroySingleton);
+       return;
+
+CATCH:
+       delete pInst;
+}
+
+void
+_MmcStorageManagerProxy::DestroySingleton(void)
+{
+       delete __pMmcStorageManagerProxyInstance;
+}
+
+_MmcStorageManagerProxy*
+_MmcStorageManagerProxy::GetInstance(void)
+{
+       static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
+
+       if (__pMmcStorageManagerProxyInstance == null)
+       {
+               ClearLastResult();
+               pthread_once(&onceBlock, InitSingleton);
+               result r = GetLastResult();
+               if (IsFailed(r))
+               {
+                       onceBlock = PTHREAD_ONCE_INIT;
+                       SysPropagate(NID_IO, r);
+               }
+       }
+
+       return __pMmcStorageManagerProxyInstance;
+}
+
+void
+_MmcStorageManagerProxy::SetMmcStorageManagerListener(_IMmcStorageManagerListener* pListener)
+{
+       __pListener = pListener;
+}
+
 result
 _MmcStorageManagerProxy::Mount(void)
 {
@@ -126,24 +176,31 @@ void
 _MmcStorageManagerProxy::MountResponseReceived(result r)
 {
        SysLog(NID_IO, "Received Async mount response msg from server is : [%s]", GetErrorMessage(r));
-
-       __pListener->OnMmcMountResponseReceived(r);
+       if (__pListener != null)
+       {
+               __pListener->OnMmcMountResponseReceived(r);
+       }
 }
 
 void
 _MmcStorageManagerProxy::UnmountResponseReceived(result r)
 {
        SysLog(NID_IO, "Received Async unmount response msg from server is : [%s]", GetErrorMessage(r));
-
-       __pListener->OnMmcUnmountResponseReceived(r);
+       if (__pListener != null)
+       {
+               __pListener->OnMmcUnmountResponseReceived(r);
+       }
 }
 
 void
 _MmcStorageManagerProxy::FormatResponseReceived(result r)
 {
        SysLog(NID_IO, "Received Async format response msg from server is : [%s]", GetErrorMessage(r));
-
-       __pListener->OnMmcFormatResponseReceived(r);
+       if (__pListener != null)
+       {
+               __pListener->OnMmcFormatResponseReceived(r);
+       }
 }
 
-} } // Tizen::Io
+}} // Tizen::Io
+
@@ -1,5 +1,5 @@
 //
-// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
 // you may not use this file except in compliance with the License.
 //
 
 /**
- * @file       FIo_IMmcStorageServiceEventListener.h
- * @brief      This is the header file for the _IMmcStorageServiceEventListener class.
+ * @file       FIo_IMmcStorageManagerListener.h
+ * @brief      This is the header file for the _IMmcStorageManagerListener class.
  */
 
-#ifndef _FIO_INTERNAL_IMMC_STORAGE_SERVICE_EVENT_LISTENER_H_
-#define _FIO_INTERNAL_IMMC_STORAGE_SERVICE_EVENT_LISTENER_H_
+#ifndef _FIO_INTERNAL_IMMC_STORAGE_MANAGER_LISTENER_H_
+#define _FIO_INTERNAL_IMMC_STORAGE_MANAGER_LISTENER_H_
 
 #include <FBaseResult.h>
 #include <FBaseRtIEventListener.h>
@@ -29,26 +29,28 @@ namespace Tizen { namespace Io
 {
 
 /**
- * @interface _IMmcStorageServiceEventListener
+ * @interface _IMmcStorageManagerListener
  * @brief      This interface defines the listener for the events from _MmcStorageManagerService
- * @since 2.1
+ * @since 2.2
  *
  * Application should not implement this interface.
  */
-class _OSP_EXPORT_ _IMmcStorageServiceEventListener
+class _OSP_EXPORT_ _IMmcStorageManagerListener
        : virtual public Tizen::Base::Runtime::IEventListener
 {
-public:
 
-       ~_IMmcStorageServiceEventListener(void) {}
+public:
+       ~_IMmcStorageManagerListener(void) {}
 
        virtual void OnMmcMountResponseReceived(result r) = 0;
 
        virtual void OnMmcUnmountResponseReceived(result r) = 0;
 
        virtual void OnMmcFormatResponseReceived(result r) = 0;
-}; // _MmcStorageServiceEventListener
 
-} } // Tizen::Io
+}; // _MmcStorageManagerListener
+
+}} // Tizen::Io
+
+#endif // _FIO_INTERNAL_IMMC_STORAGE_MANAGER_LISTENER_H_
 
-#endif // _FIO_INTERNAL_IMMC_STORAGE_SERVICE_EVENT_LISTENER_H_
index 220b6e8..a7a3217 100644 (file)
@@ -29,9 +29,7 @@
 #include <FBaseColLinkedListT.h>
 #include <FBaseRtEvent.h>
 
-#include <FIoMmcStorageManager.h>
-
-#include "FIo_IMmcStorageServiceEventListener.h"
+#include "FIo_IMmcStorageManagerListener.h"
 
 namespace Tizen { namespace Io
 {
@@ -42,7 +40,7 @@ class IMmcStorageMountListener;
 class IMmcStorageFormatListener;
 
 class _OSP_EXPORT_ _MmcStorageManagerImpl
-       : public _IMmcStorageServiceEventListener
+       : public _IMmcStorageManagerListener
 {
 
 public:
@@ -72,9 +70,11 @@ public:
 
 private:
        _MmcStorageManagerImpl(void);
+
        result Construct(void);
 
        _MmcStorageManagerImpl(const _MmcStorageManagerImpl& source);
+
        _MmcStorageManagerImpl& operator =(const _MmcStorageManagerImpl& source);
 
 private:
index f94a8dc..108ada5 100644 (file)
 #include <FBaseString.h>
 
 #include <FIo_IIpcClientEventListener.h>
+#include <FIo_IMmcStorageManagerListener.h>
 
 namespace Tizen { namespace Io
 {
 
-class _IMmcStorageServiceEventListener;
 class _IpcClient;
 
 class _MmcStorageManagerProxy
@@ -39,31 +39,45 @@ class _MmcStorageManagerProxy
 {
 public:
        _MmcStorageManagerProxy(void);
+
        virtual ~_MmcStorageManagerProxy(void);
 
-       result Construct(_IMmcStorageServiceEventListener* pListener);
+       result Construct(void);
 
        result Mount(void);
+
        result Unmount(void);
+
        result Format(void);
 
        virtual void OnIpcResponseReceived(Tizen::Io::_IpcClient& client, const IPC::Message& message);
 
        void MountResponseReceived(result r);
+
        void UnmountResponseReceived(result r);
+
        void FormatResponseReceived(result r);
 
+       void SetMmcStorageManagerListener(_IMmcStorageManagerListener* pListener);
+
+       static _MmcStorageManagerProxy* GetInstance(void);
+
 private:
        _MmcStorageManagerProxy(const _MmcStorageManagerProxy& rhs);
 
        _MmcStorageManagerProxy& operator =(const _MmcStorageManagerProxy& rhs);
 
+       static void InitSingleton(void);
+
+       static void DestroySingleton(void);
+
 private:
+       static _MmcStorageManagerProxy* __pMmcStorageManagerProxyInstance;
        _IpcClient* __pIpcClient;
-       _IMmcStorageServiceEventListener* __pListener;
+       _IMmcStorageManagerListener* __pListener;
 
 }; // _MmcStorageManagerProxy
 
-} } // Tizen::App
+}} // Tizen::Io
 
 #endif // _FIO_INTERNAL_MMC_STORAGE_MANAGER_PROXY_H_