Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FApp_NotificationManagerStub.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file         FApp_NotificationManagerStub.cpp
20  * @brief       This is the implementation for the _NotificationManagerStub class.
21  */
22 #include <cstdio>
23 #include <dlfcn.h>
24
25 #include <FBaseSysLog.h>
26 #include <FBase_StringConverter.h>
27 #include <FIo_IpcServer.h>
28 #include <FSec_AccessController.h>
29 #include <FShell_NotificationManagerIpcMessages.h>
30
31 #include "FApp_NotificationManagerService.h"
32 #include "FApp_NotificationManagerStub.h"
33
34
35 namespace Tizen { namespace App {
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Io;
40 using namespace Tizen::Security;
41 using namespace Tizen::Shell;
42
43
44 ///////////////////////////////////////////
45 // _NotificationManagerStub
46 ///////////////////////////////////////////
47
48 _NotificationManagerStub::_NotificationManagerStub()
49         :__pIpcServer(null)
50         ,__pNotificationManagerService(null)
51 {
52         SysLog(NID_APP, "_NotificationManagerStub - Enter\n");
53 }
54
55 _NotificationManagerStub::~_NotificationManagerStub()
56 {
57         if ( __pIpcServer != null)
58         {
59                 __pIpcServer->Stop();
60                 delete __pIpcServer;
61         }
62
63         SysLog(NID_APP, "_NotificationManagerStub - Exit\n");
64 }
65
66 result
67 _NotificationManagerStub::Construct(void)
68 {
69         SysLog(NID_APP, "Enter.");
70
71         __pNotificationManagerService = new (std::nothrow) _NotificationManagerService();
72         SysTryReturnResult(NID_APP, __pNotificationManagerService != null, E_OUT_OF_MEMORY, "Not enough memory.");
73
74         result r = E_SUCCESS;
75         r = __pNotificationManagerService->Construct();
76         SysTryReturn(NID_APP, !IsFailed(r), r, r, "failed to __pNotificationManagerService->Construct.(%s)", GetErrorMessage(r) );
77
78         r = StartIpcServer();
79         SysTryReturn(NID_APP, !IsFailed(r), r, r, "failed to StartIpcServer.(%s)", GetErrorMessage(r) );
80
81         SysLog(NID_APP, "Exit.");
82         return E_SUCCESS;
83 }
84
85 result
86 _NotificationManagerStub::StartIpcServer(void)
87 {
88         SysLog(NID_APP, "_NotificationManagerStub - StartIpcServer");
89
90         __pIpcServer = new (std::nothrow) _IpcServer();
91         SysTryReturnResult(NID_APP, __pIpcServer != null, E_OUT_OF_MEMORY, "Not enough memory.");
92
93         result r = __pIpcServer->Construct( "osp.app.ipcserver.notificationmanager", *this);
94         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to create IPC server(%s)", GetErrorMessage(r), "osp.app.ipcserver.notificationmanager");
95
96         r = __pIpcServer->Start();
97         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to Start IPC server(%s)", GetErrorMessage(r), "osp.app.ipcserver.notificationmanager");
98
99         return E_SUCCESS;
100
101 CATCH:
102         delete __pIpcServer;
103         __pIpcServer = null;
104         return r;
105 }
106
107 void
108 _NotificationManagerStub::OnNotifyMessage(const AppId& appId, const NotificationRequest& notiMessage,bool Ongoing, result* pRes)
109 {
110         SysTryReturnVoidResult(NID_APP, __pNotificationManagerService != null, E_INVALID_STATE, "Invalid Notification manager state.");
111
112         *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_NOTIFICATIONMANAGER);
113         SysTryReturnVoidResult(NID_APP, !IsFailed(*pRes), *pRes = E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
114
115         *pRes = __pNotificationManagerService->NotifyMessage(appId, notiMessage,Ongoing);
116 }
117
118 void
119 _NotificationManagerStub::OnRemoveNotification(const AppId& appId, bool Ongoing, result* pRes)
120 {
121         SysTryReturnVoidResult(NID_APP, __pNotificationManagerService != null, E_INVALID_STATE, "Invalid Notification manager state.");
122
123         *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_NOTIFICATIONMANAGER);
124         SysTryReturnVoidResult(NID_APP, !IsFailed(*pRes), *pRes = E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
125
126         *pRes = __pNotificationManagerService->RemoveNotification(appId, Ongoing);
127 }
128
129 ///////////////////////////////////////////
130 // ipc handlers
131 ///////////////////////////////////////////
132 void
133 _NotificationManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
134 {
135         SysLog(NID_APP, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), server.GetClientId());
136
137         IPC_BEGIN_MESSAGE_MAP(_NotificationManagerStub, message)
138                 IPC_MESSAGE_HANDLER_EX(NotificationManager_NotifyMessage, &server, OnNotifyMessage)
139                 IPC_MESSAGE_HANDLER_EX(NotificationManager_RemoveNotification, &server, OnRemoveNotification)
140         IPC_END_MESSAGE_MAP()
141 }
142
143 void
144 _NotificationManagerStub::OnIpcServerStarted(const _IpcServer& server)
145 {
146         SysLog(NID_APP, "_NotificationManagerStub::OnIpcServerStarted \n");
147 }
148
149 void
150 _NotificationManagerStub::OnIpcServerStopped(const _IpcServer& server)
151 {
152         SysLog(NID_APP, "_NotificationManagerStub::OnIpcServerStopped \n");
153 }
154
155 void
156 _NotificationManagerStub::OnIpcClientConnected(const _IpcServer& server, int clientId)
157 {
158         SysLog(NID_APP, "_NotificationManagerStub::OnIpcClientConnected (clientId:%d)\n", clientId);
159 }
160
161 void
162 _NotificationManagerStub::OnIpcClientDisconnected(const _IpcServer&server, int clientId)
163 {
164         SysLog(NID_APP, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), clientId);
165 }
166
167 }} //namespace Tizen { namespace App {