c4ea037435c85d588052e7b36d409f02cd11da9c
[platform/framework/native/shell.git] / src / core / FShell_LockManagerProxy.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         FShell_LockManagerProxy.cpp
19  * @brief       This is the implementation for the _LockManagerProxy.cpp class.
20  */
21
22 #include <new>
23 #include <memory>
24 #include <unique_ptr.h>
25 #include <FBaseErrors.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseRt.h>
28 #include <FIo_IpcClient.h>
29
30 #include "FShell_LockManagerIpcMessages.h"
31 #include "FShell_LockManagerProxy.h"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Io;
35 using namespace Tizen::Shell;
36
37 namespace
38 {
39 const char IPC_SERVER_NAME[] = "osp.shell.ipcserver.lockmanager";
40 };
41
42 namespace Tizen { namespace Shell
43 {
44
45 _LockManagerProxy* _LockManagerProxy::__pTheInstance = null;
46
47 _LockManagerProxy::_LockManagerProxy(void)
48         : __pIpcClient(null)
49 {
50 }
51
52 _LockManagerProxy::~_LockManagerProxy(void)
53 {
54 }
55
56 void
57 _LockManagerProxy::InitSingleton(void)
58 {
59         std::unique_ptr<_LockManagerProxy> pInst(new (std::nothrow) _LockManagerProxy());
60         SysTryReturnVoidResult(NID_SHELL, pInst, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
61
62         result r = pInst->Construct();
63         SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
64
65         __pTheInstance = pInst.release();
66         std::atexit(DestroySingleton);
67 }
68
69 void
70 _LockManagerProxy::DestroySingleton(void)
71 {
72         delete __pTheInstance;
73 }
74
75 _LockManagerProxy*
76 _LockManagerProxy::GetInstance(void)
77 {
78         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
79         if (__pTheInstance == null)
80         {
81                 ClearLastResult();
82                 pthread_once(&onceBlock, InitSingleton);
83                 result r = GetLastResult();
84                 if (IsFailed(r))
85                 {
86                         onceBlock = PTHREAD_ONCE_INIT;
87                 }
88         }
89         return __pTheInstance;
90 }
91
92 result
93 _LockManagerProxy::Construct(void)
94 {
95         SysLog(NID_SHELL, "Enter.");
96
97         __pIpcClient = new (std::nothrow) _IpcClient();
98         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
99
100         const int MAX_TRY_COUNT = 5;
101         const int TRY_SLEEP_TIME = 250;
102
103         int count = 0;
104         while (true)
105         {
106                 result r = __pIpcClient->Construct(IPC_SERVER_NAME);
107                 if (r == E_SUCCESS)
108                 {
109                         SysLog(NID_APP, "Succeeded in connecting service(%s)", IPC_SERVER_NAME);
110                         return E_SUCCESS;
111                 }
112
113                 SysTryReturn(NID_APP, count < MAX_TRY_COUNT, E_SYSTEM, r, "[%s] Failed to connect service.(%s)", GetErrorMessage(r), IPC_SERVER_NAME);
114
115                 count++;
116                 Tizen::Base::Runtime::Thread::Sleep(TRY_SLEEP_TIME);
117         }
118
119         SysLog(NID_SHELL, "Exit.");
120         return E_SUCCESS;
121 }
122
123 result
124 _LockManagerProxy::Unlock(void)
125 {
126       SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
127
128       result response = E_SUCCESS;
129
130       std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) LockManager_Unlock(&response));
131       result r = __pIpcClient->SendRequest(*pMsg.get());
132       SysTryReturnResult(NID_APP, !IsFailed(r), r, "SendRequest is failed.");
133
134       return response;
135 }
136
137 } } // Tizen::Shell