Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 1 | // 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 | |
| 14 | namespace shill { |
| 15 | // static |
| 16 | const int PermissionBrokerProxy::kInvalidHandle = -1; |
| 17 | |
| 18 | PermissionBrokerProxyInterface::PermissionBrokerProxyInterface() {} |
| 19 | |
| 20 | PermissionBrokerProxyInterface::~PermissionBrokerProxyInterface() {} |
| 21 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 22 | PermissionBrokerProxy::PermissionBrokerProxy(DBus::Connection* connection) |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 23 | : proxy_(connection), |
| 24 | lifeline_read_fd_(kInvalidHandle), |
| 25 | lifeline_write_fd_(kInvalidHandle) {} |
| 26 | |
| 27 | PermissionBrokerProxy::~PermissionBrokerProxy() {} |
| 28 | |
| 29 | bool PermissionBrokerProxy::RequestVpnSetup( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 30 | const std::vector<std::string>& user_names, |
| 31 | const std::string& interface) { |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 32 | 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 Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 50 | } catch (const DBus::Error& e) { |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 51 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what(); |
| 52 | } |
| 53 | return return_value; |
| 54 | } |
| 55 | |
| 56 | bool PermissionBrokerProxy::RemoveVpnSetup() { |
Prabhu Kaliamoorthi | e0e205b | 2015-02-20 14:46:23 +0100 | [diff] [blame] | 57 | bool return_value = true; |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 58 | 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 Kaliamoorthi | e0e205b | 2015-02-20 14:46:23 +0100 | [diff] [blame] | 64 | try { |
| 65 | return_value = proxy_.RemoveVpnSetup(); |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 66 | } catch (const DBus::Error& e) { |
Prabhu Kaliamoorthi | e0e205b | 2015-02-20 14:46:23 +0100 | [diff] [blame] | 67 | return_value = false; |
| 68 | LOG(FATAL) << "DBus exception: " << e.name() << ": " << e.what(); |
| 69 | } |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 70 | } |
Prabhu Kaliamoorthi | e0e205b | 2015-02-20 14:46:23 +0100 | [diff] [blame] | 71 | return return_value; |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 74 | PermissionBrokerProxy::Proxy::Proxy(DBus::Connection* connection) |
Prabhu Kaliamoorthi | 77e7683 | 2015-02-13 15:20:23 +0100 | [diff] [blame] | 75 | : DBus::ObjectProxy(*connection, |
| 76 | permission_broker::kPermissionBrokerServicePath, |
| 77 | permission_broker::kPermissionBrokerServiceName) {} |
| 78 | |
| 79 | PermissionBrokerProxy::Proxy::~Proxy() {} |
| 80 | |
| 81 | } // namespace shill |