Fix N_SE-56436 for Screen lock.
[platform/framework/native/appfw.git] / src / system / FSys_SystemServiceMessageClient.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                FSys_SystemServiceMessageClient.cpp
19  * @brief               This is the implementation file for _SystemServiceMessageClient class.
20  */
21 #include <unistd.h>
22 #include <sys/types.h>
23
24 #include <FApp_AppInfo.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseRtThread.h>
27 #include <FIo_AppServiceIpcMessages.h>
28 #include "FSys_SystemServiceMessageClient.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Io;
34
35 namespace Tizen { namespace System
36 {
37 _SystemServiceMessageClient::_SystemServiceMessageClient()
38         : __pIpcClient(null)
39         , __pSystemServiceMessageListener(null)
40 {
41 }
42
43 _SystemServiceMessageClient::_SystemServiceMessageClient(const String id)
44         : __pIpcClient(null)
45         , __pSystemServiceMessageListener(null)
46 {
47         result r = E_SUCCESS;
48         std::unique_ptr< _IpcClient > pIpcClient(new (std::nothrow) _IpcClient());
49         SysTryReturn(NID_SYS, pIpcClient != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It is failed to create IPC client");
50
51         r = pIpcClient->Construct(id, this);
52         SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
53
54         __pIpcClient = std::move(pIpcClient);
55         SetLastResult(r);
56 }
57 _SystemServiceMessageClient::~_SystemServiceMessageClient()
58 {
59 }
60
61 _SystemServiceMessageClient*
62 _SystemServiceMessageClient::CreateInstance(const Tizen::Base::String id)
63 {
64         _SystemServiceMessageClient* pSystemServiceMessageClient = new (std::nothrow) _SystemServiceMessageClient(id);
65         SysTryReturn(NID_SYS, pSystemServiceMessageClient, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It is not enough memory.");
66         return pSystemServiceMessageClient;
67 }
68
69 _IpcClient*
70 _SystemServiceMessageClient::GetIpcClient()
71 {
72         return __pIpcClient.get();
73 }
74
75 result
76 _SystemServiceMessageClient::RegisterListener(const String key, _ICommunicationDispatcherListener& listener)
77 {       
78         SysTryReturnResult(NID_SYS, !__pSystemServiceMessageListener, E_OBJ_ALREADY_EXIST,
79                 "SystemService listener was set already.[%ls]", key.GetPointer());
80         __pSystemServiceMessageListener = &listener;
81         return E_SUCCESS;
82 }
83
84 result
85 _SystemServiceMessageClient::UnregisterListener(const String key)
86 {
87         SysTryReturnResult(NID_SYS, __pSystemServiceMessageListener, E_OBJ_NOT_FOUND,
88                 "SystemService listener is not set.[%ls]",key.GetPointer());
89         __pSystemServiceMessageListener = null;
90         return E_SUCCESS;
91 }
92
93 void
94 _SystemServiceMessageClient::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
95 {
96         SysLog(NID_SYS, "Enter.");
97         IPC_BEGIN_MESSAGE_MAP(_SystemServiceMessageClient, message)
98                 IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
99         IPC_END_MESSAGE_MAP_EX()
100         SysLog(NID_SYS, "Exit.");
101 }
102
103 void
104 _SystemServiceMessageClient::OnDataReceived(const ArrayList& data)
105 {
106         result r = E_SUCCESS;
107         String* pServiceId = (String*) data.GetAt(0);
108
109         SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service id.");
110         SysLog(NID_SYS, "Service id = %ls", pServiceId->GetPointer());
111
112         SysTryReturnVoidResult(NID_SYS, __pSystemServiceMessageListener, E_SYSTEM,
113                 "Service[%ls] is available, but listener does not exist. [%s]", pServiceId->GetPointer(), GetErrorMessage(r));
114
115         __pSystemServiceMessageListener->OnDataReceived(data);
116
117         SysLog(NID_SYS, "Message is delivered to \"%ls\"[%x]", pServiceId->GetPointer(), __pSystemServiceMessageListener);
118         ArrayList* temp = const_cast< ArrayList *> (&data);
119         temp->RemoveAll(true);
120 }
121
122 void
123 _SystemServiceMessageClient::OnIpcServerDisconnected(_IpcClient& client)
124 {
125         SysLogException(NID_SYS, E_SYSTEM, "The IpcServer was disconnected. [%ls].", client.GetName().GetPointer());
126         result r = E_SUCCESS;
127         std::unique_ptr< _IpcClient > pIpcClient(new (std::nothrow) _IpcClient());
128         SysTryReturn(NID_SYS, pIpcClient != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It is failed to create IPC client");
129
130         int retry = 0;
131         do
132         {
133                 r = pIpcClient->Construct(client.GetName(), this);
134                 retry++;
135                 Tizen::Base::Runtime::Thread::Sleep(1000);
136         }while (r != E_SUCCESS && retry < 10);
137
138         SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
139
140         __pIpcClient.reset(pIpcClient.release());
141 }
142
143 }}