Tizen 2.1 base
[platform/framework/web/wrt-security.git] / src / daemon / security_daemon.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        security_daemon.h
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     1.0
20  * @brief       This is header file of Security Daemon
21  */
22
23 #ifndef WRT_SRC_SECURITY_DAEMON_SECURITY_DAEMON_H
24 #define WRT_SRC_SECURITY_DAEMON_SECURITY_DAEMON_H
25
26 #include <utility>
27 #include <memory>
28 #include <list>
29 #include <dpl/noncopyable.h>
30 #include <dpl/singleton.h>
31 #include <dpl/assert.h>
32
33 namespace SecurityDaemon {
34
35 class DaemonService : DPL::Noncopyable {
36   public:
37     virtual void initialize() = 0;
38     virtual void start() = 0;
39     virtual void stop() = 0;
40     virtual void deinitialize() = 0;
41 };
42
43 class SecurityDaemon : DPL::Noncopyable
44 {
45   public:
46     SecurityDaemon();
47
48     void initialize(int& argc, char** argv);
49     int execute();
50     void terminate(int returnValue = 0);
51
52     template<typename ServiceType, typename ...Args>
53     void registerService(Args&&... args)
54     {
55         Assert(!m_initialized && "Too late for registration");
56
57         m_servicesList.push_back(
58                 std::make_shared<ServiceType>(std::forward<Args>(args)...));
59     }
60
61     void shutdown();
62
63   private:
64     bool m_initialized;
65     bool m_terminating;
66     int m_returnValue;
67     typedef std::list<std::shared_ptr<DaemonService>> DaemonServiceList;
68     DaemonServiceList m_servicesList;
69 };
70
71 namespace DatabaseService {
72     void initialize();
73     void deinitialize();
74 };
75
76 } //namespace SecurityDaemon
77
78 typedef DPL::Singleton<SecurityDaemon::SecurityDaemon> SecurityDaemonSingleton;
79
80 #define DAEMON_REGISTER_SERVICE_MODULE(Type)                                \
81     namespace {                                                             \
82         static int initializeModule();                                      \
83         static int initializeModuleHelper = initializeModule();             \
84         int initializeModule()                                              \
85         {                                                                   \
86             (void)initializeModuleHelper;                                   \
87             SecurityDaemonSingleton::Instance().registerService<Type>();    \
88             return 0;                                                       \
89         }                                                                   \
90     }
91
92
93 #endif /* WRT_SRC_SECURITY_DAEMON_SECURITY_DAEMON_H */