Use dbus for event notification
[platform/core/security/ode.git] / server / server.h
1 /*
2  *  Copyright (c) 2015 - 2019 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 #ifndef __ODE_SERVER_H__
18 #define __ODE_SERVER_H__
19
20 #include <string>
21 #include <memory>
22 #include <mutex>
23
24 #include <gio/gio.h>
25
26 #include <klay/file-descriptor.h>
27 #include <klay/rmi/service.h>
28
29 #include "event.h"
30
31 namespace ode {
32
33 class SecureEraseServer;
34 class InternalEncryptionServer;
35 class ExternalEncryptionServer;
36 class LuksServer;
37 class KeyServer;
38
39 class ServerContext final: public rmi::Service {
40 public:
41         ServerContext();
42         ~ServerContext();
43
44         bool checkPeerPrivilege(const rmi::Credentials& cred, const std::string& privilege);
45
46         void requestStarted();
47         void requestFinished();
48
49         void dbusRegisterObjects(GDBusConnection *connection);
50
51 private:
52         void timerFunc();
53
54         std::unique_ptr<SecureEraseServer> secureErase;
55         std::unique_ptr<InternalEncryptionServer> internalEncryption;
56         std::unique_ptr<ExternalEncryptionServer> externalEncryption;
57         std::unique_ptr<LuksServer> luks;
58         std::unique_ptr<KeyServer> keys;
59
60         std::mutex timerMutex;
61         unsigned currentRequests;
62         bool newRequests;
63 };
64
65 class RequestLifetime {
66 public:
67         explicit RequestLifetime(ServerContext& c) : ctx(&c) {
68                 ctx->requestStarted();
69         }
70
71         RequestLifetime(RequestLifetime&& other) {
72                 ctx = other.ctx;
73                 other.ctx = NULL;
74         }
75
76         ~RequestLifetime() {
77                 if (ctx)
78                         ctx->requestFinished();
79         }
80
81         RequestLifetime(const RequestLifetime&) = delete;
82         RequestLifetime& operator=(const RequestLifetime&) = delete;
83
84 private:
85         ServerContext* ctx;
86 };
87
88 } // namespace ode
89
90 #endif //__ODE_SERVER_H__