blob: 80b4802a5ccf8ffd3cbce03999580ffebf70573c [file] [log] [blame]
Gilad Arnold55f39b72014-01-28 12:51:45 -08001// 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 Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_
Gilad Arnold55f39b72014-01-28 12:51:45 -08007
Gilad Arnold5ef9c482014-03-03 13:51:02 -08008// 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 Arnold55f39b72014-01-28 12:51:45 -080018#include "update_engine/policy_manager/shill_provider.h"
19
Gilad Arnold5ef9c482014-03-03 13:51:02 -080020using chromeos_update_engine::ClockInterface;
21using chromeos_update_engine::DBusWrapperInterface;
Gilad Arnold55f39b72014-01-28 12:51:45 -080022
23namespace chromeos_policy_manager {
24
Gilad Arnold5ef9c482014-03-03 13:51:02 -080025// 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.
27template<typename T>
28class 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.
48class 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 Arnold55f39b72014-01-28 12:51:45 -0800100// ShillProvider concrete implementation.
Gilad Arnold55f39b72014-01-28 12:51:45 -0800101class RealShillProvider : public ShillProvider {
102 public:
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800103 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 Arnold55f39b72014-01-28 12:51:45 -0800107
108 protected:
109 virtual bool DoInit();
110
111 private:
Gilad Arnold55f39b72014-01-28 12:51:45 -0800112 // The time when the connection type last changed.
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800113 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 Arnold55f39b72014-01-28 12:51:45 -0800126
127 DISALLOW_COPY_AND_ASSIGN(RealShillProvider);
128};
129
130} // namespace chromeos_policy_manager
131
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800132#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_