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_WIFI_BOOTSTRAP_MANAGER_H_ |
| 6 | #define BUFFET_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ |
| 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/callback.h> |
| 13 | #include <base/files/file_path.h> |
| 14 | #include <base/macros.h> |
| 15 | #include <base/memory/weak_ptr.h> |
| 16 | #include <base/scoped_observer.h> |
| 17 | |
| 18 | #include "buffet/privet/cloud_delegate.h" |
| 19 | #include "buffet/privet/daemon_state.h" |
| 20 | #include "buffet/privet/privet_types.h" |
| 21 | #include "buffet/privet/wifi_delegate.h" |
| 22 | #include "buffet/privet/wifi_ssid_generator.h" |
| 23 | |
| 24 | namespace privetd { |
| 25 | |
| 26 | class ApManagerClient; |
| 27 | class CloudDelegate; |
| 28 | class DeviceDelegate; |
| 29 | class ShillClient; |
| 30 | |
| 31 | class WifiBootstrapManager : public WifiDelegate, |
| 32 | public CloudDelegate::Observer { |
| 33 | public: |
| 34 | enum State { |
| 35 | kDisabled, |
| 36 | kBootstrapping, |
| 37 | kMonitoring, |
| 38 | kConnecting, |
| 39 | }; |
| 40 | |
| 41 | using StateListener = base::Callback<void(State)>; |
| 42 | |
| 43 | WifiBootstrapManager(DaemonState* state_store, |
| 44 | ShillClient* shill_client, |
| 45 | ApManagerClient* ap_manager_client, |
| 46 | CloudDelegate* gcd, |
| 47 | uint32_t connect_timeout_seconds, |
| 48 | uint32_t bootstrap_timeout_seconds, |
| 49 | uint32_t monitor_timeout_seconds); |
| 50 | ~WifiBootstrapManager() override = default; |
| 51 | virtual void Init(); |
| 52 | void RegisterStateListener(const StateListener& listener); |
| 53 | |
| 54 | // Overrides from WifiDelegate. |
| 55 | const ConnectionState& GetConnectionState() const override; |
| 56 | const SetupState& GetSetupState() const override; |
| 57 | bool ConfigureCredentials(const std::string& ssid, |
| 58 | const std::string& passphrase, |
| 59 | chromeos::ErrorPtr* error) override; |
| 60 | std::string GetCurrentlyConnectedSsid() const override; |
| 61 | std::string GetHostedSsid() const override; |
| 62 | std::set<WifiType> GetTypes() const override; |
| 63 | |
| 64 | // Overrides from CloudDelegate::Observer. |
| 65 | void OnDeviceInfoChanged() override; |
| 66 | |
| 67 | private: |
| 68 | // These Start* tasks: |
| 69 | // 1) Do state appropriate work for entering the indicated state. |
| 70 | // 2) Update the state variable to reflect that we're in a new state |
| 71 | // 3) Call StateListeners to notify that we've transitioned. |
| 72 | // These End* tasks perform cleanup on leaving indicated state. |
| 73 | void StartBootstrapping(); |
| 74 | void EndBootstrapping(); |
| 75 | |
| 76 | void StartConnecting(const std::string& ssid, const std::string& passphrase); |
| 77 | void EndConnecting(); |
| 78 | |
| 79 | void StartMonitoring(); |
| 80 | void EndMonitoring(); |
| 81 | |
| 82 | // Update the current state, post tasks to notify listeners accordingly to |
| 83 | // the MessageLoop. |
| 84 | void UpdateState(State new_state); |
| 85 | void NotifyStateListeners(State new_state) const; |
| 86 | |
| 87 | // If we've been bootstrapped successfully before, and we're bootstrapping |
| 88 | // again because we slipped offline for a sufficiently longtime, we want |
| 89 | // to return to monitoring mode periodically in case our connectivity issues |
| 90 | // were temporary. |
| 91 | void OnBootstrapTimeout(); |
| 92 | void OnConnectTimeout(); |
| 93 | void OnConnectSuccess(const std::string& ssid); |
| 94 | void OnConnectivityChange(bool is_connected); |
| 95 | void OnMonitorTimeout(); |
| 96 | void UpdateConnectionState(); |
| 97 | |
| 98 | // Initialization could be delayed if ssid_generator_ is not ready. |
| 99 | bool is_initialized_{false}; |
| 100 | State state_{kDisabled}; |
| 101 | // Setup state is the temporal state of the most recent bootstrapping attempt. |
| 102 | // It is not persisted to disk. |
| 103 | SetupState setup_state_{SetupState::kNone}; |
| 104 | ConnectionState connection_state_{ConnectionState::kDisabled}; |
| 105 | DaemonState* state_store_; |
| 106 | ShillClient* shill_client_; |
| 107 | ApManagerClient* ap_manager_client_; |
| 108 | WifiSsidGenerator ssid_generator_; |
| 109 | |
| 110 | uint32_t connect_timeout_seconds_; |
| 111 | uint32_t bootstrap_timeout_seconds_; |
| 112 | uint32_t monitor_timeout_seconds_; |
| 113 | std::vector<StateListener> state_listeners_; |
| 114 | bool have_ever_been_bootstrapped_{false}; |
| 115 | bool currently_online_{false}; |
| 116 | std::string last_configured_ssid_; |
| 117 | |
| 118 | ScopedObserver<CloudDelegate, CloudDelegate::Observer> cloud_observer_{this}; |
| 119 | |
| 120 | // Helps to reset irrelevant tasks switching state. |
| 121 | base::WeakPtrFactory<WifiBootstrapManager> tasks_weak_factory_{this}; |
| 122 | |
| 123 | base::WeakPtrFactory<WifiBootstrapManager> lifetime_weak_factory_{this}; |
| 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(WifiBootstrapManager); |
| 126 | }; |
| 127 | |
| 128 | } // namespace privetd |
| 129 | |
| 130 | #endif // BUFFET_PRIVET_WIFI_BOOTSTRAP_MANAGER_H_ |