Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 1 | // Copyright 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 | |
| 5 | #ifndef BUFFET_PRIVET_SHILL_CLIENT_H_ |
| 6 | #define BUFFET_PRIVET_SHILL_CLIENT_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/callback.h> |
| 14 | #include <base/cancelable_callback.h> |
| 15 | #include <base/macros.h> |
| 16 | #include <base/memory/ref_counted.h> |
| 17 | #include <base/memory/weak_ptr.h> |
| 18 | #include <dbus/bus.h> |
| 19 | |
| 20 | #include "shill/dbus-proxies.h" |
| 21 | |
| 22 | namespace privetd { |
| 23 | |
| 24 | enum class ServiceState { |
| 25 | kOffline = 0, |
| 26 | kFailure, |
| 27 | kConnecting, |
| 28 | kConnected, |
| 29 | }; |
| 30 | |
| 31 | std::string ServiceStateToString(ServiceState state); |
| 32 | |
| 33 | class ShillClient final { |
| 34 | public: |
| 35 | // A callback that interested parties can register to be notified of |
| 36 | // transitions from online to offline and vice versa. The boolean |
| 37 | // parameter will be true if we're online, and false if we're offline. |
| 38 | using ConnectivityListener = base::Callback<void(bool)>; |
| 39 | |
| 40 | ShillClient(const scoped_refptr<dbus::Bus>& bus, |
| 41 | const std::set<std::string>& device_whitelist); |
| 42 | ~ShillClient() = default; |
| 43 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 44 | void Init(); |
| 45 | void RegisterConnectivityListener(const ConnectivityListener& listener); |
| 46 | // Causes shill to attempt to connect to the given network with the given |
| 47 | // passphrase. This is accomplished by: |
| 48 | // 1) Configuring a service through the Manager with the SSID and passphrase. |
| 49 | // 2) Calling Connect() on the service. |
| 50 | // 2) Monitoring the returned Service object until we reach an online state, |
| 51 | // an error state, or another call to ConnectToService() occurs. |
| 52 | // Returns false on immediate failures with some descriptive codes in |error|. |
| 53 | bool ConnectToService(const std::string& ssid, |
| 54 | const std::string& passphrase, |
| 55 | const base::Closure& on_success, |
| 56 | chromeos::ErrorPtr* error); |
| 57 | ServiceState GetConnectionState() const; |
| 58 | bool AmOnline() const; |
| 59 | |
| 60 | private: |
| 61 | struct DeviceState { |
| 62 | std::unique_ptr<org::chromium::flimflam::DeviceProxy> device; |
| 63 | // ServiceProxy objects are shared because the connecting service will |
| 64 | // also be the selected service for a device, but is not always the selected |
| 65 | // service (for instance, in the period between configuring a WiFi service |
| 66 | // with credentials, and when Connect() is called.) |
| 67 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> selected_service; |
| 68 | ServiceState service_state{ServiceState::kOffline}; |
| 69 | }; |
| 70 | |
| 71 | bool IsMonitoredDevice(org::chromium::flimflam::DeviceProxy* device); |
| 72 | void OnShillServiceOwnerChange(const std::string& old_owner, |
| 73 | const std::string& new_owner); |
| 74 | void OnManagerPropertyChangeRegistration(const std::string& interface, |
| 75 | const std::string& signal_name, |
| 76 | bool success); |
| 77 | void OnManagerPropertyChange(const std::string& property_name, |
| 78 | const chromeos::Any& property_value); |
| 79 | void OnDevicePropertyChangeRegistration(const dbus::ObjectPath& device_path, |
| 80 | const std::string& interface, |
| 81 | const std::string& signal_name, |
| 82 | bool success); |
| 83 | void OnDevicePropertyChange(const dbus::ObjectPath& device_path, |
| 84 | const std::string& property_name, |
| 85 | const chromeos::Any& property_value); |
| 86 | void OnServicePropertyChangeRegistration(const dbus::ObjectPath& path, |
| 87 | const std::string& interface, |
| 88 | const std::string& signal_name, |
| 89 | bool success); |
| 90 | void OnServicePropertyChange(const dbus::ObjectPath& service_path, |
| 91 | const std::string& property_name, |
| 92 | const chromeos::Any& property_value); |
| 93 | |
| 94 | void OnStateChangeForConnectingService(const dbus::ObjectPath& service_path, |
| 95 | const std::string& state); |
| 96 | void OnStrengthChangeForConnectingService( |
| 97 | const dbus::ObjectPath& service_path, |
| 98 | uint8_t signal_strength); |
| 99 | void OnStateChangeForSelectedService(const dbus::ObjectPath& service_path, |
| 100 | const std::string& state); |
| 101 | void UpdateConnectivityState(); |
| 102 | void NotifyConnectivityListeners(bool am_online); |
| 103 | // Clean up state related to a connecting service. If |
| 104 | // |check_for_reset_pending| is set, then we'll check to see if we've called |
| 105 | // ConnectToService() in the time since a task to call this function was |
| 106 | // posted. |
| 107 | void CleanupConnectingService(bool check_for_reset_pending); |
| 108 | |
| 109 | const scoped_refptr<dbus::Bus> bus_; |
| 110 | org::chromium::flimflam::ManagerProxy manager_proxy_; |
| 111 | // There is logic that assumes we will never change this device list |
| 112 | // in OnManagerPropertyChange. Do not be tempted to remove this const. |
| 113 | const std::set<std::string> device_whitelist_; |
| 114 | std::vector<ConnectivityListener> connectivity_listeners_; |
| 115 | |
| 116 | // State for tracking where we are in our attempts to connect to a service. |
| 117 | bool connecting_service_reset_pending_{false}; |
| 118 | bool have_called_connect_{false}; |
| 119 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> connecting_service_; |
| 120 | base::CancelableClosure on_connect_success_; |
| 121 | |
| 122 | // State for tracking our online connectivity. |
| 123 | std::map<dbus::ObjectPath, DeviceState> devices_; |
| 124 | ServiceState connectivity_state_{ServiceState::kOffline}; |
| 125 | |
| 126 | base::WeakPtrFactory<ShillClient> weak_factory_{this}; |
| 127 | |
| 128 | DISALLOW_COPY_AND_ASSIGN(ShillClient); |
| 129 | }; |
| 130 | |
| 131 | } // namespace privetd |
| 132 | |
| 133 | #endif // BUFFET_PRIVET_SHILL_CLIENT_H_ |