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 | |
| 14 | #include <base/time.h> |
| 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 | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 25 | // A tracker for the last reported value. Whenever Update() is called with a new |
| 26 | // value, the current time is written to a pointed time object. |
| 27 | template<typename T> |
| 28 | class LastValueTracker { |
| 29 | public: |
| 30 | LastValueTracker(ClockInterface* clock, T init_val, base::Time* time_p) |
| 31 | : clock_(clock), last_val_(init_val), time_p_(time_p) {} |
| 32 | |
| 33 | const T& Update(const T& curr_val) { |
| 34 | if (curr_val != last_val_) { |
| 35 | last_val_ = curr_val; |
| 36 | *time_p_ = clock_->GetWallclockTime(); |
| 37 | } |
| 38 | return curr_val; |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | ClockInterface* const clock_; |
| 43 | T last_val_; |
| 44 | base::Time* time_p_; |
| 45 | }; |
| 46 | |
| 47 | // A DBus connector for making shill queries. |
| 48 | class ShillConnector { |
| 49 | public: |
| 50 | ShillConnector(DBusWrapperInterface* dbus) : dbus_(dbus) {} |
| 51 | |
| 52 | ~ShillConnector() { |
| 53 | if (manager_proxy_) |
| 54 | dbus_->ProxyUnref(manager_proxy_); |
| 55 | } |
| 56 | |
| 57 | // Initializes the DBus connector. Returns |true| on success. |
| 58 | bool Init(); |
| 59 | |
| 60 | // Obtains the default network connection, storing the connection status to |
| 61 | // |*is_connected_p| and (if connected) the service path for the default |
| 62 | // connection in |*default_service_path_p|. Returns |true| on success; |false| |
| 63 | // on failure, in which case no values are written. |
| 64 | bool GetDefaultConnection(bool* is_connected_p, |
| 65 | std::string* default_service_path_p); |
| 66 | |
| 67 | // Obtains the type of a network connection described by |service_path|, |
| 68 | // storing it to |*conn_type_p|. Returns |true| on success; |false| on |
| 69 | // failure, in which case no value is written. |
| 70 | bool GetConnectionType(const std::string& service_path, |
| 71 | ShillConnType* conn_type_p); |
| 72 | |
| 73 | private: |
| 74 | typedef struct { |
| 75 | const char *str; |
| 76 | ShillConnType type; |
| 77 | } ConnStrToType; |
| 78 | |
| 79 | // A mapping from shill connection type strings to enum values. |
| 80 | static const ConnStrToType shill_conn_str_to_type[]; |
| 81 | |
| 82 | // The DBus interface and connection, and a shill manager proxy. |
| 83 | DBusWrapperInterface* dbus_; |
| 84 | DBusGConnection* connection_ = NULL; |
| 85 | DBusGProxy* manager_proxy_ = NULL; |
| 86 | |
| 87 | // Return a DBus proxy for a given |path| and |interface| within shill. |
| 88 | DBusGProxy* GetProxy(const char* path, const char* interface); |
| 89 | |
| 90 | // Converts a shill connection type string into a symbolic value. |
| 91 | ShillConnType ParseConnType(const char* str); |
| 92 | |
| 93 | // Issues a GetProperties call through a given |proxy|, storing the result to |
| 94 | // |*result_p|. Returns |true| on success. |
| 95 | bool GetProperties(DBusGProxy* proxy, GHashTable** result_p); |
| 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(ShillConnector); |
| 98 | }; |
| 99 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 100 | // ShillProvider concrete implementation. |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 101 | class RealShillProvider : public ShillProvider { |
| 102 | public: |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 103 | RealShillProvider(DBusWrapperInterface* dbus, ClockInterface* clock) |
| 104 | : conn_last_changed_(clock->GetWallclockTime()), |
| 105 | dbus_(dbus), clock_(clock), |
| 106 | is_connected_tracker_(clock, false, &conn_last_changed_) {} |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 107 | |
| 108 | protected: |
| 109 | virtual bool DoInit(); |
| 110 | |
| 111 | private: |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 112 | // The time when the connection type last changed. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 113 | base::Time conn_last_changed_; |
| 114 | |
| 115 | // A shill DBus connector. |
| 116 | scoped_ptr<ShillConnector> connector_; |
| 117 | |
| 118 | // The DBus interface object (mockable). |
| 119 | DBusWrapperInterface* const dbus_; |
| 120 | |
| 121 | // A clock abstraction (mockable). |
| 122 | ClockInterface* const clock_; |
| 123 | |
| 124 | // Tracker for the latest connection status. |
| 125 | LastValueTracker<bool> is_connected_tracker_; |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 126 | |
| 127 | DISALLOW_COPY_AND_ASSIGN(RealShillProvider); |
| 128 | }; |
| 129 | |
| 130 | } // namespace chromeos_policy_manager |
| 131 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame^] | 132 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_ |