Some synchronizations
[platform/core/security/vasum.git] / server / zones-manager.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Jan Olszak <j.olszak@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author  Jan Olszak (j.olszak@samsung.com)
22  * @brief   Declaration of the class for managing many zones
23  */
24
25
26 #ifndef SERVER_ZONES_MANAGER_HPP
27 #define SERVER_ZONES_MANAGER_HPP
28
29 #include "zone.hpp"
30 #include "zones-manager-config.hpp"
31 #include "host-connection.hpp"
32 #include "input-monitor.hpp"
33 #include "proxy-call-policy.hpp"
34 #include "utils/worker.hpp"
35
36 #include <string>
37 #include <unordered_map>
38 #include <memory>
39
40
41 namespace vasum {
42
43
44 class ZonesManager final {
45
46 public:
47     ZonesManager(const std::string& managerConfigPath);
48     ~ZonesManager();
49
50     /**
51      * Create new zone.
52      *
53      * @param zoneConfig config of new zone
54      */
55     void createZone(const std::string& zoneConfigPath);
56
57     /**
58      * Destroy zone.
59      *
60      * @param zoneId id of the zone
61      */
62     void destroyZone(const std::string& zoneId);
63
64     /**
65      * Focus this zone, put it to the foreground.
66      * Method blocks until the focus is switched.
67      *
68      * @param zoneId id of the zone
69      */
70     void focus(const std::string& zoneId);
71
72     /**
73      * Start up all the configured zones
74      */
75     void startAll();
76
77     /**
78      * Stop all managed zones
79      */
80     void stopAll();
81
82     /**
83      * @return Is the zone in a paused state?
84      */
85     bool isPaused(const std::string& zoneId);
86
87     /**
88      * @return Is the zone running?
89      */
90     bool isRunning(const std::string& zoneId);
91
92     /**
93      * @return id of the currently focused/foreground zone
94      */
95     std::string getRunningForegroundZoneId() const;
96
97     /**
98      * @return id of next to currently focused/foreground zone. If currently focused zone
99      *         is last in zone map, id of fisrt zone from map is returned.
100      */
101     std::string getNextToForegroundZoneId();
102
103     /**
104      * Set whether ZonesManager should detach zones on exit
105      */
106     void setZonesDetachOnExit();
107
108 private:
109     typedef std::recursive_mutex Mutex;
110     typedef std::unique_lock<Mutex> Lock;
111
112     utils::Worker::Pointer mWorker;
113     mutable Mutex mMutex; // used to protect mZones
114     ZonesManagerConfig mConfig; //TODO make it const
115     ZonesManagerDynamicConfig mDynamicConfig;
116     HostConnection mHostConnection;
117     // to hold InputMonitor pointer to monitor if zone switching sequence is recognized
118     std::unique_ptr<InputMonitor> mSwitchingSequenceMonitor;
119     std::unique_ptr<ProxyCallPolicy> mProxyCallPolicy;
120     typedef std::unordered_map<std::string, std::shared_ptr<Zone>> ZoneMap;
121     ZoneMap mZones; // map of zones, id is the key
122     bool mDetachOnExit;
123
124     void saveDynamicConfig();
125     void switchingSequenceMonitorNotify();
126     void generateNewConfig(const std::string& id,
127                            const std::string& templatePath,
128                            const std::string& resultPath);
129
130     void notifyActiveZoneHandler(const std::string& caller,
131                                  const std::string& appliaction,
132                                  const std::string& message);
133     void displayOffHandler(const std::string& caller);
134     void handleZoneMoveFileRequest(const std::string& srcZoneId,
135                                    const std::string& dstZoneId,
136                                    const std::string& path,
137                                    dbus::MethodResultBuilder::Pointer result);
138     void handleProxyCall(const std::string& caller,
139                          const std::string& target,
140                          const std::string& targetBusName,
141                          const std::string& targetObjectPath,
142                          const std::string& targetInterface,
143                          const std::string& targetMethod,
144                          GVariant* parameters,
145                          dbus::MethodResultBuilder::Pointer result);
146     void handleGetZoneDbuses(dbus::MethodResultBuilder::Pointer result) const;
147     void handleDbusStateChanged(const std::string& zoneId, const std::string& dbusAddress);
148     void handleGetZoneIdsCall(dbus::MethodResultBuilder::Pointer result) const;
149     void handleGetActiveZoneIdCall(dbus::MethodResultBuilder::Pointer result);
150     void handleGetZoneInfoCall(const std::string& id, dbus::MethodResultBuilder::Pointer result);
151     void handleDeclareFileCall(const std::string& zone,
152                                const int32_t& type,
153                                const std::string& path,
154                                const int32_t& flags,
155                                const int32_t& mode,
156                                dbus::MethodResultBuilder::Pointer result);
157     void handleDeclareMountCall(const std::string& source,
158                                 const std::string& zone,
159                                 const std::string& target,
160                                 const std::string& type,
161                                 const uint64_t& flags,
162                                 const std::string& data,
163                                 dbus::MethodResultBuilder::Pointer result);
164     void handleDeclareLinkCall(const std::string& source,
165                                const std::string& zone,
166                                const std::string& target,
167                                dbus::MethodResultBuilder::Pointer result);
168     void handleSetActiveZoneCall(const std::string& id,
169                                  dbus::MethodResultBuilder::Pointer result);
170     void handleCreateZoneCall(const std::string& id,
171                               dbus::MethodResultBuilder::Pointer result);
172     void handleDestroyZoneCall(const std::string& id,
173                                dbus::MethodResultBuilder::Pointer result);
174     void handleShutdownZoneCall(const std::string& id,
175                                 dbus::MethodResultBuilder::Pointer result);
176     void handleStartZoneCall(const std::string& id,
177                              dbus::MethodResultBuilder::Pointer result);
178     void handleLockZoneCall(const std::string& id,
179                             dbus::MethodResultBuilder::Pointer result);
180     void handleUnlockZoneCall(const std::string& id,
181                               dbus::MethodResultBuilder::Pointer result);
182     void handleGrantDeviceCall(const std::string& id,
183                                const std::string& device,
184                                uint32_t flags,
185                                dbus::MethodResultBuilder::Pointer result);
186     void handleRevokeDeviceCall(const std::string& id,
187                                 const std::string& device,
188                                 dbus::MethodResultBuilder::Pointer result);
189 };
190
191
192 } // namespace vasum
193
194
195 #endif // SERVER_ZONES_MANAGER_HPP