blob: ffb6b0c7e9c127dce062109757754d91d7fb8ab8 [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
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/time/time.h>
Gilad Arnold5ef9c482014-03-03 13:51:02 -080015
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 Arnoldbeb39e92014-03-11 11:34:50 -070025// A DBus connector for making all shill related calls.
Gilad Arnold5ef9c482014-03-03 13:51:02 -080026class ShillConnector {
27 public:
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070028 // Expected type for the PropertyChanged signal handler.
29 typedef void (*PropertyChangedHandler)(DBusGProxy*, const char*, GValue*,
30 void*);
Gilad Arnold5ef9c482014-03-03 13:51:02 -080031
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070032 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 Arnold5ef9c482014-03-03 13:51:02 -080038
39 // Initializes the DBus connector. Returns |true| on success.
40 bool Init();
41
Gilad Arnold5ef9c482014-03-03 13:51:02 -080042 // 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 Arnoldaf309d52014-03-13 11:21:55 -070046 ConnectionType* conn_type_p);
Gilad Arnold5ef9c482014-03-03 13:51:02 -080047
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070048 // 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 Arnold5ef9c482014-03-03 13:51:02 -080054 private:
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070055 // 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 Arnoldae47a9a2014-03-26 12:16:47 -070059 struct ConnStrToType {
Gilad Arnold5ef9c482014-03-03 13:51:02 -080060 const char *str;
Gilad Arnoldaf309d52014-03-13 11:21:55 -070061 ConnectionType type;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070062 };
Gilad Arnold5ef9c482014-03-03 13:51:02 -080063
64 // A mapping from shill connection type strings to enum values.
65 static const ConnStrToType shill_conn_str_to_type[];
66
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070067 // An initialization flag.
68 bool is_init_ = false;
69
Gilad Arnold5ef9c482014-03-03 13:51:02 -080070 // The DBus interface and connection, and a shill manager proxy.
71 DBusWrapperInterface* dbus_;
72 DBusGConnection* connection_ = NULL;
73 DBusGProxy* manager_proxy_ = NULL;
74
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070075 // The shill manager signal handler credentials.
76 PropertyChangedHandler signal_handler_ = NULL;
77 void* signal_data_ = NULL;
78
Gilad Arnold5ef9c482014-03-03 13:51:02 -080079 // 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 Arnoldaf309d52014-03-13 11:21:55 -070083 ConnectionType ParseConnType(const char* str);
Gilad Arnold5ef9c482014-03-03 13:51:02 -080084
Gilad Arnold5ef9c482014-03-03 13:51:02 -080085 DISALLOW_COPY_AND_ASSIGN(ShillConnector);
86};
87
Gilad Arnold55f39b72014-01-28 12:51:45 -080088// ShillProvider concrete implementation.
Gilad Arnold55f39b72014-01-28 12:51:45 -080089class RealShillProvider : public ShillProvider {
90 public:
Gilad Arnold5ef9c482014-03-03 13:51:02 -080091 RealShillProvider(DBusWrapperInterface* dbus, ClockInterface* clock)
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070092 : dbus_(dbus), clock_(clock) {}
Gilad Arnold55f39b72014-01-28 12:51:45 -080093
94 protected:
95 virtual bool DoInit();
96
97 private:
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070098 // 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 Arnold55f39b72014-01-28 12:51:45 -0800108 // The time when the connection type last changed.
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800109 base::Time conn_last_changed_;
110
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700111 // 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 Arnoldaf309d52014-03-13 11:21:55 -0700118 ConnectionType conn_type_ = ConnectionType::kUnknown;
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700119
120 // Whether the last known connection type is valid.
121 bool is_conn_type_valid_ = false;
122
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800123 // 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 Arnoldbeb39e92014-03-11 11:34:50 -0700132 friend class ConnTypeVariable;
Gilad Arnold55f39b72014-01-28 12:51:45 -0800133 DISALLOW_COPY_AND_ASSIGN(RealShillProvider);
134};
135
136} // namespace chromeos_policy_manager
137
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800138#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_