Fix N_SE-56436 for Screen lock.
[platform/framework/native/appfw.git] / src / system / FSys_CommunicationDispatcherClient.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_CommunicationDispatcherClient.cpp
19  * @brief               This is the implementation file for _CommunicationDispatcherClient class.
20  */
21
22 #include <FApp_AppInfo.h>
23 #include <FBaseSysLog.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <FIo_AppServiceIpcMessages.h>
27
28 #include "FSys_CommunicationDispatcherClient.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 static const wchar_t* _APPLICATION_ID_APP_SERVICE = L"aospd000043.osp-app-service";
38 bool ipcReady = false;
39 static const wchar_t* _COMMUNICATION_DISPATCHER_IPC_ID = L"osp.app.ipcserver.communicationdispatcher";
40
41 _CommunicationDispatcherClient* _CommunicationDispatcherClient::__pCommunicationDispatcherClient = null;
42
43 _CommunicationDispatcherClient::_CommunicationDispatcherClient()
44 : __pIpcClient(null)
45 {
46         __communicationDispatcherClientList.Construct(0, 0, __strHashCodeProvider, __strComparer);
47
48         result r = E_SUCCESS;
49
50         if(_AppInfo::GetApplicationId() != _APPLICATION_ID_APP_SERVICE)
51         {
52                 __pIpcClient = new (std::nothrow) _IpcClient();
53                 SysTryCatch(NID_SYS, __pIpcClient != null, , r, "It is failed to create IPC client");
54
55                 r = __pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID, this);
56                 if(r != E_SUCCESS)
57                         ipcReady = false;
58                 else
59                         ipcReady = true;
60                 SysTryCatch(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
61         }
62         else
63         {
64                 SysLogException(NID_SYS, E_SYSTEM, "It is required by osp-app-service.");
65         }
66
67 CATCH:
68         SetLastResult(r);
69
70 }
71
72 _CommunicationDispatcherClient::~_CommunicationDispatcherClient()
73 {
74         delete __pIpcClient;
75         __pIpcClient = null;
76
77         __communicationDispatcherClientList.RemoveAll();
78 }
79
80 _CommunicationDispatcherClient*
81 _CommunicationDispatcherClient::GetInstance()
82 {
83         if(__pCommunicationDispatcherClient == null)
84         {
85                 __pCommunicationDispatcherClient = new (std::nothrow) _CommunicationDispatcherClient();
86         }
87         return __pCommunicationDispatcherClient;
88 }
89
90 _IpcClient*
91 _CommunicationDispatcherClient::GetIpcClient()
92 {
93         if(ipcReady == false)
94                 return null;
95
96         return __pIpcClient;
97 }
98
99 result
100 _CommunicationDispatcherClient::RegisterCommunicationListener(String key, _ICommunicationDispatcherListener& listener)
101 {
102         result r = E_SUCCESS;
103         bool exist = false;
104
105         r = __communicationDispatcherClientList.ContainsKey(key, exist);
106         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
107         SysTryReturnResult(NID_SYS, exist == false, E_OBJ_ALREADY_EXIST, "Required key is already registered.");
108         r = __communicationDispatcherClientList.Add(key, &listener);
109
110         return r;
111 }
112
113 result
114 _CommunicationDispatcherClient::UnregisterCommunicationListener(String key)
115 {
116         result r = E_SUCCESS;
117         bool exist = false;
118         r = __communicationDispatcherClientList.ContainsKey(key, exist);
119         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
120         SysTryReturnResult(NID_SYS, exist == true, E_OBJ_NOT_FOUND, "Required key is not registered.");
121         r = __communicationDispatcherClientList.Remove(key);
122         return r;
123 }
124
125 void
126 _CommunicationDispatcherClient::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
127 {
128         SysLog(NID_SYS, "Enter.");
129         IPC_BEGIN_MESSAGE_MAP(_CommunicationDispatcherClient, message)
130         IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
131         IPC_END_MESSAGE_MAP_EX()
132         SysLog(NID_SYS, "Exit.");
133 }
134
135 void
136 _CommunicationDispatcherClient::OnDataReceived(const ArrayList& data)
137 {
138         result r = E_SUCCESS;
139         String* pServiceId = (String*) data.GetAt(0);
140
141          _ICommunicationDispatcherListener* listener = null;
142
143         SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service id.");
144
145         r = __communicationDispatcherClientList.GetValue(*pServiceId, listener);
146
147         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS && listener != null, E_SYSTEM, "Service[%ls] is available, but listener does not exist. [%s]", pServiceId->GetPointer(), GetErrorMessage(r));
148
149         listener->OnDataReceived(data);
150
151         SysLog(NID_SYS, "Message is delivered to \"%ls\"[%x]", pServiceId->GetPointer(), listener);
152         ArrayList* temp = const_cast< ArrayList *> (&data);
153         temp->RemoveAll(true);
154 }
155
156 }}