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