7e72df4b643be97399fabb8168844e6d4eaf3734
[platform/core/context/context-common.git] / include / ServiceBase.h
1 /*
2  * Copyright (c) 2017 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 #ifndef __CONTEXT_SERVICE_BASE_H__
18 #define __CONTEXT_SERVICE_BASE_H__
19
20 #include <string>
21 #include <map>
22 #include <ContextTypes.h>
23
24 namespace ctx {
25
26         /* Forward declarations */
27         class ClientBase;
28         class MethodCall;
29
30         class EXPORT_API ServiceBase {
31         public:
32                 virtual ~ServiceBase();
33
34                 // These functions should not be called from individual services
35                 bool start();
36                 void stop();
37                 void notifyUserNew();
38                 void notifyUserRemoved();
39                 static void setActiveUser(uid_t uid);
40                 static void setSingleThreading();
41                 // ---
42
43                 // If the GVariant 'param' is floating, it is consumed.
44                 void publish(const std::string& busName, const std::string& signalName, GVariant* param);
45
46                 GDBusConnection* getConnection();
47
48                 static uid_t getActiveUser();
49
50                 virtual bool isUserService() = 0;
51
52         protected:
53                 ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs);
54
55                 virtual bool prepare() = 0;
56
57                 virtual void cleanup() = 0;
58
59                 virtual ClientBase* createClient(const std::string& busName) = 0;
60
61                 virtual void onUserActivated();
62
63                 virtual void onUserDeactivated();
64
65         private:
66                 static gpointer __threadFunc(gpointer data);
67
68                 static void __onMethodCalled(GDBusConnection* conn, const gchar* sender,
69                                 const gchar* path, const gchar* iface, const gchar* name,
70                                 GVariant* param, GDBusMethodInvocation* invocation, gpointer userData);
71
72                 static void __onNameOwnerChanged(GDBusConnection* conn, const gchar* sender,
73                                 const gchar* path, const gchar* iface, const gchar* name,
74                                 GVariant* param, gpointer userData);
75
76                 static gboolean __onUserActivated(gpointer data);
77
78                 static gboolean __onUserDeactivated(gpointer data);
79
80                 static gboolean __stopMainLoop(gpointer data);
81
82                 void __onMethodCalled(const std::string& sender,
83                                 const std::string& name, GVariant* param, GDBusMethodInvocation* invocation);
84
85                 void __run();
86                 bool __init();
87                 void __release();
88
89                 ClientBase* __getClient(const std::string& busName);
90                 void __removeClient(const std::string& busName);
91                 unsigned int __watch(const std::string& busName, ClientBase* client);
92                 void __unwatch(unsigned int watchId);
93
94                 bool __running;
95                 const char* __serviceName;
96
97                 GMainContext* __mainContext;
98                 GMainLoop* __mainLoop;
99                 GThread* __gthread;
100
101                 GDBusConnection* __connection;
102                 const char* __methodSpecs;
103                 std::string __objPath;
104                 std::string __interface;
105
106                 GDBusNodeInfo* __nodeInfo;
107                 guint __registrationId;
108
109                 struct ClientInfo {
110                         ClientBase* client;
111                         unsigned int watchId;
112                 };
113
114                 std::map<std::string, ClientInfo> __clients;
115
116                 static bool __singleThreading;
117         };
118
119 }
120
121 #endif  /* __CONTEXT_SERVICE_BASE_H__ */