1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_ASH_COMPONENTS_NETWORK_CELLULAR_INHIBITOR_H_
#define CHROMEOS_ASH_COMPONENTS_NETWORK_CELLULAR_INHIBITOR_H_
#include "base/component_export.h"
#include "base/containers/queue.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/ash/components/network/network_handler_callbacks.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_state_handler_observer.h"
namespace ash {
class DeviceState;
class NetworkStateHandler;
class NetworkDeviceHandler;
// Updates the "Inhibited" property of the Cellular device.
//
// When some SIM-related operations are performed, properties of the Cellular
// device can change to a temporary value and then change back. To prevent churn
// in these properties, Shill provides the "Inhibited" property to inhibit any
// scans.
//
// This class is intended to be used when performing such actions to ensure that
// these transient states never occur.
class COMPONENT_EXPORT(CHROMEOS_NETWORK) CellularInhibitor
: public NetworkStateHandlerObserver {
public:
CellularInhibitor();
CellularInhibitor(const CellularInhibitor&) = delete;
CellularInhibitor& operator=(const CellularInhibitor&) = delete;
~CellularInhibitor() override;
void Init(NetworkStateHandler* network_state_handler,
NetworkDeviceHandler* network_device_handler);
// A lock object which ensures that all other Inhibit requests are blocked
// during this it's lifetime. When a lock object is deleted, the Cellular
// device is automatically uninhibited and any pending inhibit requests are
// processed.
class InhibitLock {
public:
explicit InhibitLock(base::OnceClosure unlock_callback);
~InhibitLock();
private:
base::OnceClosure unlock_callback_;
};
class Observer : public base::CheckedObserver {
public:
~Observer() override = default;
// Invoked when the inhibit state has changed; observers should use the
// GetInhibitReason() function to determine the current state.
virtual void OnInhibitStateChanged() = 0;
};
enum class InhibitReason {
kInstallingProfile,
kRenamingProfile,
kRemovingProfile,
kConnectingToProfile,
kRefreshingProfileList,
kResettingEuiccMemory,
kDisablingProfile,
kRequestingAvailableProfiles,
};
friend std::ostream& operator<<(std::ostream& stream,
const InhibitReason& state);
// Callback which returns InhibitLock on inhibit success or nullptr on
// failure.
using InhibitCallback =
base::OnceCallback<void(std::unique_ptr<InhibitLock>)>;
// This function attempts to put the cellular device into an inhibited state.
// On success, this method will provide a lock to |callback| that will prevent
// the cellular device from becoming uninhibited until the lock is freed. On
// failure, e.g. this function fails to set the corresponding Shill device
// property, |nullptr| is provided to |callback|.
void InhibitCellularScanning(InhibitReason reason, InhibitCallback callback);
// Returns the reason that cellular scanning is currently inhibited, or null
// if it is not inhibited.
std::optional<InhibitReason> GetInhibitReason() const;
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
bool HasObserver(Observer* observer) const;
protected:
void NotifyInhibitStateChanged();
private:
FRIEND_TEST_ALL_PREFIXES(CellularInhibitorTest, SuccessSingleRequest);
FRIEND_TEST_ALL_PREFIXES(CellularInhibitorTest, SuccessMultipleRequests);
FRIEND_TEST_ALL_PREFIXES(CellularInhibitorTest, Failure);
FRIEND_TEST_ALL_PREFIXES(CellularInhibitorTest, FailurePropertySetTimeout);
FRIEND_TEST_ALL_PREFIXES(CellularInhibitorTest, FailureScanningChangeTimeout);
friend class CellularInhibitorTest;
// Timeout after which an inhibit property change is considered to be failed.
static const base::TimeDelta kInhibitPropertyChangeTimeout;
struct InhibitRequest {
InhibitRequest(InhibitReason inhibit_reason,
InhibitCallback inhibit_callback);
InhibitRequest(const InhibitRequest&) = delete;
InhibitRequest& operator=(const InhibitRequest&) = delete;
~InhibitRequest();
InhibitReason inhibit_reason;
InhibitCallback inhibit_callback;
};
enum class State {
kIdle,
kInhibiting,
kWaitForInhibit,
kInhibited,
kUninhibiting,
kWaitForUninhibit,
kWaitingForScanningToStop,
};
friend std::ostream& operator<<(std::ostream& stream, const State& state);
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class InhibitOperationResult {
kSuccess = 0,
kUninhibitTimeout = 1,
kSetInhibitFailed = 2,
kSetInhibitTimeout = 3,
kSetInhibitNoDevice = 4,
kMaxValue = kSetInhibitNoDevice
};
static void RecordInhibitOperationResult(InhibitOperationResult result);
// NetworkStateHandlerObserver:
void DeviceListChanged() override;
void DevicePropertiesUpdated(const DeviceState* device) override;
const DeviceState* GetCellularDevice() const;
void TransitionToState(State state);
void ProcessRequests();
// Called when inhibit completes. |result| is the operation error result and
// is set only for failures.
void OnInhibit(bool success, std::optional<InhibitOperationResult> result);
void AttemptUninhibit();
void OnUninhibit(bool success);
void CheckScanningIfNeeded();
void CheckForScanningStopped();
bool HasScanningStopped();
void OnScanningChangeTimeout();
void CheckInhibitPropertyIfNeeded();
void CheckForInhibit();
void CheckForUninhibit();
void OnInhibitPropertyChangeTimeout();
void PopRequestAndProcessNext();
void SetInhibitProperty();
void OnSetPropertySuccess();
void OnSetPropertyError(bool attempted_inhibit,
const std::string& error_name);
// Returns result of setting inhibit property. |result| is the operation
// error result and is set only for failures.
void ReturnSetInhibitPropertyResult(
bool success,
std::optional<InhibitOperationResult> result);
raw_ptr<NetworkStateHandler> network_state_handler_ = nullptr;
base::ScopedObservation<NetworkStateHandler, NetworkStateHandlerObserver>
network_state_handler_observer_{this};
raw_ptr<NetworkDeviceHandler> network_device_handler_ = nullptr;
State state_ = State::kIdle;
base::queue<std::unique_ptr<InhibitRequest>> inhibit_requests_;
size_t uninhibit_attempts_so_far_ = 0;
base::OneShotTimer set_inhibit_timer_;
base::OneShotTimer scanning_change_timer_;
base::ObserverList<Observer> observer_list_;
base::WeakPtrFactory<CellularInhibitor> weak_ptr_factory_{this};
};
std::ostream& COMPONENT_EXPORT(CHROMEOS_NETWORK) operator<<(
std::ostream& stream,
const ash::CellularInhibitor::State& state);
std::ostream& COMPONENT_EXPORT(CHROMEOS_NETWORK) operator<<(
std::ostream& stream,
const ash::CellularInhibitor::InhibitReason& inhibit_reason);
} // namespace ash
#endif // CHROMEOS_ASH_COMPONENTS_NETWORK_CELLULAR_INHIBITOR_H_
|