Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 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 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_ |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 7 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 8 | // TODO(garnold) Much of the functionality in this module was adapted from the |
| 9 | // update engine's connection_manager. We need to make sure to deprecate use of |
| 10 | // connection manager when the time comes. |
| 11 | |
| 12 | #include <string> |
| 13 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 14 | #include <base/time/time.h> |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 15 | |
| 16 | #include "update_engine/clock_interface.h" |
| 17 | #include "update_engine/dbus_wrapper_interface.h" |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 18 | #include "update_engine/policy_manager/shill_provider.h" |
| 19 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 20 | using chromeos_update_engine::ClockInterface; |
| 21 | using chromeos_update_engine::DBusWrapperInterface; |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 22 | |
| 23 | namespace chromeos_policy_manager { |
| 24 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 25 | // A DBus connector for making all shill related calls. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 26 | class ShillConnector { |
| 27 | public: |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 28 | // Expected type for the PropertyChanged signal handler. |
| 29 | typedef void (*PropertyChangedHandler)(DBusGProxy*, const char*, GValue*, |
| 30 | void*); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 31 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 32 | ShillConnector(DBusWrapperInterface* dbus, |
| 33 | PropertyChangedHandler signal_handler, void* signal_data) |
| 34 | : dbus_(dbus), signal_handler_(signal_handler), |
| 35 | signal_data_(signal_data) {} |
| 36 | |
| 37 | ~ShillConnector(); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 38 | |
| 39 | // Initializes the DBus connector. Returns |true| on success. |
| 40 | bool Init(); |
| 41 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 42 | // Obtains the type of a network connection described by |service_path|, |
| 43 | // storing it to |*conn_type_p|. Returns |true| on success; |false| on |
| 44 | // failure, in which case no value is written. |
| 45 | bool GetConnectionType(const std::string& service_path, |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 46 | ConnectionType* conn_type_p); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 47 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 48 | // Issues a GetProperties call to shill's manager interface, storing the |
| 49 | // result to |*result_p|. Returns |true| on success. |
| 50 | bool GetManagerProperties(GHashTable** result_p) { |
| 51 | return GetProperties(manager_proxy_, result_p); |
| 52 | } |
| 53 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 54 | private: |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 55 | // Issues a GetProperties call through a given |proxy|, storing the result to |
| 56 | // |*result_p|. Returns |true| on success. |
| 57 | bool GetProperties(DBusGProxy* proxy, GHashTable** result_p); |
| 58 | |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 59 | struct ConnStrToType { |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 60 | const char *str; |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 61 | ConnectionType type; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 62 | }; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 63 | |
| 64 | // A mapping from shill connection type strings to enum values. |
| 65 | static const ConnStrToType shill_conn_str_to_type[]; |
| 66 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 67 | // An initialization flag. |
| 68 | bool is_init_ = false; |
| 69 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 70 | // The DBus interface and connection, and a shill manager proxy. |
| 71 | DBusWrapperInterface* dbus_; |
| 72 | DBusGConnection* connection_ = NULL; |
| 73 | DBusGProxy* manager_proxy_ = NULL; |
| 74 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 75 | // The shill manager signal handler credentials. |
| 76 | PropertyChangedHandler signal_handler_ = NULL; |
| 77 | void* signal_data_ = NULL; |
| 78 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 79 | // Return a DBus proxy for a given |path| and |interface| within shill. |
| 80 | DBusGProxy* GetProxy(const char* path, const char* interface); |
| 81 | |
| 82 | // Converts a shill connection type string into a symbolic value. |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 83 | ConnectionType ParseConnType(const char* str); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 84 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 85 | DISALLOW_COPY_AND_ASSIGN(ShillConnector); |
| 86 | }; |
| 87 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 88 | // ShillProvider concrete implementation. |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 89 | class RealShillProvider : public ShillProvider { |
| 90 | public: |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 91 | RealShillProvider(DBusWrapperInterface* dbus, ClockInterface* clock) |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 92 | : dbus_(dbus), clock_(clock) {} |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 93 | |
| 94 | protected: |
| 95 | virtual bool DoInit(); |
| 96 | |
| 97 | private: |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 98 | // Process a default connection value, update last change time as needed. |
| 99 | bool ProcessDefaultService(GValue* value); |
| 100 | |
| 101 | // A handler for manager PropertyChanged signal, and a static version. |
| 102 | void HandlePropertyChanged(DBusGProxy* proxy, const char *name, |
| 103 | GValue* value); |
| 104 | static void HandlePropertyChangedStatic(DBusGProxy* proxy, const char* name, |
| 105 | GValue* value, void* data); |
| 106 | |
| 107 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 108 | // The time when the connection type last changed. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 109 | base::Time conn_last_changed_; |
| 110 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 111 | // The current connection status. |
| 112 | bool is_connected_; |
| 113 | |
| 114 | // The current default service path, if connected. |
| 115 | std::string default_service_path_; |
| 116 | |
| 117 | // The last known type of the default connection. |
Gilad Arnold | af309d5 | 2014-03-13 11:21:55 -0700 | [diff] [blame] | 118 | ConnectionType conn_type_ = ConnectionType::kUnknown; |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 119 | |
| 120 | // Whether the last known connection type is valid. |
| 121 | bool is_conn_type_valid_ = false; |
| 122 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 123 | // A shill DBus connector. |
| 124 | scoped_ptr<ShillConnector> connector_; |
| 125 | |
| 126 | // The DBus interface object (mockable). |
| 127 | DBusWrapperInterface* const dbus_; |
| 128 | |
| 129 | // A clock abstraction (mockable). |
| 130 | ClockInterface* const clock_; |
| 131 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 132 | friend class ConnTypeVariable; |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(RealShillProvider); |
| 134 | }; |
| 135 | |
| 136 | } // namespace chromeos_policy_manager |
| 137 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 138 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_ |