blob: 185ee125b452a7d6350fe9b1c8d2c2aaf1ad3dcb [file] [log] [blame]
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +01001// Copyright 2015 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/permission_broker_proxy.h"
6
7#include <string>
8#include <vector>
9
10#include <chromeos/dbus/service_constants.h>
11
12#include "shill/logging.h"
13
14namespace shill {
15// static
16const int PermissionBrokerProxy::kInvalidHandle = -1;
17
18PermissionBrokerProxyInterface::PermissionBrokerProxyInterface() {}
19
20PermissionBrokerProxyInterface::~PermissionBrokerProxyInterface() {}
21
Paul Stewart1a212a62015-06-16 13:13:10 -070022PermissionBrokerProxy::PermissionBrokerProxy(DBus::Connection* connection)
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010023 : proxy_(connection),
24 lifeline_read_fd_(kInvalidHandle),
25 lifeline_write_fd_(kInvalidHandle) {}
26
27PermissionBrokerProxy::~PermissionBrokerProxy() {}
28
29bool PermissionBrokerProxy::RequestVpnSetup(
Paul Stewart1a212a62015-06-16 13:13:10 -070030 const std::vector<std::string>& user_names,
31 const std::string& interface) {
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010032 if (lifeline_read_fd_ != kInvalidHandle ||
33 lifeline_write_fd_ != kInvalidHandle) {
34 LOG(ERROR) << "Already setup?";
35 return false;
36 }
37
38 int fds[2];
39 if (pipe(fds) != 0) {
40 LOG(ERROR) << "Failed to create lifeline pipe";
41 return false;
42 }
43 lifeline_read_fd_ = fds[0];
44 lifeline_write_fd_ = fds[1];
45
46 DBus::FileDescriptor dbus_fd(lifeline_read_fd_);
47 bool return_value = false;
48 try {
49 return_value = proxy_.RequestVpnSetup(user_names, interface, dbus_fd);
Paul Stewart1a212a62015-06-16 13:13:10 -070050 } catch (const DBus::Error& e) {
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010051 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what();
52 }
53 return return_value;
54}
55
56bool PermissionBrokerProxy::RemoveVpnSetup() {
Prabhu Kaliamoorthie0e205b2015-02-20 14:46:23 +010057 bool return_value = true;
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010058 if (lifeline_read_fd_ != kInvalidHandle &&
59 lifeline_write_fd_ != kInvalidHandle) {
60 close(lifeline_read_fd_);
61 close(lifeline_write_fd_);
62 lifeline_read_fd_ = kInvalidHandle;
63 lifeline_write_fd_ = kInvalidHandle;
Prabhu Kaliamoorthie0e205b2015-02-20 14:46:23 +010064 try {
65 return_value = proxy_.RemoveVpnSetup();
Paul Stewart1a212a62015-06-16 13:13:10 -070066 } catch (const DBus::Error& e) {
Prabhu Kaliamoorthie0e205b2015-02-20 14:46:23 +010067 return_value = false;
68 LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what();
69 }
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010070 }
Prabhu Kaliamoorthie0e205b2015-02-20 14:46:23 +010071 return return_value;
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010072}
73
Paul Stewart1a212a62015-06-16 13:13:10 -070074PermissionBrokerProxy::Proxy::Proxy(DBus::Connection* connection)
Prabhu Kaliamoorthi77e76832015-02-13 15:20:23 +010075 : DBus::ObjectProxy(*connection,
76 permission_broker::kPermissionBrokerServicePath,
77 permission_broker::kPermissionBrokerServiceName) {}
78
79PermissionBrokerProxy::Proxy::~Proxy() {}
80
81} // namespace shill