Fix pkg-config files from returning wrong relative paths.
[platform/core/security/vasum.git] / zone-daemon / daemon-connection.cpp
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   Dbus API for the Zone Daemon
23  */
24
25 #include "config.hpp"
26
27 #include "daemon-connection.hpp"
28 #include "daemon-dbus-definitions.hpp"
29 #include "exception.hpp"
30
31 #include "logger/logger.hpp"
32
33
34 namespace vasum {
35 namespace zone_daemon {
36
37 namespace {
38
39 // Timeout in ms for waiting for dbus name.
40 // Can happen if glib loop is busy or not present.
41 const unsigned int NAME_ACQUIRED_TIMEOUT = 5 * 1000;
42
43 } // namespace
44
45
46 DaemonConnection::DaemonConnection(const NameLostCallback& nameLostCallback,
47                                    const GainFocusCallback& gainFocusCallback,
48                                    const LoseFocusCallback& loseFocusCallback)
49     : mNameAcquired(false)
50     , mNameLost(false)
51 {
52     LOGD("Connecting to DBUS on system bus");
53     mDbusConnection = dbus::DbusConnection::createSystem();
54
55     LOGD("Setting DBUS name");
56     mDbusConnection->setName(zone_daemon::api::BUS_NAME,
57                              std::bind(&DaemonConnection::onNameAcquired, this),
58                              std::bind(&DaemonConnection::onNameLost, this));
59
60     if (!waitForNameAndSetCallback(NAME_ACQUIRED_TIMEOUT, nameLostCallback)) {
61         LOGE("Could not acquire dbus name: " << zone_daemon::api::BUS_NAME);
62         throw ZoneDaemonException("Could not acquire dbus name: " + zone_daemon::api::BUS_NAME);
63     }
64
65     LOGD("Setting callbacks");
66     mGainFocusCallback = gainFocusCallback;
67     mLoseFocusCallback = loseFocusCallback;
68
69
70     LOGD("Registering DBUS interface");
71     using namespace std::placeholders;
72     mDbusConnection->registerObject(zone_daemon::api::OBJECT_PATH,
73                                     zone_daemon::api::DEFINITION,
74                                     std::bind(&DaemonConnection::onMessageCall,
75                                               this, _1, _2, _3, _4, _5),
76                                     nullptr);
77     LOGD("Connected");
78 }
79
80 DaemonConnection::~DaemonConnection()
81 {
82 }
83
84 bool DaemonConnection::waitForNameAndSetCallback(const unsigned int timeoutMs,
85                                                  const NameLostCallback& nameLostCallback)
86 {
87     std::unique_lock<std::mutex> lock(mNameMutex);
88     mNameCondition.wait_for(lock,
89                             std::chrono::milliseconds(timeoutMs),
90     [this] {
91         return mNameAcquired || mNameLost;
92     });
93
94     if (mNameAcquired) {
95         mNameLostCallback = nameLostCallback;
96     }
97
98     return mNameAcquired;
99 }
100
101 void DaemonConnection::onNameAcquired()
102 {
103     std::unique_lock<std::mutex> lock(mNameMutex);
104     mNameAcquired = true;
105     mNameCondition.notify_one();
106 }
107
108 void DaemonConnection::onNameLost()
109 {
110     std::unique_lock<std::mutex> lock(mNameMutex);
111     mNameLost = true;
112     mNameCondition.notify_one();
113
114     if (mNameLostCallback) {
115         mNameLostCallback();
116     }
117 }
118
119 void DaemonConnection::onMessageCall(const std::string& objectPath,
120                                      const std::string& interface,
121                                      const std::string& methodName,
122                                      GVariant* /*parameters*/,
123                                      dbus::MethodResultBuilder::Pointer result)
124 {
125     if (objectPath != api::OBJECT_PATH || interface != api::INTERFACE) {
126         return;
127     }
128
129     if (methodName == api::METHOD_GAIN_FOCUS) {
130         if (mGainFocusCallback) {
131             mGainFocusCallback();
132             result->setVoid();
133         }
134     } else if (methodName == api::METHOD_LOSE_FOCUS) {
135         if (mLoseFocusCallback) {
136             mLoseFocusCallback();
137             result->setVoid();
138         }
139     }
140 }
141
142 } // namespace zone_daemon
143 } // namespace vasum